Package com.mobixess.jodb.tests

Source Code of com.mobixess.jodb.tests.QueryTests

/*
Copyright (C) 2007  Mobixess Inc. http://www.java-objects-database.com

This file is part of the JODB (Java Objects Database) open source project.

JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package com.mobixess.jodb.tests;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.mobixess.jodb.core.JODBConfig;
import com.mobixess.jodb.core.JODBSessionContainer;
import com.mobixess.jodb.core.JodbMini;
import com.mobixess.jodb.soda.api.Candidate;
import com.mobixess.jodb.soda.api.Constraint;
import com.mobixess.jodb.soda.api.Evaluation;
import com.mobixess.jodb.soda.api.Query;
import com.mobixess.jodb.tests.testobjects.ObjectA;
import com.mobixess.jodb.tests.testobjects.ObjectB;
import com.mobixess.jodb.tests.testobjects.ObjectWithString;
import com.mobixess.jodb.tests.testobjects.Object_Enum;
import com.mobixess.jodb.tests.testobjects.Object_Enum.TEST_ENUM;

public class QueryTests {

    static int _testCounter;
    private static String TEST_DATA_DIR = "./testData/QueryTests/";
   
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        QueryTests queryTests = new QueryTests();
        queryTests.enumFieldQueryCompare();
        queryTests.sortingTest();
        queryTests.queryByExample(true);
        queryTests.queryByExample(false);
        queryTests.evaluationQueryTest(true);
        queryTests.evaluationQueryTest(false);
        queryTests.stringFieldQueryCompare(true);
        queryTests.stringFieldQueryCompare(false);
        queryTests.primitiveFieldsQuery(true);
        queryTests.primitiveFieldsQuery(false);
        queryTests.classTypeConstraints(true);
        queryTests.classTypeConstraints(false);
        System.out.println("Test finished");
    }

    public void enumFieldQueryCompare() throws Exception{
        enumFieldQueryCompare(false);
        enumFieldQueryCompare(true);
    }
   
    public void queryByExample(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA = new ObjectA((byte)2,(byte)3,null);
        ObjectA objectA1 = new ObjectA((byte)4,(byte)3,null);
        ObjectB objectB = new ObjectB();
        objectB._val3 = objectA;
        sessionContainer.set(objectA);
        sessionContainer.set(objectA1);
        sessionContainer.set(objectB);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        ObjectA pattern = new ObjectA((byte)4,(short)3,null);
        List list = sessionContainer.get(pattern);
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
        ObjectA fromDb = (ObjectA) list.get(0);
        if(fromDb.getVal1()!=pattern.getVal1() || fromDb.getVal2()!=pattern.getVal2()|| fromDb.getVal3()!=pattern.getVal3()){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        pattern = null;
        list = sessionContainer.get(pattern);
        if(list.size()!=3){
            throw new RuntimeException(""+list.size());
        }

        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Query query = sessionContainer.query();
        pattern = new ObjectA((byte)4,(short)3,null);
        query.constrain(pattern);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
        fromDb = (ObjectA) list.get(0);
        if(fromDb.getVal1()!=pattern.getVal1() || fromDb.getVal2()!=pattern.getVal2()|| fromDb.getVal3()!=pattern.getVal3()){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
       
        query = sessionContainer.query();
        pattern = new ObjectA((byte)2,(short)0,null);
        query.constrain(pattern);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
        fromDb = (ObjectA) list.get(0);
        if(fromDb.getVal1()!=objectA.getVal1() || fromDb.getVal2()!=objectA.getVal2()|| fromDb.getVal3()!=objectA.getVal3()){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        pattern = new ObjectA((byte)0,(short)3,null);
        query.constrain(pattern);
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException(""+list.size());
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        ObjectWithString objectWithString = new ObjectWithString();
        objectWithString._val1 = "test";
        ObjectWithString objectWithString1 = new ObjectWithString();
        objectWithString1._val1 = "test1";
        sessionContainer.set(objectWithString);
        sessionContainer.set(objectWithString1);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        ObjectWithString objectWithStringPattern = new ObjectWithString();
        objectWithStringPattern._val1 = "test";
        query = sessionContainer.query();
        query.constrain(objectWithStringPattern);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
       
        ObjectWithString objectWithStringfromDb = (ObjectWithString) list.get(0);
        if(!objectWithStringfromDb._val1.equals(objectWithStringPattern._val1 )){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }

        query = sessionContainer.query();
        Constraint constraint = query.constrain(objectWithStringPattern);
        ObjectWithString objectWithStringPattern1 = new ObjectWithString();
        objectWithStringPattern1._val1 = "test1";
        query.constrain(objectWithStringPattern1).or(constraint);
       
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException(""+list.size());
        }
       
        sessionContainer.close();
       
//        objectWithStringfromDb = (ObjectWithString) list.get(0);
//        if(!objectWithStringfromDb._val1.equals(objectWithStringPattern._val1 )){
//            throw new RuntimeException();
//        }
       
    }
       
   
    public void classTypeConstraints(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA = new ObjectA((byte)2,(byte)3,null);
        ObjectB objectB = new ObjectB();
        objectB._val3 = objectA;
        sessionContainer.set(objectA);
        sessionContainer.set(objectB);

        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }

        List list = sessionContainer.query(ObjectA.class);
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }

        Query query = sessionContainer.query();
        query.constrain(ObjectA.class);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        query = sessionContainer.query();
        query.constrain(ObjectA.class).not();
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.constrain(ObjectA.class).or(query.constrain(ObjectB.class));
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.constrain(ObjectA.class).or(query.constrain(ObjectB.class).not());
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val3").constrain(ObjectA.class);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val3").constrain(ObjectB.class);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val3").constrain(ObjectA.class);
        query.descend("_val2").constrain(short.class);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        Constraint constraint = query.descend("_val3").constrain(ObjectB.class);
        query.descend("_val2").constrain(short.class).or(constraint);
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        constraint = query.descend("_val3").constrain(ObjectB.class);
        query.descend("_val2").constrain(short.class);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        constraint = query.descend("_val3").constrain(ObjectB.class);
        query.descend("_val2").constrain(int.class).or(constraint);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        constraint = query.descend("_val3").constrain(ObjectB.class);
        query.descend("_val2").constrain(int.class).and(constraint);
        list = query.execute();
        if(list.size()!=0){
            throw new RuntimeException();
        }
        sessionContainer.close();
    }
   
   
    public void primitiveFieldsQuery(boolean reopen) throws Exception{
        primitiveFieldQueryCompare(reopen);
        primitiveFieldQueryEqual(reopen);
       
    }
   
    public void primitiveFieldQueryEqual(boolean reopen)throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA = new ObjectA((byte)2, (short)3,null);
        ObjectB objectB = new ObjectB();
        objectB._val3 = objectA;
        sessionContainer.set(objectA);
        sessionContainer.set(objectB);

        sessionContainer.close();
        sessionContainer = getContainerForFile(testFile);
        Query query = sessionContainer.query();
        query.descend("_val1").constrain(new Byte((byte)2));
        List list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val1").constrain(new Byte((byte)2)).not();
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val1").constrain(new Integer((byte)2)).not();
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val2").constrain(new Byte((byte)3));
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        Constraint constraint = query.descend("_val2").constrain(new Byte((byte)3));
        query.descend("_val2").constrain(new Byte((byte)0)).or(constraint);
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
        constraint = query.descend("_val2").constrain(3);
        query.descend("_val1").constrain((short)2).and(constraint);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
        sessionContainer.close();
    }
   
    public void primitiveFieldQueryCompare(boolean reopen)throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA = new ObjectA((byte)2, (short)3,null);
        ObjectB objectB = new ObjectB();
        objectB._val3 = objectA;
        sessionContainer.set(objectA);
        sessionContainer.set(objectB);

        sessionContainer.close();
        sessionContainer = getContainerForFile(testFile);
        Query query = sessionContainer.query();
       
        query.descend("_val1").constrain((byte)2).greater().equal();
        List list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
        if(list.get(0).getClass() != ObjectA.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val1").constrain((byte)2).greater();
        list = query.execute();
        if(list.size()!=0){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val1").constrain((byte)2).smaller();
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(list.get(0).getClass() != ObjectB.class){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        Constraint constraint = query.descend("_val1").constrain((byte)2).smaller();
        query.descend("_val2").constrain(2).greater().or(constraint);
        list = query.execute();
        if(list.size()!=2){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        constraint = query.descend("_val1").constrain((byte)2).smaller();
        query.descend("_val2").constrain(2).greater().and(constraint);
        list = query.execute();
        if(list.size()!=0){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
       
        query = sessionContainer.query();
        constraint = query.constrain(ObjectA.class);
        query.descend("_val2").constrain(2).greater().and(constraint);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        constraint = query.constrain(ObjectA.class);
        query.descend("_val2").constrain(2).greater().or(constraint);
        list = query.execute();
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        sessionContainer.close();
    }
   
    public void evaluationQueryTest(boolean reopen)throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA = new ObjectA((byte)0,(short)1,null);
        ObjectB objectB = new ObjectB();
       
        sessionContainer.set(objectA);
        sessionContainer.set(objectB);
       
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        Query query = sessionContainer.query();
        query.constrain(new ObjectAEvaluation());
       
        List list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
       
        if(list.get(0) instanceof ObjectA){
        }else{
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.constrain(new ObjectAEvaluation()).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(list.get(0) instanceof ObjectB){
        }else{
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
       
        query = sessionContainer.query();
        query.descend("_val2").constrain(new ObjectFieldEvaluation((short) 1));
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(list.get(0) instanceof ObjectA){
        }else{
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val2").constrain(new ObjectFieldEvaluation((short) 0));
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(list.get(0) instanceof ObjectB){
        }else{
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
        query.descend("_val2").constrain(new ObjectFieldEvaluation((short) 0)).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        if(list.get(0) instanceof ObjectA){
        }else{
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
    }
   
    public void sortingTest() throws Exception{
        stringSortingTest(true,true);
        stringSortingTest(true,false);
        stringSortingTest(false,true);
        stringSortingTest(false,false);
        sortingTest(true,true);
        sortingTest(true,false);
        sortingTest(false,true);
        sortingTest(false,false);
    }
   
   
    public void stringSortingTest(boolean reopen, boolean ascending) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectWithString objectA1 = new ObjectWithString();
        objectA1._val1 = "a";
        ObjectWithString objectA2 = new ObjectWithString();
        objectA2._val1 = "c";
        ObjectWithString objectA3 = new ObjectWithString();
        objectA3._val1 = "b";
       
        sessionContainer.set(objectA1);
        sessionContainer.set(objectA2);
        sessionContainer.set(objectA3);
       
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        Query query = sessionContainer.query();
        if(ascending){
            query.descend("_val0").orderAscending();
        }else{
            query.descend("_val0").orderDescending();
        }
        if(ascending){
            query.descend("_val1").orderAscending();
        }else{
            query.descend("_val1").orderDescending();
        }
        JODBConfig.setCacheOnSortOperations(false);
        List list = query.execute();
       
        ObjectWithString prev = null;
       
        if(list.size() == 0){
            throw new RuntimeException();
        }
       
        for (int i = 0; i < list.size(); i++) {
            ObjectWithString current = (ObjectWithString) list.get(i);
            if(prev!=null){
                if(ascending){
                    if( prev._val1.charAt(0> current._val1.charAt(0)  ){
                        throw new RuntimeException();
                    }
                }else if(prev._val1.charAt(0< current._val1.charAt(0) ){
                    throw new RuntimeException();
                }
            }
            prev = current;
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
            query = sessionContainer.query();
            if(ascending){
                query.descend("_val1").orderAscending();
            }else{
                query.descend("_val1").orderDescending();
            }
        }
       
        JODBConfig.setCacheOnSortOperations(true);
        list = query.execute();
        if(list.size() == 0){
            throw new RuntimeException();
        }
       
        prev = null;
        for (int i = 0; i < list.size(); i++) {
            ObjectWithString current = (ObjectWithString) list.get(i);
            if(prev!=null){
                if(ascending){
                    if( prev._val1.charAt(0> current._val1.charAt(0)  ){
                        throw new RuntimeException();
                    }
                }else if(prev._val1.charAt(0< current._val1.charAt(0) ){
                    throw new RuntimeException();
                }
            }
            prev = current;
        }
        JODBConfig.setCacheOnSortOperations(true);
        sessionContainer.close();
    }
   
    public void sortingTest(boolean reopen, boolean ascending) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectA objectA1 = new ObjectA((byte)0,(short)2,null);
        ObjectA objectA2 = new ObjectA((byte)0,(short)1,null);
        ObjectA objectA3 = new ObjectA((byte)0,(short)3,null);
       
        sessionContainer.set(objectA1);
        sessionContainer.set(objectA2);
        sessionContainer.set(objectA3);
       
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        Query query = sessionContainer.query();
        if(ascending){
            query.descend("_val1").orderAscending();
        }else{
            query.descend("_val1").orderDescending();
        }
        if(ascending){
            query.descend("_val2").orderAscending();
        }else{
            query.descend("_val2").orderDescending();
        }
        JODBConfig.setCacheOnSortOperations(false);
        List list = query.execute();
        if(list.size() == 0){
            throw new RuntimeException();
        }
       
        ObjectA prev = null;
        for (int i = 0; i < list.size(); i++) {
            ObjectA current = (ObjectA) list.get(i);
            if(prev!=null){
                if(ascending){
                    if( prev.getVal2() > current.getVal2() ){
                        throw new RuntimeException();
                    }
                }else if(prev.getVal2() < current.getVal2() ){
                    throw new RuntimeException();
                }
            }
            prev = current;
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
            query = sessionContainer.query();
            if(ascending){
                query.descend("_val2").orderAscending();
            }else{
                query.descend("_val2").orderDescending();
            }
        }
       
        JODBConfig.setCacheOnSortOperations(true);
        list = query.execute();
        if(list.size() == 0){
            throw new RuntimeException();
        }
       
        prev = null;
        for (int i = 0; i < list.size(); i++) {
            ObjectA current = (ObjectA) list.get(i);
            if(prev!=null){
                if(ascending){
                    if( prev.getVal2() > current.getVal2() ){
                        throw new RuntimeException();
                    }
                }else if(prev.getVal2() < current.getVal2() ){
                    throw new RuntimeException();
                }
            }
            prev = current;
        }
        JODBConfig.setCacheOnSortOperations(true);
        sessionContainer.close();
    }
   
    public void stringFieldQueryCompare(boolean reopen)throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        ObjectWithString objectWithString = new ObjectWithString();
        objectWithString._val1 = "test1";
        sessionContainer.set(objectWithString);
   
        sessionContainer.close();
        sessionContainer = getContainerForFile(testFile);
        Query query = sessionContainer.query();
       
        query.descend("_val1").constrain(objectWithString._val1);
        List list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        ObjectWithString objectWithStringFromDb = (ObjectWithString) list.get(0);
       
        if(!objectWithStringFromDb._val1.equals(objectWithString._val1)){
            throw new RuntimeException();
        }
       
        ObjectWithString objectWithString1 = new ObjectWithString();
        objectWithString1._val1 = "test2";
       
        sessionContainer.set(objectWithString1);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
       
        query.descend("_val1").constrain(objectWithString._val1).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        objectWithStringFromDb = (ObjectWithString) list.get(0);
       
        if(!objectWithStringFromDb._val1.equals(objectWithString1._val1)){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithString._val1);
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        String stringFromDb = (String) list.get(0);
       
        if(!stringFromDb.equals(objectWithString._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithString._val1).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        stringFromDb = (String) list.get(0);
       
        if(!stringFromDb.equals(objectWithString1._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithString._val1).greater();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        stringFromDb = (String) list.get(0);
       
        if(!stringFromDb.equals(objectWithString1._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithString._val1).greater().or(query.constrain(objectWithString._val1));
       
        list = query.execute();
       
        if(list.size()!=2){
            throw new RuntimeException(""+list.size());
        }
        sessionContainer.close();
       
    }
   
    public void enumFieldQueryCompare(boolean reopen)throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,SimpleAddTest.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        Object_Enum objectWithEnum = new Object_Enum();
        objectWithEnum._val1 = TEST_ENUM.ENUM_VAL1;
        sessionContainer.set(objectWithEnum);
   
        sessionContainer.commit();
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
        Query query = sessionContainer.query();
       
        query.descend("_val1").constrain(objectWithEnum._val1);
        List list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException(""+list.size());
        }
       
        Object_Enum objectWithEnumFromDb = (Object_Enum) list.get(0);
       
        if(!objectWithEnumFromDb._val1.equals(objectWithEnum._val1)){
            throw new RuntimeException();
        }
       
        Object_Enum objectWithEnum1 = new Object_Enum();
        objectWithEnum1._val1 = TEST_ENUM.ENUM_VAL2;
       
        sessionContainer.set(objectWithEnum1);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
       
        query.descend("_val1").constrain(objectWithEnum._val1).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        objectWithEnumFromDb = (Object_Enum) list.get(0);
       
        if(!objectWithEnumFromDb._val1.equals(objectWithEnum1._val1)){
            throw new RuntimeException();
        }
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithEnum._val1);
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        Enum enumFromDb = (Enum) list.get(0);
       
        if(!enumFromDb.equals(objectWithEnum._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithEnum._val1).not();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        enumFromDb = (Enum) list.get(0);
       
        if(!enumFromDb.equals(objectWithEnum1._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithEnum._val1).greater();
       
        list = query.execute();
       
        if(list.size()!=1){
            throw new RuntimeException();
        }
       
        enumFromDb = (Enum) list.get(0);
       
        if(!enumFromDb.equals(objectWithEnum1._val1)){
            throw new RuntimeException();
        }
       
        query = sessionContainer.query();
       
        query.constrain(objectWithEnum._val1).greater().or(query.constrain(objectWithEnum._val1));
       
        list = query.execute();
       
        if(list.size()!=2){
            throw new RuntimeException(""+list.size());
        }
        sessionContainer.close();
       
    }


    public JODBSessionContainer getContainerForFile(File file) throws IOException{
        return (JODBSessionContainer) JodbMini.open(file);
    }
   
   
    private static class ObjectAEvaluation implements Evaluation{
        public void evaluate(Candidate candidate) {
            Object cand = candidate.getObject();
            if(cand instanceof ObjectA){
                candidate.include(true);
            }
        }

        public int getActivationDepth() {
            return 5;
        }
    }
   
    private static class ObjectFieldEvaluation implements Evaluation{
        short _value;
       
       
        /**
         * @param value
         */
        public ObjectFieldEvaluation(short value) {
            super();
            _value = value;
        }


        public void evaluate(Candidate candidate) {
            Object cand = candidate.getObject();
            if(cand instanceof Short){
                candidate.include(((Short)cand).shortValue() == _value);
            }
        }


        public int getActivationDepth() {
            return 5;
        }
    }
   
}
TOP

Related Classes of com.mobixess.jodb.tests.QueryTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.