KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > BidirectionalAssociationTest


1 /**
2  * Author: Matthew Baird
3  * mattbaird@yahoo.com
4  */

5 package org.apache.ojb.odmg;
6
7 import java.util.Iterator JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11 import org.apache.ojb.junit.ODMGTestCase;
12 import org.odmg.ODMGException;
13 import org.odmg.OQLQuery;
14 import org.odmg.Transaction;
15
16 /**
17  * Tests a bidirectional association A<-->B
18  * @see org.apache.ojb.broker.BidirectionalAssociationTest for equivalent test in PB API
19  */

20 public class BidirectionalAssociationTest extends ODMGTestCase
21 {
22     public static void main(String JavaDoc[] args)
23     {
24         String JavaDoc[] arr = {BidirectionalAssociationTest.class.getName()};
25         junit.textui.TestRunner.main(arr);
26     }
27
28     public void testDeleteA() throws Exception JavaDoc
29     {
30         /**
31          * create at least one A/B
32          */

33         createWithUpdate();
34         deleteA();
35     }
36
37     public void testDeleteB() throws Exception JavaDoc
38     {
39         /**
40          * create at least one A/B
41          */

42         createWithUpdate();
43         deleteB();
44     }
45
46     public void testCreateWitUpdate() throws Exception JavaDoc
47     {
48         createWithUpdate();
49     }
50
51     /**
52      * test that we can create 2 objects that have a bidirectional association in ODMG API
53      */

54     public void createWithUpdate() throws ODMGException
55     {
56         Transaction tx = odmg.newTransaction();
57         long currentTime = System.currentTimeMillis();
58
59         ObjectA a = new ObjectA();
60         a.setPk("A" + currentTime);
61         ObjectB b = new ObjectB();
62         b.setPk("B" + currentTime);
63
64         tx.begin();
65         database.makePersistent(a);
66         database.makePersistent(b);
67         tx.commit();
68
69         tx.begin();
70         tx.lock(a, Transaction.WRITE);
71         tx.lock(b, Transaction.WRITE);
72         a.setRelatedB(b);
73         b.setRelatedA(a);
74         tx.commit();
75
76          /**
77          * now make sure they are in db, A first, then B
78          */

79         tx.begin();
80         OQLQuery query = odmg.newOQLQuery();
81         int i = 0;
82         query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName() + " where pk=$1");
83         query.bind("A"+currentTime);
84         Collection JavaDoc all = (Collection JavaDoc) query.execute();
85         Iterator JavaDoc it = all.iterator();
86         while (it.hasNext())
87         {
88             i++;
89             a = (ObjectA) it.next();
90             if (a.getRelatedB() == null)
91                 fail("a should have had a related b");
92         }
93         if (i > 1)
94             fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i);
95
96         query = odmg.newOQLQuery();
97         i = 0;
98         query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName() + " where pk=$1");
99         query.bind("B"+currentTime);
100         all = (Collection JavaDoc) query.execute();
101         it = all.iterator();
102         while (it.hasNext())
103         {
104             i++;
105             b = (ObjectB) it.next();
106             if (b.getRelatedA() == null)
107                 fail("b should have had a related a");
108
109         }
110         if (i > 1)
111             fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i);
112         tx.commit();
113     }
114
115     /**
116      * this test doesn't work as OJB won't do the insert then execute the update.
117      * @throws ODMGException
118      */

119     public void testCreateWithoutUpdate() throws ODMGException
120     {
121         Transaction tx = odmg.newTransaction();
122         long currentTime = System.currentTimeMillis();
123         ObjectA a = new ObjectA();
124         a.setPk("A" + currentTime);
125         ObjectB b = new ObjectB();
126         b.setPk("B" + currentTime);
127
128         tx.begin();
129         b.setRelatedA(a);
130         a.setRelatedB(b);
131         // we use a FK from ObjectB to ObjectA, thus we
132
// make persistent B
133
database.makePersistent(b);
134         // not needed
135
//database.makePersistent(a);
136
tx.commit();
137
138         /**
139          * now make sure they are in db, A first, then B
140          */

141         tx.begin();
142         OQLQuery query = odmg.newOQLQuery();
143         int i = 0;
144         query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName() + " where pk=$1");
145         query.bind("A"+currentTime);
146          Collection JavaDoc all = (Collection JavaDoc) query.execute();
147         Iterator JavaDoc it = all.iterator();
148         while (it.hasNext())
149         {
150             i++;
151             it.next();
152         }
153         if (i > 1)
154             fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i);
155
156         query = odmg.newOQLQuery();
157         i = 0;
158         query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName() + " where pk=$1");
159         query.bind("B"+currentTime);
160         all = (Collection JavaDoc) query.execute();
161         it = all.iterator();
162         while (it.hasNext())
163         {
164             i++;
165             it.next();
166         }
167         if (i > 1)
168             fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i);
169
170     }
171
172     /**
173      * no clue why this isn't working.
174      * @throws Exception
175      */

