001: /*
002: * Created by IntelliJ IDEA.
003: * User: Matt
004: * Date: May 19, 2002
005: * Time: 3:43:36 PM
006: * To change template for new class use
007: * Code Style | Class Templates options (Tools | IDE Options).
008: */
009: package org.apache.ojb.odmg;
010:
011: import java.sql.Timestamp;
012:
013: import org.apache.ojb.broker.Contract;
014: import org.apache.ojb.broker.Effectiveness;
015: import org.apache.ojb.broker.ManageableCollection;
016: import org.apache.ojb.broker.RelatedToContract;
017: import org.apache.ojb.broker.Version;
018: import org.apache.ojb.junit.ODMGTestCase;
019: import org.odmg.Database;
020: import org.odmg.Implementation;
021: import org.odmg.OQLQuery;
022: import org.odmg.Transaction;
023:
024: public class ContractVersionEffectivenessOQLTest extends ODMGTestCase {
025:
026: private static Class CLASS = ContractVersionEffectivenessOQLTest.class;
027: private int COUNT = 10;
028:
029: /**
030: * Insert the method's description here.
031: * Creation date: (23.12.2000 18:30:38)
032: * @param args java.lang.String[]
033: */
034: public static void main(String[] args) {
035: String[] arr = { CLASS.getName() };
036: junit.textui.TestRunner.main(arr);
037: }
038:
039: public ContractVersionEffectivenessOQLTest(String name) {
040: super (name);
041: }
042:
043: private void createData(Database db, Implementation odmg)
044: throws Exception {
045: // Implementation odmg = OJB.getInstance();
046: Transaction tx = odmg.newTransaction();
047: for (int i = 0; i < COUNT; i++) {
048: tx.begin();
049: Contract contract = new Contract();
050: contract.setPk("C" + i + System.currentTimeMillis());
051: contract.setContractValue1("contractvalue1");
052: contract.setContractValue2(1);
053: contract.setContractValue3("contractvalue3");
054: contract.setContractValue4(new Timestamp(System
055: .currentTimeMillis()));
056: db.makePersistent(contract);
057:
058: Version version = new Version();
059: version.setPk("V" + i + System.currentTimeMillis());
060: version.setVersionValue1("versionvalue1");
061: version.setVersionValue2(1);
062: version.setVersionValue3(new Timestamp(System
063: .currentTimeMillis()));
064: version.setContract(contract);
065: db.makePersistent(version);
066:
067: Effectiveness eff = new Effectiveness();
068: eff.setPk("E" + i + System.currentTimeMillis());
069: eff.setEffValue1("effvalue1");
070: eff.setEffValue2(1);
071: eff.setEffValue3(new Timestamp(System.currentTimeMillis()));
072: eff.setVersion(version);
073: /**
074: * will create all
075: */
076: db.makePersistent(eff);
077: tx.commit();
078: }
079:
080: }
081:
082: public void testCreate() throws Exception {
083: createData(database, odmg);
084: }
085:
086: public void testComplexOQL() throws Exception {
087: /**
088: * 1. create the objects with specific values we'll search on later.
089: */
090: Transaction tx = odmg.newTransaction();
091: tx.begin();
092:
093: Contract contract = new Contract();
094: contract.setPk("C" + System.currentTimeMillis());
095: contract
096: .setContractValue1("version.contract.contractValue1.testComplexOQL");
097: contract.setContractValue2(1);
098: contract.setContractValue3("contractvalue3");
099: contract.setContractValue4(new Timestamp(System
100: .currentTimeMillis()));
101: database.makePersistent(contract);
102:
103: RelatedToContract rtc = new RelatedToContract();
104: rtc.setPk("R" + System.currentTimeMillis());
105: rtc.setRelatedValue1("test");
106: rtc.setRelatedValue2(5);
107: rtc.setRelatedValue3(new Timestamp(System.currentTimeMillis()));
108: contract.setRelatedToContract(rtc);
109: database.makePersistent(rtc);
110:
111: Version version = new Version();
112: version.setPk("V" + System.currentTimeMillis());
113: version.setVersionValue1("versionvalue1");
114: version.setVersionValue2(1);
115: version.setVersionValue3(new Timestamp(System
116: .currentTimeMillis()));
117: version.setContract(contract);
118: database.makePersistent(version);
119:
120: Effectiveness eff = new Effectiveness();
121: eff.setPk("E" + System.currentTimeMillis());
122: eff.setEffValue1("effValue1.testComplexOQL");
123: eff.setEffValue2(20);
124: eff.setEffValue3(new Timestamp(System.currentTimeMillis()));
125: eff.setVersion(version);
126: database.makePersistent(eff);
127:
128: tx.commit();
129: /**
130: * 2. define the complex OQL query to find the object we created
131: */
132: String oql = "select s from "
133: + org.apache.ojb.broker.Effectiveness.class.getName()
134: + " where "
135: + " version.contract.contractValue1=$1 and effValue1 = $2 and "
136: + " (effValue3 > $3 or is_undefined(effValue3)) and "
137: + " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and "
138: + " version.contract.relatedToContract.relatedValue2=$6";
139:
140: OQLQuery query = odmg.newOQLQuery();
141: query.create(oql);
142: query.bind("version.contract.contractValue1.testComplexOQL"); //version.contract.contractValue1=$1
143: query.bind("effValue1.testComplexOQL"); // effValue1 = $2
144: query.bind(new Timestamp(System.currentTimeMillis() - 5000)); // a while ago (effValue3 > $3)
145: query.bind(new Integer(20)); // effValue2 <= $4
146: query.bind(new Timestamp(System.currentTimeMillis() + 5000)); // a while from now (effValue3<$5)
147: query.bind(new Integer(5)); // version.contract.relatedToContract.relatedValue2=$6
148:
149: ManageableCollection all = (ManageableCollection) query
150: .execute();
151: java.util.Iterator it = all.ojbIterator();
152: /**
153: * make sure we got
154: */
155: int i = 0;
156: while (it.hasNext()) {
157: it.next();
158: i++;
159: }
160: if (i != 1) {
161: fail("Should have found just one object, instead found "
162: + i);
163: }
164: }
165:
166: public void testComplexOQL2() throws Exception {
167: /**
168: * 1. create the objects with specific values we'll search on later.
169: */
170: Transaction tx = odmg.newTransaction();
171: tx.begin();
172:
173: Contract contract = new Contract();
174: contract.setPk("C" + System.currentTimeMillis());
175: contract
176: .setContractValue1("version.contract.contractValue1.testComplexOQL");
177: contract.setContractValue2(1);
178: contract.setContractValue3("contractvalue3");
179: contract.setContractValue4(new Timestamp(System
180: .currentTimeMillis()));
181: database.makePersistent(contract);
182:
183: Version version = new Version();
184: version.setPk("V" + System.currentTimeMillis());
185: version.setVersionValue1("versionvalue1");
186: version.setVersionValue2(1);
187: version.setVersionValue3(new Timestamp(System
188: .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(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 oql = "select s from "
205: + org.apache.ojb.broker.Effectiveness.class.getName()
206: + " where "
207: + " version.contract.contractValue1=$1 and effValue1 = $2 and "
208: + " (effValue3 > $3 or is_undefined(effValue3)) and "
209: + " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and "
210: + " is_undefined(version.contract.relatedToContract.pk)";
211:
212: OQLQuery query = odmg.newOQLQuery();
213: query.create(oql);
214: query.bind("version.contract.contractValue1.testComplexOQL"); //version.contract.contractValue1=$1
215: query.bind("effValue1.testComplexOQL"); // effValue1 = $2
216: query.bind(new Timestamp(System.currentTimeMillis() - 5000)); // a while ago (effValue3 > $3)
217: query.bind(new Integer(20)); // effValue2 <= $4
218: query.bind(new Timestamp(System.currentTimeMillis() + 5000)); // a while from now (effValue3<$5)
219:
220: ManageableCollection all = (ManageableCollection) query
221: .execute();
222: java.util.Iterator it = all.ojbIterator();
223: /**
224: * make sure we got
225: */
226: int i = 0;
227: while (it.hasNext()) {
228: it.next();
229: i++;
230: }
231: if (i != 1) {
232: fail("Should have found just one object, instead found "
233: + i);
234: }
235: }
236:
237: public void testGetWithVersionCriteria() throws Exception {
238: createData(database, odmg);
239: OQLQuery query = odmg.newOQLQuery();
240: int i = 0;
241: query.create("select effectiveness from "
242: + Effectiveness.class.getName()
243: + " where version.versionValue1=$1");
244: query.bind("versionvalue1");
245: ManageableCollection all = (ManageableCollection) query
246: .execute();
247: java.util.Iterator it = all.ojbIterator();
248: Effectiveness temp = null;
249: while (it.hasNext()) {
250: temp = (Effectiveness) it.next();
251: if (!temp.getVersion().getVersionValue1().equals(
252: "versionvalue1")) {
253: fail("Should find only effectiveness objects where version.versionValue1='versionvalue1', found one with value "
254: + temp.getVersion().getVersionValue1());
255: }
256: i++;
257: }
258: if (i < COUNT)
259: fail("Should have found at least "
260: + COUNT
261: + " where version.versionValue1='versionvalue1' items, only found "
262: + i);
263: }
264:
265: public void testGetEmbeddedObject() throws Exception {
266: createData(database, odmg);
267: OQLQuery query = odmg.newOQLQuery();
268: query
269: .create("select effectiveness.version from "
270: + Effectiveness.class.getName()
271: + " where is_defined(effectiveness.version.versionValue1)");
272: ManageableCollection all = (ManageableCollection) query
273: .execute();
274: java.util.Iterator it = all.ojbIterator();
275: while (it.hasNext()) {
276: assertTrue("Selected item is Version",
277: (it.next() instanceof Version));
278: }
279:
280: query
281: .create("select effectiveness.version.contract from "
282: + Effectiveness.class.getName()
283: + " where is_defined(effectiveness.version.versionValue1)");
284: all = (ManageableCollection) query.execute();
285: it = all.ojbIterator();
286: while (it.hasNext()) {
287: assertTrue("Selected item is Contract",
288: (it.next() instanceof Contract));
289: }
290: }
291:
292: public void testGetWithContractCriteria() throws Exception {
293: createData(database, odmg);
294: OQLQuery query = odmg.newOQLQuery();
295: int i = 0;
296: query.create("select effectiveness from "
297: + Effectiveness.class.getName()
298: + " where version.contract.contractValue1=$1");
299: query.bind("contractvalue1");
300: ManageableCollection all = (ManageableCollection) query
301: .execute();
302: java.util.Iterator it = all.ojbIterator();
303: Effectiveness temp = null;
304: while (it.hasNext()) {
305: temp = (Effectiveness) it.next();
306: if (!temp.getVersion().getContract().getContractValue1()
307: .equals("contractvalue1")) {
308: fail("Should find only effectiveness objects where contract.contractValue1='contractvalue1', found one with value "
309: + temp.getVersion().getContract()
310: .getContractValue1());
311: }
312: i++;
313: }
314: if (i < COUNT)
315: fail("Should have found at least "
316: + COUNT
317: + " where version.contract.contractValue1='contractvalue1' items, only found "
318: + i);
319: }
320:
321: public void testGet() throws Exception {
322: createData(database, odmg);
323: OQLQuery query = odmg.newOQLQuery();
324: int i = 0;
325: query.create("select effectiveness from "
326: + Effectiveness.class.getName());
327: ManageableCollection all = (ManageableCollection) query
328: .execute();
329: java.util.Iterator it = all.ojbIterator();
330: while (it.hasNext()) {
331: it.next();
332: i++;
333: }
334: if (i < COUNT)
335: fail("Should have found at least " + COUNT
336: + " items, only found " + i);
337: }
338:
339: public void testDelete() throws Exception {
340: /**
341: * create some data for us to delete.
342: */
343: createData(database, odmg);
344:
345: // 3. Get a list of some articles
346: Transaction tx = odmg.newTransaction();
347:
348: OQLQuery query = odmg.newOQLQuery();
349: ManageableCollection all = null;
350: java.util.Iterator it = null;
351: int i = 0;
352: query.create("select effectiveness from "
353: + org.apache.ojb.broker.Effectiveness.class.getName());
354:
355: /**
356: * try doing this as part of one transaction, ODMG should figure out
357: * which order to delete in.
358: */
359: all = (ManageableCollection) query.execute();
360: // Iterator over the restricted articles objects
361: it = all.ojbIterator();
362: Effectiveness eff = null;
363: Version ver = null;
364: Contract contract = null;
365: while (it.hasNext()) {
366: eff = (Effectiveness) it.next();
367: ver = eff.getVersion();
368: contract = ver.getContract();
369:
370: tx.begin();
371: database.deletePersistent(eff);
372: tx.commit();
373:
374: tx.begin();
375: database.deletePersistent(ver);
376: tx.commit();
377:
378: tx.begin();
379: database.deletePersistent(contract);
380: tx.commit();
381: // keep the count
382: i++;
383: }
384: if (i < COUNT)
385: fail("Should have found at least " + COUNT
386: + " items to delete, only found " + i);
387: /**
388: * run query again, should get 0 results.
389: */
390: query.create("select contracts from "
391: + org.apache.ojb.broker.Contract.class.getName());
392: ManageableCollection allContracts = (ManageableCollection) query
393: .execute();
394: allContracts = (ManageableCollection) query.execute();
395: it = allContracts.ojbIterator();
396: if (it.hasNext()) {
397: fail("all contracts should have been removed, we found one.");
398: }
399: }
400:
401: /**
402: * this test needs to either be invalidated as a test case, or ODMG has to be fixed.
403: * @throws Exception
404: */
405: public void XtestNotYetWorkingDelete() throws Exception {
406: /**
407: * create some data for us to delete.
408: */
409: createData(database, odmg);
410:
411: // 3. Get a list of some articles
412: Transaction tx = odmg.newTransaction();
413:
414: OQLQuery query = odmg.newOQLQuery();
415: ManageableCollection all = null;
416: java.util.Iterator it = null;
417: int i = 0;
418: query.create("select effectiveness from "
419: + org.apache.ojb.broker.Effectiveness.class.getName());
420:
421: /**
422: * try doing this as part of one transaction, ODMG should figure out
423: * which order to delete in.
424: */
425: all = (ManageableCollection) query.execute();
426: // Iterator over the restricted articles objects
427: it = all.ojbIterator();
428: Effectiveness eff = null;
429: Version ver = null;
430: Contract contract = null;
431: /**
432: * should mark all these objects for delete then on commit
433: * ODMG should make sure they get deleted in proper order
434: */
435: tx.begin();
436: while (it.hasNext()) {
437: eff = (Effectiveness) it.next();
438: ver = eff.getVersion();
439: contract = ver.getContract();
440: /**
441: * should mean that version and effectivedate are cascade deleted.
442: */
443: database.deletePersistent(contract);
444: i++;
445: }
446: /**
447: * commit all changes.
448: */
449: tx.commit();
450: if (i < COUNT)
451: fail("Should have found at least " + COUNT
452: + " effectiveness to delete, only found " + i);
453: /**
454: * run query again, should get 0 results.
455: */
456: query.create("select contracts from "
457: + org.apache.ojb.broker.Contract.class.getName());
458: ManageableCollection allContracts = (ManageableCollection) query
459: .execute();
460: allContracts = (ManageableCollection) query.execute();
461: it = allContracts.ojbIterator();
462: if (it.hasNext()) {
463: fail("all contracts should have been removed, we found one.");
464: }
465: }
466:
467: /**
468: * test getting all (make sure basic operation is still functional)
469: */
470: public void testQuery() throws Exception {
471: createData(database, odmg);
472: // 3. Get a list of some articles
473: Transaction tx = odmg.newTransaction();
474: tx.begin();
475:
476: OQLQuery query = odmg.newOQLQuery();
477: String sql = "select effectiveness from "
478: + Effectiveness.class.getName();
479: query.create(sql);
480:
481: ManageableCollection allEffectiveness = (ManageableCollection) query
482: .execute();
483:
484: // Iterator over the restricted articles objects
485: java.util.Iterator it = allEffectiveness.ojbIterator();
486: int i = 0;
487: while (it.hasNext()) {
488: Effectiveness value = (Effectiveness) it.next();
489: /**
490: * check pk value of related contract item.
491: */
492: if (value.getVersion().getContract().getPk() == null)
493: fail("Contract PK should not be null");
494: i++;
495: }
496: if (i < COUNT)
497: fail("Should have found at least " + COUNT
498: + " items, only found: " + i);
499: tx.commit();
500: }
501:
502: /**
503: * test changing a versions fk reference to it's contract.
504: * The old bug in ODMG wouldn't trigger an update if an object reference changed.
505: */
506: public void testContractReassignment() throws Exception {
507: Transaction tx = odmg.newTransaction();
508: Contract contract = new Contract();
509: contract.setPk("contract1");
510: contract.setContractValue1("contract1value1");
511: contract.setContractValue2(1);
512: contract.setContractValue3("contract1value3");
513: contract.setContractValue4(new Timestamp(System
514: .currentTimeMillis()));
515:
516: Version version = new Version();
517: version.setPk("version1");
518: version.setVersionValue1("version1value1");
519: version.setVersionValue2(1);
520: version.setVersionValue3(new Timestamp(System
521: .currentTimeMillis()));
522: version.setContract(contract);
523:
524: Effectiveness eff = new Effectiveness();
525: eff.setPk("eff1");
526: eff.setEffValue1("eff1value1");
527: eff.setEffValue2(1);
528: eff.setEffValue3(new Timestamp(System.currentTimeMillis()));
529: eff.setVersion(version);
530:
531: Contract contract2 = new Contract();
532: contract2.setPk("contract2");
533: contract2.setContractValue1("contract2value1");
534: contract2.setContractValue2(1);
535: contract2.setContractValue3("contractvalue3");
536: contract2.setContractValue4(new Timestamp(System
537: .currentTimeMillis()));
538:
539: Version version2 = new Version();
540: version2.setPk("version2");
541: version2.setVersionValue1("version2value1");
542: version2.setVersionValue2(1);
543: version2.setVersionValue3(new Timestamp(System
544: .currentTimeMillis()));
545: version2.setContract(contract2);
546:
547: Effectiveness eff2 = new Effectiveness();
548: eff2.setPk("eff2");
549: eff2.setEffValue1("eff2value1");
550: eff2.setEffValue2(1);
551: eff2.setEffValue3(new Timestamp(System.currentTimeMillis()));
552: eff2.setVersion(version2);
553:
554: /**
555: * make them persistent
556: */
557: tx.begin();
558: database.makePersistent(eff2);
559: database.makePersistent(eff);
560: tx.commit();
561:
562: /**
563: * do the reassignment
564: */
565: tx.begin();
566: tx.lock(version, Transaction.WRITE);
567: tx.lock(version2, Transaction.WRITE);
568: version.setContract(contract2);
569: version2.setContract(contract);
570: tx.commit();
571:
572: /**
573: * query and check values
574: */
575: OQLQuery query = odmg.newOQLQuery();
576: String sql = "select version from "
577: + org.apache.ojb.broker.Version.class.getName()
578: + " where pk=$1";
579: query.create(sql);
580: query.bind("version1");
581: tx.begin();
582: ManageableCollection results = (ManageableCollection) query
583: .execute();
584: // Iterator over the restricted articles objects
585: java.util.Iterator it = results.ojbIterator();
586: Version ver1 = null;
587: while (it.hasNext()) {
588: ver1 = (Version) it.next();
589: if (!ver1.getContract().getPk().equals(contract2.getPk())) {
590: fail(ver1.getPk()
591: + " should have pointed to contract2 instead it pointed to: "
592: + ver1.getContract().getPk());
593: }
594: }
595: tx.commit();
596:
597: OQLQuery query2 = odmg.newOQLQuery();
598: String sql2 = "select version from "
599: + org.apache.ojb.broker.Version.class.getName()
600: + " where pk=$1";
601: query2.create(sql2);
602: query2.bind("version2");
603: tx.begin();
604: results = (ManageableCollection) query2.execute();
605: // Iterator over the restricted articles objects
606: java.util.Iterator it2 = results.ojbIterator();
607: Version ver2 = null;
608: while (it2.hasNext()) {
609: ver2 = (Version) it2.next();
610: if (!ver2.getContract().getPk().equals(contract.getPk())) {
611: fail(ver2.getPk()
612: + " should have pointed to contract instead it pointed to: "
613: + ver2.getContract().getPk());
614: }
615: }
616: tx.commit();
617:
618: /**
619: * clean up
620: */
621: tx.begin();
622: database.deletePersistent(eff2);
623: database.deletePersistent(eff);
624: tx.commit();
625: tx.begin();
626: database.deletePersistent(version2);
627: database.deletePersistent(version);
628: tx.commit();
629: tx.begin();
630: database.deletePersistent(contract2);
631: database.deletePersistent(contract);
632: tx.commit();
633: }
634: }
|