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 TestPersistenceManager.java
Date Sat, 03 Apr 2004 02:33:35 GMT
brianm      2004/04/02 18:33:35

  Modified:    src/java/org/apache/ojb/jdo ExtentImpl.java
                        PersistenceManagerImpl.java
               src/test/org/apache/ojb/jdo TestPersistenceManager.java
  Log:
  Extent now supports multiple iterators, and adheres to spec for iterator management. Still
some tx questions...
  
  Revision  Changes    Path
  1.5       +63 -11    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ExtentImpl.java	3 Apr 2004 01:48:59 -0000	1.4
  +++ ExtentImpl.java	3 Apr 2004 02:33:35 -0000	1.5
  @@ -20,21 +20,26 @@
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryFactory;
  +import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.otm.OTMConnection;
   
   import javax.jdo.Extent;
   import javax.jdo.PersistenceManager;
   import java.util.Iterator;
  +import java.util.Set;
  +import java.util.HashSet;
  +import java.util.NoSuchElementException;
   
   /**
    * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
    */
   public class ExtentImpl implements Extent
   {
  -	private OJBIterator m_iterator;
   	private Class m_clazz;
   	private OTMConnection m_conn;
  -	private PersistenceManager m_persistenceManager;
  +	private PersistenceManager m_pm;
  +
  +    private HashSet m_iterators = new HashSet();
   
   	/**
   	 * Constructor for ExtentImpl.
  @@ -42,16 +47,23 @@
   	public ExtentImpl(Class pClazz, OTMConnection conn, PersistenceManager pm)
   	{
   		m_clazz = pClazz;
  -		Criteria selectExtent = null;
  -		Query q = QueryFactory.newQuery(m_clazz, selectExtent);
   		m_conn = conn;
  -		m_persistenceManager = pm;
  -		m_iterator = (OJBIterator) m_conn.getIteratorByQuery(q);
  +		m_pm = pm;
   	}
   
  +    /**
  +     * @todo is this supposed to operate outside of a user specified tx? Right now it obtains
one if needed
  +     */
   	public Iterator iterator()
   	{
  -		return m_iterator;
  +        Criteria null_crit = null;
  +        QueryByCriteria q = QueryFactory.newQuery(m_clazz, null_crit);
  +        boolean auto = !m_pm.currentTransaction().isActive();
  +        if (auto) m_pm.currentTransaction().begin();
  +        ExtentIterator itty = new ExtentIterator((OJBIterator) m_conn.getIteratorByQuery(q));
  +        if (auto) m_pm.currentTransaction().commit();
  +        m_iterators.add(itty);
  +		return itty;
   	}
   
   	public boolean hasSubclasses()
  @@ -67,19 +79,59 @@
   
   	public PersistenceManager getPersistenceManager()
   	{
  -		return m_persistenceManager;
  +		return m_pm;
   	}
   
   	public void closeAll()
   	{
  -		m_iterator.releaseDbResources();
  +		for (Iterator iterator = m_iterators.iterator(); iterator.hasNext();)
  +        {
  +            ExtentIterator itty = (ExtentIterator) iterator.next();
  +            itty.close();
  +            iterator.remove();
  +        }
   	}
   
   	public void close(Iterator iterator)
   	{
  -		if (iterator instanceof OJBIterator)
  +		if (iterator instanceof ExtentIterator && m_iterators.contains(iterator))
           {
  -			((OJBIterator)iterator).releaseDbResources();
  +            m_iterators.remove(iterator);
  +            ((ExtentIterator)iterator).close();
           }
   	}
  +
  +    private class ExtentIterator implements Iterator
  +    {
  +        private OJBIterator itty;
  +        private boolean closed = false;
  +
  +        ExtentIterator(OJBIterator itty)
  +        {
  +            this.itty = itty;
  +        }
  +
  +        public boolean hasNext()
  +        {
  +            if (closed) return false;
  +            return itty.hasNext();
  +        }
  +
  +        public Object next()
  +        {
  +            if (closed) throw new NoSuchElementException("Calling next() on closed JDO
iterator");
  +            return itty.next();
  +        }
  +
  +        public void remove()
  +        {
  +            throw new UnsupportedOperationException("Operation Not Allowd");
  +        }
  +
  +        private void close()
  +        {
  +            itty.releaseDbResources();
  +            closed = true;
  +        }
  +    }
   }
  
  
  
  1.6       +7 -0      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PersistenceManagerImpl.java	3 Apr 2004 01:48:59 -0000	1.5
  +++ PersistenceManagerImpl.java	3 Apr 2004 02:33:35 -0000	1.6
  @@ -413,9 +413,16 @@
           {
               e.printStackTrace();  //To change body of catch statement use Options | File
Templates.
           }
  +        catch (ClassCastException e)
  +        {
  +            throw new IllegalArgumentException("Object passed as id is not an id: " + o);
  +        }
           return retval;
       }
   
  +    /**
  +     * @todo verify that Identity meets the requirements of the JDO spec
  +     */
       public Object getObjectId(Object o)
       {
           if (isClosed())
  
  
  
  1.2       +28 -3     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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestPersistenceManager.java	3 Apr 2004 01:51:13 -0000	1.1
  +++ TestPersistenceManager.java	3 Apr 2004 02:33:35 -0000	1.2
  @@ -11,6 +11,7 @@
   import org.apache.ojb.otm.Person;
   
   import java.util.Collection;
  +import java.util.Iterator;
   
   public class TestPersistenceManager extends TestCase
   {
  @@ -19,17 +20,41 @@
   
       public void testLoadExtent() throws Exception
       {
  +        Person article = new Person();
           PersistenceManager pm = factory.getPersistenceManager();
           Transaction tx = pm.currentTransaction();
           tx.begin();
  -        Extent extent = pm.getExtent(Article.class, true);
  +        pm.makePersistent(article);
           tx.commit();
  -        assertNotNull(extent);
  +
  +        tx = pm.currentTransaction();
  +        tx.begin();
  +        Extent extent = pm.getExtent(Person.class, true);
  +        Iterator itty = extent.iterator();
  +        assertTrue(itty.hasNext());
  +        tx.commit();
  +    }
  +
  +    /**
  +     * @todo Don't know if this should pass or not
  +     */
  +    public void _testLoadExtentWithoutUserTx() throws Exception
  +    {
  +        Person article = new Person();
  +        PersistenceManager pm = factory.getPersistenceManager();
  +        Transaction tx = pm.currentTransaction();
  +        tx.begin();
  +        pm.makePersistent(article);
  +        tx.commit();
  +
  +        Extent extent = pm.getExtent(Person.class, true);
  +        Iterator itty = extent.iterator();
  +        assertTrue(itty.hasNext());
       }
   
       /**
        * @todo figure out if transactions can be re-begined in JDO
  -     */ 
  +     */
       public void testTwoTransactions() throws Exception
       {
           Person person = new Person();
  
  
  

---------------------------------------------------------------------
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