KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > AnonymousFieldsTest


1 package org.apache.ojb.broker;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6
7 import org.apache.commons.lang.SerializationUtils;
8 import org.apache.ojb.broker.ObjectRepository.F1;
9 import org.apache.ojb.broker.metadata.ClassDescriptor;
10 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
11 import org.apache.ojb.broker.query.Criteria;
12 import org.apache.ojb.broker.query.Query;
13 import org.apache.ojb.broker.query.QueryByCriteria;
14 import org.apache.ojb.broker.query.QueryFactory;
15 import org.apache.ojb.junit.PBTestCase;
16
17 /**
18  * This TestClass tests the anonymous fields feature.
19  */

20 public class AnonymousFieldsTest extends PBTestCase
21 {
22     public static void main(String JavaDoc[] args)
23     {
24         String JavaDoc[] arr = {AnonymousFieldsTest.class.getName()};
25         junit.textui.TestRunner.main(arr);
26     }
27
28     public void testHandlingOfMultipleAnonymousFieldPerObject()
29     {
30         String JavaDoc prefix = "testHandlingOfMultipleAnonymousFieldPerObject_" + System.currentTimeMillis() + "_";
31
32         ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
33         parent.setName(prefix + "main_component");
34
35         ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
36         compSub1.setName(prefix + "sub_1");
37
38         ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
39         compSub2.setName(prefix + "sub_2");
40
41         ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
42         compSub2.setName(prefix + "sub_3");
43
44         ObjectRepository.Group group = new ObjectRepository.Group();
45         group.setName(prefix + "test_group");
46
47         compSub1.setParentComponent(parent);
48         compSub2.setParentComponent(parent);
49         compSub3.setParentComponent(parent);
50         ArrayList JavaDoc list = new ArrayList JavaDoc();
51         list.add(compSub1);
52         list.add(compSub2);
53         list.add(compSub3);
54         parent.setChildComponents(list);
55         parent.setGroup(group);
56
57         broker.beginTransaction();
58         broker.store(parent);
59         broker.commitTransaction();
60
61         broker.clearCache();
62         Query query = QueryFactory.newQueryByIdentity(parent);
63         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
64
65         Query groupQuery = QueryFactory.newQueryByIdentity(group);
66         ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
67
68         assertNotNull(parent);
69         assertNotNull(parent.getGroup());
70         assertNotNull(parent.getChildComponents());
71         assertNotNull(parent.getName());
72         assertNotNull(lookedUpGroup);
73
74         assertEquals(3, parent.getChildComponents().size());
75         assertEquals(group.getName(), (parent.getGroup().getName()));
76
77         parent.setName(prefix + "updated_comp_name");
78         parent.setGroup(null);
79
80         broker.beginTransaction();
81         broker.store(parent);
82         broker.commitTransaction();
83
84         broker.clearCache();
85         query = QueryFactory.newQueryByIdentity(parent);
86         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
87
88         assertNotNull(parent);
89         assertNull(parent.getGroup());
90         assertNotNull(parent.getChildComponents());
91         assertNotNull(parent.getName());
92
93         assertEquals(3, parent.getChildComponents().size());
94         assertEquals(prefix + "updated_comp_name", parent.getName());
95
96         broker.beginTransaction();
97         broker.delete(parent);
98         broker.commitTransaction();
99
100         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
101
102         assertNull(parent);
103         groupQuery = QueryFactory.newQueryByIdentity(group);
104         lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
105         assertNotNull(lookedUpGroup);
106     }
107
108     /**
109      * test handling of serialized objects (simulate
110      * handling of objects across different JVM, e.g. AppServer)
111      */

112     public void testSerializedObjectsDelete()
113     {
114         String JavaDoc prefix = "testSerializedObjectsDelete_" + System.currentTimeMillis() + "_";
115
116         ObjectRepository.ComponentIF comp = new ObjectRepository.Component();
117         comp.setName(prefix + "main_component");
118
119         ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
120         compSub1.setName(prefix + "sub_1");
121
122         ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
123         compSub2.setName(prefix + "sub_2");
124
125         ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
126         compSub2.setName(prefix + "sub_3");
127
128         ObjectRepository.Group group = new ObjectRepository.Group();
129         group.setName(prefix + "test_group");
130
131         compSub1.setParentComponent(comp);
132         compSub2.setParentComponent(comp);
133         compSub3.setParentComponent(comp);
134         ArrayList JavaDoc list = new ArrayList JavaDoc();
135         list.add(compSub1);
136         list.add(compSub2);
137         list.add(compSub3);
138         comp.setChildComponents(list);
139         comp.setGroup(group);
140
141         broker.beginTransaction();
142         broker.store(comp);
143         broker.commitTransaction();
144
145         broker.clearCache();
146         Query query = QueryFactory.newQueryByIdentity(comp);
147         comp = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
148
149         Query groupQuery = QueryFactory.newQueryByIdentity(group);
150         ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
151
152         assertNotNull(comp);
153         assertNotNull(comp.getGroup());
154         assertNotNull(comp.getChildComponents());
155         assertNotNull(comp.getName());
156         assertNotNull(lookedUpGroup);
157
158         assertEquals(3, comp.getChildComponents().size());
159         assertEquals(group.getName(), (comp.getGroup().getName()));
160
161         comp.setName(prefix + "updated_comp_name");
162         comp.setGroup(null);
163
164         comp = (ObjectRepository.ComponentIF) SerializationUtils.deserialize(SerializationUtils.serialize(comp));
165
166         broker.beginTransaction();
167         broker.store(comp);
168         broker.commitTransaction();
169
170         broker.clearCache();
171         query = QueryFactory.newQueryByIdentity(comp);
172         comp = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
173
174         assertNotNull(comp);
175         assertNull(comp.getGroup());
176         assertNotNull(comp.getChildComponents());
177         assertNotNull(comp.getName());
178
179         assertEquals(3, comp.getChildComponents().size());
180         assertEquals(prefix + "updated_comp_name", comp.getName());
181
182         //*****************************************
183
// now we generate a deep copy
184
comp = (ObjectRepository.ComponentIF) serializeDeserializeObject(comp);
185         broker.beginTransaction();
186         broker.delete(comp);
187         broker.commitTransaction();
188         //*****************************************
189

190         comp = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
191
192         assertNull(comp);
193         groupQuery = QueryFactory.newQueryByIdentity(group);
194         lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
195         assertNotNull(lookedUpGroup);
196     }
197
198     public void testSerializedObjectsUpdate()
199     {
200         String JavaDoc prefix = "testSerializedObjectsUpdate_" + System.currentTimeMillis() + "_";
201
202         ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
203         parent.setName(prefix + "main_component");
204
205         ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
206         compSub1.setName(prefix + "sub_1");
207
208         ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
209         compSub2.setName(prefix + "sub_2");
210
211         ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
212         compSub2.setName(prefix + "sub_3");
213
214         ObjectRepository.Group group = new ObjectRepository.Group();
215         group.setName(prefix + "test_group");
216
217         compSub1.setParentComponent(parent);
218         compSub2.setParentComponent(parent);
219         compSub3.setParentComponent(parent);
220         ArrayList JavaDoc list = new ArrayList JavaDoc();
221         list.add(compSub1);
222         list.add(compSub2);
223         list.add(compSub3);
224         parent.setChildComponents(list);
225         parent.setGroup(group);
226
227         broker.beginTransaction();
228         broker.store(parent);
229         broker.commitTransaction();
230
231         broker.clearCache();
232         Query query = QueryFactory.newQueryByIdentity(parent);
233         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
234
235         Query groupQuery = QueryFactory.newQueryByIdentity(group);
236         ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
237
238         assertNotNull(parent);
239         assertNotNull(parent.getGroup());
240         assertNotNull(parent.getChildComponents());
241         assertNotNull(parent.getName());
242         assertNotNull(lookedUpGroup);
243
244         assertEquals(3, parent.getChildComponents().size());
245         assertEquals(group.getName(), (parent.getGroup().getName()));
246
247         parent.setName(prefix + "updated_comp_name");
248         parent.setGroup(null);
249
250         //**************************************************
251
// now we generate a deep copy
252
parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
253         broker.beginTransaction();
254         broker.store(parent);
255         broker.commitTransaction();
256         //**************************************************
257

258         query = QueryFactory.newQueryByIdentity(parent);
259         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
260
261         assertNotNull(parent);
262         assertNull(parent.getGroup());
263         assertNotNull(parent.getChildComponents());
264         assertNotNull(parent.getName());
265
266         assertEquals(3, parent.getChildComponents().size());
267         assertEquals(prefix + "updated_comp_name", parent.getName());
268
269         // same with cleared cache
270
broker.clearCache();
271         query = QueryFactory.newQueryByIdentity(parent);
272         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
273
274         assertNotNull(parent);
275         assertNull(parent.getGroup());
276         assertNotNull(parent.getChildComponents());
277         assertNotNull(parent.getName());
278
279         assertEquals(3, parent.getChildComponents().size());
280         assertEquals(prefix + "updated_comp_name", parent.getName());
281
282         broker.beginTransaction();
283         broker.delete(parent);
284         broker.commitTransaction();
285
286         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
287
288         assertNull(parent);
289         groupQuery = QueryFactory.newQueryByIdentity(group);
290         lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
291         assertNotNull(lookedUpGroup);
292     }
293
294     public void testSerializedObjectsRefresh()
295     {
296         String JavaDoc prefix = "testSerializedObjectsRefresh_" + System.currentTimeMillis() + "_";
297         ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
298         parent.setName(prefix + "main_component");
299
300         ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
301         compSub1.setName(prefix + "sub_1");
302
303         ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
304         compSub2.setName(prefix + "sub_2");
305
306         ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
307         compSub2.setName(prefix + "sub_3");
308
309         ObjectRepository.Group group = new ObjectRepository.Group();
310         group.setName(prefix + "test_group");
311
312         compSub1.setParentComponent(parent);
313         compSub2.setParentComponent(parent);
314         compSub3.setParentComponent(parent);
315         ArrayList JavaDoc list = new ArrayList JavaDoc();
316         list.add(compSub1);
317         list.add(compSub2);
318         list.add(compSub3);
319         parent.setChildComponents(list);
320         parent.setGroup(group);
321
322         broker.beginTransaction();
323         broker.store(parent);
324         broker.commitTransaction();
325
326         broker.clearCache();
327         Query query = QueryFactory.newQueryByIdentity(parent);
328         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
329
330         Query groupQuery = QueryFactory.newQueryByIdentity(group);
331         ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
332
333         assertNotNull(parent);
334         assertNotNull(parent.getGroup());
335         assertNotNull(parent.getChildComponents());
336         assertNotNull(parent.getName());
337         assertEquals(3, parent.getChildComponents().size());
338         assertEquals(group.getName(), (parent.getGroup().getName()));
339         ObjectRepository.ComponentIF aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
340         assertNotNull(aChild);
341         assertNotNull(aChild.getParentComponent());
342         assertEquals(parent, aChild.getParentComponent());
343         assertNotNull(lookedUpGroup);
344
345         //*************************************
346
assertNotNull(parent);
347         assertNotNull(parent.getGroup());
348         parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
349         broker.retrieveAllReferences(parent);
350         assertNotNull(parent);
351         /*
352         Now we have a problem! After serialization we can't find the anonymous keys
353         for parent object, because object identity has changed!!
354         This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
355         */

356         assertNotNull(parent.getGroup());
357         //*************************************
358
assertNotNull(parent.getChildComponents());
359         assertNotNull(parent.getName());
360         assertEquals(3, parent.getChildComponents().size());
361         aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
362         assertNotNull(aChild);
363         assertNotNull(aChild.getParentComponent());
364         assertEquals(parent, aChild.getParentComponent());
365
366         broker.beginTransaction();
367         broker.store(parent);
368         broker.commitTransaction();
369
370         // now nothing should happen, because we don't make any changes
371
broker.clearCache();
372         query = QueryFactory.newQueryByIdentity(parent);
373         parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
374         groupQuery = QueryFactory.newQueryByIdentity(group);
375         lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
376         assertNotNull(parent);
377         assertNotNull(parent.getGroup());
378         assertNotNull(parent.getChildComponents());
379         assertNotNull(parent.getName());
380         assertEquals(3, parent.getChildComponents().size());
381         assertEquals(group.getName(), (parent.getGroup().getName()));
382         aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
383         assertNotNull(aChild);
384         assertNotNull(aChild.getParentComponent());
385         assertEquals(parent, aChild.getParentComponent());
386         assertNotNull(lookedUpGroup);
387     }
388
389     public void testSerializedObjectsRefreshWithProxy()
390     {
391         String JavaDoc prefix = "testSerializedObjectsRefreshWithProxy_" + System.currentTimeMillis() + "_";
392
393         ClassDescriptor cld = broker.getClassDescriptor(ObjectRepository.Component.class);
394         ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName("parentComponent");
395         boolean oldState = ord.isLazy();
396         try
397         {
398             ord.setLazy(true);
399             ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
400             parent.setName(prefix + "main_component");
401
402             ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
403             compSub1.setName(prefix + "sub_1");
404
405             ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
406             compSub2.setName(prefix + "sub_2");
407
408             ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
409             compSub2.setName(prefix + "sub_3");
410
411             ObjectRepository.Group group = new ObjectRepository.Group();
412             group.setName(prefix + "test_group");
413
414             compSub1.setParentComponent(parent);
415             compSub2.setParentComponent(parent);
416             compSub3.setParentComponent(parent);
417             ArrayList JavaDoc list = new ArrayList JavaDoc();
418             list.add(compSub1);
419             list.add(compSub2);
420             list.add(compSub3);
421             parent.setChildComponents(list);
422             parent.setGroup(group);
423
424             broker.beginTransaction();
425             broker.store(parent);
426             broker.commitTransaction();
427
428             broker.clearCache();
429             Query query = QueryFactory.newQueryByIdentity(parent);
430             parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
431
432             Query groupQuery = QueryFactory.newQueryByIdentity(group);
433             ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
434
435             assertNotNull(parent);
436             assertNotNull(parent.getGroup());
437             assertNotNull(parent.getChildComponents());
438             assertNotNull(parent.getName());
439             assertEquals(3, parent.getChildComponents().size());
440             assertEquals(group.getName(), (parent.getGroup().getName()));
441             ObjectRepository.ComponentIF aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
442             assertNotNull(aChild);
443             assertNotNull(aChild.getParentComponent());
444             assertEquals(parent, aChild.getParentComponent());
445             assertNotNull(lookedUpGroup);
446
447             //*************************************
448
assertNotNull(parent);
449             assertNotNull(parent.getGroup());
450             parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
451             broker.retrieveAllReferences(parent);
452             assertNotNull(parent);
453             /*
454             Now we have a problem! After serialization we can't find the anonymous keys
455             for parent object, because object identity has changed!!
456             This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
457             */

458             assertNotNull(parent.getGroup());
459             //*************************************
460
assertNotNull(parent.getChildComponents());
461             assertNotNull(parent.getName());
462             assertEquals(3, parent.getChildComponents().size());
463             aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
464             assertNotNull(aChild);
465             assertNotNull(aChild.getParentComponent());
466             assertEquals(parent, aChild.getParentComponent());
467
468             broker.beginTransaction();
469             broker.store(parent);
470             broker.commitTransaction();
471
472             // now nothing should happen, because we don't make any changes
473
broker.clearCache();
474             query = QueryFactory.newQueryByIdentity(parent);
475             parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
476             groupQuery = QueryFactory.newQueryByIdentity(group);
477             lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
478             assertNotNull(parent);
479             assertNotNull(parent.getGroup());
480             assertNotNull(parent.getChildComponents());
481             assertNotNull(parent.getName());
482             assertEquals(3, parent.getChildComponents().size());
483             assertEquals(group.getName(), (parent.getGroup().getName()));
484             aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
485             assertNotNull(aChild);
486             assertNotNull(aChild.getParentComponent());
487             assertEquals(parent, aChild.getParentComponent());
488             assertNotNull(lookedUpGroup);
489         }
490         finally
491         {
492             ord.setLazy(oldState);
493         }
494     }
495
496     /**
497      * write an entry using vertical inheritance and try to read it again. E-F
498      */

499     public void testVerticalInheritanceStoreAndLoad() throws Exception JavaDoc
500     {
501         // produce some test data
502
ObjectRepository.F entry = new ObjectRepository.F();
503         entry.setSomeSuperValue(31415926);
504         entry.setSomeValue(123456);
505         broker.beginTransaction();
506         broker.store(entry);
507         broker.commitTransaction();
508
509         Identity oid = new Identity(entry, broker);
510
511         // clear cache and retrieve a copy from the DB
512
broker.clearCache();
513         ObjectRepository.F copy = (ObjectRepository.F) broker.getObjectByIdentity(oid);
514
515         // check equality
516
assertEquals(entry.getSomeValue(), copy.getSomeValue());
517         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
518     }
519
520     /**
521      * write an entry using vertical inheritance and try to read it again.
522      * E-F-G
523      */

524     public void testVerticalInheritanceStoreAndLoad2() throws Exception JavaDoc
525     {
526         // produce some test data G
527
ObjectRepository.G entry = new ObjectRepository.G();
528         entry.setSomeSuperValue(31415926);
529         entry.setSomeValue(123456);
530         entry.setSomeSubValue(4242);
531         broker.beginTransaction();
532         broker.store(entry);
533         broker.commitTransaction();
534
535         Identity oid = new Identity(entry, broker);
536
537         // clear cache and retrieve a copy from the DB
538
broker.clearCache();
539         ObjectRepository.G copy = (ObjectRepository.G) broker.getObjectByIdentity(oid);
540
541         // check equality
542
assertEquals(entry.getSomeValue(), copy.getSomeValue());
543         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
544         assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
545     }
546
547     /**
548      * write an entry using vertical inheritance and try to read it again.
549      * E-F1-G1, autoincrement id in E
550      */

551     public void testVerticalInheritanceStoreAndLoad3() throws Exception JavaDoc
552     {
553         // produce some test data G1
554
ObjectRepository.G1 entry = new ObjectRepository.G1();
555         entry.setSomeSuperValue(31415926);
556         entry.setSomeValue(123456);
557         entry.setSomeSubValue(4242);
558         broker.beginTransaction();
559         broker.store(entry);
560         broker.commitTransaction();
561
562         Identity oid = new Identity(entry, broker);
563
564         // clear cache and retrieve a copy from the DB
565
broker.clearCache();
566         ObjectRepository.G1 copy = (ObjectRepository.G1) broker.getObjectByIdentity(oid);
567
568         // check equality
569
assertEquals(entry.getSomeValue(), copy.getSomeValue());
570         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
571         assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
572     }
573
574     /**
575      * write an entry using vertical inheritance and try to read it again. E-F
576      */

577     public void testVerticalInheritanceUpdate() throws Exception JavaDoc
578     {
579         // produce some test data
580
ObjectRepository.F entry = new ObjectRepository.F();
581         entry.setSomeSuperValue(2718281);
582         entry.setSomeValue(9999);
583         broker.beginTransaction();
584         broker.store(entry);
585         broker.commitTransaction();
586
587         Identity oid = new Identity(entry, broker);
588
589         entry.setSomeSuperValue(2718282);
590         entry.setSomeValue(10000);
591         broker.beginTransaction();
592         broker.store(entry);
593         broker.commitTransaction();
594
595         // clear cache and retrieve a copy from the DB
596
broker.clearCache();
597         ObjectRepository.F copy = (ObjectRepository.F) broker.getObjectByIdentity(oid);
598
599         // check equality
600
assertEquals(entry.getSomeValue(), copy.getSomeValue());
601         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
602     }
603
604     /**
605      * write an entry using vertical inheritance and try to read it again.
606      * E-F-G
607      */

608     public void testVerticalInheritanceUpdate2() throws Exception JavaDoc
609     {
610         // produce some test data
611
ObjectRepository.G entry = new ObjectRepository.G();
612         entry.setSomeSuperValue(2718281);
613         entry.setSomeValue(9999);
614         entry.setSomeSubValue(8888);
615         broker.beginTransaction();
616         broker.store(entry);
617         broker.commitTransaction();
618
619         Identity oid = new Identity(entry, broker);
620
621         entry.setSomeSuperValue(2718282);
622         entry.setSomeValue(10000);
623         entry.setSomeSubValue(7777);
624         broker.beginTransaction();
625         broker.store(entry);
626         broker.commitTransaction();
627
628         // clear cache and retrieve a copy from the DB
629
broker.clearCache();
630         ObjectRepository.G copy = (ObjectRepository.G) broker.getObjectByIdentity(oid);
631
632         // check equality
633
assertEquals(entry.getSomeValue(), copy.getSomeValue());
634         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
635         assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
636     }
637
638     /**
639      * write an entry using vertical inheritance and try to read it again.
640      * E-F1-G1, autoincrement id in E
641      */

642     public void testVerticalInheritanceUpdate3() throws Exception JavaDoc
643     {
644         // produce some test data
645
ObjectRepository.G1 entry = new ObjectRepository.G1();
646         entry.setSomeSuperValue(2718281);
647         entry.setSomeValue(9999);
648         entry.setSomeSubValue(8888);
649         broker.beginTransaction();
650         broker.store(entry);
651         broker.commitTransaction();
652
653         Identity oid = new Identity(entry, broker);
654
655         entry.setSomeSuperValue(2718282);
656         entry.setSomeValue(10000);
657         entry.setSomeSubValue(7777);
658         broker.beginTransaction();
659         broker.store(entry);
660         broker.commitTransaction();
661
662         // clear cache and retrieve a copy from the DB
663
broker.clearCache();
664         ObjectRepository.G1 copy = (ObjectRepository.G1) broker.getObjectByIdentity(oid);
665
666         // check equality
667
assertEquals(entry.getSomeValue(), copy.getSomeValue());
668         assertEquals(entry.getSomeSuperValue(), copy.getSomeSuperValue());
669         assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
670     }
671
672     /**
673      * Query Attribute of Super class E-F
674      */

675     public void testQuerySuperField_WithCache()
676     {
677         doTestQuerySuperField(false);
678     }
679
680     /**
681      * Query Attribute of Super class E-F
682      */

683     public void testQuerySuperField_ClearedCache()
684     {
685         doTestQuerySuperField(true);
686     }
687
688     /**
689      * Query Attribute of Super class E-F
690      */

691     public void doTestQuerySuperField(boolean clearCache)
692     {
693         int data1 = (int) (Math.random() * Integer.MAX_VALUE);
694         int data2 = (int) (Math.random() * Integer.MAX_VALUE);
695
696         broker.beginTransaction();
697
698         ObjectRepository.F f1 = new ObjectRepository.F();
699         f1.setSomeValue(data1);
700         f1.setSomeSuperValue(data2);
701         broker.store(f1);
702
703         ObjectRepository.F f2 = new ObjectRepository.F();
704         f2.setSomeValue(data1);
705         f2.setSomeSuperValue(data2);
706         broker.store(f2);
707
708         ObjectRepository.F f3 = new ObjectRepository.F();
709         f3.setSomeValue(data1);
710         f3.setSomeSuperValue(data2);
711         broker.store(f3);
712
713         broker.commitTransaction();
714
715         if(clearCache) broker.clearCache();
716
717         Criteria c = new Criteria();
718         c.addEqualTo("someSuperValue", new Integer JavaDoc(data2));
719         Query q = QueryFactory.newQuery(ObjectRepository.F.class, c);
720         Collection JavaDoc result = broker.getCollectionByQuery(q);
721         assertEquals(3, result.size());
722         ObjectRepository.F retF = (ObjectRepository.F) result.iterator().next();
723         assertEquals(data1, retF.getSomeValue());
724         assertEquals(data2, retF.getSomeSuperValue());
725     }
726
727     /**
728      * Query Attribute of Super class E-F-G
729      */

730     public void testQuerySuperField_2_WithCache()
731     {
732         doTestQuerySuperField_2(false);
733     }
734
735     /**
736      * Query Attribute of Super class E-F-G
737      */

738     public void testQuerySuperField_2_ClearedCache()
739     {
740         doTestQuerySuperField_2(true);
741     }
742
743     /**
744      * Query Attribute of Super class E-F-G
745      */

746     public void doTestQuerySuperField_2(boolean clearCache)
747     {
748         int data1 = (int) (Math.random() * Integer.MAX_VALUE);
749         int data2 = (int) (Math.random() * Integer.MAX_VALUE);
750         int data3 = (int) (Math.random() * Integer.MAX_VALUE);
751         broker.beginTransaction();
752         ObjectRepository.G g1 = new ObjectRepository.G();
753         g1.setSomeValue(data1);
754         g1.setSomeSuperValue(data2);
755         g1.setSomeSubValue(data3);
756         broker.store(g1);
757
758         ObjectRepository.G g2 = new ObjectRepository.G();
759         g2.setSomeValue(data1);
760         g2.setSomeSuperValue(data2);
761         g2.setSomeSubValue(data3);
762         broker.store(g2);
763
764         ObjectRepository.G g3 = new ObjectRepository.G();
765         g3.setSomeValue(data1);
766         g3.setSomeSuperValue(data2);
767         g3.setSomeSubValue(data3);
768         broker.store(g3);
769         broker.commitTransaction();
770
771         if(clearCache) broker.clearCache();
772         Criteria c = new Criteria();
773         c.addEqualTo("someSuperValue", new Integer JavaDoc(data2));
774         Query q = QueryFactory.newQuery(ObjectRepository.G.class, c);
775         Collection JavaDoc result = broker.getCollectionByQuery(q);
776         assertEquals(3, result.size());
777         ObjectRepository.G ret = (ObjectRepository.G) result.iterator().next();
778         assertEquals(data1, ret.getSomeValue());
779         assertEquals(data2, ret.getSomeSuperValue());
780         assertEquals(data3, ret.getSomeSubValue());
781     }
782
783     public void testMultipleJoinedInheritanceAndExtents()
784     {
785         ObjectRepository.F1 entry = new ObjectRepository.F1();
786         entry.setSomeSuperValue(1);
787         entry.setSomeValue(2);
788         broker.beginTransaction();
789         broker.store(entry);
790         broker.commitTransaction();
791
792         Integer JavaDoc id = entry.getId();
793         broker.clearCache();
794         entry = (F1) findById(ObjectRepository.F1.class, id.intValue());
795         assertEquals(id, entry.getId());
796         assertEquals(1, entry.getSomeSuperValue());
797         assertEquals(2, entry.getSomeValue());
798     }
799
800     public void testMultipleJoinedInheritanceAndExtentsWithCache()
801     {
802         ObjectRepository.F1 entry = new ObjectRepository.F1();
803         entry.setSomeSuperValue(1);
804         entry.setSomeValue(2);
805         broker.beginTransaction();
806         broker.store(entry);
807         broker.commitTransaction();
808
809         Integer JavaDoc id = entry.getId();
810         entry = (F1) findById(ObjectRepository.F1.class, id.intValue());
811         assertEquals(id, entry.getId());
812         assertEquals(1, entry.getSomeSuperValue());
813         assertEquals(2, entry.getSomeValue());
814     }
815
816     public void testMultipleJoinedInheritanceAndExtents_2()
817     {
818         ObjectRepository.F1 entry = new ObjectRepository.F1();
819         entry.setSomeSuperValue(1);
820         entry.setSomeValue(2);
821         broker.beginTransaction();
822         broker.store(entry);
823         broker.commitTransaction();
824
825         Integer JavaDoc id = entry.getId();
826         broker.clearCache();
827         entry = (F1) findById(ObjectRepository.E.class, id.intValue());
828         assertEquals(id, entry.getId());
829         assertEquals(1, entry.getSomeSuperValue());
830         assertEquals(2, entry.getSomeValue());
831     }
832
833     public void testMultipleJoinedInheritanceAndExtents_2_WithCache()
834     {
835         ObjectRepository.F1 entry = new ObjectRepository.F1();
836         entry.setSomeSuperValue(1);
837         entry.setSomeValue(2);
838         broker.beginTransaction();
839         broker.store(entry);
840         broker.commitTransaction();
841
842         Integer JavaDoc id = entry.getId();
843         entry = (F1) findById(ObjectRepository.E.class, id.intValue());
844         assertEquals(id, entry.getId());
845         assertEquals(1, entry.getSomeSuperValue());
846         assertEquals(2, entry.getSomeValue());
847     }
848
849
850     // --------------------------------------------------------------------------------------
851
// Utiltity methods.
852
private Object JavaDoc serializeDeserializeObject(Serializable JavaDoc obj)
853     {
854         return (ObjectRepository.ComponentIF) SerializationUtils.deserialize(SerializationUtils.serialize(obj));
855     }
856
857     private Object JavaDoc findById(Class JavaDoc type, int id)
858     {
859         Collection JavaDoc result = broker.getCollectionByQuery(createQueryById(type, id));
860         if(result == null || result.size() == 0)
861         {
862             return null;
863         }
864         if(result.size() > 1)
865         {
866             throw new RuntimeException JavaDoc("Unexpected unique id constraint violation ");
867         }
868
869         return result.iterator().next();
870
871     }
872
873     private Query createQueryById(Class JavaDoc type, int id)
874     {
875         Criteria byIdCriteria = new Criteria();
876         byIdCriteria.addEqualTo("id", new Integer JavaDoc(id));
877         return QueryFactory.newQuery(type, byIdCriteria);
878     }
879 }
880
Popular Tags