1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.repository;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertNull;
27  import static org.junit.Assert.assertTrue;
28  import org.junit.Test;
29  
30  import org.apache.commons.lang.NotImplementedException;
31  
32  import net.sourceforge.domian.entity.Entity;
33  import static net.sourceforge.domian.specification.SpecificationFactory.all;
34  import static net.sourceforge.domian.specification.SpecificationFactory.createSpecificationFor;
35  import static net.sourceforge.domian.specification.SpecificationFactory.entities;
36  import static net.sourceforge.domian.specification.SpecificationFactory.the;
37  import net.sourceforge.domian.test.domain.Customer;
38  import net.sourceforge.domian.test.domain.Order;
39  import static net.sourceforge.domian.test.domain.Testdata.femaleCustomer;
40  import static net.sourceforge.domian.test.domain.Testdata.maleCustomer;
41  import static net.sourceforge.domian.test.domain.Testdata.order22;
42  import static net.sourceforge.domian.test.domain.Testdata.orderWithNoCustomer;
43  
44  
45  public class NullRepositoryTest extends AbstractRepositoryTestCase {
46  
47      @Override
48      protected String getTestRepoRootPath() {
49          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
50      }
51  
52      @Override
53      protected <T extends Entity> Repository<T> getRepository() {
54          return new NullRepository<T>();
55      }
56  
57      @Override
58      protected <T extends Entity> Repository<T> getRepository(Class<T> entityType, String repositoryId) {
59          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
60      }
61  
62      /*
63      @Override
64      protected <T extends Entity> Repository<T> getFakeRepository() {
65          throw new NotImplementedException();
66      }
67      */
68  
69      @Override
70      protected <T extends Entity> PersistentRepository<T> getPersistentRepository(String repositoryId, PersistenceDefinition persistenceDefinition) {
71          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
72      }
73  
74      @Override
75      protected <T extends Entity> PartitionRepository<T> getPartitionRepository() {
76          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
77      }
78  
79      @Override
80      protected <T extends Entity> PartitionRepository<T> getPartitionRepository(Class<T> entityType, String repositoryId) {
81          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
82      }
83  
84      @Override
85      protected <R extends Repository<? extends Entity>> void reset(R repository, int maxNumberOfEntitiesToPurge) {}
86  
87      @Override
88      protected List<PersistenceDefinition> getSupportedPersistenceDefinitions() {
89          throw new UnsupportedOperationException(NOT_APPLICABLE_METHOD_MESSAGE);
90      }
91  
92      @Test
93      public void shouldStoreNoEntities() {
94          Repository<Entity> repo = getRepository();
95          repo.put(null);
96          repo.put(orderWithNoCustomer);
97          repo.put(order22);
98          repo.put(maleCustomer);
99  
100         assertEquals((Long) 0L, repo.count(all(Entity.class)));
101         assertEquals(0, repo.findAll(all(Order.class)).size());
102         assertEquals(0, repo.findAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)).size());
103     }
104 
105     @Test
106     public void shouldStoreNoBulkEntities() {
107         Repository<Entity> repo = getRepository();
108         Collection<Customer> customers = new ArrayList<Customer>();
109         customers.add(maleCustomer);
110         customers.add(femaleCustomer);
111         repo.putAll(customers);
112 
113         assertEquals((Long) 0L, repo.count(all(Entity.class)));
114         assertEquals(0, repo.findAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)).size());
115         assertEquals(0, repo.findAll(all(Customer.class)).size());
116         assertNull(repo.findSingleEntitySpecifiedBy(createSpecificationFor(Customer.class)));
117         assertNull(repo.findSingle(the(Customer.class)));
118     }
119 
120     @Test
121     public void shouldHaveNothingToRemove_1() {
122         Repository<Entity> repo = getRepository();
123         repo.put(null);
124         repo.put(orderWithNoCustomer);
125         repo.put(order22);
126         repo.put(maleCustomer);
127 
128         assertEquals((Long) 0L, repo.removeAll(entities()));
129     }
130 
131     @Test
132     public void shouldHaveNothingToRemove_2() {
133         Repository<Entity> repo = getRepository();
134         Collection<Customer> customers = new ArrayList<Customer>();
135         customers.add(maleCustomer);
136         customers.add(femaleCustomer);
137         repo.putAll(customers);
138 
139         assertEquals((Long) 0L, repo.removeAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)));
140     }
141 
142     @Test
143     public void shouldHaveNothingToRemove_3() {
144         Repository<Entity> repo = getRepository();
145         Collection<Customer> customers = new ArrayList<Customer>();
146         customers.add(maleCustomer);
147         customers.add(femaleCustomer);
148         repo.putAll(customers);
149 
150         assertEquals((Long) 0L, repo.removeAll(all(Customer.class)));
151     }
152 
153     @Test
154     public void shouldHaveNothingToRemove_4() {
155         Repository<Entity> repo = getRepository();
156         repo.put(maleCustomer);
157         assertFalse(repo.remove(maleCustomer));
158     }
159 
160     @Test
161     public void shouldNotReturnNullCollectionsOrIterators() {
162         Repository<Entity> repo = getRepository();
163         assertNotNull(repo.iterate(createSpecificationFor(Customer.class)));
164         assertNotNull(repo.iterateAll(createSpecificationFor(Customer.class)));
165         assertNotNull(repo.iterateAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)));
166         assertNotNull(repo.find(createSpecificationFor(Customer.class)));
167         assertNotNull(repo.findAll(createSpecificationFor(Customer.class)));
168         assertNotNull(repo.findAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)));
169 
170         assertFalse(repo.iterate(createSpecificationFor(Customer.class)).hasNext());
171         assertFalse(repo.iterateAll(createSpecificationFor(Customer.class)).hasNext());
172         assertFalse(repo.iterateAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)).hasNext());
173         assertTrue(repo.find(createSpecificationFor(Customer.class)).isEmpty());
174         assertTrue(repo.findAll(createSpecificationFor(Customer.class)).isEmpty());
175         assertTrue(repo.findAllEntitiesSpecifiedBy(createSpecificationFor(Customer.class)).isEmpty());
176     }
177 }