KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > BasicQueryTest


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: BasicQueryTest.java,v 1.20 2004/03/30 06:15:56 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import java.lang.reflect.Method JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Random JavaDoc;
19 import java.util.Set JavaDoc;
20 import javax.jdo.Extent;
21 import javax.jdo.JDOHelper;
22 import javax.jdo.JDOException;
23 import javax.jdo.PersistenceManager;
24 import javax.jdo.Query;
25 import javax.jdo.Transaction;
26 import org.apache.log4j.Category;
27
28
29 /**
30  * Tests the basic query functionality of the PersistenceManager.
31  *
32  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
33  * @version $Revision: 1.20 $
34  */

35
36 public class BasicQueryTest extends StorageTestCase
37 {
38     private static final Category LOG = Category.getInstance(BasicQueryTest.class);
39
40     private static final int QUERY_FLAG_USE_ORDERING = 1;
41     private static final int QUERY_FLAG_USE_SUBCLASS = 2;
42     private static final int ALL_QUERY_FLAGS = 3;
43
44     private static final String JavaDoc NORMAL_SET_FIELD = "normalSet";
45     private static final String JavaDoc NORMAL_OBJECT_SET_FIELD = "normalObjectSet";
46
47     private Method JavaDoc GET_NORMAL_SET_METHOD = null;
48     private Method JavaDoc GET_NORMAL_OBJECT_SET_METHOD = null;
49
50     private boolean schemaInitialized = false;
51
52
53     /**
54      * Used by the JUnit framework to construct tests. Normally, programmers
55      * would never explicitly use this constructor.
56      *
57      * @param name Name of the <tt>TestCase</tt>.
58      */

59
60     public BasicQueryTest(String JavaDoc name)
61     {
62         super(name);
63     }
64
65
66     protected void setUp() throws Exception JavaDoc
67     {
68         super.setUp();
69
70         if (!schemaInitialized)
71         {
72             addClassesToSchema(new Class JavaDoc[]
73                 {
74                     Widget.class,
75                     StringWidget.class,
76                     BinaryWidget.class,
77                     DateWidget.class,
78                     FloatWidget.class,
79                     SetWidget.class,
80                     ElementWidget.class,
81                     OwnerWidget.class,
82                     KeywordConflict.class,
83                     AReallyObnoxiouslyLongWindedNamedObject.class
84                 }
85             );
86
87             schemaInitialized = true;
88         }
89
90         GET_NORMAL_SET_METHOD =
91             HasNormalSetField.class.getMethod(getterName(NORMAL_SET_FIELD), null);
92         GET_NORMAL_OBJECT_SET_METHOD =
93             HasNormalSetField.class.getMethod(getterName(NORMAL_OBJECT_SET_FIELD), null);
94     }
95
96
97     /**
98      * Create the standard getter name for the given field.
99      *
100      * @param field The field name to create the getter name for.
101      *
102      * @return The standard getter name for the given field.
103      */

104     private static String JavaDoc getterName(String JavaDoc field)
105     {
106         return "get" + Character.toUpperCase(field.charAt(0)) + field.substring(1);
107     }
108
109
110     public void testQueryOnWidgets() throws Exception JavaDoc
111     {
112         insertObjects(Widget.class);
113         queryWidgetObjects(true);
114         queryWidgetObjects(false);
115         removeObjects();
116     }
117
118     public void testQueryOnStringWidgets() throws Exception JavaDoc
119     {
120         insertObjects(StringWidget.class);
121         queryStringWidgetObjects(true);
122         queryStringWidgetObjects(false);
123         removeObjects();
124     }
125
126     public void testQueryOnBinaryWidgets() throws Exception JavaDoc
127     {
128         insertObjects(BinaryWidget.class);
129         queryBinaryWidgetObjects(true);
130         queryBinaryWidgetObjects(false);
131         removeObjects();
132     }
133
134     public void testQueryOnDateWidgets() throws Exception JavaDoc
135     {
136         insertObjects(DateWidget.class);
137         queryDateWidgetObjects(true);
138         queryDateWidgetObjects(false);
139         removeObjects();
140     }
141
142     public void testQueryOnFloatWidgets() throws Exception JavaDoc
143     {
144         insertObjects(FloatWidget.class);
145         queryFloatWidgetObjects(true);
146         queryFloatWidgetObjects(false);
147         removeObjects();
148     }
149
150     public void testQueryOnSetWidgets() throws Exception JavaDoc
151     {
152         insertObjects(SetWidget.class);
153         querySetWidgetObjects(SetWidget.class, true);
154         querySetWidgetObjects(SetWidget.class, false);
155         queryElementWidgetObjects(true);
156         queryElementWidgetObjects(false);
157         removeObjects();
158
159         queryCollectionsUsingNestedContains(SetWidget.class, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
160         queryCollectionsUsingNestedContains(SetWidget.class, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
161         queryCollectionsUsingCandidateSubclasses(SetWidget.class, true);
162         queryCollectionsUsingCandidateSubclasses(SetWidget.class, false);
163     }
164
165     public void testQueryOnInterfaceFields() throws Exception JavaDoc
166     {
167         insertObjects(OwnerWidget.class);
168         queryOwnerWidgetObjects(true);
169         queryOwnerWidgetObjects(false);
170         removeObjects();
171     }
172
173     public void testQueryOnClassUsingSQLKeywords() throws Exception JavaDoc
174     {
175         insertObjects(KeywordConflict.class);
176         queryKeywordConflictObjects(true);
177         queryKeywordConflictObjects(false);
178         removeObjects();
179     }
180
181     public void testQueryOnClassWithLongName() throws Exception JavaDoc
182     {
183         insertObjects(AReallyObnoxiouslyLongWindedNamedObject.class);
184         queryKeywordConflictObjects(true);
185         queryKeywordConflictObjects(false);
186         removeObjects();
187     }
188
189
190     private void queryWidgetObjects(boolean transactional) throws Exception JavaDoc
191     {
192         /*
193          * Perform various queries and validate the resulting collection.
194          */

195
196         LOG.info("Querying " + TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
197         PersistenceManager pm = pmf.getPersistenceManager();
198         Transaction tx = pm.currentTransaction();
199
200         try
201         {
202             if (!transactional)
203                 tx.setNontransactionalRead(true);
204
205             Query query;
206             Collection JavaDoc results;
207
208             /*
209              * Query #1
210              */

211
212             if (transactional)
213                 tx.begin();
214
215             HashSet JavaDoc expected = new HashSet JavaDoc();
216
217             for (int i = 0; i < objs.length; ++i)
218             {
219                 Widget w = (Widget)objs[i];
220
221                 if (w.getByteField() > 64 || w.getByteObjField() != null || w.getBooleanField() == false ||
222                     w.getCharField() != 'X' || w.getShortField() <= 0 || w.getIntField() >= 0 || w.getLongField() < 0)
223                     expected.add(pm.getObjectById(ids[i], false));
224             }
225
226             query = pm.newQuery(pm.getExtent(Widget.class, true));
227             query.setFilter("byteField > 64 || byteObjField != null || booleanField == false || charField != 'X' || shortField <= 0 || intField >= 0 || longField < 0");
228             results = (Collection JavaDoc)query.execute();
229
230             try
231             {
232                 assertResultsEqual(expected, results);
233             }
234             finally
235             {
236                 query.closeAll();
237             }
238
239             query.declareParameters("java.lang.Byte arg");
240             query.setFilter("byteField>64|byteObjField!=arg|!booleanField|charField!='X'|shortField<=0|intField>=0|longField<0");
241             results = (Collection JavaDoc)query.execute(null);
242
243             try
244             {
245                 assertResultsEqual(expected, results);
246             }
247             finally
248             {
249                 query.closeAll();
250             }
251
252             if (transactional)
253                 tx.commit();
254
255             /*
256              * Query #2
257              */

258
259             if (transactional)
260                 tx.begin();
261
262             expected = new HashSet JavaDoc();
263
264             for (int i = 0; i < objs.length; ++i)
265             {
266                 Widget w = (Widget)objs[i];
267
268                 if (w.getIntField() > 0)
269                     expected.add(pm.getObjectById(ids[i], false));
270             }
271
272             query = pm.newQuery();
273             query.setCandidates(pm.getExtent(Widget.class, true));
274             query.setFilter("intField > 0");
275             query.setOrdering("intField ascending");
276             results = (Collection JavaDoc)query.execute();
277
278             try
279             {
280                 assertResultsEqual(expected, results);
281
282                 Iterator JavaDoc i = results.iterator();
283                 int lastIntField = 0;
284
285                 while (i.hasNext())
286                 {
287                     Widget obj = (Widget)i.next();
288
289                     assertTrue("Object returned out of order: " + obj, obj.getIntField() > lastIntField);
290
291                     lastIntField = obj.getIntField();
292                 }
293             }
294             finally
295             {
296                 query.closeAll();
297             }
298
299             if (transactional)
300                 tx.commit();
301
302             /*
303              * Query #3
304              *
305              * Tests a parameter being referenced more than once in the filter string.
306              */

307
308             if (transactional)
309                 tx.begin();
310
311             expected = new HashSet JavaDoc();
312
313             for (int i = 0; i < objs.length; ++i)
314             {
315                 Widget w = (Widget)objs[i];
316
317                 if (w.getIntField() > 5 || w.getShortField() > 5)
318                     expected.add(pm.getObjectById(ids[i], false));
319             }
320
321             query = pm.newQuery(pm.getExtent(Widget.class, true));
322             query.declareParameters("int val");
323             query.setFilter("intField > val || shortField > val");
324             results = (Collection JavaDoc)query.execute(new Integer JavaDoc(5));
325
326             try
327             {
328                 assertResultsEqual(expected, results);
329             }
330             finally
331             {
332                 query.closeAll();
333             }
334
335             if (transactional)
336                 tx.commit();
337
338             /*
339              * Query #4
340              *
341              * Tests operator precedence/parenthesis handling. All of the
342              * filter expressions include redundant parentheses.
343              */

344
345             if (transactional)
346                 tx.begin();
347
348             /*
349              * We have to keep the values of Widget.shortField and
350              * Widget.intField within these boundaries in order to avoid
351              * arithmetic overflow of these filter expressions on some
352              * databases, namely MS SQL Server.
353              */

354             short minSafeShort = Short.MIN_VALUE - Byte.MIN_VALUE;
355             short maxSafeShort = Short.MAX_VALUE - Byte.MAX_VALUE;
356             int minSafeInt = Integer.MIN_VALUE - Short.MIN_VALUE - Byte.MIN_VALUE;
357             int maxSafeInt = Integer.MAX_VALUE - Short.MAX_VALUE - Byte.MAX_VALUE;
358
359             expected = new HashSet JavaDoc();
360
361             for (int i = 0; i < objs.length; ++i)
362             {
363                 Widget tw = (Widget)objs[i];
364                 Widget pw = (Widget)pm.getObjectById(ids[i], false);
365
366
367                 short sVal = tw.getShortField();
368                 int iVal = tw.getIntField();
369
370                 if (sVal < minSafeShort || sVal > maxSafeShort)
371                 {
372                     sVal /= 2;
373                     tw.setShortField(sVal);
374                     pw.setShortField(sVal);
375                 }
376
377                 if (iVal < minSafeInt || iVal > maxSafeInt)
378                 {
379                     iVal /= 2;
380                     tw.setIntField(iVal);
381                     pw.setIntField(iVal);
382                 }
383                     
384                 if (tw.getIntField() + tw.getShortField() - tw.getByteField() > 0)
385                     expected.add(pw);
386             }
387
388
389             if (transactional)
390             {
391                 tx.commit();
392                 tx.begin();
393             }
394
395             String JavaDoc[] filters = {
396                 "intField + shortField - byteField > 0",
397                 "intField + (shortField - byteField) > 0",
398                 "(intField + shortField) - byteField > 0",
399                 "(shortField - byteField) + intField > 0",
400                 "shortField - byteField + intField > 0",
401                 "0 < intField + shortField - byteField",
402                 "0 < intField + (shortField - byteField)",
403                 "0 < (intField + shortField) - byteField",
404                 "0 < (shortField - byteField) + intField",
405                 "0 < shortField - byteField + intField"
406             };
407
408             query = pm.newQuery(pm.getExtent(Widget.class, true));
409
410             for (int i = 0; i < filters.length; ++i)
411             {
412                 query.setFilter(filters[i]);
413                 results = (Collection JavaDoc)query.execute();
414
415                 try
416                 {
417                     assertResultsEqual(expected, results);
418                 }
419                 finally
420                 {
421                     query.closeAll();
422                 }
423             }
424
425             if (transactional)
426                 tx.commit();
427
428             /*
429              * Query #5
430              *
431              * Another test of operator precedence/parenthesis handling. Most
432              * of the filter expressions include parentheses that are *not*
433              * redundant.
434              */

435
436             if (transactional)
437                 tx.begin();
438
439             expected = new HashSet JavaDoc();
440
441             for (int i = 0; i < objs.length; ++i)
442             {
443                 Widget w = (Widget)objs[i];
444
445                 if (w.getIntField() - (w.getShortField() + w.getByteField()) > 0)
446                     expected.add(pm.getObjectById(ids[i], false));
447             }
448
449             filters = new String JavaDoc[] {
450                 "intField - shortField - byteField > 0",
451                 "intField - (shortField + byteField) > 0",
452                 "(intField - shortField) - byteField > 0",
453                 "-(shortField + byteField) + intField > 0",
454                 "0 < intField - shortField - byteField",
455                 "0 < intField - (shortField + byteField)",
456                 "0 < (intField - shortField) - byteField",
457                 "0 < -(shortField + byteField) + intField"
458             };
459
460             query = pm.newQuery();
461             query.setCandidates(pm.getExtent(Widget.class, true));
462
463             for (int i = 0; i < filters.length; ++i)
464             {
465                 query.setFilter(filters[i]);
466                 results = (Collection JavaDoc)query.execute();
467
468                 try
469                 {
470                     assertResultsEqual(expected, results);
471                 }
472                 finally
473                 {
474                     query.closeAll();
475                 }
476             }
477
478             if (transactional)
479                 tx.commit();
480
481             /*
482              * Query #6
483              *
484              * Another test of operator precedence/parenthesis handling. Most
485              * of the filter expressions include parentheses that are *not*
486              * redundant.
487              */

488
489             if (transactional)
490                 tx.begin();
491
492             expected = new HashSet JavaDoc();
493
494             for (int i = 0; i < objs.length; ++i)
495             {
496                 Widget w = (Widget)objs[i];
497
498                 if (w.getBooleanField() == w.getIntField() < 0 && w.getBooleanField() == w.getShortField() < 0)
499                     expected.add(pm.getObjectById(ids[i], false));
500             }
501
502             filters = new String JavaDoc[] {
503                 "booleanField == intField < 0 && booleanField == shortField < 0",
504                 "booleanField == (intField < 0) && booleanField == (shortField < 0)",
505                 "booleanField == intField < 0 && intField < 0 == shortField < 0",
506                 "booleanField == (intField < 0) && (intField < 0) == (shortField < 0)",
507                 "(booleanField == true) == intField < 0 && intField < 0 == shortField < 0",
508                 "(booleanField == true) == (intField < 0) && (intField < 0) == (shortField < 0)",
509                 "intField < 0 == booleanField && shortField < 0 == booleanField",
510                 "(intField < 0) == booleanField && (shortField < 0) == booleanField",
511                 "intField < 0 == shortField < 0 && booleanField == intField < 0",
512                 "(intField < 0) == (shortField < 0) && booleanField == (intField < 0)",
513                 "intField < 0 == (booleanField == true) && shortField < 0 == (booleanField == true)",
514                 "(intField < 0) == (booleanField == true) && (shortField < 0) == (booleanField == true)"
515             };
516
517             query = pm.newQuery(pm.getExtent(Widget.class, true));
518
519             for (int i = 0; i < filters.length; ++i)
520             {
521                 query.setFilter(filters[i]);
522                 results = (Collection JavaDoc)query.execute();
523
524                 try
525                 {
526                     assertResultsEqual(expected, results);
527                 }
528                 finally
529                 {
530                     query.closeAll();
531                 }
532             }
533
534             if (transactional)
535                 tx.commit();
536
537
538             /*
539              * Query #7
540              *
541              * Test querying for values passed in as a parameter by a transient Set
542              * of values.
543              */

544
545             if (transactional)
546                 tx.begin();
547
548             Random JavaDoc rand = new Random JavaDoc();
549             String JavaDoc[] filterFields =
550                 new String JavaDoc[] {"booleanField", "booleanObjField",
551                               "byteField", "byteObjField",
552                               "charField", "charObjField",
553                               "shortField", "shortObjField",
554                               "intField", "intObjField",
555                               "longField", "longObjField",
556                               "this" };
557             HashSet JavaDoc[] setParameters =
558                 new HashSet JavaDoc[] {new HashSet JavaDoc(), new HashSet JavaDoc(),
559                                new HashSet JavaDoc(), new HashSet JavaDoc(),
560                                new HashSet JavaDoc(), new HashSet JavaDoc(),
561                                new HashSet JavaDoc(), new HashSet JavaDoc(),
562                                new HashSet JavaDoc(), new HashSet JavaDoc(),
563                                new HashSet JavaDoc(), new HashSet JavaDoc(),
564                                new HashSet JavaDoc()};
565             HashSet JavaDoc[] expectedResults =
566                 new HashSet JavaDoc[] {new HashSet JavaDoc(), new HashSet JavaDoc(),
567                                new HashSet JavaDoc(), new HashSet JavaDoc(),
568                                new HashSet JavaDoc(), new HashSet JavaDoc(),
569                                new HashSet JavaDoc(), new HashSet JavaDoc(),
570                                new HashSet JavaDoc(), new HashSet JavaDoc(),
571                                new HashSet JavaDoc(), new HashSet JavaDoc(),
572                                new HashSet JavaDoc()};
573
574             for (int i = 0; i < objs.length; ++i)
575             {
576                 if (rand.nextBoolean())
577                 {
578                     Widget w = (Widget)objs[i];
579
580                     setParameters[0].add(new Boolean JavaDoc(w.getBooleanField()));
581                     setParameters[1].add(w.getBooleanObjField());
582                     setParameters[2].add(new Byte JavaDoc(w.getByteField()));
583                     setParameters[3].add(w.getByteObjField());
584                     setParameters[4].add(new Character JavaDoc(w.getCharField()));
585                     setParameters[5].add(w.getCharObjField());
586                     setParameters[6].add(new Short JavaDoc(w.getShortField()));
587                     setParameters[7].add(w.getShortObjField());
588                     setParameters[8].add(new Integer JavaDoc(w.getIntField()));
589                     setParameters[9].add(w.getIntObjField());
590                     setParameters[10].add(new Long JavaDoc(w.getLongField()));
591                     setParameters[11].add(w.getLongObjField());
592                     setParameters[12].add(pm.getObjectById(ids[i], false));
593                     expectedResults[12].add(pm.getObjectById(ids[i], false));
594                 }
595             }
596             for (int i = 0; i < objs.length; i++)
597             {
598                 Widget w = (Widget)objs[i];
599                 Object JavaDoc obj = pm.getObjectById(ids[i], false);
600
601                 if (setParameters[0].contains(new Boolean JavaDoc(w.getBooleanField())))
602                     expectedResults[0].add(obj);
603                 if (setParameters[1].contains(w.getBooleanObjField()))
604                     expectedResults[1].add(obj);
605                 if (setParameters[2].contains(new Byte JavaDoc(w.getByteField())))
606                     expectedResults[2].add(obj);
607                 if (setParameters[3].contains(w.getByteObjField()))
608                     expectedResults[3].add(obj);
609                 if (setParameters[4].contains(new Character JavaDoc(w.getCharField())))
610                     expectedResults[4].add(obj);
611                 if (setParameters[5].contains(w.getCharObjField()))
612                     expectedResults[5].add(obj);
613                 if (setParameters[6].contains(new Short JavaDoc(w.getShortField())))
614                     expectedResults[6].add(obj);
615                 if (setParameters[7].contains(w.getShortObjField()))
616                     expectedResults[7].add(obj);
617                 if (setParameters[8].contains(new Integer JavaDoc(w.getIntField())))
618                     expectedResults[8].add(obj);
619                 if (setParameters[9].contains(w.getIntObjField()))
620                     expectedResults[9].add(obj);
621                 if (setParameters[10].contains(new Long JavaDoc(w.getLongField())))
622                     expectedResults[10].add(obj);
623                 if (setParameters[11].contains(w.getLongObjField()))
624                     expectedResults[11].add(obj);
625             }
626
627             query = pm.newQuery(pm.getExtent(Widget.class, true));
628
629             for (int i = 0; i < filterFields.length; ++i)
630             {
631                 if (LOG.isDebugEnabled())
632                 {
633                     LOG.debug("Filter field = " + filterFields[i]);
634                     LOG.debug("Set parameter = " + setParameters[i]);
635                 }
636
637                 query.declareParameters("java.util.Set setParameter");
638                 query.setFilter("setParameter.contains(" + filterFields[i] + ")");
639                 results = (Collection JavaDoc)query.execute(setParameters[i]);
640
641                 try
642                 {
643                     assertResultsEqual(expectedResults[i], results);
644                 }
645                 finally
646                 {
647                     query.closeAll();
648                 }
649             }
650
651             if (transactional)
652                 tx.commit();
653         }
654         finally
655         {
656             if (tx.isActive())
657                 tx.rollback();
658
659             pm.close();
660         }
661     }
662
663
664     private void queryStringWidgetObjects(boolean transactional) throws Exception JavaDoc
665     {
666         /*
667          * Perform various queries and validate the resulting collection.
668          */

669
670         LOG.info("Querying " + TEST_OBJECT_COUNT + " " + StringWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
671         PersistenceManager pm = pmf.getPersistenceManager();
672         Transaction tx = pm.currentTransaction();
673
674         try
675         {
676             if (!transactional)
677                 tx.setNontransactionalRead(true);
678
679             Query query;
680             Collection JavaDoc results;
681
682             /*
683              * Query #1
684              */

685
686             if (transactional)
687                 tx.begin();
688
689             HashSet JavaDoc expected = new HashSet JavaDoc();
690
691             for (int i = 0; i < objs.length; ++i)
692             {
693                 StringWidget sw = (StringWidget)pm.getObjectById(ids[i], false);
694
695                 if (sw.getIntField() < 0)
696                     expected.add(sw);
697             }
698
699             query = pm.newQuery(pm.getExtent(StringWidget.class, true));
700             query.setFilter("intField < 0");
701             query.setOrdering("normalString descending");
702             results = (Collection JavaDoc)query.execute();
703
704             try
705             {
706                 assertResultsEqual(expected, results);
707             }
708             finally
709             {
710                 query.closeAll();
711             }
712
713             if (transactional)
714                 tx.commit();
715
716             /*
717              * Query #2
718              *
719              * Tests the startsWith() method.
720              */

721
722             tx.begin();
723
724             expected = new HashSet JavaDoc();
725
726             for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i)
727             {
728                 StringWidget tsw = (StringWidget)objs[i * 5];
729                 StringWidget psw = (StringWidget)pm.getObjectById(ids[i * 5], false);
730                 String JavaDoc newNormalString = "at the start " + i;
731
732                 tsw.setNormalString(newNormalString);
733                 psw.setNormalString(newNormalString);
734
735                 expected.add(psw);
736             }
737
738             tx.commit();
739
740             if (transactional)
741                 tx.begin();
742
743             query = pm.newQuery(pm.getExtent(StringWidget.class, true));
744             query.setFilter("normalString.startsWith(\"at the start\")");
745             results = (Collection JavaDoc)query.execute();
746
747             try
748             {
749                 assertResultsEqual(expected, results);
750             }
751             finally
752             {
753                 query.closeAll();
754             }
755
756             query.setFilter("normalString.substring(6,12) == \" start\"");
757             results = (Collection JavaDoc)query.execute();
758
759             try
760             {
761                 assertResultsEqual(expected, results);
762             }
763             finally
764             {
765                 query.closeAll();
766             }
767
768             query.declareParameters("java.lang.String s");
769             query.setFilter("normalString.startsWith(s)");
770             results = (Collection JavaDoc)query.execute("at the start");
771
772             try
773             {
774                 assertResultsEqual(expected, results);
775             }
776             finally
777             {
778                 query.closeAll();
779             }
780
781             if (transactional)
782                 tx.commit();
783
784             /*
785              * Query #3
786              *
787              * Tests the endsWith(), toLowerCase() and toUpperCase() methods.
788              */

789
790             tx.begin();
791
792             expected = new HashSet JavaDoc();
793
794             for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i)
795             {
796                 StringWidget tsw = (StringWidget)objs[i * 5 + 1];
797                 StringWidget psw = (StringWidget)pm.getObjectById(ids[i * 5 + 1], false);
798                 String JavaDoc newNormalString = "" + i + (i % 2 == 0 ? " at the end" : " At The End");
799
800                 tsw.setNormalString(newNormalString);
801                 psw.setNormalString(newNormalString);
802
803                 expected.add(psw);
804             }
805
806             tx.commit();
807
808             if (transactional)
809                 tx.begin();
810
811             query = pm.newQuery(pm.getExtent(StringWidget.class, true));
812             query.setFilter("normalString.toLowerCase().endsWith(\"at the end\")");
813             results = (Collection JavaDoc)query.execute();
814
815             try
816             {
817                 assertResultsEqual(expected, results);
818             }
819             finally
820             {
821                 query.closeAll();
822             }
823
824             query.declareParameters("java.lang.String s");
825             query.setFilter("normalString.toUpperCase().endsWith(s)");
826             results = (Collection JavaDoc)query.execute("AT THE END");
827
828             try
829             {
830                 assertResultsEqual(expected, results);
831             }
832             finally
833             {
834                 query.closeAll();
835             }
836
837             if (transactional)
838                 tx.commit();
839
840             /*
841              * Query #4
842              *
843              * Tests the indexof() method.
844              */

845
846             tx.begin();
847
848             expected = new HashSet JavaDoc();
849
850             for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i)
851             {
852                 StringWidget tsw = (StringWidget)objs[i * 5 + 1];
853                 StringWidget psw = (StringWidget)pm.getObjectById(ids[i * 5 + 1], false);
854                 String JavaDoc newNormalString = "" + i + " in the middle " + i;
855
856                 tsw.setNormalString(newNormalString);
857                 psw.setNormalString(newNormalString);
858
859                 expected.add(psw);
860             }
861
862             tx.commit();
863
864             if (transactional)
865                 tx.begin();
866
867             query = pm.newQuery(pm.getExtent(StringWidget.class, true));
868             query.setFilter("normalString.indexOf(\"in the middle\") >= 0");
869             results = (Collection JavaDoc)query.execute();
870
871             try
872             {
873                 assertResultsEqual(expected, results);
874             }
875             finally
876             {
877                 query.closeAll();
878             }
879
880             query.declareParameters("java.lang.String s");
881             query.setFilter("normalString.indexOf(s) >= 0");
882             results = (Collection JavaDoc)query.execute("in the middle");
883
884             try
885             {
886                 assertResultsEqual(expected, results);
887             }
888             finally
889             {
890                 query.closeAll();
891             }
892
893             if (transactional)
894                 tx.commit();
895
896             /*
897              * Query #5
898              *
899              * Tests the length() method.
900              */

901
902             if (transactional)
903                 tx.begin();
904
905             expected = new HashSet JavaDoc();
906
907             for (int i = 0; i < objs.length; ++i)
908             {
909                 StringWidget sw = (StringWidget)objs[i];
910                 String JavaDoc s = sw.getNormalString();
911
912                 if (s != null && s.length() > 10)
913                     expected.add(pm.getObjectById(ids[i], false));
914             }
915
916             query = pm.newQuery(pm.getExtent(StringWidget.class, true));
917             query.setFilter("normalString.length() > 10");
918             results = (Collection JavaDoc)query.execute();
919
920             try
921             {
922                 assertResultsEqual(expected, results);
923             }
924             finally
925             {
926                 query.closeAll();
927             }
928
929             if (transactional)
930                 tx.commit();
931
932             /*
933              * Query #6
934              *
935              * Test querying for values passed in as a parameter by a transient Set
936              * of Strings.
937              */

938
939             if (transactional)
940                 tx.begin();
941
942             Random JavaDoc rand = new Random JavaDoc();
943             HashSet JavaDoc setParameters = new HashSet JavaDoc();
944             expected = new HashSet JavaDoc();
945
946             for (int i = 0; i < objs.length; ++i)
947             {
948                 if (rand.nextBoolean())
949                 {
950                     StringWidget w = (StringWidget)objs[i];
951                     setParameters.add(w.getNormalString());
952                 }
953             }
954             for (int i = 0; i < objs.length; i++)
955             {
956                 StringWidget w = (StringWidget)objs[i];
957
958                 if (setParameters.contains(w.getNormalString()))
959                     expected.add(pm.getObjectById(ids[i], false));
960             }
961
962             query = pm.newQuery();
963             query.setClass(StringWidget.class);
964             query.declareParameters("java.util.Set setParameter");
965             query.setFilter("setParameter.contains(normalString)");
966             results = (Collection JavaDoc)query.execute(setParameters);
967
968             try
969             {
970                 assertResultsEqual(expected, results);
971             }
972             finally
973             {
974                 query.closeAll();
975             }
976
977             if (transactional)
978                 tx.commit();
979         }
980         finally
981         {
982             if (tx.isActive())
983                 tx.rollback();
984
985             pm.close();
986         }
987     }
988
989
990     private void queryBinaryWidgetObjects(boolean transactional) throws Exception JavaDoc
991     {
992         /*
993          * Can't run this test on Firebird or Oracle because comparison of BLOB
994          * fields isn't supported.
995          * Can't run it on SAP DB because comparison of LONG fields isn't
996          * supported, and the normalBinary currently has to map to type LONG.
997          */

998         if ("firebird".equals(vendorID) ||
999             "oracle".equals(vendorID) ||
1000            "sapdb".equals(vendorID))
1001            return;
1002
1003        /*
1004         * Perform various queries and validate the resulting collection.
1005         */

1006
1007        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + BinaryWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
1008        PersistenceManager pm = pmf.getPersistenceManager();
1009        Transaction tx = pm.currentTransaction();
1010
1011        try
1012        {
1013            if (!transactional)
1014                tx.setNontransactionalRead(true);
1015
1016            Query query;
1017            Collection JavaDoc results;
1018
1019            /*
1020             * Query #1
1021             *
1022             * Tests comparison with binary field.
1023             */

1024
1025            if (transactional)
1026                tx.begin();
1027
1028            HashSet JavaDoc expected = new HashSet JavaDoc();
1029
1030            for (int i = 0; i < objs.length; ++i)
1031            {
1032                BinaryWidget bw = (BinaryWidget)pm.getObjectById(ids[i], false);
1033                byte[] ba = bw.getFixedLengthBinary();
1034
1035                if (ba != null && (ba[0] & 0xff) >= 0x7f)
1036                    expected.add(bw);
1037            }
1038
1039            query = pm.newQuery(BinaryWidget.class);
1040            query.declareParameters("byte[] target");
1041            query.setFilter("fixedLengthBinary >= target");
1042            results = (Collection JavaDoc)query.execute(new byte[] { 0x7f });
1043
1044            try
1045            {
1046                assertResultsEqual(expected, results);
1047            }
1048            finally
1049            {
1050                query.closeAll();
1051            }
1052
1053            if (transactional)
1054                tx.commit();
1055
1056            /*
1057             * Query #2
1058             *
1059             * Tests equality with binary field.
1060             */

1061
1062            tx.begin();
1063
1064            byte[] expectedBytes = new byte[] { -20, -10, 0, 10, 20 };
1065            expected = new HashSet JavaDoc();
1066
1067            for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i)
1068            {
1069                BinaryWidget tsw = (BinaryWidget)objs[i * 5 + 1];
1070                BinaryWidget psw = (BinaryWidget)pm.getObjectById(ids[i * 5 + 1], false);
1071
1072                tsw.setNormalBinary(expectedBytes);
1073                psw.setNormalBinary(expectedBytes);
1074
1075                expected.add(psw);
1076            }
1077
1078            tx.commit();
1079
1080            if (transactional)
1081                tx.begin();
1082
1083            query = pm.newQuery(BinaryWidget.class);
1084            query.declareParameters("byte[] target");
1085            query.setFilter("normalBinary == target");
1086            results = (Collection JavaDoc)query.execute(expectedBytes);
1087
1088            try
1089            {
1090                assertResultsEqual(expected, results);
1091            }
1092            finally
1093            {
1094                query.closeAll();
1095            }
1096
1097            if (transactional)
1098                tx.commit();
1099        }
1100        finally
1101        {
1102            if (tx.isActive())
1103                tx.rollback();
1104
1105            pm.close();
1106        }
1107    }
1108
1109
1110    private void queryDateWidgetObjects(boolean transactional) throws Exception JavaDoc
1111    {
1112        /*
1113         * Perform various queries and validate the resulting collection.
1114         */

1115
1116        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + DateWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
1117        PersistenceManager pm = pmf.getPersistenceManager();
1118        Transaction tx = pm.currentTransaction();
1119
1120        try
1121        {
1122            if (!transactional)
1123                tx.setNontransactionalRead(true);
1124
1125            Query query;
1126            Collection JavaDoc results;
1127
1128            /*
1129             * Query #1
1130             *
1131             * Tests a simple query with a java.sql.Date field.
1132             */

1133
1134            if (transactional)
1135                tx.begin();
1136
1137            java.sql.Date JavaDoc epoch = new java.sql.Date JavaDoc(0L);
1138            HashSet JavaDoc expected = new HashSet JavaDoc();
1139
1140            for (int i = 0; i < objs.length; ++i)
1141            {
1142                DateWidget dw = (DateWidget)pm.getObjectById(ids[i], false);
1143                java.sql.Date JavaDoc d = dw.getSQLDateField();
1144
1145                if (d != null && d.before(epoch))
1146                    expected.add(dw);
1147            }
1148
1149            query = pm.newQuery(pm.getExtent(DateWidget.class, true));
1150            query.declareParameters("java.sql.Date epoch");
1151            query.setFilter("sqlDateField < epoch");
1152            results = (Collection JavaDoc)query.execute(epoch);
1153
1154            try
1155            {
1156                assertResultsEqual(expected, results);
1157            }
1158            finally
1159            {
1160                query.closeAll();
1161            }
1162
1163            if (transactional)
1164                tx.commit();
1165
1166            /*
1167             * Query #2
1168             *
1169             * Tests a simple query with a java.sql.Timestamp field.
1170             */

1171
1172            if (transactional)
1173                tx.begin();
1174
1175            java.sql.Timestamp JavaDoc epochTS = new java.sql.Timestamp JavaDoc(0L);
1176            expected = new HashSet JavaDoc();
1177
1178            for (int i = 0; i < objs.length; ++i)
1179            {
1180                DateWidget dw = (DateWidget)pm.getObjectById(ids[i], false);
1181                java.sql.Timestamp JavaDoc ts = dw.getSQLTimestampField();
1182
1183                if (ts != null && ts.after(epochTS))
1184                    expected.add(dw);
1185            }
1186
1187            query = pm.newQuery(pm.getExtent(DateWidget.class, true));
1188            query.declareParameters("java.sql.Timestamp epoch");
1189            query.setFilter("sqlTimestampField > epoch");
1190            results = (Collection JavaDoc)query.execute(epochTS);
1191
1192            try
1193            {
1194                assertResultsEqual(expected, results);
1195            }
1196            finally
1197            {
1198                query.closeAll();
1199            }
1200
1201            if (transactional)
1202                tx.commit();
1203
1204            /*
1205             * Query #6
1206             *
1207             * Test querying for values passed in as a parameter by a transient Set
1208             * of Dates.
1209             */

1210
1211            if (transactional)
1212                tx.begin();
1213
1214            Random JavaDoc rand = new Random JavaDoc();
1215            String JavaDoc[] filterFields =
1216                new String JavaDoc[] {"dateField", "sqlDateField", "sqlTimestampField"};
1217            HashSet JavaDoc[] setParameters =
1218                new HashSet JavaDoc[] {new HashSet JavaDoc(), new HashSet JavaDoc(), new HashSet JavaDoc()};
1219            HashSet JavaDoc[] expectedResults =
1220                new HashSet JavaDoc[] {new HashSet JavaDoc(), new HashSet JavaDoc(), new HashSet JavaDoc()};
1221
1222            for (int i = 0; i < objs.length; ++i)
1223            {
1224                if (rand.nextBoolean())
1225                {
1226                    DateWidget w = (DateWidget)objs[i];
1227
1228                    setParameters[0].add(w.getDateField());
1229                    setParameters[1].add(w.getSQLDateField());
1230                    setParameters[2].add(w.getSQLTimestampField());
1231                }
1232            }
1233            for (int i = 0; i < objs.length; i++)
1234            {
1235                DateWidget w = (DateWidget)objs[i];
1236                Object JavaDoc obj = pm.getObjectById(ids[i], false);
1237
1238                if (setParameters[0].contains(w.getDateField()))
1239                    expectedResults[0].add(obj);
1240                if (setParameters[1].contains(w.getSQLDateField()))
1241                    expectedResults[1].add(obj);
1242                if (setParameters[2].contains(w.getSQLTimestampField()))
1243                    expectedResults[2].add(obj);
1244            }
1245
1246            query = pm.newQuery(DateWidget.class);
1247
1248            for (int i = 0; i < filterFields.length; ++i)
1249            {
1250                query.declareParameters("java.util.Set setParameter");
1251                query.setFilter("setParameter.contains(" + filterFields[i] + ")");
1252                results = (Collection JavaDoc)query.execute(setParameters[i]);
1253
1254                try
1255                {
1256                    assertResultsEqual(expectedResults[i], results);
1257                }
1258                finally
1259                {
1260                    query.closeAll();
1261                }
1262            }
1263
1264            if (transactional)
1265                tx.commit();
1266        }
1267        finally
1268        {
1269            if (tx.isActive())
1270                tx.rollback();
1271
1272            pm.close();
1273        }
1274    }
1275
1276
1277    private void queryFloatWidgetObjects(boolean transactional) throws Exception JavaDoc
1278    {
1279        /*
1280         * Perform various queries and validate the resulting collection.
1281         */

1282
1283        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + FloatWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
1284        PersistenceManager pm = pmf.getPersistenceManager();
1285        Transaction tx = pm.currentTransaction();
1286
1287        try
1288        {
1289            if (!transactional)
1290                tx.setNontransactionalRead(true);
1291
1292            Query query;
1293            Collection JavaDoc results;
1294
1295            /*
1296             * Query #1
1297             *
1298             * Tests a simple query with a float field.
1299             */

1300
1301            if (transactional)
1302                tx.begin();
1303
1304            HashSet JavaDoc expected = new HashSet JavaDoc();
1305
1306            for (int i = 0; i < objs.length; ++i)
1307            {
1308                FloatWidget fw = (FloatWidget)objs[i];
1309
1310                if (fw.getFloatField() < 0 || (fw.getFloatObjField() != null && fw.getFloatObjField().floatValue() > 0))
1311                    expected.add(pm.getObjectById(ids[i], false));
1312            }
1313
1314            query = pm.newQuery(pm.getExtent(FloatWidget.class, true));
1315            query.setFilter("floatField < 0f || floatObjField > 0.0F");
1316            results = (Collection JavaDoc)query.execute();
1317
1318            try
1319            {
1320                assertResultsEqual(expected, results);
1321            }
1322            finally
1323            {
1324                query.closeAll();
1325            }
1326
1327            if (transactional)
1328                tx.commit();
1329
1330            /*
1331             * Query #2
1332             *
1333             * Tests a simple query with a double field.
1334             */

1335
1336            if (transactional)
1337                tx.begin();
1338
1339            expected = new HashSet JavaDoc();
1340
1341            /*
1342             * In order for the following query to (hopefully) match some, not
1343             * all and not none, of the candidate objects, min and max should
1344             * be chosen such that the exponent value is about mid-range between
1345             * the boundaries set by FloatWidget.MIN_DOUBLE_VALUE/MAX_DOUBLE_VALUE.
1346             */

1347            double min = -1e31;
1348            double max = 1e31;
1349
1350            for (int i = 0; i < objs.length; ++i)
1351            {
1352                FloatWidget fw = (FloatWidget)objs[i];
1353
1354                if (fw.getDoubleField() >= min || (fw.getDoubleObjField() != null && fw.getDoubleObjField().doubleValue() <= max))
1355                    expected.add(pm.getObjectById(ids[i], false));
1356            }
1357
1358            query = pm.newQuery(pm.getExtent(FloatWidget.class, true));
1359            query.declareParameters("double min, double max");
1360            query.setFilter("doubleField >= min || doubleObjField <= max");
1361            results = (Collection JavaDoc)query.execute(new Double JavaDoc(min), new Double JavaDoc(max));
1362
1363            try
1364            {
1365                assertResultsEqual(expected, results);
1366            }
1367            finally
1368            {
1369                query.closeAll();
1370            }
1371
1372            if (transactional)
1373                tx.commit();
1374        }
1375        finally
1376        {
1377            if (tx.isActive())
1378                tx.rollback();
1379
1380            pm.close();
1381        }
1382    }
1383
1384
1385    private void querySetWidgetObjects(Class JavaDoc setClass, boolean transactional) throws Exception JavaDoc
1386    {
1387        /*
1388         * Perform various queries and validate the resulting collection.
1389         */

1390
1391        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + setClass.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
1392        PersistenceManager pm = pmf.getPersistenceManager();
1393        Transaction tx = pm.currentTransaction();
1394
1395        try
1396        {
1397            if (!transactional)
1398                tx.setNontransactionalRead(true);
1399
1400            Extent swExtent = pm.getExtent(setClass, true);
1401
1402            setWidgetQuery1(transactional, pm, tx, swExtent, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1403            setWidgetQuery1(transactional, pm, tx, swExtent, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
1404            setWidgetQuery2(transactional, pm, tx, swExtent);
1405
1406            try
1407            {
1408                setWidgetQuery3(transactional, pm, tx, swExtent, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1409                setWidgetQuery3(transactional, pm, tx, swExtent, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
1410                setWidgetQuery4(transactional, pm, tx, swExtent);
1411                setWidgetQuery5(transactional, pm, tx, swExtent, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1412                setWidgetQuery5(transactional, pm, tx, swExtent, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
1413                setWidgetQuery7(transactional, pm, tx, swExtent, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1414                setWidgetQuery7(transactional, pm, tx, swExtent, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
1415                setWidgetQuery8(transactional, pm, tx, swExtent);
1416                setWidgetQuery9(transactional, pm, tx, swExtent, NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1417                setWidgetQuery9(transactional, pm, tx, swExtent, NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
1418                setWidgetQuery10(transactional, pm, tx, swExtent);
1419            }
1420            catch (JDOException e)
1421            {
1422                if ("mysql".equals(vendorID))
1423                {
1424                    if (tx.isActive())
1425                        tx.rollback();
1426
1427                    LOG.warn("A query test involving the isEmpty() or contains() method failed, probably because MySQL doesn't (yet) support subqueries");
1428                }
1429                else
1430                    throw e;
1431            }
1432
1433            setWidgetQuery6(transactional, pm, tx, swExtent);
1434        }
1435        finally
1436        {
1437            if (tx.isActive())
1438                tx.rollback();
1439
1440            pm.close();
1441        }
1442    }
1443
1444
1445    /**
1446     * Retrieve the set from the HasNormalSetField object using the given method.
1447     *
1448     * @param sw The HasNormalSetField object to get the Set from.
1449     * @param getSetMethod The Method to use to get the set field.
1450     *
1451     * @return The set from the HasNormalSetField object using the given method.
1452     */

1453    private static Set JavaDoc getSet(HasNormalSetField sw, Method JavaDoc getSetMethod)
1454        throws Exception JavaDoc
1455    {
1456        return (Set JavaDoc) getSetMethod.invoke(sw, null);
1457    }
1458
1459
1460    private void setWidgetQuery1(boolean transactional,
1461                                 PersistenceManager pm,
1462                                 Transaction tx,
1463                                 Extent swExtent,
1464                                 String JavaDoc setFieldName,
1465                                 Method JavaDoc getSetMethod) throws Exception JavaDoc
1466    {
1467        Query query;
1468        Collection JavaDoc results;
1469
1470        /*
1471         * Query #1
1472         *
1473         * Tests a typical usage of contains() on a normal set.
1474         */

1475
1476        if (transactional)
1477            tx.begin();
1478
1479        for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
1480        {
1481            query = pm.newQuery(swExtent);
1482
1483            Class JavaDoc elementClass = (queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0 ? StringWidget.class : Widget.class;
1484
1485            query.declareVariables(elementClass.getName() + " w");
1486            query.setFilter(setFieldName + ".contains(w) && w.intField < 0");
1487
1488            if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1489                query.setOrdering("numWidgets ascending");
1490
1491            results = (Collection JavaDoc)query.execute();
1492
1493            try
1494            {
1495                HashSet JavaDoc expected = new HashSet JavaDoc();
1496
1497                for (int i = 0; i < objs.length; ++i)
1498                {
1499                    HasNormalSetField sw = (HasNormalSetField)objs[i];
1500
1501                    Iterator JavaDoc j = getSet(sw, getSetMethod).iterator();
1502                    boolean found = false;
1503
1504                    while (j.hasNext())
1505                    {
1506                        Widget w = (Widget)j.next();
1507
1508                        if (elementClass.isInstance(w) && w.getIntField() < 0)
1509                        {
1510                            found = true;
1511                            break;
1512                        }
1513                    }
1514
1515                    if (found)
1516                        expected.add(pm.getObjectById(ids[i], false));
1517                }
1518
1519                assertResultsEqual(expected, results);
1520            }
1521            finally
1522            {
1523                query.closeAll();
1524            }
1525        }
1526
1527        if (transactional)
1528            tx.commit();
1529    }
1530
1531
1532    private void setWidgetQuery2(boolean transactional,
1533                                 PersistenceManager pm,
1534                                 Transaction tx,
1535                                 Extent swExtent) throws Exception JavaDoc
1536    {
1537        if (!HasInverseSetField.class.isAssignableFrom(swExtent.getCandidateClass()))
1538            return;
1539
1540        Query query;
1541        Collection JavaDoc results;
1542
1543        /*
1544         * Query #2
1545         *
1546         * Tests a typical usage of contains() on an inverse set.
1547         */

1548
1549        if (transactional)
1550            tx.begin();
1551
1552        for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
1553        {
1554            if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1555                continue;
1556
1557            query = pm.newQuery(swExtent);
1558            query.declareVariables("com.triactive.jdo.test.ElementWidget ew");
1559            query.setFilter("inverseSet.contains(ew) && numElementWidgets >= 0 && ew.intField < 0");
1560
1561            if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1562                query.setOrdering("numElementWidgets ascending");
1563
1564            results = (Collection JavaDoc)query.execute();
1565
1566            try
1567            {
1568                HashSet JavaDoc expected = new HashSet JavaDoc();
1569
1570                for (int i = 0; i < objs.length; ++i)
1571                {
1572                    HasInverseSetField sw = (HasInverseSetField)objs[i];
1573
1574                    Iterator JavaDoc j = sw.getInverseSet().iterator();
1575                    boolean found = false;
1576
1577                    while (j.hasNext())
1578                    {
1579                        ElementWidget ew = (ElementWidget)j.next();
1580
1581                        if (ew.getIntField() < 0)
1582                        {
1583                            found = true;
1584                            break;
1585                        }
1586                    }
1587
1588                    if (found)
1589                        expected.add(pm.getObjectById(ids[i], false));
1590                }
1591
1592                assertResultsEqual(expected, results);
1593            }
1594            finally
1595            {
1596                query.closeAll();
1597            }
1598        }
1599
1600        if (transactional)
1601            tx.commit();
1602    }
1603
1604
1605    private void setWidgetQuery3(boolean transactional,
1606                                 PersistenceManager pm,
1607                                 Transaction tx,
1608                                 Extent swExtent,
1609                                 String JavaDoc setFieldName,
1610                                 Method JavaDoc getSetMethod) throws Exception JavaDoc
1611    {
1612        Query query;
1613        Collection JavaDoc results;
1614
1615        /*
1616         * Query #3
1617         *
1618         * Tests a simple usage of isEmpty() on a normal set.
1619         */

1620
1621        if (transactional)
1622            tx.begin();
1623
1624        for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
1625        {
1626            if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1627                continue;
1628
1629            query = pm.newQuery(swExtent);
1630            query.setFilter(setFieldName + ".isEmpty()");
1631
1632            if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1633                query.setOrdering("numWidgets ascending");
1634
1635            results = (Collection JavaDoc)query.execute();
1636
1637            try
1638            {
1639                HashSet JavaDoc expected = new HashSet JavaDoc();
1640
1641                for (int i = 0; i < objs.length; ++i)
1642                {
1643                    HasNormalSetField sw = (HasNormalSetField)objs[i];
1644
1645                    if (getSet(sw, getSetMethod).isEmpty())
1646                        expected.add(pm.getObjectById(ids[i], false));
1647                }
1648
1649                assertResultsEqual(expected, results);
1650            }
1651            finally
1652            {
1653                query.closeAll();
1654            }
1655        }
1656
1657        if (transactional)
1658            tx.commit();
1659    }
1660
1661
1662    private void setWidgetQuery4(boolean transactional,
1663                                 PersistenceManager pm,
1664                                 Transaction tx,
1665                                 Extent swExtent) throws Exception JavaDoc
1666    {
1667        if (!HasInverseSetField.class.isAssignableFrom(swExtent.getCandidateClass()))
1668            return;
1669
1670        Query query;
1671        Collection JavaDoc results;
1672
1673        /*
1674         * Query #4
1675         *
1676         * Tests a simple usage of isEmpty() on an inverse set.
1677         */

1678
1679        if (transactional)
1680            tx.begin();
1681
1682        for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
1683        {
1684            if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1685                continue;
1686
1687            query = pm.newQuery(swExtent);
1688            query.setFilter("inverseSet.isEmpty() == true");
1689
1690            if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1691                query.setOrdering("numElementWidgets ascending");
1692
1693            results = (Collection JavaDoc)query.execute();
1694
1695            try
1696            {
1697                HashSet JavaDoc expected = new HashSet JavaDoc();
1698
1699                for (int i = 0; i < objs.length; ++i)
1700                {
1701                    HasInverseSetField sw = (HasInverseSetField)objs[i];
1702
1703                    if (sw.getInverseSet().isEmpty())
1704                        expected.add(pm.getObjectById(ids[i], false));
1705                }
1706
1707                assertResultsEqual(expected, results);
1708            }
1709            finally
1710            {
1711                query.closeAll();
1712            }
1713        }
1714
1715        if (transactional)
1716            tx.commit();
1717    }
1718
1719
1720    private void setWidgetQuery5(boolean transactional,
1721                                 PersistenceManager pm,
1722                                 Transaction tx,
1723                                 Extent swExtent,
1724                                 String JavaDoc setFieldName,
1725                                 Method JavaDoc getSetMethod) throws Exception JavaDoc
1726    {
1727        Query query;
1728        Collection JavaDoc results;
1729
1730        /*
1731         * Query #5
1732         *
1733         * Tests the use of a variable where we're *not* navigating into
1734         * any fields within the variable.
1735         */

1736
1737        if (transactional)
1738            tx.begin();
1739
1740        for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
1741        {
1742            if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1743                continue;
1744
1745            HashSet JavaDoc expected = new HashSet JavaDoc();
1746            Widget target;
1747
1748            /*
1749             * Find a ???SetWidget with a non-empty normalSet field. Get a
1750             * Widget out of that set to serve as the target for the next
1751             * query.
1752             */

1753            query = pm.newQuery(swExtent);
1754            query.setFilter("!" + setFieldName + ".isEmpty()");
1755
1756            results = (Collection JavaDoc)query.execute();
1757
1758            try
1759            {
1760                HasNormalSetField sw = (HasNormalSetField)results.iterator().next();
1761
1762                target = (Widget)getSet(sw, getSetMethod).iterator().next();
1763                expected.add(sw);
1764            }
1765            finally
1766            {
1767                query.closeAll();
1768            }
1769
1770            /*
1771             * Query for the ???SetWidget whose normalSet contains the target
1772             * Widget.
1773             */

1774            query = pm.newQuery(swExtent);
1775            query.declareImports("import com.triactive.jdo.test.Widget;");
1776            query.declareParameters("Widget target");
1777            query.declareVariables("Widget w");
1778            query.setFilter(setFieldName + ".contains(w) && w == target");
1779
1780            if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1781                query.setOrdering("numWidgets ascending");
1782
1783            results = (Collection JavaDoc)query.execute(target);
1784
1785            try
1786            {
1787                assertResultsEqual(expected, results);
1788            }
1789            finally
1790            {
1791                query.closeAll();
1792            }
1793        }
1794
1795        if (transactional)
1796            tx.commit();
1797    }
1798
1799
1800    private void setWidgetQuery6(boolean transactional,
1801                                 PersistenceManager pm,
1802                                 Transaction tx,
1803                                 Extent swExtent) throws Exception JavaDoc
1804    {
1805        Query query;
1806        Collection JavaDoc results;
1807
1808        /*
1809         * Query #6
1810         *
1811         * Tests a query that filters an extent by candidate class, in
1812         * addition to an explicit filter.
1813         */

1814
1815        if (transactional)
1816            tx.begin();
1817
1818        query = pm.newQuery(pm.getExtent(Widget.class, true));
1819        query.setClass(StringWidget.class);
1820        query.setFilter("intField < 0");
1821
1822        results = (Collection JavaDoc)query.execute();
1823        
1824        try
1825        {
1826            Query expectedQuery = pm.newQuery(pm.getExtent(StringWidget.class, false));
1827            expectedQuery.setFilter("intField < 0");
1828
1829            Collection JavaDoc expectedResults = (Collection JavaDoc)expectedQuery.execute();
1830
1831            try
1832            {
1833                HashSet JavaDoc expected = new HashSet JavaDoc(expectedResults);
1834
1835                assertResultsEqual(expected, results);
1836            }
1837            finally
1838            {
1839                expectedQuery.closeAll();
1840            }
1841        }
1842        finally
1843        {
1844            query.closeAll();
1845        }
1846
1847        if (transactional)
1848            tx.commit();
1849    }
1850
1851
1852    private void setWidgetQuery7(boolean transactional,
1853                                 PersistenceManager pm,
1854                                 Transaction tx,
1855                                 Extent swExtent,
1856                                 String JavaDoc setFieldName,
1857                                 Method JavaDoc getSetMethod) throws Exception JavaDoc
1858    {
1859        Query query;
1860        Collection JavaDoc results;
1861        HashSet JavaDoc expected;
1862
1863        /*
1864         * Query #7
1865         *
1866         * Test querying for containment in a persistent normal Set parameter.
1867         */

1868
1869        boolean hasRun = false;
1870        for (int i = 0; i < objs.length && !hasRun; ++i)
1871        {
1872            if (transactional)
1873                tx.begin();
1874
1875            expected = new HashSet JavaDoc();
1876            HasNormalSetField sw = (HasNormalSetField)pm.getObjectById(ids[i], false);
1877
1878            final Set JavaDoc set = getSet(sw, getSetMethod);
1879            if (!set.isEmpty())
1880            {
1881                expected.addAll(set);
1882
1883                query = pm.newQuery(pm.getExtent(Widget.class, true));
1884                query.declareParameters("java.util.Set setParameter");
1885                query.setFilter("setParameter.contains(this)");
1886                results = (Collection JavaDoc)query.execute(set);
1887
1888                try
1889                {
1890                    assertResultsEqual(expected, results);
1891                }
1892                finally
1893                {
1894                    query.closeAll();
1895                }
1896
1897                hasRun = true;
1898            }
1899
1900            if (transactional)
1901                tx.commit();
1902        }
1903    }
1904
1905
1906    private void setWidgetQuery8(boolean transactional,
1907                                 PersistenceManager pm,
1908                                 Transaction tx,
1909                                 Extent swExtent) throws Exception JavaDoc
1910    {
1911        if (!HasInverseSetField.class.isAssignableFrom(swExtent.getCandidateClass()))
1912            return;
1913
1914        Query query;
1915        Collection JavaDoc results;
1916        HashSet JavaDoc expected;
1917
1918        /*
1919         * Query #8
1920         *
1921         * Test querying for containment in a persistent inverse Set parameter.
1922         */

1923
1924        boolean hasRun = false;
1925        for (int i = 0; i < objs.length && !hasRun; ++i)
1926        {
1927            if (transactional)
1928                tx.begin();
1929
1930            expected = new HashSet JavaDoc();
1931            HasInverseSetField sw = (HasInverseSetField)pm.getObjectById(ids[i], false);
1932
1933            if (!sw.getInverseSet().isEmpty())
1934            {
1935                expected.addAll(sw.getInverseSet());
1936
1937                query = pm.newQuery(pm.getExtent(Widget.class, true));
1938                query.declareParameters("java.util.Set setParameter");
1939                query.setFilter("setParameter.contains(this)");
1940                results = (Collection JavaDoc)query.execute(sw.getInverseSet());
1941
1942                try
1943                {
1944                    assertResultsEqual(expected, results);
1945                }
1946                finally
1947                {
1948                    query.closeAll();
1949                }
1950
1951                hasRun = true;
1952            }
1953
1954            if (transactional)
1955                tx.commit();
1956        }
1957    }
1958
1959
1960    private void setWidgetQuery9(boolean transactional,
1961                                 PersistenceManager pm,
1962                                 Transaction tx,
1963                                 Extent swExtent,
1964                                 String JavaDoc setFieldName,
1965                                 Method JavaDoc getSetMethod) throws Exception JavaDoc
1966    {
1967        Query query;
1968        Collection JavaDoc results;
1969        HashSet JavaDoc expected;
1970
1971        /*
1972         * Query #9
1973         *
1974         * Test querying for non-containment in a persistent normal Set parameter.
1975         */

1976
1977        boolean hasRun = false;
1978        for (int i = 0; i < objs.length && !hasRun; ++i)
1979        {
1980            if (transactional)
1981                tx.begin();
1982
1983            expected = new HashSet JavaDoc();
1984            HasNormalSetField sw = (HasNormalSetField)pm.getObjectById(ids[i], false);
1985
1986            final Set JavaDoc set = getSet(sw, getSetMethod);
1987            if (!set.isEmpty())
1988            {
1989                Extent e = pm.getExtent(Widget.class, true);
1990                for (Iterator JavaDoc it = e.iterator(); it.hasNext(); )
1991                {
1992                    expected.add(it.next());
1993                }
1994                expected.removeAll(set);
1995
1996                query = pm.newQuery(e);
1997                query.declareParameters("java.util.Set setParameter");
1998                query.setFilter("!setParameter.contains(this)");
1999                results = (Collection JavaDoc)query.execute(set);
2000
2001                try
2002                {
2003                    assertResultsEqual(expected, results);
2004                }
2005                finally
2006                {
2007                    query.closeAll();
2008                }
2009
2010                hasRun = true;
2011            }
2012
2013            if (transactional)
2014                tx.commit();
2015        }
2016    }
2017
2018
2019    private void setWidgetQuery10(boolean transactional,
2020                                  PersistenceManager pm,
2021                                  Transaction tx,
2022                                  Extent swExtent) throws Exception JavaDoc
2023    {
2024        if (!HasInverseSetField.class.isAssignableFrom(swExtent.getCandidateClass()))
2025            return;
2026
2027        Query query;
2028        Collection JavaDoc results;
2029        HashSet JavaDoc expected;
2030
2031        /*
2032         * Query #10
2033         *
2034         * Test querying for non-containment in a persistent inverse Set parameter.
2035         */

2036
2037        boolean hasRun = false;
2038        for (int i = 0; i < objs.length && !hasRun; ++i)
2039        {
2040            if (transactional)
2041                tx.begin();
2042
2043            expected = new HashSet JavaDoc();
2044            HasInverseSetField sw = (HasInverseSetField)pm.getObjectById(ids[i], false);
2045
2046            if (!sw.getInverseSet().isEmpty())
2047            {
2048                Extent e = pm.getExtent(Widget.class, true);
2049                for (Iterator JavaDoc it = e.iterator(); it.hasNext(); )
2050                {
2051                    expected.add(it.next());
2052                }
2053                expected.removeAll(sw.getInverseSet());
2054
2055                query = pm.newQuery(e);
2056                query.declareParameters("java.util.Set setParameter");
2057                query.setFilter("!setParameter.contains(this)");
2058                results = (Collection JavaDoc)query.execute(sw.getInverseSet());
2059
2060                try
2061                {
2062                    assertResultsEqual(expected, results);
2063                }
2064                finally
2065                {
2066                    query.closeAll();
2067                }
2068
2069                hasRun = true;
2070            }
2071
2072            if (transactional)
2073                tx.commit();
2074        }
2075    }
2076
2077
2078    private void queryElementWidgetObjects(boolean transactional) throws Exception JavaDoc
2079    {
2080        /*
2081         * Perform various queries and validate the resulting collection.
2082         */

2083
2084        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + ElementWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
2085        PersistenceManager pm = pmf.getPersistenceManager();
2086        Transaction tx = pm.currentTransaction();
2087
2088        try
2089        {
2090            if (!transactional)
2091                tx.setNontransactionalRead(true);
2092
2093            Query query;
2094            Collection JavaDoc results;
2095
2096            /*
2097             * Query #1
2098             */

2099
2100            if (transactional)
2101                tx.begin();
2102
2103            query = pm.newQuery(pm.getExtent(ElementWidget.class, true));
2104            query.declareVariables("com.triactive.jdo.test.ElementWidget ew");
2105            query.setFilter("intField < 0 & owner.normalSet.contains(ew) & ew.intField < 0");
2106            results = (Collection JavaDoc)query.execute();
2107
2108            try
2109            {
2110                HashSet JavaDoc expected = new HashSet JavaDoc();
2111
2112                for (int i = 0; i < objs.length; ++i)
2113                {
2114                    SetWidget sw = (SetWidget)pm.getObjectById(ids[i], false);
2115                    Iterator JavaDoc j = sw.getNormalSet().iterator();
2116                    boolean found = false;
2117
2118                    while (j.hasNext())
2119                    {
2120                        Widget w = (Widget)j.next();
2121
2122                        if (w.getIntField() < 0)
2123                        {
2124                            found = true;
2125                            break;
2126                        }
2127                    }
2128
2129                    if (found)
2130                    {
2131                        j = sw.getInverseSet().iterator();
2132
2133                        while (j.hasNext())
2134                        {
2135                            ElementWidget ew = (ElementWidget)j.next();
2136
2137                            if (ew.getIntField() < 0)
2138                                expected.add(ew);
2139                        }
2140                    }
2141                }
2142
2143                assertResultsEqual(expected, results);
2144            }
2145            finally
2146            {
2147                query.closeAll();
2148            }
2149
2150            if (transactional)
2151                tx.commit();
2152
2153            /*
2154             * Query #2
2155             *
2156             * Test querying for values passed in as a parameter by a transient Set
2157             * of nested Objects.
2158             */

2159
2160            if (transactional)
2161                tx.begin();
2162
2163            Random JavaDoc rand = new Random JavaDoc();
2164            HashSet JavaDoc setParameters = new HashSet JavaDoc();
2165            HashSet JavaDoc expected = new HashSet JavaDoc();
2166
2167            for (int i = 0; i < objs.length; ++i)
2168            {
2169                SetWidget sw = (SetWidget)pm.getObjectById(ids[i], false);
2170
2171                if (rand.nextBoolean() && !sw.getInverseSet().isEmpty())
2172                {
2173                    for (Iterator JavaDoc it = sw.getInverseSet().iterator(); it.hasNext(); )
2174                    {
2175                        if (rand.nextBoolean())
2176                        {
2177                            ElementWidget ew = (ElementWidget)it.next();
2178                            setParameters.add(sw);
2179                            expected.add(ew);
2180                        }
2181                    }
2182                }
2183            }
2184
2185            query = pm.newQuery(pm.getExtent(ElementWidget.class, true));
2186            query.declareParameters("java.util.Set setParameter");
2187            query.setFilter("setParameter.contains(owner)");
2188            results = (Collection JavaDoc)query.execute(setParameters);
2189
2190            try
2191            {
2192                assertResultsEqual(expected, results);
2193            }
2194            finally
2195            {
2196                query.closeAll();
2197            }
2198
2199            if (transactional)
2200                tx.commit();
2201        }
2202        finally
2203        {
2204            if (tx.isActive())
2205                tx.rollback();
2206
2207            pm.close();
2208        }
2209    }
2210
2211
2212    private void queryCollectionsUsingNestedContains(Class JavaDoc setClass,
2213                                                     String JavaDoc setFieldName,
2214                                                     Method JavaDoc getSetMethod) throws Exception JavaDoc
2215    {
2216        /*
2217         * Perform various queries and validate the resulting collection.
2218         */

2219        PersistenceManager pm = pmf.getPersistenceManager();
2220        Transaction tx = pm.currentTransaction();
2221
2222        try
2223        {
2224            LOG.info("Inserting " + TEST_OBJECT_COUNT + " " + setClass.getName() + " objects");
2225
2226            for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
2227            {
2228                tx.begin();
2229
2230                HasNormalSetField sw = (HasNormalSetField)setClass.newInstance();
2231                sw.fillRandom(10, true);
2232
2233                objs[i] = (TestObject)((TestObject)sw).clone();
2234
2235                pm.makePersistent(sw);
2236
2237                ids[i] = JDOHelper.getObjectId(sw);
2238
2239                tx.commit();
2240            }
2241
2242            LOG.info("Querying nested collections (" + setFieldName + ") in a " + setClass.getName() + " object");
2243
2244            tx.begin();
2245
2246            for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags)
2247            {
2248                if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
2249                    continue;
2250
2251                Query query = pm.newQuery(pm.getExtent(setClass, true));
2252                query.declareImports("import com.triactive.jdo.test.Widget;");
2253                query.declareVariables(setClass.getName() + " sw; Widget w");
2254                query.setFilter(setFieldName + ".contains(sw) && sw." + setFieldName + ".contains(w) && w.intField < 0");
2255
2256                if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
2257                    query.setOrdering("numWidgets ascending");
2258
2259                Collection JavaDoc results = (Collection JavaDoc)query.execute();
2260
2261                try
2262                {
2263                    HashSet JavaDoc expected = new HashSet JavaDoc();
2264
2265                    for (int i = 0; i < objs.length; ++i)
2266                    {
2267                        HasNormalSetField sw = (HasNormalSetField)objs[i];
2268
2269                        Iterator JavaDoc j = getSet(sw, getSetMethod).iterator();
2270                        boolean found = false;
2271
2272                        while (j.hasNext())
2273                        {
2274                            Widget w = (Widget)j.next();
2275
2276                            if (w instanceof HasNormalSetField)
2277                            {
2278                                HasNormalSetField sw1 = (HasNormalSetField)w;
2279                                Iterator JavaDoc k = getSet(sw1, getSetMethod).iterator();
2280
2281                                while (k.hasNext())
2282                                {
2283                                    w = (Widget)k.next();
2284
2285                                    if (w.getIntField() < 0)
2286                                    {
2287                                        found = true;
2288                                        break;
2289                                    }
2290                                }
2291
2292                                if (found)
2293                                    break;
2294                            }
2295                        }
2296
2297                        if (found)
2298                            expected.add(pm.getObjectById(ids[i], false));
2299                    }
2300
2301                    assertResultsEqual(expected, results);
2302                }
2303                finally
2304                {
2305                    query.closeAll();
2306                }
2307            }
2308
2309            tx.commit();
2310        }
2311        finally
2312        {
2313            if (tx.isActive())
2314                tx.rollback();
2315
2316            pm.close();
2317        }
2318
2319        removeObjects();
2320    }
2321
2322
2323    private void queryCollectionsUsingCandidateSubclasses(Class JavaDoc setClass, boolean transactional) throws Exception JavaDoc
2324    {
2325        /*
2326         * Perform various queries and validate the resulting collection.
2327         */

2328
2329        LOG.info("Querying collections in a " + setClass.getName() + " object " + (transactional ? "in" : "outside") + " a transaction");
2330        PersistenceManager pm = pmf.getPersistenceManager();
2331        Transaction tx = pm.currentTransaction();
2332
2333        try
2334        {
2335            if (!transactional)
2336                tx.setNontransactionalRead(true);
2337
2338            tx.begin();
2339
2340            HasNormalSetField sw = (HasNormalSetField)setClass.newInstance();
2341            sw.fillRandom(TEST_OBJECT_COUNT);
2342            pm.makePersistent(sw);
2343
2344            tx.commit();
2345
2346            Query query;
2347            Collection JavaDoc results;
2348
2349            /*
2350             * Query #1
2351             */

2352
2353            if (transactional)
2354                tx.begin();
2355
2356            Collection JavaDoc normalSet = sw.getNormalSet();
2357
2358            query = pm.newQuery(DateWidget.class, normalSet);
2359            query.declareImports("import java.util.Date;");
2360            query.declareParameters("Date epoch");
2361            query.setFilter("dateField < epoch");
2362            results = (Collection JavaDoc)query.execute(new Date JavaDoc(0L));
2363
2364            try
2365            {
2366                Query expectedQuery = pm.newQuery(pm.getExtent(DateWidget.class, true));
2367                expectedQuery.declareImports("import java.util.Date;");
2368                expectedQuery.declareParameters("Date epoch");
2369                expectedQuery.setFilter("dateField < epoch");
2370
2371                Collection JavaDoc expectedResults = (Collection JavaDoc)expectedQuery.execute(new Date JavaDoc(0L));
2372
2373                try
2374                {
2375                    HashSet JavaDoc expected = new HashSet JavaDoc(expectedResults);
2376
2377                    assertResultsEqual(expected, results);
2378                }
2379                finally
2380                {
2381                    expectedQuery.closeAll();
2382                }
2383            }
2384            finally
2385            {
2386                query.closeAll();
2387            }
2388
2389            if (transactional)
2390                tx.commit();
2391
2392            tx.begin();
2393
2394            pm.deletePersistent(sw);
2395
2396            tx.commit();
2397        }
2398        finally
2399        {
2400            if (tx.isActive())
2401                tx.rollback();
2402
2403            pm.close();
2404        }
2405    }
2406
2407
2408    private void queryOwnerWidgetObjects(boolean transactional) throws Exception JavaDoc
2409    {
2410        /*
2411         * Perform various queries and validate the resulting collection.
2412         */

2413
2414        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + OwnerWidget.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
2415        PersistenceManager pm = pmf.getPersistenceManager();
2416        Transaction tx = pm.currentTransaction();
2417
2418        try
2419        {
2420            if (!transactional)
2421                tx.setNontransactionalRead(true);
2422
2423            Query query;
2424            Collection JavaDoc results;
2425
2426            /*
2427             * Query #1
2428             */

2429
2430            if (transactional)
2431                tx.begin();
2432
2433            query = pm.newQuery(pm.getExtent(OwnerWidget.class, true));
2434            query.declareImports("import com.triactive.jdo.test.DateWidget; import java.util.Date;");
2435            query.declareParameters("Date epoch");
2436            query.setFilter("((DateWidget)cloneable).dateField != null && ((DateWidget)cloneable).dateField < epoch");
2437
2438            Date JavaDoc epoch = new Date JavaDoc(0L);
2439            results = (Collection JavaDoc)query.execute(epoch);
2440
2441            try
2442            {
2443                HashSet JavaDoc expected = new HashSet JavaDoc();
2444
2445                for (int i = 0; i < objs.length; ++i)
2446                {
2447                    OwnerWidget ow = (OwnerWidget)objs[i];
2448
2449                    if (ow.getCloneable() instanceof DateWidget)
2450                    {
2451                        DateWidget dw = (DateWidget)ow.getCloneable();
2452
2453                        if (dw.getDateField() != null && dw.getDateField().before(epoch))
2454                            expected.add(pm.getObjectById(ids[i], false));
2455                    }
2456                }
2457
2458                assertResultsEqual(expected, results);
2459            }
2460            finally
2461            {
2462                query.closeAll();
2463            }
2464
2465            if (transactional)
2466                tx.commit();
2467
2468            /*
2469             * Query #2
2470             */

2471
2472            if (transactional)
2473                tx.begin();
2474
2475            query = pm.newQuery(pm.getExtent(OwnerWidget.class, true));
2476            query.declareImports("import com.triactive.jdo.test.DateWidget; import java.util.Date;");
2477            query.declareParameters("Date epoch");
2478            query.setFilter("((DateWidget)testObject).dateField != null && ((DateWidget)testObject).dateField > epoch");
2479
2480            results = (Collection JavaDoc)query.execute(epoch);
2481
2482            try
2483            {
2484                HashSet JavaDoc expected = new HashSet JavaDoc();
2485
2486                for (int i = 0; i < objs.length; ++i)
2487                {
2488                    OwnerWidget ow = (OwnerWidget)objs[i];
2489
2490                    if (ow.getTestObject() instanceof DateWidget)
2491                    {
2492                        DateWidget dw = (DateWidget)ow.getTestObject();
2493
2494                        if (dw.getDateField() != null && dw.getDateField().after(epoch))
2495                            expected.add(pm.getObjectById(ids[i], false));
2496                    }
2497                }
2498
2499                assertResultsEqual(expected, results);
2500            }
2501            finally
2502            {
2503                query.closeAll();
2504            }
2505
2506            if (transactional)
2507                tx.commit();
2508        }
2509        finally
2510        {
2511            if (tx.isActive())
2512                tx.rollback();
2513
2514            pm.close();
2515        }
2516    }
2517
2518
2519    private void queryKeywordConflictObjects(boolean transactional) throws Exception JavaDoc
2520    {
2521        /*
2522         * Perform various queries and validate the resulting collection.
2523         */

2524
2525        LOG.info("Querying " + TEST_OBJECT_COUNT + " " + KeywordConflict.class.getName() + " objects " + (transactional ? "in" : "outside") + " a transaction");
2526        PersistenceManager pm = pmf.getPersistenceManager();
2527        Transaction tx = pm.currentTransaction();
2528
2529        try
2530        {
2531            if (!transactional)
2532                tx.setNontransactionalRead(true);
2533
2534            Query query;
2535            Collection JavaDoc results;
2536
2537            /*
2538             * Query #1
2539             */

2540
2541            if (transactional)
2542                tx.begin();
2543
2544            query = pm.newQuery(pm.getExtent(KeywordConflict.class, true));
2545            query.setFilter("column < 0 || select >= 0 || where < 0 || varchar >= 0 || decimal < 0");
2546            results = (Collection JavaDoc)query.execute();
2547
2548            try
2549            {
2550                HashSet JavaDoc expected = new HashSet JavaDoc();
2551
2552                for (int i = 0; i < objs.length; ++i)
2553                {
2554                    KeywordConflict t = (KeywordConflict)objs[i];
2555
2556                    if (t.getColumn() < 0 || t.getSelect() >= 0 || t.getWhere() < 0
2557                     || t.getVarchar() >= 0 || t.getDecimal() < 0)
2558                        expected.add(pm.getObjectById(ids[i], false));
2559                }
2560
2561                assertResultsEqual(expected, results);
2562            }
2563            finally
2564            {
2565                query.closeAll();
2566            }
2567
2568            if (transactional)
2569                tx.commit();
2570        }
2571        finally
2572        {
2573            if (tx.isActive())
2574                tx.rollback();
2575
2576            pm.close();
2577        }
2578    }
2579}
2580
Popular Tags