1 package org.bsf.smartValueObject.container; 2 3 import junit.framework.TestCase; 4 5 import java.util.*; 6 7 import org.bsf.smartValueObject.container.SmartContainer; 8 import org.bsf.smartValueObject.TestVO; 9 import org.bsf.smartValueObject.Versionable; 10 import org.bsf.smartValueObject.Version; 11 import org.bsf.smartValueObject.SmartAccess; 12 13 21 public abstract class AbstractTestSmartContainer extends TestCase { 22 26 private Collection loggingContainer; 27 30 private SmartContainer containerUnderTest; 31 32 35 public void setUp() { 36 loggingContainer = new ArrayList(); 37 containerUnderTest = createContainer(); 38 assertTrue(containerUnderTest != null); 39 } 40 41 44 public void tearDown() { 45 checkIntegrity(); 49 } 50 51 55 protected abstract SmartContainer createContainer(); 56 57 62 protected abstract Object addToContainer(TestVO t); 63 64 69 protected abstract Object removeFromContainer(TestVO t); 70 71 75 protected abstract TestVO getOne(); 76 77 81 protected int getSize() { 82 return loggingContainer.size(); 83 } 84 85 89 protected int randomNumber() { 90 return (getSize() == 0) ? 0 : 91 (int) (Math.random() * getSize()) + 1; 92 } 93 94 99 protected boolean contains(Object o) { 100 return loggingContainer.contains(o); 101 } 102 103 107 protected Iterator iterator() { 108 return loggingContainer.iterator(); 109 } 110 111 114 protected void clear() { 115 loggingContainer.clear(); 116 } 117 118 122 protected Versionable getVersionable() { 123 return new Version(); 124 } 125 126 127 131 protected TestVO deleteOne() { 132 TestVO moribund = getOne(); 133 _removeFromContainer(moribund); 134 return moribund; 135 } 136 137 141 protected TestVO[] deleteMany() { 142 return deleteMany(randomNumber()); 143 } 144 145 150 protected TestVO[] deleteMany(int n) { 151 assertTrue("You can't delete more than " + getSize() + " elements", n<=getSize()); 152 assertTrue("You must delete at least 1 element", n >= 1); 153 TestVO[] a = new TestVO[n]; 154 for (int i = 0; i < n; i++) { 155 TestVO t = getOne(); 156 _removeFromContainer(t); 157 a[i] = t; 158 } 159 return a; 160 } 161 162 protected TestVO createOne() { 163 return createOne(false); 164 } 165 166 protected TestVO[] createMany() { 167 return createMany(false); 168 } 169 170 protected TestVO[] createMany(int n) { 171 return createMany(n, false); 172 } 173 174 178 protected TestVO createOne(boolean clean) { 179 TestVO t = new TestVO(); 180 int id = (int) (Math.random() * Integer.MAX_VALUE); 181 t.setName("New Object no. " + id); 182 t.setId(id); 183 184 if (clean) { 185 t.markClean(); 186 } 187 _addToContainer(t); 188 189 return t; 190 } 191 192 196 protected TestVO[] createMany(boolean clean) { 197 return createMany((int) (Math.random() * 94) + 5, clean); 198 } 199 200 205 protected TestVO[] createMany(int n, boolean clean) { 206 assertTrue("Use sensible parameters", n > 0 && n < 100); 207 TestVO[] a = new TestVO[n]; 208 for (int i=0; i<n; i++) { 209 a[i] = createOne(clean); 210 } 211 return a; 212 } 213 214 219 private void _addToContainer(TestVO t) { 220 loggingContainer.add(t); 221 addToContainer(t); 222 } 223 224 229 private void _removeFromContainer(TestVO t) { 230 loggingContainer.remove(t); 231 removeFromContainer(t); 232 } 233 234 240 private void checkIntegrity() { 241 assertEquals("Container size mismatch", 242 loggingContainer.size(), containerUnderTest.size()); 243 244 int deleted = containerUnderTest.getDeleted(); 245 int created = containerUnderTest.getCreated(); 246 247 Iterator it = SmartAccess.createdIterator(containerUnderTest); 248 int counter = 0; 249 while (it.hasNext()) { 250 counter++; 251 assertTrue("Added object not found in logging container", 253 contains(it.next())); 254 } 255 assertEquals("Created objects count mismatch", created, counter); 256 257 it = SmartAccess.deletedIterator(containerUnderTest); 258 counter = 0; 259 while (it.hasNext()) { 260 counter++; 261 assertTrue("Deleted object was found in logging container", 263 !contains(it.next())); 264 } 265 assertEquals("Deleted objects count mismatch", deleted, counter); 266 } 267 268 274 protected boolean isInArray(TestVO test, TestVO[] testarray) { 275 for (int i = 0; i < testarray.length; i++) { 276 TestVO testVO = testarray[i]; 277 if (test == testVO) 278 return true; 279 } 280 return false; 281 } 282 283 public void testAddandRemove() { 284 createMany(true); 285 TestVO[] deleted = deleteMany(); 286 for (int i = 0; i < deleted.length; i++) { 287 TestVO testVO = deleted[i]; 288 assertTrue(testVO.isDeleted()); 289 } 290 } 291 } 292 | Popular Tags |