001: package org.apache.ojb.broker;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.apache.commons.lang.SerializationUtils;
010: import org.apache.ojb.broker.metadata.ClassDescriptor;
011: import org.apache.ojb.broker.metadata.CollectionDescriptor;
012: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
013: import org.apache.ojb.broker.query.Criteria;
014: import org.apache.ojb.broker.query.Query;
015: import org.apache.ojb.broker.query.QueryFactory;
016: import org.apache.ojb.junit.PBTestCase;
017:
018: /**
019: * Test case for collection handling.
020: *
021: * IMPORTANT NOTE: The global runtime metadata changes made by this test case
022: * are NOT recommended in multithreaded environments, because they are global
023: * and each thread will be affected.
024: *
025: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
026: * @version $Id: CollectionTest2.java,v 1.5.2.1 2004/07/27 00:31:50 arminw Exp $
027: */
028: public class CollectionTest2 extends PBTestCase {
029: public static void main(String[] args) {
030: String[] arr = { CollectionTest2.class.getName() };
031: junit.textui.TestRunner.main(arr);
032: }
033:
034: public void tearDown() throws Exception {
035: if (broker != null) {
036: changeAutoSetting(Project.class, "subProjects", false,
037: false, false, false);
038: changeAutoSetting(Project.class, "developers", false,
039: false, false, false);
040: changeAutoSetting(SubProject.class, "project", false,
041: false, false, false);
042: broker.close();
043: }
044: super .tearDown();
045: }
046:
047: public void testAutoUpdateDeleteSettings() {
048: changeAutoSetting(Project.class, "subProjects", false, false,
049: false, false);
050: CollectionDescriptor ord = broker.getClassDescriptor(
051: Project.class).getCollectionDescriptorByName(
052: "subProjects");
053: assertEquals(CollectionDescriptor.CASCADE_LINK, ord
054: .getCascadingStore());
055: assertEquals(CollectionDescriptor.CASCADE_NONE, ord
056: .getCascadingDelete());
057: assertEquals(false, ord.getCascadeStore());
058: assertEquals(false, ord.getCascadeDelete());
059:
060: changeAutoSetting(Project.class, "subProjects", false, true,
061: true, false);
062: ord = broker.getClassDescriptor(Project.class)
063: .getCollectionDescriptorByName("subProjects");
064: assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord
065: .getCascadingStore());
066: assertEquals(ObjectReferenceDescriptor.CASCADE_OBJECT, ord
067: .getCascadingDelete());
068: assertEquals(true, ord.getCascadeStore());
069: assertEquals(true, ord.getCascadeDelete());
070: }
071:
072: public void testStoreUpdateDelete_1a() {
073: changeAutoSetting(Project.class, "subProjects", false, false,
074: false, false);
075: changeAutoSetting(Project.class, "developers", false, false,
076: false, false);
077: changeAutoSetting(SubProject.class, "project", false, false,
078: false, false);
079: doTestStoreUpdateDelete_1();
080: }
081:
082: public void testStoreUpdateDelete_1b() {
083: changeAutoSetting(Project.class, "subProjects", false, false,
084: false, false);
085: changeAutoSetting(Project.class, "developers", false, false,
086: false, false);
087: changeAutoSetting(SubProject.class, "project", false, false,
088: false, false);
089: doTestStoreUpdateDelete_1();
090: }
091:
092: public void doTestStoreUpdateDelete_1() {
093: String name = "testStoreUpdateDelete_1"
094: + System.currentTimeMillis();
095: changeAutoSetting(Project.class, "subProjects", false, false,
096: false, false);
097: changeAutoSetting(Project.class, "developers", false, false,
098: false, false);
099: changeAutoSetting(SubProject.class, "project", false, false,
100: false, false);
101:
102: Developer dev1 = new Developer(name);
103: Developer dev2 = new Developer(name);
104: ArrayList devList = new ArrayList();
105: devList.add(dev1);
106: devList.add(dev2);
107:
108: SubProject sub1 = new SubProject(name, null);
109: SubProject sub2 = new SubProject(name, null);
110: ArrayList subList = new ArrayList();
111: subList.add(sub1);
112: subList.add(sub2);
113:
114: Project pro = new Project(name, subList, devList);
115: sub1.setProject(pro);
116: sub2.setProject(pro);
117: pro.setDevelopers(devList);
118: pro.setSubProjects(subList);
119: sub1.setProject(pro);
120: sub2.setProject(pro);
121:
122: Query queryProject = createQueryFor(Project.class, "name", name);
123: Query querySubProject = createQueryFor(SubProject.class,
124: "name", name);
125: Query queryDeveloper = createQueryFor(Developer.class, "name",
126: name);
127:
128: //*****************************************
129: // insert
130: //*****************************************
131: broker.beginTransaction();
132: broker.store(pro);
133: broker.commitTransaction();
134:
135: broker.clearCache();
136: Collection result = broker.getCollectionByQuery(queryProject);
137: assertEquals(1, result.size());
138: assertNotNull(result.iterator().next());
139: result = broker.getCollectionByQuery(querySubProject);
140: assertEquals(0, result.size());
141: result = broker.getCollectionByQuery(queryDeveloper);
142: assertEquals(0, result.size());
143:
144: broker.beginTransaction();
145: broker.store(sub1);
146: broker.store(sub2);
147: broker.store(dev1);
148: broker.store(dev2);
149: broker.commitTransaction();
150:
151: broker.clearCache();
152: result = broker.getCollectionByQuery(queryProject);
153: assertEquals(1, result.size());
154: assertNotNull(result.iterator().next());
155: result = broker.getCollectionByQuery(querySubProject);
156: assertEquals(2, result.size());
157: result = broker.getCollectionByQuery(queryDeveloper);
158: assertEquals(2, result.size());
159:
160: Identity proOid = new Identity(pro, broker);
161: Project newPro = (Project) broker.getObjectByIdentity(proOid);
162: assertNull(newPro.getDevelopers());
163: assertNull(newPro.getSubProjects());
164: //assertEquals(0, newPro.getDevelopers().size());
165: //assertEquals(0, newPro.getSubProjects().size());
166: broker.retrieveAllReferences(newPro);
167: assertNotNull(newPro.getDevelopers());
168: assertNotNull(newPro.getSubProjects());
169: assertEquals(2, newPro.getDevelopers().size());
170: assertEquals(2, newPro.getSubProjects().size());
171: SubProject sub = (SubProject) newPro.getSubProjects()
172: .iterator().next();
173: assertNotNull(sub);
174: assertNull(sub.getProject());
175: broker.retrieveAllReferences(sub);
176: assertNotNull(sub.getProject());
177: assertEquals(sub.getProject().getId(), newPro.getId());
178:
179: //*****************************************
180: // update
181: //*****************************************
182: broker.clearCache();
183: newPro = (Project) broker.getObjectByIdentity(proOid);
184: newPro.setName("updated_" + name);
185: broker.retrieveAllReferences(newPro);
186:
187: broker.beginTransaction();
188: broker.store(newPro);
189: Iterator it = newPro.getSubProjects().iterator();
190: while (it.hasNext()) {
191: SubProject subProject = (SubProject) it.next();
192: broker.retrieveAllReferences(subProject);
193: subProject.setName("updated_" + name);
194: broker.store(subProject);
195: }
196: broker.commitTransaction();
197:
198: broker.clearCache();
199: newPro = (Project) broker.getObjectByIdentity(proOid);
200: assertEquals("updated_" + name, newPro.getName());
201: assertNull(newPro.getDevelopers());
202: assertNull(newPro.getSubProjects());
203: broker.retrieveAllReferences(newPro);
204: assertNotNull(newPro.getDevelopers());
205: assertNotNull(newPro.getSubProjects());
206: assertEquals(2, newPro.getDevelopers().size());
207: assertEquals(2, newPro.getSubProjects().size());
208: it = newPro.getSubProjects().iterator();
209: while (it.hasNext()) {
210: SubProject subProject = (SubProject) it.next();
211: assertEquals("updated_" + name, subProject.getName());
212: }
213:
214: //*****************************************
215: // delete
216: //*****************************************
217: broker.clearCache();
218: broker.beginTransaction();
219: broker.delete(sub1);
220: broker.delete(sub2);
221: broker.delete(dev1);
222: broker.delete(dev2);
223: broker.delete(pro);
224: broker.commitTransaction();
225:
226: result = broker.getCollectionByQuery(queryProject);
227: assertEquals(0, result.size());
228: result = broker.getCollectionByQuery(querySubProject);
229: assertEquals(0, result.size());
230: result = broker.getCollectionByQuery(queryDeveloper);
231: assertEquals(0, result.size());
232: }
233:
234: public void testStoreUpdateDelete_2a() {
235: changeAutoSetting(Project.class, "subProjects", true, true,
236: true, false);
237: changeAutoSetting(Project.class, "developers", true, true,
238: true, false);
239: changeAutoSetting(SubProject.class, "project", true, true,
240: true, false);
241: doTestStoreUpdateDelete_2();
242: }
243:
244: public void testStoreUpdateDelete_2b() {
245: changeAutoSetting(Project.class, "subProjects", true, true,
246: true, true);
247: changeAutoSetting(Project.class, "developers", true, true,
248: true, true);
249: changeAutoSetting(SubProject.class, "project", true, true,
250: true, true);
251: doTestStoreUpdateDelete_2();
252: }
253:
254: public void doTestStoreUpdateDelete_2() {
255: String name = "testStoreUpdateDelete_2"
256: + System.currentTimeMillis();
257:
258: Developer dev1 = new Developer(name);
259: Developer dev2 = new Developer(name);
260: ArrayList devList = new ArrayList();
261: devList.add(dev1);
262: devList.add(dev2);
263:
264: SubProject sub1 = new SubProject(name, null);
265: SubProject sub2 = new SubProject(name, null);
266: ArrayList subList = new ArrayList();
267: subList.add(sub1);
268: subList.add(sub2);
269:
270: Project pro = new Project(name, subList, devList);
271: sub1.setProject(pro);
272: sub2.setProject(pro);
273:
274: sub1.setProject(pro);
275: sub2.setProject(pro);
276: pro.setSubProjects(subList);
277: pro.setDevelopers(devList);
278:
279: Query queryProject = createQueryFor(Project.class, "name", name);
280: Query querySubProject = createQueryFor(SubProject.class,
281: "name", name);
282: Query queryDeveloper = createQueryFor(Developer.class, "name",
283: name);
284:
285: //*****************************************
286: // insert
287: //*****************************************
288: broker.beginTransaction();
289: broker.store(pro);
290: broker.commitTransaction();
291:
292: broker.clearCache();
293: Collection result = broker.getCollectionByQuery(queryProject);
294: assertEquals(1, result.size());
295: assertNotNull(result.iterator().next());
296: result = broker.getCollectionByQuery(querySubProject);
297: assertEquals(2, result.size());
298: result = broker.getCollectionByQuery(queryDeveloper);
299: assertEquals(2, result.size());
300:
301: Identity proOid = new Identity(pro, broker);
302: Project newPro = (Project) broker.getObjectByIdentity(proOid);
303: assertNotNull(newPro.getDevelopers());
304: assertNotNull(newPro.getSubProjects());
305: assertEquals(2, newPro.getDevelopers().size());
306: assertEquals(2, newPro.getSubProjects().size());
307: SubProject sub = (SubProject) newPro.getSubProjects()
308: .iterator().next();
309: assertNotNull(sub);
310: assertNotNull(sub.getProject());
311: assertEquals(sub.getProject().getId(), newPro.getId());
312:
313: //*****************************************
314: // update
315: //*****************************************
316: broker.clearCache();
317: newPro = (Project) broker.getObjectByIdentity(proOid);
318: newPro.setName("updated_" + name);
319: broker.beginTransaction();
320: Iterator it = newPro.getSubProjects().iterator();
321: while (it.hasNext()) {
322: SubProject subProject = (SubProject) it.next();
323: subProject.setName("updated_" + name);
324: }
325: broker.store(newPro);
326: broker.commitTransaction();
327:
328: broker.clearCache();
329: newPro = (Project) broker.getObjectByIdentity(proOid);
330: assertEquals("updated_" + name, newPro.getName());
331: assertNotNull(newPro.getDevelopers());
332: assertNotNull(newPro.getSubProjects());
333: assertEquals(2, newPro.getDevelopers().size());
334: assertEquals(2, newPro.getSubProjects().size());
335: it = newPro.getSubProjects().iterator();
336: while (it.hasNext()) {
337: SubProject subProject = (SubProject) it.next();
338: assertEquals("updated_" + name, subProject.getName());
339: }
340:
341: //*****************************************
342: // delete
343: //*****************************************
344: broker.clearCache();
345: broker.beginTransaction();
346: broker.delete(pro);
347: broker.commitTransaction();
348:
349: result = broker.getCollectionByQuery(queryProject);
350: assertEquals(0, result.size());
351: result = broker.getCollectionByQuery(querySubProject);
352: assertEquals(0, result.size());
353: result = broker.getCollectionByQuery(queryDeveloper);
354: assertEquals(0, result.size());
355: }
356:
357: public void testStoreUpdateDeleteSerialize_1a() {
358: changeAutoSetting(Project.class, "subProjects", true, true,
359: true, false);
360: changeAutoSetting(Project.class, "developers", true, true,
361: true, false);
362: changeAutoSetting(SubProject.class, "project", true, true,
363: true, false);
364: doTestStoreUpdateDeleteSerialize();
365: }
366:
367: public void testStoreUpdateDeleteSerialize_1b() {
368: changeAutoSetting(Project.class, "subProjects", true, true,
369: true, true);
370: changeAutoSetting(Project.class, "developers", true, true,
371: true, true);
372: changeAutoSetting(SubProject.class, "project", true, true,
373: true, true);
374: doTestStoreUpdateDeleteSerialize();
375: }
376:
377: public void doTestStoreUpdateDeleteSerialize() {
378: String name = "testStoreUpdateDelete_2"
379: + System.currentTimeMillis();
380:
381: Developer dev1 = new Developer(name);
382: Developer dev2 = new Developer(name);
383: ArrayList devList = new ArrayList();
384: devList.add(dev1);
385: devList.add(dev2);
386:
387: SubProject sub1 = new SubProject(name, null);
388: SubProject sub2 = new SubProject(name, null);
389: ArrayList subList = new ArrayList();
390: subList.add(sub1);
391: subList.add(sub2);
392:
393: Project pro = new Project(name, subList, devList);
394: sub1.setProject(pro);
395: sub2.setProject(pro);
396:
397: sub1.setProject(pro);
398: sub2.setProject(pro);
399: pro.setSubProjects(subList);
400: pro.setDevelopers(devList);
401:
402: Query queryProject = createQueryFor(Project.class, "name", name);
403: Query querySubProject = createQueryFor(SubProject.class,
404: "name", name);
405: Query queryDeveloper = createQueryFor(Developer.class, "name",
406: name);
407:
408: //*****************************************
409: // insert
410: //*****************************************
411: broker.beginTransaction();
412: broker.store(pro);
413: broker.commitTransaction();
414:
415: broker.clearCache();
416: Collection result = broker.getCollectionByQuery(queryProject);
417: assertEquals(1, result.size());
418: assertNotNull(result.iterator().next());
419: result = broker.getCollectionByQuery(querySubProject);
420: assertEquals(2, result.size());
421: result = broker.getCollectionByQuery(queryDeveloper);
422: assertEquals(2, result.size());
423:
424: Identity proOid = new Identity(pro, broker);
425: Project newPro = (Project) broker.getObjectByIdentity(proOid);
426: assertNotNull(newPro.getDevelopers());
427: assertNotNull(newPro.getSubProjects());
428: assertEquals(2, newPro.getDevelopers().size());
429: assertEquals(2, newPro.getSubProjects().size());
430: SubProject sub = (SubProject) newPro.getSubProjects()
431: .iterator().next();
432: assertNotNull(sub);
433: assertNotNull(sub.getProject());
434: assertEquals(sub.getProject().getId(), newPro.getId());
435:
436: //*****************************************
437: // update
438: //*****************************************
439: broker.clearCache();
440: newPro = (Project) broker.getObjectByIdentity(proOid);
441: newPro.setName("updated_" + name);
442:
443: //*****************************************
444: // de-/serialize object
445: //*****************************************
446: newPro = (Project) SerializationUtils
447: .deserialize(SerializationUtils.serialize(newPro));
448:
449: broker.beginTransaction();
450: newPro.getSubProjects().get(0);
451: Iterator it = newPro.getSubProjects().iterator();
452: while (it.hasNext()) {
453: SubProject subProject = (SubProject) it.next();
454: subProject.setName("updated_" + name);
455: }
456: broker.store(newPro);
457: broker.commitTransaction();
458:
459: broker.clearCache();
460: newPro = (Project) broker.getObjectByIdentity(proOid);
461: assertEquals("updated_" + name, newPro.getName());
462: assertNotNull(newPro.getDevelopers());
463: assertNotNull(newPro.getSubProjects());
464: assertEquals(2, newPro.getDevelopers().size());
465: assertEquals(2, newPro.getSubProjects().size());
466: it = newPro.getSubProjects().iterator();
467: while (it.hasNext()) {
468: SubProject subProject = (SubProject) it.next();
469: assertEquals("updated_" + name, subProject.getName());
470: }
471:
472: //*****************************************
473: // de-/serialize object
474: //*****************************************
475: newPro = (Project) SerializationUtils
476: .deserialize(SerializationUtils.serialize(newPro));
477:
478: //*****************************************
479: // delete
480: //*****************************************
481: broker.clearCache();
482: broker.beginTransaction();
483: broker.delete(pro);
484: broker.commitTransaction();
485:
486: result = broker.getCollectionByQuery(queryProject);
487: assertEquals(0, result.size());
488: result = broker.getCollectionByQuery(querySubProject);
489: assertEquals(0, result.size());
490: result = broker.getCollectionByQuery(queryDeveloper);
491: assertEquals(0, result.size());
492: }
493:
494: public void testStoreUpdateDelete_3a() {
495: changeAutoSetting(Project.class, "subProjects", true,
496: CollectionDescriptor.CASCADE_OBJECT,
497: CollectionDescriptor.CASCADE_LINK, false);
498: changeAutoSetting(Project.class, "developers", true,
499: CollectionDescriptor.CASCADE_LINK,
500: CollectionDescriptor.CASCADE_OBJECT, false);
501: changeAutoSetting(SubProject.class, "project", true,
502: CollectionDescriptor.CASCADE_OBJECT,
503: CollectionDescriptor.CASCADE_NONE, false);
504: doTestStoreUpdateDelete_3();
505: }
506:
507: public void testStoreUpdateDelete_3b() {
508: changeAutoSetting(Project.class, "subProjects", true,
509: CollectionDescriptor.CASCADE_OBJECT,
510: CollectionDescriptor.CASCADE_LINK, true);
511: changeAutoSetting(Project.class, "developers", true,
512: CollectionDescriptor.CASCADE_LINK,
513: CollectionDescriptor.CASCADE_OBJECT, true);
514: changeAutoSetting(SubProject.class, "project", true,
515: CollectionDescriptor.CASCADE_OBJECT,
516: CollectionDescriptor.CASCADE_NONE, true);
517: doTestStoreUpdateDelete_3();
518: }
519:
520: public void doTestStoreUpdateDelete_3() {
521: String name = "testStoreUpdateDelete_2"
522: + System.currentTimeMillis();
523:
524: Developer dev1 = new Developer(name);
525: Developer dev2 = new Developer(name);
526: ArrayList devList = new ArrayList();
527: devList.add(dev1);
528: devList.add(dev2);
529:
530: SubProject sub1 = new SubProject(name, null);
531: SubProject sub2 = new SubProject(name, null);
532: ArrayList subList = new ArrayList();
533: subList.add(sub1);
534: subList.add(sub2);
535:
536: Project pro = new Project(name, subList, devList);
537: sub1.setProject(pro);
538: sub2.setProject(pro);
539:
540: sub1.setProject(pro);
541: sub2.setProject(pro);
542: pro.setSubProjects(subList);
543: pro.setDevelopers(devList);
544:
545: Query queryProject = createQueryFor(Project.class, "name", name);
546: Query querySubProject = createQueryFor(SubProject.class,
547: "name", name);
548: Query queryDeveloper = createQueryFor(Developer.class, "name",
549: name);
550:
551: //*****************************************
552: // insert
553: //*****************************************
554: broker.beginTransaction();
555: broker.store(pro);
556: broker.commitTransaction();
557:
558: broker.clearCache();
559: Collection result = broker.getCollectionByQuery(queryProject);
560: assertEquals(1, result.size());
561: assertNotNull(result.iterator().next());
562: result = broker.getCollectionByQuery(querySubProject);
563: assertEquals(2, result.size());
564: result = broker.getCollectionByQuery(queryDeveloper);
565: assertEquals(0, result.size());
566:
567: broker.beginTransaction();
568: dev1.setProjectId(pro.getId());
569: dev2.setProjectId(pro.getId());
570: broker.store(dev1);
571: broker.store(dev2);
572: broker.commitTransaction();
573:
574: broker.clearCache();
575: Identity proOid = new Identity(pro, broker);
576: Project newPro = (Project) broker.getObjectByIdentity(proOid);
577: assertNotNull(newPro.getDevelopers());
578: assertNotNull(newPro.getSubProjects());
579: assertEquals(2, newPro.getDevelopers().size());
580: assertEquals(2, newPro.getSubProjects().size());
581: SubProject sub = (SubProject) newPro.getSubProjects()
582: .iterator().next();
583: assertNotNull(sub);
584: assertNotNull(sub.getProject());
585: assertEquals(sub.getProject().getId(), newPro.getId());
586:
587: //*****************************************
588: // update
589: //*****************************************
590: broker.clearCache();
591: newPro = (Project) broker.getObjectByIdentity(proOid);
592: newPro.setName("updated_" + name);
593: broker.beginTransaction();
594: Iterator it = newPro.getSubProjects().iterator();
595: while (it.hasNext()) {
596: SubProject subProject = (SubProject) it.next();
597: subProject.setName("updated_" + name);
598: }
599: broker.store(newPro);
600: broker.commitTransaction();
601:
602: broker.clearCache();
603: newPro = (Project) broker.getObjectByIdentity(proOid);
604: assertEquals("updated_" + name, newPro.getName());
605: assertNotNull(newPro.getDevelopers());
606: assertNotNull(newPro.getSubProjects());
607: assertEquals(2, newPro.getDevelopers().size());
608: assertEquals(2, newPro.getSubProjects().size());
609: it = newPro.getSubProjects().iterator();
610: while (it.hasNext()) {
611: SubProject subProject = (SubProject) it.next();
612: assertEquals("updated_" + name, subProject.getName());
613: }
614:
615: //*****************************************
616: // delete
617: //*****************************************
618: broker.clearCache();
619: broker.beginTransaction();
620: broker.serviceBrokerHelper().unlink(sub1, "project");
621: broker.serviceBrokerHelper().unlink(sub2, "project");
622: broker.store(sub1);
623: broker.store(sub2);
624: broker.delete(pro);
625: broker.commitTransaction();
626:
627: result = broker.getCollectionByQuery(queryProject);
628: assertEquals(0, result.size());
629: result = broker.getCollectionByQuery(querySubProject);
630: assertEquals(2, result.size());
631: result = broker.getCollectionByQuery(queryDeveloper);
632: assertEquals(0, result.size());
633: }
634:
635: public void testUpdate_1() {
636: changeAutoSetting(Project.class, "subProjects", true,
637: CollectionDescriptor.CASCADE_OBJECT,
638: CollectionDescriptor.CASCADE_NONE, false);
639: changeAutoSetting(Project.class, "developers", true,
640: CollectionDescriptor.CASCADE_LINK,
641: CollectionDescriptor.CASCADE_LINK, false);
642: changeAutoSetting(SubProject.class, "project", true,
643: CollectionDescriptor.CASCADE_OBJECT,
644: CollectionDescriptor.CASCADE_NONE, false);
645: }
646:
647: public void testUpdate_2() {
648: changeAutoSetting(Project.class, "subProjects", true,
649: CollectionDescriptor.CASCADE_OBJECT,
650: CollectionDescriptor.CASCADE_NONE, true);
651: changeAutoSetting(Project.class, "developers", true,
652: CollectionDescriptor.CASCADE_LINK,
653: CollectionDescriptor.CASCADE_LINK, true);
654: changeAutoSetting(SubProject.class, "project", true,
655: CollectionDescriptor.CASCADE_OBJECT,
656: CollectionDescriptor.CASCADE_NONE, true);
657: }
658:
659: public void doTestUpdate() {
660: String name = "testStoreUpdateDelete_2"
661: + System.currentTimeMillis();
662:
663: changeAutoSetting(Project.class, "subProjects", true,
664: CollectionDescriptor.CASCADE_OBJECT,
665: CollectionDescriptor.CASCADE_NONE, false);
666: changeAutoSetting(Project.class, "developers", true,
667: CollectionDescriptor.CASCADE_LINK,
668: CollectionDescriptor.CASCADE_LINK, false);
669: changeAutoSetting(SubProject.class, "project", true,
670: CollectionDescriptor.CASCADE_OBJECT,
671: CollectionDescriptor.CASCADE_NONE, false);
672:
673: Developer dev1 = new Developer(name);
674: Developer dev2 = new Developer(name);
675: ArrayList devList = new ArrayList();
676: devList.add(dev1);
677: devList.add(dev2);
678:
679: SubProject sub1 = new SubProject(name, null);
680: SubProject sub2 = new SubProject(name, null);
681: ArrayList subList = new ArrayList();
682: subList.add(sub1);
683: subList.add(sub2);
684:
685: Project pro = new Project(name, subList, devList);
686: sub1.setProject(pro);
687: sub2.setProject(pro);
688:
689: sub1.setProject(pro);
690: sub2.setProject(pro);
691: pro.setSubProjects(subList);
692: pro.setDevelopers(devList);
693:
694: Query queryProject = createQueryFor(Project.class, "name", name);
695: Query querySubProject = createQueryFor(SubProject.class,
696: "name", name);
697: Query queryDeveloper = createQueryFor(Developer.class, "name",
698: name);
699:
700: //*****************************************
701: // insert
702: //*****************************************
703: broker.beginTransaction();
704: broker.store(pro);
705: broker.commitTransaction();
706:
707: broker.clearCache();
708: Collection result = broker.getCollectionByQuery(queryProject);
709: assertEquals(1, result.size());
710: assertNotNull(result.iterator().next());
711: result = broker.getCollectionByQuery(querySubProject);
712: assertEquals(2, result.size());
713: result = broker.getCollectionByQuery(queryDeveloper);
714: assertEquals(0, result.size());
715: }
716:
717: //============================================================================
718: // helper methods
719: //============================================================================
720:
721: void changeAutoSetting(Class clazz, String referenceField,
722: boolean autoRetrieve, boolean autoUpdate,
723: boolean autoDelete, boolean useProxy) {
724: ClassDescriptor cld = broker.getClassDescriptor(clazz);
725: ObjectReferenceDescriptor ref = cld
726: .getCollectionDescriptorByName(referenceField);
727: if (ref == null)
728: ref = cld
729: .getObjectReferenceDescriptorByName(referenceField);
730: ref.setLazy(useProxy);
731: ref.setCascadeRetrieve(autoRetrieve);
732: ref.setCascadeStore(autoUpdate);
733: ref.setCascadeDelete(autoDelete);
734: }
735:
736: void changeAutoSetting(Class clazz, String referenceField,
737: boolean autoRetrieve, int autoUpdate, int autoDelete,
738: boolean useProxy) {
739: ClassDescriptor cld = broker.getClassDescriptor(clazz);
740: ObjectReferenceDescriptor ref = cld
741: .getCollectionDescriptorByName(referenceField);
742: if (ref == null)
743: ref = cld
744: .getObjectReferenceDescriptorByName(referenceField);
745: ref.setLazy(useProxy);
746: ref.setCascadeRetrieve(autoRetrieve);
747: ref.setCascadingStore(autoUpdate);
748: ref.setCascadingDelete(autoDelete);
749: }
750:
751: Query createQueryFor(Class clazz, String attribute, String value) {
752: Criteria crit = new Criteria();
753: crit.addLike(attribute, "%" + value + "%");
754: return QueryFactory.newQuery(clazz, crit);
755: }
756:
757: //============================================================================
758: // inner classes, used test classes
759: //============================================================================
760:
761: public static class Project implements ProjectIF {
762: private Integer id;
763: private String name;
764: private List subProjects;
765: private Collection developers;
766:
767: public Project() {
768: }
769:
770: public Project(String name, List subProjects,
771: Collection developers) {
772: this .name = name;
773: this .subProjects = subProjects;
774: this .developers = developers;
775: }
776:
777: public Integer getId() {
778: return id;
779: }
780:
781: public void setId(Integer id) {
782: this .id = id;
783: }
784:
785: public String getName() {
786: return name;
787: }
788:
789: public void setName(String name) {
790: this .name = name;
791: }
792:
793: public List getSubProjects() {
794: return subProjects;
795: }
796:
797: public void setSubProjects(List subProjects) {
798: this .subProjects = subProjects;
799: }
800:
801: public Collection getDevelopers() {
802: return developers;
803: }
804:
805: public void setDevelopers(Collection developers) {
806: this .developers = developers;
807: }
808: }
809:
810: public static interface ProjectIF extends Serializable {
811: public Integer getId();
812:
813: public void setId(Integer id);
814:
815: public String getName();
816:
817: public void setName(String name);
818:
819: public List getSubProjects();
820:
821: public void setSubProjects(List subProjects);
822:
823: public Collection getDevelopers();
824:
825: public void setDevelopers(Collection developers);
826: }
827:
828: public static class SubProject implements SubProjectIF {
829: private Integer id;
830: private String name;
831: private ProjectIF project;
832:
833: public SubProject() {
834: }
835:
836: public SubProject(String name, Project project) {
837: this .name = name;
838: this .project = project;
839: }
840:
841: public Integer getId() {
842: return id;
843: }
844:
845: public void setId(Integer id) {
846: this .id = id;
847: }
848:
849: public String getName() {
850: return name;
851: }
852:
853: public void setName(String name) {
854: this .name = name;
855: }
856:
857: public ProjectIF getProject() {
858: return project;
859: }
860:
861: public void setProject(ProjectIF project) {
862: this .project = project;
863: }
864: }
865:
866: public static interface SubProjectIF extends Serializable {
867: public Integer getId();
868:
869: public void setId(Integer id);
870:
871: public String getName();
872:
873: public void setName(String name);
874:
875: public ProjectIF getProject();
876:
877: public void setProject(ProjectIF project);
878: }
879:
880: public static class Developer implements Serializable {
881: private Integer id;
882: private String name;
883: private Integer projectId;
884:
885: public Developer() {
886: }
887:
888: public Developer(String name) {
889: this .name = name;
890: }
891:
892: public Integer getId() {
893: return id;
894: }
895:
896: public void setId(Integer id) {
897: this .id = id;
898: }
899:
900: public String getName() {
901: return name;
902: }
903:
904: public void setName(String name) {
905: this .name = name;
906: }
907:
908: public Integer getProjectId() {
909: return projectId;
910: }
911:
912: public void setProjectId(Integer projectId) {
913: this.projectId = projectId;
914: }
915: }
916: }
|