KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.odmg;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.ojb.broker.Fish;
6 import org.apache.ojb.broker.Salad;
7 import org.apache.ojb.junit.ODMGTestCase;
8 import org.apache.ojb.odmg.shared.ODMGGourmet;
9 import org.odmg.OQLQuery;
10 import org.odmg.Transaction;
11
12 public class ManyToManyTest extends ODMGTestCase
13 {
14     public static void main(String JavaDoc[] args)
15     {
16         String JavaDoc[] arr = {ManyToManyTest.class.getName()};
17         junit.textui.TestRunner.main(arr);
18     }
19
20     public ManyToManyTest(String JavaDoc name)
21     {
22         super(name);
23     }
24
25     /**
26      * this tests if polymorph collections (i.e. collections of objects
27      * implementing a common interface) are treated correctly
28      */

29     public void testPolymorphMToN() throws Exception JavaDoc
30     {
31         String JavaDoc postfix = "testPolymorphMToN_" + System.currentTimeMillis();
32         ODMGGourmet james = new ODMGGourmet(postfix + "_james");
33         ODMGGourmet doris = new ODMGGourmet(postfix + "_doris");
34
35         Fish tuna = new Fish(postfix + "_tuna", 242, "salt");
36         Fish trout = new Fish(postfix + "_trout", 52, "fresh water");
37
38         Salad radiccio = new Salad(postfix + "_Radiccio", 7, "red");
39         Salad lolloverde = new Salad(postfix + "_Lollo verde", 7, "green");
40
41         james.addFavoriteFood(tuna);
42         james.addFavoriteFood(radiccio);
43
44         doris.addFavoriteFood(tuna);
45         doris.addFavoriteFood(trout);
46         doris.addFavoriteFood(lolloverde);
47
48         TransactionExt tx = (TransactionExt) odmg.newTransaction();
49         tx.begin();
50         database.makePersistent(james);
51         database.makePersistent(doris);
52         tx.commit();
53
54         int dorisId = doris.getGourmetId();
55         int jamesId = james.getGourmetId();
56
57         tx = (TransactionExt) odmg.newTransaction();
58         tx.begin();
59         tx.getBroker().clearCache();
60         
61         OQLQuery query = odmg.newOQLQuery();
62         query.create("select gourmets from " + ODMGGourmet.class.getName() +
63                 " where gourmetId=$1");
64         query.bind(new Integer JavaDoc(dorisId));
65         List JavaDoc gourmets = (List JavaDoc) query.execute();
66         tx.commit();
67         assertEquals(1, gourmets.size());
68         ODMGGourmet loadedDoris = (ODMGGourmet) gourmets.get(0);
69         //System.err.println(loadedDoris);
70
assertEquals(3, loadedDoris.getFavoriteFood().size());
71
72         tx.begin();
73         query = odmg.newOQLQuery();
74         query.create("select gourmets from " + ODMGGourmet.class.getName() +
75                 " where gourmetId=$1");
76         query.bind(new Integer JavaDoc(jamesId));
77         gourmets = (List JavaDoc) query.execute();
78         tx.commit();
79         assertEquals(1, gourmets.size());
80         ODMGGourmet loadedJames = (ODMGGourmet) gourmets.get(0);
81         //System.err.println(loadedJames);
82
assertEquals(2, loadedJames.getFavoriteFood().size());
83     }
84
85 // /**
86
// * Store the objects and return the result of the query
87
// * "select gourmets from " + ODMGGourmet.class.getName() + " where gourmetId=$1"
88
// */
89
// private int store(Implementation odmg, Database db, ODMGGourmet gourmet) throws Exception
90
// {
91
// Transaction tx = odmg.newTransaction();
92
// tx.begin();
93
// db.makePersistent(gourmet);
94
// tx.commit();
95
//
96
// tx.begin();
97
// OQLQuery query = odmg.newOQLQuery();
98
// query = odmg.newOQLQuery();
99
// query.create("select gourmets from " + ODMGGourmet.class.getName() +
100
// " where gourmetId=$1");
101
// query.bind(new Integer(gourmet.getGourmetId()));
102
// List gourmets = (List) query.execute();
103
// tx.commit();
104
// return gourmets.size();
105
// }
106

107     public void testMtoNSeparate_I() throws Exception JavaDoc
108     {
109         ODMGGourmet paula = new ODMGGourmet("a_testMtoNSeparate_I");
110         ODMGGourmet candy = new ODMGGourmet("b_testMtoNSeparate_I");
111
112         long timestamp = System.currentTimeMillis();
113         Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
114         Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
115
116         paula.addFavoriteFood(trout);
117         candy.addFavoriteFood(tuna);
118
119         TransactionExt tx = (TransactionExt) odmg.newTransaction();
120         tx.begin();
121         database.makePersistent(paula);
122         database.makePersistent(candy);
123         tx.commit();
124
125         OQLQuery query = odmg.newOQLQuery();
126         query.create("select fishs from " + Fish.class.getName() +
127                 " where (name=$1 or name=$2)");
128         query.bind(tuna.getName());
129         query.bind(trout.getName());
130         List JavaDoc fishs = (List JavaDoc) query.execute();
131         /*
132         we expect 2 created 'fish'
133         */

134         assertEquals(2, fishs.size());
135     }
136
137     public void testMtoNSeparate_II() throws Exception JavaDoc
138     {
139         ODMGGourmet james = new ODMGGourmet("a_testMtoNSeparate_II");
140         ODMGGourmet doris = new ODMGGourmet("b_testMtoNSeparate_II");
141
142         long timestamp = System.currentTimeMillis();
143         Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
144         Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
145
146         james.addFavoriteFood(tuna);
147
148         doris.addFavoriteFood(tuna);
149         doris.addFavoriteFood(trout);
150
151         TransactionExt tx = (TransactionExt) odmg.newTransaction();
152         tx.begin();
153         database.makePersistent(james);
154         database.makePersistent(doris);
155         tx.commit();
156
157         OQLQuery query = odmg.newOQLQuery();
158         query.create("select fishs from " + Fish.class.getName() +
159                 " where (name=$1 or name=$2)");
160         query.bind(tuna.getName());
161         query.bind(trout.getName());
162         List JavaDoc fishs = (List JavaDoc) query.execute();
163         /*
164         we expect 2 created 'fish'
165         */

166         assertEquals(2, fishs.size());
167     }
168
169     public void testMtoNTogether() throws Exception JavaDoc
170     {
171         long timestamp = System.currentTimeMillis();
172         Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
173         Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
174
175         ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether");
176         ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether");
177         ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether");
178         ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether");
179
180         paula.addFavoriteFood(trout);
181         candy.addFavoriteFood(tuna);
182         james.addFavoriteFood(tuna);
183         doris.addFavoriteFood(tuna);
184         doris.addFavoriteFood(trout);
185
186         TransactionExt tx = (TransactionExt) odmg.newTransaction();
187         tx.begin();
188         database.makePersistent(james);
189         database.makePersistent(doris);
190         database.makePersistent(candy);
191         database.makePersistent(paula);
192         tx.commit();
193
194         OQLQuery query = odmg.newOQLQuery();
195         query.create("select fishs from " + Fish.class.getName() +
196                 " where (name=$1 or name=$2)");
197         query.bind(tuna.getName());
198         query.bind(trout.getName());
199         List JavaDoc fishs = (List JavaDoc) query.execute();
200         /*
201         we expect 2 created 'fish'
202         */

203         assertEquals(2, fishs.size());
204     }
205
206     /**
207      * main object gourment has list of food objects, this
208      * test check if we add one food object to the list
209      * and lock the main object, do get an updated list
210      */

211     public void testMtoNPolymorphUpdate() throws Exception JavaDoc
212     {
213         long timestamp = System.currentTimeMillis();
214         Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
215         Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
216         Fish goldfish = new Fish("goldfish_" + timestamp, 10, "brackish water");
217
218         ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether"+ timestamp);
219         ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether"+ timestamp);
220         ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether"+ timestamp);
221         ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether"+ timestamp);
222
223         paula.addFavoriteFood(trout);
224         candy.addFavoriteFood(tuna);
225         james.addFavoriteFood(tuna);
226         doris.addFavoriteFood(tuna);
227         doris.addFavoriteFood(trout);
228
229         TransactionExt tx = (TransactionExt) odmg.newTransaction();
230         tx.begin();
231         database.makePersistent(james);
232         database.makePersistent(doris);
233         database.makePersistent(candy);
234         database.makePersistent(paula);
235         tx.commit();
236
237         OQLQuery query = odmg.newOQLQuery();
238         query.create("select fishs from " + Fish.class.getName() +
239                 " where (name=$1 or name=$2)");
240         query.bind(tuna.getName());
241         query.bind(trout.getName());
242         List JavaDoc fishs = (List JavaDoc) query.execute();
243         /*
244         we expect 2 created 'fish'
245         */

246         assertEquals(2, fishs.size());
247
248         tx = (TransactionExt) odmg.newTransaction();
249         tx.begin();
250         query = odmg.newOQLQuery();
251         query.create("select gourmets from " + ODMGGourmet.class.getName() +
252                 " where name=$1");
253         query.bind(doris.getName());
254         List JavaDoc result = (List JavaDoc) query.execute();
255         assertEquals("We should found a gourmet", 1, result.size());
256         ODMGGourmet gourmet = (ODMGGourmet)result.get(0);
257
258         /*
259         now we lock main object and add a new reference object
260         */

261         tx.lock(gourmet, Transaction.WRITE);
262         gourmet.addFavoriteFood(goldfish);
263         tx.commit();
264
265         query = odmg.newOQLQuery();
266         query.create("select fishs from " + Fish.class.getName() +
267                 " where (name=$1 or name=$2 or name=$3)");
268         query.bind(tuna.getName());
269         query.bind(trout.getName());
270         query.bind(goldfish.getName());
271
272         tx = (TransactionExt) odmg.newTransaction();
273         tx.begin();
274         fishs = (List JavaDoc) query.execute();
275         tx.commit();
276         assertEquals("seems referenced object was not added (if found <3) ",3, fishs.size());
277     }
278
279     /**
280      * main object gourment has list of food objects, this
281      * test check if we delete one food object from the list
282      * and lock the main object, do get an updated list
283      */

284     public void testMtoNPolymorphDelete() throws Exception JavaDoc
285     {
286         long timestamp = System.currentTimeMillis();
287         String JavaDoc name = "testMtoNPolymorphDelete_" + timestamp;
288         Fish tuna = new Fish(name + "_tuna", 242, "salt");
289         Fish trout = new Fish(name + "_trout", 52, "fresh water");
290         Fish goldfish = new Fish(name + "_goldfish", 10, "brackish water");
291
292         ODMGGourmet paula = new ODMGGourmet(name + "_paula");
293         ODMGGourmet candy = new ODMGGourmet(name + "_candy");
294         ODMGGourmet james = new ODMGGourmet(name + "_james");
295         ODMGGourmet doris = new ODMGGourmet(name + "_doris");
296
297         paula.addFavoriteFood(trout);
298         candy.addFavoriteFood(tuna);
299         james.addFavoriteFood(tuna);
300         doris.addFavoriteFood(tuna);
301         doris.addFavoriteFood(trout);
302         doris.addFavoriteFood(goldfish);
303
304         /*
305         we expect one created 'gourment' per store
306         */

307         TransactionExt tx = (TransactionExt) odmg.newTransaction();
308         tx.begin();
309         database.makePersistent(paula);
310         database.makePersistent(james);
311         database.makePersistent(candy);
312         database.makePersistent(doris);
313         tx.commit();
314
315         tx.begin();
316         OQLQuery query = odmg.newOQLQuery();
317         query.create("select fishs from " + Fish.class.getName() +
318                 " where (name=$1 or name=$2 or name=$3)");
319         query.bind(tuna.getName());
320         query.bind(trout.getName());
321         query.bind(goldfish.getName());
322
323         List JavaDoc fishs = (List JavaDoc) query.execute();
324         tx.commit();
325         /*
326         we expect 3 created 'fish'
327         */

328         assertEquals(3, fishs.size());
329
330         tx = (TransactionExt) odmg.newTransaction();
331         tx.begin();
332         query = odmg.newOQLQuery();
333         query.create("select gourmets from " + ODMGGourmet.class.getName() +
334                 " where name=$1");
335         query.bind(doris.getName());
336         List JavaDoc result = (List JavaDoc) query.execute();
337         assertEquals("We should found a gourmet_doris", 1, result.size());
338         ODMGGourmet gourmet_doris = (ODMGGourmet)result.get(0);
339         assertEquals(name + "_doris", gourmet_doris.getName());
340         assertEquals(3, gourmet_doris.getFavoriteFood().size());
341
342         /*
343         now we lock main object and add remove a reference object
344         */

345         tx.lock(gourmet_doris, Transaction.WRITE);
346         List JavaDoc foodList = gourmet_doris.getFavoriteFood();
347         foodList.remove(0);
348         //gourmet_doris.setFavoriteFood(foodList);
349
tx.commit();
350
351         query = odmg.newOQLQuery();
352         query.create("select gourmets from " + ODMGGourmet.class.getName() +
353                 " where name=$1");
354         query.bind(doris.getName());
355
356         tx = (TransactionExt) odmg.newTransaction();
357         tx.begin();
358         result = (List JavaDoc) query.execute();
359         assertEquals("We should found a gourmet_doris", 1, result.size());
360         gourmet_doris = (ODMGGourmet)result.get(0);
361         tx.commit();
362         assertEquals(
363             "We removed one fish, so doris should only have two entries left",
364             2, gourmet_doris.getFavoriteFood().size());
365     }
366 }
367
Popular Tags