001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.metadata.ClassDescriptor;
004: import org.apache.ojb.broker.metadata.CollectionDescriptor;
005: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
006: import org.apache.ojb.broker.util.collections.ManageableArrayList;
007: import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
008: import org.apache.ojb.junit.PBTestCase;
009:
010: import java.util.ArrayList;
011: import java.util.Arrays;
012: import java.util.Collection;
013: import java.util.Date;
014: import java.util.List;
015: import java.util.Vector;
016:
017: /**
018: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
019: * @version $Id: $
020: */
021: public class MtoNTest extends PBTestCase {
022: private static Class CLASS = MtoNTest.class;
023:
024: public static void main(String[] args) {
025: String[] arr = { CLASS.getName() };
026: junit.textui.TestRunner.main(arr);
027: }
028:
029: private Paper createPaper() {
030: String now = new Date().toString();
031: Paper paper = new Paper();
032: paper.setAuthor("Jonny Myers");
033: paper.setDate(now);
034:
035: Qualifier qual1 = new Topic();
036: qual1.setName("qual1 " + now);
037: Qualifier qual2 = new Topic();
038: qual2.setName("qual2 " + now);
039:
040: List qualifiers = new Vector();
041: qualifiers.add(qual1);
042: qualifiers.add(qual2);
043: paper.setQualifiers(qualifiers);
044:
045: broker.beginTransaction();
046: broker.store(qual1);
047: broker.store(qual2);
048: broker.store(paper);
049: Identity paperId = new Identity(paper, broker);
050: broker.commitTransaction();
051:
052: // sanity check
053: broker.clearCache();
054: broker.beginTransaction();
055: Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
056: qualifiers = retPaper.getQualifiers();
057:
058: assertEquals(2, qualifiers.size());
059: broker.commitTransaction();
060:
061: return retPaper;
062: }
063:
064: /**
065: * Store m-side and intermediary
066: */
067: public void testStoringWithAutoUpdateFalse1() {
068: ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
069: CollectionDescriptor cod = cld
070: .getCollectionDescriptorByName("qualifiers");
071: int autoUpdate = cod.getCascadingStore();
072:
073: cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);
074:
075: try {
076: String now = new Date().toString();
077: Paper paper = new Paper();
078: paper.setAuthor("Jonny Myers");
079: paper.setDate(now);
080: Qualifier qual = new Topic();
081: qual.setName("qual " + now);
082: paper
083: .setQualifiers(Arrays
084: .asList(new Qualifier[] { qual }));
085: broker.beginTransaction();
086: // TODO: use constraint in DB and fix test
087: // store paper and set indirection table, ignore new Qualifier
088: // object. Will cause Key Constraint Exception when constraint are set
089: broker.store(paper);
090: Identity paperId = new Identity(paper, broker);
091: broker.commitTransaction();
092:
093: broker.clearCache();
094: broker.beginTransaction();
095: Paper retPaper = (Paper) broker
096: .getObjectByIdentity(paperId);
097: assertEquals(0, retPaper.getQualifiers().size());
098: broker.commitTransaction();
099: } finally {
100: cod.setCascadingStore(autoUpdate);
101: }
102: }
103:
104: /**
105: * Store m-side, intermediary and n-side
106: * n-side forced by using broker.store()
107: */
108: public void testStoringWithAutoUpdateFalse2() {
109: ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
110: CollectionDescriptor cod = cld
111: .getCollectionDescriptorByName("qualifiers");
112: int autoUpdate = cod.getCascadingStore();
113:
114: cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);
115:
116: try {
117: String now = new Date().toString();
118: Paper paper = new Paper();
119: paper.setAuthor("Jonny Myers");
120: paper.setDate(now);
121: Qualifier qual = new Topic();
122: qual.setName("qual " + now);
123: paper
124: .setQualifiers(Arrays
125: .asList(new Qualifier[] { qual }));
126: broker.beginTransaction();
127: broker.store(qual); // store Qualifier
128: broker.store(paper); // store Paper and intermediary table only
129: Identity paperId = broker.serviceIdentity().buildIdentity(
130: paper);
131: broker.commitTransaction();
132:
133: broker.clearCache();
134: broker.beginTransaction();
135: Paper retPaper = (Paper) broker
136: .getObjectByIdentity(paperId);
137: assertEquals(1, retPaper.getQualifiers().size());
138: broker.commitTransaction();
139: } finally {
140: cod.setCascadingStore(autoUpdate);
141: }
142: }
143:
144: /**
145: * Store m-side, intermediary and n-side
146: */
147: public void testStoringWithAutoUpdateTrue() {
148: ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
149: CollectionDescriptor cod = cld
150: .getCollectionDescriptorByName("qualifiers");
151: int autoUpdate = cod.getCascadingStore();
152:
153: cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_OBJECT);
154:
155: try {
156: String now = new Date().toString();
157: Paper paper = new Paper();
158: paper.setAuthor("Jonny Myers");
159: paper.setDate(now);
160: Qualifier qual = new Topic();
161: qual.setName("qual " + now);
162: paper
163: .setQualifiers(Arrays
164: .asList(new Qualifier[] { qual }));
165: broker.beginTransaction();
166: broker.store(paper); // store Paper, intermediary and Qualifier
167: Identity paperId = new Identity(paper, broker);
168: broker.commitTransaction();
169:
170: broker.clearCache();
171: broker.beginTransaction();
172: Paper retPaper = (Paper) broker
173: .getObjectByIdentity(paperId);
174: assertEquals(1, retPaper.getQualifiers().size());
175: broker.commitTransaction();
176: } finally {
177: cod.setCascadingStore(autoUpdate);
178: }
179: }
180:
181: // delete from intermediary table only when collection NOT removal aware
182: public void testDelete_NonRemovalAware() {
183: ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
184: CollectionDescriptor cod = cld
185: .getCollectionDescriptorByName("qualifiers");
186: Class collectionClass = cod.getCollectionClass();
187:
188: cod.setCollectionClass(ManageableArrayList.class);
189:
190: try {
191: Paper paper = createPaper();
192: Identity paperId = new Identity(paper, broker);
193: List qualifiers = paper.getQualifiers();
194: Qualifier qual1 = (Qualifier) qualifiers.get(0);
195: Qualifier qual2 = (Qualifier) qualifiers.get(1);
196:
197: // remove first object
198: qualifiers.remove(0);
199: broker.beginTransaction();
200: broker.store(paper);
201: broker.commitTransaction();
202:
203: broker.clearCache();
204: broker.beginTransaction();
205: Paper retPaper = (Paper) broker
206: .getObjectByIdentity(paperId);
207: assertEquals(1, retPaper.getQualifiers().size());
208:
209: // target object qual1 should NOT be deleted
210: Qualifier retQual1 = (Qualifier) broker
211: .getObjectByIdentity(new Identity(qual1, broker));
212: Qualifier retQual2 = (Qualifier) broker
213: .getObjectByIdentity(new Identity(qual2, broker));
214:
215: assertNotNull(retQual1);
216: assertNotNull(retQual2);
217:
218: broker.commitTransaction();
219: } finally {
220: cod.setCollectionClass(collectionClass);
221: }
222:
223: }
224:
225: // delete from intermediary AND target-table when collection removal aware
226: public void testDelete_RemovalAware() {
227: ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
228: CollectionDescriptor cod = cld
229: .getCollectionDescriptorByName("qualifiers");
230: Class collectionClass = cod.getCollectionClass();
231:
232: cod.setCollectionClass(RemovalAwareCollection.class);
233:
234: try {
235: Paper paper = createPaper();
236: List qualifiers = paper.getQualifiers();
237: Qualifier qual1 = (Qualifier) qualifiers.get(0);
238: Qualifier qual2 = (Qualifier) qualifiers.get(1);
239: Identity paperId = new Identity(paper, broker);
240:
241: // remove first object
242: qualifiers.remove(0);
243: broker.beginTransaction();
244: broker.store(paper);
245: broker.commitTransaction();
246:
247: broker.clearCache();
248: broker.beginTransaction();
249: Paper retPaper = (Paper) broker
250: .getObjectByIdentity(paperId);
251: assertEquals(1, retPaper.getQualifiers().size());
252:
253: // target object qual1 should be deleted
254: Qualifier retQual1 = (Qualifier) broker
255: .getObjectByIdentity(new Identity(qual1, broker));
256: Qualifier retQual2 = (Qualifier) broker
257: .getObjectByIdentity(new Identity(qual2, broker));
258:
259: assertNull(retQual1);
260: assertNotNull(retQual2);
261:
262: broker.commitTransaction();
263: } finally {
264: cod.setCollectionClass(collectionClass);
265: }
266: }
267:
268: public void testDeletionFromIntermediaryTableWithNullList() {
269: Paper paper = createPaper();
270: Identity paperId = new Identity(paper, broker);
271: List qualifiers = paper.getQualifiers();
272: Qualifier qual1 = (Qualifier) qualifiers.get(0);
273: Qualifier qual2 = (Qualifier) qualifiers.get(1);
274:
275: // now set collection to null and check if changes get persisted
276: paper.setQualifiers(null);
277: broker.beginTransaction();
278: broker.store(paper);
279: broker.commitTransaction();
280:
281: broker.clearCache();
282: broker.beginTransaction();
283: Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
284: assertEquals(0, retPaper.getQualifiers().size());
285:
286: // target objects should NOT be deleted
287: Qualifier retQual1 = (Qualifier) broker
288: .getObjectByIdentity(new Identity(qual1, broker));
289: Qualifier retQual2 = (Qualifier) broker
290: .getObjectByIdentity(new Identity(qual2, broker));
291:
292: assertNotNull(retQual1);
293: assertNotNull(retQual2);
294:
295: broker.commitTransaction();
296: }
297:
298: public void testDeletionWithClearedList() {
299: Paper paper = createPaper();
300: Identity paperId = new Identity(paper, broker);
301: List qualifiers = paper.getQualifiers();
302: Qualifier qual1 = (Qualifier) qualifiers.get(0);
303: Qualifier qual2 = (Qualifier) qualifiers.get(1);
304:
305: // now clear collection
306: paper.getQualifiers().clear();
307: broker.beginTransaction();
308: broker.store(paper);
309: broker.commitTransaction();
310:
311: broker.clearCache();
312: broker.beginTransaction();
313: Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
314: assertEquals(0, retPaper.getQualifiers().size());
315:
316: // target objects should NOT be deleted
317: Qualifier retQual1 = (Qualifier) broker
318: .getObjectByIdentity(new Identity(qual1, broker));
319: Qualifier retQual2 = (Qualifier) broker
320: .getObjectByIdentity(new Identity(qual2, broker));
321:
322: assertNotNull(retQual1);
323: assertNotNull(retQual2);
324:
325: broker.commitTransaction();
326: }
327:
328: public void testDeletionFromIntermediaryTableWithEmptyList() {
329: Paper paper = createPaper();
330: Identity paperId = new Identity(paper, broker);
331: List qualifiers = paper.getQualifiers();
332: Qualifier qual1 = (Qualifier) qualifiers.get(0);
333: Qualifier qual2 = (Qualifier) qualifiers.get(1);
334:
335: // now empty collection and check if changes get persisted
336: paper.setQualifiers(new RemovalAwareCollection());
337: broker.beginTransaction();
338: broker.store(paper);
339: broker.commitTransaction();
340:
341: broker.clearCache();
342: broker.beginTransaction();
343: Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
344: assertEquals(0, retPaper.getQualifiers().size());
345:
346: // target objects should NOT be deleted
347: Qualifier retQual1 = (Qualifier) broker
348: .getObjectByIdentity(new Identity(qual1, broker));
349: Qualifier retQual2 = (Qualifier) broker
350: .getObjectByIdentity(new Identity(qual2, broker));
351:
352: assertNotNull(retQual1);
353: assertNotNull(retQual2);
354:
355: broker.commitTransaction();
356: }
357:
358: public void testDeleteMtoNImplementor() throws Exception {
359: News newsId2 = new News(2);
360: Identity id = new Identity(newsId2, broker);
361: News newNews = (News) broker.getObjectByIdentity(id);
362: int size = newNews.getQualifiers().size();
363:
364: Category categoryId1 = new Category(1);
365:
366: MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers",
367: newsId2, categoryId1);
368: broker.deleteMtoNImplementor(m2n);
369:
370: broker.clearCache();
371: newNews = (News) broker.getObjectByIdentity(id);
372:
373: assertEquals(size - 1, newNews.getQualifiers().size());
374: }
375:
376: public void testStoreMtoNImplementor() throws Exception {
377: News newsId2 = new News(2);
378: Category categoryId2 = new Category(2);
379:
380: Identity id = new Identity(newsId2, broker);
381: News newNews = (News) broker.getObjectByIdentity(id);
382: int size = newNews.getQualifiers().size();
383:
384: MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers",
385: newsId2, categoryId2);
386: broker.addMtoNImplementor(m2n);
387:
388: broker.clearCache();
389: newNews = (News) broker.getObjectByIdentity(id);
390:
391: assertEquals(size + 1, newNews.getQualifiers().size());
392:
393: }
394:
395: // Bidirectional m:n relationship using Collection
396: public void testStoreBidirectionalCollection() {
397: Person personA = new Person();
398: personA.setFirstname("Anton");
399:
400: Project proj1 = new Project();
401: proj1.setTitle("Project 1");
402:
403: Project proj2 = new Project();
404: proj2.setTitle("Project 2");
405:
406: Collection persons = new ArrayList();
407: persons.add(personA);
408: proj1.setPersons(persons);
409: proj2.setPersons(persons);
410:
411: Collection projects = new ArrayList();
412: projects.add(proj1);
413: projects.add(proj2);
414: personA.setProjects(projects);
415:
416: broker.beginTransaction();
417: broker.store(personA);
418: broker.store(proj1);
419: broker.store(proj2);
420: broker.commitTransaction();
421: }
422:
423: // Bidirectional m:n relationship using Array
424: public void testStoreBidirectionalArray() {
425: PersonWithArray personA = new PersonWithArray();
426: personA.setFirstname("Anton");
427:
428: ProjectWithArray proj1 = new ProjectWithArray();
429: proj1.setTitle("Project 1");
430:
431: ProjectWithArray proj2 = new ProjectWithArray();
432: proj2.setTitle("Project 2");
433:
434: proj1.setPersons(new PersonWithArray[] { personA });
435: proj2.setPersons(new PersonWithArray[] { personA });
436: personA.setProjects(new ProjectWithArray[] { proj1, proj2 });
437:
438: broker.beginTransaction();
439: broker.store(personA);
440: broker.store(proj1);
441: broker.store(proj2);
442: broker.commitTransaction();
443: }
444: }
|