db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/jdo TestQueries.java TestTransactions.java TestPersistenceManager.java
Date Fri, 09 Apr 2004 01:06:00 GMT
brianm      2004/04/08 18:06:00

  Modified:    src/java/org/apache/ojb/jdo ExtentImpl.java
                        PersistenceManagerImpl.java QueryImpl.java
                        TransactionImpl.java
               src/test/org/apache/ojb/jdo TestPersistenceManager.java
  Added:       src/java/org/apache/ojb/jdo/spi StateManagerImpl.java
               src/test/org/apache/ojb/jdo TestQueries.java
                        TestTransactions.java
  Removed:     src/java/org/apache/ojb/jdo StateManagerImpl.java
  Log:
  * Started to flesh out Query
  * Continue to fill in methods on PersistenceManager
  * Packaging client and spi code seperately (moved StateManagerImpl)
  * JDO Transactions can be committed then began again
  * Test assumptions about behavior around user errors
  
  Revision  Changes    Path
  1.7       +13 -1     db-ojb/src/java/org/apache/ojb/jdo/ExtentImpl.java
  
  Index: ExtentImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/ExtentImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ExtentImpl.java	4 Apr 2004 23:53:38 -0000	1.6
  +++ ExtentImpl.java	9 Apr 2004 01:06:00 -0000	1.7
  @@ -16,6 +16,7 @@
    */
   
   import org.apache.ojb.broker.accesslayer.OJBIterator;
  +import org.apache.ojb.broker.accesslayer.RsIterator;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  @@ -38,7 +39,6 @@
   	private Class m_clazz;
   	private OTMConnection m_conn;
   	private PersistenceManager m_pm;
  -
       private HashSet m_iterators = new HashSet();
   
   	/**
  @@ -51,6 +51,11 @@
   		m_pm = pm;
   	}
   
  +    Class ojbGetClass()
  +    {
  +        return m_clazz;
  +    }
  +
       /**
        * @todo is this supposed to operate outside of a user specified tx? Right now it obtains
one if needed
        */
  @@ -117,7 +122,14 @@
           public Object next()
           {
               if (closed) throw new NoSuchElementException("Calling next() on closed JDO
iterator");
  +            try
  +            {
               return itty.next();
  +            }
  +            catch (RsIterator.ResourceClosedException e)
  +            {
  +                throw new NoSuchElementException("Calling next() on closed JDO iterator");
  +            }
           }
   
           public void remove()
  
  
  
  1.8       +27 -11    db-ojb/src/java/org/apache/ojb/jdo/PersistenceManagerImpl.java
  
  Index: PersistenceManagerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/PersistenceManagerImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PersistenceManagerImpl.java	4 Apr 2004 23:53:38 -0000	1.7
  +++ PersistenceManagerImpl.java	9 Apr 2004 01:06:00 -0000	1.8
  @@ -18,11 +18,13 @@
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.otm.OTMConnection;
  +import org.apache.ojb.otm.OTMKit;
   import org.apache.ojb.otm.copy.ObjectCopyStrategy;
   import org.apache.ojb.otm.kit.SimpleKit;
   import org.apache.ojb.otm.lock.LockType;
   import org.apache.ojb.otm.lock.LockingException;
   import org.apache.ojb.otm.states.State;
  +import org.apache.ojb.jdo.spi.StateManagerImpl;
   
   import javax.jdo.*;
   import javax.jdo.spi.StateManager;
  @@ -36,7 +38,7 @@
   public class PersistenceManagerImpl implements PersistenceManager
   {
       private OTMConnection m_conn;
  -    private SimpleKit m_kit;
  +    private OTMKit m_kit;
       private boolean m_multiThreaded = false;
       private boolean m_ignoreCache = false;
       private PersistenceManagerFactory m_factory;
  @@ -47,7 +49,6 @@
       private TransactionImpl m_tx;
       private StateManager m_stateManager;
   
  -
       public PersistenceManagerImpl(PersistenceManagerFactory factory, String alias, String
userid, String password)
       {
           m_factory = factory;
  @@ -68,6 +69,17 @@
               m_conn = m_kit.acquireConnection(key);
           }
           m_stateManager = new StateManagerImpl(this);
  +        m_tx = new TransactionImpl(this, m_kit, m_conn);
  +    }
  +
  +    OTMConnection getConnection()
  +    {
  +        return this.m_conn;
  +    }
  +
  +    OTMKit getKit()
  +    {
  +        return this.m_kit;
       }
   
       public boolean isClosed()
  @@ -75,25 +87,22 @@
           return m_conn.isClosed();
       }
   
  +    /**
  +     * Close database resources and
  +     * Rollback transaction if there is one in progress
  +     */
       public void close()
       {
           if (isClosed())
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("close()"));
           }
  +        if (m_tx.isActive()) m_tx.rollback();
           m_conn.close();
       }
   
       public Transaction currentTransaction()
       {
  -        if (isClosed())
  -        {
  -            throw new JDOFatalUserException(generateIsClosedErrorMessage("currentTransaction()"));
  -        }
  -        if (m_tx == null || m_tx.isExpired())
  -        {
  -            m_tx = new TransactionImpl(this, m_kit.getTransaction(m_conn));
  -        }
           return m_tx;
       }
   
  @@ -296,7 +305,14 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Object)"));
           }
  -        throw new UnsupportedOperationException("Not yet implemented!");
  +        try
  +        {
  +            return ((QueryImpl)o).ojbClone();
  +        }
  +        catch (ClassCastException e)
  +        {
  +            throw new IllegalArgumentException("newQuery(Object) must be passed a Query
instance");
  +        }
       }
   
       public Query newQuery(String s, Object o)
  
  
  
  1.6       +13 -6     db-ojb/src/java/org/apache/ojb/jdo/QueryImpl.java
  
  Index: QueryImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/QueryImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- QueryImpl.java	4 Apr 2004 23:53:38 -0000	1.5
  +++ QueryImpl.java	9 Apr 2004 01:06:00 -0000	1.6
  @@ -50,19 +50,26 @@
           this.m_candidates = null;
       }
   
  +    QueryImpl ojbClone()
  +    {
  +        QueryImpl query = new QueryImpl(m_pm, m_conn);
  +        query.m_candidates = m_candidates;
  +        query.m_searchClass = m_searchClass;
  +        query.m_criteria = m_criteria;
  +        return query;
  +    }
  +
   	public void setClass(Class aClass)
   	{
           this.m_searchClass = aClass;
   	}
   
  +    /**
  +     * Simply ovewrites the setClass(..) value
  +     */
   	public void setCandidates(Extent extent)
   	{
  -        if (m_candidates == null) m_candidates = new HashSet();
  -        Iterator itty = extent.iterator();
  -        while (itty.hasNext())
  -        {
  -            m_candidates.add(itty.next());
  -        }
  +        this.m_searchClass = ((ExtentImpl)extent).ojbGetClass();
   	}
   
   	public void setCandidates(Collection collection)
  
  
  
  1.5       +32 -20    db-ojb/src/java/org/apache/ojb/jdo/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/TransactionImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TransactionImpl.java	4 Apr 2004 23:53:38 -0000	1.4
  +++ TransactionImpl.java	9 Apr 2004 01:06:00 -0000	1.5
  @@ -15,8 +15,12 @@
    */
   package org.apache.ojb.jdo;
   
  +import org.apache.ojb.otm.OTMKit;
  +import org.apache.ojb.otm.OTMConnection;
  +
   import javax.jdo.Transaction;
   import javax.jdo.PersistenceManager;
  +import javax.jdo.JDOUserException;
   import javax.transaction.Synchronization;
   
   /**
  @@ -25,45 +29,52 @@
   
   public class TransactionImpl implements Transaction
   {
  -	private PersistenceManager m_persistenceManager;
  -	private org.apache.ojb.otm.core.Transaction m_tx;
  -    private boolean m_isExpired = false;
  -
  -	public TransactionImpl(PersistenceManager persistenceManager, org.apache.ojb.otm.core.Transaction
tx)
  -	{
  -		m_persistenceManager = persistenceManager;
  -		m_tx = tx;
  +	private PersistenceManager m_pm;
  +    private OTMKit m_kit;
  +    private OTMConnection m_conn;
  +
  +	public TransactionImpl(PersistenceManager pm, OTMKit kit, OTMConnection conn)
  +	{
  +		m_pm = pm;
  +        m_conn = conn;
  +        m_kit = kit;
   	}
   
  -    boolean isExpired()
  -    {
  -        return this.m_isExpired;
  -    }
  -
   	public void begin()
   	{
  -		m_tx.begin();
  +        if (m_kit.getTransaction(m_conn).isInProgress())
  +        {
  +            throw new JDOUserException("Transaction already in progress");
  +        }
  +        m_kit.getTransaction(m_conn).begin();
   	}
   
   	public void commit()
   	{
  -		m_tx.commit();
  -        m_isExpired = true;
  +        if (!m_kit.getTransaction(m_conn).isInProgress())
  +        {
  +            throw new JDOUserException("Transaction not in progress");
  +        }
  +        m_kit.getTransaction(m_conn).commit();
   	}
   
   	public void rollback()
   	{
  -		m_tx.rollback();
  -        m_isExpired = true;
  +		if (!m_kit.getTransaction(m_conn).isInProgress())
  +        {
  +            throw new JDOUserException("Transaction not in progress");
  +        }
  +        m_kit.getTransaction(m_conn).rollback();
   	}
   
   	public boolean isActive()
   	{
  -		return m_tx.isInProgress();
  +		return m_kit.getTransaction(m_conn).isInProgress();
   	}
   
   	public void setNontransactionalRead(boolean b)
   	{
  +        throw new javax.jdo.JDOUnsupportedOptionException("Unsupported");
   	}
   
   	public boolean getNontransactionalRead()
  @@ -73,6 +84,7 @@
   
   	public void setNontransactionalWrite(boolean b)
   	{
  +        throw new javax.jdo.JDOUnsupportedOptionException("Unsupported");
   	}
   
   	public boolean getNontransactionalWrite()
  @@ -118,6 +130,6 @@
   
   	public PersistenceManager getPersistenceManager()
   	{
  -		return m_persistenceManager;
  +		return m_pm;
   	}
   }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/jdo/spi/StateManagerImpl.java
  
  Index: StateManagerImpl.java
  ===================================================================
  package org.apache.ojb.jdo.spi;
  
  import javax.jdo.spi.StateManager;
  import javax.jdo.spi.PersistenceCapable;
  import javax.jdo.PersistenceManager;
  
  /* Copyright 2003-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /**
   * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
   */
  
  public class StateManagerImpl implements StateManager
  {
  	private PersistenceManager m_persistenceManager;
  
  	public StateManagerImpl(PersistenceManager persistenceManager)
  	{
  		m_persistenceManager = persistenceManager;
  	}
  
  	public byte replacingFlags(PersistenceCapable persistenceCapable)
  	{
  		return 0;
  	}
  
  	public StateManager replacingStateManager(PersistenceCapable persistenceCapable, StateManager
stateManager)
  	{
  		return null;
  	}
  
  	public boolean isDirty(PersistenceCapable persistenceCapable)
  	{
  		return false;
  	}
  
  	public boolean isTransactional(PersistenceCapable persistenceCapable)
  	{
  		return false;
  	}
  
  	public boolean isPersistent(PersistenceCapable persistenceCapable)
  	{
  		return false;
  	}
  
  	public boolean isNew(PersistenceCapable persistenceCapable)
  	{
  		return false;
  	}
  
  	public boolean isDeleted(PersistenceCapable persistenceCapable)
  	{
  		return false;
  	}
  
  	public PersistenceManager getPersistenceManager(PersistenceCapable persistenceCapable)
  	{
  		return null;
  	}
  
  	public void makeDirty(PersistenceCapable persistenceCapable, String s)
  	{
  	}
  
  	public Object getObjectId(PersistenceCapable persistenceCapable)
  	{
  		return null;
  	}
  
  	public Object getTransactionalObjectId(PersistenceCapable persistenceCapable)
  	{
  		return null;
  	}
  
  	public boolean isLoaded(PersistenceCapable persistenceCapable, int i)
  	{
  		return false;
  	}
  
  	public void preSerialize(PersistenceCapable persistenceCapable)
  	{
  	}
  
  	public boolean getBooleanField(PersistenceCapable persistenceCapable, int i, boolean b)
  	{
  		return false;
  	}
  
  	public char getCharField(PersistenceCapable persistenceCapable, int i, char c)
  	{
  		return 0;
  	}
  
  	public byte getByteField(PersistenceCapable persistenceCapable, int i, byte b)
  	{
  		return 0;
  	}
  
  	public short getShortField(PersistenceCapable persistenceCapable, int i, short i1)
  	{
  		return 0;
  	}
  
  	public int getIntField(PersistenceCapable persistenceCapable, int i, int i1)
  	{
  		return 0;
  	}
  
  	public long getLongField(PersistenceCapable persistenceCapable, int i, long l)
  	{
  		return 0;
  	}
  
  	public float getFloatField(PersistenceCapable persistenceCapable, int i, float v)
  	{
  		return 0;
  	}
  
  	public double getDoubleField(PersistenceCapable persistenceCapable, int i, double v)
  	{
  		return 0;
  	}
  
  	public String getStringField(PersistenceCapable persistenceCapable, int i, String s)
  	{
  		return null;
  	}
  
  	public Object getObjectField(PersistenceCapable persistenceCapable, int i, Object o)
  	{
  		return null;
  	}
  
  	public void setBooleanField(PersistenceCapable persistenceCapable, int i, boolean b, boolean
b1)
  	{
  	}
  
  	public void setCharField(PersistenceCapable persistenceCapable, int i, char c, char c1)
  	{
  	}
  
  	public void setByteField(PersistenceCapable persistenceCapable, int i, byte b, byte b1)
  	{
  	}
  
  	public void setShortField(PersistenceCapable persistenceCapable, int i, short i1, short
i2)
  	{
  	}
  
  	public void setIntField(PersistenceCapable persistenceCapable, int i, int i1, int i2)
  	{
  	}
  
  	public void setLongField(PersistenceCapable persistenceCapable, int i, long l, long l1)
  	{
  	}
  
  	public void setFloatField(PersistenceCapable persistenceCapable, int i, float v, float
v1)
  	{
  	}
  
  	public void setDoubleField(PersistenceCapable persistenceCapable, int i, double v, double
v1)
  	{
  	}
  
  	public void setStringField(PersistenceCapable persistenceCapable, int i, String s, String
s1)
  	{
  	}
  
  	public void setObjectField(PersistenceCapable persistenceCapable, int i, Object o, Object
o1)
  	{
  	}
  
  	public void providedBooleanField(PersistenceCapable persistenceCapable, int i, boolean
b)
  	{
  	}
  
  	public void providedCharField(PersistenceCapable persistenceCapable, int i, char c)
  	{
  	}
  
  	public void providedByteField(PersistenceCapable persistenceCapable, int i, byte b)
  	{
  	}
  
  	public void providedShortField(PersistenceCapable persistenceCapable, int i, short i1)
  	{
  	}
  
  	public void providedIntField(PersistenceCapable persistenceCapable, int i, int i1)
  	{
  	}
  
  	public void providedLongField(PersistenceCapable persistenceCapable, int i, long l)
  	{
  	}
  
  	public void providedFloatField(PersistenceCapable persistenceCapable, int i, float v)
  	{
  	}
  
  	public void providedDoubleField(PersistenceCapable persistenceCapable, int i, double v)
  	{
  	}
  
  	public void providedStringField(PersistenceCapable persistenceCapable, int i, String s)
  	{
  	}
  
  	public void providedObjectField(PersistenceCapable persistenceCapable, int i, Object o)
  	{
  	}
  
  	public boolean replacingBooleanField(PersistenceCapable persistenceCapable, int i)
  	{
  		return false;
  	}
  
  	public char replacingCharField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public byte replacingByteField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public short replacingShortField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public int replacingIntField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public long replacingLongField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public float replacingFloatField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public double replacingDoubleField(PersistenceCapable persistenceCapable, int i)
  	{
  		return 0;
  	}
  
  	public String replacingStringField(PersistenceCapable persistenceCapable, int i)
  	{
  		return null;
  	}
  
  	public Object replacingObjectField(PersistenceCapable persistenceCapable, int i)
  	{
  		return null;
  	}
  }
  
  
  
  1.4       +34 -27    db-ojb/src/test/org/apache/ojb/jdo/TestPersistenceManager.java
  
  Index: TestPersistenceManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/jdo/TestPersistenceManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestPersistenceManager.java	4 Apr 2004 23:53:44 -0000	1.3
  +++ TestPersistenceManager.java	9 Apr 2004 01:06:00 -0000	1.4
  @@ -1,5 +1,6 @@
   package org.apache.ojb.jdo;
  -/* Copyright 2004-2004 The Apache Software Foundation
  +
  +/* Copyright 2004 The Apache Software Foundation
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -17,10 +18,7 @@
   import junit.framework.TestCase;
   import org.apache.ojb.otm.Person;
   
  -import javax.jdo.Extent;
  -import javax.jdo.PersistenceManager;
  -import javax.jdo.Query;
  -import javax.jdo.Transaction;
  +import javax.jdo.*;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.NoSuchElementException;
  @@ -39,17 +37,14 @@
           pm.makePersistent(article);
           tx.commit();
   
  -        tx = pm.currentTransaction();
           tx.begin();
           Extent extent = pm.getExtent(Person.class, true);
           Iterator itty = extent.iterator();
           assertTrue(itty.hasNext());
           tx.commit();
  +        pm.close();
       }
   
  -    /**
  -     * @todo Don't know if this should pass or not
  -     */
       public void testIteratorClosedWhenOutsideTx() throws Exception
       {
           Person article = new Person();
  @@ -59,7 +54,6 @@
           pm.makePersistent(article);
           tx.commit();
   
  -        tx = pm.currentTransaction();
           tx.begin();
           Extent extent = pm.getExtent(Person.class, true);
           Iterator itty = extent.iterator();
  @@ -73,27 +67,40 @@
           {
               assertTrue("Flow of control will pass through here", true);
           }
  +        pm.close();
       }
   
  -    /**
  -     * @todo figure out if transactions can be re-begined in JDO
  -     */
  -    public void testTwoTransactions() throws Exception
  +    public void testVerifyCannotQueryExtentWithoutSubclasses()
       {
  -        Person person = new Person();
  -        person.setFirstname("Brian");
  -        person.setLastname("McCallister");
           PersistenceManager pm = factory.getPersistenceManager();
  -        Transaction tx = pm.currentTransaction();
  -        tx.begin();
  -        pm.makePersistent(person);
  -        tx.commit();
  +        pm.currentTransaction().begin();
  +        try
  +        {
  +            pm.getExtent(Person.class, false);
  +            fail("Not supposed to be supported!");
  +        }
  +        catch (UnsupportedOperationException e)
  +        {
  +            assertTrue("Flow goes through here", true);
  +        }
  +        pm.currentTransaction().commit();
  +        pm.close();
  +    }
   
  -        tx = pm.currentTransaction();
  -        tx.begin();
  -        Query q = pm.newQuery(Person.class);
  -        Collection persons = (Collection) q.execute();
  -        tx.commit();
  -        assertTrue(persons.size() > 0);
  +    public void testQueryClassCast()
  +    {
  +        PersistenceManager pm = factory.getPersistenceManager();
  +        pm.currentTransaction().begin();
  +        try
  +        {
  +            pm.newQuery(new Object());
  +            fail("Should not be able to pass incorrect argument to newQuery(Object)");
  +        }
  +        catch (IllegalArgumentException e)
  +        {
  +            assertTrue("Flow goes through here", true);
  +        }
  +        pm.currentTransaction().commit();
  +        pm.close();
       }
   }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/jdo/TestQueries.java
  
  Index: TestQueries.java
  ===================================================================
  package org.apache.ojb.jdo;
  
  import junit.framework.TestCase;
  
  import javax.jdo.PersistenceManager;
  import javax.jdo.Query;
  
  import org.apache.ojb.otm.Person;
  
  import java.util.Collection;
  import java.util.List;
  
  /* Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  public class TestQueries extends TestCase
  {
      private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
  
      private PersistenceManager pm;
  
      public void setUp()
      {
          this.pm = factory.getPersistenceManager();
      }
  
      public void tearDown()
      {
          if (!this.pm.isClosed()) this.pm.close();
      }
  
      public void testEmptyFilter()
      {
          Person p = new Person("George", "Harrison");
          pm.currentTransaction().begin();
          pm.makePersistent(p);
          pm.currentTransaction().commit();
  
          pm.currentTransaction().begin();
          Query q = pm.newQuery(Person.class);
          Collection results = (Collection) q.execute();
          assertTrue(results.size() > 0);
          assertTrue(results.iterator().next() instanceof Person);
          pm.currentTransaction().commit();
      }
  
      public void testEmptyFilterSettingExtent()
      {
          Person p = new Person("George", "Harrison");
          pm.currentTransaction().begin();
          pm.makePersistent(p);
          pm.currentTransaction().commit();
  
          pm.currentTransaction().begin();
          Query q = pm.newQuery();
          q.setCandidates(pm.getExtent(Person.class, true));
          Collection results = (Collection) q.execute();
          assertTrue(results.size() > 0);
          assertTrue(results.iterator().next() instanceof Person);
          pm.currentTransaction().commit();
      }
  
      public void testClonedQuery()
      {
          Person p = new Person("George", "Harrison");
          pm.currentTransaction().begin();
          pm.makePersistent(p);
          pm.currentTransaction().commit();
  
          pm.currentTransaction().begin();
          Query q = pm.newQuery(Person.class);
          q.compile();
          Query q2 = pm.newQuery(q);
          Collection r = (Collection) q2.execute();
          assertTrue(r.size() > 0);
          assertTrue(r.iterator().next() instanceof Person);
          pm.currentTransaction().commit();
      }
  }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/jdo/TestTransactions.java
  
  Index: TestTransactions.java
  ===================================================================
  package org.apache.ojb.jdo;
  
  /* Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import junit.framework.TestCase;
  import org.apache.ojb.otm.Person;
  
  import javax.jdo.JDOUserException;
  import javax.jdo.PersistenceManager;
  import javax.jdo.Query;
  import javax.jdo.Transaction;
  import java.util.Collection;
  
  public class TestTransactions extends TestCase
  {
      /** Cheat and use our impl directly */
      private PersistenceManagerFactoryImpl factory = new PersistenceManagerFactoryImpl();
  
  
      public void testReBeginTx() throws Exception
      {
          Person person = new Person();
          person.setFirstname("Brian");
          person.setLastname("McCallister");
          PersistenceManager pm = factory.getPersistenceManager();
          Transaction tx = pm.currentTransaction();
          tx.begin();
          pm.makePersistent(person);
          tx.commit();
  
          tx.begin();
          Query q = pm.newQuery(Person.class);
          Collection persons = (Collection) q.execute();
          tx.commit();
          assertTrue(persons.size() > 0);
          pm.close();
      }
  
      public void testExceptionOnReBegin()
      {
          PersistenceManager pm = factory.getPersistenceManager();
          Transaction tx = pm.currentTransaction();
          tx.begin();
          try
          {
              tx.begin();
              fail("Should have thrown an exception");
          }
          catch (JDOUserException e)
          {
              assertTrue("Flow passes through here", true);
          }
          pm.close();
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message