Package org.apache.ojb.odmg

Source Code of org.apache.ojb.odmg.ScrollableQueryResultsTest

package org.apache.ojb.odmg;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Iterator;

import junit.framework.TestCase;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.TestHelper;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.OQLQuery;
import org.odmg.QueryInvalidException;
import org.odmg.Transaction;

/**
*
* @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
* @version $Id: ScrollableQueryResultsTest.java,v 1.18 2003/11/26 17:33:48 brj Exp $
*/
public class ScrollableQueryResultsTest extends TestCase
{

    private static Class CLASS = ScrollableQueryResultsTest.class;
    private String databaseName;
    private static final int CONTROL_SIZE = 50;

    public static void main(String[] args)
    {
        String[] arr = {CLASS.getName()};
        junit.textui.TestRunner.main(arr);
    }

    /**
     * Insert the method's description here.
     * Creation date: (06.12.2000 21:58:53)
     */
    public void setUp()
    {
        databaseName = TestHelper.DEF_DATABASE_NAME;
    }

    /**
     * Insert the method's description here.
     * Creation date: (06.12.2000 21:59:14)
     */
    public void tearDown()
    {
        databaseName = null;
    }

    public ScrollableQueryResultsTest(String name)
    {
        super(name);
    }

    private void createData(Database db, Implementation odmg) throws Exception
    {
//        Implementation odmg = OJB.getInstance();
        Transaction tx = odmg.newTransaction();
//        db.open(databaseName, Database.OPEN_READ_WRITE);
        tx.begin();
        for (int i = 1; i <= CONTROL_SIZE; i++)
        {
            org.apache.ojb.broker.Person aPerson =
                    new org.apache.ojb.broker.Person();
            aPerson.setId(i);
            aPerson.setFirstname("firstname" + i);
            aPerson.setLastname("lastname" + i);
            db.makePersistent(aPerson);
        }
        tx.commit();
    }

    /**
     * test removing all
     */
    public void testRemoveAll() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();

        db.open(databaseName, Database.OPEN_READ_WRITE);

        // 1. Get a list of some articles
        Transaction tx = odmg.newTransaction();
        tx.begin();

        OQLQuery query = odmg.newOQLQuery();
        String oql =
                "select allPersons from "
                + org.apache.ojb.broker.Person.class.getName();
        query.create(oql);
        ManageableCollection allPersons =
                (ManageableCollection) query.execute();

