KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created by IntelliJ IDEA.
3  * User: Matt
4  * Date: May 19, 2002
5  * Time: 3:43:36 PM
6  * To change template for new class use
7  * Code Style | Class Templates options (Tools | IDE Options).
8  */

9 package org.apache.ojb.odmg;
10
11 import java.sql.Timestamp JavaDoc;
12
13 import org.apache.ojb.broker.Contract;
14 import org.apache.ojb.broker.Effectiveness;
15 import org.apache.ojb.broker.ManageableCollection;
16 import org.apache.ojb.broker.RelatedToContract;
17 import org.apache.ojb.broker.Version;
18 import org.apache.ojb.junit.ODMGTestCase;
19 import org.odmg.Database;
20 import org.odmg.Implementation;
21 import org.odmg.OQLQuery;
22 import org.odmg.Transaction;
23
24 public class ContractVersionEffectivenessOQLTest extends ODMGTestCase
25 {
26
27     private static Class JavaDoc CLASS = ContractVersionEffectivenessOQLTest.class;
28     private int COUNT = 10;
29
30     /**
31      * Insert the method's description here.
32      * Creation date: (23.12.2000 18:30:38)
33      * @param args java.lang.String[]
34      */

35     public static void main(String JavaDoc[] args)
36     {
37         String JavaDoc[] arr = { CLASS.getName()};
38         junit.textui.TestRunner.main(arr);
39     }
40
41     public ContractVersionEffectivenessOQLTest(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     private void createData(Database db, Implementation odmg)
47             throws Exception JavaDoc
48     {
49 // Implementation odmg = OJB.getInstance();
50
Transaction tx = odmg.newTransaction();
51         for (int i = 0; i < COUNT; i++)
52         {
53             tx.begin();
54             Contract contract = new Contract();
55             contract.setPk("C" + i + System.currentTimeMillis());
56             contract.setContractValue1("contractvalue1");
57             contract.setContractValue2(1);
58             contract.setContractValue3("contractvalue3");
59             contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
60             db.makePersistent(contract);
61
62             Version version = new Version();
63             version.setPk("V" + i + System.currentTimeMillis());
64             version.setVersionValue1("versionvalue1");
65             version.setVersionValue2(1);
66             version.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
67             version.setContract(contract);
68             db.makePersistent(version);
69
70             Effectiveness eff = new Effectiveness();
71             eff.setPk("E" + i + System.currentTimeMillis());
72             eff.setEffValue1("effvalue1");
73             eff.setEffValue2(1);
74             eff.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
75             eff.setVersion(version);
76             /**
77              * will create all
78              */

79             db.makePersistent(eff);
80             tx.commit();
81         }
82
83     }
84
85     public void testCreate() throws Exception JavaDoc
86     {
87         createData(database, odmg);
88     }
89
90     public void testComplexOQL()
91             throws Exception JavaDoc
92     {
93         /**
94          * 1. create the objects with specific values we'll search on later.
95          */

96         Transaction tx = odmg.newTransaction();
97         tx.begin();
98
99         Contract contract = new Contract();
100         contract.setPk("C" + System.currentTimeMillis());
101         contract.setContractValue1("version.contract.contractValue1.testComplexOQL");
102         contract.setContractValue2(1);
103         contract.setContractValue3("contractvalue3");
104         contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
105         database.makePersistent(contract);
106
107         RelatedToContract rtc = new RelatedToContract();
108         rtc.setPk("R" + System.currentTimeMillis());
109         rtc.setRelatedValue1("test");
110         rtc.setRelatedValue2(5);
111         rtc.setRelatedValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
112         contract.setRelatedToContract(rtc);
113         database.makePersistent(rtc);
114
115         Version version = new Version();
116         version.setPk("V" + System.currentTimeMillis());
117         version.setVersionValue1("versionvalue1");
118         version.setVersionValue2(1);
119         version.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
120         version.setContract(contract);
121         database.makePersistent(version);
122
123         Effectiveness eff = new Effectiveness();
124         eff.setPk("E" + System.currentTimeMillis());
125         eff.setEffValue1("effValue1.testComplexOQL");
126         eff.setEffValue2(20);
127         eff.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
128         eff.setVersion(version);
129         database.makePersistent(eff);
130
131         tx.commit();
132         /**
133          * 2. define the complex OQL query to find the object we created
134          */

135         String JavaDoc oql = "select s from " + org.apache.ojb.broker.Effectiveness.class.getName() + " where " +
136                 " version.contract.contractValue1=$1 and effValue1 = $2 and " +
137                 " (effValue3 > $3 or is_undefined(effValue3)) and " +
138                 " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and " +
139                 " version.contract.relatedToContract.relatedValue2=$6";
140
141         OQLQuery query = odmg.newOQLQuery();
142         query.create(oql);
143         query.bind("version.contract.contractValue1.testComplexOQL"); //version.contract.contractValue1=$1
144
query.bind("effValue1.testComplexOQL"); // effValue1 = $2
145
query.bind(new Timestamp JavaDoc(System.currentTimeMillis() - 5000)); // a while ago (effValue3 > $3)
146
query.bind(new Integer JavaDoc(20)); // effValue2 <= $4
147
query.bind(new Timestamp JavaDoc(System.currentTimeMillis() + 5000)); // a while from now (effValue3<$5)
148
query.bind(new Integer JavaDoc(5)); // version.contract.relatedToContract.relatedValue2=$6
149

150         ManageableCollection all = (ManageableCollection) query.execute();
151         java.util.Iterator JavaDoc it = all.ojbIterator();
152         /**
153          * make sure we got
154          */

155         int i = 0;
156         while (it.hasNext())
157         {
158             it.next();
159             i++;
160         }
161         if (i != 1)
162         {
163             fail("Should have found just one object, instead found " + i);
164         }
165     }
166
167      public void testComplexOQL2()
168             throws Exception JavaDoc
169     {
170         /**
171          * 1. create the objects with specific values we'll search on later.
172          */

173         Transaction tx = odmg.newTransaction();
174         tx.begin();
175
176         Contract contract = new Contract();
177         contract.setPk("C" + System.currentTimeMillis());
178         contract.setContractValue1("version.contract.contractValue1.testComplexOQL");
179         contract.setContractValue2(1);
180         contract.setContractValue3("contractvalue3");
181         contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
182         database.makePersistent(contract);
183
184         Version version = new Version();
185         version.setPk("V" + System.currentTimeMillis());
186         version.setVersionValue1("versionvalue1");
187         version.setVersionValue2(1);
188         version.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
189         version.setContract(contract);
190         database.makePersistent(version);
191
192         Effectiveness eff = new Effectiveness();
193         eff.setPk("E" + System.currentTimeMillis());
194         eff.setEffValue1("effValue1.testComplexOQL");
195         eff.setEffValue2(20);
196         eff.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
197         eff.setVersion(version);
198         database.makePersistent(eff);
199
200         tx.commit();
201         /**
202          * 2. define the complex OQL query to find the object we created
203          */

204         String JavaDoc oql = "select s from " + org.apache.ojb.broker.Effectiveness.class.getName() + " where " +
205                 " version.contract.contractValue1=$1 and effValue1 = $2 and " +
206                 " (effValue3 > $3 or is_undefined(effValue3)) and " +
207                 " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and " +
208                 " is_undefined(version.contract.relatedToContract.pk)";
209
210         OQLQuery query = odmg.newOQLQuery();
211         query.create(oql);
212         query.bind("version.contract.contractValue1.testComplexOQL"); //version.contract.contractValue1=$1
213
query.bind("effValue1.testComplexOQL"); // effValue1 = $2
214
query.bind(new Timestamp JavaDoc(System.currentTimeMillis() - 5000)); // a while ago (effValue3 > $3)
215
query.bind(new Integer JavaDoc(20)); // effValue2 <= $4
216
query.bind(new Timestamp JavaDoc(System.currentTimeMillis() + 5000)); // a while from now (effValue3<$5)
217

218         ManageableCollection all = (ManageableCollection) query.execute();
219         java.util.Iterator JavaDoc it = all.ojbIterator();
220         /**
221          * make sure we got
222          */

223         int i = 0;
224         while (it.hasNext())
225         {
226             it.next();
227             i++;
228         }
229         if (i != 1)
230         {
231             fail("Should have found just one object, instead found " + i);
232         }
233     }
234
235     public void testGetWithVersionCriteria() throws Exception JavaDoc
236     {
237         createData(database, odmg);
238         OQLQuery query = odmg.newOQLQuery();
239         int i = 0;
240         query.create("select effectiveness from " + Effectiveness.class.getName() + " where version.versionValue1=$1");
241         query.bind("versionvalue1");
242         ManageableCollection all = (ManageableCollection) query.execute();
243         java.util.Iterator JavaDoc it = all.ojbIterator();
244         Effectiveness temp = null;
245         while (it.hasNext())
246         {
247             temp = (Effectiveness) it.next();
248             if (!temp.getVersion().getVersionValue1().equals("versionvalue1"))
249             {
250                 fail("Should find only effectiveness objects where version.versionValue1='versionvalue1', found one with value " + temp.getVersion().getVersionValue1());
251             }
252             i++;
253         }
254         if (i < COUNT)
255             fail("Should have found at least " + COUNT + " where version.versionValue1='versionvalue1' items, only found " + i);
256     }
257
258     public void testGetEmbeddedObject() throws Exception JavaDoc
259     {
260         createData(database, odmg);
261         OQLQuery query = odmg.newOQLQuery();
262         query.create("select effectiveness.version from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)");
263         ManageableCollection all = (ManageableCollection) query.execute();
264         java.util.Iterator JavaDoc it = all.ojbIterator();
265         while (it.hasNext())
266         {
267             assertTrue("Selected item is Version", (it.next() instanceof Version));
268         }
269
270         query.create("select effectiveness.version.contract from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)");
271         all = (ManageableCollection) query.execute();
272         it = all.ojbIterator();
273         while (it.hasNext())
274         {
275             assertTrue("Selected item is Contract", (it.next() instanceof Contract));
276         }
277     }
278
279
280     public void testGetWithContractCriteria() throws Exception JavaDoc
281     {
282         createData(database, odmg);
283         OQLQuery query = odmg.newOQLQuery();
284         int i = 0;
285         query.create("select effectiveness from " + Effectiveness.class.getName() + " where version.contract.contractValue1=$1");
286         query.bind("contractvalue1");
287         ManageableCollection all = (ManageableCollection) query.execute();
288         java.util.Iterator JavaDoc it = all.ojbIterator();
289         Effectiveness temp = null;
290         while (it.hasNext())
291         {
292             temp = (Effectiveness) it.next();
293             if (!temp.getVersion().getContract().getContractValue1().equals("contractvalue1"))
294             {
295                 fail("Should find only effectiveness objects where contract.contractValue1='contractvalue1', found one with value " + temp.getVersion().getContract().getContractValue1());
296             }
297             i++;
298         }
299         if (i < COUNT)
300             fail("Should have found at least " + COUNT + " where version.contract.contractValue1='contractvalue1' items, only found " + i);
301     }
302
303     public void testGet() throws Exception JavaDoc
304     {
305         createData(database, odmg);
306         OQLQuery query = odmg.newOQLQuery();
307         int i = 0;
308         query.create("select effectiveness from " + Effectiveness.class.getName());
309         ManageableCollection all = (ManageableCollection) query.execute();
310         java.util.Iterator JavaDoc it = all.ojbIterator();
311         while (it.hasNext())
312         {
313             it.next();
314             i++;
315         }
316         if (i < COUNT)
317             fail("Should have found at least " + COUNT + " items, only found " + i);
318     }
319
320     public void testDelete() throws Exception JavaDoc
321     {
322         /**
323          * create some data for us to delete.
324          */

325         createData(database, odmg);
326
327         // 3. Get a list of some articles
328
Transaction tx = odmg.newTransaction();
329
330         OQLQuery query = odmg.newOQLQuery();
331         ManageableCollection all = null;
332         java.util.Iterator JavaDoc it = null;
333         int i = 0;
334         query.create("select effectiveness from " + org.apache.ojb.broker.Effectiveness.class.getName());
335
336         /**
337          * try doing this as part of one transaction, ODMG should figure out
338          * which order to delete in.
339          */

340         all = (ManageableCollection) query.execute();
341         // Iterator over the restricted articles objects
342
it = all.ojbIterator();
343         Effectiveness eff = null;
344         Version ver = null;
345         Contract contract = null;
346         while (it.hasNext())
347         {
348             eff = (Effectiveness) it.next();
349             ver = eff.getVersion();
350             contract = ver.getContract();
351
352             tx.begin();
353             database.deletePersistent(eff);
354             tx.commit();
355
356             tx.begin();
357             database.deletePersistent(ver);
358             tx.commit();
359
360             tx.begin();
361             database.deletePersistent(contract);
362             tx.commit();
363             // keep the count
364
i++;
365         }
366         if (i < COUNT)
367             fail("Should have found at least " + COUNT + " items to delete, only found " + i);
368         /**
369          * run query again, should get 0 results.
370          */

371         query.create("select contracts from " + org.apache.ojb.broker.Contract.class.getName());
372         ManageableCollection allContracts = (ManageableCollection) query.execute();
373         allContracts = (ManageableCollection) query.execute();
374         it = allContracts.ojbIterator();
375         if (it.hasNext())
376         {
377             fail("all contracts should have been removed, we found one.");
378         }
379     }
380
381     /**
382      * this test needs to either be invalidated as a test case, or ODMG has to be fixed.
383      * @throws Exception
384      */

385     public void XtestNotYetWorkingDelete() throws Exception JavaDoc
386     {
387         /**
388          * create some data for us to delete.
389          */

390         createData(database, odmg);
391
392         // 3. Get a list of some articles
393
Transaction tx = odmg.newTransaction();
394
395         OQLQuery query = odmg.newOQLQuery();
396         ManageableCollection all = null;
397         java.util.Iterator JavaDoc it = null;
398         int i = 0;
399         query.create("select effectiveness from " + org.apache.ojb.broker.Effectiveness.class.getName());
400
401         /**
402          * try doing this as part of one transaction, ODMG should figure out
403          * which order to delete in.
404          */

405         all = (ManageableCollection) query.execute();
406         // Iterator over the restricted articles objects
407
it = all.ojbIterator();
408         Effectiveness eff = null;
409         Version ver = null;
410         Contract contract = null;
411         /**
412          * should mark all these objects for delete then on commit
413          * ODMG should make sure they get deleted in proper order
414          */

415         tx.begin();
416         while (it.hasNext())
417         {
418             eff = (Effectiveness) it.next();
419             ver = eff.getVersion();
420             contract = ver.getContract();
421             /**
422              * should mean that version and effectivedate are cascade deleted.
423              */

424             database.deletePersistent(contract);
425             i++;
426         }
427         /**
428          * commit all changes.
429          */

430         tx.commit();
431         if (i < COUNT)
432             fail("Should have found at least " + COUNT + " effectiveness to delete, only found " + i);
433         /**
434          * run query again, should get 0 results.
435          */

436         query.create("select contracts from " + org.apache.ojb.broker.Contract.class.getName());
437         ManageableCollection allContracts = (ManageableCollection) query.execute();
438         allContracts = (ManageableCollection) query.execute();
439         it = allContracts.ojbIterator();
440         if (it.hasNext())
441         {
442             fail("all contracts should have been removed, we found one.");
443         }
444     }
445
446     /**
447      * test getting all (make sure basic operation is still functional)
448      */

449     public void testQuery() throws Exception JavaDoc
450     {
451         createData(database, odmg);
452         // 3. Get a list of some articles
453
Transaction tx = odmg.newTransaction();
454         tx.begin();
455
456         OQLQuery query = odmg.newOQLQuery();
457         String JavaDoc sql = "select effectiveness from " + Effectiveness.class.getName();
458         query.create(sql);
459
460         ManageableCollection allEffectiveness = (ManageableCollection) query.execute();
461
462         // Iterator over the restricted articles objects
463
java.util.Iterator JavaDoc it = allEffectiveness.ojbIterator();
464         int i = 0;
465         while (it.hasNext())
466         {
467             Effectiveness value = (Effectiveness) it.next();
468             /**
469              * check pk value of related contract item.
470              */

471             if (value.getVersion().getContract().getPk() == null)
472                 fail("Contract PK should not be null");
473             i++;
474         }
475         if (i < COUNT)
476             fail("Should have found at least " + COUNT + " items, only found: " + i);
477         tx.commit();
478     }
479
480     /**
481      * test changing a versions fk reference to it's contract.
482      * The old bug in ODMG wouldn't trigger an update if an object reference changed.
483      */

484     public void testContractReassignment() throws Exception JavaDoc
485     {
486         Transaction tx = odmg.newTransaction();
487         Contract contract = new Contract();
488         contract.setPk("contract1");
489         contract.setContractValue1("contract1value1");
490         contract.setContractValue2(1);
491         contract.setContractValue3("contract1value3");
492         contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
493
494         Version version = new Version();
495         version.setPk("version1");
496         version.setVersionValue1("version1value1");
497         version.setVersionValue2(1);
498         version.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
499         version.setContract(contract);
500
501         Effectiveness eff = new Effectiveness();
502         eff.setPk("eff1");
503         eff.setEffValue1("eff1value1");
504         eff.setEffValue2(1);
505         eff.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
506         eff.setVersion(version);
507
508         Contract contract2 = new Contract();
509         contract2.setPk("contract2");
510         contract2.setContractValue1("contract2value1");
511         contract2.setContractValue2(1);
512         contract2.setContractValue3("contractvalue3");
513         contract2.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
514
515         Version version2 = new Version();
516         version2.setPk("version2");
517         version2.setVersionValue1("version2value1");
518         version2.setVersionValue2(1);
519         version2.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
520         version2.setContract(contract2);
521
522         Effectiveness eff2 = new Effectiveness();
523         eff2.setPk("eff2");
524         eff2.setEffValue1("eff2value1");
525         eff2.setEffValue2(1);
526         eff2.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
527         eff2.setVersion(version2);
528
529         /**
530          * make them persistent
531          */

532         tx.begin();
533         database.makePersistent(eff2);
534         database.makePersistent(eff);
535         tx.commit();
536
537         /**
538          * do the reassignment
539          */

540         tx.begin();
541         tx.lock(version, Transaction.WRITE);
542         tx.lock(version2, Transaction.WRITE);
543         version.setContract(contract2);
544         version2.setContract(contract);
545         tx.commit();
546
547         /**
548          * query and check values
549          */

550         OQLQuery query = odmg.newOQLQuery();
551         String JavaDoc sql = "select version from " + org.apache.ojb.broker.Version.class.getName() + " where pk=$1";
552         query.create(sql);
553         query.bind("version1");
554         tx.begin();
555         ManageableCollection results = (ManageableCollection) query.execute();
556         // Iterator over the restricted articles objects
557
java.util.Iterator JavaDoc it = results.ojbIterator();
558         Version ver1 = null;
559         while (it.hasNext())
560         {
561             ver1 = (Version) it.next();
562             if (!ver1.getContract().getPk().equals(contract2.getPk()))
563             {
564                 fail(ver1.getPk() + " should have pointed to contract2 instead it pointed to: " + ver1.getContract().getPk());
565             }
566         }
567         tx.commit();
568
569         OQLQuery query2 = odmg.newOQLQuery();
570         String JavaDoc sql2 = "select version from " + org.apache.ojb.broker.Version.class.getName() + " where pk=$1";
571         query2.create(sql2);
572         query2.bind("version2");
573         tx.begin();
574         results = (ManageableCollection) query2.execute();
575         // Iterator over the restricted articles objects
576
java.util.Iterator JavaDoc it2 = results.ojbIterator();
577         Version ver2 = null;
578         while (it2.hasNext())
579         {
580             ver2 = (Version) it2.next();
581             if (!ver2.getContract().getPk().equals(contract.getPk()))
582             {
583                 fail(ver2.getPk() + " should have pointed to contract instead it pointed to: " + ver2.getContract().getPk());
584             }
585         }
586         tx.commit();
587
588         /**
589          * clean up
590          */

591         tx.begin();
592         database.deletePersistent(eff2);
593         database.deletePersistent(eff);
594         tx.commit();
595         tx.begin();
596         database.deletePersistent(version2);
597         database.deletePersistent(version);
598         tx.commit();
599         tx.begin();
600         database.deletePersistent(contract2);
601         database.deletePersistent(contract);
602         tx.commit();
603     }
604 }
605
Popular Tags