Package org.apache.ojb.odmg

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

package org.apache.ojb.odmg;

import java.util.List;

import junit.framework.TestCase;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.TestHelper;
import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
import org.apache.ojb.odmg.shared.Person;
import org.apache.ojb.odmg.shared.PersonImpl;
import org.apache.ojb.odmg.shared.Site;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
import org.odmg.OQLQuery;
import org.odmg.Transaction;
import org.odmg.TransactionNotInProgressException;


/**
* Collection of test cases sent by OJB users
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: UserTestCases.java,v 1.13.2.3 2005/02/19 20:36:20 arminw Exp $
*/
public class UserTestCases extends TestCase
{
    private String databaseName;

    /**
     * Send by Antonio
     * Note: The name attribute was declared as
     * unique in the DB.
     */
    public void testDuplicateInsertion() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        String name = "testDuplicateInsertion_" + System.currentTimeMillis();
        String nameNew = "testDuplicateInsertion_New_" + System.currentTimeMillis();

        db.open(databaseName, Database.OPEN_READ_WRITE);
        //System.out.println("TEST: Database open");

        // insert an object with UNIQUE field NAME="A site"
        //System.out.println("TEST: Insert first object");
        newSite(odmg, name, 2, 1);

        // insert another object with UNIQUE field NAME="A site"
        // This should not create a new object (UNIQUE fields conflict) but
        // should resume gracefuly
        //System.out.println("TEST: Insert second object, should fail");
        try
        {
            newSite(odmg, name, 3, 2);
            assertTrue("We should get a SqlException 'Violation of unique index'", false);
        }
        catch (Exception e)
        {
            // we wait for this exception
            assertTrue(true);
        }

        // insert an object with new UNIQUE field NAME
        // should always work
        //System.out.println("TEST: Insert third object");
        try
        {
            newSite(odmg, nameNew, 1, 2);
            assertTrue(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            assertTrue("This exception should not happend: " + e.getMessage(), false);
            throw e;
        }

        db.close();
        //System.out.println("TEST: Database closed");
    }

    private void newSite(Implementation odmg, String name, int year, int semester) throws Exception
    {
        Transaction tx = null;
        tx = odmg.newTransaction();
        Site site = null;
        tx.begin();

        site = new Site();
        site.setName(name);
        site.setYear(new Integer(year));
        site.setSemester(new Integer(semester));

        tx.lock(site, Transaction.WRITE);
        tx.commit();
    }

    public void testSimpleQueryDelete() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        String name = "testSimpleQueryDelete - " + System.currentTimeMillis();

        db.open(databaseName, Database.OPEN_READ_WRITE);
        Site site = new Site();
        site.setName(name);
        Transaction tx = odmg.newTransaction();
        tx.begin();
        tx.lock(site, Transaction.WRITE);
        tx.commit();

        OQLQuery query = odmg.newOQLQuery();
        query.create("select sites from " + Site.class.getName() + " where name=$1");
        query.bind(name);
        tx.begin();
        List result = (List) query.execute();
        if (result.size() == 0)
        {
            fail("Stored object not found");
        }
        tx.commit();

        tx.begin();
        db.deletePersistent(site);
        tx.commit();