        // Iterator over the restricted articles objects
        java.util.Iterator it = allPersons.ojbIterator();
        while (it.hasNext())
        {
            db.deletePersistent(it.next());
        }
        tx.commit();
    }

    private void removeAllData(Database db, Implementation odmg) throws Exception
    {
//        Implementation odmg = OJB.getInstance();
//        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();
        tx.begin();
        OQLQuery query = odmg.newOQLQuery();
        String sql =
                "select allPersons from "
                + org.apache.ojb.broker.Person.class.getName();
        query.create(sql);
        ManageableCollection allPersons =
                (ManageableCollection) query.execute();
        Iterator it = allPersons.ojbIterator();
        while (it.hasNext())
        {
            db.deletePersistent(it.next());
        }
        tx.commit();
    }

    /**
     * test getting all (make sure basic operation is still functional)
     */
    public void testGetAllUnrestricted() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        try
        {
            // 1. remove all data
            removeAllData(db, odmg);
            // 2. Insert a bunch of articles objects into the database

            createData(db, odmg);

            // 3. Get a list of some articles
            Transaction tx = odmg.newTransaction();
            tx.begin();

            OQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select allPersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql);

            ManageableCollection allPersons =
                    (ManageableCollection) query.execute();

            // Iterator over the restricted articles objects
            java.util.Iterator it = allPersons.ojbIterator();
            int count = 0;
            while (it.hasNext())
            {
                it.next();
                count++;
            }
            tx.commit();
            // check that we got the right amount back.
            if (count != (CONTROL_SIZE))
            {
                fail(
                        "count not right, found <"
                        + count
                        + "> should have got <"
                        + (CONTROL_SIZE)
                        + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
            }
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.out);
            fail("testGetAllUnrestricted: " + t.getMessage());
        }
        finally
        {
            db.close();
        }
    }

    /**
     * test starting at an index and ending at an index.
     */

    public void testGetSomeA() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        int start = 10;
        int end = 15;
        try
        {
            // 1. remove all data
            removeAllData(db, odmg);
            // 2. Insert a bunch of articles objects into the database

            createData(db, odmg);

            // 3. Get a list of some articles
            Transaction tx = odmg.newTransaction();
            tx.begin();

            EnhancedOQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select somePersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql, start, end);

            ManageableCollection somePersons =
                    (ManageableCollection) query.execute();

            // Iterator over the restricted articles objects
            java.util.Iterator it = somePersons.ojbIterator();
            int count = 0;
            while (it.hasNext())
            {
                it.next();
                count++;
            }
            tx.commit();
            // check that we got the right amount back.
            if (count != (end - start + 1))
            {
                fail(
                        "count not right, found <"
                        + count
                        + "> should have got <"
                        + (end - start)
                        + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
            }
        }
        finally
        {
            db.close();
        }
    }

    /**
     * test start at beginning, and go to an end index.
     */
    public void testGetSomeB() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        int start = Query.NO_START_AT_INDEX;
        int end = 15;
        try
        {
            // 1. remove all data
            removeAllData(db, odmg);
            // 2. Insert a bunch of articles objects into the database

            createData(db, odmg);

            // 3. Get a list of some articles

            Transaction tx = odmg.newTransaction();
            tx.begin();

            EnhancedOQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select somePersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql, start, end);

            ManageableCollection somePersons =
                    (ManageableCollection) query.execute();

            // Iterator over the restricted articles objects
            java.util.Iterator it = somePersons.ojbIterator();
            int count = 0;
            while (it.hasNext())
            {
                it.next();
                count++;
            }
            tx.commit();
            // check that we got the right amount back.
            if (count != end)
            {
                fail(
                        "count not right, found <"
                        + count
                        + "> should have got <"
                        + (end)
                        + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
            }
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.out);
            fail("testGetSomeB: " + t.getMessage());
        }
    }

    /**
     * test starting at a specific place, and have no ending index
     */
    public void testGetSomeC() throws Exception
    {

        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        int start = 10;
        int end = Query.NO_END_AT_INDEX;
        try
        {
            // 1. remove all data
            removeAllData(db, odmg);
            // 2. Insert a bunch of articles objects into the database

            createData(db, odmg);

            // 3. Get a list of some articles

            TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
            tx.begin();
            PersistenceBroker broker = tx.getBroker();

            Connection conn =
                    broker
                    .serviceConnectionManager()
                    .getConnection();
            /**
             * only execute this test if scrolling is supported.
             */
            if (!conn
                    .getMetaData()
                    .supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
            {
                tx.commit();
                return;
            }

            EnhancedOQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select somePersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql, start, end);
            ManageableCollection somePersons =
                    (ManageableCollection) query.execute();
            // Iterator over the restricted articles objects
            java.util.Iterator it = somePersons.ojbIterator();
            int count = 0;
            while (it.hasNext())
            {
                it.next();
                count++;
            }
            tx.commit();
            // check that we got the right amount back.
            if (count != (CONTROL_SIZE - start + 1)) /* +1 because the last row is inclusive */
            {
                fail(
                        "count not right, found <"
                        + count
                        + "> should have got <"
                        + (CONTROL_SIZE - start + 1)
                        + "> This failure is expected if your driver doesn't support advanced JDBC operations.");
            }
        }
        finally
        {
            db.close();
        }

    }

    /**
     * test the condition where start is after end.
     */
    public void testGetSomeD() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        int start = 10;
        int end = 5;
        Transaction tx = odmg.newTransaction();
        try
        {
            tx.begin();
            EnhancedOQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select somePersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql, start, end);
            query.execute();
            fail("should have thrown QueryInvalidException");
        }
        catch (QueryInvalidException iqe)
        {
            // we wait for this exception
            assertTrue(true);
            tx.abort();
        }
        finally
        {
            db.close();
        }
    }

    /**
     * test condition where start and end are the same.
     */
    public void testGetSomeE() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        int start = 10;
        int end = 10;
        try
        {
            Transaction tx = odmg.newTransaction();
            tx.begin();
            EnhancedOQLQuery query = odmg.newOQLQuery();
            String sql =
                    "select somePersons from "
                    + org.apache.ojb.broker.Person.class.getName();
            query.create(sql, start, end);
            query.execute();
            fail("should have thrown QueryInvalidException");
        }
        catch (QueryInvalidException iqe)
        {
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.out);
            fail("testGetSomeC: " + t.getMessage());
        }
    }
}
TOP

Related Classes of org.apache.ojb.odmg.ScrollableQueryResultsTest

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.