KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import org.apache.ojb.broker.metadata.ClassDescriptor;
4 import org.apache.ojb.broker.metadata.CollectionDescriptor;
5 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
6 import org.apache.ojb.broker.util.collections.ManageableArrayList;
7 import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
8 import org.apache.ojb.junit.PBTestCase;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
19  * @version $Id: $
20  */

21 public class MtoNTest extends PBTestCase
22 {
23     private static Class JavaDoc CLASS = MtoNTest.class;
24
25     public static void main(String JavaDoc[] args)
26     {
27         String JavaDoc[] arr = { CLASS.getName()};
28         junit.textui.TestRunner.main(arr);
29     }
30
31     private Paper createPaper()
32     {
33         String JavaDoc now = new Date JavaDoc().toString();
34         Paper paper = new Paper();
35         paper.setAuthor("Jonny Myers");
36         paper.setDate(now);
37
38         Qualifier qual1 = new Topic();
39         qual1.setName("qual1 " + now);
40         Qualifier qual2 = new Topic();
41         qual2.setName("qual2 " + now);
42
43         List qualifiers = new Vector JavaDoc();
44         qualifiers.add(qual1);
45         qualifiers.add(qual2);
46         paper.setQualifiers(qualifiers);
47
48         broker.beginTransaction();
49         broker.store(qual1);
50         broker.store(qual2);
51         broker.store(paper);
52         Identity paperId = new Identity(paper, broker);
53         broker.commitTransaction();
54
55         // sanity check
56
broker.clearCache();
57         broker.beginTransaction();
58         Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
59         qualifiers = retPaper.getQualifiers();
60
61         assertEquals(2, qualifiers.size());
62         broker.commitTransaction();
63
64         return retPaper;
65     }
66
67     /**
68      * Store m-side and intermediary
69      */

70     public void testStoringWithAutoUpdateFalse1()
71     {
72         ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
73         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
74         int autoUpdate = cod.getCascadingStore();
75
76         cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);
77
78         try
79         {
80             String JavaDoc now = new Date JavaDoc().toString();
81             Paper paper = new Paper();
82             paper.setAuthor("Jonny Myers");
83             paper.setDate(now);
84             Qualifier qual = new Topic();
85             qual.setName("qual " + now);
86             paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
87             broker.beginTransaction();
88             // TODO: use constraint in DB and fix test
89
// store paper and set indirection table, ignore new Qualifier
90
// object. Will cause Key Constraint Exception when constraint are set
91
broker.store(paper);
92             Identity paperId = new Identity(paper, broker);
93             broker.commitTransaction();
94
95             broker.clearCache();
96             broker.beginTransaction();
97             Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
98             assertEquals(0, retPaper.getQualifiers().size());
99             broker.commitTransaction();
100         }
101         finally
102         {
103             cod.setCascadingStore(autoUpdate);
104         }
105     }
106
107     /**
108      * Store m-side, intermediary and n-side
109      * n-side forced by using broker.store()
110      */

111     public void testStoringWithAutoUpdateFalse2()
112     {
113         ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
114         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
115         int autoUpdate = cod.getCascadingStore();
116
117         cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_LINK);
118
119         try
120         {
121             String JavaDoc now = new Date JavaDoc().toString();
122             Paper paper = new Paper();
123             paper.setAuthor("Jonny Myers");
124             paper.setDate(now);
125             Qualifier qual = new Topic();
126             qual.setName("qual " + now);
127             paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
128             broker.beginTransaction();
129             broker.store(qual); // store Qualifier
130
broker.store(paper); // store Paper and intermediary table only
131
Identity paperId = broker.serviceIdentity().buildIdentity(paper);
132             broker.commitTransaction();
133
134             broker.clearCache();
135             broker.beginTransaction();
136             Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
137             assertEquals(1, retPaper.getQualifiers().size());
138             broker.commitTransaction();
139         }
140         finally
141         {
142             cod.setCascadingStore(autoUpdate);
143         }
144     }
145
146     /**
147      * Store m-side, intermediary and n-side
148      */