176     public void testGetA() throws Exception JavaDoc
177     {
178         /**
179          * create at least one A/B combo
180          */

181         deleteA();
182         deleteB();
183         createWithUpdate();
184
185         OQLQuery query = odmg.newOQLQuery();
186         int i = 0;
187         query.create("select allA from " + ObjectA.class.getName());
188         Transaction tx = odmg.newTransaction();
189         tx.begin();
190         Collection JavaDoc all = (Collection JavaDoc) query.execute();
191         Iterator JavaDoc it = all.iterator();
192         ObjectA temp = null;
193         while (it.hasNext())
194         {
195             temp = (ObjectA) it.next();
196             if (temp.getRelatedB() == null)
197                 fail("should have relatedB");
198             i++;
199         }
200         tx.commit();
201         if (i == 0)
202             fail("Should have found at least 1 bidirectionalAssociationObjectA object");
203     }
204
205     public void testGetB() throws Exception JavaDoc
206     {
207         /**
208          * create at least one A/B combo
209          */

210         deleteA();
211         deleteB();
212         createWithUpdate();
213
214         OQLQuery query = odmg.newOQLQuery();
215         int i = 0;
216         query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName());
217         Transaction tx = odmg.newTransaction();
218         tx.begin();
219         Collection JavaDoc all = (Collection JavaDoc) query.execute();
220         Iterator JavaDoc it = all.iterator();
221         ObjectB temp = null;
222         while (it.hasNext())
223         {
224             temp = (ObjectB) it.next();
225             if (temp.getRelatedA() == null)
226                 fail("should have relatedA");
227             i++;
228         }
229         tx.commit();
230         if (i == 0)
231             fail("Should have found at least 1 bidirectionalAssociationObjectA object");
232     }
233
234     /**
235      * test deleting an object participating in a bidirectional associative relationship. Will throw if it can't delete.
236      * @throws Exception
237      */

238     public void deleteA() throws Exception JavaDoc
239     {
240         ObjectA a;
241         ObjectB b;
242
243         OQLQuery query = odmg.newOQLQuery();
244         query.create("select bidirectionalAssociationObjectA from " + ObjectA.class.getName());
245         TransactionExt tx = (TransactionExt) odmg.newTransaction();
246         tx.begin();
247         Collection JavaDoc all = (Collection JavaDoc) query.execute();
248         Iterator JavaDoc it = all.iterator();
249
250         while (it.hasNext())
251         {
252             a = (ObjectA)it.next();
253             b = a.getRelatedB();
254             if (b != null)
255             {
256                 tx.lock(b, Transaction.WRITE);
257                 b.setRelatedA(null); // break relationship to avoid ri violation
258
}
259             database.deletePersistent(a);
260         }
261         tx.commit();
262     }
263
264     /**
265      * test deleting an object participating in a bidirectional associative relationship. Will throw if it can't delete.
266      * @throws Exception
267      */

268     public void deleteB() throws Exception JavaDoc
269     {
270         ObjectA a;
271         ObjectB b;
272
273         OQLQuery query = odmg.newOQLQuery();
274         query.create("select bidirectionalAssociationObjectB from " + ObjectB.class.getName());
275         Transaction tx = odmg.newTransaction();
276         tx.begin();
277         Collection JavaDoc all = (Collection JavaDoc) query.execute();
278         Iterator JavaDoc it = all.iterator();
279
280         while (it.hasNext())
281         {
282             b = (ObjectB)it.next();
283             a = b.getRelatedA();
284             if (a != null)
285             {
286                 tx.lock(a, Transaction.WRITE);
287                 a.setRelatedB(null); // break relationship to avoid ri violation
288
}
289             database.deletePersistent(b);
290         }
291
292         tx.commit();
293     }
294
295     /**
296      * Insert the method's description here.
297      * Creation date: (24.12.2000 00:33:40)
298      */

299     public BidirectionalAssociationTest(String JavaDoc name)
300     {
301         super(name);
302     }
303
304     public void tearDown() throws Exception JavaDoc
305     {
306         super.tearDown();
307     }
308
309
310
311     public static class ObjectA implements Serializable JavaDoc
312     {
313         private String JavaDoc pk;
314         private String JavaDoc fkToB;
315         private ObjectB relatedB;
316
317         public ObjectA()
318         {
319         }
320
321         public String JavaDoc getPk()
322         {
323             return pk;
324         }
325
326         public void setPk(String JavaDoc pk)
327         {
328             this.pk = pk;
329         }
330
331         public String JavaDoc getFkToB()
332         {
333             return fkToB;
334         }
335
336         public void setFkToB(String JavaDoc fkToB)
337         {
338             this.fkToB = fkToB;
339         }
340
341         public ObjectB getRelatedB()
342         {
343             return relatedB;
344         }
345
346         public void setRelatedB(ObjectB relatedB)
347         {
348             this.relatedB = relatedB;
349         }
350     }
351
352
353     public static class ObjectB implements Serializable JavaDoc
354     {
355         private String JavaDoc pk;
356         private String JavaDoc fkToA;
357         private ObjectA relatedA;
358
359         public ObjectB()
360         {
361         }
362
363         public String JavaDoc getPk()
364         {
365             return pk;
366         }
367
368         public void setPk(String JavaDoc pk)
369         {
370             this.pk = pk;
371         }
372
373         public String JavaDoc getFkToA()
374         {
375             return fkToA;
376         }
377
378         public void setFkToA(String JavaDoc fkToA)
379         {
380             this.fkToA = fkToA;
381         }
382
383         public ObjectA getRelatedA()
384         {
385             return relatedA;
386         }
387
388         public void setRelatedA(ObjectA relatedA)
389         {
390             this.relatedA = relatedA;
391         }
392     }
393 }
394
Popular Tags