001: package org.apache.ojb.broker;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Collection;
006:
007: import org.apache.commons.lang.SerializationUtils;
008: import org.apache.ojb.broker.ObjectRepository.F1;
009: import org.apache.ojb.broker.metadata.ClassDescriptor;
010: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
011: import org.apache.ojb.broker.query.Criteria;
012: import org.apache.ojb.broker.query.Query;
013: import org.apache.ojb.broker.query.QueryByCriteria;
014: import org.apache.ojb.broker.query.QueryFactory;
015: import org.apache.ojb.junit.PBTestCase;
016:
017: /**
018: * This TestClass tests the anonymous fields feature.
019: */
020: public class AnonymousFieldsTest extends PBTestCase {
021: public static void main(String[] args) {
022: String[] arr = { AnonymousFieldsTest.class.getName() };
023: junit.textui.TestRunner.main(arr);
024: }
025:
026: public void testHandlingOfMultipleAnonymousFieldPerObject() {
027: String prefix = "testHandlingOfMultipleAnonymousFieldPerObject_"
028: + System.currentTimeMillis() + "_";
029:
030: ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
031: parent.setName(prefix + "main_component");
032:
033: ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
034: compSub1.setName(prefix + "sub_1");
035:
036: ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
037: compSub2.setName(prefix + "sub_2");
038:
039: ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
040: compSub2.setName(prefix + "sub_3");
041:
042: ObjectRepository.Group group = new ObjectRepository.Group();
043: group.setName(prefix + "test_group");
044:
045: compSub1.setParentComponent(parent);
046: compSub2.setParentComponent(parent);
047: compSub3.setParentComponent(parent);
048: ArrayList list = new ArrayList();
049: list.add(compSub1);
050: list.add(compSub2);
051: list.add(compSub3);
052: parent.setChildComponents(list);
053: parent.setGroup(group);
054:
055: broker.beginTransaction();
056: broker.store(parent);
057: broker.commitTransaction();
058:
059: broker.clearCache();
060: Query query = QueryFactory.newQueryByIdentity(parent);
061: parent = (ObjectRepository.ComponentIF) broker
062: .getObjectByQuery(query);
063:
064: Query groupQuery = QueryFactory.newQueryByIdentity(group);
065: ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker
066: .getObjectByQuery(groupQuery);
067:
068: assertNotNull(parent);
069: assertNotNull(parent.getGroup());
070: assertNotNull(parent.getChildComponents());
071: assertNotNull(parent.getName());
072: assertNotNull(lookedUpGroup);
073:
074: assertEquals(3, parent.getChildComponents().size());
075: assertEquals(group.getName(), (parent.getGroup().getName()));
076:
077: parent.setName(prefix + "updated_comp_name");
078: parent.setGroup(null);
079:
080: broker.beginTransaction();
081: broker.store(parent);
082: broker.commitTransaction();
083:
084: broker.clearCache();
085: query = QueryFactory.newQueryByIdentity(parent);
086: parent = (ObjectRepository.ComponentIF) broker
087: .getObjectByQuery(query);
088:
089: assertNotNull(parent);
090: assertNull(parent.getGroup());
091: assertNotNull(parent.getChildComponents());
092: assertNotNull(parent.getName());
093:
094: assertEquals(3, parent.getChildComponents().size());
095: assertEquals(prefix + "updated_comp_name", parent.getName());
096:
097: broker.beginTransaction();
098: broker.delete(parent);
099: broker.commitTransaction();
100:
101: parent = (ObjectRepository.ComponentIF) broker
102: .getObjectByQuery(query);
103:
104: assertNull(parent);
105: groupQuery = QueryFactory.newQueryByIdentity(group);
106: lookedUpGroup = (ObjectRepository.Group) broker
107: .getObjectByQuery(groupQuery);
108: assertNotNull(lookedUpGroup);
109: }
110:
111: /**
112: * test handling of serialized objects (simulate
113: * handling of objects across different JVM, e.g. AppServer)
114: */
115: public void testSerializedObjectsDelete() {
116: String prefix = "testSerializedObjectsDelete_"
117: + System.currentTimeMillis() + "_";
118:
119: ObjectRepository.ComponentIF comp = new ObjectRepository.Component();
120: comp.setName(prefix + "main_component");
121:
122: ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
123: compSub1.setName(prefix + "sub_1");
124:
125: ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
126: compSub2.setName(prefix + "sub_2");
127:
128: ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
129: compSub2.setName(prefix + "sub_3");
130:
131: ObjectRepository.Group group = new ObjectRepository.Group();
132: group.setName(prefix + "test_group");
133:
134: compSub1.setParentComponent(comp);
135: compSub2.setParentComponent(comp);
136: compSub3.setParentComponent(comp);
137: ArrayList list = new ArrayList();
138: list.add(compSub1);
139: list.add(compSub2);
140: list.add(compSub3);
141: comp.setChildComponents(list);
142: comp.setGroup(group);
143:
144: broker.beginTransaction();
145: broker.store(comp);
146: broker.commitTransaction();
147:
148: broker.clearCache();
149: Query query = QueryFactory.newQueryByIdentity(comp);
150: comp = (ObjectRepository.ComponentIF) broker
151: .getObjectByQuery(query);
152:
153: Query groupQuery = QueryFactory.newQueryByIdentity(group);
154: ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker
155: .getObjectByQuery(groupQuery);
156:
157: assertNotNull(comp);
158: assertNotNull(comp.getGroup());
159: assertNotNull(comp.getChildComponents());
160: assertNotNull(comp.getName());
161: assertNotNull(lookedUpGroup);
162:
163: assertEquals(3, comp.getChildComponents().size());
164: assertEquals(group.getName(), (comp.getGroup().getName()));
165:
166: comp.setName(prefix + "updated_comp_name");
167: comp.setGroup(null);
168:
169: comp = (ObjectRepository.ComponentIF) SerializationUtils
170: .deserialize(SerializationUtils.serialize(comp));
171:
172: broker.beginTransaction();
173: broker.store(comp);
174: broker.commitTransaction();
175:
176: broker.clearCache();
177: query = QueryFactory.newQueryByIdentity(comp);
178: comp = (ObjectRepository.ComponentIF) broker
179: .getObjectByQuery(query);
180:
181: assertNotNull(comp);
182: assertNull(comp.getGroup());
183: assertNotNull(comp.getChildComponents());
184: assertNotNull(comp.getName());
185:
186: assertEquals(3, comp.getChildComponents().size());
187: assertEquals(prefix + "updated_comp_name", comp.getName());
188:
189: //*****************************************
190: // now we generate a deep copy
191: comp = (ObjectRepository.ComponentIF) serializeDeserializeObject(comp);
192: broker.beginTransaction();
193: broker.delete(comp);
194: broker.commitTransaction();
195: //*****************************************
196:
197: comp = (ObjectRepository.ComponentIF) broker
198: .getObjectByQuery(query);
199:
200: assertNull(comp);
201: groupQuery = QueryFactory.newQueryByIdentity(group);
202: lookedUpGroup = (ObjectRepository.Group) broker
203: .getObjectByQuery(groupQuery);
204: assertNotNull(lookedUpGroup);
205: }
206:
207: public void testSerializedObjectsUpdate() {
208: String prefix = "testSerializedObjectsUpdate_"
209: + System.currentTimeMillis() + "_";
210:
211: ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
212: parent.setName(prefix + "main_component");
213:
214: ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
215: compSub1.setName(prefix + "sub_1");
216:
217: ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
218: compSub2.setName(prefix + "sub_2");
219:
220: ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
221: compSub2.setName(prefix + "sub_3");
222:
223: ObjectRepository.Group group = new ObjectRepository.Group();
224: group.setName(prefix + "test_group");
225:
226: compSub1.setParentComponent(parent);
227: compSub2.setParentComponent(parent);
228: compSub3.setParentComponent(parent);
229: ArrayList list = new ArrayList();
230: list.add(compSub1);
231: list.add(compSub2);
232: list.add(compSub3);
233: parent.setChildComponents(list);
234: parent.setGroup(group);
235:
236: broker.beginTransaction();
237: broker.store(parent);
238: broker.commitTransaction();
239:
240: broker.clearCache();
241: Query query = QueryFactory.newQueryByIdentity(parent);
242: parent = (ObjectRepository.ComponentIF) broker
243: .getObjectByQuery(query);
244:
245: Query groupQuery = QueryFactory.newQueryByIdentity(group);
246: ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker
247: .getObjectByQuery(groupQuery);
248:
249: assertNotNull(parent);
250: assertNotNull(parent.getGroup());
251: assertNotNull(parent.getChildComponents());
252: assertNotNull(parent.getName());
253: assertNotNull(lookedUpGroup);
254:
255: assertEquals(3, parent.getChildComponents().size());
256: assertEquals(group.getName(), (parent.getGroup().getName()));
257:
258: parent.setName(prefix + "updated_comp_name");
259: parent.setGroup(null);
260:
261: //**************************************************
262: // now we generate a deep copy
263: parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
264: broker.beginTransaction();
265: broker.store(parent);
266: broker.commitTransaction();
267: //**************************************************
268:
269: query = QueryFactory.newQueryByIdentity(parent);
270: parent = (ObjectRepository.ComponentIF) broker
271: .getObjectByQuery(query);
272:
273: assertNotNull(parent);
274: assertNull(parent.getGroup());
275: assertNotNull(parent.getChildComponents());
276: assertNotNull(parent.getName());
277:
278: assertEquals(3, parent.getChildComponents().size());
279: assertEquals(prefix + "updated_comp_name", parent.getName());
280:
281: // same with cleared cache
282: broker.clearCache();
283: query = QueryFactory.newQueryByIdentity(parent);
284: parent = (ObjectRepository.ComponentIF) broker
285: .getObjectByQuery(query);
286:
287: assertNotNull(parent);
288: assertNull(parent.getGroup());
289: assertNotNull(parent.getChildComponents());
290: assertNotNull(parent.getName());
291:
292: assertEquals(3, parent.getChildComponents().size());
293: assertEquals(prefix + "updated_comp_name", parent.getName());
294:
295: broker.beginTransaction();
296: broker.delete(parent);
297: broker.commitTransaction();
298:
299: parent = (ObjectRepository.ComponentIF) broker
300: .getObjectByQuery(query);
301:
302: assertNull(parent);
303: groupQuery = QueryFactory.newQueryByIdentity(group);
304: lookedUpGroup = (ObjectRepository.Group) broker
305: .getObjectByQuery(groupQuery);
306: assertNotNull(lookedUpGroup);
307: }
308:
309: public void testSerializedObjectsRefresh() {
310: String prefix = "testSerializedObjectsRefresh_"
311: + System.currentTimeMillis() + "_";
312: ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
313: parent.setName(prefix + "main_component");
314:
315: ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
316: compSub1.setName(prefix + "sub_1");
317:
318: ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
319: compSub2.setName(prefix + "sub_2");
320:
321: ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
322: compSub2.setName(prefix + "sub_3");
323:
324: ObjectRepository.Group group = new ObjectRepository.Group();
325: group.setName(prefix + "test_group");
326:
327: compSub1.setParentComponent(parent);
328: compSub2.setParentComponent(parent);
329: compSub3.setParentComponent(parent);
330: ArrayList list = new ArrayList();
331: list.add(compSub1);
332: list.add(compSub2);
333: list.add(compSub3);
334: parent.setChildComponents(list);
335: parent.setGroup(group);
336:
337: broker.beginTransaction();
338: broker.store(parent);
339: broker.commitTransaction();
340:
341: broker.clearCache();
342: Query query = QueryFactory.newQueryByIdentity(parent);
343: parent = (ObjectRepository.ComponentIF) broker
344: .getObjectByQuery(query);
345:
346: Query groupQuery = QueryFactory.newQueryByIdentity(group);
347: ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker
348: .getObjectByQuery(groupQuery);
349:
350: assertNotNull(parent);
351: assertNotNull(parent.getGroup());
352: assertNotNull(parent.getChildComponents());
353: assertNotNull(parent.getName());
354: assertEquals(3, parent.getChildComponents().size());
355: assertEquals(group.getName(), (parent.getGroup().getName()));
356: ObjectRepository.ComponentIF aChild = (ObjectRepository.ComponentIF) parent
357: .getChildComponents().iterator().next();
358: assertNotNull(aChild);
359: assertNotNull(aChild.getParentComponent());
360: assertEquals(parent, aChild.getParentComponent());
361: assertNotNull(lookedUpGroup);
362:
363: //*************************************
364: assertNotNull(parent);
365: assertNotNull(parent.getGroup());
366: parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
367: broker.retrieveAllReferences(parent);
368: assertNotNull(parent);
369: /*
370: Now we have a problem! After serialization we can't find the anonymous keys
371: for parent object, because object identity has changed!!
372: This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
373: */
374: assertNotNull(parent.getGroup());
375: //*************************************
376: assertNotNull(parent.getChildComponents());
377: assertNotNull(parent.getName());
378: assertEquals(3, parent.getChildComponents().size());
379: aChild = (ObjectRepository.ComponentIF) parent
380: .getChildComponents().iterator().next();
381: assertNotNull(aChild);
382: assertNotNull(aChild.getParentComponent());
383: assertEquals(parent, aChild.getParentComponent());
384:
385: broker.beginTransaction();
386: broker.store(parent);
387: broker.commitTransaction();
388:
389: // now nothing should happen, because we don't make any changes
390: broker.clearCache();
391: query = QueryFactory.newQueryByIdentity(parent);
392: parent = (ObjectRepository.ComponentIF) broker
393: .getObjectByQuery(query);
394: groupQuery = QueryFactory.newQueryByIdentity(group);
395: lookedUpGroup = (ObjectRepository.Group) broker
396: .getObjectByQuery(groupQuery);
397: assertNotNull(parent);
398: assertNotNull(parent.getGroup());
399: assertNotNull(parent.getChildComponents());
400: assertNotNull(parent.getName());
401: assertEquals(3, parent.getChildComponents().size());
402: assertEquals(group.getName(), (parent.getGroup().getName()));
403: aChild = (ObjectRepository.ComponentIF) parent
404: .getChildComponents().iterator().next();
405: assertNotNull(aChild);
406: assertNotNull(aChild.getParentComponent());
407: assertEquals(parent, aChild.getParentComponent());
408: assertNotNull(lookedUpGroup);
409: }
410:
411: public void testSerializedObjectsRefreshWithProxy() {
412: String prefix = "testSerializedObjectsRefreshWithProxy_"
413: + System.currentTimeMillis() + "_";
414:
415: ClassDescriptor cld = broker
416: .getClassDescriptor(ObjectRepository.Component.class);
417: ObjectReferenceDescriptor ord = cld
418: .getObjectReferenceDescriptorByName("parentComponent");
419: boolean oldState = ord.isLazy();
420: try {
421: ord.setLazy(true);
422: ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
423: parent.setName(prefix + "main_component");
424:
425: ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
426: compSub1.setName(prefix + "sub_1");
427:
428: ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
429: compSub2.setName(prefix + "sub_2");
430:
431: ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
432: compSub2.setName(prefix + "sub_3");
433:
434: ObjectRepository.Group group = new ObjectRepository.Group();
435: group.setName(prefix + "test_group");
436:
437: compSub1.setParentComponent(parent);
438: compSub2.setParentComponent(parent);
439: compSub3.setParentComponent(parent);
440: ArrayList list = new ArrayList();
441: list.add(compSub1);
442: list.add(compSub2);
443: list.add(compSub3);
444: parent.setChildComponents(list);
445: parent.setGroup(group);
446:
447: broker.beginTransaction();
448: broker.store(parent);
449: broker.commitTransaction();
450:
451: broker.clearCache();
452: Query query = QueryFactory.newQueryByIdentity(parent);
453: parent = (ObjectRepository.ComponentIF) broker
454: .getObjectByQuery(query);
455:
456: Query groupQuery = QueryFactory.newQueryByIdentity(group);
457: ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker
458: .getObjectByQuery(groupQuery);
459:
460: assertNotNull(parent);
461: assertNotNull(parent.getGroup());
462: assertNotNull(parent.getChildComponents());
463: assertNotNull(parent.getName());
464: assertEquals(3, parent.getChildComponents().size());
465: assertEquals(group.getName(), (parent.getGroup().getName()));
466: ObjectRepository.ComponentIF aChild = (ObjectRepository.ComponentIF) parent
467: .getChildComponents().iterator().next();
468: assertNotNull(aChild);
469: assertNotNull(aChild.getParentComponent());
470: assertEquals(parent, aChild.getParentComponent());
471: assertNotNull(lookedUpGroup);
472:
473: //*************************************
474: assertNotNull(parent);
475: assertNotNull(parent.getGroup());
476: parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
477: broker.retrieveAllReferences(parent);
478: assertNotNull(parent);
479: /*
480: Now we have a problem! After serialization we can't find the anonymous keys
481: for parent object, because object identity has changed!!
482: This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
483: */
484: assertNotNull(parent.getGroup());
485: //*************************************
486: assertNotNull(parent.getChildComponents());
487: assertNotNull(parent.getName());
488: assertEquals(3, parent.getChildComponents().size());
489: aChild = (ObjectRepository.ComponentIF) parent
490: .getChildComponents().iterator().next();
491: assertNotNull(aChild);
492: assertNotNull(aChild.getParentComponent());
493: assertEquals(parent, aChild.getParentComponent());
494:
495: broker.beginTransaction();
496: broker.store(parent);
497: broker.commitTransaction();
498:
499: // now nothing should happen, because we don't make any changes
500: broker.clearCache();
501: query = QueryFactory.newQueryByIdentity(parent);
502: parent = (ObjectRepository.ComponentIF) broker
503: .getObjectByQuery(query);
504: groupQuery = QueryFactory.newQueryByIdentity(group);
505: lookedUpGroup = (ObjectRepository.Group) broker
506: .getObjectByQuery(groupQuery);
507: assertNotNull(parent);
508: assertNotNull(parent.getGroup());
509: assertNotNull(parent.getChildComponents());
510: assertNotNull(parent.getName());
511: assertEquals(3, parent.getChildComponents().size());
512: assertEquals(group.getName(), (parent.getGroup().getName()));
513: aChild = (ObjectRepository.ComponentIF) parent
514: .getChildComponents().iterator().next();
515: assertNotNull(aChild);
516: assertNotNull(aChild.getParentComponent());
517: assertEquals(parent, aChild.getParentComponent());
518: assertNotNull(lookedUpGroup);
519: } finally {
520: ord.setLazy(oldState);
521: }
522: }
523:
524: /**
525: * write an entry using vertical inheritance and try to read it again. E-F
526: */
527: public void testVerticalInheritanceStoreAndLoad() throws Exception {
528: // produce some test data
529: ObjectRepository.F entry = new ObjectRepository.F();
530: entry.setSomeSuperValue(31415926);
531: entry.setSomeValue(123456);
532: broker.beginTransaction();
533: broker.store(entry);
534: broker.commitTransaction();
535:
536: Identity oid = new Identity(entry, broker);
537:
538: // clear cache and retrieve a copy from the DB
539: broker.clearCache();
540: ObjectRepository.F copy = (ObjectRepository.F) broker
541: .getObjectByIdentity(oid);
542:
543: // check equality
544: assertEquals(entry.getSomeValue(), copy.getSomeValue());
545: assertEquals(entry.getSomeSuperValue(), copy
546: .getSomeSuperValue());
547: }
548:
549: /**
550: * write an entry using vertical inheritance and try to read it again.
551: * E-F-G
552: */
553: public void testVerticalInheritanceStoreAndLoad2() throws Exception {
554: // produce some test data G
555: ObjectRepository.G entry = new ObjectRepository.G();
556: entry.setSomeSuperValue(31415926);
557: entry.setSomeValue(123456);
558: entry.setSomeSubValue(4242);
559: broker.beginTransaction();
560: broker.store(entry);
561: broker.commitTransaction();
562:
563: Identity oid = new Identity(entry, broker);
564:
565: // clear cache and retrieve a copy from the DB
566: broker.clearCache();
567: ObjectRepository.G copy = (ObjectRepository.G) broker
568: .getObjectByIdentity(oid);
569:
570: // check equality
571: assertEquals(entry.getSomeValue(), copy.getSomeValue());
572: assertEquals(entry.getSomeSuperValue(), copy
573: .getSomeSuperValue());
574: assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
575: }
576:
577: /**
578: * write an entry using vertical inheritance and try to read it again.
579: * E-F1-G1, autoincrement id in E
580: */
581: public void testVerticalInheritanceStoreAndLoad3() throws Exception {
582: // produce some test data G1
583: ObjectRepository.G1 entry = new ObjectRepository.G1();
584: entry.setSomeSuperValue(31415926);
585: entry.setSomeValue(123456);
586: entry.setSomeSubValue(4242);
587: broker.beginTransaction();
588: broker.store(entry);
589: broker.commitTransaction();
590:
591: Identity oid = new Identity(entry, broker);
592:
593: // clear cache and retrieve a copy from the DB
594: broker.clearCache();
595: ObjectRepository.G1 copy = (ObjectRepository.G1) broker
596: .getObjectByIdentity(oid);
597:
598: // check equality
599: assertEquals(entry.getSomeValue(), copy.getSomeValue());
600: assertEquals(entry.getSomeSuperValue(), copy
601: .getSomeSuperValue());
602: assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
603: }
604:
605: /**
606: * write an entry using vertical inheritance and try to read it again. E-F
607: */
608: public void testVerticalInheritanceUpdate() throws Exception {
609: // produce some test data
610: ObjectRepository.F entry = new ObjectRepository.F();
611: entry.setSomeSuperValue(2718281);
612: entry.setSomeValue(9999);
613: broker.beginTransaction();
614: broker.store(entry);
615: broker.commitTransaction();
616:
617: Identity oid = new Identity(entry, broker);
618:
619: entry.setSomeSuperValue(2718282);
620: entry.setSomeValue(10000);
621: broker.beginTransaction();
622: broker.store(entry);
623: broker.commitTransaction();
624:
625: // clear cache and retrieve a copy from the DB
626: broker.clearCache();
627: ObjectRepository.F copy = (ObjectRepository.F) broker
628: .getObjectByIdentity(oid);
629:
630: // check equality
631: assertEquals(entry.getSomeValue(), copy.getSomeValue());
632: assertEquals(entry.getSomeSuperValue(), copy
633: .getSomeSuperValue());
634: }
635:
636: /**
637: * write an entry using vertical inheritance and try to read it again.
638: * E-F-G
639: */
640: public void testVerticalInheritanceUpdate2() throws Exception {
641: // produce some test data
642: ObjectRepository.G entry = new ObjectRepository.G();
643: entry.setSomeSuperValue(2718281);
644: entry.setSomeValue(9999);
645: entry.setSomeSubValue(8888);
646: broker.beginTransaction();
647: broker.store(entry);
648: broker.commitTransaction();
649:
650: Identity oid = new Identity(entry, broker);
651:
652: entry.setSomeSuperValue(2718282);
653: entry.setSomeValue(10000);
654: entry.setSomeSubValue(7777);
655: broker.beginTransaction();
656: broker.store(entry);
657: broker.commitTransaction();
658:
659: // clear cache and retrieve a copy from the DB
660: broker.clearCache();
661: ObjectRepository.G copy = (ObjectRepository.G) broker
662: .getObjectByIdentity(oid);
663:
664: // check equality
665: assertEquals(entry.getSomeValue(), copy.getSomeValue());
666: assertEquals(entry.getSomeSuperValue(), copy
667: .getSomeSuperValue());
668: assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
669: }
670:
671: /**
672: * write an entry using vertical inheritance and try to read it again.
673: * E-F1-G1, autoincrement id in E
674: */
675: public void testVerticalInheritanceUpdate3() throws Exception {
676: // produce some test data
677: ObjectRepository.G1 entry = new ObjectRepository.G1();
678: entry.setSomeSuperValue(2718281);
679: entry.setSomeValue(9999);
680: entry.setSomeSubValue(8888);
681: broker.beginTransaction();
682: broker.store(entry);
683: broker.commitTransaction();
684:
685: Identity oid = new Identity(entry, broker);
686:
687: entry.setSomeSuperValue(2718282);
688: entry.setSomeValue(10000);
689: entry.setSomeSubValue(7777);
690: broker.beginTransaction();
691: broker.store(entry);
692: broker.commitTransaction();
693:
694: // clear cache and retrieve a copy from the DB
695: broker.clearCache();
696: ObjectRepository.G1 copy = (ObjectRepository.G1) broker
697: .getObjectByIdentity(oid);
698:
699: // check equality
700: assertEquals(entry.getSomeValue(), copy.getSomeValue());
701: assertEquals(entry.getSomeSuperValue(), copy
702: .getSomeSuperValue());
703: assertEquals(entry.getSomeSubValue(), copy.getSomeSubValue());
704: }
705:
706: /**
707: * Query Attribute of Super class E-F
708: */
709: public void testQuerySuperField_WithCache() {
710: doTestQuerySuperField(false);
711: }
712:
713: /**
714: * Query Attribute of Super class E-F
715: */
716: public void testQuerySuperField_ClearedCache() {
717: doTestQuerySuperField(true);
718: }
719:
720: /**
721: * Query Attribute of Super class E-F
722: */
723: public void doTestQuerySuperField(boolean clearCache) {
724: int data1 = (int) (Math.random() * Integer.MAX_VALUE);
725: int data2 = (int) (Math.random() * Integer.MAX_VALUE);
726:
727: broker.beginTransaction();
728:
729: ObjectRepository.F f1 = new ObjectRepository.F();
730: f1.setSomeValue(data1);
731: f1.setSomeSuperValue(data2);
732: broker.store(f1);
733:
734: ObjectRepository.F f2 = new ObjectRepository.F();
735: f2.setSomeValue(data1);
736: f2.setSomeSuperValue(data2);
737: broker.store(f2);
738:
739: ObjectRepository.F f3 = new ObjectRepository.F();
740: f3.setSomeValue(data1);
741: f3.setSomeSuperValue(data2);
742: broker.store(f3);
743:
744: broker.commitTransaction();
745:
746: if (clearCache)
747: broker.clearCache();
748:
749: Criteria c = new Criteria();
750: c.addEqualTo("someSuperValue", new Integer(data2));
751: Query q = QueryFactory.newQuery(ObjectRepository.F.class, c);
752: Collection result = broker.getCollectionByQuery(q);
753: assertEquals(3, result.size());
754: ObjectRepository.F retF = (ObjectRepository.F) result
755: .iterator().next();
756: assertEquals(data1, retF.getSomeValue());
757: assertEquals(data2, retF.getSomeSuperValue());
758: }
759:
760: /**
761: * Query Attribute of Super class E-F-G
762: */
763: public void testQuerySuperField_2_WithCache() {
764: doTestQuerySuperField_2(false);
765: }
766:
767: /**
768: * Query Attribute of Super class E-F-G
769: */
770: public void testQuerySuperField_2_ClearedCache() {
771: doTestQuerySuperField_2(true);
772: }
773:
774: /**
775: * Query Attribute of Super class E-F-G
776: */
777: public void doTestQuerySuperField_2(boolean clearCache) {
778: int data1 = (int) (Math.random() * Integer.MAX_VALUE);
779: int data2 = (int) (Math.random() * Integer.MAX_VALUE);
780: int data3 = (int) (Math.random() * Integer.MAX_VALUE);
781: broker.beginTransaction();
782: ObjectRepository.G g1 = new ObjectRepository.G();
783: g1.setSomeValue(data1);
784: g1.setSomeSuperValue(data2);
785: g1.setSomeSubValue(data3);
786: broker.store(g1);
787:
788: ObjectRepository.G g2 = new ObjectRepository.G();
789: g2.setSomeValue(data1);
790: g2.setSomeSuperValue(data2);
791: g2.setSomeSubValue(data3);
792: broker.store(g2);
793:
794: ObjectRepository.G g3 = new ObjectRepository.G();
795: g3.setSomeValue(data1);
796: g3.setSomeSuperValue(data2);
797: g3.setSomeSubValue(data3);
798: broker.store(g3);
799: broker.commitTransaction();
800:
801: if (clearCache)
802: broker.clearCache();
803: Criteria c = new Criteria();
804: c.addEqualTo("someSuperValue", new Integer(data2));
805: Query q = QueryFactory.newQuery(ObjectRepository.G.class, c);
806: Collection result = broker.getCollectionByQuery(q);
807: assertEquals(3, result.size());
808: ObjectRepository.G ret = (ObjectRepository.G) result.iterator()
809: .next();
810: assertEquals(data1, ret.getSomeValue());
811: assertEquals(data2, ret.getSomeSuperValue());
812: assertEquals(data3, ret.getSomeSubValue());
813: }
814:
815: public void testMultipleJoinedInheritanceAndExtents() {
816: ObjectRepository.F1 entry = new ObjectRepository.F1();
817: entry.setSomeSuperValue(1);
818: entry.setSomeValue(2);
819: broker.beginTransaction();
820: broker.store(entry);
821: broker.commitTransaction();
822:
823: Integer id = entry.getId();
824: broker.clearCache();
825: entry = (F1) findById(ObjectRepository.F1.class, id.intValue());
826: assertEquals(id, entry.getId());
827: assertEquals(1, entry.getSomeSuperValue());
828: assertEquals(2, entry.getSomeValue());
829: }
830:
831: public void testMultipleJoinedInheritanceAndExtentsWithCache() {
832: ObjectRepository.F1 entry = new ObjectRepository.F1();
833: entry.setSomeSuperValue(1);
834: entry.setSomeValue(2);
835: broker.beginTransaction();
836: broker.store(entry);
837: broker.commitTransaction();
838:
839: Integer id = entry.getId();
840: entry = (F1) findById(ObjectRepository.F1.class, id.intValue());
841: assertEquals(id, entry.getId());
842: assertEquals(1, entry.getSomeSuperValue());
843: assertEquals(2, entry.getSomeValue());
844: }
845:
846: public void testMultipleJoinedInheritanceAndExtents_2() {
847: ObjectRepository.F1 entry = new ObjectRepository.F1();
848: entry.setSomeSuperValue(1);
849: entry.setSomeValue(2);
850: broker.beginTransaction();
851: broker.store(entry);
852: broker.commitTransaction();
853:
854: Integer id = entry.getId();
855: broker.clearCache();
856: entry = (F1) findById(ObjectRepository.E.class, id.intValue());
857: assertEquals(id, entry.getId());
858: assertEquals(1, entry.getSomeSuperValue());
859: assertEquals(2, entry.getSomeValue());
860: }
861:
862: public void testMultipleJoinedInheritanceAndExtents_2_WithCache() {
863: ObjectRepository.F1 entry = new ObjectRepository.F1();
864: entry.setSomeSuperValue(1);
865: entry.setSomeValue(2);
866: broker.beginTransaction();
867: broker.store(entry);
868: broker.commitTransaction();
869:
870: Integer id = entry.getId();
871: entry = (F1) findById(ObjectRepository.E.class, id.intValue());
872: assertEquals(id, entry.getId());
873: assertEquals(1, entry.getSomeSuperValue());
874: assertEquals(2, entry.getSomeValue());
875: }
876:
877: // --------------------------------------------------------------------------------------
878: // Utiltity methods.
879: private Object serializeDeserializeObject(Serializable obj) {
880: return (ObjectRepository.ComponentIF) SerializationUtils
881: .deserialize(SerializationUtils.serialize(obj));
882: }
883:
884: private Object findById(Class type, int id) {
885: Collection result = broker
886: .getCollectionByQuery(createQueryById(type, id));
887: if (result == null || result.size() == 0) {
888: return null;
889: }
890: if (result.size() > 1) {
891: throw new RuntimeException(
892: "Unexpected unique id constraint violation ");
893: }
894:
895: return result.iterator().next();
896:
897: }
898:
899: private Query createQueryById(Class type, int id) {
900: Criteria byIdCriteria = new Criteria();
901: byIdCriteria.addEqualTo("id", new Integer(id));
902: return QueryFactory.newQuery(type, byIdCriteria);
903: }
904: }
|