Package com.mobixess.jodb.tests

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

/*
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.Comparator;
import java.util.Random;

import com.mobixess.jodb.core.JODBSessionContainer;
import com.mobixess.jodb.core.JodbMini;
import com.mobixess.jodb.query.api.ObjectSet;
import com.mobixess.jodb.query.api.Predicate;
import com.mobixess.jodb.tests.testobjects.ObjectA;
import com.mobixess.jodb.tests.testobjects.ObjectB;
import com.mobixess.jodb.tests.testobjects.Pilot;

public class NQTests {
    protected static int _testCounter;
    protected static String TEST_DATA_DIR = "./testData/NQueryTests/";
    protected int _testAccessField;
    private boolean _checkOptimization;
   
   
    public NQTests(boolean checkOptimization) {
        _checkOptimization = checkOptimization;
    }
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        NQTests queryTests = new NQTests(false);
        queryTests.queryOuterAccessTest();
        queryTests.sortingUnoptimizedWithIndex(true);
        queryTests.sortingUnoptimizedWithIndex(false);
        queryTests.sortingUnoptimized(true);
        queryTests.sortingUnoptimized(false);
        queryTests.sortingAndIndexing(true);
        queryTests.sortingAndIndexing(false);
        queryTests.sorting(true);
        queryTests.sorting(false);
        queryTests.simpleNQuery1(false);
        queryTests.simpleNQuery1(true);
        queryTests.directFieldRef(false);
        queryTests.directFieldRef(true);
        queryTests.unoptimized(true);
        queryTests.unoptimized(false);
        queryTests.simpleNQuery(false);
        queryTests.simpleNQuery(true);
        System.out.println("Test finished");
    }
   
    public JODBSessionContainer getContainerForFile(File file) throws IOException{
        return (JODBSessionContainer) JodbMini.open(file);
    }
   
   
    public void queryOuterAccessTest() throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);

       
        Predicate<Pilot> predicate= new Predicate<Pilot>() {
            public boolean match(Pilot pilot) {
                outerAccessMethod();
                return pilot.getPoints() > 100 && pilot.getPoints() < 500 && pilot.getName().startsWith("Nam");
            }
           
            public boolean outerAccessMethod(){
                return _testAccessField == 0;
            }
        };
       
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
       
       
        Predicate<ObjectB> predicate1= new Predicate<ObjectB>() {
            public boolean match(ObjectB objectB) {
                return objectB._val1>0;
            }
        };
       
        Comparator<ObjectB> comparator = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                outerAccessMethod();
                return o1._val4 - o2._val4;
            }
           
            public boolean outerAccessMethod(){
                return _testAccessField == 0;
            }
           
        };
       
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate1, comparator)){
            throw new RuntimeException();
        }
       
       
        sessionContainer.close();
    }

    public void simpleNQuery1(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
        Random random = new Random(19287);
        for (int i = 0; i < 1000; i++) {
            int next = random.nextInt(1000);
            Pilot pilot = new Pilot(next, "Name"+i);
            sessionContainer.set(pilot);
        }
       
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<Pilot> predicate= new Predicate<Pilot>() {
            public boolean match(Pilot pilot) {
                return pilot.getPoints() > 100 && pilot.getPoints() < 500 && pilot.getName().startsWith("Nam");
            }
        };
       
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
       
        ObjectSet<Pilot> pilots = sessionContainer.query(predicate);
       
        if(pilots.size() == 0){
            throw new RuntimeException();
        }
       
        while (pilots.hasNext()) {
            if(pilots.next().getPoints()<=100){
                throw new RuntimeException();
            }
        }
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        predicate= new Predicate<Pilot>() {
            public boolean match(Pilot pilot) {
                return pilot.getPoints() > 100 && pilot.getPoints() < 500 && pilot._name.startsWith("Nam");
            }
        };
       
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
       
        pilots = sessionContainer.query(predicate);
       
        if(pilots.size() == 0){
            throw new RuntimeException();
        }
       
        while (pilots.hasNext()) {
            if(pilots.next().getPoints()<=100){
                throw new RuntimeException();
            }
        }
        sessionContainer.close();
    }
   
    public void simpleNQuery(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.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);
        }
       
        Predicate<ObjectA> predicate = new Predicate<ObjectA>(){
            @Override
            public boolean match(ObjectA candidate) throws IOException {
                if(candidate.getVal1()==4){
                    return true;
                }
                return false;
            }
           
        };
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectA> result = sessionContainer.query(predicate);
        if(result.size() != 1){
            throw new RuntimeException(""+result.size());
        }
        ObjectA objectA2 = result.get(0);
        if(!objectA2.equals(objectA1)){
            throw new RuntimeException();
        }
       
        sessionContainer.close();
//        objectWithStringfromDb = (ObjectWithString) list.get(0);
//        if(!objectWithStringfromDb._val1.equals(objectWithStringPattern._val1 )){
//            throw new RuntimeException();
//        }
       
    }
   
    public void directFieldRef(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.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;
        objectB._val1 = 4;
       
        ObjectB objectB2 = new ObjectB();
        objectB2._val1 =10;
        sessionContainer.set(objectA);
        sessionContainer.set(objectA1);
        sessionContainer.set(objectB);
        sessionContainer.set(objectB2);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>(){
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                if(candidate._val1==10){
                    return true;
                }
                return false;
            }
           
        };
       
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
       
        ObjectSet<ObjectB> result = sessionContainer.query(predicate);
        if(result.size() != 1){
            throw new RuntimeException(""+result.size());
        }
        ObjectB objectB3 = result.get(0);
        if(!objectB3.equals(objectB2)){
            throw new RuntimeException();
        }
       
        sessionContainer.close();
    }
   
    public void sorting(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
       
        //sessionContainer.configureIndex(ObjectB.class, "_val4", true);
       
        Random random = new Random(237468);
        for (int i = 0; i < 1000; ++i) {
            ObjectB objectB = new ObjectB();
            objectB._val4 =  random.nextInt(1000);
            sessionContainer.set(objectB);
        }

        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>(){
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                if(candidate._val4>100){
                    return true;
                }
                return false;
            }
           
        };
        Comparator<ObjectB> comparatorForward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o1._val4 - o2._val4;
            }
           
        };
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, comparatorForward)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectB> result = sessionContainer.query(predicate, comparatorForward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        ObjectB prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4>next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        Comparator<ObjectB> comparatorBackward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o2._val4 - o1._val4;
            }
           
        };
       
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, comparatorBackward)){
            throw new RuntimeException();
        }
       
        result = sessionContainer.query(predicate, comparatorBackward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4<next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        sessionContainer.close();
    }
   
   
   
   
   
    @SuppressWarnings({ "serial", "deprecation" })
    public void sortingAndIndexing(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
       
        sessionContainer.configureIndex(ObjectB.class, "_val4", true);
       
        Random random = new Random(237468);
        for (int i = 0; i < 1000; ++i) {
            ObjectB objectB = new ObjectB();
            objectB._val4 =  random.nextInt(1000);
            sessionContainer.set(objectB);
        }

        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>() {
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                if(candidate._val4>100){
                    return true;
                }
                return false;
            }
           
        } ;
        Comparator<ObjectB> comparatorForward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o1._val4 - o2._val4;
            }
           
        };
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, comparatorForward)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectB> result = sessionContainer.query(predicate, comparatorForward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        ObjectB prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4>next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        Comparator<ObjectB> comparatorBackward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o2._val4 - o1._val4;
            }
           
        };
       
        if(_checkOptimization && !sessionContainer.isOptimizedQuery(predicate, comparatorBackward)){
            throw new RuntimeException();
        }
       
        result = sessionContainer.query(predicate, comparatorBackward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4<next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        sessionContainer.close();
    }
   
    public void unoptimized(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        final String findString = "find me";
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.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;
        objectB._val1 = 4;
        objectB._var5 = findString;
       
        ObjectB objectB2 = new ObjectB();
        objectB2._val1 =10;
        sessionContainer.set(objectA);
        sessionContainer.set(objectA1);
        sessionContainer.set(objectB);
        sessionContainer.set(objectB2);
        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>(){
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                unoptimize(candidate);
                if(candidate._var5!=null && candidate._var5.equals(findString)){
                    return true;
                }
                return false;
            }
           
            public void unoptimize(ObjectB predicate){
               
            }
           
        };
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, null)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectB> result = sessionContainer.query(predicate);
        if(result.size() != 1){
            throw new RuntimeException(""+result.size());
        }
        ObjectB objectB3 = result.get(0);
        if(!findString.equals(objectB3._var5)){
            throw new RuntimeException();
        }
       
        sessionContainer.close();
    }
   
   
    @SuppressWarnings({ "serial", "deprecation" })
    public void sortingUnoptimized(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
       
        //sessionContainer.configureIndex(ObjectB.class, "_val4", true);
       
        Random random = new Random(237468);
        for (int i = 0; i < 1000; ++i) {
            ObjectB objectB = new ObjectB();
            objectB._val4 =  random.nextInt(1000);
            sessionContainer.set(objectB);
        }

        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>(){
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                unoptimize(candidate);
                if(candidate._val4>100){
                    return true;
                }
                return false;
            }
           
            void unoptimize(ObjectB candidate){
               
            }
           
        };
        Comparator<ObjectB> comparatorForward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o1._val4 - o2._val4;
            }
           
        };
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, comparatorForward)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectB> result = sessionContainer.query(predicate, comparatorForward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        ObjectB prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4>next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        Comparator<ObjectB> comparatorBackward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o2._val4 - o1._val4;
            }
           
        };
       
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, comparatorBackward)){
            throw new RuntimeException();
        }
       
        result = sessionContainer.query(predicate, comparatorBackward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4<next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        sessionContainer.close();
    }
   
   
   
    @SuppressWarnings({ "serial", "deprecation" })
    public void sortingUnoptimizedWithIndex(boolean reopen) throws Exception{
        File testFileDir = new File(TEST_DATA_DIR);
        testFileDir.mkdirs();
        File testFile = new File(testFileDir,NQTests.class.getSimpleName()+(_testCounter++)+".jdb");
        testFile.delete();
        JODBSessionContainer sessionContainer = getContainerForFile(testFile);
       
        sessionContainer.configureIndex(ObjectB.class, "_val4", true);
       
        Random random = new Random(237468);
        for (int i = 0; i < 1000; ++i) {
            ObjectB objectB = new ObjectB();
            objectB._val4 =  random.nextInt(1000);
            sessionContainer.set(objectB);
        }

        sessionContainer.commit();
       
        if(reopen){
            sessionContainer.close();
            sessionContainer = getContainerForFile(testFile);
        }
       
        Predicate<ObjectB> predicate = new Predicate<ObjectB>(){
            @Override
            public boolean match(ObjectB candidate) throws IOException {
                unoptimize(candidate);
                if(candidate._val4>100){
                    return true;
                }
                return false;
            }
           
            void unoptimize(ObjectB candidate){
               
            }
           
        };
        Comparator<ObjectB> comparatorForward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o1._val4 - o2._val4;
            }
           
        };
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, comparatorForward)){
            throw new RuntimeException();
        }
        ObjectSet<ObjectB> result = sessionContainer.query(predicate, comparatorForward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        ObjectB prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4>next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        Comparator<ObjectB> comparatorBackward = new Comparator<ObjectB>(){

            public int compare(ObjectB o1, ObjectB o2) {
                return o2._val4 - o1._val4;
            }
           
        };
       
        if(_checkOptimization && sessionContainer.isOptimizedQuery(predicate, comparatorBackward)){
            throw new RuntimeException();
        }
       
        result = sessionContainer.query(predicate, comparatorBackward);
        if(result.size() == 0){
            throw new RuntimeException(""+result.size());
        }
        prev = null;
        while (result.hasNext()) {
            ObjectB next = result.next();
            if(prev != null && prev._val4<next._val4){
                throw new RuntimeException();
            }
            prev = next;
        }
       
        sessionContainer.close();
    }
}
TOP

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

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.