        query = odmg.newOQLQuery();
        query.create("select sites from " + Site.class.getName() + " where name=$1");
        query.bind(name);
        tx.begin();
        List result2 = (List) query.execute();
        if (result2.size() > 0)
        {
            fail("We should not found deleted objects");
        }
        tx.commit();
    }

    /**
     * User test case posted by Charles:
     *
     * Up to now, we've been just using the broker layer. I now have a usecase
        where we will need to use the ODMG layer. We do not want to use implicit
        locking; I want my developers to explicit lock each object to an ODMG
        transaction (implicit locking generates loads of queries for all the proxy
        collections).

        It seems that something 'funny' happens if implicit locking is turned off -
        objects are not marked as being "dirty" when changed - even when they are
        explicitly lock to the transaction.

        As I am a complete novice in the ways of the ODMG, I don't really know where
        to look to sort this issue out so I have added a new test method to
        org.apache.ojb.odmg.UserTestCases (it should be attached to this email).
        Essentially, it creates an object and persists it; retrieves and updates it;
        then flushes the cache, and retrieves it again to ensure the update worked.
        If ImplicitLocking is TRUE, the test passes. If ImplicitLocking is FALSE,
        the test fails.

        I think this is incorrect, and would dearly like this to be resolved.

        thma's comment: IMO this works as designed. objects must be locked to
        an ODMG tx before any modifications are taking place.
        I simply moved the lock two lines up and the test passed.
     */
    public void testImplicitLocking() throws Exception
    {
        OjbConfiguration conf = (OjbConfiguration)OjbConfigurator.getInstance().getConfigurationFor(null);
        boolean useImplicitLocking = conf.useImplicitLocking();
        boolean restore = false;

        // prepare test case
        try
        {
            if(useImplicitLocking)
            {
                restore = true;
                conf.setUseImplicitLocking(false);
            }
            Implementation odmg = OJB.getInstance();
            Database db = odmg.newDatabase();
            db.open(databaseName, Database.OPEN_READ_WRITE);

            String name = "testImplicitLocking - " + System.currentTimeMillis();
            String queryString = "select sites from " + Site.class.getName() + " where name = $1";

            /* Create an object */
            Site site = new Site();
            site.setName(name);

            Transaction tx = odmg.newTransaction();
            tx.begin();
            tx.lock(site, Transaction.WRITE);
            tx.commit();

            /* Retrieve from the object created, and set the year*/
            OQLQuery query = odmg.newOQLQuery();
            query.create(queryString);
            query.bind(name);

            tx.begin();
            List result = (List) query.execute();
            assertEquals(1, result.size());
            site = (Site) result.get(0);
            assertNotNull(site);
            assertNull(site.getYear());
            tx.lock(site, Transaction.WRITE);
            site.setYear(new Integer(2003));

            tx.commit();

            /* Flush the cache, and retrieve the object again */
            query = odmg.newOQLQuery();
            query.create(queryString);
            query.bind(name);
            tx.begin();
            ((HasBroker) tx).getBroker().clearCache();
            result = (List) query.execute();
            assertEquals(1, result.size());
            site = (Site) result.get(0);
            assertNotNull(site);
            assertNotNull("year should not be null", site.getYear());
            tx.commit();

            db.close();
        }
        finally
        {
            // reset configuration
            if(restore)
            {
                conf.setUseImplicitLocking(useImplicitLocking);
            }
        }
    }

    /**
       * store an object and then retrieve it by id.
       */
      public void testStoreRetrieveSameTxn() throws Exception
      {
          Implementation odmg = OJB.getInstance();
          Database db = odmg.newDatabase();
          //open database
          try
          {
            db.open(databaseName, Database.OPEN_READ_WRITE);
          }
          catch (ODMGException ex)
          {
            fail("ODMGException: " + ex.getMessage());
          }
          String name = "testStoreRetrieveSameTxn_" + System.currentTimeMillis();
          Person mum = new PersonImpl();
          mum.setFirstname(name);

          TransactionExt txn = (TransactionExt)odmg.newTransaction();
          txn.begin();
          txn.lock(mum, Transaction.WRITE);
          // System.out.println("locked for write: " + mum);
          txn.commit();

          txn.begin();
          txn.getBroker().clearCache();
          Identity mumId = txn.getBroker().serviceIdentity().buildIdentity(mum);
          Person mum2 = (Person)txn.getBroker().getObjectByIdentity(mumId);
          // System.out.println("retrieved: " + mum2);
          txn.commit();
          db.close();
          assertNotNull(mum2);
          assertEquals(name, mum2.getFirstname());
      }

    public void testRetrieveNonExistent()
  {
    try
    {
      Implementation odmg = OJB.getInstance();
      Database db = odmg.newDatabase();
      //open database
      try
      {
        db.open(databaseName, Database.OPEN_READ_WRITE);
      }
      catch (ODMGException ex)
      {
        fail("ODMGException: " + ex.getMessage());
      }
      // construct an id that does not exist in the database
      Identity id = new Identity(PersonImpl.class, Person.class,  new Integer[]{new Integer(-1)});
      TransactionImpl txn = (TransactionImpl)odmg.newTransaction();
      txn.begin();
      txn.getObjectByIdentity(id);
      txn.abort();
    }
    catch (Exception exc)
    {
      exc.printStackTrace();
      fail("caught unexpected exception: " + exc.toString());
    }
  }

  public void testRetrieveOutsideTxn()
  {
    try
    {
      Implementation odmg = OJB.getInstance();
      Database db = odmg.newDatabase();
      //open database
      try
      {
        db.open(databaseName, Database.OPEN_READ_WRITE);
      }
      catch (ODMGException ex)
      {
        fail("ODMGException: " + ex.getMessage());
      }
      // construct an id that does not exist in the database
      Identity id = new Identity(Person.class, Person.class, new Integer[]{new Integer(-1)});
      TransactionImpl txn = (TransactionImpl)odmg.newTransaction();
      try
      {
        txn.getObjectByIdentity(id);
        fail("expected TransactionNotInProgressException not thrown");
      }
      catch (TransactionNotInProgressException exc)
      {
        // expected.
      }
    }
    catch (Exception exc)
    {
      exc.printStackTrace();
      fail("caught unexpected exception: " + exc.toString());
    }
  }

    public static void main(String[] args)
    {
        String[] arr = {UserTestCases.class.getName()};
        junit.textui.TestRunner.main(arr);
//        new UserTestCases().init();
    }

//    private void init()
//    {
//        try
//        {
//            setUp();
//            testDuplicateInsertion();
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//    }

    public void setUp()
    {
        databaseName = TestHelper.DEF_DATABASE_NAME;
    }

    protected void tearDown() throws Exception
    {
        super.tearDown();
    }

}
TOP

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

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.