db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb repository_junit_odmg.xml
Date Mon, 09 May 2005 14:26:55 GMT
arminw      2005/05/09 07:26:55

  Modified:    src/java/org/apache/ojb/broker/core Tag: OJB_1_0_RELEASE
                        MtoNBroker.java
               src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
                        BrokerHelper.java
               src/java/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        MtoNImplementor.java
               src/test/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        MtoNTest.java
               src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
                        M2NTest.java
               src/test/org/apache/ojb Tag: OJB_1_0_RELEASE
                        repository_junit_odmg.xml
  Log:
  - fix OJB-39, multiple m:n references problem when same referenced types are used
  - add new test, fix odmg test cases, fix mapping
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.10.2.6  +3 -21     db-ojb/src/java/org/apache/ojb/broker/core/MtoNBroker.java
  
  Index: MtoNBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/MtoNBroker.java,v
  retrieving revision 1.10.2.5
  retrieving revision 1.10.2.6
  diff -u -r1.10.2.5 -r1.10.2.6
  --- MtoNBroker.java	18 Mar 2005 19:25:05 -0000	1.10.2.5
  +++ MtoNBroker.java	9 May 2005 14:26:55 -0000	1.10.2.6
  @@ -19,7 +19,6 @@
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
  -import java.util.Vector;
   
   import org.apache.commons.lang.builder.EqualsBuilder;
   import org.apache.commons.lang.builder.HashCodeBuilder;
  @@ -265,8 +264,8 @@
           // handle extents on teh right class
           ClassDescriptor leftCld = dr.getDescriptorFor(leftClass);
           ClassDescriptor rightCld = dr.getDescriptorFor(rightClass);
  -        Vector leftColds = leftCld.getCollectionDescriptors();
  -        CollectionDescriptor wanted = findCollectionDescriptor(leftClass, rightClass, leftColds);
  +        //Vector leftColds = leftCld.getCollectionDescriptors();
  +        CollectionDescriptor wanted = m2nImpl.getLeftDescriptor();
   
           if(leftObject == null || rightObject == null)
           {
  @@ -305,23 +304,6 @@
           }
       }
   
  -    private CollectionDescriptor findCollectionDescriptor(Class leftClass, Class rightClass,
Vector leftColds)
  -    {
  -        for(Iterator iter = leftColds.iterator(); iter.hasNext();)
  -        {
  -            CollectionDescriptor element = (CollectionDescriptor) iter.next();
  -
  -            //try super classes and interfaces too
  -            if(element.getItemClass().isAssignableFrom(rightClass))
  -            {
  -                //we found it !
  -                return element;
  -            }
  -        }
  -        throw new PersistenceBrokerException("Can't find reasonable collection descriptor
for MtoN implementor left[" + leftClass
  -                + "] right[" + rightClass + "]");
  -    }
  -
       private String[] mergeColumns(String[] first, String[] second)
       {
           String[] cols = new String[first.length + second.length];
  
  
  
  No                   revision
  No                   revision
  1.57.2.15 +5 -5      db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.57.2.14
  retrieving revision 1.57.2.15
  diff -u -r1.57.2.14 -r1.57.2.15
  --- BrokerHelper.java	26 Apr 2005 03:41:36 -0000	1.57.2.14
  +++ BrokerHelper.java	9 May 2005 14:26:55 -0000	1.57.2.15
  @@ -916,7 +916,7 @@
       {
           if(cds.isMtoNRelation())
           {
  -            m_broker.deleteMtoNImplementor(new MtoNImplementor(source, referenceToUnlink));
  +            m_broker.deleteMtoNImplementor(new MtoNImplementor(cds, source, referenceToUnlink));
           }
           else
           {
  @@ -943,15 +943,15 @@
       /**
        * Link a single 1:n or m:n object.
        *
  -     * @param source The source object with reference.
  -     * @param cds The {@link org.apache.ojb.broker.metadata.CollectionDescriptor} of the
relation.
  +     * @param source The source object with the declared reference.
  +     * @param cds The {@link org.apache.ojb.broker.metadata.CollectionDescriptor} of the
relation declared in source object.
        * @param referenceToLink The referenced object to link.
        */
       public void link(Object source, CollectionDescriptor cds, Object referenceToLink)
       {
           if(cds.isMtoNRelation())
           {
  -            m_broker.addMtoNImplementor(new MtoNImplementor(source, referenceToLink));
  +            m_broker.addMtoNImplementor(new MtoNImplementor(cds, source, referenceToLink));
           }
           else
           {
  
  
  
  No                   revision
  No                   revision
  1.7.2.3   +28 -116   db-ojb/src/java/org/apache/ojb/broker/MtoNImplementor.java
  
  Index: MtoNImplementor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/MtoNImplementor.java,v
  retrieving revision 1.7.2.2
  retrieving revision 1.7.2.3
  diff -u -r1.7.2.2 -r1.7.2.3
  --- MtoNImplementor.java	18 Mar 2005 19:25:05 -0000	1.7.2.2
  +++ MtoNImplementor.java	9 May 2005 14:26:55 -0000	1.7.2.3
  @@ -21,6 +21,8 @@
   
   /**
    * Helper class to handle single m:n relation entries (m:n indirection table entries).
  + * <br/>
  + * The "left / right" notation is only used to differ both sides of the relation.
    *
    * @author <a href="leandro@ibnetwork.com.br">Leandro Rodrigo Saad Cruz</a>
    * @version $Id$
  @@ -31,46 +33,57 @@
       private Object rightObject;
       private Class leftClass;
       private Class rightClass;
  -    //private CollectionDescriptor collectionDesc;
  -    //private PersistenceBroker broker;
  +    private CollectionDescriptor leftDescriptor;
   
       /**
        * @deprecated
        */
  -    public MtoNImplementor(PersistenceBroker pb, CollectionDescriptor cod, Object left,
Object right)
  +    public MtoNImplementor(PersistenceBroker pb, CollectionDescriptor codLeft, Object left,
Object right)
       {
  -        init(left, right);
  +        init(codLeft, left, right);
       }
   
       /**
  -     * @deprecated
  +     * Constructor for m:n entry.
  +     *
  +     * @param pb Current used {@link PersistenceBroker} instance.
  +     * @param leftFieldName Field name of the left m:n reference.
  +     * @param left The left side object.
  +     * @param right The right side object.
        */
  -    public MtoNImplementor(PersistenceBroker pb, String collectionName, Object left, Object
right)
  +    public MtoNImplementor(PersistenceBroker pb, String leftFieldName, Object left, Object
right)
       {
  -        //CollectionDescriptor cod = pb.getClassDescriptor(ProxyHelper.getRealClass(leftObject)).getCollectionDescriptorByName(collectionName);
  -        init(left, right);
  +        if(left == null || right == null)
  +        {
  +            throw new IllegalArgumentException("both objects must exist");
  +        }
  +        CollectionDescriptor cod = pb.getClassDescriptor(ProxyHelper.getRealClass(left)).getCollectionDescriptorByName(leftFieldName);
  +        init(cod, left, right);
       }
   
  -    public MtoNImplementor(Object leftObject, Object rightObject)
  +    public MtoNImplementor(CollectionDescriptor leftDescriptor, Object leftObject, Object
rightObject)
       {
  -        init(leftObject, rightObject);
  +        init(leftDescriptor, leftObject, rightObject);
       }
   
  -    private void init(Object left, Object right)
  +    private void init(CollectionDescriptor leftDescriptor, Object left, Object right)
       {
           if(left == null || right == null)
           {
               throw new IllegalArgumentException("both objects must exist");
           }
  -
  -        //broker = pb;
  -        //collectionDesc = cod;
  +        this.leftDescriptor = leftDescriptor;
           leftObject = left;
           rightObject = right;
           leftClass = ProxyHelper.getRealClass(leftObject);
           rightClass = ProxyHelper.getRealClass(rightObject);
       }
   
  +    public CollectionDescriptor getLeftDescriptor()
  +    {
  +        return leftDescriptor;
  +    }
  +
       public Class getLeftClass()
       {
           return leftClass;
  @@ -90,106 +103,5 @@
       {
           return rightObject;
       }
  -
  -//    public CollectionDescriptor getCollectionDescriptor()
  -//    {
  -//        return collectionDesc;
  -//    }
  -
  -//	/**
  -//	 *
  -//	 * @param mnKeys
  -//	 * @return
  -//	 */
  -//    public String getInsertStmt(Collection mnKeys)
  -//    {
  -//		String[] leftPkColumns = collectionDesc.getFksToThisClass();
  -//		String[] rightPkColumns = collectionDesc.getFksToItemClass();
  -//		String table = collectionDesc.getIndirectionTable();
  -//		Key key = new Key(getRightPkValues());
  -//		if (mnKeys.contains(key))
  -//		{
  -//			return null;
  -//		}
  -//		return broker.serviceSqlGenerator().getInsertMNStatement(table, leftPkColumns, rightPkColumns);
  -//    }
  -
  -
  -//	/**
  -//	 *
  -//	 * @return an Object Array with the primary key values of the left object
  -//	 */
  -//	public Object[] getLeftPkValues()
  -//	{
  -//		ClassDescriptor leftCld = broker.getClassDescriptor(leftClass);
  -//        BrokerHelper bh = broker.serviceBrokerHelper();
  -//		return bh.extractValueArray(bh.getKeyValues(leftCld, leftObject));
  -//	}
  -//
  -//	/**
  -//	 *
  -//	 * @return an Object Array with the primary key values of the right object
  -//	 */
  -//	public Object[] getRightPkValues()
  -//	{
  -//		ClassDescriptor rightCld = broker.getClassDescriptor(rightClass);
  -//        BrokerHelper bh = broker.serviceBrokerHelper();
  -//		return bh.extractValueArray(bh.getKeyValues(rightCld, rightObject));
  -//	}
  -
  -//    /**
  -//     * Inner class to model the key
  -//     */
  -//    private static final class Key
  -//    {
  -//
  -//        final Object[] m_key;
  -//
  -//        Key(final Object[] aKey)
  -//        {
  -//            m_key = new Object[aKey.length];
  -//
  -//            for (int i = 0; i < aKey.length; i++)
  -//            {
  -//                // BRJ:
  -//                // convert all Numbers to Long to simplify equals
  -//                // could lead to problems when Floats are used as key
  -//                if (aKey[i] instanceof Number)
  -//                {
  -//                    m_key[i] = new Long(((Number)aKey[i]).longValue());
  -//                }
  -//                else
  -//                {
  -//                    m_key[i] = aKey[i];
  -//                }
  -//            }
  -//        }
  -//
  -//        public boolean equals(Object other)
  -//        {
  -//            if (other == this)
  -//            {
  -//                return true;
  -//            }
  -//            if (!(other instanceof Key))
  -//            {
  -//                return false;
  -//            }
  -//
  -//            Key otherKey = (Key) other;
  -//            EqualsBuilder eb = new EqualsBuilder();
  -//
  -//            eb.append(m_key, otherKey.m_key);
  -//            return eb.isEquals();
  -//        }
  -//
  -//        public int hashCode()
  -//        {
  -//            HashCodeBuilder hb = new HashCodeBuilder();
  -//            hb.append(m_key);
  -//
  -//            return hb.toHashCode();
  -//        }
  -//    }
   }
   
  
  
  
  No                   revision
  No                   revision
  1.15.2.4  +2 -2      db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java
  
  Index: MtoNTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/MtoNTest.java,v
  retrieving revision 1.15.2.3
  retrieving revision 1.15.2.4
  diff -u -r1.15.2.3 -r1.15.2.4
  --- MtoNTest.java	18 Mar 2005 19:22:30 -0000	1.15.2.3
  +++ MtoNTest.java	9 May 2005 14:26:55 -0000	1.15.2.4
  @@ -366,7 +366,7 @@
   
           Category categoryId1 = new Category(1);
   
  -        MtoNImplementor m2n = new MtoNImplementor(newsId2,categoryId1);
  +        MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers", newsId2, categoryId1);
           broker.deleteMtoNImplementor(m2n);
   
   		broker.clearCache();
  @@ -385,7 +385,7 @@
   		News newNews = (News) broker.getObjectByIdentity(id);
   		int size = newNews.getQualifiers().size();
   
  -		MtoNImplementor m2n = new MtoNImplementor(newsId2,categoryId2);
  +		MtoNImplementor m2n = new MtoNImplementor(broker, "qualifiers", newsId2,categoryId2);
   		broker.addMtoNImplementor(m2n);
   
   		broker.clearCache();
  
  
  
  No                   revision
  No                   revision
  1.3.2.5   +104 -14   db-ojb/src/test/org/apache/ojb/odmg/M2NTest.java
  
  Index: M2NTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/M2NTest.java,v
  retrieving revision 1.3.2.4
  retrieving revision 1.3.2.5
  diff -u -r1.3.2.4 -r1.3.2.5
  --- M2NTest.java	7 May 2005 20:04:47 -0000	1.3.2.4
  +++ M2NTest.java	9 May 2005 14:26:55 -0000	1.3.2.5
  @@ -12,9 +12,7 @@
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.M2NTest.Actor;
  -import org.apache.ojb.broker.M2NTest.Movie;
  -import org.apache.ojb.broker.M2NTest.MovieImpl;
  +import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  @@ -167,6 +165,75 @@
           tx.commit();
       }
   
  +    public void testTwoMtoNRelationsInOneClass() throws Exception
  +    {
  +        String postfixId = "testTwoMtoNRelationsInOneClass_" + System.currentTimeMillis();
  +        Movie m = new MovieImpl(postfixId, "Movie_" + postfixId, "none");
  +
  +        Actor a1 = new Actor("Actor_1_" + postfixId);
  +        m.addActors(a1);
  +
  +        Actor a2a = new Actor("Actor_2a_" + postfixId);
  +        m.addActors2(a2a);
  +        Actor a2b = new Actor("Actor_2b_" + postfixId);
  +        m.addActors2(a2b);
  +
  +        TransactionExt tx = (TransactionExt) odmg.newTransaction();
  +        tx.begin();
  +        database.makePersistent(m);
  +        tx.commit();
  +
  +        tx.begin();
  +        tx.getBroker().clearCache();
  +
  +        OQLQuery queryMovie = movieQuery(postfixId);
  +        Collection resultMovie = (Collection) queryMovie.execute();
  +        assertEquals(1, resultMovie.size());
  +        Movie newMovie = (Movie) resultMovie.iterator().next();
  +        assertNotNull(newMovie.getActors());
  +        assertEquals(1, newMovie.getActors().size());
  +        assertEquals(2, newMovie.getActors2().size());
  +
  +        OQLQuery queryActor = actorQuery(postfixId);
  +        Collection resultActor = (Collection) queryActor.execute();
  +        assertEquals(1 + 2, resultActor.size());
  +
  +        OQLQuery queryRole = roleQuery(null, m);
  +        Collection resultRole = (Collection) queryRole.execute();
  +        assertEquals(1, resultRole.size());
  +
  +        // remove both Actors from relation and delete one Actor
  +        // instance completely, the other Actor should still in DB
  +        Object removed1 = newMovie.getActors2().remove(0);
  +        Actor removed2 = (Actor) newMovie.getActors2().remove(0);
  +        database.deletePersistent(removed1);
  +        // update the Actor unlinked from relation
  +        tx.lock(removed2, Transaction.WRITE);
  +        String newName = removed2.getName() + "_updated";
  +        removed2.setName(newName);
  +        tx.commit();
  +
  +        queryMovie = movieQuery(postfixId);
  +        resultMovie = (Collection) queryMovie.execute();
  +        assertEquals(1, resultMovie.size());
  +        newMovie = (Movie) resultMovie.iterator().next();
  +        assertNotNull(newMovie.getActors());
  +        assertEquals(1, newMovie.getActors().size());
  +        assertEquals(0, newMovie.getActors2().size());
  +
  +        queryActor = actorQuery(postfixId);
  +        resultActor = (Collection) queryActor.execute();
  +        assertEquals(1 + 1, resultActor.size());
  +
  +        queryActor = actorQuery(newName);
  +        resultActor = (Collection) queryActor.execute();
  +        assertEquals(1, resultActor.size());
  +
  +        queryRole = roleQuery(null, m);
  +        resultRole = (Collection) queryRole.execute();
  +        assertEquals(1, resultRole.size());
  +   }
  +
       public void testStore() throws Exception
       {
           // arminw: fixed
  @@ -254,7 +321,7 @@
   
           OQLQuery queryActor = actorQuery(postfix);
           Collection resultActor = (Collection) queryActor.execute();
  -        assertEquals(3, resultActor.size());
  +        assertEquals(5, resultActor.size());
   
           OQLQuery queryRole = roleQuery(null, movie);
           Collection resultRole = (Collection) queryRole.execute();
  @@ -443,7 +510,7 @@
   
           OQLQuery queryActor = actorQuery(postfix);
           Collection resultActor = (Collection) queryActor.execute();
  -        assertEquals(3, resultActor.size());
  +        assertEquals(5, resultActor.size());
   
           OQLQuery queryRole = roleQuery(null, movie);
           Collection resultRole = (Collection) queryRole.execute();
  @@ -480,7 +547,7 @@
   
           queryActor = actorQuery(postfix);
           resultActor = (Collection) queryActor.execute();
  -        assertEquals(2, resultActor.size());
  +        assertEquals(4, resultActor.size());
   
           queryRole = roleQuery(null, movie);
           resultRole = (Collection) queryRole.execute();
  @@ -527,7 +594,7 @@
   
           OQLQuery queryActor = actorQuery(postfix);
           Collection resultActor = (Collection) queryActor.execute();
  -        assertEquals(3, resultActor.size());
  +        assertEquals(5, resultActor.size());
   
           OQLQuery queryRole = roleQuery(null, movie);
           Collection resultRole = (Collection) queryRole.execute();
  @@ -575,7 +642,7 @@
           // we only remove the association
           queryActor = actorQuery(postfix);
           resultActor = (Collection) queryActor.execute();
  -        assertEquals(3, resultActor.size());
  +        assertEquals(5, resultActor.size());
   
           tx.commit();
       }
  @@ -876,11 +943,14 @@
   
       public static interface Movie
       {
  +        public void addActors(Actor actor);
  +        public void addActors2(Actor actor);
  +
           public Collection getActors();
           public void setActors(Collection actors);
   
  -        public Collection getActors2();
  -        public void setActors2(Collection actors);
  +        public List getActors2();
  +        public void setActors2(List actors);
   
           public Integer getIdInt2();
           public Integer getIdInt();
  @@ -906,7 +976,7 @@
           private String title;
           private String description;
           private Collection actors;
  -        private Collection actors2;
  +        private List actors2;
   
           public MovieImpl()
           {
  @@ -919,6 +989,26 @@
               this.description = description;
           }
   
  +        public void addActors(Actor actor)
  +        {
  +            if(actors == null)
  +            {
  +                actors = new ArrayList();
  +            }
  +            if(!actors.contains(actor)) actors.add(actor);
  +            else throw new OJBRuntimeException("Can't add same object twice");
  +        }
  +
  +        public void addActors2(Actor actor)
  +        {
  +            if(actors2 == null)
  +            {
  +                actors2 = new ArrayList();
  +            }
  +            if(!actors2.contains(actor)) actors2.add(actor);
  +            else throw new OJBRuntimeException("Can't add same object twice");
  +        }
  +
           public Collection getActors()
           {
               return actors;
  @@ -929,12 +1019,12 @@
               this.actors = actors;
           }
   
  -       public Collection getActors2()
  +       public List getActors2()
           {
               return actors2;
           }
   
  -        public void setActors2(Collection actors)
  +        public void setActors2(List actors)
           {
               this.actors2 = actors;
           }
  
  
  
  No                   revision
  No                   revision
  1.13.2.12 +3 -2      db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml
  
  Index: repository_junit_odmg.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_odmg.xml,v
  retrieving revision 1.13.2.11
  retrieving revision 1.13.2.12
  diff -u -r1.13.2.11 -r1.13.2.12
  --- repository_junit_odmg.xml	7 May 2005 20:04:47 -0000	1.13.2.11
  +++ repository_junit_odmg.xml	9 May 2005 14:26:55 -0000	1.13.2.12
  @@ -1205,7 +1205,8 @@
       
       <collection-descriptor
   	name="actors2"
  -	element-class-ref="org.apache.ojb.broker.M2NTest$Actor"
  +    collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
  +	element-class-ref="org.apache.ojb.odmg.M2NTest$Actor"
   	indirection-table="M2N_TEST_ROLE_2"
   	proxy="false"
   	auto-retrieve="true"
  
  
  

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