KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * User: om
3  */

4
5 package org.apache.ojb.otm;
6
7 import java.util.Arrays JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import junit.framework.TestCase;
13 import org.apache.ojb.broker.metadata.ClassDescriptor;
14 import org.apache.ojb.broker.metadata.CollectionDescriptor;
15 import org.apache.ojb.broker.util.collections.ManageableArrayList;
16 import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
17 import org.apache.ojb.broker.*;
18 import org.apache.ojb.otm.lock.LockingException;
19 import org.apache.ojb.otm.core.Transaction;
20
21 /**
22  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
23  * @version $Id$
24  */

25 public class MtoNTest extends TestCase
26 {
27     private static Class JavaDoc CLASS = MtoNTest.class;
28     private TestKit _kit;
29     private OTMConnection _conn;
30     private static final int COUNT = 1;
31     private static final long TIME = System.currentTimeMillis();
32
33     public void setUp() throws LockingException
34     {
35         _kit = TestKit.getTestInstance();
36         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
37     }
38
39     public void tearDown() throws LockingException
40     {
41         _conn.close();
42         _conn = null;
43     };
44
45     public static void main(String JavaDoc[] args)
46     {
47         String JavaDoc[] arr = {CLASS.getName()};
48         junit.textui.TestRunner.main(arr);
49     }
50
51     private Paper createPaper() throws LockingException
52     {
53         String JavaDoc now = new Date JavaDoc().toString();
54         Paper paper = new Paper();
55         paper.setAuthor("Jonny Myers");
56         paper.setDate(now);
57
58         Qualifier qual1 = new Topic();
59         qual1.setName("qual1 " + now);
60         Qualifier qual2 = new Topic();
61         qual2.setName("qual2 " + now);
62
63         List JavaDoc qualifiers = new Vector JavaDoc();
64         qualifiers.add(qual1);
65         qualifiers.add(qual2);
66         paper.setQualifiers(qualifiers);
67         Transaction trans = _kit.getTransaction(_conn);
68         trans.begin();
69         _conn.makePersistent(qual1);
70         _conn.makePersistent(qual2);
71         _conn.makePersistent(paper);
72         Identity paperId = _conn.getIdentity(paper);
73         trans.commit();
74
75         // sanity check
76
trans = _kit.getTransaction(_conn);
77         trans.begin();
78         Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
79         qualifiers = retPaper.getQualifiers();
80
81         assertEquals(2, qualifiers.size());
82         trans.commit();
83
84         return retPaper;
85     }
86
87     public void testCreate() throws Exception JavaDoc
88     {
89         Paper paper = createPaper();
90     }
91
92     public void testStoringWithAutoUpdateFalse() throws Exception JavaDoc
93     {
94         ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
95         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
96         boolean autoUpdate = cod.getCascadeStore();
97         cod.setCascadeStore(false);
98         try
99         {
100             String JavaDoc now = new Date JavaDoc().toString();
101             Paper paper = new Paper();
102             paper.setAuthor("Jonny Myers");
103             paper.setDate(now);
104             Qualifier qual = new Topic();
105             qual.setName("qual " + now);
106             paper.setQualifiers(Arrays.asList(new Qualifier[]{qual}));
107             Transaction trans = _kit.getTransaction(_conn);
108             trans.begin();
109             _conn.makePersistent(paper); // store Paper and intermediary table only
110
Identity paperId = _conn.getIdentity(paper);
111             trans.commit();
112
113         // broker.clearCache();
114
trans = _kit.getTransaction(_conn);
115             trans.begin();
116             Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
117             assertEquals(0, retPaper.getQualifiers().size());
118             trans.commit();
119             ;
120         }
121         finally
122         {
123             cod.setCascadeStore(autoUpdate);
124         }
125     }
126
127     public void testStoringWithAutoUpdateTrue() throws Exception JavaDoc
128     {
129         ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
130         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
131         boolean autoUpdate = cod.getCascadeStore();
132
133         cod.setCascadeStore(true);
134
135         try
136         {
137             String JavaDoc now = new Date JavaDoc().toString();
138             Paper paper = new Paper();
139             paper.setAuthor("Jonny Myers");
140             paper.setDate(now);
141             Qualifier qual = new Topic();
142             qual.setName("qual " + now);
143             paper.setQualifiers(Arrays.asList(new Qualifier[]{qual}));
144             Transaction trans = _kit.getTransaction(_conn);
145             trans.begin();
146             _conn.makePersistent(paper); // store Paper, intermediary and Qualifier
147
Identity paperId = _conn.getIdentity(paper);
148             trans.commit();
149             //broker.clearCache();
150
trans = _kit.getTransaction(_conn);
151             trans.begin();
152             Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
153             assertEquals(1, retPaper.getQualifiers().size());
154             trans.commit();
155             ;
156         }
157         finally
158         {
159             cod.setCascadeStore(autoUpdate);
160         }
161     }
162
163
164     // delete from intermediary table only when collection NOT removal aware
165
public void testDelete_NonRemovalAware() throws Exception JavaDoc
166     {
167         ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
168         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
169         Class JavaDoc collectionClass = cod.getCollectionClass();
170
171         cod.setCollectionClass(ManageableArrayList.class);
172
173         try
174         {
175             Paper paper = createPaper();
176             Identity paperId = _conn.getIdentity(paper);
177             List JavaDoc qualifiers = paper.getQualifiers();
178             Qualifier qual1 = (Qualifier) qualifiers.get(0);
179             Qualifier qual2 = (Qualifier) qualifiers.get(1);
180
181             // remove first object
182
qualifiers.remove(0);
183             Transaction trans = _kit.getTransaction(_conn);
184             trans.begin();
185             _conn.makePersistent(paper);
186             trans.commit();
187             ;
188
189             //broker.clearCache();
190
trans = _kit.getTransaction(_conn);
191             trans.begin();
192             Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
193             assertEquals(1, retPaper.getQualifiers().size());
194             // target object qual1 should NOT be deleted
195
Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
196             Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
197
198             assertNotNull(retQual1);
199             assertNotNull(retQual2);
200
201             trans.commit();
202             ;
203         }
204         finally
205         {
206             cod.setCollectionClass(collectionClass);
207         }
208
209     }
210
211     // delete from intermediary AND target-table when collection removal aware
212
public void testDelete_RemovalAware() throws Exception JavaDoc
213     {
214         ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
215         CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
216         Class JavaDoc collectionClass = cod.getCollectionClass();
217
218         cod.setCollectionClass(RemovalAwareCollection.class);
219
220         try
221         {
222             Paper paper = createPaper();
223             List JavaDoc qualifiers = paper.getQualifiers();
224             Qualifier qual1 = (Qualifier) qualifiers.get(0);
225             Qualifier qual2 = (Qualifier) qualifiers.get(1);
226             Identity paperId = _conn.getIdentity(paper);
227
228             // remove first object
229
qualifiers.remove(0);
230             Transaction trans = _kit.getTransaction(_conn);
231             trans.begin();
232             _conn.makePersistent(paper);
233             trans.commit();
234             ;
235
236         // broker.clearCache();
237
trans = _kit.getTransaction(_conn);
238             trans.begin();
239             Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
240             assertEquals(1, retPaper.getQualifiers().size());
241
242             // target object qual1 should be deleted
243
Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
244             Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
245
246             assertNull(retQual1);
247             assertNotNull(retQual2);
248
249             trans.commit();
250             ;
251         }
252         finally
253         {
254             cod.setCollectionClass(collectionClass);
255         }
256     }
257
258     public void testDeletionFromIntermediaryTableWithNullList() throws Exception JavaDoc
259     {
260         Paper paper = createPaper();
261         Identity paperId = _conn.getIdentity(paper);
262         List JavaDoc qualifiers = paper.getQualifiers();
263         Qualifier qual1 = (Qualifier) qualifiers.get(0);
264         Qualifier qual2 = (Qualifier) qualifiers.get(1);
265
266         // now set collection to null and check if changes get persisted
267
paper.setQualifiers(null);
268         Transaction trans = _kit.getTransaction(_conn);
269         trans.begin();
270         _conn.makePersistent(paper);
271         trans.commit();
272         ;
273
274         //broker.clearCache();
275
trans = _kit.getTransaction(_conn);
276         trans.begin();
277         Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
278         assertEquals(0, retPaper.getQualifiers().size());
279
280         // target objects should NOT be deleted
281
Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
282         Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
283
284         assertNotNull(retQual1);
285         assertNotNull(retQual2);
286
287         trans.commit();
288         ;
289     }
290
291     public void testDeletionWithClearedList() throws Exception JavaDoc
292     {
293         Paper paper = createPaper();
294         Identity paperId = _conn.getIdentity(paper);
295         List JavaDoc qualifiers = paper.getQualifiers();
296         Qualifier qual1 = (Qualifier) qualifiers.get(0);
297         Qualifier qual2 = (Qualifier) qualifiers.get(1);
298
299         // now clear collection
300
paper.getQualifiers().clear();
301         Transaction trans = _kit.getTransaction(_conn);
302         trans.begin();
303         _conn.makePersistent(paper);
304         trans.commit();
305         ;
306
307         //broker.clearCache();
308
trans = _kit.getTransaction(_conn);
309         trans.begin();
310         Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
311         assertEquals(0, retPaper.getQualifiers().size());
312
313         // target objects should be deleted
314
Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
315         Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
316
317         assertNull(retQual1);
318         assertNull(retQual2);
319
320         trans.commit();
321         ;
322     }
323
324     public void testDeletionFromIntermediaryTableWithEmptyList() throws Exception JavaDoc
325     {
326         Paper paper = createPaper();
327         Identity paperId = _conn.getIdentity(paper);
328         List JavaDoc qualifiers = paper.getQualifiers();
329         Qualifier qual1 = (Qualifier) qualifiers.get(0);
330         Qualifier qual2 = (Qualifier) qualifiers.get(1);
331
332         // now empty collection and check if changes get persisted
333
paper.setQualifiers(new RemovalAwareCollection());
334         Transaction trans = _kit.getTransaction(_conn);
335         trans.begin();
336         _conn.makePersistent(paper);
337         trans.commit();
338         ;
339
340     // broker.clearCache();
341
trans = _kit.getTransaction(_conn);
342         trans.begin();
343         Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
344         assertEquals(0, retPaper.getQualifiers().size());
345
346         // target objects should NOT be deleted
347
Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
348         Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
349
350         assertNotNull(retQual1);
351         assertNotNull(retQual2);
352
353         trans.commit();
354         ;
355     }
356
357 }
358
Popular Tags