KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > SwizzleTests


1 package org.apache.ojb.otm;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.sql.Timestamp JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.ojb.broker.Article;
23 import org.apache.ojb.broker.Contract;
24 import org.apache.ojb.broker.Effectiveness;
25 import org.apache.ojb.broker.Identity;
26 import org.apache.ojb.broker.PBFactoryException;
27 import org.apache.ojb.broker.PersistenceBrokerException;
28 import org.apache.ojb.broker.PersistenceBrokerFactory;
29 import org.apache.ojb.broker.ProductGroup;
30 import org.apache.ojb.broker.RelatedToContract;
31 import org.apache.ojb.broker.Version;
32 import org.apache.ojb.broker.query.Criteria;
33 import org.apache.ojb.broker.query.Query;
34 import org.apache.ojb.broker.query.QueryFactory;
35 import org.apache.ojb.odmg.shared.TestClassA;
36 import org.apache.ojb.odmg.shared.TestClassB;
37 import org.apache.ojb.otm.core.Transaction;
38 import org.apache.ojb.otm.core.TransactionException;
39 import org.apache.ojb.otm.lock.LockType;
40 import org.apache.ojb.otm.lock.LockingException;
41 import org.apache.ojb.junit.OJBTestCase;
42
43 /**
44  * User: Matthew Baird
45  * Date: Jun 21, 2003
46  * Time: 3:59:08 PM
47  * @version $Id: SwizzleTests.java,v 1.15.2.4 2005/12/21 22:32:01 tomdz Exp $
48  */

