You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/03/14 11:46:19 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/odmg UserTestCases.java

arminw      2003/03/14 02:46:19

  Modified:    src/java/org/apache/ojb/odmg TransactionImpl.java
               src/test/org/apache/ojb/odmg UserTestCases.java
  Log:
  Fix bug + test cases posted by
  Oliver Matz:
  I found and fixed a minor bug in odmg.TransactionImpl
  It occurs when getObjectByIdentity() is called while
  the broker is null.  It results in a NPE.
  
  Revision  Changes    Path
  1.37      +3 -3      db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- TransactionImpl.java	14 Mar 2003 01:35:06 -0000	1.36
  +++ TransactionImpl.java	14 Mar 2003 10:46:19 -0000	1.37
  @@ -678,15 +678,15 @@
       public Object getObjectByIdentity(Identity id)
               throws PersistenceBrokerException
       {
  +        checkOpen();
           ObjectEnvelope envelope = objectEnvelopeTable.getByIdentity(id);
  -
           if (envelope != null)
           {
               return (envelope.needsDelete() ? null : envelope.getObject());
           }
           else
           {
  -            return broker.getObjectByIdentity(id);
  +            return getBroker().getObjectByIdentity(id);
           }
       }
   
  
  
  
  1.10      +111 -5    db-ojb/src/test/org/apache/ojb/odmg/UserTestCases.java
  
  Index: UserTestCases.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/UserTestCases.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- UserTestCases.java	10 Mar 2003 06:07:21 -0000	1.9
  +++ UserTestCases.java	14 Mar 2003 10:46:19 -0000	1.10
  @@ -5,12 +5,15 @@
   import junit.framework.TestCase;
   
   import org.apache.ojb.broker.TestHelper;
  +import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.util.configuration.impl.OjbConfiguration;
   import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
   import org.odmg.Database;
   import org.odmg.Implementation;
   import org.odmg.OQLQuery;
   import org.odmg.Transaction;
  +import org.odmg.ODMGException;
  +import org.odmg.TransactionNotInProgressException;
   
   
   /**
  @@ -38,7 +41,7 @@
   
           // insert an object with UNIQUE field NAME="A site"
           //System.out.println("TEST: Insert first object");
  -        newSite(odmg, "A site", 2, 1);
  +        newSite(odmg, "testDuplicateInsertion", 2, 1);
   
           // insert another object with UNIQUE field NAME="A site"
           // This should not create a new object (UNIQUE fields conflict) but
  @@ -46,7 +49,7 @@
           //System.out.println("TEST: Insert second object, should fail");
           try
           {
  -            newSite(odmg, "A site", 3, 2);
  +            newSite(odmg, "testDuplicateInsertion", 3, 2);
               assertTrue("We should get a SqlException 'Violation of unique index'", false);
           }
           catch (Exception e)
  @@ -153,7 +156,7 @@
           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.
  @@ -201,7 +204,7 @@
               assertNull(site.getYear());
               tx.lock(site, Transaction.UPGRADE);
               site.setYear(new Integer(2003));
  -            
  +
               tx.commit();
   
               /* Flush the cache, and retrieve the object again */
  @@ -229,6 +232,109 @@
           }
       }
   
  +    /**
  +       * store an object and then retrieve it by id.
  +       */
  +      public void testStoreRetrieveSameTxn()
  +      {
  +        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());
  +          }
  +
  +          Person mum = new PersonImpl();
  +          TransactionImpl txn = (TransactionImpl)odmg.newTransaction();
  +          txn.begin();
  +          txn.lock(mum, Transaction.WRITE);
  +          System.out.println("locked for write: " + mum);
  +          Identity mumId = new Identity(mum, txn.getBroker());
  +          txn.commit();
  +          txn.begin();
  +
  +          Person mum2 = (Person)txn.getObjectByIdentity(mumId);
  +          System.out.println("retrieved: " + mum2);
  +          txn.commit();
  +          db.close();
  +        }
  +        catch (Exception exc)
  +        {
  +          exc.printStackTrace();
  +          fail("caught unexpected exception: " + exc.toString());
  +        }
  +      }
  +
  +    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, 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, 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)
       {