149     public void testStoringWithAutoUpdateTrue()
150     {
151         ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
152         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
153         int autoUpdate = cod.getCascadingStore();
154
155         cod.setCascadingStore(ObjectReferenceDescriptor.CASCADE_OBJECT);
156
157         try
158         {
159             String JavaDoc now = new Date JavaDoc().toString();
160             Paper paper = new Paper();
161             paper.setAuthor("Jonny Myers");
162             paper.setDate(now);
163             Qualifier qual = new Topic();
164             qual.setName("qual " + now);
165             paper.setQualifiers(Arrays.asList(new Qualifier[] { qual }));
166             broker.beginTransaction();
167             broker.store(paper); // store Paper, intermediary and Qualifier
168
Identity paperId = new Identity(paper, broker);
169             broker.commitTransaction();
170
171             broker.clearCache();
172             broker.beginTransaction();
173             Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
174             assertEquals(1, retPaper.getQualifiers().size());
175             broker.commitTransaction();
176         }
177         finally
178         {
179             cod.setCascadingStore(autoUpdate);
180         }
181     }
182
183
184     // delete from intermediary table only when collection NOT removal aware
185
public void testDelete_NonRemovalAware()
186     {
187         ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
188         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
189         Class JavaDoc collectionClass = cod.getCollectionClass();
190
191         cod.setCollectionClass(ManageableArrayList.class);
192
193         try
194         {
195             Paper paper = createPaper();
196             Identity paperId = new Identity(paper, broker);
197             List qualifiers = paper.getQualifiers();
198             Qualifier qual1 = (Qualifier) qualifiers.get(0);
199             Qualifier qual2 = (Qualifier) qualifiers.get(1);
200
201             // remove first object
202
qualifiers.remove(0);
203             broker.beginTransaction();
204             broker.store(paper);
205             broker.commitTransaction();
206
207             broker.clearCache();
208             broker.beginTransaction();
209             Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
210             assertEquals(1, retPaper.getQualifiers().size());
211
212             // target object qual1 should NOT be deleted
213
Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
214             Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));
215
216             assertNotNull(retQual1);
217             assertNotNull(retQual2);
218
219             broker.commitTransaction();
220         }
221         finally
222         {
223             cod.setCollectionClass(collectionClass);
224         }
225
226     }
227
228     // delete from intermediary AND target-table when collection removal aware
229
public void testDelete_RemovalAware()
230     {
231         ClassDescriptor cld = broker.getClassDescriptor(Paper.class);
232         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
233         Class JavaDoc collectionClass = cod.getCollectionClass();
234
235         cod.setCollectionClass(RemovalAwareCollection.class);
236
237         try
238         {
239             Paper paper = createPaper();
240             List qualifiers = paper.getQualifiers();
241             Qualifier qual1 = (Qualifier) qualifiers.get(0);
242             Qualifier qual2 = (Qualifier) qualifiers.get(1);
243             Identity paperId = new Identity(paper, broker);
244
245             // remove first object
246
qualifiers.remove(0);
247             broker.beginTransaction();
248             broker.store(paper);
249             broker.commitTransaction();
250
251             broker.clearCache();
252             broker.beginTransaction();
253             Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
254             assertEquals(1, retPaper.getQualifiers().size());
255
256             // target object qual1 should be deleted
257
Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
258             Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));
259
260             assertNull(retQual1);
261             assertNotNull(retQual2);
262
263             broker.commitTransaction();
264         }
265         finally
266         {
267             cod.setCollectionClass(collectionClass);
268         }
269     }
270
271     public void testDeletionFromIntermediaryTableWithNullList()
272     {
273         Paper paper = createPaper();
274         Identity paperId = new Identity(paper, broker);
275         List qualifiers = paper.getQualifiers();
276         Qualifier qual1 = (Qualifier) qualifiers.get(0);
277         Qualifier qual2 = (Qualifier) qualifiers.get(1);
278
279         // now set collection to null and check if changes get persisted
280
paper.setQualifiers(null);
281         broker.beginTransaction();
282         broker.store(paper);
283         broker.commitTransaction();
284
285         broker.clearCache();
286         broker.beginTransaction();
287         Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
288         assertEquals(0, retPaper.getQualifiers().size());
289
290         // target objects should NOT be deleted
291
Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
292         Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));
293
294         assertNotNull(retQual1);
295         assertNotNull(retQual2);
296
297         broker.commitTransaction();
298     }
299
300     public void testDeletionWithClearedList()
301     {
302         Paper paper = createPaper();
303         Identity paperId = new Identity(paper, broker);
304         List qualifiers = paper.getQualifiers();
305         Qualifier qual1 = (Qualifier) qualifiers.get(0);
306         Qualifier qual2 = (Qualifier) qualifiers.get(1);
307
308         // now clear collection
309
paper.getQualifiers().clear();
310         broker.beginTransaction();
311         broker.store(paper);
312         broker.commitTransaction();
313
314         broker.clearCache();
315         broker.beginTransaction();
316         Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
317         assertEquals(0, retPaper.getQualifiers().size());
318
319         // target objects should NOT be deleted
320
Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
321         Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));
322
323         assertNotNull(retQual1);
324         assertNotNull(retQual2);
325
326         broker.commitTransaction();
327     }
328
329     public void testDeletionFromIntermediaryTableWithEmptyList()
330     {
331         Paper paper = createPaper();
332         Identity paperId = new Identity(paper, broker);
333         List qualifiers = paper.getQualifiers();
334         Qualifier qual1 = (Qualifier) qualifiers.get(0);
335         Qualifier qual2 = (Qualifier) qualifiers.get(1);
336
337         // now empty collection and check if changes get persisted
338
paper.setQualifiers(new RemovalAwareCollection());
339         broker.beginTransaction();
340         broker.store(paper);
341         broker.commitTransaction();
342
343         broker.clearCache();
344         broker.beginTransaction();
345         Paper retPaper = (Paper) broker.getObjectByIdentity(paperId);
346         assertEquals(0, retPaper.getQualifiers().size());
347
348         // target objects should NOT be deleted
349
Qualifier retQual1 = (Qualifier) broker.getObjectByIdentity(new Identity(qual1, broker));
350         Qualifier retQual2 = (Qualifier) broker.getObjectByIdentity(new Identity(qual2, broker));
351
352         assertNotNull(retQual1);
353         assertNotNull(retQual2);
354
355         broker.commitTransaction();
356     }
357
358
359     public void testDeleteMtoNImplementor()
360         throws Exception JavaDoc
361     {
362         News newsId2 = new News(2);
363         Identity id = new Identity(newsId2,broker);
364         News newNews = (News) broker.getObjectByIdentity(id);
365         int size = newNews.getQualifiers().size();
366
367         Category categoryId1 = new Category(1);
368
369         MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers", newsId2, categoryId1);
370         broker.deleteMtoNImplementor(m2n);
371
372         broker.clearCache();
373         newNews = (News) broker.getObjectByIdentity(id);
374
375         assertEquals(size - 1,newNews.getQualifiers().size());
376     }
377
378     public void testStoreMtoNImplementor()
379         throws Exception JavaDoc
380     {
381         News newsId2 = new News(2);
382         Category categoryId2 = new Category(2);
383
384         Identity id = new Identity(newsId2,broker);
385         News newNews = (News) broker.getObjectByIdentity(id);
386         int size = newNews.getQualifiers().size();
387
388         MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers", newsId2,categoryId2);
389         broker.addMtoNImplementor(m2n);
390
391         broker.clearCache();
392         newNews = (News) broker.getObjectByIdentity(id);
393
394         assertEquals(size + 1,newNews.getQualifiers().size());
395
396     }
397
398
399     // Bidirectional m:n relationship using Collection
400
public void testStoreBidirectionalCollection()
401     {
402         Person personA = new Person();
403         personA.setFirstname("Anton");
404
405         Project proj1 = new Project();
406         proj1.setTitle("Project 1");
407
408         Project proj2 = new Project();
409         proj2.setTitle("Project 2");
410
411         Collection persons = new ArrayList();
412         persons.add(personA);
413         proj1.setPersons(persons);
414         proj2.setPersons(persons);
415
416         Collection projects = new ArrayList();
417         projects.add(proj1);
418         projects.add(proj2);
419         personA.setProjects(projects);
420
421         broker.beginTransaction();
422         broker.store(personA);
423         broker.store(proj1);
424         broker.store(proj2);
425         broker.commitTransaction();
426     }
427
428     // Bidirectional m:n relationship using Array
429
public void testStoreBidirectionalArray()
430     {
431         PersonWithArray personA = new PersonWithArray();
432         personA.setFirstname("Anton");
433
434         ProjectWithArray proj1 = new ProjectWithArray();
435         proj1.setTitle("Project 1");
436
437         ProjectWithArray proj2 = new ProjectWithArray();
438         proj2.setTitle("Project 2");
439
440         proj1.setPersons(new PersonWithArray[] { personA });
441         proj2.setPersons(new PersonWithArray[] { personA });
442         personA.setProjects(new ProjectWithArray[] { proj1, proj2 });
443
444         broker.beginTransaction();
445         broker.store(personA);
446         broker.store(proj1);
447         broker.store(proj2);
448         broker.commitTransaction();
449     }
450 }
451
Popular Tags