49 public class SwizzleTests extends OJBTestCase
50 {
51     private static Class JavaDoc CLASS = SwizzleTests.class;
52     private TestKit _kit;
53     private OTMConnection _conn;
54     private static final int COUNT = 1;
55     private static final long TIME = System.currentTimeMillis();
56
57     public void setUp() throws Exception JavaDoc
58     {
59         super.setUp();
60         ojbChangeReferenceSetting(TestClassA.class, "b", true, true, true, false);
61         ojbChangeReferenceSetting(TestClassB.class, "a", true, true, true, false);
62         ojbChangeReferenceSetting(ProductGroup.class, "allArticlesInGroup", true, true, true, false);
63         ojbChangeReferenceSetting(Article.class, "productGroup", true, true, true, false);
64         _kit = TestKit.getTestInstance();
65         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
66     }
67
68     public void tearDown() throws Exception JavaDoc
69     {
70         _conn.close();
71         _conn = null;
72         super.tearDown();
73     }
74
75     public static void main(String JavaDoc[] args)
76     {
77         String JavaDoc[] arr = {CLASS.getName()};
78         junit.textui.TestRunner.main(arr);
79     }
80
81     public void testSwizzle() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
82     {
83         deleteAllData();
84         createTestData();
85         /**
86         * first get the contract object.
87         */

88         PersistenceBrokerFactory.defaultPersistenceBroker().clearCache();
89         Transaction tx = _kit.getTransaction(_conn);
90         tx.begin();
91         Criteria crit = new Criteria();
92         crit.addEqualTo("pk", "C" + TIME);
93         Query q = QueryFactory.newQuery(Contract.class, crit);
94         Iterator JavaDoc it = _conn.getIteratorByQuery(q, LockType.WRITE_LOCK);
95         Object JavaDoc retval = null;
96         RelatedToContract r2c = new RelatedToContract();
97         r2c.setPk("R2C" + TIME);
98         r2c.setRelatedValue1("matt");
99         r2c.setRelatedValue2(34);
100         r2c.setRelatedValue3(new Timestamp JavaDoc(TIME));
101         _conn.makePersistent(r2c);
102         while (it.hasNext())
103         {
104             retval = it.next();
105             ((Contract) retval).setRelatedToContract(r2c);
106         }
107         tx.commit();
108         r2c = null;
109         tx = _kit.getTransaction(_conn);
110         tx.begin();
111         crit = new Criteria();
112         crit.addEqualTo("pk", "E" + TIME);
113         q = QueryFactory.newQuery(Effectiveness.class, crit);
114         it = _conn.getIteratorByQuery(q);
115         retval = null;
116         while (it.hasNext())
117         {
118             retval = it.next();
119         }
120         tx.commit();
121         assertTrue("contract object should have a RelatedToContract instance attached", ((Effectiveness) retval).getVersion().getContract().getRelatedToContract() != null);
122     }
123
124      public void testSwizzle3() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
125     {
126         clearTestData();
127         TestClassA a = generateTestData();
128         Transaction tx = _kit.getTransaction(_conn);
129         tx.begin();
130         _conn.makePersistent(a.getB());
131         _conn.makePersistent(a);
132         TestClassB b = a.getB();
133         tx.commit();
134         /**
135         * clear to start test
136         */

137         _conn.invalidateAll();
138         tx = _kit.getTransaction(_conn);
139         tx.begin();
140         /**
141          * load B
142          */

143         Identity oidb = _conn.getIdentity(b);
144         TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
145         assertTrue(b1 != null);
146         /**
147          * load A
148          */

149         Identity oida = _conn.getIdentity(a);
150         TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
151
152         /**
153          * B, as navigated from A, should be the same as B gotten directly.
154          */

155         assertTrue(a1.getB().equals(b1));
156         tx.commit();
157
158         /**
159          * clear
160          */

161         clearTestData();
162     }
163
164     private void createTestData() throws TransactionException, LockingException
165     {
166         for (int i = 0; i < COUNT; i++)
167         {
168             Transaction tx = _kit.getTransaction(_conn);
169             tx.begin();
170             Contract contract = new Contract();
171             contract.setPk("C" + TIME);
172             contract.setContractValue1("contractvalue1");
173             contract.setContractValue2(1);
174             contract.setContractValue3("contractvalue3");
175             contract.setContractValue4(new Timestamp JavaDoc(TIME));
176             _conn.makePersistent(contract);
177             tx.commit();
178             tx = _kit.getTransaction(_conn);
179             tx.begin();
180             Version version = new Version();
181             version.setPk("V" + TIME);
182             version.setVersionValue1("versionvalue1");
183             version.setVersionValue2(1);
184             version.setVersionValue3(new Timestamp JavaDoc(TIME));
185             version.setContract(contract);
186             _conn.makePersistent(version);
187             tx.commit();
188             tx = _kit.getTransaction(_conn);
189             tx.begin();
190             Effectiveness eff = new Effectiveness();
191             eff.setPk("E" + TIME);
192             eff.setEffValue1("effvalue1");
193             eff.setEffValue2(1);
194             eff.setEffValue3(new Timestamp JavaDoc(TIME));
195             eff.setVersion(version);
196             _conn.makePersistent(eff);
197             tx.commit();
198         }
199     }
200
201     public void deleteAllData() throws LockingException
202     {
203         Criteria crit = new Criteria();
204         Query q;
205         Iterator JavaDoc iter;
206         /**
207         * delete effectiveness first
208         */

209         Transaction tx = _kit.getTransaction(_conn);
210         tx.begin();
211         q = QueryFactory.newQuery(Effectiveness.class, crit);
212         iter = _conn.getIteratorByQuery(q);
213         while (iter.hasNext())
214         {
215             _conn.deletePersistent(iter.next());
216         }
217         tx.commit();
218         /**
219         * then version
220         */

221         tx = _kit.getTransaction(_conn);
222         tx.begin();
223         q = QueryFactory.newQuery(Version.class, crit);
224         iter = _conn.getIteratorByQuery(q);
225         while (iter.hasNext())
226         {
227             _conn.deletePersistent(iter.next());
228         }
229         tx.commit();
230         /**
231         * the contract
232         */

233         tx = _kit.getTransaction(_conn);
234         tx.begin();
235         q = QueryFactory.newQuery(Contract.class, crit);
236         iter = _conn.getIteratorByQuery(q);
237         while (iter.hasNext())
238         {
239             _conn.deletePersistent(iter.next());
240         }
241         tx.commit();
242     }
243
244     public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
245     {
246         clearTestData();
247         TestClassA a = generateTestData();
248         Transaction tx = _kit.getTransaction(_conn);
249         tx.begin();
250         _conn.makePersistent(a.getB());
251         _conn.makePersistent(a);
252         tx.commit();
253         /**
254         * clear to start test
255         */

256         _conn.invalidateAll();
257         /**
258         * get A to make it and the related B in cache
259         */

260         tx = _kit.getTransaction(_conn);
261         tx.begin();
262         Identity oid = _conn.getIdentity(a);
263         TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
264         assertTrue(a1.getB() != null);
265         assertTrue(a1.getB().getValue1().equals("hi there"));
266         /**
267         * everything is good, update b
268         */

269         tx.commit();
270
271         /**
272         * now get B and update it, do NOT get it by traversing A
273         */

274         tx = _kit.getTransaction(_conn);
275         tx.begin();
276         Identity boid = _conn.getIdentity(a.getB());
277         TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
278         assertTrue(b1 != null);
279         assertTrue(b1.getValue1().equals("hi there"));
280         /**
281         * everything is good, update b
282         */

283         _conn.lockForWrite(b1);
284         b1.setValue1("goodbye there");
285         tx.commit();
286         /**
287         * make sure b was updated
288         */

289         tx = _kit.getTransaction(_conn);
290         tx.begin();
291         boid = _conn.getIdentity(a.getB());
292         b1 = (TestClassB) _conn.getObjectByIdentity(boid);
293         assertTrue(b1 != null);
294         assertTrue(b1.getValue1().equals("goodbye there"));
295         tx.commit();
296
297         /**
298         * now get A again and make sure the related B is updated to reflect
299         * the new value.
300         */

301         tx = _kit.getTransaction(_conn);
302         tx.begin();
303         TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
304         assertTrue(a2.getB() != null);
305         assertTrue(a2.getB().getValue1().equals("goodbye there"));
306         tx.commit();
307         clearTestData();
308     }
309
310     public void testSwizzleNto1() throws Exception JavaDoc
311     {
312         clearTestData();
313         TestClassA a = generateTestData();
314         TestClassB b2 = generateAnotherB();
315         Transaction tx = _kit.getTransaction(_conn);
316         tx.begin();
317         _conn.makePersistent(a.getB());
318         _conn.makePersistent(a);
319         tx.commit();
320         /**
321          * change B
322          */

323         tx = _kit.getTransaction(_conn);
324         tx.begin();
325         Identity oid = _conn.getIdentity(a);
326         TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
327         _conn.makePersistent(b2);
328         a1.setB(b2);
329         tx.commit();
330
331         tx = _kit.getTransaction(_conn);
332         tx.begin();
333         a = (TestClassA) _conn.getObjectByIdentity(oid);
334         assertTrue(a.getB() != null);
335         assertTrue(a.getB().getValue1().equals("value2"));
336         a.setB(null);
337         tx.commit();
338
339         tx = _kit.getTransaction(_conn);
340         tx.begin();
341         a = (TestClassA) _conn.getObjectByIdentity(oid);
342         assertTrue(a.getB() == null);
343         tx.commit();
344     }
345
346     public void testSwizzle1toN() throws Exception JavaDoc
347     {
348         if (ojbSkipKnownIssueProblem("OTM-layer has caching issues"))
349         {
350             return;
351         }
352         clearTestData();
353         Transaction tx = _kit.getTransaction(_conn);
354         tx.begin();
355         ProductGroup pg = new ProductGroup();
356         pg.setId(new Integer JavaDoc(77777));
357         pg.setName("1");
358         _conn.makePersistent(pg);
359         Article article = Article.createInstance();
360         article.setArticleId(new Integer JavaDoc(77777));
361         article.setStock(333);
362         pg.add(article);
363         article.setProductGroup(pg);
364         _conn.makePersistent(article);
365         Identity pgOid = _conn.getIdentity(pg);
366         tx.commit();
367
368         tx = _kit.getTransaction(_conn);
369         tx.begin();
370         pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
371         pg.getAllArticlesInGroup().clear();
372         tx.commit();
373
374         tx = _kit.getTransaction(_conn);
375         tx.begin();
376         pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
377         assertEquals("should be equal", 0, pg.getAllArticlesInGroup().size());
378         tx.commit();
379
380         tx = _kit.getTransaction(_conn);
381         tx.begin();
382         pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
383         pg.getAllArticlesInGroup().add(article);
384         tx.commit();
385
386         tx = _kit.getTransaction(_conn);
387         tx.begin();
388         pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
389         assertEquals("should be equal", 1, pg.getAllArticlesInGroup().size());
390         tx.commit();
391         clearTestData();
392     }
393
394      public void testSwizzle4() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
395     {
396         clearTestData();
397         TestClassA a = generateTestData();
398         TestClassB b = a.getB();
399         Transaction tx = _kit.getTransaction(_conn);
400
401         tx.begin();
402         _conn.makePersistent(b);
403         _conn.makePersistent(a);
404         b.setA(a);
405         tx.commit();
406         /**
407         * clear to start test
408         */

409         _conn.invalidateAll();
410         tx = _kit.getTransaction(_conn);
411         tx.begin();
412         /**
413          * load B
414          */

415         Identity oidb = _conn.getIdentity(b);
416         TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
417         /**
418          * load A
419          */

420         Identity oida = _conn.getIdentity(a);
421         TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
422         assertTrue(a1 != null);
423         assertTrue(a1.getB().equals(b1));
424         assertTrue(b1.getA().equals(a1));
425         /**
426          * update B
427          */

428         a.setValue1("a");
429         _conn.makePersistent(a);
430
431         /**
432          * B, as navigated from A, should be the same as B gotten directly.
433          */

434         assertTrue(a1.getValue1().equals(a.getValue1()));
435         tx.commit();
436
437         /**
438          * clear
439          */

440         clearTestData();
441     }
442
443     /**
444      * Cache data must be independent of any objects available to used,
445      * otherwise modification of user objects outside transaction will
446      * damage cache data
447      */

448     public void testCacheIndependence() throws Throwable JavaDoc {
449         Transaction tx = null;
450         Collection JavaDoc addresses = this.getAddresses();
451         deleteAddresses(addresses);
452         Identity oid;
453         Address address = new Address("oldCountry", "oldCity", "oldStreet");
454
455         try {
456             tx = _kit.getTransaction(_conn);
457             tx.begin();
458             _conn.makePersistent(address);
459             oid = _conn.getIdentity(address);
460             tx.commit();
461
462             address.setStreet("newStreet");
463
464             tx = _kit.getTransaction(_conn);
465             tx.begin();
466             address = (Address) _conn.getObjectByIdentity(oid);
467             assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
468             tx.commit();
469
470             address.setStreet("newStreet");
471
472             tx = _kit.getTransaction(_conn);
473             tx.begin();
474             address = (Address) _conn.getObjectByIdentity(oid, LockType.WRITE_LOCK);
475             assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
476             tx.commit();
477
478             address.setStreet("newStreet");
479
480             tx = _kit.getTransaction(_conn);
481             tx.begin();
482             address = (Address) _conn.getObjectByIdentity(oid);
483             assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
484             tx.commit();
485         } catch (Throwable JavaDoc e) {
486             if (tx != null) {
487                 try {
488                     tx.rollback();
489                 } catch (Throwable JavaDoc ex) {
490                     ex.printStackTrace();
491                 }
492             }
493             throw e;
494         }
495     }
496
497     public void testSomethingSimple() throws Throwable JavaDoc {
498         Collection JavaDoc addresses = this.getAddresses();
499         addresses = deleteAddresses(addresses);
500
501         addresses.add(new Address("oldCountry", "oldCity", "oldStreet"));
502
503         addresses = this.updateAddresses(addresses);
504
505         Iterator JavaDoc iter = addresses.iterator();
506         while (iter.hasNext()) {
507             Address address = (Address)iter.next();
508             address.setStreet("newStreet");
509         }
510         addresses = this.updateAddresses(addresses);
511         addresses = this.getAddresses();
512         assertEquals("Collection of addresses must be 1. ", 1, addresses.size());
513         iter = addresses.iterator();
514         while (iter.hasNext()) {
515             Address address = (Address)iter.next();
516             assertEquals("New street not set.", "newStreet",
517                                                 address.getStreet());
518         }
519     }
520
521     private Collection JavaDoc getAddresses() throws Throwable JavaDoc {
522         Transaction tx = null;
523         Collection JavaDoc addresses;
524         try {
525             tx = _kit.getTransaction(_conn);
526             tx.begin();
527             _conn.invalidateAll();
528             Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
529             addresses = _conn.getCollectionByQuery(q);
530             tx.commit();
531         } catch (Throwable JavaDoc e) {
532             if (tx != null) {
533                 try {
534                     tx.rollback();
535                 } catch (Throwable JavaDoc ex) {
536                     ex.printStackTrace();
537                 }
538             }
539             throw e;
540         }
541         return addresses;
542     }
543
544     private Collection JavaDoc updateAddresses(Collection JavaDoc newAddresses)
545             throws Throwable JavaDoc {
546         Transaction tx = null;
547         Collection JavaDoc oldAddresses;
548         try {
549             tx = _kit.getTransaction(_conn);
550             tx.begin();
551
552             Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
553             oldAddresses = _conn.getCollectionByQuery(q);
554
555             Iterator JavaDoc oldAddressesIterator = oldAddresses.iterator();
556             while (oldAddressesIterator.hasNext()) {
557                 Address oldAddress = (Address)oldAddressesIterator.next();
558                 if (!newAddresses.contains(oldAddress)) {
559                     _conn.deletePersistent(oldAddress);
560                 }
561             }
562
563             Iterator JavaDoc newAddressesIterator = newAddresses.iterator();
564             while (newAddressesIterator.hasNext()) {
565                 Address newAddress = (Address)newAddressesIterator.next();
566                 _conn.makePersistent(newAddress);
567             }
568             tx.commit();
569         } catch (Throwable JavaDoc e) {
570             if (tx != null) {
571                 try {
572                     tx.rollback();
573                 } catch (Throwable JavaDoc ex) {
574                     ex.printStackTrace();
575                 }
576             }
577             throw e;
578         }
579         return newAddresses;
580     }
581
582     private Collection JavaDoc deleteAddresses(Collection JavaDoc oldAddresses)
583             throws Throwable JavaDoc {
584         Transaction tx = null;
585         try {
586             tx = _kit.getTransaction(_conn);
587             tx.begin();
588
589             Iterator JavaDoc oldAddressesIterator = oldAddresses.iterator();
590             while (oldAddressesIterator.hasNext()) {
591                 Address oldAddress = (Address)oldAddressesIterator.next();
592                 _conn.deletePersistent(oldAddress);
593                 oldAddressesIterator.remove();
594             }
595             tx.commit();
596         } catch (Throwable JavaDoc e) {
597             if (tx != null) {
598                 try {
599                     tx.rollback();
600                 } catch (Throwable JavaDoc ex) {
601                     ex.printStackTrace();
602                 }
603             }
604             throw e;
605         }
606         return oldAddresses;
607     }
608
609     private void clearTestData() throws LockingException
610     {
611         TestClassA a = generateTestData();
612         TestClassB b2 = generateAnotherB();
613         Transaction tx = _kit.getTransaction(_conn);
614         tx.begin();
615         Identity oid = _conn.getIdentity(a);
616         Identity oidb = _conn.getIdentity(a.getB());
617         Identity oidb2 = _conn.getIdentity(b2);
618         TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
619         if (a1 != null)
620         {
621             _conn.deletePersistent(a1);
622         }
623         TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
624         if (b1 != null)
625         {
626             _conn.deletePersistent(b1);
627         }
628         b2 = (TestClassB) _conn.getObjectByIdentity(oidb2);
629         if (b2 != null)
630         {
631             _conn.deletePersistent(b2);
632         }
633
634         Article article = Article.createInstance();
635         article.setArticleId(new Integer JavaDoc(77777));
636         ProductGroup pg = new ProductGroup();
637         pg.setId(new Integer JavaDoc(77777));
638         Identity oidArt = _conn.getIdentity(article);
639         Identity oidPG = _conn.getIdentity(pg);
640         article = (Article) _conn.getObjectByIdentity(oidArt);
641         if (article != null)
642         {
643             _conn.deletePersistent(article);
644         }
645         pg = (ProductGroup) _conn.getObjectByIdentity(oidPG);
646         if (pg != null)
647         {
648             _conn.deletePersistent(pg);
649         }
650         tx.commit();
651     }
652
653     private TestClassA generateTestData()
654     {
655         TestClassA tca = new TestClassA();
656         tca.setOid("someoid");
657         tca.setValue1("abc");
658         tca.setValue2("123");
659         tca.setValue3(5);
660         TestClassB tcb = new TestClassB();
661         tcb.setOid("boid");
662         tcb.setValue1("hi there");
663         tca.setB(tcb);
664         return tca;
665     }
666
667     private TestClassB generateAnotherB()
668     {
669         TestClassB tcb = new TestClassB();
670         tcb.setOid("boid2");
671         tcb.setValue1("value2");
672         return tcb;
673     }
674 }
675
Popular Tags