0001: /*
0002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
0003:
0004: This file is part of the JODB (Java Objects Database) open source project.
0005:
0006: JODB is free software; you can redistribute it and/or modify it under
0007: the terms of version 2 of the GNU General Public License as published
0008: by the Free Software Foundation.
0009:
0010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
0011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0013: for more details.
0014:
0015: You should have received a copy of the GNU General Public License along
0016: with this program; if not, write to the Free Software Foundation, Inc.,
0017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0018: */
0019: package com.mobixess.jodb.tests;
0020:
0021: import java.io.File;
0022: import java.io.IOException;
0023: import java.util.List;
0024:
0025: import com.mobixess.jodb.core.JODB;
0026: import com.mobixess.jodb.core.JODBConfig;
0027: import com.mobixess.jodb.core.JODBSessionContainer;
0028: import com.mobixess.jodb.soda.api.Candidate;
0029: import com.mobixess.jodb.soda.api.Constraint;
0030: import com.mobixess.jodb.soda.api.Evaluation;
0031: import com.mobixess.jodb.soda.api.Query;
0032: import com.mobixess.jodb.tests.testobjects.ObjectA;
0033: import com.mobixess.jodb.tests.testobjects.ObjectB;
0034: import com.mobixess.jodb.tests.testobjects.ObjectWithString;
0035:
0036: public class QueryTests {
0037:
0038: static int _testCounter;
0039: private static String TEST_DATA_DIR = "./testData/QueryTests/";
0040:
0041: /**
0042: * @param args
0043: * @throws IOException
0044: */
0045: public static void main(String[] args) throws Exception {
0046: QueryTests queryTests = new QueryTests();
0047: queryTests.sortingTest();
0048: queryTests.queryByExample(true);
0049: queryTests.queryByExample(false);
0050: queryTests.evaluationQueryTest(true);
0051: queryTests.evaluationQueryTest(false);
0052: queryTests.stringFieldQueryCompare(true);
0053: queryTests.stringFieldQueryCompare(false);
0054: queryTests.primitiveFieldsQuery(true);
0055: queryTests.primitiveFieldsQuery(false);
0056: queryTests.classTypeConstraints(true);
0057: queryTests.classTypeConstraints(false);
0058: System.out.println("Test finished");
0059: }
0060:
0061: public void queryByExample(boolean reopen) throws Exception {
0062: File testFileDir = new File(TEST_DATA_DIR);
0063: testFileDir.mkdirs();
0064: File testFile = new File(testFileDir, SimpleAddTest.class
0065: .getSimpleName()
0066: + (_testCounter++) + ".jdb");
0067: testFile.delete();
0068: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0069: ObjectA objectA = new ObjectA((byte) 2, (byte) 3, null);
0070: ObjectA objectA1 = new ObjectA((byte) 4, (byte) 3, null);
0071: ObjectB objectB = new ObjectB();
0072: objectB._val3 = objectA;
0073: sessionContainer.set(objectA);
0074: sessionContainer.set(objectA1);
0075: sessionContainer.set(objectB);
0076: sessionContainer.commit();
0077:
0078: if (reopen) {
0079: sessionContainer.close();
0080: sessionContainer = getContainerForFile(testFile);
0081: }
0082:
0083: ObjectA pattern = new ObjectA((byte) 4, (short) 3, null);
0084: List list = sessionContainer.get(pattern);
0085: if (list.size() != 1) {
0086: throw new RuntimeException("" + list.size());
0087: }
0088: ObjectA fromDb = (ObjectA) list.get(0);
0089: if (fromDb.getVal1() != pattern.getVal1()
0090: || fromDb.getVal2() != pattern.getVal2()
0091: || fromDb.getVal3() != pattern.getVal3()) {
0092: throw new RuntimeException();
0093: }
0094:
0095: if (reopen) {
0096: sessionContainer.close();
0097: sessionContainer = getContainerForFile(testFile);
0098: }
0099:
0100: pattern = null;
0101: list = sessionContainer.get(pattern);
0102: if (list.size() != 3) {
0103: throw new RuntimeException("" + list.size());
0104: }
0105:
0106: if (reopen) {
0107: sessionContainer.close();
0108: sessionContainer = getContainerForFile(testFile);
0109: }
0110:
0111: Query query = sessionContainer.query();
0112: pattern = new ObjectA((byte) 4, (short) 3, null);
0113: query.constrain(pattern);
0114: list = query.execute();
0115: if (list.size() != 1) {
0116: throw new RuntimeException("" + list.size());
0117: }
0118: fromDb = (ObjectA) list.get(0);
0119: if (fromDb.getVal1() != pattern.getVal1()
0120: || fromDb.getVal2() != pattern.getVal2()
0121: || fromDb.getVal3() != pattern.getVal3()) {
0122: throw new RuntimeException();
0123: }
0124:
0125: if (reopen) {
0126: sessionContainer.close();
0127: sessionContainer = getContainerForFile(testFile);
0128: }
0129:
0130: query = sessionContainer.query();
0131: pattern = new ObjectA((byte) 2, (short) 0, null);
0132: query.constrain(pattern);
0133: list = query.execute();
0134: if (list.size() != 1) {
0135: throw new RuntimeException("" + list.size());
0136: }
0137: fromDb = (ObjectA) list.get(0);
0138: if (fromDb.getVal1() != objectA.getVal1()
0139: || fromDb.getVal2() != objectA.getVal2()
0140: || fromDb.getVal3() != objectA.getVal3()) {
0141: throw new RuntimeException();
0142: }
0143:
0144: if (reopen) {
0145: sessionContainer.close();
0146: sessionContainer = getContainerForFile(testFile);
0147: }
0148:
0149: query = sessionContainer.query();
0150: pattern = new ObjectA((byte) 0, (short) 3, null);
0151: query.constrain(pattern);
0152: list = query.execute();
0153: if (list.size() != 2) {
0154: throw new RuntimeException("" + list.size());
0155: }
0156:
0157: if (reopen) {
0158: sessionContainer.close();
0159: sessionContainer = getContainerForFile(testFile);
0160: }
0161:
0162: ObjectWithString objectWithString = new ObjectWithString();
0163: objectWithString._val1 = "test";
0164: ObjectWithString objectWithString1 = new ObjectWithString();
0165: objectWithString1._val1 = "test1";
0166: sessionContainer.set(objectWithString);
0167: sessionContainer.set(objectWithString1);
0168: sessionContainer.commit();
0169:
0170: if (reopen) {
0171: sessionContainer.close();
0172: sessionContainer = getContainerForFile(testFile);
0173: }
0174:
0175: ObjectWithString objectWithStringPattern = new ObjectWithString();
0176: objectWithStringPattern._val1 = "test";
0177: query = sessionContainer.query();
0178: query.constrain(objectWithStringPattern);
0179: list = query.execute();
0180: if (list.size() != 1) {
0181: throw new RuntimeException("" + list.size());
0182: }
0183:
0184: ObjectWithString objectWithStringfromDb = (ObjectWithString) list
0185: .get(0);
0186: if (!objectWithStringfromDb._val1
0187: .equals(objectWithStringPattern._val1)) {
0188: throw new RuntimeException();
0189: }
0190:
0191: if (reopen) {
0192: sessionContainer.close();
0193: sessionContainer = getContainerForFile(testFile);
0194: }
0195:
0196: query = sessionContainer.query();
0197: Constraint constraint = query
0198: .constrain(objectWithStringPattern);
0199: ObjectWithString objectWithStringPattern1 = new ObjectWithString();
0200: objectWithStringPattern1._val1 = "test1";
0201: query.constrain(objectWithStringPattern1).or(constraint);
0202:
0203: list = query.execute();
0204: if (list.size() != 2) {
0205: throw new RuntimeException("" + list.size());
0206: }
0207:
0208: sessionContainer.close();
0209:
0210: // objectWithStringfromDb = (ObjectWithString) list.get(0);
0211: // if(!objectWithStringfromDb._val1.equals(objectWithStringPattern._val1 )){
0212: // throw new RuntimeException();
0213: // }
0214:
0215: }
0216:
0217: public void classTypeConstraints(boolean reopen) throws Exception {
0218: File testFileDir = new File(TEST_DATA_DIR);
0219: testFileDir.mkdirs();
0220: File testFile = new File(testFileDir, SimpleAddTest.class
0221: .getSimpleName()
0222: + (_testCounter++) + ".jdb");
0223: testFile.delete();
0224: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0225: ObjectA objectA = new ObjectA((byte) 2, (byte) 3, null);
0226: ObjectB objectB = new ObjectB();
0227: objectB._val3 = objectA;
0228: sessionContainer.set(objectA);
0229: sessionContainer.set(objectB);
0230:
0231: sessionContainer.commit();
0232:
0233: if (reopen) {
0234: sessionContainer.close();
0235: sessionContainer = getContainerForFile(testFile);
0236: }
0237:
0238: List list = sessionContainer.query(ObjectA.class);
0239: if (list.size() != 1) {
0240: throw new RuntimeException();
0241: }
0242: if (list.get(0).getClass() != ObjectA.class) {
0243: throw new RuntimeException();
0244: }
0245:
0246: if (reopen) {
0247: sessionContainer.close();
0248: sessionContainer = getContainerForFile(testFile);
0249: }
0250:
0251: Query query = sessionContainer.query();
0252: query.constrain(ObjectA.class);
0253: list = query.execute();
0254: if (list.size() != 1) {
0255: throw new RuntimeException();
0256: }
0257: if (list.get(0).getClass() != ObjectA.class) {
0258: throw new RuntimeException();
0259: }
0260:
0261: if (reopen) {
0262: sessionContainer.close();
0263: sessionContainer = getContainerForFile(testFile);
0264: }
0265: query = sessionContainer.query();
0266: query.constrain(ObjectA.class).not();
0267: list = query.execute();
0268: if (list.size() != 1) {
0269: throw new RuntimeException();
0270: }
0271: if (list.get(0).getClass() != ObjectB.class) {
0272: throw new RuntimeException();
0273: }
0274:
0275: if (reopen) {
0276: sessionContainer.close();
0277: sessionContainer = getContainerForFile(testFile);
0278: }
0279:
0280: query = sessionContainer.query();
0281: query.constrain(ObjectA.class).or(
0282: query.constrain(ObjectB.class));
0283: list = query.execute();
0284: if (list.size() != 2) {
0285: throw new RuntimeException();
0286: }
0287:
0288: if (reopen) {
0289: sessionContainer.close();
0290: sessionContainer = getContainerForFile(testFile);
0291: }
0292:
0293: query = sessionContainer.query();
0294: query.constrain(ObjectA.class).or(
0295: query.constrain(ObjectB.class).not());
0296: list = query.execute();
0297: if (list.size() != 1) {
0298: throw new RuntimeException();
0299: }
0300: if (list.get(0).getClass() != ObjectA.class) {
0301: throw new RuntimeException();
0302: }
0303:
0304: if (reopen) {
0305: sessionContainer.close();
0306: sessionContainer = getContainerForFile(testFile);
0307: }
0308:
0309: query = sessionContainer.query();
0310: query.descend("_val3").constrain(ObjectA.class);
0311: list = query.execute();
0312: if (list.size() != 1) {
0313: throw new RuntimeException();
0314: }
0315: if (list.get(0).getClass() != ObjectB.class) {
0316: throw new RuntimeException();
0317: }
0318:
0319: if (reopen) {
0320: sessionContainer.close();
0321: sessionContainer = getContainerForFile(testFile);
0322: }
0323:
0324: query = sessionContainer.query();
0325: query.descend("_val3").constrain(ObjectB.class);
0326: list = query.execute();
0327: if (list.size() != 1) {
0328: throw new RuntimeException();
0329: }
0330: if (list.get(0).getClass() != ObjectA.class) {
0331: throw new RuntimeException();
0332: }
0333:
0334: if (reopen) {
0335: sessionContainer.close();
0336: sessionContainer = getContainerForFile(testFile);
0337: }
0338:
0339: query = sessionContainer.query();
0340: query.descend("_val3").constrain(ObjectA.class);
0341: query.descend("_val2").constrain(short.class);
0342: list = query.execute();
0343: if (list.size() != 1) {
0344: throw new RuntimeException();
0345: }
0346: if (list.get(0).getClass() != ObjectB.class) {
0347: throw new RuntimeException();
0348: }
0349:
0350: if (reopen) {
0351: sessionContainer.close();
0352: sessionContainer = getContainerForFile(testFile);
0353: }
0354:
0355: query = sessionContainer.query();
0356: Constraint constraint = query.descend("_val3").constrain(
0357: ObjectB.class);
0358: query.descend("_val2").constrain(short.class).or(constraint);
0359: list = query.execute();
0360: if (list.size() != 2) {
0361: throw new RuntimeException();
0362: }
0363:
0364: if (reopen) {
0365: sessionContainer.close();
0366: sessionContainer = getContainerForFile(testFile);
0367: }
0368:
0369: query = sessionContainer.query();
0370: constraint = query.descend("_val3").constrain(ObjectB.class);
0371: query.descend("_val2").constrain(short.class);
0372: list = query.execute();
0373: if (list.size() != 1) {
0374: throw new RuntimeException();
0375: }
0376: if (list.get(0).getClass() != ObjectA.class) {
0377: throw new RuntimeException();
0378: }
0379:
0380: if (reopen) {
0381: sessionContainer.close();
0382: sessionContainer = getContainerForFile(testFile);
0383: }
0384:
0385: query = sessionContainer.query();
0386: constraint = query.descend("_val3").constrain(ObjectB.class);
0387: query.descend("_val2").constrain(int.class).or(constraint);
0388: list = query.execute();
0389: if (list.size() != 1) {
0390: throw new RuntimeException();
0391: }
0392: if (list.get(0).getClass() != ObjectA.class) {
0393: throw new RuntimeException();
0394: }
0395:
0396: if (reopen) {
0397: sessionContainer.close();
0398: sessionContainer = getContainerForFile(testFile);
0399: }
0400:
0401: query = sessionContainer.query();
0402: constraint = query.descend("_val3").constrain(ObjectB.class);
0403: query.descend("_val2").constrain(int.class).and(constraint);
0404: list = query.execute();
0405: if (list.size() != 0) {
0406: throw new RuntimeException();
0407: }
0408: sessionContainer.close();
0409: }
0410:
0411: public void primitiveFieldsQuery(boolean reopen) throws Exception {
0412: primitiveFieldQueryCompare(reopen);
0413: primitiveFieldQueryEqual(reopen);
0414:
0415: }
0416:
0417: public void primitiveFieldQueryEqual(boolean reopen)
0418: throws Exception {
0419: File testFileDir = new File(TEST_DATA_DIR);
0420: testFileDir.mkdirs();
0421: File testFile = new File(testFileDir, SimpleAddTest.class
0422: .getSimpleName()
0423: + (_testCounter++) + ".jdb");
0424: testFile.delete();
0425: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0426: ObjectA objectA = new ObjectA((byte) 2, (short) 3, null);
0427: ObjectB objectB = new ObjectB();
0428: objectB._val3 = objectA;
0429: sessionContainer.set(objectA);
0430: sessionContainer.set(objectB);
0431:
0432: sessionContainer.close();
0433: sessionContainer = getContainerForFile(testFile);
0434: Query query = sessionContainer.query();
0435: query.descend("_val1").constrain(new Byte((byte) 2));
0436: List list = query.execute();
0437: if (list.size() != 1) {
0438: throw new RuntimeException();
0439: }
0440: if (list.get(0).getClass() != ObjectA.class) {
0441: throw new RuntimeException();
0442: }
0443:
0444: if (reopen) {
0445: sessionContainer.close();
0446: sessionContainer = getContainerForFile(testFile);
0447: }
0448:
0449: query = sessionContainer.query();
0450: query.descend("_val1").constrain(new Byte((byte) 2)).not();
0451: list = query.execute();
0452: if (list.size() != 1) {
0453: throw new RuntimeException();
0454: }
0455: if (list.get(0).getClass() != ObjectB.class) {
0456: throw new RuntimeException();
0457: }
0458:
0459: if (reopen) {
0460: sessionContainer.close();
0461: sessionContainer = getContainerForFile(testFile);
0462: }
0463:
0464: query = sessionContainer.query();
0465: query.descend("_val1").constrain(new Integer((byte) 2)).not();
0466: list = query.execute();
0467: if (list.size() != 1) {
0468: throw new RuntimeException();
0469: }
0470: if (list.get(0).getClass() != ObjectB.class) {
0471: throw new RuntimeException();
0472: }
0473:
0474: if (reopen) {
0475: sessionContainer.close();
0476: sessionContainer = getContainerForFile(testFile);
0477: }
0478:
0479: query = sessionContainer.query();
0480: query.descend("_val2").constrain(new Byte((byte) 3));
0481: list = query.execute();
0482: if (list.size() != 1) {
0483: throw new RuntimeException();
0484: }
0485: if (list.get(0).getClass() != ObjectA.class) {
0486: throw new RuntimeException();
0487: }
0488:
0489: if (reopen) {
0490: sessionContainer.close();
0491: sessionContainer = getContainerForFile(testFile);
0492: }
0493:
0494: query = sessionContainer.query();
0495: Constraint constraint = query.descend("_val2").constrain(
0496: new Byte((byte) 3));
0497: query.descend("_val2").constrain(new Byte((byte) 0)).or(
0498: constraint);
0499: list = query.execute();
0500: if (list.size() != 2) {
0501: throw new RuntimeException();
0502: }
0503:
0504: query = sessionContainer.query();
0505: constraint = query.descend("_val2").constrain(3);
0506: query.descend("_val1").constrain((short) 2).and(constraint);
0507: list = query.execute();
0508: if (list.size() != 1) {
0509: throw new RuntimeException();
0510: }
0511: if (list.get(0).getClass() != ObjectA.class) {
0512: throw new RuntimeException();
0513: }
0514: sessionContainer.close();
0515: }
0516:
0517: public void primitiveFieldQueryCompare(boolean reopen)
0518: throws Exception {
0519: File testFileDir = new File(TEST_DATA_DIR);
0520: testFileDir.mkdirs();
0521: File testFile = new File(testFileDir, SimpleAddTest.class
0522: .getSimpleName()
0523: + (_testCounter++) + ".jdb");
0524: testFile.delete();
0525: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0526: ObjectA objectA = new ObjectA((byte) 2, (short) 3, null);
0527: ObjectB objectB = new ObjectB();
0528: objectB._val3 = objectA;
0529: sessionContainer.set(objectA);
0530: sessionContainer.set(objectB);
0531:
0532: sessionContainer.close();
0533: sessionContainer = getContainerForFile(testFile);
0534: Query query = sessionContainer.query();
0535:
0536: query.descend("_val1").constrain((byte) 2).greater().equal();
0537: List list = query.execute();
0538: if (list.size() != 1) {
0539: throw new RuntimeException();
0540: }
0541: if (list.get(0).getClass() != ObjectA.class) {
0542: throw new RuntimeException();
0543: }
0544:
0545: if (reopen) {
0546: sessionContainer.close();
0547: sessionContainer = getContainerForFile(testFile);
0548: }
0549:
0550: query = sessionContainer.query();
0551: query.descend("_val1").constrain((byte) 2).greater();
0552: list = query.execute();
0553: if (list.size() != 0) {
0554: throw new RuntimeException();
0555: }
0556:
0557: if (reopen) {
0558: sessionContainer.close();
0559: sessionContainer = getContainerForFile(testFile);
0560: }
0561:
0562: query = sessionContainer.query();
0563: query.descend("_val1").constrain((byte) 2).smaller();
0564: list = query.execute();
0565: if (list.size() != 1) {
0566: throw new RuntimeException();
0567: }
0568:
0569: if (list.get(0).getClass() != ObjectB.class) {
0570: throw new RuntimeException();
0571: }
0572:
0573: if (reopen) {
0574: sessionContainer.close();
0575: sessionContainer = getContainerForFile(testFile);
0576: }
0577:
0578: query = sessionContainer.query();
0579: Constraint constraint = query.descend("_val1").constrain(
0580: (byte) 2).smaller();
0581: query.descend("_val2").constrain(2).greater().or(constraint);
0582: list = query.execute();
0583: if (list.size() != 2) {
0584: throw new RuntimeException();
0585: }
0586:
0587: if (reopen) {
0588: sessionContainer.close();
0589: sessionContainer = getContainerForFile(testFile);
0590: }
0591:
0592: query = sessionContainer.query();
0593: constraint = query.descend("_val1").constrain((byte) 2)
0594: .smaller();
0595: query.descend("_val2").constrain(2).greater().and(constraint);
0596: list = query.execute();
0597: if (list.size() != 0) {
0598: throw new RuntimeException();
0599: }
0600:
0601: if (reopen) {
0602: sessionContainer.close();
0603: sessionContainer = getContainerForFile(testFile);
0604: }
0605:
0606: query = sessionContainer.query();
0607: constraint = query.constrain(ObjectA.class);
0608: query.descend("_val2").constrain(2).greater().and(constraint);
0609: list = query.execute();
0610: if (list.size() != 1) {
0611: throw new RuntimeException();
0612: }
0613:
0614: if (reopen) {
0615: sessionContainer.close();
0616: sessionContainer = getContainerForFile(testFile);
0617: }
0618:
0619: query = sessionContainer.query();
0620: constraint = query.constrain(ObjectA.class);
0621: query.descend("_val2").constrain(2).greater().or(constraint);
0622: list = query.execute();
0623: if (list.size() != 1) {
0624: throw new RuntimeException();
0625: }
0626:
0627: if (reopen) {
0628: sessionContainer.close();
0629: sessionContainer = getContainerForFile(testFile);
0630: }
0631: sessionContainer.close();
0632: }
0633:
0634: public void evaluationQueryTest(boolean reopen) throws Exception {
0635: File testFileDir = new File(TEST_DATA_DIR);
0636: testFileDir.mkdirs();
0637: File testFile = new File(testFileDir, SimpleAddTest.class
0638: .getSimpleName()
0639: + (_testCounter++) + ".jdb");
0640: testFile.delete();
0641: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0642: ObjectA objectA = new ObjectA((byte) 0, (short) 1, null);
0643: ObjectB objectB = new ObjectB();
0644:
0645: sessionContainer.set(objectA);
0646: sessionContainer.set(objectB);
0647:
0648: sessionContainer.commit();
0649:
0650: if (reopen) {
0651: sessionContainer.close();
0652: sessionContainer = getContainerForFile(testFile);
0653: }
0654: Query query = sessionContainer.query();
0655: query.constrain(new ObjectAEvaluation());
0656:
0657: List list = query.execute();
0658:
0659: if (list.size() != 1) {
0660: throw new RuntimeException();
0661: }
0662:
0663: if (list.get(0) instanceof ObjectA) {
0664: } else {
0665: throw new RuntimeException();
0666: }
0667:
0668: if (reopen) {
0669: sessionContainer.close();
0670: sessionContainer = getContainerForFile(testFile);
0671: }
0672:
0673: query = sessionContainer.query();
0674: query.constrain(new ObjectAEvaluation()).not();
0675:
0676: list = query.execute();
0677:
0678: if (list.size() != 1) {
0679: throw new RuntimeException();
0680: }
0681:
0682: if (list.get(0) instanceof ObjectB) {
0683: } else {
0684: throw new RuntimeException();
0685: }
0686:
0687: if (reopen) {
0688: sessionContainer.close();
0689: sessionContainer = getContainerForFile(testFile);
0690: }
0691:
0692: query = sessionContainer.query();
0693: query.descend("_val2").constrain(
0694: new ObjectFieldEvaluation((short) 1));
0695:
0696: list = query.execute();
0697:
0698: if (list.size() != 1) {
0699: throw new RuntimeException();
0700: }
0701:
0702: if (list.get(0) instanceof ObjectA) {
0703: } else {
0704: throw new RuntimeException();
0705: }
0706:
0707: if (reopen) {
0708: sessionContainer.close();
0709: sessionContainer = getContainerForFile(testFile);
0710: }
0711:
0712: query = sessionContainer.query();
0713: query.descend("_val2").constrain(
0714: new ObjectFieldEvaluation((short) 0));
0715:
0716: list = query.execute();
0717:
0718: if (list.size() != 1) {
0719: throw new RuntimeException();
0720: }
0721:
0722: if (list.get(0) instanceof ObjectB) {
0723: } else {
0724: throw new RuntimeException();
0725: }
0726:
0727: if (reopen) {
0728: sessionContainer.close();
0729: sessionContainer = getContainerForFile(testFile);
0730: }
0731:
0732: query = sessionContainer.query();
0733: query.descend("_val2").constrain(
0734: new ObjectFieldEvaluation((short) 0)).not();
0735:
0736: list = query.execute();
0737:
0738: if (list.size() != 1) {
0739: throw new RuntimeException();
0740: }
0741:
0742: if (list.get(0) instanceof ObjectA) {
0743: } else {
0744: throw new RuntimeException();
0745: }
0746:
0747: if (reopen) {
0748: sessionContainer.close();
0749: sessionContainer = getContainerForFile(testFile);
0750: }
0751: }
0752:
0753: public void sortingTest() throws Exception {
0754: stringSortingTest(true, true);
0755: stringSortingTest(true, false);
0756: stringSortingTest(false, true);
0757: stringSortingTest(false, false);
0758: sortingTest(true, true);
0759: sortingTest(true, false);
0760: sortingTest(false, true);
0761: sortingTest(false, false);
0762: }
0763:
0764: public void stringSortingTest(boolean reopen, boolean ascending)
0765: throws Exception {
0766: File testFileDir = new File(TEST_DATA_DIR);
0767: testFileDir.mkdirs();
0768: File testFile = new File(testFileDir, SimpleAddTest.class
0769: .getSimpleName()
0770: + (_testCounter++) + ".jdb");
0771: testFile.delete();
0772: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0773: ObjectWithString objectA1 = new ObjectWithString();
0774: objectA1._val1 = "a";
0775: ObjectWithString objectA2 = new ObjectWithString();
0776: objectA2._val1 = "c";
0777: ObjectWithString objectA3 = new ObjectWithString();
0778: objectA3._val1 = "b";
0779:
0780: sessionContainer.set(objectA1);
0781: sessionContainer.set(objectA2);
0782: sessionContainer.set(objectA3);
0783:
0784: sessionContainer.commit();
0785:
0786: if (reopen) {
0787: sessionContainer.close();
0788: sessionContainer = getContainerForFile(testFile);
0789: }
0790: Query query = sessionContainer.query();
0791: if (ascending) {
0792: query.descend("_val0").orderAscending();
0793: } else {
0794: query.descend("_val0").orderDescending();
0795: }
0796: if (ascending) {
0797: query.descend("_val1").orderAscending();
0798: } else {
0799: query.descend("_val1").orderDescending();
0800: }
0801: JODBConfig.setCacheOnSortOperations(false);
0802: List list = query.execute();
0803:
0804: ObjectWithString prev = null;
0805:
0806: if (list.size() == 0) {
0807: throw new RuntimeException();
0808: }
0809:
0810: for (int i = 0; i < list.size(); i++) {
0811: ObjectWithString current = (ObjectWithString) list.get(i);
0812: if (prev != null) {
0813: if (ascending) {
0814: if (prev._val1.charAt(0) > current._val1.charAt(0)) {
0815: throw new RuntimeException();
0816: }
0817: } else if (prev._val1.charAt(0) < current._val1
0818: .charAt(0)) {
0819: throw new RuntimeException();
0820: }
0821: }
0822: prev = current;
0823: }
0824:
0825: if (reopen) {
0826: sessionContainer.close();
0827: sessionContainer = getContainerForFile(testFile);
0828: query = sessionContainer.query();
0829: if (ascending) {
0830: query.descend("_val1").orderAscending();
0831: } else {
0832: query.descend("_val1").orderDescending();
0833: }
0834: }
0835:
0836: JODBConfig.setCacheOnSortOperations(true);
0837: list = query.execute();
0838: if (list.size() == 0) {
0839: throw new RuntimeException();
0840: }
0841:
0842: prev = null;
0843: for (int i = 0; i < list.size(); i++) {
0844: ObjectWithString current = (ObjectWithString) list.get(i);
0845: if (prev != null) {
0846: if (ascending) {
0847: if (prev._val1.charAt(0) > current._val1.charAt(0)) {
0848: throw new RuntimeException();
0849: }
0850: } else if (prev._val1.charAt(0) < current._val1
0851: .charAt(0)) {
0852: throw new RuntimeException();
0853: }
0854: }
0855: prev = current;
0856: }
0857: JODBConfig.setCacheOnSortOperations(true);
0858: sessionContainer.close();
0859: }
0860:
0861: public void sortingTest(boolean reopen, boolean ascending)
0862: throws Exception {
0863: File testFileDir = new File(TEST_DATA_DIR);
0864: testFileDir.mkdirs();
0865: File testFile = new File(testFileDir, SimpleAddTest.class
0866: .getSimpleName()
0867: + (_testCounter++) + ".jdb");
0868: testFile.delete();
0869: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0870: ObjectA objectA1 = new ObjectA((byte) 0, (short) 2, null);
0871: ObjectA objectA2 = new ObjectA((byte) 0, (short) 1, null);
0872: ObjectA objectA3 = new ObjectA((byte) 0, (short) 3, null);
0873:
0874: sessionContainer.set(objectA1);
0875: sessionContainer.set(objectA2);
0876: sessionContainer.set(objectA3);
0877:
0878: sessionContainer.commit();
0879:
0880: if (reopen) {
0881: sessionContainer.close();
0882: sessionContainer = getContainerForFile(testFile);
0883: }
0884: Query query = sessionContainer.query();
0885: if (ascending) {
0886: query.descend("_val1").orderAscending();
0887: } else {
0888: query.descend("_val1").orderDescending();
0889: }
0890: if (ascending) {
0891: query.descend("_val2").orderAscending();
0892: } else {
0893: query.descend("_val2").orderDescending();
0894: }
0895: JODBConfig.setCacheOnSortOperations(false);
0896: List list = query.execute();
0897: if (list.size() == 0) {
0898: throw new RuntimeException();
0899: }
0900:
0901: ObjectA prev = null;
0902: for (int i = 0; i < list.size(); i++) {
0903: ObjectA current = (ObjectA) list.get(i);
0904: if (prev != null) {
0905: if (ascending) {
0906: if (prev.getVal2() > current.getVal2()) {
0907: throw new RuntimeException();
0908: }
0909: } else if (prev.getVal2() < current.getVal2()) {
0910: throw new RuntimeException();
0911: }
0912: }
0913: prev = current;
0914: }
0915:
0916: if (reopen) {
0917: sessionContainer.close();
0918: sessionContainer = getContainerForFile(testFile);
0919: query = sessionContainer.query();
0920: if (ascending) {
0921: query.descend("_val2").orderAscending();
0922: } else {
0923: query.descend("_val2").orderDescending();
0924: }
0925: }
0926:
0927: JODBConfig.setCacheOnSortOperations(true);
0928: list = query.execute();
0929: if (list.size() == 0) {
0930: throw new RuntimeException();
0931: }
0932:
0933: prev = null;
0934: for (int i = 0; i < list.size(); i++) {
0935: ObjectA current = (ObjectA) list.get(i);
0936: if (prev != null) {
0937: if (ascending) {
0938: if (prev.getVal2() > current.getVal2()) {
0939: throw new RuntimeException();
0940: }
0941: } else if (prev.getVal2() < current.getVal2()) {
0942: throw new RuntimeException();
0943: }
0944: }
0945: prev = current;
0946: }
0947: JODBConfig.setCacheOnSortOperations(true);
0948: sessionContainer.close();
0949: }
0950:
0951: public void stringFieldQueryCompare(boolean reopen)
0952: throws Exception {
0953: File testFileDir = new File(TEST_DATA_DIR);
0954: testFileDir.mkdirs();
0955: File testFile = new File(testFileDir, SimpleAddTest.class
0956: .getSimpleName()
0957: + (_testCounter++) + ".jdb");
0958: testFile.delete();
0959: JODBSessionContainer sessionContainer = getContainerForFile(testFile);
0960: ObjectWithString objectWithString = new ObjectWithString();
0961: objectWithString._val1 = "test1";
0962: sessionContainer.set(objectWithString);
0963:
0964: sessionContainer.close();
0965: sessionContainer = getContainerForFile(testFile);
0966: Query query = sessionContainer.query();
0967:
0968: query.descend("_val1").constrain(objectWithString._val1);
0969: List list = query.execute();
0970:
0971: if (list.size() != 1) {
0972: throw new RuntimeException();
0973: }
0974:
0975: ObjectWithString objectWithStringFromDb = (ObjectWithString) list
0976: .get(0);
0977:
0978: if (!objectWithStringFromDb._val1
0979: .equals(objectWithString._val1)) {
0980: throw new RuntimeException();
0981: }
0982:
0983: ObjectWithString objectWithString1 = new ObjectWithString();
0984: objectWithString1._val1 = "test2";
0985:
0986: sessionContainer.set(objectWithString1);
0987: sessionContainer.commit();
0988:
0989: if (reopen) {
0990: sessionContainer.close();
0991: sessionContainer = getContainerForFile(testFile);
0992: }
0993:
0994: query = sessionContainer.query();
0995:
0996: query.descend("_val1").constrain(objectWithString._val1).not();
0997:
0998: list = query.execute();
0999:
1000: if (list.size() != 1) {
1001: throw new RuntimeException();
1002: }
1003:
1004: objectWithStringFromDb = (ObjectWithString) list.get(0);
1005:
1006: if (!objectWithStringFromDb._val1
1007: .equals(objectWithString1._val1)) {
1008: throw new RuntimeException();
1009: }
1010:
1011: if (reopen) {
1012: sessionContainer.close();
1013: sessionContainer = getContainerForFile(testFile);
1014: }
1015:
1016: query = sessionContainer.query();
1017:
1018: query.constrain(objectWithString._val1);
1019:
1020: list = query.execute();
1021:
1022: if (list.size() != 1) {
1023: throw new RuntimeException();
1024: }
1025:
1026: String stringFromDb = (String) list.get(0);
1027:
1028: if (!stringFromDb.equals(objectWithString._val1)) {
1029: throw new RuntimeException();
1030: }
1031:
1032: query = sessionContainer.query();
1033:
1034: query.constrain(objectWithString._val1).not();
1035:
1036: list = query.execute();
1037:
1038: if (list.size() != 1) {
1039: throw new RuntimeException();
1040: }
1041:
1042: stringFromDb = (String) list.get(0);
1043:
1044: if (!stringFromDb.equals(objectWithString1._val1)) {
1045: throw new RuntimeException();
1046: }
1047:
1048: query = sessionContainer.query();
1049:
1050: query.constrain(objectWithString._val1).greater();
1051:
1052: list = query.execute();
1053:
1054: if (list.size() != 1) {
1055: throw new RuntimeException();
1056: }
1057:
1058: stringFromDb = (String) list.get(0);
1059:
1060: if (!stringFromDb.equals(objectWithString1._val1)) {
1061: throw new RuntimeException();
1062: }
1063:
1064: query = sessionContainer.query();
1065:
1066: query.constrain(objectWithString._val1).greater().or(
1067: query.constrain(objectWithString._val1));
1068:
1069: list = query.execute();
1070:
1071: if (list.size() != 2) {
1072: throw new RuntimeException("" + list.size());
1073: }
1074: sessionContainer.close();
1075:
1076: }
1077:
1078: public JODBSessionContainer getContainerForFile(File file)
1079: throws IOException {
1080: return (JODBSessionContainer) JODB.open(file);
1081: }
1082:
1083: private static class ObjectAEvaluation implements Evaluation {
1084: public void evaluate(Candidate candidate) {
1085: Object cand = candidate.getObject();
1086: if (cand instanceof ObjectA) {
1087: candidate.include(true);
1088: }
1089: }
1090:
1091: public int getActivationDepth() {
1092: return 5;
1093: }
1094: }
1095:
1096: private static class ObjectFieldEvaluation implements Evaluation {
1097: short _value;
1098:
1099: /**
1100: * @param value
1101: */
1102: public ObjectFieldEvaluation(short value) {
1103: super ();
1104: _value = value;
1105: }
1106:
1107: public void evaluate(Candidate candidate) {
1108: Object cand = candidate.getObject();
1109: if (cand instanceof Short) {
1110: candidate
1111: .include(((Short) cand).shortValue() == _value);
1112: }
1113: }
1114:
1115: public int getActivationDepth() {
1116: return 5;
1117: }
1118: }
1119:
1120: }
|