1 package org.apache.ojb.otm; 2 3 17 18 import org.apache.ojb.broker.*; 19 import org.apache.ojb.odmg.shared.TestClassA; 20 import org.apache.ojb.odmg.shared.TestClassB; 21 import org.apache.ojb.otm.copy.MetadataObjectCopyStrategy; 22 import org.apache.ojb.otm.copy.ObjectCopyStrategy; 23 import org.apache.ojb.otm.copy.ReflectiveObjectCopyStrategy; 24 import org.apache.ojb.otm.copy.SerializeObjectCopyStrategy; 25 import org.apache.ojb.otm.core.Transaction; 26 import org.apache.ojb.otm.lock.LockingException; 27 import org.apache.ojb.junit.OJBTestCase; 28 29 35 public class CopyTest extends OJBTestCase 36 { 37 private static Class CLASS = CopyTest.class; 38 private PersistenceBroker m_pb; 39 private static final int ITERATIONS = 10000; 40 private ObjectCopyStrategy m_mdcs = new MetadataObjectCopyStrategy(); 41 private ObjectCopyStrategy m_scs = new SerializeObjectCopyStrategy(); 42 private ObjectCopyStrategy m_rcs = new ReflectiveObjectCopyStrategy(); 43 private TestKit _kit; 44 private OTMConnection _conn; 45 private Zoo m_zoo; 46 private TestClassA m_tca; 47 private BidirectionalAssociationObjectA m_baoa; 48 49 public CopyTest(String name) 50 { 51 super(name); 52 } 53 54 public void setUp() throws Exception 55 { 56 super.setUp(); 57 ojbChangeReferenceSetting(TestClassA.class, "b", true, true, true, false); 58 ojbChangeReferenceSetting(TestClassB.class, "a", true, true, true, false); 59 m_pb = PersistenceBrokerFactory.defaultPersistenceBroker(); 60 _kit = TestKit.getTestInstance(); 61 _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 62 } 63 64 public void tearDown() throws Exception 65 { 66 m_pb.close(); 67 _conn.close(); 68 _conn = null; 69 super.tearDown(); 70 } 71 72 public static void main(String [] args) 73 { 74 String [] arr = {CLASS.getName()}; 75 junit.textui.TestRunner.main(arr); 76 } 77 78 public void testMetadataCopy() throws LockingException 79 { 80 TestClassA tca = generateTestData(); 81 TestClassB tcb = tca.getB(); 82 internalTest(m_mdcs, tca, tcb); 83 } 84 85 public void testSerializedCopy() throws LockingException 86 { 87 TestClassA tca = generateTestData(); 88 TestClassB tcb = tca.getB(); 89 internalTest(m_scs, tca, tcb); 90 } 91 92 public void testReflectiveCopy() throws LockingException 93 { 94 TestClassA tca = generateTestData(); 95 TestClassB tcb = tca.getB(); 96 internalTest(m_rcs, tca, tcb); 97 } 98 99 private void internalTest(ObjectCopyStrategy strategy, TestClassA a, TestClassB b) 100 { 101 TestClassA copy = (TestClassA) strategy.copy(a, m_pb); 102 assertTrue(a != copy); 103 assertTrue(copy.getOid().equals("someoid")); 104 assertTrue(copy.getValue1().equals("abc")); 105 assertTrue(copy.getValue2().equals("123")); 106 assertTrue(copy.getValue3() == 5); 107 assertTrue(copy.getB() != b); 108 assertTrue(copy.getB().getOid().equals("boid")); 109 assertTrue(copy.getB().getValue1().equals("hi there")); 110 } 111 112 public void testMetadataCopy2() throws LockingException 113 { 114 Zoo zoo = generateZoo(); 115 internalTest2(m_mdcs, zoo); 116 } 117 118 public void testSerializeCopy2() throws LockingException 119 { 120 Zoo zoo = generateZoo(); 121 internalTest2(m_scs, zoo); 122 } 123 124 public void testReflectiveCopy2() throws LockingException 125 { 126 Zoo zoo = generateZoo(); 127 internalTest2(m_rcs, zoo); 128 } 129 130 133 public void testMetadataCopy3() throws LockingException 134 { 135 BidirectionalAssociationObjectA a = generateBidirectional(); 136 internalTest3(m_mdcs, a); 137 } 138 139 public void testSerializeCopy3() throws LockingException 140 { 141 BidirectionalAssociationObjectA a = generateBidirectional(); 142 internalTest3(m_scs, a); 143 } 144 145 public void testReflectiveCopy3() throws LockingException 146 { 147 BidirectionalAssociationObjectA a = generateBidirectional(); 148 internalTest3(m_rcs, a); 149 } 150 151 private void internalTest3(ObjectCopyStrategy strategy, BidirectionalAssociationObjectA a) 152 { 153 BidirectionalAssociationObjectA copy = (BidirectionalAssociationObjectA) strategy.copy(a, m_pb); 154 assertTrue(a != copy); 155 assertTrue(copy.getPk().equals("abc123")); 156 assertTrue(copy.getRelatedB().getPk().equals("xyz987")); 157 } 158 159 private void internalTest2(ObjectCopyStrategy strategy, Zoo zoo) 160 { 161 Zoo copy = (Zoo) strategy.copy(zoo, m_pb); 162 assertTrue(zoo != copy); 163 assertTrue(zoo.getAnimals().size() == copy.getAnimals().size()); 164 } 165 166 private BidirectionalAssociationObjectA generateBidirectional() throws LockingException 167 { 168 if (m_baoa != null) 169 { 170 return m_baoa; 171 } 172 else 173 { 174 Transaction tx = _kit.getTransaction(_conn); 175 tx.begin(); 176 BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA(); 177 a.setPk("abc123"); 178 Identity oid = _conn.getIdentity(a); 179 a = (BidirectionalAssociationObjectA) _conn.getObjectByIdentity(oid); 180 if (a == null) 181 { 182 a = new BidirectionalAssociationObjectA(); 183 a.setPk("abc123"); 184 _conn.makePersistent(a); 185 BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB(); 186 b.setPk("xyz987"); 187 _conn.makePersistent(b); 188 a.setRelatedB(b); 189 b.setRelatedA(a); 190 } 191 tx.commit(); 192 m_baoa = a; 193 return m_baoa; 194 } 195 } 196 197 198 private Zoo generateZoo() throws LockingException 199 { 200 if (m_zoo != null) 201 { 202 return m_zoo; 203 } 204 else 205 { 206 Transaction tx = _kit.getTransaction(_conn); 207 tx.begin(); 208 Zoo zoo = new Zoo(); 209 zoo.setZooId(1234); 210 Identity oid = _conn.getIdentity(zoo); 211 zoo = (Zoo) _conn.getObjectByIdentity(oid); 212 if (zoo == null) 213 { 214 zoo = new Zoo(); 215 zoo.setZooId(1234); 216 _conn.makePersistent(zoo); 217 Mammal mammal = new Mammal(); 218 mammal.setName("molly"); 219 mammal.setNumLegs(4); 220 mammal.setAge(55); 221 zoo.addAnimal(mammal); 222 _conn.makePersistent(mammal); 223 Reptile reptile = new Reptile(); 224 reptile.setColor("green"); 225 reptile.setName("hubert"); 226 reptile.setAge(51); 227 zoo.addAnimal(reptile); 228 _conn.makePersistent(reptile); 229 } 230 tx.commit(); 231 m_zoo = zoo; 232 return m_zoo; 233 } 234 } 235 236 public void testPerformance() throws LockingException 237 { 238 long start = System.currentTimeMillis(); 239 for (int i = 0; i < ITERATIONS; i++) 240 { 241 TestClassA tca = generateTestData(); 242 TestClassB tcb = tca.getB(); 243 TestClassA copy = (TestClassA) m_scs.copy(tca, m_pb); 244 } 245 long stop = System.currentTimeMillis(); 246 System.out.println("testSerializedCopy took: " + (stop - start)); 247 start = System.currentTimeMillis(); 248 for (int i = 0; i < ITERATIONS; i++) 249 { 250 TestClassA tca = generateTestData(); 251 TestClassB tcb = tca.getB(); 252 TestClassA copy = (TestClassA) m_mdcs.copy(tca, m_pb); 253 } 254 stop = System.currentTimeMillis(); 255 System.out.println("testMetadataCopy took: " + (stop - start)); 256 start = System.currentTimeMillis(); 257 for (int i = 0; i < ITERATIONS; i++) 258 { 259 TestClassA tca = generateTestData(); 260 TestClassB tcb = tca.getB(); 261 TestClassA copy = (TestClassA) m_rcs.copy(tca, m_pb); 262 } 263 stop = System.currentTimeMillis(); 264 System.out.println("testReflectiveCopy took: " + (stop - start)); 265 } 266 267 private TestClassA generateTestData() throws LockingException 268 { 269 if (m_tca != null) 270 { 271 return m_tca; 272 } 273 else 274 { 275 Transaction tx = _kit.getTransaction(_conn); 276 tx.begin(); 277 TestClassA tca = new TestClassA(); 278 tca.setOid("someoid"); 279 Identity oid = _conn.getIdentity(tca); 280 tca = (TestClassA) _conn.getObjectByIdentity(oid); 281 if (tca == null) 282 { 283 tca = new TestClassA(); 284 tca.setOid("someoid"); 285 tca.setValue1("abc"); 286 tca.setValue2("123"); 287 tca.setValue3(5); 288 _conn.makePersistent(tca); 289 TestClassB tcb = new TestClassB(); 290 tcb.setOid("boid"); 291 tcb.setValue1("hi there"); 292 _conn.makePersistent(tcb); 293 tca.setB(tcb); 294 } 295 tx.commit(); 296 m_tca = tca; 297 return m_tca; 298 } 299 } 300 } 301 | Popular Tags |