0001: /**
0002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
0003: * I/O sub-system.
0004: * Copyright (C) 2001-2004 France Telecom R&D
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2 of the License, or (at your option) any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0019: *
0020: *
0021: *
0022: * Contact: speedo@objectweb.org
0023: *
0024: * Authors: S. Chassande-Barrioz
0025: *
0026: */package org.objectweb.speedo.runtime.query;
0027:
0028: import java.util.ArrayList;
0029: import java.util.Arrays;
0030: import java.util.Collection;
0031: import java.util.Collections;
0032: import java.util.HashMap;
0033: import java.util.HashSet;
0034: import java.util.Iterator;
0035: import java.util.List;
0036: import java.util.Map;
0037: import java.util.NoSuchElementException;
0038:
0039: import javax.jdo.Extent;
0040: import javax.jdo.JDOException;
0041: import javax.jdo.PersistenceManager;
0042: import javax.jdo.Query;
0043:
0044: import junit.framework.Assert;
0045:
0046: import org.objectweb.speedo.SpeedoTestHelper;
0047: import org.objectweb.speedo.api.ExceptionHelper;
0048: import org.objectweb.speedo.pobjects.basic.BasicA;
0049: import org.objectweb.speedo.pobjects.basic.Product;
0050: import org.objectweb.speedo.pobjects.collection.AMMB;
0051: import org.objectweb.speedo.pobjects.collection.BMMB;
0052: import org.objectweb.speedo.pobjects.collection.Group;
0053: import org.objectweb.speedo.pobjects.collection.Ref2Ref2AMMB;
0054: import org.objectweb.speedo.pobjects.collection.User;
0055: import org.objectweb.speedo.pobjects.inheritance.query.GroupUser;
0056: import org.objectweb.speedo.pobjects.inheritance.query.MailingList;
0057: import org.objectweb.speedo.pobjects.inheritance.query.NewsGroup;
0058: import org.objectweb.speedo.pobjects.inheritance.userCache.Litem;
0059: import org.objectweb.speedo.pobjects.ref.Department;
0060: import org.objectweb.speedo.pobjects.ref.Employee;
0061: import org.objectweb.speedo.pobjects.ref.GeoRef;
0062: import org.objectweb.speedo.pobjects.userid.AutoIncFieldId;
0063: import org.objectweb.speedo.pobjects.userid.Ref2AutoIncFieldId;
0064: import org.objectweb.util.monolog.api.BasicLevel;
0065:
0066: /**
0067: * This test tests the access to 2 fields from the Employee class (name
0068: * and salary), Salary is a Float (with 2 parameters, a String and a Float
0069: * object).
0070: */
0071: public class TestQueries extends SpeedoTestHelper {
0072:
0073: public TestQueries(String name) {
0074: super (name);
0075: }
0076:
0077: protected String getLoggerName() {
0078: return LOG_NAME + ".rt.query.TestQueries";
0079: }
0080:
0081: public void test0Parameter() {
0082: logger.log(BasicLevel.DEBUG, "test0Parameter");
0083: PersistenceManager pm = pmf.getPersistenceManager();
0084: try {
0085: Class empClass = Employee.class;
0086: Query query = pm.newQuery(empClass);
0087: Collection col = (Collection) query.execute();
0088: Iterator it = col.iterator();
0089: while (it.hasNext()) {
0090: Employee e = (Employee) it.next();
0091: Assert.assertNotNull(
0092: "null element in the query result", e);
0093: Assert.assertNotNull(
0094: "null name of a element in the query result",
0095: e.getName());
0096: }
0097: query.close(col);
0098: } catch (JDOException e) {
0099: Exception ie = ExceptionHelper.getNested(e);
0100: logger.log(BasicLevel.ERROR, "", ie);
0101: fail(ie.getMessage());
0102: } finally {
0103: pm.close();
0104: }
0105: }
0106:
0107: public void test10rder() {
0108: logger.log(BasicLevel.DEBUG, "test10rder");
0109: PersistenceManager pm = pmf.getPersistenceManager();
0110: try {
0111: Class empClass = Employee.class;
0112: Query query = pm.newQuery(empClass);
0113: query.setOrdering("name ascending");
0114: Collection col = (Collection) query.execute();
0115: Iterator it = col.iterator();
0116: int i = 0;
0117: while (it.hasNext()) {
0118: assertTrue("More result than expected ",
0119: i < POBuilder.nameOrder.length);
0120: Employee e = (Employee) it.next();
0121: Assert.assertNotNull(
0122: "null element in the query result", e);
0123: try {
0124: Assert.assertEquals("Bad order (" + i + ")",
0125: POBuilder.names[POBuilder.nameOrder[i]], e
0126: .getName());
0127: } catch (Throwable e1) {
0128: e1.printStackTrace();
0129: }
0130: i++;
0131: }
0132: assertEquals("Not enough result",
0133: POBuilder.nameOrder.length, i);
0134: query.close(col);
0135: } catch (JDOException e) {
0136: Exception ie = ExceptionHelper.getNested(e);
0137: logger.log(BasicLevel.ERROR, "", ie);
0138: fail(ie.getMessage());
0139: } finally {
0140: pm.close();
0141: }
0142: }
0143:
0144: public void test20rder() {
0145: logger.log(BasicLevel.DEBUG, "test20rder");
0146: PersistenceManager pm = pmf.getPersistenceManager();
0147: Class empClass = Employee.class;
0148: Query query = pm.newQuery(empClass);
0149: Collection col = null;
0150: try {
0151: query.setOrdering("salary descending, name ascending");
0152: col = (Collection) query.execute();
0153: Iterator it = col.iterator();
0154: int i = 0;
0155: while (it.hasNext()) {
0156: assertTrue("More result than expected ",
0157: i < POBuilder.salariesOrder.length);
0158: Assert.assertTrue("No enough values in the result", it
0159: .hasNext());
0160: Employee e = (Employee) it.next();
0161: Assert.assertNotNull(
0162: "null element in the query result", e);
0163: try {
0164: Assert
0165: .assertEquals(
0166: "Bad order (" + i + ")",
0167: POBuilder.names[POBuilder.salariesOrder[i]],
0168: e.getName());
0169: } catch (Throwable e1) {
0170: e1.printStackTrace();
0171: }
0172: i++;
0173: }
0174: assertEquals("Not enough result",
0175: POBuilder.salariesOrder.length, i);
0176: } catch (JDOException e) {
0177: Exception ie = ExceptionHelper.getNested(e);
0178: logger.log(BasicLevel.ERROR, "", ie);
0179: fail(ie.getMessage());
0180: } finally {
0181: try {
0182: query.closeAll();
0183: } finally {
0184: pm.close();
0185: }
0186: }
0187: }
0188:
0189: public void test1Parameter() {
0190: logger.log(BasicLevel.DEBUG, "test1Parameter");
0191: PersistenceManager pm = pmf.getPersistenceManager();
0192: try {
0193: Class empClass = Employee.class;
0194: Query query = pm.newQuery(empClass);
0195: query.declareParameters("String aName");
0196: query.setFilter("(name == aName)");
0197: Collection col = (Collection) query
0198: .execute(POBuilder.names[1]);
0199: Iterator iter = col.iterator();
0200: Assert.assertTrue("The query result is empty", iter
0201: .hasNext());
0202: Employee e = (Employee) iter.next();
0203: Assert.assertNotNull("Null collection element", e);
0204: Assert.assertEquals("Bad employee name",
0205: POBuilder.names[1], e.getName());
0206: Assert.assertTrue(
0207: "More than one element in the query result", !iter
0208: .hasNext());
0209: Assert.assertEquals("Bad query result size", 1, col.size());
0210: query.close(col);
0211: } catch (JDOException e) {
0212: Exception ie = ExceptionHelper.getNested(e);
0213: logger.log(BasicLevel.ERROR, "", ie);
0214: fail(ie.getMessage());
0215: } finally {
0216: pm.close();
0217: }
0218: }
0219:
0220: public void test1ParameterBis() {
0221: logger.log(BasicLevel.DEBUG, "test1ParameterBis");
0222: PersistenceManager pm = pmf.getPersistenceManager();
0223: try {
0224: Class empClass = Employee.class;
0225: Query query = pm.newQuery(empClass);
0226: query.declareParameters("float sal");
0227: query.setFilter("(salary > sal)");
0228: Collection col = (Collection) query.execute(new Float(
0229: POBuilder.salaries[2]));
0230: Iterator iter = col.iterator();
0231: HashSet s = new HashSet();
0232: while (iter.hasNext()) {
0233: Employee e = (Employee) iter.next();
0234: s.add(e.getName());
0235: }
0236: query.close(col);
0237: assertSameCollection("Bad query result", Arrays
0238: .asList(new String[] { POBuilder.names[0],
0239: POBuilder.names[1], POBuilder.names[3] }),
0240: s);
0241: } catch (JDOException e) {
0242: Exception ie = ExceptionHelper.getNested(e);
0243: logger.log(BasicLevel.ERROR, "", ie);
0244: fail(ie.getMessage());
0245: } finally {
0246: pm.close();
0247: }
0248: }
0249:
0250: public void testFieldParameter() {
0251: logger.log(BasicLevel.DEBUG, "testFieldParameter");
0252: PersistenceManager pm = pmf.getPersistenceManager();
0253: try {
0254: Class empClass = Employee.class;
0255: Query query = pm.newQuery(empClass);
0256:
0257: String filterFieldParameter = "((salary == mysalary) && (name == myname))";
0258: String param = "String myname, Float mysalary";
0259:
0260: query.declareParameters(param);
0261: query.setFilter(filterFieldParameter);
0262:
0263: Collection col = (Collection) query.execute(
0264: POBuilder.names[1], new Float(3000.0));
0265: Iterator iter = col.iterator();
0266: Assert.assertTrue("The query result is empty", iter
0267: .hasNext());
0268: Employee e = (Employee) iter.next();
0269: Assert.assertNotNull("Null collection element", e);
0270: Assert.assertEquals("Bad employee name",
0271: POBuilder.names[1], e.getName());
0272: Assert.assertEquals("Bad employee salary",
0273: new Float(3000.0), new Float(e.getSalary()));
0274: Assert.assertTrue(
0275: "More than one element in the query result", !iter
0276: .hasNext());
0277: Assert.assertEquals("Bad query result size", 1, col.size());
0278: query.close(col);
0279: } catch (JDOException e) {
0280: Exception ie = ExceptionHelper.getNested(e);
0281: logger.log(BasicLevel.ERROR, "", ie);
0282: fail(ie.getMessage());
0283: } finally {
0284: pm.close();
0285: }
0286: }
0287:
0288: public void testMatches() {
0289: logger.log(BasicLevel.DEBUG, "testMatches");
0290: PersistenceManager pm = pmf.getPersistenceManager();
0291: try {
0292: Query query = pm.newQuery(Employee.class);
0293: String filterFieldParameter = "((name.toUpperCase().matches(\"%IE\")) && (name.length()>2))";
0294: query.setFilter(filterFieldParameter);
0295: ArrayList col = new ArrayList((Collection) query.execute());
0296: query.closeAll();
0297: for (int i = 0; i < col.size(); i++) {
0298: col.add(i, ((Employee) col.remove(i)).getName());
0299: }
0300: assertSameCollection(
0301: "Bad result of the testMatches method", Arrays
0302: .asList(new String[] { POBuilder.names[2],
0303: POBuilder.names[3] }), col);
0304: } catch (JDOException e) {
0305: Exception ie = ExceptionHelper.getNested(e);
0306: logger.log(BasicLevel.ERROR, "", ie);
0307: fail(ie.getMessage());
0308: } finally {
0309: pm.close();
0310: }
0311: }
0312:
0313: public void testStartsWith() {
0314: logger.log(BasicLevel.DEBUG, "testStartsWith");
0315: PersistenceManager pm = pmf.getPersistenceManager();
0316: try {
0317: Query query = pm.newQuery(Employee.class);
0318: String filterFieldParameter = "(name.toUpperCase().startsWith(\"JOH\"))";
0319: query.setFilter(filterFieldParameter);
0320: ArrayList col = new ArrayList((Collection) query.execute());
0321: query.closeAll();
0322: for (int i = 0; i < col.size(); i++) {
0323: col.add(i, ((Employee) col.remove(i)).getName());
0324: }
0325: assertSameCollection(
0326: "Bad result of the testStartsWith method",
0327: Arrays.asList(new String[] { POBuilder.names[0] }),
0328: col);
0329: } catch (JDOException e) {
0330: Exception ie = ExceptionHelper.getNested(e);
0331: logger.log(BasicLevel.ERROR, "", ie);
0332: fail(ie.getMessage());
0333: } finally {
0334: pm.close();
0335: }
0336: }
0337:
0338: public void _testSubString() {
0339: //TODO: support Substring operator in parsing
0340: logger.log(BasicLevel.DEBUG, "testMatches");
0341: PersistenceManager pm = pmf.getPersistenceManager();
0342: try {
0343: Query query = pm.newQuery(Employee.class);
0344: String filterFieldParameter = "(name.substring(0 2).matches(\"%ie\"))";
0345: query.setFilter(filterFieldParameter);
0346: ArrayList col = new ArrayList((Collection) query.execute());
0347: query.closeAll();
0348: for (int i = 0; i < col.size(); i++) {
0349: col.add(i, ((Employee) col.remove(i)).getName());
0350: }
0351: assertSameCollection(
0352: "Bad result of the testMatches method", Arrays
0353: .asList(new String[] { POBuilder.names[2],
0354: POBuilder.names[3] }), col);
0355: } catch (JDOException e) {
0356: Exception ie = ExceptionHelper.getNested(e);
0357: logger.log(BasicLevel.ERROR, "", ie);
0358: fail(ie.getMessage());
0359: } finally {
0360: pm.close();
0361: }
0362: }
0363:
0364: public void testUpper() {
0365: logger.log(BasicLevel.DEBUG, "testMatches");
0366: PersistenceManager pm = pmf.getPersistenceManager();
0367: try {
0368: Query query = pm.newQuery(Employee.class);
0369: String filterFieldParameter = "(name.toUpperCase().matches(\"%IE\"))";
0370: query.setFilter(filterFieldParameter);
0371: ArrayList col = new ArrayList((Collection) query.execute());
0372: query.closeAll();
0373: for (int i = 0; i < col.size(); i++) {
0374: col.add(i, ((Employee) col.remove(i)).getName());
0375: }
0376: assertSameCollection(
0377: "Bad result of the testMatches method", Arrays
0378: .asList(new String[] { POBuilder.names[2],
0379: POBuilder.names[3] }), col);
0380: } catch (JDOException e) {
0381: Exception ie = ExceptionHelper.getNested(e);
0382: logger.log(BasicLevel.ERROR, "", ie);
0383: fail(ie.getMessage());
0384: } finally {
0385: pm.close();
0386: }
0387: }
0388:
0389: public void test2Parameters() {
0390: logger.log(BasicLevel.DEBUG, "test2Parameters");
0391: PersistenceManager pm = pmf.getPersistenceManager();
0392: try {
0393: Class empClass = Employee.class;
0394: Query query = pm.newQuery(empClass);
0395: query.declareParameters("String aName, String anotherName");
0396: query
0397: .setFilter("((name == aName) || (name == anotherName))");
0398: Collection col = (Collection) query.execute(
0399: POBuilder.names[1], POBuilder.names[2]);
0400: Iterator iter = col.iterator();
0401: Assert.assertTrue("The query result is empty", iter
0402: .hasNext());
0403: Employee e1 = (Employee) iter.next();
0404: Assert.assertNotNull("Null collection element 1", e1);
0405: Assert.assertTrue("Only one element in the query result",
0406: iter.hasNext());
0407: Employee e2 = (Employee) iter.next();
0408: Assert.assertNotNull("Null collection element 2", e2);
0409: Assert.assertTrue(
0410: "More than one element in the query result", !iter
0411: .hasNext());
0412: Assert.assertEquals("Bad query result size", 2, col.size());
0413: query.close(col);
0414:
0415: if (POBuilder.names[1].equals(e1.getName())) {
0416: Assert.assertEquals("Bad employee name",
0417: POBuilder.names[2], e2.getName());
0418: } else if (POBuilder.names[1].equals(e2.getName())) {
0419: Assert.assertEquals("Bad employee name",
0420: POBuilder.names[2], e1.getName());
0421: } else {
0422: fail("Bad employee name");
0423: }
0424: } catch (JDOException e) {
0425: Exception ie = ExceptionHelper.getNested(e);
0426: logger.log(BasicLevel.ERROR, "", ie);
0427: fail(ie.getMessage());
0428: } finally {
0429: pm.close();
0430: }
0431: }
0432:
0433: public void test3Parameters() {
0434: logger.log(BasicLevel.DEBUG, "test3Parameters");
0435: PersistenceManager pm = pmf.getPersistenceManager();
0436: try {
0437: Class empClass = Employee.class;
0438: Query query = pm.newQuery(empClass);
0439: String filter = "(name == aName) | (name == anotherName) | (name == thirdName)";
0440: String param = "String aName, String anotherName, String thirdName";
0441: query.declareParameters(param);
0442: query.setFilter(filter);
0443: Collection col = (Collection) query.execute(
0444: POBuilder.names[1], POBuilder.names[2],
0445: POBuilder.names[3]);
0446: Iterator iter = col.iterator();
0447: Collection expectedNames = new ArrayList(3);
0448: Collection foundNames = new ArrayList(3);
0449: expectedNames.add(POBuilder.names[1]);
0450: expectedNames.add(POBuilder.names[2]);
0451: expectedNames.add(POBuilder.names[3]);
0452: Assert.assertTrue("The query result is empty", iter
0453: .hasNext());
0454: Employee e = (Employee) iter.next();
0455: Assert.assertNotNull("Null collection element 1", e);
0456: foundNames.add(e.getName());
0457: Assert.assertTrue("Only one element in the query result",
0458: iter.hasNext());
0459: e = (Employee) iter.next();
0460: Assert.assertNotNull("Null collection element 2", e);
0461: foundNames.add(e.getName());
0462: e = (Employee) iter.next();
0463: Assert.assertNotNull("Null collection element 3", e);
0464: foundNames.add(e.getName());
0465: Assert.assertTrue(
0466: "More than one element in the query result", !iter
0467: .hasNext());
0468: Assert.assertEquals("Bad query result size", expectedNames
0469: .size(), col.size());
0470: query.close(col);
0471:
0472: Assert.assertTrue("Bad Employee names, expected:"
0473: + expectedNames + " / found: " + foundNames,
0474: expectedNames.containsAll(foundNames)
0475: && foundNames.containsAll(expectedNames));
0476: } catch (JDOException e) {
0477: Exception ie = ExceptionHelper.getNested(e);
0478: logger.log(BasicLevel.ERROR, "", ie);
0479: fail(ie.getMessage());
0480: } finally {
0481: pm.close();
0482: }
0483: }
0484:
0485: public void testMapParameters() {
0486: logger.log(BasicLevel.DEBUG, "testMapParameters");
0487: PersistenceManager pm = pmf.getPersistenceManager();
0488: try {
0489: Class empClass = Employee.class;
0490: Query query = pm.newQuery(empClass);
0491:
0492: String filter = "((name == aName) || (name == anotherName))";
0493: String param = "String aName, String anotherName";
0494:
0495: query.declareParameters(param);
0496: query.setFilter(filter);
0497:
0498: Map map = new HashMap();
0499: map.put("aName", POBuilder.names[1]);
0500: map.put("anotherName", POBuilder.names[2]);
0501:
0502: Collection col = (Collection) query.executeWithMap(map);
0503: Iterator iter = col.iterator();
0504: Collection foundNames = new ArrayList(2);
0505: Collection expectedNames = map.values();
0506:
0507: Assert.assertTrue("The query result is empty", iter
0508: .hasNext());
0509: Employee e = (Employee) iter.next();
0510: Assert.assertNotNull("Null collection element 1", e);
0511: foundNames.add(e.getName());
0512: Assert.assertTrue("Only one element in the query result",
0513: iter.hasNext());
0514: e = (Employee) iter.next();
0515: Assert.assertNotNull("Null collection element 2", e);
0516: foundNames.add(e.getName());
0517: Assert.assertTrue(
0518: "More than one element in the query result", !iter
0519: .hasNext());
0520: Assert.assertEquals("Bad query result size", expectedNames
0521: .size(), col.size());
0522: query.close(col);
0523:
0524: Assert.assertTrue("Bad Employee names, expected:"
0525: + expectedNames + " / found: " + foundNames,
0526: expectedNames.containsAll(foundNames)
0527: && foundNames.containsAll(expectedNames));
0528: } catch (JDOException e) {
0529: Exception ie = ExceptionHelper.getNested(e);
0530: logger.log(BasicLevel.ERROR, "", ie);
0531: fail(ie.getMessage());
0532: } finally {
0533: pm.close();
0534: }
0535: }
0536:
0537: public void testArrayParameters() {
0538: logger.log(BasicLevel.DEBUG, "testArrayParameters");
0539: PersistenceManager pm = pmf.getPersistenceManager();
0540: try {
0541: Class empClass = Employee.class;
0542: Query query = pm.newQuery(empClass);
0543:
0544: String filter = "((name == aName) | (name == anotherName))";
0545: String param = "String aName, String anotherName";
0546:
0547: query.declareParameters(param);
0548: query.setFilter(filter);
0549:
0550: Collection expectedNames = new ArrayList(2);
0551: expectedNames.add(POBuilder.names[1]);
0552: expectedNames.add(POBuilder.names[2]);
0553: Collection col = (Collection) query
0554: .executeWithArray(expectedNames.toArray());
0555: Iterator iter = col.iterator();
0556:
0557: Collection foundNames = new ArrayList(2);
0558: Assert.assertTrue("The query result is empty", iter
0559: .hasNext());
0560: Employee e = (Employee) iter.next();
0561: Assert.assertNotNull("Null collection element 1", e);
0562: foundNames.add(e.getName());
0563: Assert.assertTrue("Only one element in the query result",
0564: iter.hasNext());
0565: e = (Employee) iter.next();
0566: Assert.assertNotNull("Null collection element 2", e);
0567: foundNames.add(e.getName());
0568: Assert.assertTrue(
0569: "More than one element in the query result", !iter
0570: .hasNext());
0571: Assert.assertEquals("Bad query result size", expectedNames
0572: .size(), col.size());
0573: query.close(col);
0574:
0575: Assert.assertTrue("Bad Employee names, expected:"
0576: + expectedNames + " / found: " + foundNames,
0577: expectedNames.containsAll(foundNames)
0578: && foundNames.containsAll(expectedNames));
0579: } catch (JDOException e) {
0580: Exception ie = ExceptionHelper.getNested(e);
0581: logger.log(BasicLevel.ERROR, "", ie);
0582: fail(ie.getMessage());
0583: } finally {
0584: pm.close();
0585: }
0586: }
0587:
0588: public void testFieldRef() {
0589: logger.log(BasicLevel.DEBUG, "testFieldRef");
0590: PersistenceManager pm = pmf.getPersistenceManager();
0591: try {
0592: Class empClass = Employee.class;
0593: Query query = pm.newQuery(empClass);
0594: String filterFieldRef = "((dept.name == depName) && (name == \""
0595: + POBuilder.names[0] + "\"))";
0596: String param = "String depName";
0597:
0598: query.declareParameters(param);
0599: query.setFilter(filterFieldRef);
0600:
0601: Collection col = (Collection) query.execute("RD");
0602: Iterator iter = col.iterator();
0603: Assert.assertTrue("The query result is empty", iter
0604: .hasNext());
0605: Employee e = (Employee) iter.next();
0606: Assert.assertNotNull("Null collection element", e);
0607: Assert.assertEquals("Bad employee name",
0608: POBuilder.names[0], e.getName());
0609: Assert.assertTrue(
0610: "More than one element in the query result", !iter
0611: .hasNext());
0612: Assert.assertEquals("Bad query result size", 1, col.size());
0613:
0614: iter = col.iterator();
0615: Assert.assertTrue("The query result is empty", iter
0616: .hasNext());
0617: e = (Employee) iter.next();
0618: Assert.assertNotNull("Null collection element", e);
0619: Assert.assertEquals("Bad employee name",
0620: POBuilder.names[0], e.getName());
0621: Assert.assertTrue(
0622: "More than one element in the query result", !iter
0623: .hasNext());
0624:
0625: query.close(col);
0626: } catch (Exception e) {
0627: Exception ie = ExceptionHelper.getNested(e);
0628: logger.log(BasicLevel.ERROR, "", ie);
0629: fail(ie.getMessage());
0630: } finally {
0631: pm.close();
0632: }
0633: }
0634:
0635: public void testFieldRefNull() {
0636: logger.log(BasicLevel.DEBUG, "testFieldRef");
0637: PersistenceManager pm = pmf.getPersistenceManager();
0638: final int nbEmp = 5;
0639: Object[] oid = new Object[nbEmp];
0640: for (int i = 0; i < nbEmp; i++) {
0641: Employee e = new Employee("testFieldRefNull" + i, null);
0642: pm.makePersistent(e);
0643: oid[i] = pm.getObjectId(e);
0644: }
0645: pm.close();
0646: try {
0647: pm = pmf.getPersistenceManager();
0648: Query query = pm.newQuery(Employee.class);
0649: query.declareParameters("Department d");
0650: query.setFilter("(dept == d)");
0651: Collection col = (Collection) query.execute(null);
0652: int size = col.size();
0653: query.close(col);
0654: assertEquals("Bad number of employee without department",
0655: nbEmp, size);
0656: } catch (Exception e) {
0657: Exception ie = ExceptionHelper.getNested(e);
0658: logger.log(BasicLevel.ERROR, "", ie);
0659: fail(ie.getMessage());
0660: } finally {
0661: pm.currentTransaction().begin();
0662: for (int i = 0; i < nbEmp; i++) {
0663: Employee e = (Employee) pm.getObjectById(oid[i], false);
0664: pm.deletePersistent(e);
0665: }
0666: pm.currentTransaction().commit();
0667: pm.close();
0668: }
0669: }
0670:
0671: public void testThis() {
0672: logger.log(BasicLevel.DEBUG, "testThis");
0673: PersistenceManager pm = pmf.getPersistenceManager();
0674: try {
0675: Class empClass = Employee.class;
0676: Query query = pm.newQuery(empClass);
0677:
0678: String filter = "(this.name == aName)";
0679: String param = "String aName";
0680:
0681: query.declareParameters(param);
0682: query.setFilter(filter);
0683:
0684: Collection col = (Collection) query
0685: .execute(POBuilder.names[1]);
0686: Iterator iter = col.iterator();
0687: Assert.assertTrue("The query result is empty", iter
0688: .hasNext());
0689: Employee e = (Employee) iter.next();
0690: Assert.assertNotNull("Null collection element", e);
0691: Assert.assertEquals("Bad employee name",
0692: POBuilder.names[1], e.getName());
0693: Assert.assertTrue(
0694: "More than one element in the query result", !iter
0695: .hasNext());
0696: Assert.assertEquals("Bad query result size", 1, col.size());
0697: query.close(col);
0698: } catch (Exception e) {
0699: Exception ie = ExceptionHelper.getNested(e);
0700: logger.log(BasicLevel.ERROR, "", ie);
0701: fail(ie.getMessage());
0702: } finally {
0703: pm.close();
0704: }
0705: }
0706:
0707: public void testParamRef() {
0708: logger.log(BasicLevel.DEBUG, "testParamRef");
0709: PersistenceManager pm = pmf.getPersistenceManager();
0710: try {
0711: Query query = pm.newQuery(Department.class);
0712: String filter = "(this.name == \"" + POBuilder.depName
0713: + "\")";
0714: query.setFilter(filter);
0715: Collection col = (Collection) query.execute();
0716: Iterator iter = col.iterator();
0717: Assert.assertTrue("", iter.hasNext());
0718: Department d = (Department) iter.next();
0719: query.closeAll();
0720:
0721: query = pm.newQuery(Employee.class);
0722: filter = "(this.dept == d)";
0723: String param = "Department d";
0724: query.declareParameters(param);
0725: query.setFilter(filter);
0726: col = (Collection) query.execute(d);
0727: iter = col.iterator();
0728: Assert.assertTrue("The query result is empty", iter
0729: .hasNext());
0730: Assert.assertEquals("Bad size", POBuilder.names.length, col
0731: .size());
0732: query.closeAll();
0733: } catch (Exception e) {
0734: Exception ie = ExceptionHelper.getNested(e);
0735: logger.log(BasicLevel.ERROR, "", ie);
0736: fail(ie.getMessage());
0737: } finally {
0738: pm.close();
0739: }
0740: }
0741:
0742: public void testVarContains() {
0743: logger.log(BasicLevel.DEBUG, "testVarContains");
0744: PersistenceManager pm = pmf.getPersistenceManager();
0745: try {
0746: Query query = pm.newQuery(Department.class);
0747: query.setFilter("(depts.contains(name))");
0748: query.declareParameters("Collection depts");
0749: Collection col = (Collection) query.execute(Arrays
0750: .asList(new String[] { POBuilder.depName }));
0751: Assert.assertEquals("Bad size", 1, col.size());
0752: query.closeAll();
0753:
0754: query = pm.newQuery(Department.class);
0755: query.setFilter("(depts.contains(name))");
0756: query.declareParameters("Collection depts");
0757: col = (Collection) query.execute(Arrays
0758: .asList(new String[] { "toto", "titi" }));
0759: Assert.assertEquals("Bad size", 0, col.size());
0760: query.closeAll();
0761: } catch (Exception e) {
0762: Exception ie = ExceptionHelper.getNested(e);
0763: logger.log(BasicLevel.ERROR, "", ie);
0764: fail(ie.getMessage());
0765: } finally {
0766: pm.close();
0767: }
0768: }
0769:
0770: public void testBsContainsParam(long ida, long idb, boolean exist) {
0771: logger.log(BasicLevel.DEBUG, "testBsContainsParam_" + ida + '_'
0772: + idb + '_' + exist);
0773: PersistenceManager pm = pmf.getPersistenceManager();
0774: try {
0775: Query query = pm.newQuery(BMMB.class);
0776: query.setFilter("(idb == param)");
0777: query.declareParameters("long param");
0778: Collection col = (Collection) query.execute(new Long(idb));
0779: Iterator iter = col.iterator();
0780: if (!iter.hasNext()) {
0781: fail("Test not initialized: impossible to load BMMB("
0782: + idb + "): not found");
0783: }
0784: BMMB bmmb = (BMMB) iter.next();
0785: query.close(col);
0786:
0787: // loggerFactory.getLogger("org.objectweb.speedo.rt.query").setIntLevel(BasicLevel.DEBUG);
0788: // loggerFactory.getLogger("org.objectweb.medor").setIntLevel(BasicLevel.DEBUG);
0789: query = pm.newQuery(AMMB.class);
0790:
0791: String filter = "((ida == a) && bs.contains(b))";
0792: query.declareParameters("BMMB b, long a");
0793: query.setFilter(filter);
0794: col = (Collection) query.execute(bmmb, new Long(ida));
0795: iter = col.iterator();
0796: Assert.assertEquals("The BMMB(" + idb + ") was "
0797: + (exist ? "not " : "")
0798: + "found in the collection AMMB(" + ida + ").bs",
0799: exist, iter.hasNext());
0800: if (exist) {
0801: Assert.assertEquals("Not the same value", ida,
0802: ((AMMB) iter.next()).getIda());
0803: }
0804: query.close(col);
0805: } catch (Exception e) {
0806: Exception ie = ExceptionHelper.getNested(e);
0807: logger.log(BasicLevel.ERROR, "", ie);
0808: fail(ie.getMessage());
0809: } finally {
0810: pm.close();
0811: }
0812: }
0813:
0814: public void testBsContains2Param_0_3() {
0815: testBsContains2Param(0, 3);
0816: }
0817:
0818: public void testBsContains2Param(long ida, long idb) {
0819: logger.log(BasicLevel.DEBUG, "testBsContainsParam_" + ida + '_'
0820: + idb);
0821: PersistenceManager pm = pmf.getPersistenceManager();
0822: try {
0823: Query query = pm.newQuery(BMMB.class);
0824: query.setFilter("((idb == param1) || (idb == param2))");
0825: query.declareParameters("long param1, long param2");
0826: Collection col = (Collection) query.execute(new Long(idb),
0827: new Long(ida));
0828: Iterator iter = col.iterator();
0829: if (!iter.hasNext()) {
0830: fail("Test not initialized: impossible to load BMMB("
0831: + idb + "): not found");
0832: }
0833: BMMB bmmb1 = (BMMB) iter.next();
0834: BMMB bmmb2 = (BMMB) iter.next();
0835: query.close(col);
0836:
0837: // loggerFactory.getLogger("org.objectweb.speedo.rt.query").setIntLevel(BasicLevel.DEBUG);
0838: // loggerFactory.getLogger("org.objectweb.medor").setIntLevel(BasicLevel.DEBUG);
0839: query = pm.newQuery(AMMB.class);
0840:
0841: String filter = "((bs.contains(b1)) || (bs.contains(b2)))";
0842: query.declareParameters("BMMB b1, BMMB b2");
0843: query.setFilter(filter);
0844: col = (Collection) query.execute(bmmb1, bmmb2);
0845: iter = col.iterator();
0846: Assert.assertEquals("The BMMB(" + idb
0847: + ") was not found in the collection AMMB.bs",
0848: true, iter.hasNext());
0849: iter.next();
0850: Assert.assertEquals("The BMMB(" + ida
0851: + ") was not found in the collection AMMB.bs",
0852: true, iter.hasNext());
0853: query.close(col);
0854: } catch (Exception e) {
0855: Exception ie = ExceptionHelper.getNested(e);
0856: logger.log(BasicLevel.ERROR, "", ie);
0857: fail(ie.getMessage());
0858: } finally {
0859: pm.close();
0860: }
0861: }
0862:
0863: public void testBsContainsParamA0B0() {
0864: testBsContainsParam(0, 0, true);
0865: }
0866:
0867: public void testBsContainsParamA0B2() {
0868: testBsContainsParam(0, 2, false);
0869: }
0870:
0871: public void testBsContainsVar(long id, Collection aNames) {
0872: String testName = "testBsContainsVar_" + id + '_' + aNames;
0873: logger.log(BasicLevel.DEBUG, testName);
0874: PersistenceManager pm = pmf.getPersistenceManager();
0875: try {
0876: Query query = pm.newQuery(AMMB.class);
0877:
0878: String filter = "(bs.contains(b) && (b.name==name))";
0879: query.declareParameters("String name");
0880: query.declareVariables("BMMB b");
0881: query.setFilter(filter);
0882: Collection col = (Collection) query.execute("B" + id);
0883: Collection res = new ArrayList();
0884: Iterator iter = col.iterator();
0885: while (iter.hasNext()) {
0886: res.add(((AMMB) iter.next()).getName());
0887: }
0888: query.close(col);
0889: assertSameCollection(testName + ":", aNames, res);
0890: } catch (Exception e) {
0891: Exception ie = ExceptionHelper.getNested(e);
0892: logger.log(BasicLevel.ERROR, "", ie);
0893: fail(ie.getMessage());
0894: } finally {
0895: pm.close();
0896: }
0897: }
0898:
0899: public void testBsContainsVarB3EmptyShortPath() {
0900: testBsContainsVar(3, Collections.EMPTY_LIST);
0901: }
0902:
0903: public void testBsContainsVarB0ShortPath() {
0904: testBsContainsVar(0, Arrays.asList(new String[] { "A0", "A1" }));
0905: }
0906:
0907: public void testBsContainsVarB1ShortPath() {
0908: testBsContainsVar(1, Arrays.asList(new String[] { "A0", "A1",
0909: "A2" }));
0910: }
0911:
0912: public void testBsContainsVarLongPath(long id, Collection ids) {
0913: String testName = "testBsContainsVarLongPath_" + id + '_' + ids;
0914: logger.log(BasicLevel.DEBUG, testName);
0915: //NavigatorNodeFactory.logger = logger;
0916: PersistenceManager pm = pmf.getPersistenceManager();
0917: try {
0918: Query query = pm.newQuery(Ref2Ref2AMMB.class);
0919:
0920: String filter = "(ref.ref.bs.contains(b) && (b.name==name))";
0921: query.declareParameters("String name");
0922: query.declareVariables("BMMB b");
0923: query.setFilter(filter);
0924: Collection col = (Collection) query.execute("B" + id);
0925: Collection res = new ArrayList();
0926: Iterator iter = col.iterator();
0927: while (iter.hasNext()) {
0928: res.add(new Long(((Ref2Ref2AMMB) iter.next()).getId()));
0929: }
0930: query.close(col);
0931: assertSameCollection(testName + ":", ids, res);
0932: } catch (Exception e) {
0933: Exception ie = ExceptionHelper.getNested(e);
0934: logger.log(BasicLevel.ERROR, "", ie);
0935: fail(ie.getMessage());
0936: } finally {
0937: pm.close();
0938: }
0939: }
0940:
0941: public void testBsContainsVarB3EmptyLongPath() {
0942: testBsContainsVarLongPath(3, Collections.EMPTY_LIST);
0943: }
0944:
0945: public void testBsContainsVarB0LongPath() {
0946: testBsContainsVarLongPath(0, Arrays.asList(new Long[] {
0947: new Long(0), new Long(100) }));
0948: }
0949:
0950: public void testBsContainsVarB1LongPath() {
0951: testBsContainsVarLongPath(1, Arrays.asList(new Long[] {
0952: new Long(0), new Long(100), new Long(200) }));
0953: }
0954:
0955: public void testBsIsEmpty(long ida, boolean isEmpty) {
0956: logger.log(BasicLevel.DEBUG, "testBsIsEmpty_" + ida + '_'
0957: + isEmpty);
0958: //fail("The isEmpty operator is not support in Medor");
0959: PersistenceManager pm = pmf.getPersistenceManager();
0960: try {
0961: Query query = pm.newQuery(AMMB.class);
0962: String filter = "((ida == a) && bs.isEmpty())";
0963: query.declareParameters("long a");
0964: query.setFilter(filter);
0965: Collection col = (Collection) query.execute(new Long(ida));
0966: Iterator iter = col.iterator();
0967: Assert.assertEquals("The AMMB(" + ida + ").bs is "
0968: + (isEmpty ? "not " : "") + "empty.", isEmpty, iter
0969: .hasNext());
0970: query.close(col);
0971: } catch (Exception e) {
0972: Exception ie = ExceptionHelper.getNested(e);
0973: logger.log(BasicLevel.ERROR, "", ie);
0974: fail(ie.getMessage());
0975: } finally {
0976: pm.close();
0977: }
0978: }
0979:
0980: public void testBsIsEmptyA0() {
0981: testBsIsEmpty(0, false);
0982: }
0983:
0984: public void testBsIsEmptyA3() {
0985: testBsIsEmpty(3, true);
0986: }
0987:
0988: public void testBsIsEmptyWithNot(long ida, boolean hasResult) {
0989: logger.log(BasicLevel.DEBUG, "testBsIsEmptyWithNot_" + ida
0990: + '_' + hasResult);
0991: //fail("The isEmpty operator is not support in Medor");
0992: PersistenceManager pm = pmf.getPersistenceManager();
0993: try {
0994: Query query = pm.newQuery(AMMB.class);
0995: String filter = "(!(ida == a) && bs.isEmpty())";
0996: query.declareParameters("long a");
0997: query.setFilter(filter);
0998: Collection col = (Collection) query.execute(new Long(ida));
0999: Iterator iter = col.iterator();
1000: Assert.assertEquals("It "
1001: + (hasResult ? " does not exist" : "exists ")
1002: + " ida != " + ida + " with empty collection.",
1003: hasResult, iter.hasNext());
1004: query.close(col);
1005: } catch (Exception e) {
1006: Exception ie = ExceptionHelper.getNested(e);
1007: logger.log(BasicLevel.ERROR, "", ie);
1008: fail(ie.getMessage());
1009: } finally {
1010: pm.close();
1011: }
1012: }
1013:
1014: public void testBsIsEmptyA0WithNot() {
1015: testBsIsEmptyWithNot(0, true);
1016: }
1017:
1018: public void testBsIsEmptyA3WithNot() {
1019: testBsIsEmptyWithNot(3, false);
1020: }
1021:
1022: public void testBsIsNotEmpty(long ida, boolean isNotEmpty,
1023: boolean globalNot) {
1024: logger.log(BasicLevel.DEBUG, "testBsIsNotEmpty_" + ida
1025: + "_ise=" + isNotEmpty + "_gn=" + globalNot);
1026: //fail("The isEmpty operator is not support in Medor");
1027: PersistenceManager pm = pmf.getPersistenceManager();
1028: try {
1029: Query query = pm.newQuery(AMMB.class);
1030: String filter = (globalNot ? "(!((ida != a) || bs.isEmpty()))"
1031: : "((ida == a) && !(bs.isEmpty()))");
1032: query.declareParameters("long a");
1033: query.setFilter(filter);
1034: Collection col = (Collection) query.execute(new Long(ida));
1035: Iterator iter = col.iterator();
1036: Assert.assertEquals("The AMMB(" + ida + ").bs is "
1037: + (isNotEmpty ? "" : "not ") + "empty.",
1038: isNotEmpty, iter.hasNext());
1039: query.close(col);
1040: } catch (Exception e) {
1041: Exception ie = ExceptionHelper.getNested(e);
1042: logger.log(BasicLevel.ERROR, "", ie);
1043: fail(ie.getMessage());
1044: } finally {
1045: pm.close();
1046: }
1047: }
1048:
1049: public void testBsIsNotEmptyA0Global() {
1050: testBsIsNotEmpty(0, true, true);
1051: }
1052:
1053: public void testBsIsNotEmptyA0Local() {
1054: testBsIsNotEmpty(0, true, false);
1055: }
1056:
1057: public void testBsIsNotEmptyA3Global() {
1058: testBsIsNotEmpty(3, false, true);
1059: }
1060:
1061: public void testBsIsNotEmptyA3Local() {
1062: testBsIsNotEmpty(3, false, false);
1063: }
1064:
1065: public void testNot(long ida, Collection idsa) {
1066: logger.log(BasicLevel.DEBUG, "testNot_" + ida + '_' + idsa);
1067: PersistenceManager pm = pmf.getPersistenceManager();
1068: try {
1069: Query query = pm.newQuery(AMMB.class);
1070: String filter = "(!(ida == a))";
1071: query.declareParameters("long a");
1072: query.setFilter(filter);
1073: Collection col = (Collection) query.execute(new Long(ida));
1074: ArrayList res = new ArrayList(idsa.size());
1075: Iterator iter = col.iterator();
1076: while (iter.hasNext()) {
1077: res.add(new Long(((AMMB) iter.next()).getIda()));
1078: }
1079: query.close(col);
1080: this .assertSameCollection("", idsa, res);
1081: } catch (Exception e) {
1082: Exception ie = ExceptionHelper.getNested(e);
1083: logger.log(BasicLevel.ERROR, "", ie);
1084: fail(ie.getMessage());
1085: } finally {
1086: pm.close();
1087: }
1088: }
1089:
1090: public void testNotA0() {
1091: testNot(0, Arrays.asList(new Long[] { new Long(1), new Long(2),
1092: new Long(3) }));
1093: }
1094:
1095: public void testExtent(Class clazz, boolean withSubclass,
1096: Collection ids) {
1097: logger.log(BasicLevel.DEBUG, "testExtent_" + clazz.getName()
1098: + "_sc=" + withSubclass);
1099: PersistenceManager pm = pmf.getPersistenceManager();
1100: try {
1101: Extent e = pm.getExtent(clazz, withSubclass);
1102: Assert.assertEquals("Bad candidate class on the extent",
1103: clazz, e.getCandidateClass());
1104: Assert.assertEquals("Bad sub class value on the extent",
1105: withSubclass, e.hasSubclasses());
1106: Iterator it = e.iterator();
1107: ArrayList found = new ArrayList(ids.size());
1108: while (it.hasNext()) {
1109: Object o = it.next();
1110: if (o == null) {
1111: fail("Null object returned by the extent iterator of the class "
1112: + clazz.getName());
1113: } else if (o instanceof AMMB) {
1114: AMMB a = (AMMB) o;
1115: found.add(new Long(a.getIda()));
1116: } else if (o instanceof BMMB) {
1117: BMMB b = (BMMB) o;
1118: found.add(new Long(b.getIdb()));
1119: } else {
1120: fail("the test does not manage the class "
1121: + o.getClass().getName());
1122: }
1123: }
1124: try {
1125: it.remove();
1126: fail("the remove operation does not throw an exception");
1127: } catch (UnsupportedOperationException e1) {
1128: }
1129: assertSameCollection("Bad extent of the class "
1130: + clazz.getName(), ids, found);
1131: e.close(it);
1132: try {
1133: it.hasNext();
1134: fail("the iterator does not throw an exception on the use of "
1135: + "the 'hasNext' method whereas it has been closed");
1136: } catch (NoSuchElementException e1) {
1137: }
1138: try {
1139: it.next();
1140: fail("the iterator does not throw an exception on the use of "
1141: + "the 'next' method whereas it has been closed");
1142: } catch (NoSuchElementException e1) {
1143: }
1144: e = pm.getExtent(clazz, withSubclass);
1145: Iterator[] its = new Iterator[5];
1146: for (int i = 0; i < its.length; i++) {
1147: its[i] = e.iterator();
1148: }
1149: e.closeAll();
1150: for (int i = 0; i < its.length; i++) {
1151: try {
1152: its[i].next();
1153: fail("the iterator "
1154: + i
1155: + " does not throw an exception on "
1156: + "the use of the 'next' method whereas all"
1157: + " iterator have been closed");
1158: } catch (NoSuchElementException e1) {
1159: }
1160: }
1161: it = e.iterator();
1162: for (int i = 0; i < ids.size(); i++) {
1163: try {
1164: it.next();
1165: } catch (NoSuchElementException e1) {
1166: Assert.assertEquals("Bad size: ", ids.size(), i);
1167: }
1168: }
1169: e.close(it);
1170: } catch (Exception e) {
1171: Exception ie = ExceptionHelper.getNested(e);
1172: logger.log(BasicLevel.ERROR, "", ie);
1173: fail(ie.getMessage());
1174: } finally {
1175: pm.close();
1176: }
1177: }
1178:
1179: public void testExtentAMMBfalse() {
1180: ArrayList al = new ArrayList(POBuilder.NB_XMMB);
1181: for (int i = 0; i < POBuilder.NB_XMMB; i++) {
1182: al.add(new Long(i));
1183: }
1184: testExtent(AMMB.class, false, al);
1185: }
1186:
1187: public void testExtentBMMBfalse() {
1188: ArrayList al = new ArrayList(POBuilder.NB_XMMB);
1189: for (int i = 0; i < POBuilder.NB_XMMB; i++) {
1190: al.add(new Long(i));
1191: }
1192: testExtent(BMMB.class, false, al);
1193: }
1194:
1195: public void testQueryBasedOnExtent() {
1196: Class clazz = AMMB.class;
1197: boolean withSubclass = false;
1198: logger.log(BasicLevel.DEBUG, "testExtent_" + clazz.getName()
1199: + "_sc=" + withSubclass);
1200: PersistenceManager pm = pmf.getPersistenceManager();
1201: try {
1202: Extent e = pm.getExtent(clazz, false);
1203: Query q = pm.newQuery(e);
1204: Collection c = (Collection) q.execute();
1205: Assert
1206: .assertEquals("bad size", POBuilder.NB_XMMB, c
1207: .size());
1208: q.close(c);
1209: } catch (Exception e) {
1210: Exception ie = ExceptionHelper.getNested(e);
1211: logger.log(BasicLevel.ERROR, "", ie);
1212: fail(ie.getMessage());
1213: } finally {
1214: pm.close();
1215: }
1216: }
1217:
1218: public void testSequenceIdNavigateToPrimitive() {
1219: logger.log(BasicLevel.DEBUG,
1220: "testSequenceIdNavigateToPrimitive");
1221: PersistenceManager pm = pmf.getPersistenceManager();
1222: try {
1223: Query query = pm.newQuery(Ref2AutoIncFieldId.class);
1224: query.setFilter("(simpleRef.f1 == \"toto\")");
1225: Collection col = (Collection) query.execute();
1226: Iterator iter = col.iterator();
1227: Assert.assertTrue("The query result is empty", !iter
1228: .hasNext());
1229: query.close(col);
1230: } catch (Exception e) {
1231: Exception ie = ExceptionHelper.getNested(e);
1232: logger.log(BasicLevel.ERROR, "", ie);
1233: fail(ie.getMessage());
1234: } finally {
1235: pm.close();
1236: }
1237: }
1238:
1239: public void testSequenceIdNavigateToPrimitive2() {
1240: logger.log(BasicLevel.DEBUG,
1241: "testSequenceIdNavigateToPrimitive");
1242: PersistenceManager pm = pmf.getPersistenceManager();
1243: try {
1244: Query query = pm.newQuery(AutoIncFieldId.class);
1245: query.setFilter("(independantRef.f1 == \"toto\")");
1246: query.setOrdering("independantRef.f1 ascending");
1247: Collection col = (Collection) query.execute();
1248: Iterator iter = col.iterator();
1249: Assert.assertTrue("The query result is empty", !iter
1250: .hasNext());
1251: query.close(col);
1252: } catch (Exception e) {
1253: Exception ie = ExceptionHelper.getNested(e);
1254: logger.log(BasicLevel.ERROR, "", ie);
1255: fail(ie.getMessage());
1256: } finally {
1257: pm.close();
1258: }
1259: }
1260:
1261: public void testNotIgnoreCache() {
1262: logger.log(BasicLevel.DEBUG,
1263: "testSequenceIdNavigateToPrimitive");
1264: PersistenceManager pm = pmf.getPersistenceManager();
1265: BasicA ba = new BasicA();
1266: ba.writeF1("testNotIgnoreCache1");
1267: ba.writeF2(1);
1268: pm.makePersistent(ba);
1269: pm.close();
1270:
1271: pm = pmf.getPersistenceManager();
1272: ba.writeF1("testNotIgnoreCache2");
1273: pm.setIgnoreCache(false);
1274: Assert.assertTrue("Bad ignore cache value", !pm
1275: .getIgnoreCache());
1276: Query query = pm.newQuery(BasicA.class);
1277: query.setFilter("(f1 == \"testNotIgnoreCache2\")");
1278: Collection col = (Collection) query.execute();
1279: assertEquals("The cache is ignored", 1, col.size());
1280: query.close(col);
1281: pm.close();
1282:
1283: pm = pmf.getPersistenceManager();
1284: pm.currentTransaction().begin();
1285: pm.deletePersistent(ba);
1286: pm.currentTransaction().commit();
1287: pm.close();
1288: }
1289:
1290: public void testAvgSingle() {
1291: testAggregateSingle("AVG(salary)", new Float(3250), false);
1292: testAggregateSingle("avg(salary)", new Float(3250), false);
1293: testAggregateSingle("AVG(salary)", new Float(3250), true);
1294: testAggregateSingle("avg(salary)", new Float(3250), true);
1295: }
1296:
1297: public void testSumSingle() {
1298: testAggregateSingle("SUM(salary)", new Float(13000), false);
1299: testAggregateSingle("sum(salary)", new Float(13000), false);
1300: testAggregateSingle("SUM(salary)", new Float(13000), true);
1301: testAggregateSingle("sum(salary)", new Float(13000), true);
1302: }
1303:
1304: public void testMaxSingle() {
1305: testAggregateSingle("MAX(salary)", new Float(4000), false);
1306: testAggregateSingle("max(salary)", new Float(4000), false);
1307: testAggregateSingle("MAX(salary)", new Float(4000), true);
1308: testAggregateSingle("max(salary)", new Float(4000), true);
1309: }
1310:
1311: public void testMinSingle() {
1312: testAggregateSingle("MIN(salary)", new Float(2999), false);
1313: testAggregateSingle("min(salary)", new Float(2999), false);
1314: testAggregateSingle("MIN(salary)", new Float(2999), true);
1315: testAggregateSingle("min(salary)", new Float(2999), true);
1316: }
1317:
1318: public void testCountSingle() {
1319: testAggregateSingle("COUNT(salary)", new Long(4), false);
1320: testAggregateSingle("count(salary)", new Long(4), false);
1321: testAggregateSingle("count(this)", new Long(4), false);
1322: testAggregateSingle("count(*)", new Long(4), false);
1323: testAggregateSingle("COUNT(salary)", new Long(4), true);
1324: testAggregateSingle("count(salary)", new Long(4), true);
1325: testAggregateSingle("count(this)", new Long(4), true);
1326: testAggregateSingle("count(*)", new Long(4), true);
1327: }
1328:
1329: public void testAggregateSingle(String select, Object result,
1330: boolean unique) {
1331: logger.log(BasicLevel.DEBUG, "testAggregateSingle(" + select
1332: + ", " + result + ")");
1333: PersistenceManager pm = pmf.getPersistenceManager();
1334: try {
1335: Query query = pm.newQuery(Employee.class);
1336: query.setResult(select);
1337: query.setUnique(unique);
1338: Object res = query.execute();
1339: if (unique) {
1340: assertEquals("Bad result", result, res);
1341: } else {
1342: Iterator it = ((List) res).iterator();
1343: assertTrue("Not result!", it.hasNext());
1344: Object o = it.next();
1345: assertNotNull("Result is null", o);
1346: assertEquals("Bad result type: ", o.getClass(), result
1347: .getClass());
1348: assertEquals("Bad result value", o, result);
1349: assertTrue("More than one result!", !it.hasNext());
1350: }
1351: query.closeAll();
1352: } catch (JDOException e) {
1353: Exception ie = ExceptionHelper.getNested(e);
1354: logger.log(BasicLevel.ERROR, "", ie);
1355: fail(ie.getMessage());
1356: } finally {
1357: pm.close();
1358: }
1359: }
1360:
1361: public void testSingleField() {
1362: logger.log(BasicLevel.DEBUG, "testSingleField");
1363: PersistenceManager pm = pmf.getPersistenceManager();
1364: try {
1365: Query query = pm.newQuery(Employee.class);
1366: query.setResult("name");
1367: Collection col = (Collection) query.execute();
1368: assertSameCollection("Bad name collection", Arrays
1369: .asList(POBuilder.names), col);
1370: query.close(col);
1371: } catch (JDOException e) {
1372: Exception ie = ExceptionHelper.getNested(e);
1373: logger.log(BasicLevel.ERROR, "", ie);
1374: fail(ie.getMessage());
1375: } finally {
1376: pm.close();
1377: }
1378: }
1379:
1380: public void testSingleFieldRef() {
1381: logger.log(BasicLevel.DEBUG, "testSingleFieldRef");
1382: PersistenceManager pm = pmf.getPersistenceManager();
1383: try {
1384: Query query = pm.newQuery(Employee.class);
1385: query.setResult("dept");
1386: List list = (List) query.execute();
1387: assertEquals("bad collection size", 4, list.size());
1388: assertEquals("Bad dept", POBuilder.depName,
1389: ((Department) list.get(0)).getName());
1390: assertEquals("0!=1", list.get(0), list.get(1));
1391: assertEquals("1!=2", list.get(1), list.get(2));
1392: assertEquals("2!=3", list.get(2), list.get(3));
1393: query.close(list);
1394: } catch (JDOException e) {
1395: Exception ie = ExceptionHelper.getNested(e);
1396: logger.log(BasicLevel.ERROR, "", ie);
1397: fail(ie.getMessage());
1398: } finally {
1399: pm.close();
1400: }
1401: }
1402:
1403: public void testDistinctSingleFieldRef() {
1404: logger.log(BasicLevel.DEBUG, "testDistinctSingleFieldRef");
1405: PersistenceManager pm = pmf.getPersistenceManager();
1406: try {
1407: Query query = pm.newQuery(Employee.class);
1408: query.setResult("distinct dept");
1409: Collection col = (Collection) query.execute();
1410: List list = (List) query.execute();
1411: assertEquals("bad collection size", 1, list.size());
1412: assertEquals("Bad dept", POBuilder.depName,
1413: ((Department) list.get(0)).getName());
1414: query.close(col);
1415: } catch (JDOException e) {
1416: Exception ie = ExceptionHelper.getNested(e);
1417: logger.log(BasicLevel.ERROR, "", ie);
1418: fail(ie.getMessage());
1419: } finally {
1420: pm.close();
1421: }
1422: }
1423:
1424: public static class StringFloat {
1425: public String name;
1426: public Float value;
1427:
1428: public StringFloat(String n, Float v) {
1429: name = n;
1430: value = v;
1431: }
1432: }
1433:
1434: public void testCompositeResultWithUserClass() {
1435: testCompositeResult(true);
1436: }
1437:
1438: public void testCompositeResultWithoutUserClass() {
1439: testCompositeResult(false);
1440: }
1441:
1442: public void testCompositeResult(boolean withUserClassResult) {
1443: logger.log(BasicLevel.DEBUG, "testUserClassResult");
1444: PersistenceManager pm = pmf.getPersistenceManager();
1445: try {
1446: Query query = pm.newQuery(Employee.class);
1447: query.setResult("distinct dept.name, salary");
1448: if (withUserClassResult) {
1449: query.setResultClass(StringFloat.class);
1450: }
1451: Collection col = (Collection) query.execute();
1452: Iterator it = col.iterator();
1453:
1454: query.closeAll();
1455: } catch (JDOException e) {
1456: Exception ie = ExceptionHelper.getNested(e);
1457: logger.log(BasicLevel.ERROR, "", ie);
1458: fail(ie.getMessage());
1459: } finally {
1460: pm.close();
1461: }
1462: }
1463:
1464: public void testCompositeResultAgregWithUserClass() {
1465: testCompositeResultAgreg(true);
1466: }
1467:
1468: public void testCompositeResultAgregWithoutUserClass() {
1469: testCompositeResultAgreg(false);
1470: }
1471:
1472: public void testCompositeResultAgreg(boolean withUserClassResult) {
1473: logger.log(BasicLevel.DEBUG, "testUserClassResult");
1474: PersistenceManager pm = pmf.getPersistenceManager();
1475: try {
1476: Query query = pm.newQuery(Employee.class);
1477: query.setResult("dept.name, avg(salary)");
1478: if (withUserClassResult) {
1479: query.setResultClass(StringFloat.class);
1480: }
1481: query.setGrouping("dept.name");
1482: Collection col = (Collection) query.execute();
1483: Iterator it = col.iterator();
1484:
1485: query.closeAll();
1486: } catch (JDOException e) {
1487: Exception ie = ExceptionHelper.getNested(e);
1488: logger.log(BasicLevel.ERROR, "", ie);
1489: fail(ie.getMessage());
1490: } finally {
1491: pm.close();
1492: }
1493: }
1494:
1495: public void _testCompositeWithRefResultAgregWithUserClass() {
1496: testCompositeWithRefResultAgreg(true);
1497: }
1498:
1499: public void testCompositeWithRefResultAgregWithoutUserClass() {
1500: testCompositeWithRefResultAgreg(false);
1501: }
1502:
1503: public void testCompositeWithRefResultAgreg(
1504: boolean withUserClassResult) {
1505: logger.log(BasicLevel.DEBUG, "testUserClassResult");
1506: PersistenceManager pm = pmf.getPersistenceManager();
1507: try {
1508: Query query = pm.newQuery(Employee.class);
1509: query.setResult("avg(salary)");
1510: if (withUserClassResult) {
1511: query.setResultClass(StringFloat.class);
1512: }
1513: query.setGrouping("dept");
1514: Collection col = (Collection) query.execute();
1515: Iterator it = col.iterator();
1516:
1517: query.closeAll();
1518: } catch (JDOException e) {
1519: Exception ie = ExceptionHelper.getNested(e);
1520: logger.log(BasicLevel.ERROR, "", ie);
1521: fail(ie.getMessage());
1522: } finally {
1523: pm.close();
1524: }
1525: }
1526:
1527: public void testUserCacheSingle() {
1528: logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1529: PersistenceManager pm = pmf.getPersistenceManager();
1530: pm.currentTransaction().begin();
1531: final String n = "testUserCacheSingle_";
1532: for (int i = 0; i < 10; i++) {
1533: Product p = new Product(i);
1534: p.setProductName(n + i);
1535: pm.makePersistent(p);
1536: }
1537: pm.currentTransaction().commit();
1538: try {
1539: pm.currentTransaction().begin();
1540: Query query = pm.newQuery(Product.class);
1541: query.setFilter("productName == p");
1542: query.declareParameters("String p");
1543: query.setUnique(true);
1544: final String id = n + 3;
1545: Product p = (Product) query.execute(id); //no query
1546: assertNotNull("Null value for " + id, p);
1547: p.setProductName(id + "bis");
1548:
1549: //Check that the old value is unbound
1550: p = (Product) query.execute(id); //query
1551: assertNull("Old value not unbound from the user cache, "
1552: + id, p);
1553:
1554: p = (Product) query.execute(id + "bis");//query because the cache
1555: // does not contains the old value neither the new value
1556: assertNotNull("Null value for " + id + "bis", p);
1557:
1558: p = (Product) query.execute(n + 10); //query because the object does not exist
1559: assertNull("Non null value for " + n + 10, p);
1560: pm.currentTransaction().commit();
1561: //At this time, the user cache has been updated with the modified Product
1562:
1563: pm.currentTransaction().begin();
1564: p = (Product) query.execute(id + "bis"); //no query
1565: assertNotNull("Null value for " + id + "bis", p);
1566: pm.currentTransaction().commit();
1567:
1568: pm.evictAll();
1569:
1570: pm.currentTransaction().begin();
1571: p = (Product) query.execute(id + "bis"); //query
1572: assertNotNull("Null value for " + id + "bis", p);
1573: pm.currentTransaction().commit();
1574:
1575: pm.currentTransaction().begin();
1576: Extent e = pm.getExtent(Product.class);
1577: for (Iterator iter = e.iterator(); iter.hasNext();) {
1578: pm.deletePersistent(iter.next());
1579: }
1580: pm.currentTransaction().commit();
1581: } catch (JDOException e) {
1582: Exception ie = ExceptionHelper.getNested(e);
1583: logger.log(BasicLevel.ERROR, "", ie);
1584: fail(ie.getMessage());
1585: } finally {
1586: pm.close();
1587: }
1588:
1589: }
1590:
1591: public void testUserCacheInheritance() {
1592: logger.log(BasicLevel.DEBUG, "testUserCacheInheritance");
1593: PersistenceManager pm = pmf.getPersistenceManager();
1594: pm.currentTransaction().begin();
1595: final String n = "testUserCacheInheritance_";
1596: for (int i = 0; i < 10; i++) {
1597: org.objectweb.speedo.pobjects.inheritance.userCache.GeoRef geoRef = new org.objectweb.speedo.pobjects.inheritance.userCache.GeoRef(
1598: n + i);
1599: pm.makePersistent(geoRef);
1600: }
1601: pm.currentTransaction().commit();
1602: try {
1603: pm.currentTransaction().begin();
1604: Extent e = pm.getExtent(Litem.class, true);//with subclasses
1605: Query query = pm.newQuery(e);
1606: query.setFilter("name == id");
1607: query.declareParameters("String id");
1608: query.setUnique(true);
1609: final String id = n + 3;
1610: Litem litem = (Litem) query.execute(id);//no query
1611: assertNotNull("Null value for " + id, litem);
1612: litem.setName(id + "bis");
1613:
1614: //Check that the old value is unbound
1615: litem = (Litem) query.execute(id);//query
1616: assertNull("Old value not unbound from the user cache, "
1617: + id, litem);
1618:
1619: litem = (Litem) query.execute(id + "bis");//query
1620: assertNotNull("Null value for " + id + "bis", litem);
1621:
1622: litem = (Litem) query.execute(n + 10);//query
1623: assertNull("Non null value for " + n + 10, litem);
1624: pm.currentTransaction().commit();
1625:
1626: pm.evictAll();
1627:
1628: pm.currentTransaction().begin();
1629: litem = (Litem) query.execute(id + "bis"); //query
1630: assertNotNull("Null value for " + id + "bis", litem);
1631: pm.currentTransaction().commit();
1632:
1633: pm.currentTransaction().begin();
1634: String myId = n + 1;
1635: litem = (Litem) query.execute(myId);//no query
1636: assertNotNull("Null value for " + myId, litem);
1637: litem = (Litem) query.execute(myId);//no query
1638: assertNotNull("Null value for " + myId, litem);
1639: pm.currentTransaction().commit();
1640:
1641: pm.currentTransaction().begin();
1642: e = pm.getExtent(Litem.class, true);
1643: for (Iterator iter = e.iterator(); iter.hasNext();) {
1644: pm.deletePersistent(iter.next());
1645: }
1646: pm.currentTransaction().commit();
1647: } catch (JDOException e) {
1648: Exception ie = ExceptionHelper.getNested(e);
1649: logger.log(BasicLevel.ERROR, "", ie);
1650: fail(ie.getMessage());
1651: } finally {
1652: pm.close();
1653: }
1654: }
1655:
1656: public void testNotContains() {
1657: logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1658: PersistenceManager pm = pmf.getPersistenceManager();
1659: Query q = pm.newQuery(Group.class);
1660: q.declareParameters("String p1");
1661: q.declareVariables("User u");
1662: q.setFilter("!(this.users.contains(u)) && (u.name == p1)");
1663: Collection c = (Collection) q.execute("user_g0_u0");
1664: Collection founds = new ArrayList(c.size());
1665: for (Iterator iter = c.iterator(); iter.hasNext();) {
1666: Group g = (Group) iter.next();
1667: founds.add(g.getName());
1668: }
1669: q.closeAll();
1670: pm.close();
1671: assertSameCollection("Bad group found", Arrays
1672: .asList(new String[] { "group_1", "group_2" }), founds);
1673: }
1674:
1675: public final static int NB_GROUP = 2;
1676: public final static int NB_USER_PER_GROUP = 2;
1677:
1678: protected GroupUser commonUser = new GroupUser("user_0and2");
1679:
1680: public void notContainsInheritanceABuilder() {
1681: PersistenceManager pm = pmf.getPersistenceManager();
1682: pm.currentTransaction().begin();
1683: //add newsgroup_0 -> [user_0and2]
1684: NewsGroup ng0 = new NewsGroup("newsgroup_0");
1685: Collection users = ng0.getUsers();
1686: users.add(commonUser);
1687: pm.makePersistent(ng0);
1688: //add newsgroup_1 -> [user_1]
1689: NewsGroup ng1 = new NewsGroup("newsgroup_1");
1690: users = ng1.getUsers();
1691: users.add(new GroupUser("user_1"));
1692: pm.makePersistent(ng1);
1693: pm.currentTransaction().commit();
1694: pm.close();
1695: }
1696:
1697: //not contains user_Oand2 --> newsgroup_1
1698: public void testNotContainsInheritanceA1() {
1699: notContainsInheritanceABuilder();
1700: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA1");
1701: try {
1702: notContainsInheritance("user_0and2",
1703: new String[] { "newsgroup_1" });
1704: } finally {
1705: removingOfPersistentObject();
1706: }
1707: }
1708:
1709: //not contains user_1 --> newsgroup_0
1710: public void testNotContainsInheritanceA2() {
1711: notContainsInheritanceABuilder();
1712: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA2");
1713: try {
1714: notContainsInheritance("user_1",
1715: new String[] { "newsgroup_0" });
1716: } finally {
1717: removingOfPersistentObject();
1718: }
1719: }
1720:
1721: //not contains dummy --> all
1722: public void testNotContainsInheritanceA3() {
1723: notContainsInheritanceABuilder();
1724: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceA3");
1725: try {
1726: notContainsInheritance("dummy", new String[] {
1727: "newsgroup_0", "newsgroup_1" });
1728: } finally {
1729: removingOfPersistentObject();
1730: }
1731: }
1732:
1733: public void notContainsInheritanceBBuilder() {
1734: notContainsInheritanceABuilder();
1735: PersistenceManager pm = pmf.getPersistenceManager();
1736: //add newsgroup_2 -> [user_0and2]
1737: NewsGroup ng = new NewsGroup("newsgroup_2");
1738: Collection users = ng.getUsers();
1739: users.add(commonUser);
1740: pm.makePersistent(ng);
1741: pm.close();
1742: }
1743:
1744: //not contains user_Oand2 --> newsgroup_1
1745: public void testNotContainsInheritanceB1() {
1746: notContainsInheritanceBBuilder();
1747: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB1");
1748: try {
1749: notContainsInheritance("user_0and2",
1750: new String[] { "newsgroup_1" });
1751: } finally {
1752: removingOfPersistentObject();
1753: }
1754: }
1755:
1756: //not contains user_1 --> newsgroup_0, newsgroup_2
1757: public void testNotContainsInheritanceB2() {
1758: notContainsInheritanceBBuilder();
1759: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB2");
1760: try {
1761: notContainsInheritance("user_1", new String[] {
1762: "newsgroup_0", "newsgroup_2" });
1763: } finally {
1764: removingOfPersistentObject();
1765: }
1766: }
1767:
1768: //not contains dummy --> all
1769: public void testNotContainsInheritanceB3() {
1770: notContainsInheritanceBBuilder();
1771: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceB3");
1772: try {
1773: notContainsInheritance("dummy", new String[] {
1774: "newsgroup_0", "newsgroup_1", "newsgroup_2" });
1775: } finally {
1776: removingOfPersistentObject();
1777: }
1778: }
1779:
1780: public void notContainsInheritanceCBuilder() {
1781: notContainsInheritanceBBuilder();
1782: PersistenceManager pm = pmf.getPersistenceManager();
1783: //add newsgroup_3 -> [empty]
1784: NewsGroup ng = new NewsGroup("newsgroup_3");
1785: pm.makePersistent(ng);
1786: pm.close();
1787: }
1788:
1789: //not contains user_Oand2 --> newsgroup_1 and newsgroup_3
1790: public void testNotContainsInheritanceC1() {
1791: notContainsInheritanceCBuilder();
1792: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC1");
1793: try {
1794: notContainsInheritance("user_0and2", new String[] {
1795: "newsgroup_1", "newsgroup_3" });
1796: } finally {
1797: removingOfPersistentObject();
1798: }
1799: }
1800:
1801: //not contains user_1 --> newsgroup_0, newsgroup_2, newsgroup_3
1802: public void testNotContainsInheritanceC2() {
1803: notContainsInheritanceCBuilder();
1804: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC2");
1805: try {
1806: notContainsInheritance("user_1", new String[] {
1807: "newsgroup_0", "newsgroup_2", "newsgroup_3" });
1808: } finally {
1809: removingOfPersistentObject();
1810: }
1811: }
1812:
1813: //not contains dummy --> all
1814: public void testNotContainsInheritanceC3() {
1815: notContainsInheritanceCBuilder();
1816: logger.log(BasicLevel.DEBUG, "testNotContainsInheritanceC3");
1817: try {
1818: notContainsInheritance("dummy", new String[] {
1819: "newsgroup_0", "newsgroup_1", "newsgroup_2",
1820: "newsgroup_3" });
1821: } finally {
1822: removingOfPersistentObject();
1823: }
1824: }
1825:
1826: private void notContainsInheritance(String parameter,
1827: String[] resultExpected) {
1828: PersistenceManager pm = pmf.getPersistenceManager();
1829: Query q = pm.newQuery(NewsGroup.class);
1830: q.declareParameters("String p1");
1831: q.declareVariables("GroupUser u");
1832: q.setFilter("!(this.users.contains(u)) && (u.name == p1)");
1833: Collection c = (Collection) q.execute(parameter);
1834: Collection founds = new ArrayList(c.size());
1835: for (Iterator iter = c.iterator(); iter.hasNext();) {
1836: NewsGroup ng = (NewsGroup) iter.next();
1837: founds.add(ng.getName());
1838: }
1839: q.closeAll();
1840: pm.close();
1841: assertSameCollection("Bad group found", Arrays
1842: .asList(resultExpected), founds);
1843: }
1844:
1845: public void removingOfPersistentObject() {
1846: PersistenceManager pm = pmf.getPersistenceManager();
1847: try {
1848: Class[] cs = new Class[] { NewsGroup.class, GroupUser.class };
1849: pm.currentTransaction().begin();
1850: for (int i = 0; i < cs.length; i++) {
1851: Query query = pm.newQuery(cs[i]);
1852: Collection col = (Collection) query.execute();
1853: Iterator it = col.iterator();
1854: while (it.hasNext()) {
1855: Object o = it.next();
1856: Assert.assertNotNull(
1857: "null object in the query result"
1858: + cs[i].getName(), o);
1859: pm.deletePersistent(o);
1860:
1861: }
1862: query.close(col);
1863: }
1864: pm.currentTransaction().commit();
1865: } catch (JDOException e) {
1866: Exception ie = ExceptionHelper.getNested(e);
1867: logger.log(BasicLevel.ERROR, "", ie);
1868: fail(ie.getMessage());
1869: } finally {
1870: pm.close();
1871: }
1872: }
1873:
1874: public void testNotContainsComposite() {
1875: logger.log(BasicLevel.DEBUG, "testNotContainsComposite");
1876: PersistenceManager pm = pmf.getPersistenceManager();
1877: Query q = pm.newQuery(MailingList.class);
1878: q.declareParameters("String p1, String p2");
1879: q.declareVariables("GroupModerator gm");
1880: q
1881: .setFilter("!(this.moderators.contains(gm)) && (gm.firstName == p1) && (gm.lastName == p2)");
1882: Collection c = (Collection) q.execute("moderator_ml0", "mod0");
1883: Collection founds = new ArrayList(c.size());
1884: for (Iterator iter = c.iterator(); iter.hasNext();) {
1885: MailingList ml = (MailingList) iter.next();
1886: founds.add(ml.getName());
1887: }
1888: q.closeAll();
1889: pm.close();
1890: assertSameCollection("Bad group found",
1891: Arrays.asList(new String[] { "mailinglist_1",
1892: "mailinglist_2" }), founds);
1893: }
1894:
1895: public void testTwoSameContains() {
1896: logger.log(BasicLevel.DEBUG, "testUserCacheSingle");
1897: PersistenceManager pm = pmf.getPersistenceManager();
1898: Query q = pm.newQuery(Group.class);
1899: q.declareParameters("String p1, String p2");
1900: q.declareVariables("User u1 ; User u2");
1901: q
1902: .setFilter("((this.users.contains(u1)) && (u1.name == p1)) && ((this.users.contains(u2)) && (u2.name == p2))");
1903: Collection c = (Collection) q.execute("user_g0_u0",
1904: "user_g0_u1");
1905: Collection founds = new ArrayList(c.size());
1906: for (Iterator iter = c.iterator(); iter.hasNext();) {
1907: Group g = (Group) iter.next();
1908: founds.add(g.getName());
1909: }
1910: q.closeAll();
1911: pm.close();
1912: assertSameCollection("Bad group found", Arrays
1913: .asList(new String[] { "group_0" }), founds);
1914: }
1915:
1916: public void testAutoRef() {
1917: String nameToFind = "g1";
1918: logger.log(BasicLevel.DEBUG, "testAutoRef");
1919: PersistenceManager pm = pmf.getPersistenceManager();
1920: String filter = "(previousRef != null) && (previousRef != this) && (previousRef.name == referenceName)";
1921: Query q = pm.newQuery(GeoRef.class, filter);
1922: q.declareParameters("String referenceName");
1923: Collection results = (Collection) q.execute(nameToFind);
1924: assertTrue("Result is empty", !results.isEmpty());
1925: Iterator it = results.iterator();
1926: while (it.hasNext()) {
1927: GeoRef g = (GeoRef) it.next();
1928: assertTrue("Name of previous ref is not correct",
1929: nameToFind.equals(g.getPreviousRef().getName()));
1930:
1931: }
1932: q.closeAll();
1933: pm.close();
1934: }
1935:
1936: public void testSetRange() {
1937: logger.log(BasicLevel.DEBUG, "testSetRange");
1938: List expectedNames = POBuilder.getUserNames();
1939: Collections.sort(expectedNames);
1940: PersistenceManager pm = pmf.getPersistenceManager();
1941: pm.currentTransaction().begin();
1942: Query q = pm.newQuery(User.class);
1943: q.setOrdering("name ascending");
1944: q.setRange(0, 5);
1945: Collection c = (Collection) q.execute();
1946: ArrayList foundNames = new ArrayList();
1947: for (Iterator it = c.iterator(); it.hasNext();) {
1948: User u = (User) it.next();
1949: foundNames.add(u.getName());
1950: }
1951: q.closeAll();
1952: pm.currentTransaction().commit();
1953: pm.evictAll();
1954: assertEquals("not expected result",
1955: expectedNames.subList(0, 5), foundNames);
1956:
1957: pm.currentTransaction().begin();
1958: q = pm.newQuery(User.class);
1959: q.setOrdering("name ascending");
1960: q.setRange(2, 5);
1961: c = (Collection) q.execute();
1962: foundNames.clear();
1963: for (Iterator it = c.iterator(); it.hasNext();) {
1964: User u = (User) it.next();
1965: foundNames.add(u.getName());
1966: }
1967: q.closeAll();
1968: pm.currentTransaction().commit();
1969: pm.evictAll();
1970: assertEquals("not expected result",
1971: expectedNames.subList(2, 5), foundNames);
1972:
1973: pm.currentTransaction().begin();
1974: q = pm.newQuery(User.class);
1975: q.setOrdering("name ascending");
1976: c = (Collection) q.execute();
1977: foundNames.clear();
1978: for (Iterator it = c.iterator(); it.hasNext();) {
1979: User u = (User) it.next();
1980: foundNames.add(u.getName());
1981: }
1982: q.closeAll();
1983: pm.currentTransaction().commit();
1984: pm.evictAll();
1985: assertEquals("not expected result", expectedNames, foundNames);
1986:
1987: pm.currentTransaction().begin();
1988: q = pm.newQuery(Group.class);
1989: Query q2 = pm.newQuery(Group.class);
1990: Collection usernames = Arrays.asList(new String[] {
1991: "user_g0_u0", "user_g0_u1", "user_g1_u0" });
1992: String var = "User u";
1993: q.declareVariables(var);
1994: q2.declareVariables(var);
1995: String param = "Collection usernames";
1996: q.declareParameters(param);
1997: q2.declareParameters(param);
1998: String filter = "(users.contains(u)) && usernames.contains(u.name)";
1999: q.setFilter(filter);
2000: q2.setFilter(filter);
2001: q.setOrdering("name ascending");
2002: q.setRange(0, 5);
2003: q2.setOrdering("name ascending");
2004:
2005: q2.setResult("count(*)");
2006: q2.setUnique(true);
2007:
2008: int count = ((Long) q2.execute(usernames)).intValue();
2009: c = (Collection) q.execute(usernames);
2010: int s = c.size();
2011: q.closeAll();
2012: q2.closeAll();
2013: pm.evictAll();
2014:
2015: assertEquals("Bad count result", 3, count);
2016: assertEquals("Bad result size", 3, s);
2017:
2018: pm.currentTransaction().begin();
2019: q = pm.newQuery(Group.class);
2020: q2 = pm.newQuery(Group.class);
2021: usernames = Arrays.asList(new String[] { "user_g0_u0",
2022: "user_g0_u1", "user_g1_u0" });
2023: var = "User u";
2024: q.declareVariables(var);
2025: q2.declareVariables(var);
2026: param = "Collection usernames";
2027: q.declareParameters(param);
2028: q2.declareParameters(param);
2029: filter = "(users.contains(u)) && usernames.contains(u.name)";
2030: q.setFilter(filter);
2031: q2.setFilter(filter);
2032: q.setGrouping("name");
2033: q.setRange(0, 5);
2034: q2.setGrouping("name");
2035:
2036: q2.setResult("count(*)");
2037:
2038: Collection c1 = (Collection) q2.execute(usernames);
2039: Iterator itC1 = c1.iterator();
2040: count = 0;
2041: while (itC1.hasNext()) {
2042: count += ((Long) itC1.next()).intValue();
2043: }
2044: c = (Collection) q.execute(usernames);
2045: s = c.size();
2046: q.closeAll();
2047: q2.closeAll();
2048:
2049: pm.currentTransaction().commit();
2050: pm.close();
2051:
2052: assertEquals("Bad count result", 3, count);
2053: assertEquals("Bad result size", 3, s);
2054: }
2055:
2056: public void testSetRange2() {
2057: logger.log(BasicLevel.DEBUG, "testSetRange2");
2058: List expectedNames = Arrays.asList(POBuilder.names);
2059: Collections.sort(expectedNames);
2060: PersistenceManager pm = pmf.getPersistenceManager();
2061: pm.currentTransaction().begin();
2062: Query q = pm.newQuery(Employee.class, "dept.name == p1");
2063: q.declareParameters("String p1");
2064: q.setOrdering("name ascending");
2065: q.setRange(2, 4);
2066: Collection c = (Collection) q.execute(POBuilder.depName);
2067: ArrayList foundNames = new ArrayList();
2068: for (Iterator it = c.iterator(); it.hasNext();) {
2069: Employee e = (Employee) it.next();
2070: foundNames.add(e.getName());
2071: }
2072: q.closeAll();
2073: pm.currentTransaction().commit();
2074: pm.close();
2075: assertEquals("not expected result",
2076: expectedNames.subList(2, 4), foundNames);
2077: }
2078:
2079: public void testNullEqualityWithPrimitive() {
2080: PersistenceManager pm = pmf.getPersistenceManager();
2081: Query q = pm.newQuery(Group.class);
2082: q.setFilter("name != null");
2083: Collection c = (Collection) q.execute();
2084: int s = c.size();
2085: q.closeAll();
2086: pm.close();
2087: assertEquals("Bad result size", 3, s);
2088: }
2089:
2090: }
|