0001: package org.apache.ojb.broker;
0002:
0003: /* Copyright 2002-2005 The Apache Software Foundation
0004: *
0005: * Licensed under the Apache License, Version 2.0 (the "License");
0006: * you may not use this file except in compliance with the License.
0007: * You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: import java.sql.Types;
0019: import java.util.ArrayList;
0020: import java.util.Arrays;
0021: import java.util.Collection;
0022: import java.util.HashSet;
0023: import java.util.Iterator;
0024: import java.util.List;
0025: import java.util.Set;
0026: import java.util.Vector;
0027: import java.math.BigDecimal;
0028:
0029: import org.apache.commons.lang.time.StopWatch;
0030: import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
0031: import org.apache.ojb.broker.metadata.ClassDescriptor;
0032: import org.apache.ojb.broker.metadata.CollectionDescriptor;
0033: import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
0034: import org.apache.ojb.broker.query.Criteria;
0035: import org.apache.ojb.broker.query.LikeCriteria;
0036: import org.apache.ojb.broker.query.Query;
0037: import org.apache.ojb.broker.query.QueryByCriteria;
0038: import org.apache.ojb.broker.query.QueryFactory;
0039: import org.apache.ojb.broker.query.ReportQueryByCriteria;
0040: import org.apache.ojb.junit.PBTestCase;
0041:
0042: /**
0043: * Testing the query API.
0044: * @version $Id: QueryTest.java,v 1.61.2.20 2005/12/21 22:31:23 tomdz Exp $
0045: */
0046: public class QueryTest extends PBTestCase {
0047:
0048: // Product groups above high water mark are modified by other testcases,
0049: // setting this low makes it easier to re-run this test
0050: private static final Integer PGROUP_ID_HI_WATERMARK = new Integer(
0051: 4000);
0052:
0053: /**
0054: * BrokerTests constructor comment.
0055: */
0056: public QueryTest(String name) {
0057: super (name);
0058: }
0059:
0060: public static void main(String[] args) {
0061: String[] arr = { QueryTest.class.getName() };
0062: junit.textui.TestRunner.main(arr);
0063: }
0064:
0065: public void testQueryZero() {
0066: String name = "testQueryZero_" + System.currentTimeMillis();
0067: ObjectRepository.Group group = new ObjectRepository.Group();
0068: group.setId(new Integer(0));
0069: group.setName(name);
0070:
0071: // prepare test
0072: broker.beginTransaction();
0073: Query q = QueryFactory.newQuery(group);
0074: broker.deleteByQuery(q);
0075: broker.commitTransaction();
0076: broker.clearCache();
0077:
0078: QueryByCriteria query = new QueryByCriteria(group);
0079: ObjectRepository.Group newGroup = (ObjectRepository.Group) broker
0080: .getObjectByQuery(query);
0081: assertNull(newGroup);
0082:
0083: broker.beginTransaction();
0084: broker.store(group);
0085: broker.commitTransaction();
0086:
0087: ObjectRepository.Group groupTemp = new ObjectRepository.Group();
0088: groupTemp.setId(new Integer(0));
0089: Query queryNew = QueryFactory.newQuery(groupTemp);
0090: newGroup = (ObjectRepository.Group) broker
0091: .getObjectByQuery(queryNew);
0092: assertNotNull(newGroup);
0093: assertEquals(new Integer(0), newGroup.getId());
0094:
0095: newGroup = (ObjectRepository.Group) broker
0096: .getObjectByQuery(queryNew);
0097: assertNotNull(newGroup);
0098: assertEquals(new Integer(0), newGroup.getId());
0099:
0100: broker.clearCache();
0101: newGroup = (ObjectRepository.Group) broker
0102: .getObjectByQuery(queryNew);
0103: assertNotNull(newGroup);
0104: assertEquals(new Integer(0), newGroup.getId());
0105: }
0106:
0107: /**
0108: * Criteria containing other Criteria only
0109: */
0110: public void testCriteria() {
0111: String name = "testCriteria_" + System.currentTimeMillis();
0112: Person p1 = new Person();
0113: p1.setFirstname("tomm");
0114: p1.setLastname(name);
0115:
0116: Person p2 = new Person();
0117: p2.setFirstname("tom");
0118: p2.setLastname(name);
0119:
0120: Person p3 = new Person();
0121: p3.setFirstname("xtom");
0122: p3.setLastname(name);
0123:
0124: broker.beginTransaction();
0125: broker.store(p1);
0126: broker.store(p2);
0127: broker.store(p3);
0128: broker.commitTransaction();
0129:
0130: Criteria crit1 = new Criteria();
0131: Criteria crit2 = new Criteria();
0132: Criteria crit3 = new Criteria();
0133:
0134: crit2.addEqualTo("lastname", name);
0135: crit2.setNegative(false);
0136:
0137: crit3.addEqualTo("firstname", "tom");
0138: crit3.setNegative(true);
0139:
0140: crit1.addAndCriteria(crit2);
0141: crit1.addAndCriteria(crit3);
0142:
0143: Query q = QueryFactory.newQuery(Person.class, crit1);
0144: Collection results = broker.getCollectionByQuery(q);
0145:
0146: // all persons except tom
0147: assertEquals(2, results.size());
0148: }
0149:
0150: /**
0151: * test EqualTo Criteria
0152: */
0153: public void testEqualCriteria() {
0154: Criteria crit = new Criteria();
0155: crit.addEqualTo("firstname", "tom");
0156: Query q = QueryFactory.newQuery(Person.class, crit);
0157:
0158: Collection results = broker.getCollectionByQuery(q);
0159: assertNotNull(results);
0160: assertTrue(results.size() > 0);
0161: }
0162:
0163: /**
0164: * test sql Criteria
0165: */
0166: public void testSqlCriteria() {
0167: Criteria crit;
0168: Query q;
0169: Collection results;
0170:
0171: // sql only
0172: crit = new Criteria();
0173: crit.addSql("upper(firstname) = 'TOM' and id = 1");
0174: q = QueryFactory.newQuery(Person.class, crit);
0175:
0176: results = broker.getCollectionByQuery(q);
0177: int size1 = results.size();
0178:
0179: // sql plus attribute
0180: crit = new Criteria();
0181: crit.addSql("upper(firstname) = 'TOM'");
0182: crit.addEqualTo("id", new Integer(1));
0183: q = QueryFactory.newQuery(Person.class, crit);
0184:
0185: results = broker.getCollectionByQuery(q);
0186: int size2 = results.size();
0187:
0188: // attribute plus sql
0189: crit = new Criteria();
0190: crit.addEqualTo("upper(firstname)", "TOM");
0191: crit.addSql("id = 1");
0192: q = QueryFactory.newQuery(Person.class, crit);
0193:
0194: results = broker.getCollectionByQuery(q);
0195: int size3 = results.size();
0196:
0197: assertTrue(size2 == size1);
0198: assertTrue(size3 == size2);
0199: }
0200:
0201: /**
0202: * test OrderBy and Count
0203: */
0204: public void testOrderByCount() {
0205: Criteria crit = new Criteria();
0206: crit.addEqualTo("firstname", "tom");
0207: QueryByCriteria q = QueryFactory.newQuery(Person.class, crit);
0208: q.addOrderByAscending("firstname");
0209:
0210: Collection results = broker.getCollectionByQuery(q);
0211: assertNotNull(results);
0212: assertTrue(results.size() > 0);
0213:
0214: // compare with count
0215: int count = broker.getCount(q);
0216: assertEquals(results.size(), count);
0217: }
0218:
0219: /**
0220: * test OrderBy joined column
0221: */
0222: public void testOrderByJoined() {
0223: String name = "testOrderByJoined" + System.currentTimeMillis();
0224: Person p = new Person();
0225: p.setFirstname("tom");
0226: p.setLastname(name);
0227:
0228: Project p1 = new Project();
0229: p1.setTitle(name);
0230: ArrayList list_1 = new ArrayList();
0231: list_1.add(p);
0232: p1.setPersons(list_1);
0233:
0234: Project p2 = new Project();
0235: p2.setTitle(name);
0236: ArrayList list_2 = new ArrayList();
0237: list_2.add(p);
0238: p2.setPersons(list_2);
0239:
0240: ArrayList list_projects = new ArrayList();
0241: list_projects.add(p1);
0242: list_projects.add(p2);
0243: p.setProjects(list_projects);
0244:
0245: Project p3 = new Project();
0246: p3.setTitle(name);
0247: ArrayList list_3 = new ArrayList();
0248: // empty list
0249: p3.setPersons(list_3);
0250:
0251: broker.beginTransaction();
0252: broker.store(p1);
0253: //broker.store(p2);
0254: //broker.store(p3);
0255: broker.commitTransaction();
0256:
0257: Criteria crit = new Criteria();
0258: crit.addLike("title", name);
0259: QueryByCriteria q = QueryFactory.newQuery(Project.class, crit);
0260: q.addOrderByAscending("title");
0261: q.addOrderByAscending("persons.lastname");
0262: q.addOrderByAscending("persons.firstname");
0263:
0264: Collection results = broker.getCollectionByQuery(q);
0265: assertNotNull(results);
0266: assertEquals(2, results.size());
0267:
0268: // compare with count
0269: int count = broker.getCount(q);
0270: assertEquals(results.size(), count);
0271: }
0272:
0273: /**
0274: * test Subquery get all product groups without articles
0275: * <p/>
0276: * test may fail if db does not support sub queries
0277: */
0278: public void testSubQuery2() {
0279: Collection results = null;
0280: String stamp = "" + System.currentTimeMillis();
0281: int loops = 10;
0282: // create ProductGroups without article
0283: broker.beginTransaction();
0284: for (int i = 0; i < loops; i++) {
0285: ProductGroup pg = new ProductGroup();
0286: pg.setGroupName("test group " + stamp);
0287: pg.setDescription("build by QueryTest#testSubQuery2");
0288: broker.store(pg);
0289: }
0290: broker.commitTransaction();
0291:
0292: ReportQueryByCriteria subQuery;
0293: Criteria subCrit = new Criteria();
0294: Criteria crit = new Criteria();
0295:
0296: subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
0297: subQuery.setAttributes(new String[] { "productGroupId" });
0298: subQuery.setDistinct(true);
0299:
0300: crit.addEqualTo("groupName", "test group " + stamp);
0301: crit.addNotIn("groupId", subQuery);
0302: Query q = QueryFactory.newQuery(ProductGroup.class, crit);
0303:
0304: results = broker.getCollectionByQuery(q);
0305: assertNotNull(results);
0306: assertEquals(
0307: "Result of the query with sub-query does not match",
0308: loops, results.size());
0309: }
0310:
0311: /**
0312: * test Subquery get all articles with price > avg(price) PROBLEM:
0313: * avg(price) is NOT extent aware !!
0314: * <p/>
0315: * test may fail if db does not support sub queries
0316: */
0317: public void testSubQuery1() {
0318:
0319: ReportQueryByCriteria subQuery;
0320: Criteria subCrit = new Criteria();
0321: Criteria crit = new Criteria();
0322:
0323: subCrit.addLike("articleName", "A%");
0324: subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
0325: subQuery.setAttributes(new String[] { "avg(price)" });
0326:
0327: crit.addGreaterOrEqualThan("price", subQuery);
0328: Query q = QueryFactory.newQuery(Article.class, crit);
0329:
0330: Collection results = broker.getCollectionByQuery(q);
0331: assertNotNull(results);
0332: assertTrue(results.size() > 0);
0333:
0334: }
0335:
0336: /**
0337: * test Subquery get all product groups with more than 10 articles, uses
0338: * attribute as value ! see testSubQuery4 for a better way
0339: * <p/>
0340: * test may fail if db does not support sub queries
0341: */
0342: public void testSubQuery3() {
0343:
0344: ReportQueryByCriteria subQuery;
0345: Criteria subCrit = new Criteria();
0346: Criteria crit = new Criteria();
0347:
0348: subCrit.addEqualToField("productGroupId",
0349: Criteria.PARENT_QUERY_PREFIX + "groupId");
0350: subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
0351: subQuery
0352: .setAttributes(new String[] { "count(productGroupId)" });
0353:
0354: crit.addLessThan("10", subQuery); // MORE than 10 articles, uses
0355: // attribute as value !
0356: crit.addLessThan("groupId", PGROUP_ID_HI_WATERMARK);
0357: Query q = QueryFactory.newQuery(ProductGroup.class, crit);
0358:
0359: Collection results = broker.getCollectionByQuery(q);
0360: assertNotNull(results);
0361: assertEquals(4, results.size());
0362: }
0363:
0364: /**
0365: * test Subquery get all product groups with more than 10 articles
0366: * <p/>
0367: * test may fail if db does not support sub queries
0368: */
0369: public void testSubQuery4() {
0370:
0371: ReportQueryByCriteria subQuery;
0372: Criteria subCrit = new Criteria();
0373: Criteria crit = new Criteria();
0374:
0375: subCrit.addEqualToField("productGroupId",
0376: Criteria.PARENT_QUERY_PREFIX + "groupId");
0377: subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
0378: subQuery
0379: .setAttributes(new String[] { "count(productGroupId)" });
0380:
0381: // mkalen: if using String("10") instead of Integer below,
0382: // PostgreSQL will return 7 (sic!) groups
0383: crit.addGreaterThan(subQuery, new Integer(10)); // MORE than 10 articles
0384: crit.addLessThan("groupId", PGROUP_ID_HI_WATERMARK);
0385: Query q = QueryFactory.newQuery(ProductGroup.class, crit);
0386:
0387: Collection results = broker.getCollectionByQuery(q);
0388: assertNotNull(results);
0389: assertEquals(4, results.size());
0390: }
0391:
0392: /**
0393: * test Like Criteria
0394: */
0395: public void testLikeCriteria() {
0396:
0397: Criteria crit = new Criteria();
0398: crit.addLike("firstname", "%o%");
0399: Query q = QueryFactory.newQuery(Person.class, crit);
0400:
0401: Collection results = broker.getCollectionByQuery(q);
0402: assertNotNull(results);
0403: assertTrue(results.size() > 0);
0404:
0405: // compare with count
0406: int count = broker.getCount(q);
0407: assertEquals(results.size(), count);
0408: }
0409:
0410: /**
0411: * test escaped Like Criteria
0412: */
0413: public void testLikeEscapedCriteria1() {
0414: Criteria crit = new Criteria();
0415: crit.addLike("firstname", "h%\\%");
0416: Query q = QueryFactory.newQuery(Person.class, crit);
0417:
0418: Collection results = broker.getCollectionByQuery(q);
0419: assertNotNull(results);
0420: assertEquals(1, results.size());
0421:
0422: // compare with count
0423: int count = broker.getCount(q);
0424: assertEquals(results.size(), count);
0425: }
0426:
0427: /**
0428: * test escaped Like Criteria with escape character
0429: */
0430: public void testLikeEscapedCriteria2() {
0431: LikeCriteria.setEscapeCharacter('|');
0432:
0433: Criteria crit = new Criteria();
0434: crit.addLike("firstname", "h%|%");
0435: Query q = QueryFactory.newQuery(Person.class, crit);
0436:
0437: Collection results = broker.getCollectionByQuery(q);
0438: assertNotNull(results);
0439: assertEquals(1, results.size());
0440:
0441: // compare with count
0442: int count = broker.getCount(q);
0443: assertEquals(results.size(), count);
0444: }
0445:
0446: /**
0447: * test escaped Like Criteria
0448: */
0449: public void testLikeEscapedCriteria3() {
0450: String name = "testLikeEscapedCriteria3()_"
0451: + System.currentTimeMillis();
0452: Person p = new Person();
0453: p.setFirstname("123%45");
0454: p.setLastname(name);
0455: broker.beginTransaction();
0456: broker.store(p);
0457: broker.commitTransaction();
0458:
0459: LikeCriteria.setEscapeCharacter('/');
0460: Criteria crit = new Criteria();
0461: crit.addEqualTo("lastname", name);
0462: crit.addLike("firstname", "%/%45");
0463: Query q = QueryFactory.newQuery(Person.class, crit);
0464:
0465: Collection results = broker.getCollectionByQuery(q);
0466: assertNotNull(results);
0467: assertEquals(1, results.size());
0468: }
0469:
0470: /**
0471: * test Null Criteria
0472: */
0473: public void testNullCriteria() {
0474: String name = "testNullCriteria_" + System.currentTimeMillis();
0475: Person p = new Person();
0476: p.setLastname(name);
0477: broker.beginTransaction();
0478: broker.store(p);
0479: broker.commitTransaction();
0480:
0481: Criteria crit = new Criteria();
0482: crit.addIsNull("firstname");
0483: Criteria crit2 = new Criteria();
0484: crit2.addLike("lastname", name);
0485: crit.addAndCriteria(crit2);
0486:
0487: Query q = QueryFactory.newQuery(Person.class, crit);
0488:
0489: Collection results = broker.getCollectionByQuery(q);
0490: assertNotNull(results);
0491: assertEquals(1, results.size());
0492: }
0493:
0494: /**
0495: * Add an empty criteria
0496: */
0497: public void testEmptyORed() throws Exception {
0498: Collection result;
0499: Criteria crit1 = new Criteria();
0500: crit1.addEqualTo("articleName", "Hamlet");
0501: crit1
0502: .addEqualTo("productGroup.description",
0503: "Strange Books...");
0504:
0505: Criteria crit2 = new Criteria();
0506:
0507: crit1.addOrCriteria(crit2);
0508: QueryByCriteria q = QueryFactory.newQuery(Article.class, crit1);
0509:
0510: result = broker.getCollectionByQuery(q);
0511: assertNotNull(result);
0512:
0513: int count = broker.getCount(q);
0514: assertEquals(count, result.size());
0515: }
0516:
0517: /**
0518: * Add an empty criteria
0519: */
0520: public void testEmptyANDed() throws Exception {
0521: Collection result;
0522: Criteria crit1 = new Criteria();
0523: crit1.addEqualTo("articleName", "Hamlet");
0524: crit1
0525: .addEqualTo("productGroup.description",
0526: "Strange Books...");
0527:
0528: Criteria crit2 = new Criteria();
0529:
0530: crit1.addAndCriteria(crit2);
0531: QueryByCriteria q = QueryFactory.newQuery(Article.class, crit1);
0532:
0533: result = broker.getCollectionByQuery(q);
0534: assertNotNull(result);
0535:
0536: int count = broker.getCount(q);
0537: assertEquals(count, result.size());
0538: }
0539:
0540: /**
0541: * test Between Criteria
0542: */
0543: public void testBetweenCriteria() {
0544:
0545: Criteria crit = new Criteria();
0546: crit.addBetween("id", new Integer(1), new Integer(5));
0547: Query q = QueryFactory.newQuery(Person.class, crit);
0548:
0549: Collection results = broker.getCollectionByQuery(q);
0550: assertNotNull(results);
0551: assertTrue(results.size() == 5);
0552:
0553: // compare with count
0554: int count = broker.getCount(q);
0555: assertEquals(results.size(), count);
0556: }
0557:
0558: /**
0559: * test In Criteria
0560: */
0561: public void testInCriteria() {
0562:
0563: Criteria crit = new Criteria();
0564: Collection ids = new Vector();
0565: ids.add(new Integer(1));
0566: ids.add(new Integer(3));
0567: ids.add(new Integer(5));
0568:
0569: crit.addIn("id", ids);
0570: Query q = QueryFactory.newQuery(Person.class, crit);
0571:
0572: Collection results = broker.getCollectionByQuery(q);
0573: assertNotNull(results);
0574: assertTrue(results.size() == 3);
0575:
0576: // compare with count
0577: int count = broker.getCount(q);
0578: assertEquals(results.size(), count);
0579: }
0580:
0581: /**
0582: * Single Path Expression
0583: */
0584: public void testPathExpressions() {
0585:
0586: Criteria crit = new Criteria();
0587: crit.addEqualTo("productGroup.groupName", "Liquors");
0588: Query q = QueryFactory.newQuery(Article.class, crit);
0589:
0590: Collection results = broker.getCollectionByQuery(q);
0591: assertNotNull(results);
0592: assertTrue(results.size() > 0);
0593:
0594: // compare with count
0595: int count = broker.getCount(q);
0596: assertEquals(results.size(), count);
0597: }
0598:
0599: /**
0600: * Multi Path Expression over decomposed m:n
0601: */
0602: public void testPathExpressionsMtoNDecomposed() {
0603:
0604: Criteria crit = new Criteria();
0605: crit.addEqualTo("roles.project.title", "HSQLDB");
0606: // use decomposed m:n
0607: Query q = QueryFactory.newQuery(Person.class, crit);
0608:
0609: Collection results = broker.getCollectionByQuery(q);
0610: assertNotNull(results);
0611:
0612: }
0613:
0614: /**
0615: * Multi Path Expression over nondecomposed m:n
0616: */
0617: public void testPathExpressionsMtoN() {
0618:
0619: Criteria crit = new Criteria();
0620: crit.addEqualTo("projects.title", "HSQLDB"); // direct m:n
0621: Query q = QueryFactory.newQuery(Person.class, crit);
0622:
0623: Collection results = broker.getCollectionByQuery(q);
0624: assertNotNull(results);
0625:
0626: }
0627:
0628: /**
0629: * Multi Path Expression over nondecomposed m:n new test case for the 'not
0630: * unique alias' problem with m:n relationship
0631: */
0632: public void testPathExpressionsMtoN2() {
0633: Criteria crit = new Criteria();
0634: crit.addEqualTo("projects.roles.roleName", "developer");
0635: crit.addLike("projects.persons.lastname", "%b%");
0636: Query q = QueryFactory.newQuery(Person.class, crit);
0637:
0638: Collection results = broker.getCollectionByQuery(q);
0639: assertNotNull(results);
0640: assertTrue(results.size() > 0);
0641: }
0642:
0643: /**
0644: * Distinct Query
0645: */
0646: public void testDistinct() {
0647:
0648: Criteria crit = new Criteria();
0649: crit.addLike("allArticlesInGroup.articleName", "C%");
0650: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
0651: crit, true);
0652: q.addOrderByAscending("groupId");
0653:
0654: Collection results = broker.getCollectionByQuery(q);
0655: assertNotNull(results);
0656: assertEquals(5, results.size());
0657:
0658: // compare with count
0659: int count = broker.getCount(q);
0660: assertEquals(results.size(), count);
0661: }
0662:
0663: /**
0664: * Distinct Query
0665: */
0666: public void testDistinctMultiPk() {
0667:
0668: Criteria crit = new Criteria();
0669: crit.addEqualTo("project_id", new Integer(1));
0670: QueryByCriteria q = QueryFactory.newQuery(Role.class, crit,
0671: true);
0672:
0673: Collection results = broker.getCollectionByQuery(q);
0674: assertNotNull(results);
0675: assertEquals(3, results.size());
0676:
0677: // compare with count
0678: int count = broker.getCount(q);
0679:
0680: assertEquals(results.size(), count); // FAILS
0681: }
0682:
0683: /**
0684: * Simple ReportQuery returning rows with 3 columns of Person
0685: */
0686: public void testReportQuery() {
0687: String name = "testReportQuery_" + System.currentTimeMillis();
0688: Person p1 = new Person();
0689: p1.setFirstname("Robert");
0690: p1.setLastname(name);
0691: Person p2 = new Person();
0692: p2.setFirstname("Tom");
0693: p2.setLastname(name);
0694: Person p3 = new Person();
0695: p3.setFirstname("Roger");
0696: p3.setLastname(name);
0697: broker.beginTransaction();
0698: broker.store(p1);
0699: broker.store(p2);
0700: broker.store(p3);
0701: broker.commitTransaction();
0702:
0703: Criteria crit = new Criteria();
0704: Collection results = new Vector();
0705: crit.addLike("firstname", "%o%");
0706: Criteria crit2 = new Criteria();
0707: crit2.addLike("lastname", name);
0708: crit.addAndCriteria(crit2);
0709: ReportQueryByCriteria q = QueryFactory.newReportQuery(
0710: Person.class, crit);
0711: q.setAttributes(new String[] { "id", "firstname", "count(*)" });
0712: q.addGroupBy(new String[] { "id", "firstname" });
0713:
0714: Iterator iter = broker.getReportQueryIteratorByQuery(q);
0715:
0716: assertNotNull(iter);
0717: while (iter.hasNext()) {
0718: Object[] row = (Object[]) iter.next();
0719: assertEquals(3, row.length);
0720:
0721: results.add(row);
0722: }
0723:
0724: assertEquals(3, results.size());
0725:
0726: // compare with count
0727: int count = broker.getCount(q);
0728: assertEquals(results.size(), count);
0729: }
0730:
0731: /**
0732: * Simple ReportQuery returning rows with max(id) of Person grouped by not selected columns
0733: */
0734: public void testReportQueryGroupByNonSelectColumns() {
0735: String name = "testReportQueryGroupByNonSelectColumns_"
0736: + System.currentTimeMillis();
0737: Person p1 = new Person();
0738: p1.setFirstname("Robert");
0739: p1.setLastname(name);
0740: Person p2 = new Person();
0741: p2.setFirstname("Tom");
0742: p2.setLastname(name);
0743: Person p3 = new Person();
0744: p3.setFirstname("Roger");
0745: p3.setLastname(name);
0746: broker.beginTransaction();
0747: broker.store(p1);
0748: broker.store(p2);
0749: broker.store(p3);
0750: broker.commitTransaction();
0751:
0752: Criteria crit = new Criteria();
0753: Collection results = new Vector();
0754: crit.addLike("firstname", "%o%");
0755: Criteria crit2 = new Criteria();
0756: crit2.addLike("lastname", name);
0757: crit.addAndCriteria(crit2);
0758: ReportQueryByCriteria q = QueryFactory.newReportQuery(
0759: Person.class, crit);
0760: q.setAttributes(new String[] { "max(id)" });
0761: q.addGroupBy(new String[] { "lastname", "firstname" });
0762:
0763: Iterator iter = broker.getReportQueryIteratorByQuery(q);
0764:
0765: assertNotNull(iter);
0766: while (iter.hasNext()) {
0767: Object[] row = (Object[]) iter.next();
0768: assertEquals(1, row.length);
0769:
0770: results.add(row);
0771: }
0772:
0773: assertEquals(3, results.size());
0774:
0775: // compare with count
0776: int count = broker.getCount(q);
0777: assertEquals(results.size(), count);
0778: }
0779:
0780: /**
0781: * Simple ReportQuery returning rows with 3 columns of Person
0782: * Type of column data defined by sqltypes
0783: *
0784: * @see java.sql.Types
0785: */
0786: public void testReportQueryWithJdbcTypes() {
0787: String name = "testReportQuery_" + System.currentTimeMillis();
0788: Person p1 = new Person();
0789: p1.setFirstname("Robert");
0790: p1.setLastname(name);
0791: Person p2 = new Person();
0792: p2.setFirstname("Tom");
0793: p2.setLastname(name);
0794: Person p3 = new Person();
0795: p3.setFirstname("Roger");
0796: p3.setLastname(name);
0797: broker.beginTransaction();
0798: broker.store(p1);
0799: broker.store(p2);
0800: broker.store(p3);
0801: broker.commitTransaction();
0802:
0803: int types[] = new int[] { Types.DECIMAL, Types.VARCHAR,
0804: Types.BIGINT };
0805: Criteria crit = new Criteria();
0806: Collection results = new Vector();
0807: crit.addLike("firstname", "%o%");
0808: Criteria crit2 = new Criteria();
0809: crit2.addLike("lastname", name);
0810: crit.addAndCriteria(crit2);
0811:
0812: ReportQueryByCriteria q = QueryFactory.newReportQuery(
0813: Person.class, crit);
0814: q.setAttributes(new String[] { "id", "firstname", "count(*)" });
0815: q.addGroupBy(new String[] { "id", "firstname" });
0816: q.setJdbcTypes(types);
0817:
0818: Iterator iter = broker.getReportQueryIteratorByQuery(q);
0819:
0820: assertNotNull(iter);
0821: while (iter.hasNext()) {
0822: Object[] row = (Object[]) iter.next();
0823: assertEquals(3, row.length);
0824:
0825: // assertEquals(row[0].getClass(), BigDecimal.class);
0826: // assertEquals(row[1].getClass(), String.class);
0827: // assertEquals(row[2].getClass(), Long.class);
0828:
0829: results.add(row);
0830: }
0831:
0832: assertEquals(3, results.size());
0833:
0834: }
0835:
0836: /**
0837: * Simple ReportQuery returning rows with 3 columns of Person
0838: * needs SQL paging
0839: */
0840: public void testReportQueryWithStartAndEnd() {
0841: Criteria crit = new Criteria();
0842: Collection results = new Vector();
0843: crit.addLike("firstname", "%o%");
0844: ReportQueryByCriteria q = QueryFactory.newReportQuery(
0845: Person.class, crit);
0846: q.setAttributes(new String[] { "id", "firstname", "count(*)" });
0847: q.addGroupBy(new String[] { "id", "firstname" });
0848:
0849: q.setStartAtIndex(3);
0850: q.setEndAtIndex(5);
0851: Iterator iter = broker.getReportQueryIteratorByQuery(q);
0852: assertNotNull(iter);
0853: while (iter.hasNext()) {
0854: results.add(iter.next());
0855: }
0856: assertTrue(results.size() == 3);
0857:
0858: results.clear();
0859: q.setStartAtIndex(1);
0860: q.setEndAtIndex(5);
0861: iter = broker.getReportQueryIteratorByQuery(q);
0862: assertNotNull(iter);
0863: while (iter.hasNext()) {
0864: results.add(iter.next());
0865: }
0866: assertTrue(results.size() == 5);
0867: }
0868:
0869: /**
0870: * Simple ReportQuery returning rows with 2 columns of Person
0871: */
0872: public void testReportQueryExtent() {
0873:
0874: Criteria crit = new Criteria();
0875: Collection results = new Vector();
0876: crit.addLike("articleName", "%o%");
0877: ReportQueryByCriteria q = QueryFactory.newReportQuery(
0878: Article.class, crit);
0879: q.setAttributes(new String[] { "articleId", "articleName",
0880: "price" });
0881:
0882: Iterator iter = broker.getReportQueryIteratorByQuery(q);
0883:
0884: assertNotNull(iter);
0885: while (iter.hasNext()) {
0886: results.add(iter.next());
0887: }
0888: assertTrue(results.size() > 0);
0889:
0890: // compare with count
0891: int count = broker.getCount(q);
0892: assertEquals(results.size(), count);
0893: }
0894:
0895: /**
0896: * Concrete Class in SubQuery
0897: */
0898: public void testSubQueryAgainstConcreteClass() {
0899: String name = "testSubQueryAgainstConcreteClass_"
0900: + System.currentTimeMillis();
0901: Article article = new BookArticle();
0902: article.setArticleName(name);
0903: ProductGroup group = new ProductGroup();
0904: group.setGroupName(name);
0905: article.setProductGroup(group);
0906:
0907: broker.beginTransaction();
0908: broker.store(group);
0909: broker.store(article);
0910: broker.commitTransaction();
0911:
0912: Criteria critMain = new Criteria();
0913: Criteria critSub = new Criteria();
0914:
0915: critSub.addEqualTo("articleName", name);
0916: ReportQueryByCriteria querySub = QueryFactory.newReportQuery(
0917: BookArticle.class, critSub);
0918: querySub.setAttributes(new String[] { "productGroupId" });
0919:
0920: ReportQueryByCriteria queryMain = QueryFactory.newReportQuery(
0921: AbstractProductGroup.class, critMain);
0922: queryMain
0923: .setAttributes(new String[] { "groupId", "groupName" });
0924: critMain.addIn("groupId", querySub);
0925:
0926: Iterator iter = broker.getReportQueryIteratorByQuery(queryMain);
0927: int result = 0;
0928: assertNotNull(iter);
0929: while (iter.hasNext()) {
0930: iter.next();
0931: ++result;
0932: }
0933: assertEquals(1, result);
0934:
0935: // compare with count
0936: int count = broker.getCount(queryMain);
0937: assertEquals(result, count);
0938: }
0939:
0940: /**
0941: * Class with extents in SubQuery.
0942: * SubQueries are NOT extent aware ! so we have to use two queries.
0943: */
0944: public void testSubQueryAgainstExtents() {
0945: String name = "testSubQueryAgainstExtents_"
0946: + System.currentTimeMillis();
0947: Article article = new BookArticle();
0948: article.setArticleName(name);
0949: ProductGroup group = new ProductGroup();
0950: group.setGroupName(name);
0951: article.setProductGroup(group);
0952:
0953: broker.beginTransaction();
0954: broker.store(group);
0955: broker.store(article);
0956: broker.commitTransaction();
0957:
0958: Criteria critMain = new Criteria();
0959: Criteria critSub = new Criteria();
0960:
0961: critSub.addEqualTo("articleName", name);
0962: ReportQueryByCriteria querySub = QueryFactory.newReportQuery(
0963: Article.class, critSub);
0964: querySub.setAttributes(new String[] { "productGroupId" });
0965: Iterator subIter = broker
0966: .getReportQueryIteratorByQuery(querySub);
0967: Collection subIds = new ArrayList();
0968: while (subIter.hasNext()) {
0969: Object[] id = (Object[]) subIter.next();
0970: subIds.add(id[0]);
0971: }
0972:
0973: ReportQueryByCriteria queryMain = QueryFactory.newReportQuery(
0974: AbstractProductGroup.class, critMain);
0975: queryMain
0976: .setAttributes(new String[] { "groupId", "groupName" });
0977: critMain.addIn("groupId", subIds);
0978:
0979: Iterator iter = broker.getReportQueryIteratorByQuery(queryMain);
0980: int result = 0;
0981: assertNotNull(iter);
0982: while (iter.hasNext()) {
0983: iter.next();
0984: ++result;
0985: }
0986: assertEquals(1, result);
0987:
0988: // compare with count
0989: int count = broker.getCount(queryMain);
0990: assertEquals(result, count);
0991: }
0992:
0993: /**
0994: * ReportQuery with pathExpression in columns
0995: */
0996: public void testReportQueryPathExpression() {
0997: Criteria crit = new Criteria();
0998: Collection results = new Vector();
0999: crit.addLike("articleName", "C%");
1000: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1001: Article.class, crit);
1002: q.setAttributes(new String[] { "productGroup.groupName",
1003: "articleId", "articleName", "price" });
1004:
1005: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1006:
1007: assertNotNull(iter);
1008: while (iter.hasNext()) {
1009: results.add(iter.next());
1010: }
1011: assertEquals(9, results.size());
1012:
1013: // compare with count
1014: int count = broker.getCount(q);
1015: assertEquals(results.size(), count);
1016:
1017: }
1018:
1019: /**
1020: * ReportQuery returning rows with some "Liquor" data ordered by price
1021: */
1022: public void testReportQueryOrderBy() {
1023:
1024: Criteria crit = new Criteria();
1025: Collection results = new Vector();
1026: crit.addEqualTo("productGroup.groupName", "Liquors");
1027: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1028: Article.class, crit);
1029: q.setAttributes(new String[] { "articleId", "articleName",
1030: "price" });
1031: q.addOrderByAscending("price");
1032:
1033: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1034: assertNotNull(iter);
1035: while (iter.hasNext()) {
1036: results.add(iter.next());
1037: }
1038: assertTrue(results.size() > 0);
1039:
1040: // compare with count
1041: int count = broker.getCount(q);
1042: assertEquals(results.size(), count);
1043:
1044: }
1045:
1046: /**
1047: * ReportQuery returning rows with some "Liquor" data ordered by productGroup.groupId
1048: */
1049: public void testReportQueryOrderByNonSelectColumn() {
1050:
1051: Criteria crit = new Criteria();
1052: Collection results = new Vector();
1053: crit.addEqualTo("productGroup.groupName", "Liquors");
1054: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1055: Article.class, crit);
1056: q.setAttributes(new String[] { "articleId", "articleName",
1057: "price" });
1058: q.addOrderByAscending("productGroup.groupId");
1059:
1060: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1061: assertNotNull(iter);
1062: while (iter.hasNext()) {
1063: results.add(iter.next());
1064: }
1065: assertTrue(results.size() > 0);
1066:
1067: // compare with count
1068: int count = broker.getCount(q);
1069: assertEquals(results.size(), count);
1070:
1071: }
1072:
1073: /**
1074: * ReportQuery returning rows with summed stock and price per article group
1075: * The selected columns point to a class having extents.<br>
1076: * The query produces a wrong sql, selecting only rows of the top-class
1077: */
1078: public void testReportQueryGroupByExtents2() {
1079: Criteria crit = new Criteria();
1080: Collection results = new Vector();
1081: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1082: ProductGroup.class, crit);
1083: q.setAttributes(new String[] { "groupName",
1084: "sum(allArticlesInGroup.stock)",
1085: "sum(allArticlesInGroup.price)" });
1086: q.addGroupBy("groupName");
1087:
1088: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1089:
1090: // SELECT A0.KategorieName,sum(A1.Lagerbestand),sum(A1.Einzelpreis)
1091: // FROM Kategorien A0
1092: // LEFT OUTER JOIN artikel A1 ON A0.Kategorie_Nr=A1.Kategorie_Nr
1093: // LEFT OUTER JOIN books A1E2 ON A0.Kategorie_Nr=A1E2.Kategorie_Nr
1094: // LEFT OUTER JOIN cds A1E1 ON A0.Kategorie_Nr=A1E1.Kategorie_Nr
1095: // GROUP BY A0.KategorieName
1096:
1097: assertNotNull(iter);
1098: while (iter.hasNext()) {
1099: results.add(iter.next());
1100: }
1101: assertTrue(results.size() > 0);
1102:
1103: // TODO: resolve
1104: if (ojbSkipKnownIssueProblem("broker.getCount() vs .getReportQueryIteratorByQuery().size()")) {
1105: return;
1106: }
1107:
1108: // compare with count
1109: int count = broker.getCount(q);
1110: assertEquals(results.size(), count); // FAILS !
1111: }
1112:
1113: /**
1114: * ReportQuery returning rows with summed stock and price per article group
1115: * The selected class has the extents.<br>
1116: * The query returns summed values for each row, so there may be multiple rows
1117: * for one productgroup because one query for each extent is executed.
1118: */
1119: public void testReportQueryGroupByExtents1() {
1120: Criteria crit = new Criteria();
1121: Collection results = new Vector();
1122: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1123: Article.class, crit);
1124: q.setAttributes(new String[] { "productGroup.groupName",
1125: "sum(stock)", "sum(price)" });
1126: q.addGroupBy("productGroup.groupName");
1127:
1128: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1129:
1130: // SELECT A1.KategorieName,sum(A0.Lagerbestand),sum(A0.Einzelpreis)
1131: // FROM artikel A0
1132: // INNER JOIN Kategorien A1 ON A0.Kategorie_Nr=A1.Kategorie_Nr
1133: // GROUP BY A1.KategorieName
1134: //
1135: // SELECT A1.KategorieName,sum(A0.Lagerbestand),sum(A0.Einzelpreis)
1136: // FROM cds A0
1137: // INNER JOIN Kategorien A1 ON A0.Kategorie_Nr=A1.Kategorie_Nr
1138: // GROUP BY A1.KategorieName
1139: //
1140: // SELECT A1.KategorieName,sum(A0.Lagerbestand),sum(A0.Einzelpreis)
1141: // FROM books A0
1142: // INNER JOIN Kategorien A1 ON A0.Kategorie_Nr=A1.Kategorie_Nr
1143: // GROUP BY A1.KategorieName
1144:
1145: assertNotNull(iter);
1146: while (iter.hasNext()) {
1147: results.add(iter.next());
1148: }
1149: assertTrue(results.size() > 0);
1150:
1151: // TODO: resolve
1152: if (ojbSkipKnownIssueProblem("broker.getCount() vs .getReportQueryIteratorByQuery().size()")) {
1153: return;
1154: }
1155:
1156: // compare with count
1157: int count = broker.getCount(q);
1158: assertEquals(results.size(), count); // FAILS !
1159: }
1160:
1161: /**
1162: * Read a CD and then read the ProductGroup for the CD
1163: */
1164: public void testInversePathExpression() {
1165: QueryByCriteria query;
1166: Criteria crit;
1167: CdArticle cd;
1168: ProductGroupProxy pg, cdPg;
1169:
1170: crit = new Criteria();
1171: crit.addEqualTo("articleId", new Integer(200));
1172: query = new QueryByCriteria(CdArticle.class, crit);
1173: cd = (CdArticle) broker.getObjectByQuery(query);
1174: cdPg = (ProductGroupProxy) cd.getProductGroup();
1175:
1176: crit = new Criteria();
1177: crit.addEqualTo("allArticlesInGroup.articleId", cd
1178: .getArticleId());
1179: query = new QueryByCriteria(ProductGroup.class, crit);
1180: query.setPathClass("allArticlesInGroup", CdArticle.class);
1181: pg = (ProductGroupProxy) broker.getObjectByQuery(query);
1182:
1183: // this test can only succeed in singlevm mode:
1184: // if (!BrokerHelper.isRunningInServerMode())
1185: // {
1186: // assertNotNull(pg);
1187: // assertNotNull(cdPg);
1188: // assertEquals("ProductGroups should be identical", pg.getRealSubject().toString(), cdPg.getRealSubject().toString());
1189: // }
1190:
1191: assertNotNull(pg);
1192: assertNotNull(cdPg);
1193: assertEquals("ProductGroups should be identical", pg
1194: .getRealSubject().toString(), cdPg.getRealSubject()
1195: .toString());
1196: }
1197:
1198: /**
1199: * prefetch Articles for ProductGroupsWithArray, Does not yet work with
1200: * Arrays
1201: */
1202: public void testPrefetchedArraySingleKey() {
1203: ClassDescriptor cldArticle = broker
1204: .getClassDescriptor(Article.class);
1205: Class articleProxy = cldArticle.getProxyClass();
1206:
1207: //
1208: // use ProductGroup and Articles with disabled Proxy
1209: //
1210: broker.clearCache();
1211: cldArticle.setProxyClass(null);
1212:
1213: Criteria crit = new Criteria();
1214: crit.addLessOrEqualThan("groupId", new Integer(5));
1215: QueryByCriteria q = QueryFactory.newQuery(
1216: ProductGroupWithArray.class, crit);
1217: q.addOrderByDescending("groupId");
1218: q.addPrefetchedRelationship("allArticlesInGroup");
1219:
1220: Collection results = broker.getCollectionByQuery(q);
1221: assertNotNull(results);
1222: assertTrue(results.size() > 0);
1223: ProductGroupWithArray pg = (ProductGroupWithArray) results
1224: .toArray()[0];
1225: int articleSize = pg.getAllArticles().length;
1226: assertTrue(articleSize != 0);
1227: String articleString = Arrays.asList(pg.getAllArticles())
1228: .toString();
1229:
1230: //
1231: // use ProductGroupWithArray and Articles with original Proxy settings
1232: //
1233: broker.clearCache();
1234: cldArticle.setProxyClass(articleProxy);
1235:
1236: crit = new Criteria();
1237: crit.addEqualTo("groupId", new Integer(5));
1238: q = QueryFactory.newQuery(ProductGroupWithArray.class, crit);
1239: results = broker.getCollectionByQuery(q);
1240: ProductGroupWithArray pg2 = (ProductGroupWithArray) results
1241: .toArray()[0];
1242: InterfaceArticle[] articles = pg2.getAllArticles();
1243: assertNotNull("Array of articles should not be null!", articles);
1244: assertTrue("Array should contain more than 0 entries!",
1245: articles.length != 0);
1246:
1247: // force materialization
1248: for (int i = 0; i < articles.length; i++) {
1249: articles[i].getArticleName();
1250: }
1251: int articleSize2 = articles.length;
1252: String articleString2 = Arrays.asList(articles).toString();
1253:
1254: //
1255: // compare prefetched and 'normal' data
1256: //
1257: assertEquals("Check size", articleSize, articleSize2);
1258: assertEquals("Check content", articleString, articleString2);
1259:
1260: }
1261:
1262: /**
1263: * orderby for prefetch Articles of ProductGroups
1264: */
1265: public void testPrefetchedCollectionOrderBy() {
1266: ClassDescriptor cldProductGroup = broker
1267: .getClassDescriptor(ProductGroup.class);
1268: ClassDescriptor cldArticle = broker
1269: .getClassDescriptor(Article.class);
1270: Class productGroupProxy = cldProductGroup.getProxyClass();
1271: Class articleProxy = cldArticle.getProxyClass();
1272: CollectionDescriptor cds = cldProductGroup
1273: .getCollectionDescriptorByName("allArticlesInGroup");
1274:
1275: //
1276: // use ProductGroup and Articles with disabled Proxy
1277: //
1278: cldProductGroup.setProxyClass(null);
1279: cldProductGroup.setProxyClassName(null);
1280: cldArticle.setProxyClass(null);
1281: cldArticle.setProxyClassName(null);
1282: broker.getDescriptorRepository().setClassDescriptor(
1283: cldProductGroup);
1284: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1285:
1286: //
1287: // orderby articleId, ASC
1288: //
1289: broker.clearCache();
1290: cds.getOrderBy().clear();
1291: cds.addOrderBy("articleId", true);
1292:
1293: Criteria crit = new Criteria();
1294: crit.addLessOrEqualThan("groupId", new Integer(5));
1295: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
1296: crit);
1297: q.addOrderByDescending("groupId");
1298: q.addPrefetchedRelationship("allArticlesInGroup");
1299:
1300: Collection results = broker.getCollectionByQuery(q);
1301: assertNotNull(results);
1302: assertTrue(results.size() == 5);
1303: InterfaceProductGroup pg = (InterfaceProductGroup) results
1304: .toArray()[1];
1305: assertNotNull(pg.getAllArticles());
1306: Object articles[] = pg.getAllArticles().toArray();
1307: int articleSize = articles.length;
1308: assertTrue(articleSize == 10);
1309: Article a1 = (Article) articles[0];
1310: Article a2 = (Article) articles[9];
1311: assertTrue(a1.getArticleId().intValue() < a2.getArticleId()
1312: .intValue());
1313:
1314: //
1315: // orderby articleId, DESC
1316: //
1317: broker.clearCache();
1318: cds.getOrderBy().clear();
1319: cds.addOrderBy("articleId", false);
1320:
1321: results = broker.getCollectionByQuery(q);
1322: assertNotNull(results);
1323: assertTrue(results.size() == 5);
1324: pg = (InterfaceProductGroup) results.toArray()[1];
1325: assertNotNull(pg.getAllArticles());
1326: articles = pg.getAllArticles().toArray();
1327: articleSize = articles.length;
1328: assertTrue(articleSize == 10);
1329: Article b1 = (Article) articles[0];
1330: Article b2 = (Article) articles[9];
1331: assertTrue(b1.getArticleId().intValue() > b2.getArticleId()
1332: .intValue());
1333:
1334: assertEquals(a1.getArticleId(), b2.getArticleId());
1335: assertEquals(a2.getArticleId(), b1.getArticleId());
1336:
1337: //
1338: // use ProductGroup and Articles with original Proxy settings
1339: //
1340: cldProductGroup.setProxyClass(productGroupProxy);
1341: cldProductGroup.setProxyClassName(productGroupProxy.getName());
1342: cldArticle.setProxyClass(articleProxy);
1343: cldArticle.setProxyClassName(articleProxy.getName());
1344: broker.getDescriptorRepository().setClassDescriptor(
1345: cldProductGroup);
1346: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1347: }
1348:
1349: /**
1350: * prefetch Articles for ProductGroups
1351: */
1352: public void testPrefetchedCollectionSingleKey() {
1353: ClassDescriptor cldProductGroup = broker
1354: .getClassDescriptor(ProductGroup.class);
1355: ClassDescriptor cldArticle = broker
1356: .getClassDescriptor(Article.class);
1357: Class productGroupProxy = cldProductGroup.getProxyClass();
1358: Class articleProxy = cldArticle.getProxyClass();
1359:
1360: //
1361: // use ProductGroup and Articles with disabled Proxy
1362: //
1363: broker.clearCache();
1364: cldProductGroup.setProxyClass(null);
1365: cldProductGroup.setProxyClassName(null);
1366: cldArticle.setProxyClass(null);
1367: cldArticle.setProxyClassName(null);
1368: broker.getDescriptorRepository().setClassDescriptor(
1369: cldProductGroup);
1370: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1371:
1372: Criteria crit = new Criteria();
1373: crit.addLessOrEqualThan("groupId", new Integer(5));
1374: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
1375: crit);
1376: q.addOrderByDescending("groupId");
1377: q.addPrefetchedRelationship("allArticlesInGroup");
1378:
1379: Collection results = broker.getCollectionByQuery(q);
1380: assertNotNull(results);
1381: assertTrue(results.size() > 0);
1382: InterfaceProductGroup pg = (InterfaceProductGroup) results
1383: .toArray()[0];
1384: assertNotNull(pg.getAllArticles());
1385: int articleSize = pg.getAllArticles().size();
1386: String articleString = pg.getAllArticles().toString();
1387:
1388: //
1389: // use ProductGroup and Articles with original Proxy settings
1390: //
1391: broker.clearCache();
1392: cldProductGroup.setProxyClass(productGroupProxy);
1393: cldProductGroup.setProxyClassName(productGroupProxy.getName());
1394: cldArticle.setProxyClass(articleProxy);
1395: cldArticle.setProxyClassName(articleProxy.getName());
1396: broker.getDescriptorRepository().setClassDescriptor(
1397: cldProductGroup);
1398: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1399:
1400: crit = new Criteria();
1401: crit.addEqualTo("groupId", new Integer(5));
1402: q = QueryFactory.newQuery(ProductGroup.class, crit);
1403: results = broker.getCollectionByQuery(q);
1404: InterfaceProductGroup pg2 = (InterfaceProductGroup) results
1405: .toArray()[0];
1406: // force materialization
1407: for (Iterator it = pg2.getAllArticles().iterator(); it
1408: .hasNext();) {
1409: ((InterfaceArticle) it.next()).getArticleName();
1410: }
1411: int articleSize2 = pg2.getAllArticles().size();
1412: String articleString2 = pg2.getAllArticles().toString();
1413:
1414: //
1415: // compare prefetched and 'normal' data
1416: //
1417: assertEquals("Check size", articleSize, articleSize2);
1418: assertEquals("Check content", articleString, articleString2);
1419: }
1420:
1421: /**
1422: * Test nested joins using pathExpressions
1423: */
1424: public void testNestedJoins() {
1425: ArrayList list = new java.util.ArrayList();
1426:
1427: Criteria crit = new Criteria();
1428: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1429: Person.class, crit, true);
1430:
1431: q.setAttributes(new String[] { "roles.roleName",
1432: "roles.project.title", "firstname", });
1433:
1434: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1435: while (iter.hasNext()) {
1436: list.add(iter.next());
1437: }
1438:
1439: assertTrue(list.size() > 0);
1440: }
1441:
1442: /**
1443: * Test multiple non nested joins using pathExpressions
1444: */
1445: public void testMultipleJoins() {
1446: ArrayList list = new java.util.ArrayList();
1447:
1448: Criteria crit = new Criteria();
1449: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1450: Role.class, crit, true);
1451:
1452: q.setAttributes(new String[] { "roleName", "project.title",
1453: "person.firstname", });
1454:
1455: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1456: while (iter.hasNext()) {
1457: list.add(iter.next());
1458: }
1459:
1460: assertTrue(list.size() > 0);
1461: }
1462:
1463: /**
1464: * Test nested joins using pathExpressions *** Fails under hsqldb because
1465: * of join using multiple keys ***
1466: */
1467: public void tesXNestedJoins2() {
1468: ArrayList list = new ArrayList();
1469: Criteria crit = new Criteria();
1470: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1471: Task.class, crit);
1472:
1473: q.setAttributes(new String[] { "role.roleName",
1474: "role.project.title", "role.person.firstname", });
1475:
1476: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1477: while (iter.hasNext()) {
1478: list.add(iter.next());
1479: }
1480: assertTrue(list.size() > 0);
1481: }
1482:
1483: /**
1484: * prefetch ProductGroups for Articles
1485: */
1486: public void testPrefetchedReferencesSingleKey() {
1487: ClassDescriptor cldProductGroup = broker
1488: .getClassDescriptor(ProductGroup.class);
1489: ClassDescriptor cldArticle = broker
1490: .getClassDescriptor(Article.class);
1491: Class productGroupProxy = cldProductGroup.getProxyClass();
1492: Class articleProxy = cldArticle.getProxyClass();
1493:
1494: //
1495: // use ProductGroup and Articles with disabled Proxy
1496: //
1497: broker.clearCache();
1498: cldProductGroup.setProxyClass(null);
1499: cldProductGroup.setProxyClassName(null);
1500: cldArticle.setProxyClass(null);
1501: cldArticle.setProxyClassName(null);
1502: broker.getDescriptorRepository().setClassDescriptor(
1503: cldProductGroup);
1504: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1505:
1506: Criteria crit = new Criteria();
1507: crit.addNotNull("productGroupId");
1508: crit.addLessOrEqualThan("productGroupId", new Integer(5));
1509: QueryByCriteria q = QueryFactory.newQuery(Article.class, crit);
1510: q.addOrderByDescending("productGroupId");
1511: q.addPrefetchedRelationship("productGroup");
1512:
1513: Collection results = broker.getCollectionByQuery(q);
1514: Set pgs = new HashSet();
1515: Iterator iter = results.iterator();
1516: while (iter.hasNext()) {
1517: InterfaceArticle a = (InterfaceArticle) iter.next();
1518: pgs.add(a.getProductGroup().getName());
1519: }
1520:
1521: assertTrue(pgs.size() > 0);
1522: String pgsString = pgs.toString();
1523:
1524: //
1525: // use ProductGroup and Articles with original Proxy settings
1526: //
1527: broker.clearCache();
1528: cldProductGroup.setProxyClass(productGroupProxy);
1529: cldProductGroup.setProxyClassName(productGroupProxy.getName());
1530: cldArticle.setProxyClass(articleProxy);
1531: cldArticle.setProxyClassName(articleProxy.getName());
1532: broker.getDescriptorRepository().setClassDescriptor(
1533: cldProductGroup);
1534: broker.getDescriptorRepository().setClassDescriptor(cldArticle);
1535:
1536: crit = new Criteria();
1537: crit.addNotNull("productGroupId");
1538: crit.addLessOrEqualThan("productGroupId", new Integer(5));
1539: q = QueryFactory.newQuery(Article.class, crit);
1540: q.addOrderByDescending("productGroupId");
1541:
1542: results = broker.getCollectionByQuery(q);
1543: Set pgs2 = new HashSet();
1544: iter = results.iterator();
1545: while (iter.hasNext()) {
1546: InterfaceArticle a = (InterfaceArticle) iter.next();
1547: pgs2.add(a.getProductGroup().getName());
1548: }
1549:
1550: assertTrue(pgs2.size() > 0);
1551: String pgsString2 = pgs2.toString();
1552:
1553: //
1554: // compare prefetched and 'normal' data
1555: //
1556: assertEquals("Check size", pgs.size(), pgs2.size());
1557: assertEquals("Check content", pgsString, pgsString2);
1558:
1559: }
1560:
1561: /**
1562: * test PathExpression pointing to abstract class (InterfaceArticle)
1563: */
1564: public void testReportPathExpressionAbstractExtent() {
1565: // TODO: make path expressions extent aware
1566: if (ojbSkipKnownIssueProblem("Make path expressions extent aware"))
1567: return;
1568:
1569: ArrayList list = new ArrayList();
1570: Criteria crit = new Criteria();
1571: crit.addEqualTo("groupId", new Integer(5));
1572:
1573: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1574: ProductGroupWithAbstractArticles.class, crit, true);
1575: q.setAttributes(new String[] { "groupId", "groupName",
1576: "allArticlesInGroup.articleId",
1577: "allArticlesInGroup.articleName" });
1578:
1579: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1580: while (iter.hasNext()) {
1581: list.add(iter.next());
1582: }
1583:
1584: // 7 Articles, 2 Books, 3 Cds
1585: // BRJ: path expression is not yet extent aware
1586: assertEquals("check size", 12, list.size());
1587: }
1588:
1589: /**
1590: * ReportQuery returning rows with some "Liquor" data ordered by productGroup.groupId
1591: */
1592: public void testQueryOrderByNonSelectColumn() {
1593:
1594: Criteria crit = new Criteria();
1595: Collection results = new Vector();
1596: crit.addEqualTo("productGroup.groupName", "Liquors");
1597: QueryByCriteria q = QueryFactory.newQuery(Article.class, crit);
1598: q.addOrderByAscending("productGroup.groupId");
1599:
1600: Iterator iter = broker.getIteratorByQuery(q);
1601: assertNotNull(iter);
1602: while (iter.hasNext()) {
1603: results.add(iter.next());
1604: }
1605: assertTrue(results.size() > 0);
1606:
1607: }
1608:
1609: /**
1610: * test PathExpression pointing to abstract class (InterfaceArticle)
1611: */
1612: public void testPathExpressionForAbstractExtent() {
1613: ArrayList list = new java.util.ArrayList();
1614:
1615: Criteria crit = new Criteria();
1616: crit.addLike("allArticlesInGroup.articleName", "Chef%");
1617: Criteria crit1 = new Criteria();
1618: crit1.addEqualTo("allArticlesInGroup.articleName", "Faust");
1619: crit.addOrCriteria(crit1);
1620:
1621: QueryByCriteria q = QueryFactory.newQuery(
1622: ProductGroupWithAbstractArticles.class, crit, true);
1623: q.addOrderByAscending("groupId");
1624:
1625: Iterator iter = broker.getIteratorByQuery(q);
1626: while (iter.hasNext()) {
1627: list.add(iter.next());
1628: }
1629:
1630: // Groups 2, 5
1631: assertEquals("check size", 2, list.size());
1632:
1633: assertEquals("Group 2", 2,
1634: ((InterfaceProductGroup) list.get(0)).getId()
1635: .intValue());
1636: assertEquals("Group 5", 5,
1637: ((InterfaceProductGroup) list.get(1)).getId()
1638: .intValue());
1639: }
1640:
1641: /**
1642: * Test pathExpression and Extents
1643: */
1644: public void testReportPathExpressionForExtents1() {
1645: ArrayList list = new java.util.ArrayList();
1646:
1647: Criteria crit = new Criteria();
1648: crit.addGreaterOrEqualThan("allArticlesInGroup.articleId",
1649: new Integer(1));
1650: crit.addLessOrEqualThan("allArticlesInGroup.articleId",
1651: new Integer(5));
1652:
1653: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1654: ProductGroup.class, crit, true);
1655: q.setAttributes(new String[] { "groupId", "groupName",
1656: "allArticlesInGroup.articleId" });
1657:
1658: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1659: while (iter.hasNext()) {
1660: Object row;
1661: Object[] columns;
1662:
1663: assertNotNull(
1664: "Invalid ReportQueryIterator, hasNext() is true but next() is null",
1665: row = iter.next());
1666: assertTrue("ReportQuery result row is not Object[]",
1667: row instanceof Object[]);
1668: columns = (Object[]) row;
1669: list.add(columns);
1670:
1671: assertTrue(
1672: "ReportQuery result row does not contain all expected columns",
1673: columns.length == 3);
1674:
1675: /*
1676: arminw:
1677: think hsql returns the wrong result or interpret the query in wrong
1678: way (e.g. using hashcode of values instead values itself), so skip test
1679: evaluation for this DB
1680: */
1681: if (!broker.serviceConnectionManager()
1682: .getSupportedPlatform().getClass().equals(
1683: PlatformHsqldbImpl.class)) {
1684: // System.out.println("### " + ((Object[]) obj)[0]
1685: // + " " + ((Object[]) obj)[1]
1686: // + " " + ((Object[]) obj)[2]);
1687: Object articleId = columns[2];
1688: int i = -1;
1689: if (articleId instanceof Integer) {
1690: i = ((Integer) articleId).intValue();
1691: } else if (articleId instanceof BigDecimal) {
1692: i = ((BigDecimal) articleId).intValue();
1693: } else {
1694: assertTrue(
1695: "TODO: Your platforms resulting class for INTEGER ("
1696: + articleId.getClass().getName()
1697: + ") is not yet supported in testcase.",
1698: false);
1699: }
1700:
1701: assertTrue("i=" + i, i < 6 & i > 0);
1702: }
1703: }
1704: }
1705:
1706: /**
1707: * Test pathExpression and Extents
1708: */
1709: public void testReportPathExpressionForExtents2() {
1710: ArrayList list = new java.util.ArrayList();
1711:
1712: Criteria crit = new Criteria();
1713: crit.addLike("groupName", "%o%");
1714: crit.addLike("allArticlesInGroup.articleName", "%\u00f6%"); //unicode for �
1715:
1716: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1717: ProductGroup.class, crit, true);
1718: q.setAttributes(new String[] { "groupId", "groupName" });
1719:
1720: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1721: while (iter.hasNext()) {
1722: list.add(iter.next());
1723: }
1724:
1725: // Groups: 3 Books , 1 Dairy Product
1726: assertEquals("check size", 2, list.size());
1727: }
1728:
1729: /**
1730: * ReportQuery with Expression in column need to add table alias to the field (price)
1731: **/
1732: public void testReportQueryExpressionInStatement() {
1733: // TODO: Resolve attributes of expressions
1734: if (ojbSkipKnownIssueProblem("Resolve attributes of expressions"))
1735: return;
1736:
1737: Criteria crit = new Criteria();
1738: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1739: Article.class, crit);
1740: q.setAttributes(new String[] { "articleId", "price+10" });
1741: ClassDescriptor cd = broker
1742: .getClassDescriptor(q.getBaseClass());
1743: SqlGenerator sqlg = broker.serviceSqlGenerator();
1744: String sql = sqlg.getPreparedSelectStatement(q, cd)
1745: .getStatement();
1746:
1747: assertTrue(
1748: "Bad query generated. the 'price' field has not table prefix. SQL Output: "
1749: + sql,
1750: sql
1751: .equalsIgnoreCase("SELECT A0.Artikel_Nr,A0.Einzelpreis+10 FROM Artikel A0"));
1752: }
1753:
1754: /**
1755: * Test pathExpression and Extents
1756: */
1757: public void testPathExpressionForExtents1() {
1758: ArrayList list = new java.util.ArrayList();
1759:
1760: Criteria crit = new Criteria();
1761: crit.addEqualTo("allArticlesInGroup.productGroupId",
1762: new Integer(5));
1763:
1764: Query q = QueryFactory.newQuery(ProductGroup.class, crit, true);
1765:
1766: Iterator iter = broker.getIteratorByQuery(q);
1767: while (iter.hasNext()) {
1768: list.add(iter.next());
1769: }
1770:
1771: // ProductGroup 5
1772: assertEquals("check size", 1, list.size());
1773: }
1774:
1775: /**
1776: * Test pathExpression and Extents
1777: */
1778: public void testPathExpressionForExtents2() {
1779: ArrayList list = new java.util.ArrayList();
1780:
1781: Criteria crit = new Criteria();
1782: crit.addLike("upper(allArticlesInGroup.articleName)", "F%");
1783:
1784: Query q = QueryFactory.newQuery(ProductGroup.class, crit, true);
1785:
1786: Iterator iter = broker.getIteratorByQuery(q);
1787: while (iter.hasNext()) {
1788: list.add(iter.next());
1789: }
1790:
1791: // ProductGroups 4 and 5
1792: assertEquals("check size", 2, list.size());
1793:
1794: }
1795:
1796: /**
1797: * Test pathExpression and Extents musicians is only defined in CD
1798: */
1799: public void testPathExpressionForExtents3() {
1800: ArrayList list = new java.util.ArrayList();
1801:
1802: Criteria crit = new Criteria();
1803: crit.addLike("allArticlesInGroup.musicians", "%");
1804:
1805: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
1806: crit, true);
1807: q.setPathClass("allArticlesInGroup", CdArticle.class);
1808:
1809: Iterator iter = broker.getIteratorByQuery(q);
1810: while (iter.hasNext()) {
1811: list.add(iter.next());
1812: }
1813:
1814: // ProductGroups 5
1815: assertEquals("check size", 1, list.size());
1816: }
1817:
1818: /**
1819: * Test pathExpression and Extents Abstract Base
1820: */
1821: public void testPathExpressionForExtents4() {
1822: ArrayList list = new java.util.ArrayList();
1823:
1824: Criteria crit = new Criteria();
1825: crit.addEqualTo("allArticlesInGroup.productGroupId",
1826: new Integer(5));
1827:
1828: Query q = QueryFactory.newQuery(AbstractProductGroup.class,
1829: crit, true);
1830:
1831: Iterator iter = broker.getIteratorByQuery(q);
1832: while (iter.hasNext()) {
1833: list.add(iter.next());
1834: }
1835:
1836: // ProductGroup 5
1837: assertEquals("check size", 1, list.size());
1838: }
1839:
1840: /**
1841: * Test pathExpression and Extents using Alias
1842: */
1843: public void testPathExpressionForExtentsAlias() {
1844: ArrayList list = new java.util.ArrayList();
1845:
1846: Criteria crit1 = new Criteria();
1847: crit1.setAlias("NAMES");
1848: crit1.addLike("upper(allArticlesInGroup.articleName)", "F%");
1849:
1850: Criteria crit2 = new Criteria();
1851: crit2.addGreaterOrEqualThan("allArticlesInGroup.stock",
1852: new Integer(110));
1853:
1854: crit1.addAndCriteria(crit2);
1855: Query q = QueryFactory
1856: .newQuery(ProductGroup.class, crit1, true);
1857:
1858: Iterator iter = broker.getIteratorByQuery(q);
1859: while (iter.hasNext()) {
1860: list.add(iter.next());
1861: }
1862:
1863: // ProductGroup 4
1864: assertEquals("check size", 1, list.size());
1865: }
1866:
1867: /**
1868: * Test ReportQuery and Alias
1869: */
1870: public void testReportQueryAlias() {
1871: ArrayList list = new java.util.ArrayList();
1872:
1873: Criteria crit1 = new Criteria();
1874: crit1.setAlias("NAMES");
1875: crit1.addLike("upper(allArticlesInGroup.articleName)", "F%");
1876:
1877: Criteria crit2 = new Criteria();
1878: crit2.setAlias("STOCKS");
1879: crit2.addGreaterOrEqualThan("allArticlesInGroup.stock",
1880: new Integer(110));
1881:
1882: crit1.addAndCriteria(crit2);
1883: ReportQueryByCriteria q = QueryFactory.newReportQuery(
1884: ProductGroup.class, crit1);
1885: q.setAttributes(new String[] { "groupId", "groupName",
1886: "STOCKS.allArticlesInGroup.articleName",
1887: "NAMES.allArticlesInGroup.articleName",
1888: "NAMES.allArticlesInGroup.stock" });
1889:
1890: // Due to AliasPrefixes ArticleName is taken from A2 and A1, Stock from A1,
1891: // SELECT A0.Kategorie_Nr,A0.KategorieName,A2.Artikelname,A1.Artikelname,A1.Lagerbestand FROM
1892:
1893: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1894: while (iter.hasNext()) {
1895: list.add(iter.next());
1896: }
1897:
1898: // ProductGroup 4 with it's Articles
1899: assertEquals("check size", 1, list.size());
1900: }
1901:
1902: /**
1903: * Run a query range test that includes one record less than the total
1904: * number of records that exist.
1905: */
1906: public void testQueryRangeOneLessThanTotal() {
1907: this .runQueryRangeTest(-1);
1908: }
1909:
1910: /**
1911: * Run a query range test that includes all of the records that exist.
1912: */
1913: public void testQueryRangeAllRecords() {
1914: this .runQueryRangeTest(0);
1915: }
1916:
1917: /**
1918: * Run a query range test.
1919: */
1920: public void testQueryRangeMassTest() {
1921: String name = "testQueryRangeMassTest_"
1922: + System.currentTimeMillis();
1923: int objCount = 2000;
1924:
1925: broker.beginTransaction();
1926: for (int i = 0; i < objCount; i++) {
1927: Gourmet a = new Gourmet();
1928: a.setName(name);
1929: broker.store(a);
1930: }
1931: broker.commitTransaction();
1932:
1933: Criteria crit = new Criteria();
1934: crit.addEqualTo("name", name);
1935: QueryByCriteria q = QueryFactory.newQuery(Gourmet.class, crit);
1936: q.setStartAtIndex(100);
1937: q.setEndAtIndex(109);
1938:
1939: StopWatch watch = new StopWatch();
1940: watch.start();
1941: Collection c = broker.getCollectionByQuery(q);
1942: watch.stop();
1943: System.out.println("# Query 10 of " + objCount
1944: + " objects take " + watch.getTime() + " ms");
1945: assertNotNull(c);
1946: List result = new ArrayList(c);
1947: assertEquals(10, result.size());
1948:
1949: crit = new Criteria();
1950: crit.addEqualTo("name", name);
1951: q = QueryFactory.newQuery(Gourmet.class, crit);
1952: watch.reset();
1953: watch.start();
1954: c = broker.getCollectionByQuery(q);
1955: watch.stop();
1956: System.out.println("# Query all " + objCount + " objects take "
1957: + watch.getTime() + " ms");
1958: assertNotNull(c);
1959: result = new ArrayList(c);
1960: assertEquals(objCount, result.size());
1961:
1962: broker.beginTransaction();
1963: for (int i = 0; i < result.size(); i++) {
1964: broker.delete(result.get(i));
1965: }
1966: broker.commitTransaction();
1967:
1968: c = broker.getCollectionByQuery(q);
1969: assertNotNull(c);
1970: result = new ArrayList(c);
1971: assertEquals(0, result.size());
1972: }
1973:
1974: /**
1975: * Run a query range test that includes one record more than the total
1976: * number of records that exist.
1977: */
1978: public void testQueryRangeOneMoreThanTotal() {
1979: this .runQueryRangeTest(+1);
1980: }
1981:
1982: /**
1983: * Run a query range test.
1984: *
1985: * @param delta the amount to add to the existing record count when setting
1986: * the start/end index for the query that we'll use.
1987: */
1988: private void runQueryRangeTest(int delta) {
1989:
1990: // How many records are there in the table?
1991: Query countQuery = QueryFactory.newQuery(ProductGroup.class,
1992: null, false);
1993:
1994: // Get the existing record count
1995: int recordCount = broker.getCollectionByQuery(countQuery)
1996: .toArray().length;
1997:
1998: // Build a query that will get the range we're looking for.
1999: Query listQuery = QueryFactory.newQuery(ProductGroup.class,
2000: null, false);
2001: listQuery.setStartAtIndex(1);
2002: listQuery.setEndAtIndex(recordCount + delta);
2003:
2004: // Get the list.
2005: Object[] theObjects = broker.getCollectionByQuery(listQuery)
2006: .toArray();
2007:
2008: // Verify the record count
2009: if (delta <= 0) {
2010: assertEquals("record count", (recordCount + delta),
2011: theObjects.length);
2012: } else {
2013: assertEquals("record count", recordCount, theObjects.length);
2014: }
2015:
2016: // Verify the query size, fullSize is 0
2017: // assertEquals("Query size", recordCount, listQuery.fullSize());
2018: }
2019:
2020: public void testQueryMN_Alias1() throws Exception {
2021: Criteria crit1 = new Criteria();
2022: Criteria crit2 = new Criteria();
2023: QueryByCriteria q;
2024: Collection result;
2025:
2026: broker.clearCache();
2027:
2028: // get persons working for projects OJB and SODA
2029: crit1.addLike("projects.title", "OJB%");
2030: crit1.setAlias("alias1");
2031: crit2.addLike("projects.title", "SODA%");
2032: crit2.setAlias("alias2");
2033: crit1.addAndCriteria(crit2);
2034:
2035: q = QueryFactory.newQuery(Person.class, crit1, true);
2036: result = broker.getCollectionByQuery(q);
2037: assertNotNull(result);
2038: assertTrue(result.size() == 2); // bob, tom
2039: }
2040:
2041: public void testQueryMN_Alias2() throws Exception {
2042: Criteria crit1 = new Criteria();
2043: Criteria crit2 = new Criteria();
2044: QueryByCriteria q;
2045: Collection result1;
2046: Collection result2;
2047:
2048: broker.clearCache();
2049:
2050: // get persons working for projects OJB (alias should have no effect)
2051: crit1.setAlias("myAlias");
2052: crit1.addLike("firstname", "%o%");
2053: crit1.addLike("projects.title", "OJB%");
2054:
2055: q = QueryFactory.newQuery(Person.class, crit1, true);
2056: result1 = broker.getCollectionByQuery(q);
2057: assertNotNull(result1);
2058: assertTrue(result1.size() == 2); // bob, tom
2059:
2060: // WITHOUT ALIAS
2061: // get persons working for projects OJB
2062: crit2.addLike("firstname", "%o%");
2063: crit2.addLike("projects.title", "OJB%");
2064:
2065: q = QueryFactory.newQuery(Person.class, crit2, true);
2066: result2 = broker.getCollectionByQuery(q);
2067: assertNotNull(result2);
2068: assertTrue(result2.size() == 2); // bob, tom
2069:
2070: }
2071:
2072: public void testQueryMN() throws Exception {
2073: Criteria crit1 = new Criteria();
2074: Criteria crit2 = new Criteria();
2075: QueryByCriteria q;
2076: Collection result;
2077:
2078: broker.clearCache();
2079:
2080: // get persons working for projects OJB _or_ SODA
2081: crit1.addLike("projects.title", "OJB%");
2082: crit2.addLike("projects.title", "SODA%");
2083: crit1.addOrCriteria(crit2);
2084:
2085: q = QueryFactory.newQuery(Person.class, crit1, true);
2086: result = broker.getCollectionByQuery(q);
2087: assertNotNull(result);
2088: assertTrue(result.size() == 5); // bob, tom, cindy, albert ,betrand
2089: }
2090:
2091: public void testQueryCommutative12() throws Exception {
2092: Collection result;
2093: Criteria crit1 = new Criteria();
2094: crit1.addEqualTo("articleName", "Hamlet");
2095: crit1
2096: .addEqualTo("productGroup.description",
2097: "Strange Books...");
2098:
2099: Criteria crit2 = new Criteria();
2100: crit2.addEqualTo("stock", new Integer(32));
2101:
2102: Criteria crit3 = new Criteria();
2103: crit3.addEqualTo("stock", new Integer(42));
2104:
2105: crit2.addOrCriteria(crit3);
2106: crit1.addAndCriteria(crit2);
2107: QueryByCriteria qry12 = QueryFactory.newQuery(Article.class,
2108: crit1);
2109: qry12.setPathOuterJoin("productGroup");
2110:
2111: result = broker.getCollectionByQuery(qry12);
2112: assertNotNull(result);
2113:
2114: int count = broker.getCount(qry12);
2115: assertEquals(count, result.size());
2116: }
2117:
2118: public void testQueryCommutative21() {
2119: Collection result;
2120: Criteria crit1 = new Criteria();
2121: crit1.addEqualTo("articleName", "Hamlet");
2122: crit1
2123: .addEqualTo("productGroup.description",
2124: "Strange Books...");
2125:
2126: Criteria crit2 = new Criteria();
2127: crit2.addEqualTo("stock", new Integer(32));
2128:
2129: Criteria crit3 = new Criteria();
2130: crit3.addEqualTo("stock", new Integer(42));
2131:
2132: crit2.addOrCriteria(crit3);
2133: crit2.addAndCriteria(crit1);
2134: QueryByCriteria qry21 = QueryFactory.newQuery(Article.class,
2135: crit2);
2136: qry21.setPathOuterJoin("productGroup");
2137:
2138: result = broker.getCollectionByQuery(qry21);
2139: assertNotNull(result);
2140:
2141: int count = broker.getCount(qry21);
2142: assertEquals(count, result.size());
2143: }
2144:
2145: public void testOuterJoin() {
2146: Article a = new Article();
2147: a.articleName = "Good stuff";
2148: a.productGroup = null;
2149: broker.beginTransaction();
2150: broker.store(a);
2151: broker.commitTransaction();
2152:
2153: Criteria crit = new Criteria();
2154: crit.addLike("articleName", "G%");
2155: crit.addIsNull("productGroup.description");
2156:
2157: QueryByCriteria qry1 = QueryFactory.newQuery(Article.class,
2158: crit);
2159: Collection result1 = broker.getCollectionByQuery(qry1);
2160:
2161: QueryByCriteria qry2 = QueryFactory.newQuery(Article.class,
2162: crit);
2163: qry2.setPathOuterJoin("productGroup");
2164: Collection result2 = broker.getCollectionByQuery(qry2);
2165:
2166: assertEquals(0, result1.size());
2167: assertEquals(1, result2.size());
2168:
2169: broker.beginTransaction();
2170: broker.delete(a);
2171: broker.commitTransaction();
2172: }
2173:
2174: public void testExtentByInterface() {
2175: String name = "testExtentByInterface_"
2176: + System.currentTimeMillis();
2177: Zoo zoo = new Zoo();
2178: zoo.setName(name);
2179: Mammal m1 = new Mammal();
2180: m1.setName(name);
2181: Mammal m2 = new Mammal();
2182: m2.setName(name);
2183: Reptile r1 = new Reptile();
2184: r1.setName(name);
2185: broker.beginTransaction();
2186: broker.store(zoo);
2187: m1.setZooId(zoo.getZooId());
2188: m2.setZooId(zoo.getZooId());
2189: r1.setZooId(zoo.getZooId());
2190: broker.store(m1);
2191: broker.store(m2);
2192: broker.store(r1);
2193: broker.commitTransaction();
2194:
2195: Criteria crit = new Criteria();
2196: crit.addEqualTo("name", name);
2197:
2198: QueryByCriteria queryAnimals = QueryFactory.newQuery(
2199: InterfaceAnimal.class, crit);
2200: QueryByCriteria queryReptile = QueryFactory.newQuery(
2201: Reptile.class, crit);
2202: QueryByCriteria queryMammal = QueryFactory.newQuery(
2203: Mammal.class, crit);
2204:
2205: Collection resultA = broker.getCollectionByQuery(queryAnimals);
2206: Collection resultR = broker.getCollectionByQuery(queryReptile);
2207: Collection resultM = broker.getCollectionByQuery(queryMammal);
2208:
2209: assertEquals(3, resultA.size());
2210: assertEquals(1, resultR.size());
2211: assertEquals(2, resultM.size());
2212:
2213: for (Iterator iterator = resultA.iterator(); iterator.hasNext();) {
2214: InterfaceAnimal a = (InterfaceAnimal) iterator.next();
2215: assertEquals(name, a.getName());
2216:
2217: }
2218: for (Iterator iterator = resultR.iterator(); iterator.hasNext();) {
2219: Reptile a = (Reptile) iterator.next();
2220: assertEquals(name, a.getName());
2221: }
2222: for (Iterator iterator = resultM.iterator(); iterator.hasNext();) {
2223: Mammal a = (Mammal) iterator.next();
2224: assertEquals(name, a.getName());
2225: }
2226:
2227: Reptile reptile = (Reptile) broker
2228: .getObjectByQuery(queryReptile);
2229: Mammal mammal = (Mammal) broker.getObjectByQuery(queryMammal);
2230: assertNotNull(reptile);
2231: assertNotNull(mammal);
2232: }
2233:
2234: }
|