1 package org.bsf.smartValueObject; 2 3 import org.bsf.smartValueObject.container.SmartCollection; 4 import org.bsf.smartValueObject.container.SmartContainer; 5 import org.bsf.smartValueObject.container.AbstractTestSmartContainer; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 10 15 public class TestSmartAccess extends AbstractTestSmartContainer { 16 private SmartCollection sc; 17 18 protected SmartContainer createContainer() { 19 sc = new SmartCollection(new ArrayList (), getVersionable()); 20 return sc; 21 } 22 23 protected Object addToContainer(TestVO t) { 24 assertTrue(sc.add(t)); 25 return t; 26 } 27 28 protected Object removeFromContainer(TestVO t) { 29 assertTrue(sc.remove(t)); 30 return t; 31 } 32 33 protected TestVO getOne() { 34 assertTrue("Container is empty", sc.size() > 0); 35 return (TestVO) sc.iterator().next(); 36 } 37 38 public void testGetDeleted() { 39 int numCreate = 20; 40 int numDelete = 10; 41 createMany(numCreate, true); 42 TestVO[] dead = deleteMany(numDelete); 43 44 assertEquals("deleteMany() failed", 45 numDelete, dead.length); 46 assertEquals("Invalid delete count", 47 numDelete, sc.getDeleted()); 48 49 Iterator it = SmartAccess.deletedIterator(sc); 50 int counter = 0; 51 loop: while (it.hasNext()) { 52 counter++; 53 TestVO t = (TestVO) it.next(); 54 assertTrue("Object is not flagged as deleted", t.isDeleted()); 55 for (int i=0; i<numDelete; i++) { 56 if (dead[i] == t) { 57 continue loop; 58 } 59 } 60 fail("Not all deleted elements found"); 61 } 62 assertEquals(numDelete, counter); 63 } 64 65 public void testGetCreated() { 66 TestVO[] living1 = createMany(); 67 for (int i = 0; i < living1.length; i++) { 68 TestVO testVO = living1[i]; 69 testVO.markClean(); 70 } 71 int firstCreate = living1.length; 72 int numCreate = 10; 73 74 TestVO[] living2 = createMany(numCreate); 75 76 assertEquals("createMany() failed", 77 numCreate, living2.length); 78 assertEquals("Invalid create count", 79 firstCreate+numCreate, sc.getCreated()); 80 81 Iterator it = SmartAccess.createdIterator(sc); 82 int counter = 0; 83 loop: while (it.hasNext()) { 84 counter++; 85 TestVO t = (TestVO) it.next(); 86 assertTrue("Object is not flagged as created", t.isCreated()); 87 for (int i=0; i<numCreate; i++) { 88 if (living2[i] == t) 89 continue loop; 90 } 91 fail("Not all created elements found"); 92 } 93 assertEquals(numCreate, counter); 94 95 for (int i = 0; i < living1.length; i++) { 98 TestVO testVO = living1[i]; 99 testVO.create(); 100 } 101 } 102 103 public void testIsDirty() { 104 TestVO test = new TestVO(); 105 test.touch(); 106 assertTrue("Test object is not marked dirty", 107 SmartAccess.isDirty(test)); 108 } 109 110 public void testIsCreated() { 111 TestVO test = new TestVO(); 112 test.create(); 113 assertTrue("Test object is not marked created", 114 SmartAccess.isCreated(test)); 115 } 116 117 public void testIsDeleted() { 118 TestVO test = new TestVO(); 119 test.delete(); 120 assertTrue("Test object is not marked deleted", 121 SmartAccess.isDeleted(test)); 122 } 123 124 public void testIsDependantDirtSimple() { 125 TestVO test1 = new TestVO(); 126 TestVO test2 = new TestVO(); 127 test1.setOtherTestVO(test2); 128 129 test1.markClean(); 130 test2.markClean(); 131 132 assertTrue("Dependent object is not marked clean", 133 !SmartAccess.isGraphDirty(test1)); 134 135 test2.touch(); 136 137 assertTrue("Dependent object is not marked dirty", 138 SmartAccess.isGraphDirty(test1)); 139 } 140 141 public void testIsDependantDirtyGraph() { 142 TestVO test1 = new TestVO(); 143 TestVO test2 = new TestVO(); 144 TestVO test3 = new TestVO(); 145 test1.setOtherTestVO(test2); 146 test2.setOtherTestVO(test3); 147 148 test1.markClean(); 149 test2.markClean(); 150 test3.markClean(); 151 152 assertTrue("Dependent object is not marked clean", 153 !SmartAccess.isGraphDirty(test1)); 154 155 test3.touch(); 156 157 assertTrue("Dependent object is not marked dirty", 158 SmartAccess.isGraphDirty(test1)); 159 } 160 161 public void testIsDependantDirtyCyclic() { 162 TestVO test1 = new TestVO(); 163 TestVO test2 = new TestVO(); 164 TestVO test3 = new TestVO(); 165 166 test1.setOtherTestVO(test2); 167 test2.setOtherTestVO(test3); 168 test3.setOtherTestVO(test1); 170 171 test1.markClean(); 172 test2.markClean(); 173 test3.markClean(); 174 175 assertTrue("Dependent object is not marked clean", 176 !SmartAccess.isGraphDirty(test1)); 177 178 test3.touch(); 179 180 assertTrue("Dependent object is not marked dirty", 181 SmartAccess.isGraphDirty(test1)); 182 } 183 184 public void testResetAll() { 185 TestVO test1 = new TestVO(); 186 TestVO test2 = new TestVO(); 187 TestVO test3 = new TestVO(); 188 189 test1.setOtherTestVO(test2); 190 test2.setOtherTestVO(test3); 191 test3.setOtherTestVO(test1); 193 194 SmartAccess.resetGraph(test1); 195 assertTrue(!SmartAccess.isGraphDirty(test1)); 196 } 197 198 public void testAfterAddRemoveShouldBeClean() { 199 TestVO test1 = new TestVO(); 200 TestVO test2 = new TestVO(); 201 202 SmartAccess.reset(test1); 203 test1.addTestVO(test2); 204 test1.removeTestVO(test2); 205 206 assertTrue("Dependent objects should be clean after add-remove", 207 !SmartAccess.isGraphDirty(test1)); 208 } 209 } 210 | Popular Tags |