001: package org.apache.ojb.odmg;
002:
003: import java.util.List;
004:
005: import org.apache.ojb.broker.Fish;
006: import org.apache.ojb.broker.Salad;
007: import org.apache.ojb.junit.ODMGTestCase;
008: import org.apache.ojb.odmg.shared.ODMGGourmet;
009: import org.odmg.OQLQuery;
010: import org.odmg.Transaction;
011:
012: public class ManyToManyTest extends ODMGTestCase {
013: public static void main(String[] args) {
014: String[] arr = { ManyToManyTest.class.getName() };
015: junit.textui.TestRunner.main(arr);
016: }
017:
018: public ManyToManyTest(String name) {
019: super (name);
020: }
021:
022: /**
023: * this tests if polymorph collections (i.e. collections of objects
024: * implementing a common interface) are treated correctly
025: */
026: public void testPolymorphMToN() throws Exception {
027: String postfix = "testPolymorphMToN_"
028: + System.currentTimeMillis();
029: ODMGGourmet james = new ODMGGourmet(postfix + "_james");
030: ODMGGourmet doris = new ODMGGourmet(postfix + "_doris");
031:
032: Fish tuna = new Fish(postfix + "_tuna", 242, "salt");
033: Fish trout = new Fish(postfix + "_trout", 52, "fresh water");
034:
035: Salad radiccio = new Salad(postfix + "_Radiccio", 7, "red");
036: Salad lolloverde = new Salad(postfix + "_Lollo verde", 7,
037: "green");
038:
039: james.addFavoriteFood(tuna);
040: james.addFavoriteFood(radiccio);
041:
042: doris.addFavoriteFood(tuna);
043: doris.addFavoriteFood(trout);
044: doris.addFavoriteFood(lolloverde);
045:
046: TransactionExt tx = (TransactionExt) odmg.newTransaction();
047: tx.begin();
048: database.makePersistent(james);
049: database.makePersistent(doris);
050: tx.commit();
051:
052: int dorisId = doris.getGourmetId();
053: int jamesId = james.getGourmetId();
054:
055: tx = (TransactionExt) odmg.newTransaction();
056: tx.begin();
057: tx.getBroker().clearCache();
058:
059: OQLQuery query = odmg.newOQLQuery();
060: query.create("select gourmets from "
061: + ODMGGourmet.class.getName() + " where gourmetId=$1");
062: query.bind(new Integer(dorisId));
063: List gourmets = (List) query.execute();
064: tx.commit();
065: assertEquals(1, gourmets.size());
066: ODMGGourmet loadedDoris = (ODMGGourmet) gourmets.get(0);
067: //System.err.println(loadedDoris);
068: assertEquals(3, loadedDoris.getFavoriteFood().size());
069:
070: tx.begin();
071: query = odmg.newOQLQuery();
072: query.create("select gourmets from "
073: + ODMGGourmet.class.getName() + " where gourmetId=$1");
074: query.bind(new Integer(jamesId));
075: gourmets = (List) query.execute();
076: tx.commit();
077: assertEquals(1, gourmets.size());
078: ODMGGourmet loadedJames = (ODMGGourmet) gourmets.get(0);
079: //System.err.println(loadedJames);
080: assertEquals(2, loadedJames.getFavoriteFood().size());
081: }
082:
083: // /**
084: // * Store the objects and return the result of the query
085: // * "select gourmets from " + ODMGGourmet.class.getName() + " where gourmetId=$1"
086: // */
087: // private int store(Implementation odmg, Database db, ODMGGourmet gourmet) throws Exception
088: // {
089: // Transaction tx = odmg.newTransaction();
090: // tx.begin();
091: // db.makePersistent(gourmet);
092: // tx.commit();
093: //
094: // tx.begin();
095: // OQLQuery query = odmg.newOQLQuery();
096: // query = odmg.newOQLQuery();
097: // query.create("select gourmets from " + ODMGGourmet.class.getName() +
098: // " where gourmetId=$1");
099: // query.bind(new Integer(gourmet.getGourmetId()));
100: // List gourmets = (List) query.execute();
101: // tx.commit();
102: // return gourmets.size();
103: // }
104:
105: public void testMtoNSeparate_I() throws Exception {
106: ODMGGourmet paula = new ODMGGourmet("a_testMtoNSeparate_I");
107: ODMGGourmet candy = new ODMGGourmet("b_testMtoNSeparate_I");
108:
109: long timestamp = System.currentTimeMillis();
110: Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
111: Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
112:
113: paula.addFavoriteFood(trout);
114: candy.addFavoriteFood(tuna);
115:
116: TransactionExt tx = (TransactionExt) odmg.newTransaction();
117: tx.begin();
118: database.makePersistent(paula);
119: database.makePersistent(candy);
120: tx.commit();
121:
122: OQLQuery query = odmg.newOQLQuery();
123: query.create("select fishs from " + Fish.class.getName()
124: + " where (name=$1 or name=$2)");
125: query.bind(tuna.getName());
126: query.bind(trout.getName());
127: List fishs = (List) query.execute();
128: /*
129: we expect 2 created 'fish'
130: */
131: assertEquals(2, fishs.size());
132: }
133:
134: public void testMtoNSeparate_II() throws Exception {
135: ODMGGourmet james = new ODMGGourmet("a_testMtoNSeparate_II");
136: ODMGGourmet doris = new ODMGGourmet("b_testMtoNSeparate_II");
137:
138: long timestamp = System.currentTimeMillis();
139: Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
140: Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
141:
142: james.addFavoriteFood(tuna);
143:
144: doris.addFavoriteFood(tuna);
145: doris.addFavoriteFood(trout);
146:
147: TransactionExt tx = (TransactionExt) odmg.newTransaction();
148: tx.begin();
149: database.makePersistent(james);
150: database.makePersistent(doris);
151: tx.commit();
152:
153: OQLQuery query = odmg.newOQLQuery();
154: query.create("select fishs from " + Fish.class.getName()
155: + " where (name=$1 or name=$2)");
156: query.bind(tuna.getName());
157: query.bind(trout.getName());
158: List fishs = (List) query.execute();
159: /*
160: we expect 2 created 'fish'
161: */
162: assertEquals(2, fishs.size());
163: }
164:
165: public void testMtoNTogether() throws Exception {
166: long timestamp = System.currentTimeMillis();
167: Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
168: Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
169:
170: ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether");
171: ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether");
172: ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether");
173: ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether");
174:
175: paula.addFavoriteFood(trout);
176: candy.addFavoriteFood(tuna);
177: james.addFavoriteFood(tuna);
178: doris.addFavoriteFood(tuna);
179: doris.addFavoriteFood(trout);
180:
181: TransactionExt tx = (TransactionExt) odmg.newTransaction();
182: tx.begin();
183: database.makePersistent(james);
184: database.makePersistent(doris);
185: database.makePersistent(candy);
186: database.makePersistent(paula);
187: tx.commit();
188:
189: OQLQuery query = odmg.newOQLQuery();
190: query.create("select fishs from " + Fish.class.getName()
191: + " where (name=$1 or name=$2)");
192: query.bind(tuna.getName());
193: query.bind(trout.getName());
194: List fishs = (List) query.execute();
195: /*
196: we expect 2 created 'fish'
197: */
198: assertEquals(2, fishs.size());
199: }
200:
201: /**
202: * main object gourment has list of food objects, this
203: * test check if we add one food object to the list
204: * and lock the main object, do get an updated list
205: */
206: public void testMtoNPolymorphUpdate() throws Exception {
207: long timestamp = System.currentTimeMillis();
208: Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
209: Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
210: Fish goldfish = new Fish("goldfish_" + timestamp, 10,
211: "brackish water");
212:
213: ODMGGourmet paula = new ODMGGourmet("a_testMtoNTogether"
214: + timestamp);
215: ODMGGourmet candy = new ODMGGourmet("b_testMtoNTogether"
216: + timestamp);
217: ODMGGourmet james = new ODMGGourmet("c_testMtoNTogether"
218: + timestamp);
219: ODMGGourmet doris = new ODMGGourmet("d_testMtoNTogether"
220: + timestamp);
221:
222: paula.addFavoriteFood(trout);
223: candy.addFavoriteFood(tuna);
224: james.addFavoriteFood(tuna);
225: doris.addFavoriteFood(tuna);
226: doris.addFavoriteFood(trout);
227:
228: TransactionExt tx = (TransactionExt) odmg.newTransaction();
229: tx.begin();
230: database.makePersistent(james);
231: database.makePersistent(doris);
232: database.makePersistent(candy);
233: database.makePersistent(paula);
234: tx.commit();
235:
236: OQLQuery query = odmg.newOQLQuery();
237: query.create("select fishs from " + Fish.class.getName()
238: + " where (name=$1 or name=$2)");
239: query.bind(tuna.getName());
240: query.bind(trout.getName());
241: List fishs = (List) query.execute();
242: /*
243: we expect 2 created 'fish'
244: */
245: assertEquals(2, fishs.size());
246:
247: tx = (TransactionExt) odmg.newTransaction();
248: tx.begin();
249: query = odmg.newOQLQuery();
250: query.create("select gourmets from "
251: + ODMGGourmet.class.getName() + " where name=$1");
252: query.bind(doris.getName());
253: List result = (List) query.execute();
254: assertEquals("We should found a gourmet", 1, result.size());
255: ODMGGourmet gourmet = (ODMGGourmet) result.get(0);
256:
257: /*
258: now we lock main object and add a new reference object
259: */
260: tx.lock(gourmet, Transaction.WRITE);
261: gourmet.addFavoriteFood(goldfish);
262: tx.commit();
263:
264: query = odmg.newOQLQuery();
265: query.create("select fishs from " + Fish.class.getName()
266: + " where (name=$1 or name=$2 or name=$3)");
267: query.bind(tuna.getName());
268: query.bind(trout.getName());
269: query.bind(goldfish.getName());
270:
271: tx = (TransactionExt) odmg.newTransaction();
272: tx.begin();
273: fishs = (List) query.execute();
274: tx.commit();
275: assertEquals(
276: "seems referenced object was not added (if found <3) ",
277: 3, fishs.size());
278: }
279:
280: /**
281: * main object gourment has list of food objects, this
282: * test check if we delete one food object from the list
283: * and lock the main object, do get an updated list
284: */
285: public void testMtoNPolymorphDelete() throws Exception {
286: long timestamp = System.currentTimeMillis();
287: String 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,
291: "brackish water");
292:
293: ODMGGourmet paula = new ODMGGourmet(name + "_paula");
294: ODMGGourmet candy = new ODMGGourmet(name + "_candy");
295: ODMGGourmet james = new ODMGGourmet(name + "_james");
296: ODMGGourmet doris = new ODMGGourmet(name + "_doris");
297:
298: paula.addFavoriteFood(trout);
299: candy.addFavoriteFood(tuna);
300: james.addFavoriteFood(tuna);
301: doris.addFavoriteFood(tuna);
302: doris.addFavoriteFood(trout);
303: doris.addFavoriteFood(goldfish);
304:
305: /*
306: we expect one created 'gourment' per store
307: */
308: TransactionExt tx = (TransactionExt) odmg.newTransaction();
309: tx.begin();
310: database.makePersistent(paula);
311: database.makePersistent(james);
312: database.makePersistent(candy);
313: database.makePersistent(doris);
314: tx.commit();
315:
316: tx.begin();
317: OQLQuery query = odmg.newOQLQuery();
318: query.create("select fishs from " + Fish.class.getName()
319: + " where (name=$1 or name=$2 or name=$3)");
320: query.bind(tuna.getName());
321: query.bind(trout.getName());
322: query.bind(goldfish.getName());
323:
324: List fishs = (List) query.execute();
325: tx.commit();
326: /*
327: we expect 3 created 'fish'
328: */
329: assertEquals(3, fishs.size());
330:
331: tx = (TransactionExt) odmg.newTransaction();
332: tx.begin();
333: query = odmg.newOQLQuery();
334: query.create("select gourmets from "
335: + ODMGGourmet.class.getName() + " where name=$1");
336: query.bind(doris.getName());
337: List result = (List) query.execute();
338: assertEquals("We should found a gourmet_doris", 1, result
339: .size());
340: ODMGGourmet gourmet_doris = (ODMGGourmet) result.get(0);
341: assertEquals(name + "_doris", gourmet_doris.getName());
342: assertEquals(3, gourmet_doris.getFavoriteFood().size());
343:
344: /*
345: now we lock main object and add remove a reference object
346: */
347: tx.lock(gourmet_doris, Transaction.WRITE);
348: List foodList = gourmet_doris.getFavoriteFood();
349: foodList.remove(0);
350: //gourmet_doris.setFavoriteFood(foodList);
351: tx.commit();
352:
353: query = odmg.newOQLQuery();
354: query.create("select gourmets from "
355: + ODMGGourmet.class.getName() + " where name=$1");
356: query.bind(doris.getName());
357:
358: tx = (TransactionExt) odmg.newTransaction();
359: tx.begin();
360: result = (List) query.execute();
361: assertEquals("We should found a gourmet_doris", 1, result
362: .size());
363: gourmet_doris = (ODMGGourmet) result.get(0);
364: tx.commit();
365: assertEquals(
366: "We removed one fish, so doris should only have two entries left",
367: 2, gourmet_doris.getFavoriteFood().size());
368: }
369: }
|