Source Code Cross Referenced for BasicQueryTest.java in  » Database-ORM » TJDO » com » triactive » jdo » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » TJDO » com.triactive.jdo.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright 2004 (C) TJDO.
0003:         * All rights reserved.
0004:         *
0005:         * This software is distributed under the terms of the TJDO License version 1.0.
0006:         * See the terms of the TJDO License in the documentation provided with this software.
0007:         *
0008:         * $Id: BasicQueryTest.java,v 1.20 2004/03/30 06:15:56 jackknifebarber Exp $
0009:         */
0010:
0011:        package com.triactive.jdo.test;
0012:
0013:        import java.lang.reflect.Method;
0014:        import java.util.Collection;
0015:        import java.util.Date;
0016:        import java.util.HashSet;
0017:        import java.util.Iterator;
0018:        import java.util.Random;
0019:        import java.util.Set;
0020:        import javax.jdo.Extent;
0021:        import javax.jdo.JDOHelper;
0022:        import javax.jdo.JDOException;
0023:        import javax.jdo.PersistenceManager;
0024:        import javax.jdo.Query;
0025:        import javax.jdo.Transaction;
0026:        import org.apache.log4j.Category;
0027:
0028:        /**
0029:         * Tests the basic query functionality of the PersistenceManager.
0030:         *
0031:         * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
0032:         * @version $Revision: 1.20 $
0033:         */
0034:
0035:        public class BasicQueryTest extends StorageTestCase {
0036:            private static final Category LOG = Category
0037:                    .getInstance(BasicQueryTest.class);
0038:
0039:            private static final int QUERY_FLAG_USE_ORDERING = 1;
0040:            private static final int QUERY_FLAG_USE_SUBCLASS = 2;
0041:            private static final int ALL_QUERY_FLAGS = 3;
0042:
0043:            private static final String NORMAL_SET_FIELD = "normalSet";
0044:            private static final String NORMAL_OBJECT_SET_FIELD = "normalObjectSet";
0045:
0046:            private Method GET_NORMAL_SET_METHOD = null;
0047:            private Method GET_NORMAL_OBJECT_SET_METHOD = null;
0048:
0049:            private boolean schemaInitialized = false;
0050:
0051:            /**
0052:             * Used by the JUnit framework to construct tests.  Normally, programmers
0053:             * would never explicitly use this constructor.
0054:             *
0055:             * @param name   Name of the <tt>TestCase</tt>.
0056:             */
0057:
0058:            public BasicQueryTest(String name) {
0059:                super (name);
0060:            }
0061:
0062:            protected void setUp() throws Exception {
0063:                super .setUp();
0064:
0065:                if (!schemaInitialized) {
0066:                    addClassesToSchema(new Class[] { Widget.class,
0067:                            StringWidget.class, BinaryWidget.class,
0068:                            DateWidget.class, FloatWidget.class,
0069:                            SetWidget.class, ElementWidget.class,
0070:                            OwnerWidget.class, KeywordConflict.class,
0071:                            AReallyObnoxiouslyLongWindedNamedObject.class });
0072:
0073:                    schemaInitialized = true;
0074:                }
0075:
0076:                GET_NORMAL_SET_METHOD = HasNormalSetField.class.getMethod(
0077:                        getterName(NORMAL_SET_FIELD), null);
0078:                GET_NORMAL_OBJECT_SET_METHOD = HasNormalSetField.class
0079:                        .getMethod(getterName(NORMAL_OBJECT_SET_FIELD), null);
0080:            }
0081:
0082:            /**
0083:             * Create the standard getter name for the given field.
0084:             *
0085:             * @param field  The field name to create the getter name for.
0086:             *
0087:             * @return  The standard getter name for the given field.
0088:             */
0089:            private static String getterName(String field) {
0090:                return "get" + Character.toUpperCase(field.charAt(0))
0091:                        + field.substring(1);
0092:            }
0093:
0094:            public void testQueryOnWidgets() throws Exception {
0095:                insertObjects(Widget.class);
0096:                queryWidgetObjects(true);
0097:                queryWidgetObjects(false);
0098:                removeObjects();
0099:            }
0100:
0101:            public void testQueryOnStringWidgets() throws Exception {
0102:                insertObjects(StringWidget.class);
0103:                queryStringWidgetObjects(true);
0104:                queryStringWidgetObjects(false);
0105:                removeObjects();
0106:            }
0107:
0108:            public void testQueryOnBinaryWidgets() throws Exception {
0109:                insertObjects(BinaryWidget.class);
0110:                queryBinaryWidgetObjects(true);
0111:                queryBinaryWidgetObjects(false);
0112:                removeObjects();
0113:            }
0114:
0115:            public void testQueryOnDateWidgets() throws Exception {
0116:                insertObjects(DateWidget.class);
0117:                queryDateWidgetObjects(true);
0118:                queryDateWidgetObjects(false);
0119:                removeObjects();
0120:            }
0121:
0122:            public void testQueryOnFloatWidgets() throws Exception {
0123:                insertObjects(FloatWidget.class);
0124:                queryFloatWidgetObjects(true);
0125:                queryFloatWidgetObjects(false);
0126:                removeObjects();
0127:            }
0128:
0129:            public void testQueryOnSetWidgets() throws Exception {
0130:                insertObjects(SetWidget.class);
0131:                querySetWidgetObjects(SetWidget.class, true);
0132:                querySetWidgetObjects(SetWidget.class, false);
0133:                queryElementWidgetObjects(true);
0134:                queryElementWidgetObjects(false);
0135:                removeObjects();
0136:
0137:                queryCollectionsUsingNestedContains(SetWidget.class,
0138:                        NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
0139:                queryCollectionsUsingNestedContains(SetWidget.class,
0140:                        NORMAL_OBJECT_SET_FIELD, GET_NORMAL_OBJECT_SET_METHOD);
0141:                queryCollectionsUsingCandidateSubclasses(SetWidget.class, true);
0142:                queryCollectionsUsingCandidateSubclasses(SetWidget.class, false);
0143:            }
0144:
0145:            public void testQueryOnInterfaceFields() throws Exception {
0146:                insertObjects(OwnerWidget.class);
0147:                queryOwnerWidgetObjects(true);
0148:                queryOwnerWidgetObjects(false);
0149:                removeObjects();
0150:            }
0151:
0152:            public void testQueryOnClassUsingSQLKeywords() throws Exception {
0153:                insertObjects(KeywordConflict.class);
0154:                queryKeywordConflictObjects(true);
0155:                queryKeywordConflictObjects(false);
0156:                removeObjects();
0157:            }
0158:
0159:            public void testQueryOnClassWithLongName() throws Exception {
0160:                insertObjects(AReallyObnoxiouslyLongWindedNamedObject.class);
0161:                queryKeywordConflictObjects(true);
0162:                queryKeywordConflictObjects(false);
0163:                removeObjects();
0164:            }
0165:
0166:            private void queryWidgetObjects(boolean transactional)
0167:                    throws Exception {
0168:                /*
0169:                 * Perform various queries and validate the resulting collection.
0170:                 */
0171:
0172:                LOG
0173:                        .info("Querying " + TEST_OBJECT_COUNT + " "
0174:                                + Widget.class.getName() + " objects "
0175:                                + (transactional ? "in" : "outside")
0176:                                + " a transaction");
0177:                PersistenceManager pm = pmf.getPersistenceManager();
0178:                Transaction tx = pm.currentTransaction();
0179:
0180:                try {
0181:                    if (!transactional)
0182:                        tx.setNontransactionalRead(true);
0183:
0184:                    Query query;
0185:                    Collection results;
0186:
0187:                    /*
0188:                     * Query #1
0189:                     */
0190:
0191:                    if (transactional)
0192:                        tx.begin();
0193:
0194:                    HashSet expected = new HashSet();
0195:
0196:                    for (int i = 0; i < objs.length; ++i) {
0197:                        Widget w = (Widget) objs[i];
0198:
0199:                        if (w.getByteField() > 64
0200:                                || w.getByteObjField() != null
0201:                                || w.getBooleanField() == false
0202:                                || w.getCharField() != 'X'
0203:                                || w.getShortField() <= 0
0204:                                || w.getIntField() >= 0 || w.getLongField() < 0)
0205:                            expected.add(pm.getObjectById(ids[i], false));
0206:                    }
0207:
0208:                    query = pm.newQuery(pm.getExtent(Widget.class, true));
0209:                    query
0210:                            .setFilter("byteField > 64 || byteObjField != null || booleanField == false || charField != 'X' || shortField <= 0 || intField >= 0 || longField < 0");
0211:                    results = (Collection) query.execute();
0212:
0213:                    try {
0214:                        assertResultsEqual(expected, results);
0215:                    } finally {
0216:                        query.closeAll();
0217:                    }
0218:
0219:                    query.declareParameters("java.lang.Byte arg");
0220:                    query
0221:                            .setFilter("byteField>64|byteObjField!=arg|!booleanField|charField!='X'|shortField<=0|intField>=0|longField<0");
0222:                    results = (Collection) query.execute(null);
0223:
0224:                    try {
0225:                        assertResultsEqual(expected, results);
0226:                    } finally {
0227:                        query.closeAll();
0228:                    }
0229:
0230:                    if (transactional)
0231:                        tx.commit();
0232:
0233:                    /*
0234:                     * Query #2
0235:                     */
0236:
0237:                    if (transactional)
0238:                        tx.begin();
0239:
0240:                    expected = new HashSet();
0241:
0242:                    for (int i = 0; i < objs.length; ++i) {
0243:                        Widget w = (Widget) objs[i];
0244:
0245:                        if (w.getIntField() > 0)
0246:                            expected.add(pm.getObjectById(ids[i], false));
0247:                    }
0248:
0249:                    query = pm.newQuery();
0250:                    query.setCandidates(pm.getExtent(Widget.class, true));
0251:                    query.setFilter("intField > 0");
0252:                    query.setOrdering("intField ascending");
0253:                    results = (Collection) query.execute();
0254:
0255:                    try {
0256:                        assertResultsEqual(expected, results);
0257:
0258:                        Iterator i = results.iterator();
0259:                        int lastIntField = 0;
0260:
0261:                        while (i.hasNext()) {
0262:                            Widget obj = (Widget) i.next();
0263:
0264:                            assertTrue("Object returned out of order: " + obj,
0265:                                    obj.getIntField() > lastIntField);
0266:
0267:                            lastIntField = obj.getIntField();
0268:                        }
0269:                    } finally {
0270:                        query.closeAll();
0271:                    }
0272:
0273:                    if (transactional)
0274:                        tx.commit();
0275:
0276:                    /*
0277:                     * Query #3
0278:                     *
0279:                     * Tests a parameter being referenced more than once in the filter string.
0280:                     */
0281:
0282:                    if (transactional)
0283:                        tx.begin();
0284:
0285:                    expected = new HashSet();
0286:
0287:                    for (int i = 0; i < objs.length; ++i) {
0288:                        Widget w = (Widget) objs[i];
0289:
0290:                        if (w.getIntField() > 5 || w.getShortField() > 5)
0291:                            expected.add(pm.getObjectById(ids[i], false));
0292:                    }
0293:
0294:                    query = pm.newQuery(pm.getExtent(Widget.class, true));
0295:                    query.declareParameters("int val");
0296:                    query.setFilter("intField > val || shortField > val");
0297:                    results = (Collection) query.execute(new Integer(5));
0298:
0299:                    try {
0300:                        assertResultsEqual(expected, results);
0301:                    } finally {
0302:                        query.closeAll();
0303:                    }
0304:
0305:                    if (transactional)
0306:                        tx.commit();
0307:
0308:                    /*
0309:                     * Query #4
0310:                     *
0311:                     * Tests operator precedence/parenthesis handling.  All of the
0312:                     * filter expressions include redundant parentheses.
0313:                     */
0314:
0315:                    if (transactional)
0316:                        tx.begin();
0317:
0318:                    /*
0319:                     * We have to keep the values of Widget.shortField and
0320:                     * Widget.intField within these boundaries in order to avoid
0321:                     * arithmetic overflow of these filter expressions on some
0322:                     * databases, namely MS SQL Server.
0323:                     */
0324:                    short minSafeShort = Short.MIN_VALUE - Byte.MIN_VALUE;
0325:                    short maxSafeShort = Short.MAX_VALUE - Byte.MAX_VALUE;
0326:                    int minSafeInt = Integer.MIN_VALUE - Short.MIN_VALUE
0327:                            - Byte.MIN_VALUE;
0328:                    int maxSafeInt = Integer.MAX_VALUE - Short.MAX_VALUE
0329:                            - Byte.MAX_VALUE;
0330:
0331:                    expected = new HashSet();
0332:
0333:                    for (int i = 0; i < objs.length; ++i) {
0334:                        Widget tw = (Widget) objs[i];
0335:                        Widget pw = (Widget) pm.getObjectById(ids[i], false);
0336:
0337:                        short sVal = tw.getShortField();
0338:                        int iVal = tw.getIntField();
0339:
0340:                        if (sVal < minSafeShort || sVal > maxSafeShort) {
0341:                            sVal /= 2;
0342:                            tw.setShortField(sVal);
0343:                            pw.setShortField(sVal);
0344:                        }
0345:
0346:                        if (iVal < minSafeInt || iVal > maxSafeInt) {
0347:                            iVal /= 2;
0348:                            tw.setIntField(iVal);
0349:                            pw.setIntField(iVal);
0350:                        }
0351:
0352:                        if (tw.getIntField() + tw.getShortField()
0353:                                - tw.getByteField() > 0)
0354:                            expected.add(pw);
0355:                    }
0356:
0357:                    if (transactional) {
0358:                        tx.commit();
0359:                        tx.begin();
0360:                    }
0361:
0362:                    String[] filters = {
0363:                            "intField + shortField - byteField > 0",
0364:                            "intField + (shortField - byteField) > 0",
0365:                            "(intField + shortField) - byteField > 0",
0366:                            "(shortField - byteField) + intField > 0",
0367:                            "shortField - byteField + intField > 0",
0368:                            "0 < intField + shortField - byteField",
0369:                            "0 < intField + (shortField - byteField)",
0370:                            "0 < (intField + shortField) - byteField",
0371:                            "0 < (shortField - byteField) + intField",
0372:                            "0 < shortField - byteField + intField" };
0373:
0374:                    query = pm.newQuery(pm.getExtent(Widget.class, true));
0375:
0376:                    for (int i = 0; i < filters.length; ++i) {
0377:                        query.setFilter(filters[i]);
0378:                        results = (Collection) query.execute();
0379:
0380:                        try {
0381:                            assertResultsEqual(expected, results);
0382:                        } finally {
0383:                            query.closeAll();
0384:                        }
0385:                    }
0386:
0387:                    if (transactional)
0388:                        tx.commit();
0389:
0390:                    /*
0391:                     * Query #5
0392:                     *
0393:                     * Another test of operator precedence/parenthesis handling.  Most
0394:                     * of the filter expressions include parentheses that are *not*
0395:                     * redundant.
0396:                     */
0397:
0398:                    if (transactional)
0399:                        tx.begin();
0400:
0401:                    expected = new HashSet();
0402:
0403:                    for (int i = 0; i < objs.length; ++i) {
0404:                        Widget w = (Widget) objs[i];
0405:
0406:                        if (w.getIntField()
0407:                                - (w.getShortField() + w.getByteField()) > 0)
0408:                            expected.add(pm.getObjectById(ids[i], false));
0409:                    }
0410:
0411:                    filters = new String[] {
0412:                            "intField - shortField - byteField > 0",
0413:                            "intField - (shortField + byteField) > 0",
0414:                            "(intField - shortField) - byteField > 0",
0415:                            "-(shortField + byteField) + intField > 0",
0416:                            "0 < intField - shortField - byteField",
0417:                            "0 < intField - (shortField + byteField)",
0418:                            "0 < (intField - shortField) - byteField",
0419:                            "0 < -(shortField + byteField) + intField" };
0420:
0421:                    query = pm.newQuery();
0422:                    query.setCandidates(pm.getExtent(Widget.class, true));
0423:
0424:                    for (int i = 0; i < filters.length; ++i) {
0425:                        query.setFilter(filters[i]);
0426:                        results = (Collection) query.execute();
0427:
0428:                        try {
0429:                            assertResultsEqual(expected, results);
0430:                        } finally {
0431:                            query.closeAll();
0432:                        }
0433:                    }
0434:
0435:                    if (transactional)
0436:                        tx.commit();
0437:
0438:                    /*
0439:                     * Query #6
0440:                     *
0441:                     * Another test of operator precedence/parenthesis handling.  Most
0442:                     * of the filter expressions include parentheses that are *not*
0443:                     * redundant.
0444:                     */
0445:
0446:                    if (transactional)
0447:                        tx.begin();
0448:
0449:                    expected = new HashSet();
0450:
0451:                    for (int i = 0; i < objs.length; ++i) {
0452:                        Widget w = (Widget) objs[i];
0453:
0454:                        if (w.getBooleanField() == w.getIntField() < 0
0455:                                && w.getBooleanField() == w.getShortField() < 0)
0456:                            expected.add(pm.getObjectById(ids[i], false));
0457:                    }
0458:
0459:                    filters = new String[] {
0460:                            "booleanField == intField < 0 && booleanField == shortField < 0",
0461:                            "booleanField == (intField < 0) && booleanField == (shortField < 0)",
0462:                            "booleanField == intField < 0 && intField < 0 == shortField < 0",
0463:                            "booleanField == (intField < 0) && (intField < 0) == (shortField < 0)",
0464:                            "(booleanField == true) == intField < 0 && intField < 0 == shortField < 0",
0465:                            "(booleanField == true) == (intField < 0) && (intField < 0) == (shortField < 0)",
0466:                            "intField < 0 == booleanField && shortField < 0 == booleanField",
0467:                            "(intField < 0) == booleanField && (shortField < 0) == booleanField",
0468:                            "intField < 0 == shortField < 0 && booleanField == intField < 0",
0469:                            "(intField < 0) == (shortField < 0) && booleanField == (intField < 0)",
0470:                            "intField < 0 == (booleanField == true) && shortField < 0 == (booleanField == true)",
0471:                            "(intField < 0) == (booleanField == true) && (shortField < 0) == (booleanField == true)" };
0472:
0473:                    query = pm.newQuery(pm.getExtent(Widget.class, true));
0474:
0475:                    for (int i = 0; i < filters.length; ++i) {
0476:                        query.setFilter(filters[i]);
0477:                        results = (Collection) query.execute();
0478:
0479:                        try {
0480:                            assertResultsEqual(expected, results);
0481:                        } finally {
0482:                            query.closeAll();
0483:                        }
0484:                    }
0485:
0486:                    if (transactional)
0487:                        tx.commit();
0488:
0489:                    /*
0490:                     * Query #7
0491:                     *
0492:                     * Test querying for values passed in as a parameter by a transient Set
0493:                     * of values.  
0494:                     */
0495:
0496:                    if (transactional)
0497:                        tx.begin();
0498:
0499:                    Random rand = new Random();
0500:                    String[] filterFields = new String[] { "booleanField",
0501:                            "booleanObjField", "byteField", "byteObjField",
0502:                            "charField", "charObjField", "shortField",
0503:                            "shortObjField", "intField", "intObjField",
0504:                            "longField", "longObjField", "this" };
0505:                    HashSet[] setParameters = new HashSet[] { new HashSet(),
0506:                            new HashSet(), new HashSet(), new HashSet(),
0507:                            new HashSet(), new HashSet(), new HashSet(),
0508:                            new HashSet(), new HashSet(), new HashSet(),
0509:                            new HashSet(), new HashSet(), new HashSet() };
0510:                    HashSet[] expectedResults = new HashSet[] { new HashSet(),
0511:                            new HashSet(), new HashSet(), new HashSet(),
0512:                            new HashSet(), new HashSet(), new HashSet(),
0513:                            new HashSet(), new HashSet(), new HashSet(),
0514:                            new HashSet(), new HashSet(), new HashSet() };
0515:
0516:                    for (int i = 0; i < objs.length; ++i) {
0517:                        if (rand.nextBoolean()) {
0518:                            Widget w = (Widget) objs[i];
0519:
0520:                            setParameters[0].add(new Boolean(w
0521:                                    .getBooleanField()));
0522:                            setParameters[1].add(w.getBooleanObjField());
0523:                            setParameters[2].add(new Byte(w.getByteField()));
0524:                            setParameters[3].add(w.getByteObjField());
0525:                            setParameters[4]
0526:                                    .add(new Character(w.getCharField()));
0527:                            setParameters[5].add(w.getCharObjField());
0528:                            setParameters[6].add(new Short(w.getShortField()));
0529:                            setParameters[7].add(w.getShortObjField());
0530:                            setParameters[8].add(new Integer(w.getIntField()));
0531:                            setParameters[9].add(w.getIntObjField());
0532:                            setParameters[10].add(new Long(w.getLongField()));
0533:                            setParameters[11].add(w.getLongObjField());
0534:                            setParameters[12].add(pm.getObjectById(ids[i],
0535:                                    false));
0536:                            expectedResults[12].add(pm.getObjectById(ids[i],
0537:                                    false));
0538:                        }
0539:                    }
0540:                    for (int i = 0; i < objs.length; i++) {
0541:                        Widget w = (Widget) objs[i];
0542:                        Object obj = pm.getObjectById(ids[i], false);
0543:
0544:                        if (setParameters[0].contains(new Boolean(w
0545:                                .getBooleanField())))
0546:                            expectedResults[0].add(obj);
0547:                        if (setParameters[1].contains(w.getBooleanObjField()))
0548:                            expectedResults[1].add(obj);
0549:                        if (setParameters[2]
0550:                                .contains(new Byte(w.getByteField())))
0551:                            expectedResults[2].add(obj);
0552:                        if (setParameters[3].contains(w.getByteObjField()))
0553:                            expectedResults[3].add(obj);
0554:                        if (setParameters[4].contains(new Character(w
0555:                                .getCharField())))
0556:                            expectedResults[4].add(obj);
0557:                        if (setParameters[5].contains(w.getCharObjField()))
0558:                            expectedResults[5].add(obj);
0559:                        if (setParameters[6].contains(new Short(w
0560:                                .getShortField())))
0561:                            expectedResults[6].add(obj);
0562:                        if (setParameters[7].contains(w.getShortObjField()))
0563:                            expectedResults[7].add(obj);
0564:                        if (setParameters[8].contains(new Integer(w
0565:                                .getIntField())))
0566:                            expectedResults[8].add(obj);
0567:                        if (setParameters[9].contains(w.getIntObjField()))
0568:                            expectedResults[9].add(obj);
0569:                        if (setParameters[10].contains(new Long(w
0570:                                .getLongField())))
0571:                            expectedResults[10].add(obj);
0572:                        if (setParameters[11].contains(w.getLongObjField()))
0573:                            expectedResults[11].add(obj);
0574:                    }
0575:
0576:                    query = pm.newQuery(pm.getExtent(Widget.class, true));
0577:
0578:                    for (int i = 0; i < filterFields.length; ++i) {
0579:                        if (LOG.isDebugEnabled()) {
0580:                            LOG.debug("Filter field = " + filterFields[i]);
0581:                            LOG.debug("Set parameter = " + setParameters[i]);
0582:                        }
0583:
0584:                        query.declareParameters("java.util.Set setParameter");
0585:                        query.setFilter("setParameter.contains("
0586:                                + filterFields[i] + ")");
0587:                        results = (Collection) query.execute(setParameters[i]);
0588:
0589:                        try {
0590:                            assertResultsEqual(expectedResults[i], results);
0591:                        } finally {
0592:                            query.closeAll();
0593:                        }
0594:                    }
0595:
0596:                    if (transactional)
0597:                        tx.commit();
0598:                } finally {
0599:                    if (tx.isActive())
0600:                        tx.rollback();
0601:
0602:                    pm.close();
0603:                }
0604:            }
0605:
0606:            private void queryStringWidgetObjects(boolean transactional)
0607:                    throws Exception {
0608:                /*
0609:                 * Perform various queries and validate the resulting collection.
0610:                 */
0611:
0612:                LOG
0613:                        .info("Querying " + TEST_OBJECT_COUNT + " "
0614:                                + StringWidget.class.getName() + " objects "
0615:                                + (transactional ? "in" : "outside")
0616:                                + " a transaction");
0617:                PersistenceManager pm = pmf.getPersistenceManager();
0618:                Transaction tx = pm.currentTransaction();
0619:
0620:                try {
0621:                    if (!transactional)
0622:                        tx.setNontransactionalRead(true);
0623:
0624:                    Query query;
0625:                    Collection results;
0626:
0627:                    /*
0628:                     * Query #1
0629:                     */
0630:
0631:                    if (transactional)
0632:                        tx.begin();
0633:
0634:                    HashSet expected = new HashSet();
0635:
0636:                    for (int i = 0; i < objs.length; ++i) {
0637:                        StringWidget sw = (StringWidget) pm.getObjectById(
0638:                                ids[i], false);
0639:
0640:                        if (sw.getIntField() < 0)
0641:                            expected.add(sw);
0642:                    }
0643:
0644:                    query = pm.newQuery(pm.getExtent(StringWidget.class, true));
0645:                    query.setFilter("intField < 0");
0646:                    query.setOrdering("normalString descending");
0647:                    results = (Collection) query.execute();
0648:
0649:                    try {
0650:                        assertResultsEqual(expected, results);
0651:                    } finally {
0652:                        query.closeAll();
0653:                    }
0654:
0655:                    if (transactional)
0656:                        tx.commit();
0657:
0658:                    /*
0659:                     * Query #2
0660:                     *
0661:                     * Tests the startsWith() method.
0662:                     */
0663:
0664:                    tx.begin();
0665:
0666:                    expected = new HashSet();
0667:
0668:                    for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i) {
0669:                        StringWidget tsw = (StringWidget) objs[i * 5];
0670:                        StringWidget psw = (StringWidget) pm.getObjectById(
0671:                                ids[i * 5], false);
0672:                        String newNormalString = "at the start " + i;
0673:
0674:                        tsw.setNormalString(newNormalString);
0675:                        psw.setNormalString(newNormalString);
0676:
0677:                        expected.add(psw);
0678:                    }
0679:
0680:                    tx.commit();
0681:
0682:                    if (transactional)
0683:                        tx.begin();
0684:
0685:                    query = pm.newQuery(pm.getExtent(StringWidget.class, true));
0686:                    query
0687:                            .setFilter("normalString.startsWith(\"at the start\")");
0688:                    results = (Collection) query.execute();
0689:
0690:                    try {
0691:                        assertResultsEqual(expected, results);
0692:                    } finally {
0693:                        query.closeAll();
0694:                    }
0695:
0696:                    query
0697:                            .setFilter("normalString.substring(6,12) == \" start\"");
0698:                    results = (Collection) query.execute();
0699:
0700:                    try {
0701:                        assertResultsEqual(expected, results);
0702:                    } finally {
0703:                        query.closeAll();
0704:                    }
0705:
0706:                    query.declareParameters("java.lang.String s");
0707:                    query.setFilter("normalString.startsWith(s)");
0708:                    results = (Collection) query.execute("at the start");
0709:
0710:                    try {
0711:                        assertResultsEqual(expected, results);
0712:                    } finally {
0713:                        query.closeAll();
0714:                    }
0715:
0716:                    if (transactional)
0717:                        tx.commit();
0718:
0719:                    /*
0720:                     * Query #3
0721:                     *
0722:                     * Tests the endsWith(), toLowerCase() and toUpperCase() methods.
0723:                     */
0724:
0725:                    tx.begin();
0726:
0727:                    expected = new HashSet();
0728:
0729:                    for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i) {
0730:                        StringWidget tsw = (StringWidget) objs[i * 5 + 1];
0731:                        StringWidget psw = (StringWidget) pm.getObjectById(
0732:                                ids[i * 5 + 1], false);
0733:                        String newNormalString = "" + i
0734:                                + (i % 2 == 0 ? " at the end" : " At The End");
0735:
0736:                        tsw.setNormalString(newNormalString);
0737:                        psw.setNormalString(newNormalString);
0738:
0739:                        expected.add(psw);
0740:                    }
0741:
0742:                    tx.commit();
0743:
0744:                    if (transactional)
0745:                        tx.begin();
0746:
0747:                    query = pm.newQuery(pm.getExtent(StringWidget.class, true));
0748:                    query
0749:                            .setFilter("normalString.toLowerCase().endsWith(\"at the end\")");
0750:                    results = (Collection) query.execute();
0751:
0752:                    try {
0753:                        assertResultsEqual(expected, results);
0754:                    } finally {
0755:                        query.closeAll();
0756:                    }
0757:
0758:                    query.declareParameters("java.lang.String s");
0759:                    query.setFilter("normalString.toUpperCase().endsWith(s)");
0760:                    results = (Collection) query.execute("AT THE END");
0761:
0762:                    try {
0763:                        assertResultsEqual(expected, results);
0764:                    } finally {
0765:                        query.closeAll();
0766:                    }
0767:
0768:                    if (transactional)
0769:                        tx.commit();
0770:
0771:                    /*
0772:                     * Query #4
0773:                     *
0774:                     * Tests the indexof() method.
0775:                     */
0776:
0777:                    tx.begin();
0778:
0779:                    expected = new HashSet();
0780:
0781:                    for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i) {
0782:                        StringWidget tsw = (StringWidget) objs[i * 5 + 1];
0783:                        StringWidget psw = (StringWidget) pm.getObjectById(
0784:                                ids[i * 5 + 1], false);
0785:                        String newNormalString = "" + i + " in the middle " + i;
0786:
0787:                        tsw.setNormalString(newNormalString);
0788:                        psw.setNormalString(newNormalString);
0789:
0790:                        expected.add(psw);
0791:                    }
0792:
0793:                    tx.commit();
0794:
0795:                    if (transactional)
0796:                        tx.begin();
0797:
0798:                    query = pm.newQuery(pm.getExtent(StringWidget.class, true));
0799:                    query
0800:                            .setFilter("normalString.indexOf(\"in the middle\") >= 0");
0801:                    results = (Collection) query.execute();
0802:
0803:                    try {
0804:                        assertResultsEqual(expected, results);
0805:                    } finally {
0806:                        query.closeAll();
0807:                    }
0808:
0809:                    query.declareParameters("java.lang.String s");
0810:                    query.setFilter("normalString.indexOf(s) >= 0");
0811:                    results = (Collection) query.execute("in the middle");
0812:
0813:                    try {
0814:                        assertResultsEqual(expected, results);
0815:                    } finally {
0816:                        query.closeAll();
0817:                    }
0818:
0819:                    if (transactional)
0820:                        tx.commit();
0821:
0822:                    /*
0823:                     * Query #5
0824:                     *
0825:                     * Tests the length() method.
0826:                     */
0827:
0828:                    if (transactional)
0829:                        tx.begin();
0830:
0831:                    expected = new HashSet();
0832:
0833:                    for (int i = 0; i < objs.length; ++i) {
0834:                        StringWidget sw = (StringWidget) objs[i];
0835:                        String s = sw.getNormalString();
0836:
0837:                        if (s != null && s.length() > 10)
0838:                            expected.add(pm.getObjectById(ids[i], false));
0839:                    }
0840:
0841:                    query = pm.newQuery(pm.getExtent(StringWidget.class, true));
0842:                    query.setFilter("normalString.length() > 10");
0843:                    results = (Collection) query.execute();
0844:
0845:                    try {
0846:                        assertResultsEqual(expected, results);
0847:                    } finally {
0848:                        query.closeAll();
0849:                    }
0850:
0851:                    if (transactional)
0852:                        tx.commit();
0853:
0854:                    /*
0855:                     * Query #6
0856:                     *
0857:                     * Test querying for values passed in as a parameter by a transient Set
0858:                     * of Strings. 
0859:                     */
0860:
0861:                    if (transactional)
0862:                        tx.begin();
0863:
0864:                    Random rand = new Random();
0865:                    HashSet setParameters = new HashSet();
0866:                    expected = new HashSet();
0867:
0868:                    for (int i = 0; i < objs.length; ++i) {
0869:                        if (rand.nextBoolean()) {
0870:                            StringWidget w = (StringWidget) objs[i];
0871:                            setParameters.add(w.getNormalString());
0872:                        }
0873:                    }
0874:                    for (int i = 0; i < objs.length; i++) {
0875:                        StringWidget w = (StringWidget) objs[i];
0876:
0877:                        if (setParameters.contains(w.getNormalString()))
0878:                            expected.add(pm.getObjectById(ids[i], false));
0879:                    }
0880:
0881:                    query = pm.newQuery();
0882:                    query.setClass(StringWidget.class);
0883:                    query.declareParameters("java.util.Set setParameter");
0884:                    query.setFilter("setParameter.contains(normalString)");
0885:                    results = (Collection) query.execute(setParameters);
0886:
0887:                    try {
0888:                        assertResultsEqual(expected, results);
0889:                    } finally {
0890:                        query.closeAll();
0891:                    }
0892:
0893:                    if (transactional)
0894:                        tx.commit();
0895:                } finally {
0896:                    if (tx.isActive())
0897:                        tx.rollback();
0898:
0899:                    pm.close();
0900:                }
0901:            }
0902:
0903:            private void queryBinaryWidgetObjects(boolean transactional)
0904:                    throws Exception {
0905:                /*
0906:                 * Can't run this test on Firebird or Oracle because comparison of BLOB
0907:                 * fields isn't supported.
0908:                 * Can't run it on SAP DB because comparison of LONG fields isn't
0909:                 * supported, and the normalBinary currently has to map to type LONG.
0910:                 */
0911:                if ("firebird".equals(vendorID) || "oracle".equals(vendorID)
0912:                        || "sapdb".equals(vendorID))
0913:                    return;
0914:
0915:                /*
0916:                 * Perform various queries and validate the resulting collection.
0917:                 */
0918:
0919:                LOG
0920:                        .info("Querying " + TEST_OBJECT_COUNT + " "
0921:                                + BinaryWidget.class.getName() + " objects "
0922:                                + (transactional ? "in" : "outside")
0923:                                + " a transaction");
0924:                PersistenceManager pm = pmf.getPersistenceManager();
0925:                Transaction tx = pm.currentTransaction();
0926:
0927:                try {
0928:                    if (!transactional)
0929:                        tx.setNontransactionalRead(true);
0930:
0931:                    Query query;
0932:                    Collection results;
0933:
0934:                    /*
0935:                     * Query #1
0936:                     *
0937:                     * Tests comparison with binary field.
0938:                     */
0939:
0940:                    if (transactional)
0941:                        tx.begin();
0942:
0943:                    HashSet expected = new HashSet();
0944:
0945:                    for (int i = 0; i < objs.length; ++i) {
0946:                        BinaryWidget bw = (BinaryWidget) pm.getObjectById(
0947:                                ids[i], false);
0948:                        byte[] ba = bw.getFixedLengthBinary();
0949:
0950:                        if (ba != null && (ba[0] & 0xff) >= 0x7f)
0951:                            expected.add(bw);
0952:                    }
0953:
0954:                    query = pm.newQuery(BinaryWidget.class);
0955:                    query.declareParameters("byte[] target");
0956:                    query.setFilter("fixedLengthBinary >= target");
0957:                    results = (Collection) query.execute(new byte[] { 0x7f });
0958:
0959:                    try {
0960:                        assertResultsEqual(expected, results);
0961:                    } finally {
0962:                        query.closeAll();
0963:                    }
0964:
0965:                    if (transactional)
0966:                        tx.commit();
0967:
0968:                    /*
0969:                     * Query #2
0970:                     *
0971:                     * Tests equality with binary field.
0972:                     */
0973:
0974:                    tx.begin();
0975:
0976:                    byte[] expectedBytes = new byte[] { -20, -10, 0, 10, 20 };
0977:                    expected = new HashSet();
0978:
0979:                    for (int i = 0; i < TEST_OBJECT_COUNT / 5; ++i) {
0980:                        BinaryWidget tsw = (BinaryWidget) objs[i * 5 + 1];
0981:                        BinaryWidget psw = (BinaryWidget) pm.getObjectById(
0982:                                ids[i * 5 + 1], false);
0983:
0984:                        tsw.setNormalBinary(expectedBytes);
0985:                        psw.setNormalBinary(expectedBytes);
0986:
0987:                        expected.add(psw);
0988:                    }
0989:
0990:                    tx.commit();
0991:
0992:                    if (transactional)
0993:                        tx.begin();
0994:
0995:                    query = pm.newQuery(BinaryWidget.class);
0996:                    query.declareParameters("byte[] target");
0997:                    query.setFilter("normalBinary == target");
0998:                    results = (Collection) query.execute(expectedBytes);
0999:
1000:                    try {
1001:                        assertResultsEqual(expected, results);
1002:                    } finally {
1003:                        query.closeAll();
1004:                    }
1005:
1006:                    if (transactional)
1007:                        tx.commit();
1008:                } finally {
1009:                    if (tx.isActive())
1010:                        tx.rollback();
1011:
1012:                    pm.close();
1013:                }
1014:            }
1015:
1016:            private void queryDateWidgetObjects(boolean transactional)
1017:                    throws Exception {
1018:                /*
1019:                 * Perform various queries and validate the resulting collection.
1020:                 */
1021:
1022:                LOG
1023:                        .info("Querying " + TEST_OBJECT_COUNT + " "
1024:                                + DateWidget.class.getName() + " objects "
1025:                                + (transactional ? "in" : "outside")
1026:                                + " a transaction");
1027:                PersistenceManager pm = pmf.getPersistenceManager();
1028:                Transaction tx = pm.currentTransaction();
1029:
1030:                try {
1031:                    if (!transactional)
1032:                        tx.setNontransactionalRead(true);
1033:
1034:                    Query query;
1035:                    Collection results;
1036:
1037:                    /*
1038:                     * Query #1
1039:                     *
1040:                     * Tests a simple query with a java.sql.Date field.
1041:                     */
1042:
1043:                    if (transactional)
1044:                        tx.begin();
1045:
1046:                    java.sql.Date epoch = new java.sql.Date(0L);
1047:                    HashSet expected = new HashSet();
1048:
1049:                    for (int i = 0; i < objs.length; ++i) {
1050:                        DateWidget dw = (DateWidget) pm.getObjectById(ids[i],
1051:                                false);
1052:                        java.sql.Date d = dw.getSQLDateField();
1053:
1054:                        if (d != null && d.before(epoch))
1055:                            expected.add(dw);
1056:                    }
1057:
1058:                    query = pm.newQuery(pm.getExtent(DateWidget.class, true));
1059:                    query.declareParameters("java.sql.Date epoch");
1060:                    query.setFilter("sqlDateField < epoch");
1061:                    results = (Collection) query.execute(epoch);
1062:
1063:                    try {
1064:                        assertResultsEqual(expected, results);
1065:                    } finally {
1066:                        query.closeAll();
1067:                    }
1068:
1069:                    if (transactional)
1070:                        tx.commit();
1071:
1072:                    /*
1073:                     * Query #2
1074:                     *
1075:                     * Tests a simple query with a java.sql.Timestamp field.
1076:                     */
1077:
1078:                    if (transactional)
1079:                        tx.begin();
1080:
1081:                    java.sql.Timestamp epochTS = new java.sql.Timestamp(0L);
1082:                    expected = new HashSet();
1083:
1084:                    for (int i = 0; i < objs.length; ++i) {
1085:                        DateWidget dw = (DateWidget) pm.getObjectById(ids[i],
1086:                                false);
1087:                        java.sql.Timestamp ts = dw.getSQLTimestampField();
1088:
1089:                        if (ts != null && ts.after(epochTS))
1090:                            expected.add(dw);
1091:                    }
1092:
1093:                    query = pm.newQuery(pm.getExtent(DateWidget.class, true));
1094:                    query.declareParameters("java.sql.Timestamp epoch");
1095:                    query.setFilter("sqlTimestampField > epoch");
1096:                    results = (Collection) query.execute(epochTS);
1097:
1098:                    try {
1099:                        assertResultsEqual(expected, results);
1100:                    } finally {
1101:                        query.closeAll();
1102:                    }
1103:
1104:                    if (transactional)
1105:                        tx.commit();
1106:
1107:                    /*
1108:                     * Query #6
1109:                     *
1110:                     * Test querying for values passed in as a parameter by a transient Set
1111:                     * of Dates. 
1112:                     */
1113:
1114:                    if (transactional)
1115:                        tx.begin();
1116:
1117:                    Random rand = new Random();
1118:                    String[] filterFields = new String[] { "dateField",
1119:                            "sqlDateField", "sqlTimestampField" };
1120:                    HashSet[] setParameters = new HashSet[] { new HashSet(),
1121:                            new HashSet(), new HashSet() };
1122:                    HashSet[] expectedResults = new HashSet[] { new HashSet(),
1123:                            new HashSet(), new HashSet() };
1124:
1125:                    for (int i = 0; i < objs.length; ++i) {
1126:                        if (rand.nextBoolean()) {
1127:                            DateWidget w = (DateWidget) objs[i];
1128:
1129:                            setParameters[0].add(w.getDateField());
1130:                            setParameters[1].add(w.getSQLDateField());
1131:                            setParameters[2].add(w.getSQLTimestampField());
1132:                        }
1133:                    }
1134:                    for (int i = 0; i < objs.length; i++) {
1135:                        DateWidget w = (DateWidget) objs[i];
1136:                        Object obj = pm.getObjectById(ids[i], false);
1137:
1138:                        if (setParameters[0].contains(w.getDateField()))
1139:                            expectedResults[0].add(obj);
1140:                        if (setParameters[1].contains(w.getSQLDateField()))
1141:                            expectedResults[1].add(obj);
1142:                        if (setParameters[2].contains(w.getSQLTimestampField()))
1143:                            expectedResults[2].add(obj);
1144:                    }
1145:
1146:                    query = pm.newQuery(DateWidget.class);
1147:
1148:                    for (int i = 0; i < filterFields.length; ++i) {
1149:                        query.declareParameters("java.util.Set setParameter");
1150:                        query.setFilter("setParameter.contains("
1151:                                + filterFields[i] + ")");
1152:                        results = (Collection) query.execute(setParameters[i]);
1153:
1154:                        try {
1155:                            assertResultsEqual(expectedResults[i], results);
1156:                        } finally {
1157:                            query.closeAll();
1158:                        }
1159:                    }
1160:
1161:                    if (transactional)
1162:                        tx.commit();
1163:                } finally {
1164:                    if (tx.isActive())
1165:                        tx.rollback();
1166:
1167:                    pm.close();
1168:                }
1169:            }
1170:
1171:            private void queryFloatWidgetObjects(boolean transactional)
1172:                    throws Exception {
1173:                /*
1174:                 * Perform various queries and validate the resulting collection.
1175:                 */
1176:
1177:                LOG
1178:                        .info("Querying " + TEST_OBJECT_COUNT + " "
1179:                                + FloatWidget.class.getName() + " objects "
1180:                                + (transactional ? "in" : "outside")
1181:                                + " a transaction");
1182:                PersistenceManager pm = pmf.getPersistenceManager();
1183:                Transaction tx = pm.currentTransaction();
1184:
1185:                try {
1186:                    if (!transactional)
1187:                        tx.setNontransactionalRead(true);
1188:
1189:                    Query query;
1190:                    Collection results;
1191:
1192:                    /*
1193:                     * Query #1
1194:                     *
1195:                     * Tests a simple query with a float field.
1196:                     */
1197:
1198:                    if (transactional)
1199:                        tx.begin();
1200:
1201:                    HashSet expected = new HashSet();
1202:
1203:                    for (int i = 0; i < objs.length; ++i) {
1204:                        FloatWidget fw = (FloatWidget) objs[i];
1205:
1206:                        if (fw.getFloatField() < 0
1207:                                || (fw.getFloatObjField() != null && fw
1208:                                        .getFloatObjField().floatValue() > 0))
1209:                            expected.add(pm.getObjectById(ids[i], false));
1210:                    }
1211:
1212:                    query = pm.newQuery(pm.getExtent(FloatWidget.class, true));
1213:                    query.setFilter("floatField < 0f || floatObjField > 0.0F");
1214:                    results = (Collection) query.execute();
1215:
1216:                    try {
1217:                        assertResultsEqual(expected, results);
1218:                    } finally {
1219:                        query.closeAll();
1220:                    }
1221:
1222:                    if (transactional)
1223:                        tx.commit();
1224:
1225:                    /*
1226:                     * Query #2
1227:                     *
1228:                     * Tests a simple query with a double field.
1229:                     */
1230:
1231:                    if (transactional)
1232:                        tx.begin();
1233:
1234:                    expected = new HashSet();
1235:
1236:                    /*
1237:                     * In order for the following query to (hopefully) match some, not
1238:                     * all and not none, of the candidate objects, min and max should
1239:                     * be chosen such that the exponent value is about mid-range between
1240:                     * the boundaries set by FloatWidget.MIN_DOUBLE_VALUE/MAX_DOUBLE_VALUE.
1241:                     */
1242:                    double min = -1e31;
1243:                    double max = 1e31;
1244:
1245:                    for (int i = 0; i < objs.length; ++i) {
1246:                        FloatWidget fw = (FloatWidget) objs[i];
1247:
1248:                        if (fw.getDoubleField() >= min
1249:                                || (fw.getDoubleObjField() != null && fw
1250:                                        .getDoubleObjField().doubleValue() <= max))
1251:                            expected.add(pm.getObjectById(ids[i], false));
1252:                    }
1253:
1254:                    query = pm.newQuery(pm.getExtent(FloatWidget.class, true));
1255:                    query.declareParameters("double min, double max");
1256:                    query
1257:                            .setFilter("doubleField >= min || doubleObjField <= max");
1258:                    results = (Collection) query.execute(new Double(min),
1259:                            new Double(max));
1260:
1261:                    try {
1262:                        assertResultsEqual(expected, results);
1263:                    } finally {
1264:                        query.closeAll();
1265:                    }
1266:
1267:                    if (transactional)
1268:                        tx.commit();
1269:                } finally {
1270:                    if (tx.isActive())
1271:                        tx.rollback();
1272:
1273:                    pm.close();
1274:                }
1275:            }
1276:
1277:            private void querySetWidgetObjects(Class setClass,
1278:                    boolean transactional) throws Exception {
1279:                /*
1280:                 * Perform various queries and validate the resulting collection.
1281:                 */
1282:
1283:                LOG
1284:                        .info("Querying " + TEST_OBJECT_COUNT + " "
1285:                                + setClass.getName() + " objects "
1286:                                + (transactional ? "in" : "outside")
1287:                                + " a transaction");
1288:                PersistenceManager pm = pmf.getPersistenceManager();
1289:                Transaction tx = pm.currentTransaction();
1290:
1291:                try {
1292:                    if (!transactional)
1293:                        tx.setNontransactionalRead(true);
1294:
1295:                    Extent swExtent = pm.getExtent(setClass, true);
1296:
1297:                    setWidgetQuery1(transactional, pm, tx, swExtent,
1298:                            NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1299:                    setWidgetQuery1(transactional, pm, tx, swExtent,
1300:                            NORMAL_OBJECT_SET_FIELD,
1301:                            GET_NORMAL_OBJECT_SET_METHOD);
1302:                    setWidgetQuery2(transactional, pm, tx, swExtent);
1303:
1304:                    try {
1305:                        setWidgetQuery3(transactional, pm, tx, swExtent,
1306:                                NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1307:                        setWidgetQuery3(transactional, pm, tx, swExtent,
1308:                                NORMAL_OBJECT_SET_FIELD,
1309:                                GET_NORMAL_OBJECT_SET_METHOD);
1310:                        setWidgetQuery4(transactional, pm, tx, swExtent);
1311:                        setWidgetQuery5(transactional, pm, tx, swExtent,
1312:                                NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1313:                        setWidgetQuery5(transactional, pm, tx, swExtent,
1314:                                NORMAL_OBJECT_SET_FIELD,
1315:                                GET_NORMAL_OBJECT_SET_METHOD);
1316:                        setWidgetQuery7(transactional, pm, tx, swExtent,
1317:                                NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1318:                        setWidgetQuery7(transactional, pm, tx, swExtent,
1319:                                NORMAL_OBJECT_SET_FIELD,
1320:                                GET_NORMAL_OBJECT_SET_METHOD);
1321:                        setWidgetQuery8(transactional, pm, tx, swExtent);
1322:                        setWidgetQuery9(transactional, pm, tx, swExtent,
1323:                                NORMAL_SET_FIELD, GET_NORMAL_SET_METHOD);
1324:                        setWidgetQuery9(transactional, pm, tx, swExtent,
1325:                                NORMAL_OBJECT_SET_FIELD,
1326:                                GET_NORMAL_OBJECT_SET_METHOD);
1327:                        setWidgetQuery10(transactional, pm, tx, swExtent);
1328:                    } catch (JDOException e) {
1329:                        if ("mysql".equals(vendorID)) {
1330:                            if (tx.isActive())
1331:                                tx.rollback();
1332:
1333:                            LOG
1334:                                    .warn("A query test involving the isEmpty() or contains() method failed, probably because MySQL doesn't (yet) support subqueries");
1335:                        } else
1336:                            throw e;
1337:                    }
1338:
1339:                    setWidgetQuery6(transactional, pm, tx, swExtent);
1340:                } finally {
1341:                    if (tx.isActive())
1342:                        tx.rollback();
1343:
1344:                    pm.close();
1345:                }
1346:            }
1347:
1348:            /**
1349:             * Retrieve the set from the HasNormalSetField object using the given method.
1350:             *
1351:             * @param sw            The HasNormalSetField object to get the Set from.
1352:             * @param getSetMethod  The Method to use to get the set field.
1353:             *
1354:             * @return The set from the HasNormalSetField object using the given method.
1355:             */
1356:            private static Set getSet(HasNormalSetField sw, Method getSetMethod)
1357:                    throws Exception {
1358:                return (Set) getSetMethod.invoke(sw, null);
1359:            }
1360:
1361:            private void setWidgetQuery1(boolean transactional,
1362:                    PersistenceManager pm, Transaction tx, Extent swExtent,
1363:                    String setFieldName, Method getSetMethod) throws Exception {
1364:                Query query;
1365:                Collection results;
1366:
1367:                /*
1368:                 * Query #1
1369:                 *
1370:                 * Tests a typical usage of contains() on a normal set.
1371:                 */
1372:
1373:                if (transactional)
1374:                    tx.begin();
1375:
1376:                for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
1377:                    query = pm.newQuery(swExtent);
1378:
1379:                    Class elementClass = (queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0 ? StringWidget.class
1380:                            : Widget.class;
1381:
1382:                    query.declareVariables(elementClass.getName() + " w");
1383:                    query.setFilter(setFieldName
1384:                            + ".contains(w) && w.intField < 0");
1385:
1386:                    if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1387:                        query.setOrdering("numWidgets ascending");
1388:
1389:                    results = (Collection) query.execute();
1390:
1391:                    try {
1392:                        HashSet expected = new HashSet();
1393:
1394:                        for (int i = 0; i < objs.length; ++i) {
1395:                            HasNormalSetField sw = (HasNormalSetField) objs[i];
1396:
1397:                            Iterator j = getSet(sw, getSetMethod).iterator();
1398:                            boolean found = false;
1399:
1400:                            while (j.hasNext()) {
1401:                                Widget w = (Widget) j.next();
1402:
1403:                                if (elementClass.isInstance(w)
1404:                                        && w.getIntField() < 0) {
1405:                                    found = true;
1406:                                    break;
1407:                                }
1408:                            }
1409:
1410:                            if (found)
1411:                                expected.add(pm.getObjectById(ids[i], false));
1412:                        }
1413:
1414:                        assertResultsEqual(expected, results);
1415:                    } finally {
1416:                        query.closeAll();
1417:                    }
1418:                }
1419:
1420:                if (transactional)
1421:                    tx.commit();
1422:            }
1423:
1424:            private void setWidgetQuery2(boolean transactional,
1425:                    PersistenceManager pm, Transaction tx, Extent swExtent)
1426:                    throws Exception {
1427:                if (!HasInverseSetField.class.isAssignableFrom(swExtent
1428:                        .getCandidateClass()))
1429:                    return;
1430:
1431:                Query query;
1432:                Collection results;
1433:
1434:                /*
1435:                 * Query #2
1436:                 *
1437:                 * Tests a typical usage of contains() on an inverse set.
1438:                 */
1439:
1440:                if (transactional)
1441:                    tx.begin();
1442:
1443:                for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
1444:                    if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1445:                        continue;
1446:
1447:                    query = pm.newQuery(swExtent);
1448:                    query
1449:                            .declareVariables("com.triactive.jdo.test.ElementWidget ew");
1450:                    query
1451:                            .setFilter("inverseSet.contains(ew) && numElementWidgets >= 0 && ew.intField < 0");
1452:
1453:                    if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1454:                        query.setOrdering("numElementWidgets ascending");
1455:
1456:                    results = (Collection) query.execute();
1457:
1458:                    try {
1459:                        HashSet expected = new HashSet();
1460:
1461:                        for (int i = 0; i < objs.length; ++i) {
1462:                            HasInverseSetField sw = (HasInverseSetField) objs[i];
1463:
1464:                            Iterator j = sw.getInverseSet().iterator();
1465:                            boolean found = false;
1466:
1467:                            while (j.hasNext()) {
1468:                                ElementWidget ew = (ElementWidget) j.next();
1469:
1470:                                if (ew.getIntField() < 0) {
1471:                                    found = true;
1472:                                    break;
1473:                                }
1474:                            }
1475:
1476:                            if (found)
1477:                                expected.add(pm.getObjectById(ids[i], false));
1478:                        }
1479:
1480:                        assertResultsEqual(expected, results);
1481:                    } finally {
1482:                        query.closeAll();
1483:                    }
1484:                }
1485:
1486:                if (transactional)
1487:                    tx.commit();
1488:            }
1489:
1490:            private void setWidgetQuery3(boolean transactional,
1491:                    PersistenceManager pm, Transaction tx, Extent swExtent,
1492:                    String setFieldName, Method getSetMethod) throws Exception {
1493:                Query query;
1494:                Collection results;
1495:
1496:                /*
1497:                 * Query #3
1498:                 *
1499:                 * Tests a simple usage of isEmpty() on a normal set.
1500:                 */
1501:
1502:                if (transactional)
1503:                    tx.begin();
1504:
1505:                for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
1506:                    if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1507:                        continue;
1508:
1509:                    query = pm.newQuery(swExtent);
1510:                    query.setFilter(setFieldName + ".isEmpty()");
1511:
1512:                    if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1513:                        query.setOrdering("numWidgets ascending");
1514:
1515:                    results = (Collection) query.execute();
1516:
1517:                    try {
1518:                        HashSet expected = new HashSet();
1519:
1520:                        for (int i = 0; i < objs.length; ++i) {
1521:                            HasNormalSetField sw = (HasNormalSetField) objs[i];
1522:
1523:                            if (getSet(sw, getSetMethod).isEmpty())
1524:                                expected.add(pm.getObjectById(ids[i], false));
1525:                        }
1526:
1527:                        assertResultsEqual(expected, results);
1528:                    } finally {
1529:                        query.closeAll();
1530:                    }
1531:                }
1532:
1533:                if (transactional)
1534:                    tx.commit();
1535:            }
1536:
1537:            private void setWidgetQuery4(boolean transactional,
1538:                    PersistenceManager pm, Transaction tx, Extent swExtent)
1539:                    throws Exception {
1540:                if (!HasInverseSetField.class.isAssignableFrom(swExtent
1541:                        .getCandidateClass()))
1542:                    return;
1543:
1544:                Query query;
1545:                Collection results;
1546:
1547:                /*
1548:                 * Query #4
1549:                 *
1550:                 * Tests a simple usage of isEmpty() on an inverse set.
1551:                 */
1552:
1553:                if (transactional)
1554:                    tx.begin();
1555:
1556:                for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
1557:                    if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1558:                        continue;
1559:
1560:                    query = pm.newQuery(swExtent);
1561:                    query.setFilter("inverseSet.isEmpty() == true");
1562:
1563:                    if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1564:                        query.setOrdering("numElementWidgets ascending");
1565:
1566:                    results = (Collection) query.execute();
1567:
1568:                    try {
1569:                        HashSet expected = new HashSet();
1570:
1571:                        for (int i = 0; i < objs.length; ++i) {
1572:                            HasInverseSetField sw = (HasInverseSetField) objs[i];
1573:
1574:                            if (sw.getInverseSet().isEmpty())
1575:                                expected.add(pm.getObjectById(ids[i], false));
1576:                        }
1577:
1578:                        assertResultsEqual(expected, results);
1579:                    } finally {
1580:                        query.closeAll();
1581:                    }
1582:                }
1583:
1584:                if (transactional)
1585:                    tx.commit();
1586:            }
1587:
1588:            private void setWidgetQuery5(boolean transactional,
1589:                    PersistenceManager pm, Transaction tx, Extent swExtent,
1590:                    String setFieldName, Method getSetMethod) throws Exception {
1591:                Query query;
1592:                Collection results;
1593:
1594:                /*
1595:                 * Query #5
1596:                 *
1597:                 * Tests the use of a variable where we're *not* navigating into
1598:                 * any fields within the variable.
1599:                 */
1600:
1601:                if (transactional)
1602:                    tx.begin();
1603:
1604:                for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
1605:                    if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
1606:                        continue;
1607:
1608:                    HashSet expected = new HashSet();
1609:                    Widget target;
1610:
1611:                    /*
1612:                     * Find a ???SetWidget with a non-empty normalSet field.  Get a
1613:                     * Widget out of that set to serve as the target for the next
1614:                     * query.
1615:                     */
1616:                    query = pm.newQuery(swExtent);
1617:                    query.setFilter("!" + setFieldName + ".isEmpty()");
1618:
1619:                    results = (Collection) query.execute();
1620:
1621:                    try {
1622:                        HasNormalSetField sw = (HasNormalSetField) results
1623:                                .iterator().next();
1624:
1625:                        target = (Widget) getSet(sw, getSetMethod).iterator()
1626:                                .next();
1627:                        expected.add(sw);
1628:                    } finally {
1629:                        query.closeAll();
1630:                    }
1631:
1632:                    /*
1633:                     * Query for the ???SetWidget whose normalSet contains the target
1634:                     * Widget.
1635:                     */
1636:                    query = pm.newQuery(swExtent);
1637:                    query
1638:                            .declareImports("import com.triactive.jdo.test.Widget;");
1639:                    query.declareParameters("Widget target");
1640:                    query.declareVariables("Widget w");
1641:                    query.setFilter(setFieldName
1642:                            + ".contains(w) && w == target");
1643:
1644:                    if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
1645:                        query.setOrdering("numWidgets ascending");
1646:
1647:                    results = (Collection) query.execute(target);
1648:
1649:                    try {
1650:                        assertResultsEqual(expected, results);
1651:                    } finally {
1652:                        query.closeAll();
1653:                    }
1654:                }
1655:
1656:                if (transactional)
1657:                    tx.commit();
1658:            }
1659:
1660:            private void setWidgetQuery6(boolean transactional,
1661:                    PersistenceManager pm, Transaction tx, Extent swExtent)
1662:                    throws Exception {
1663:                Query query;
1664:                Collection results;
1665:
1666:                /*
1667:                 * Query #6
1668:                 *
1669:                 * Tests a query that filters an extent by candidate class, in
1670:                 * addition to an explicit filter.
1671:                 */
1672:
1673:                if (transactional)
1674:                    tx.begin();
1675:
1676:                query = pm.newQuery(pm.getExtent(Widget.class, true));
1677:                query.setClass(StringWidget.class);
1678:                query.setFilter("intField < 0");
1679:
1680:                results = (Collection) query.execute();
1681:
1682:                try {
1683:                    Query expectedQuery = pm.newQuery(pm.getExtent(
1684:                            StringWidget.class, false));
1685:                    expectedQuery.setFilter("intField < 0");
1686:
1687:                    Collection expectedResults = (Collection) expectedQuery
1688:                            .execute();
1689:
1690:                    try {
1691:                        HashSet expected = new HashSet(expectedResults);
1692:
1693:                        assertResultsEqual(expected, results);
1694:                    } finally {
1695:                        expectedQuery.closeAll();
1696:                    }
1697:                } finally {
1698:                    query.closeAll();
1699:                }
1700:
1701:                if (transactional)
1702:                    tx.commit();
1703:            }
1704:
1705:            private void setWidgetQuery7(boolean transactional,
1706:                    PersistenceManager pm, Transaction tx, Extent swExtent,
1707:                    String setFieldName, Method getSetMethod) throws Exception {
1708:                Query query;
1709:                Collection results;
1710:                HashSet expected;
1711:
1712:                /*
1713:                 * Query #7
1714:                 *
1715:                 * Test querying for containment in a persistent normal Set parameter.
1716:                 */
1717:
1718:                boolean hasRun = false;
1719:                for (int i = 0; i < objs.length && !hasRun; ++i) {
1720:                    if (transactional)
1721:                        tx.begin();
1722:
1723:                    expected = new HashSet();
1724:                    HasNormalSetField sw = (HasNormalSetField) pm
1725:                            .getObjectById(ids[i], false);
1726:
1727:                    final Set set = getSet(sw, getSetMethod);
1728:                    if (!set.isEmpty()) {
1729:                        expected.addAll(set);
1730:
1731:                        query = pm.newQuery(pm.getExtent(Widget.class, true));
1732:                        query.declareParameters("java.util.Set setParameter");
1733:                        query.setFilter("setParameter.contains(this)");
1734:                        results = (Collection) query.execute(set);
1735:
1736:                        try {
1737:                            assertResultsEqual(expected, results);
1738:                        } finally {
1739:                            query.closeAll();
1740:                        }
1741:
1742:                        hasRun = true;
1743:                    }
1744:
1745:                    if (transactional)
1746:                        tx.commit();
1747:                }
1748:            }
1749:
1750:            private void setWidgetQuery8(boolean transactional,
1751:                    PersistenceManager pm, Transaction tx, Extent swExtent)
1752:                    throws Exception {
1753:                if (!HasInverseSetField.class.isAssignableFrom(swExtent
1754:                        .getCandidateClass()))
1755:                    return;
1756:
1757:                Query query;
1758:                Collection results;
1759:                HashSet expected;
1760:
1761:                /*
1762:                 * Query #8
1763:                 *
1764:                 * Test querying for containment in a persistent inverse Set parameter.
1765:                 */
1766:
1767:                boolean hasRun = false;
1768:                for (int i = 0; i < objs.length && !hasRun; ++i) {
1769:                    if (transactional)
1770:                        tx.begin();
1771:
1772:                    expected = new HashSet();
1773:                    HasInverseSetField sw = (HasInverseSetField) pm
1774:                            .getObjectById(ids[i], false);
1775:
1776:                    if (!sw.getInverseSet().isEmpty()) {
1777:                        expected.addAll(sw.getInverseSet());
1778:
1779:                        query = pm.newQuery(pm.getExtent(Widget.class, true));
1780:                        query.declareParameters("java.util.Set setParameter");
1781:                        query.setFilter("setParameter.contains(this)");
1782:                        results = (Collection) query
1783:                                .execute(sw.getInverseSet());
1784:
1785:                        try {
1786:                            assertResultsEqual(expected, results);
1787:                        } finally {
1788:                            query.closeAll();
1789:                        }
1790:
1791:                        hasRun = true;
1792:                    }
1793:
1794:                    if (transactional)
1795:                        tx.commit();
1796:                }
1797:            }
1798:
1799:            private void setWidgetQuery9(boolean transactional,
1800:                    PersistenceManager pm, Transaction tx, Extent swExtent,
1801:                    String setFieldName, Method getSetMethod) throws Exception {
1802:                Query query;
1803:                Collection results;
1804:                HashSet expected;
1805:
1806:                /*
1807:                 * Query #9
1808:                 *
1809:                 * Test querying for non-containment in a persistent normal Set parameter.
1810:                 */
1811:
1812:                boolean hasRun = false;
1813:                for (int i = 0; i < objs.length && !hasRun; ++i) {
1814:                    if (transactional)
1815:                        tx.begin();
1816:
1817:                    expected = new HashSet();
1818:                    HasNormalSetField sw = (HasNormalSetField) pm
1819:                            .getObjectById(ids[i], false);
1820:
1821:                    final Set set = getSet(sw, getSetMethod);
1822:                    if (!set.isEmpty()) {
1823:                        Extent e = pm.getExtent(Widget.class, true);
1824:                        for (Iterator it = e.iterator(); it.hasNext();) {
1825:                            expected.add(it.next());
1826:                        }
1827:                        expected.removeAll(set);
1828:
1829:                        query = pm.newQuery(e);
1830:                        query.declareParameters("java.util.Set setParameter");
1831:                        query.setFilter("!setParameter.contains(this)");
1832:                        results = (Collection) query.execute(set);
1833:
1834:                        try {
1835:                            assertResultsEqual(expected, results);
1836:                        } finally {
1837:                            query.closeAll();
1838:                        }
1839:
1840:                        hasRun = true;
1841:                    }
1842:
1843:                    if (transactional)
1844:                        tx.commit();
1845:                }
1846:            }
1847:
1848:            private void setWidgetQuery10(boolean transactional,
1849:                    PersistenceManager pm, Transaction tx, Extent swExtent)
1850:                    throws Exception {
1851:                if (!HasInverseSetField.class.isAssignableFrom(swExtent
1852:                        .getCandidateClass()))
1853:                    return;
1854:
1855:                Query query;
1856:                Collection results;
1857:                HashSet expected;
1858:
1859:                /*
1860:                 * Query #10
1861:                 *
1862:                 * Test querying for non-containment in a persistent inverse Set parameter.
1863:                 */
1864:
1865:                boolean hasRun = false;
1866:                for (int i = 0; i < objs.length && !hasRun; ++i) {
1867:                    if (transactional)
1868:                        tx.begin();
1869:
1870:                    expected = new HashSet();
1871:                    HasInverseSetField sw = (HasInverseSetField) pm
1872:                            .getObjectById(ids[i], false);
1873:
1874:                    if (!sw.getInverseSet().isEmpty()) {
1875:                        Extent e = pm.getExtent(Widget.class, true);
1876:                        for (Iterator it = e.iterator(); it.hasNext();) {
1877:                            expected.add(it.next());
1878:                        }
1879:                        expected.removeAll(sw.getInverseSet());
1880:
1881:                        query = pm.newQuery(e);
1882:                        query.declareParameters("java.util.Set setParameter");
1883:                        query.setFilter("!setParameter.contains(this)");
1884:                        results = (Collection) query
1885:                                .execute(sw.getInverseSet());
1886:
1887:                        try {
1888:                            assertResultsEqual(expected, results);
1889:                        } finally {
1890:                            query.closeAll();
1891:                        }
1892:
1893:                        hasRun = true;
1894:                    }
1895:
1896:                    if (transactional)
1897:                        tx.commit();
1898:                }
1899:            }
1900:
1901:            private void queryElementWidgetObjects(boolean transactional)
1902:                    throws Exception {
1903:                /*
1904:                 * Perform various queries and validate the resulting collection.
1905:                 */
1906:
1907:                LOG
1908:                        .info("Querying " + TEST_OBJECT_COUNT + " "
1909:                                + ElementWidget.class.getName() + " objects "
1910:                                + (transactional ? "in" : "outside")
1911:                                + " a transaction");
1912:                PersistenceManager pm = pmf.getPersistenceManager();
1913:                Transaction tx = pm.currentTransaction();
1914:
1915:                try {
1916:                    if (!transactional)
1917:                        tx.setNontransactionalRead(true);
1918:
1919:                    Query query;
1920:                    Collection results;
1921:
1922:                    /*
1923:                     * Query #1
1924:                     */
1925:
1926:                    if (transactional)
1927:                        tx.begin();
1928:
1929:                    query = pm
1930:                            .newQuery(pm.getExtent(ElementWidget.class, true));
1931:                    query
1932:                            .declareVariables("com.triactive.jdo.test.ElementWidget ew");
1933:                    query
1934:                            .setFilter("intField < 0 & owner.normalSet.contains(ew) & ew.intField < 0");
1935:                    results = (Collection) query.execute();
1936:
1937:                    try {
1938:                        HashSet expected = new HashSet();
1939:
1940:                        for (int i = 0; i < objs.length; ++i) {
1941:                            SetWidget sw = (SetWidget) pm.getObjectById(ids[i],
1942:                                    false);
1943:                            Iterator j = sw.getNormalSet().iterator();
1944:                            boolean found = false;
1945:
1946:                            while (j.hasNext()) {
1947:                                Widget w = (Widget) j.next();
1948:
1949:                                if (w.getIntField() < 0) {
1950:                                    found = true;
1951:                                    break;
1952:                                }
1953:                            }
1954:
1955:                            if (found) {
1956:                                j = sw.getInverseSet().iterator();
1957:
1958:                                while (j.hasNext()) {
1959:                                    ElementWidget ew = (ElementWidget) j.next();
1960:
1961:                                    if (ew.getIntField() < 0)
1962:                                        expected.add(ew);
1963:                                }
1964:                            }
1965:                        }
1966:
1967:                        assertResultsEqual(expected, results);
1968:                    } finally {
1969:                        query.closeAll();
1970:                    }
1971:
1972:                    if (transactional)
1973:                        tx.commit();
1974:
1975:                    /*
1976:                     * Query #2
1977:                     *
1978:                     * Test querying for values passed in as a parameter by a transient Set
1979:                     * of nested Objects. 
1980:                     */
1981:
1982:                    if (transactional)
1983:                        tx.begin();
1984:
1985:                    Random rand = new Random();
1986:                    HashSet setParameters = new HashSet();
1987:                    HashSet expected = new HashSet();
1988:
1989:                    for (int i = 0; i < objs.length; ++i) {
1990:                        SetWidget sw = (SetWidget) pm.getObjectById(ids[i],
1991:                                false);
1992:
1993:                        if (rand.nextBoolean() && !sw.getInverseSet().isEmpty()) {
1994:                            for (Iterator it = sw.getInverseSet().iterator(); it
1995:                                    .hasNext();) {
1996:                                if (rand.nextBoolean()) {
1997:                                    ElementWidget ew = (ElementWidget) it
1998:                                            .next();
1999:                                    setParameters.add(sw);
2000:                                    expected.add(ew);
2001:                                }
2002:                            }
2003:                        }
2004:                    }
2005:
2006:                    query = pm
2007:                            .newQuery(pm.getExtent(ElementWidget.class, true));
2008:                    query.declareParameters("java.util.Set setParameter");
2009:                    query.setFilter("setParameter.contains(owner)");
2010:                    results = (Collection) query.execute(setParameters);
2011:
2012:                    try {
2013:                        assertResultsEqual(expected, results);
2014:                    } finally {
2015:                        query.closeAll();
2016:                    }
2017:
2018:                    if (transactional)
2019:                        tx.commit();
2020:                } finally {
2021:                    if (tx.isActive())
2022:                        tx.rollback();
2023:
2024:                    pm.close();
2025:                }
2026:            }
2027:
2028:            private void queryCollectionsUsingNestedContains(Class setClass,
2029:                    String setFieldName, Method getSetMethod) throws Exception {
2030:                /*
2031:                 * Perform various queries and validate the resulting collection.
2032:                 */
2033:                PersistenceManager pm = pmf.getPersistenceManager();
2034:                Transaction tx = pm.currentTransaction();
2035:
2036:                try {
2037:                    LOG.info("Inserting " + TEST_OBJECT_COUNT + " "
2038:                            + setClass.getName() + " objects");
2039:
2040:                    for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
2041:                        tx.begin();
2042:
2043:                        HasNormalSetField sw = (HasNormalSetField) setClass
2044:                                .newInstance();
2045:                        sw.fillRandom(10, true);
2046:
2047:                        objs[i] = (TestObject) ((TestObject) sw).clone();
2048:
2049:                        pm.makePersistent(sw);
2050:
2051:                        ids[i] = JDOHelper.getObjectId(sw);
2052:
2053:                        tx.commit();
2054:                    }
2055:
2056:                    LOG.info("Querying nested collections (" + setFieldName
2057:                            + ") in a " + setClass.getName() + " object");
2058:
2059:                    tx.begin();
2060:
2061:                    for (int queryFlags = 0; queryFlags <= ALL_QUERY_FLAGS; ++queryFlags) {
2062:                        if ((queryFlags & QUERY_FLAG_USE_SUBCLASS) != 0)
2063:                            continue;
2064:
2065:                        Query query = pm.newQuery(pm.getExtent(setClass, true));
2066:                        query
2067:                                .declareImports("import com.triactive.jdo.test.Widget;");
2068:                        query.declareVariables(setClass.getName()
2069:                                + " sw; Widget w");
2070:                        query.setFilter(setFieldName + ".contains(sw) && sw."
2071:                                + setFieldName
2072:                                + ".contains(w) && w.intField < 0");
2073:
2074:                        if ((queryFlags & QUERY_FLAG_USE_ORDERING) != 0)
2075:                            query.setOrdering("numWidgets ascending");
2076:
2077:                        Collection results = (Collection) query.execute();
2078:
2079:                        try {
2080:                            HashSet expected = new HashSet();
2081:
2082:                            for (int i = 0; i < objs.length; ++i) {
2083:                                HasNormalSetField sw = (HasNormalSetField) objs[i];
2084:
2085:                                Iterator j = getSet(sw, getSetMethod)
2086:                                        .iterator();
2087:                                boolean found = false;
2088:
2089:                                while (j.hasNext()) {
2090:                                    Widget w = (Widget) j.next();
2091:
2092:                                    if (w instanceof  HasNormalSetField) {
2093:                                        HasNormalSetField sw1 = (HasNormalSetField) w;
2094:                                        Iterator k = getSet(sw1, getSetMethod)
2095:                                                .iterator();
2096:
2097:                                        while (k.hasNext()) {
2098:                                            w = (Widget) k.next();
2099:
2100:                                            if (w.getIntField() < 0) {
2101:                                                found = true;
2102:                                                break;
2103:                                            }
2104:                                        }
2105:
2106:                                        if (found)
2107:                                            break;
2108:                                    }
2109:                                }
2110:
2111:                                if (found)
2112:                                    expected.add(pm
2113:                                            .getObjectById(ids[i], false));
2114:                            }
2115:
2116:                            assertResultsEqual(expected, results);
2117:                        } finally {
2118:                            query.closeAll();
2119:                        }
2120:                    }
2121:
2122:                    tx.commit();
2123:                } finally {
2124:                    if (tx.isActive())
2125:                        tx.rollback();
2126:
2127:                    pm.close();
2128:                }
2129:
2130:                removeObjects();
2131:            }
2132:
2133:            private void queryCollectionsUsingCandidateSubclasses(
2134:                    Class setClass, boolean transactional) throws Exception {
2135:                /*
2136:                 * Perform various queries and validate the resulting collection.
2137:                 */
2138:
2139:                LOG.info("Querying collections in a " + setClass.getName()
2140:                        + " object " + (transactional ? "in" : "outside")
2141:                        + " a transaction");
2142:                PersistenceManager pm = pmf.getPersistenceManager();
2143:                Transaction tx = pm.currentTransaction();
2144:
2145:                try {
2146:                    if (!transactional)
2147:                        tx.setNontransactionalRead(true);
2148:
2149:                    tx.begin();
2150:
2151:                    HasNormalSetField sw = (HasNormalSetField) setClass
2152:                            .newInstance();
2153:                    sw.fillRandom(TEST_OBJECT_COUNT);
2154:                    pm.makePersistent(sw);
2155:
2156:                    tx.commit();
2157:
2158:                    Query query;
2159:                    Collection results;
2160:
2161:                    /*
2162:                     * Query #1
2163:                     */
2164:
2165:                    if (transactional)
2166:                        tx.begin();
2167:
2168:                    Collection normalSet = sw.getNormalSet();
2169:
2170:                    query = pm.newQuery(DateWidget.class, normalSet);
2171:                    query.declareImports("import java.util.Date;");
2172:                    query.declareParameters("Date epoch");
2173:                    query.setFilter("dateField < epoch");
2174:                    results = (Collection) query.execute(new Date(0L));
2175:
2176:                    try {
2177:                        Query expectedQuery = pm.newQuery(pm.getExtent(
2178:                                DateWidget.class, true));
2179:                        expectedQuery.declareImports("import java.util.Date;");
2180:                        expectedQuery.declareParameters("Date epoch");
2181:                        expectedQuery.setFilter("dateField < epoch");
2182:
2183:                        Collection expectedResults = (Collection) expectedQuery
2184:                                .execute(new Date(0L));
2185:
2186:                        try {
2187:                            HashSet expected = new HashSet(expectedResults);
2188:
2189:                            assertResultsEqual(expected, results);
2190:                        } finally {
2191:                            expectedQuery.closeAll();
2192:                        }
2193:                    } finally {
2194:                        query.closeAll();
2195:                    }
2196:
2197:                    if (transactional)
2198:                        tx.commit();
2199:
2200:                    tx.begin();
2201:
2202:                    pm.deletePersistent(sw);
2203:
2204:                    tx.commit();
2205:                } finally {
2206:                    if (tx.isActive())
2207:                        tx.rollback();
2208:
2209:                    pm.close();
2210:                }
2211:            }
2212:
2213:            private void queryOwnerWidgetObjects(boolean transactional)
2214:                    throws Exception {
2215:                /*
2216:                 * Perform various queries and validate the resulting collection.
2217:                 */
2218:
2219:                LOG
2220:                        .info("Querying " + TEST_OBJECT_COUNT + " "
2221:                                + OwnerWidget.class.getName() + " objects "
2222:                                + (transactional ? "in" : "outside")
2223:                                + " a transaction");
2224:                PersistenceManager pm = pmf.getPersistenceManager();
2225:                Transaction tx = pm.currentTransaction();
2226:
2227:                try {
2228:                    if (!transactional)
2229:                        tx.setNontransactionalRead(true);
2230:
2231:                    Query query;
2232:                    Collection results;
2233:
2234:                    /*
2235:                     * Query #1
2236:                     */
2237:
2238:                    if (transactional)
2239:                        tx.begin();
2240:
2241:                    query = pm.newQuery(pm.getExtent(OwnerWidget.class, true));
2242:                    query
2243:                            .declareImports("import com.triactive.jdo.test.DateWidget; import java.util.Date;");
2244:                    query.declareParameters("Date epoch");
2245:                    query
2246:                            .setFilter("((DateWidget)cloneable).dateField != null && ((DateWidget)cloneable).dateField < epoch");
2247:
2248:                    Date epoch = new Date(0L);
2249:                    results = (Collection) query.execute(epoch);
2250:
2251:                    try {
2252:                        HashSet expected = new HashSet();
2253:
2254:                        for (int i = 0; i < objs.length; ++i) {
2255:                            OwnerWidget ow = (OwnerWidget) objs[i];
2256:
2257:                            if (ow.getCloneable() instanceof  DateWidget) {
2258:                                DateWidget dw = (DateWidget) ow.getCloneable();
2259:
2260:                                if (dw.getDateField() != null
2261:                                        && dw.getDateField().before(epoch))
2262:                                    expected.add(pm
2263:                                            .getObjectById(ids[i], false));
2264:                            }
2265:                        }
2266:
2267:                        assertResultsEqual(expected, results);
2268:                    } finally {
2269:                        query.closeAll();
2270:                    }
2271:
2272:                    if (transactional)
2273:                        tx.commit();
2274:
2275:                    /*
2276:                     * Query #2
2277:                     */
2278:
2279:                    if (transactional)
2280:                        tx.begin();
2281:
2282:                    query = pm.newQuery(pm.getExtent(OwnerWidget.class, true));
2283:                    query
2284:                            .declareImports("import com.triactive.jdo.test.DateWidget; import java.util.Date;");
2285:                    query.declareParameters("Date epoch");
2286:                    query
2287:                            .setFilter("((DateWidget)testObject).dateField != null && ((DateWidget)testObject).dateField > epoch");
2288:
2289:                    results = (Collection) query.execute(epoch);
2290:
2291:                    try {
2292:                        HashSet expected = new HashSet();
2293:
2294:                        for (int i = 0; i < objs.length; ++i) {
2295:                            OwnerWidget ow = (OwnerWidget) objs[i];
2296:
2297:                            if (ow.getTestObject() instanceof  DateWidget) {
2298:                                DateWidget dw = (DateWidget) ow.getTestObject();
2299:
2300:                                if (dw.getDateField() != null
2301:                                        && dw.getDateField().after(epoch))
2302:                                    expected.add(pm
2303:                                            .getObjectById(ids[i], false));
2304:                            }
2305:                        }
2306:
2307:                        assertResultsEqual(expected, results);
2308:                    } finally {
2309:                        query.closeAll();
2310:                    }
2311:
2312:                    if (transactional)
2313:                        tx.commit();
2314:                } finally {
2315:                    if (tx.isActive())
2316:                        tx.rollback();
2317:
2318:                    pm.close();
2319:                }
2320:            }
2321:
2322:            private void queryKeywordConflictObjects(boolean transactional)
2323:                    throws Exception {
2324:                /*
2325:                 * Perform various queries and validate the resulting collection.
2326:                 */
2327:
2328:                LOG
2329:                        .info("Querying " + TEST_OBJECT_COUNT + " "
2330:                                + KeywordConflict.class.getName() + " objects "
2331:                                + (transactional ? "in" : "outside")
2332:                                + " a transaction");
2333:                PersistenceManager pm = pmf.getPersistenceManager();
2334:                Transaction tx = pm.currentTransaction();
2335:
2336:                try {
2337:                    if (!transactional)
2338:                        tx.setNontransactionalRead(true);
2339:
2340:                    Query query;
2341:                    Collection results;
2342:
2343:                    /*
2344:                     * Query #1
2345:                     */
2346:
2347:                    if (transactional)
2348:                        tx.begin();
2349:
2350:                    query = pm.newQuery(pm.getExtent(KeywordConflict.class,
2351:                            true));
2352:                    query
2353:                            .setFilter("column < 0 || select >= 0 || where < 0 || varchar >= 0 || decimal < 0");
2354:                    results = (Collection) query.execute();
2355:
2356:                    try {
2357:                        HashSet expected = new HashSet();
2358:
2359:                        for (int i = 0; i < objs.length; ++i) {
2360:                            KeywordConflict t = (KeywordConflict) objs[i];
2361:
2362:                            if (t.getColumn() < 0 || t.getSelect() >= 0
2363:                                    || t.getWhere() < 0 || t.getVarchar() >= 0
2364:                                    || t.getDecimal() < 0)
2365:                                expected.add(pm.getObjectById(ids[i], false));
2366:                        }
2367:
2368:                        assertResultsEqual(expected, results);
2369:                    } finally {
2370:                        query.closeAll();
2371:                    }
2372:
2373:                    if (transactional)
2374:                        tx.commit();
2375:                } finally {
2376:                    if (tx.isActive())
2377:                        tx.rollback();
2378:
2379:                    pm.close();
2380:                }
2381:            }
2382:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.