db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r452834 - /db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java
Date Wed, 04 Oct 2006 11:13:41 GMT
Author: arminw
Date: Wed Oct  4 04:13:41 2006
New Revision: 452834

URL: http://svn.apache.org/viewvc?view=rev&rev=452834
Log:
add test, use IdentityFactory

Modified:
    db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java

Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java?view=diff&rev=452834&r1=452833&r2=452834
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java (original)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/M2NTest.java Wed Oct  4 04:13:41 2006
@@ -6,11 +6,13 @@
 import java.util.List;
 
 import org.apache.commons.lang.ClassUtils;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.time.StopWatch;
 import org.apache.ojb.broker.core.proxy.CollectionProxy;
+import org.apache.ojb.broker.core.proxy.ProxyFactory;
 import org.apache.ojb.broker.metadata.CollectionDescriptor;
 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
 import org.apache.ojb.broker.query.Criteria;
@@ -27,7 +29,6 @@
  * are NOT recommended in multithreaded environments, because they are global
  * and each thread will be affected.
  *
- * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
  * @version $Id$
  */
 public class M2NTest extends PBTestCase
@@ -205,9 +206,13 @@
        
         for (int i = 0;i < moviesArray.length; i++)
         {
-            CollectionProxy proxy = (CollectionProxy) moviesArray[i].getActors();
-            proxy.load();					// materialize the proxy
-            assertTrue(proxy.isLoaded());
+            ProxyFactory helper = broker.getProxyFactory();
+            List result = moviesArray[i].getActors();
+            assertTrue(helper.isCollectionProxy(result));
+            // when prefetching for proxy is used the second object has an already materialized
proxy
+            if(i==0) assertFalse(helper.getCollectionProxy(result).isLoaded());
+            result.get(0);
+            assertTrue(helper.getCollectionProxy(result).isLoaded());
         }
         
         assertEquals(10, moviesArray[0].getActors().size());
@@ -1131,7 +1136,7 @@
         Collection resultActor = broker.getCollectionByQuery(queryActor);
         assertEquals(1, resultActor.size());
 
-        Query queryRole = queryRole(actor, null);
+        Query queryRole = queryRole(actor, (Movie) null);
         Collection resultRole = broker.getCollectionByQuery(queryRole);
         assertEquals(3, resultRole.size());
 
@@ -1178,7 +1183,6 @@
         all in all we expect 3 movie, 6 actor, 3 role entries after first
         store.
         */
-
         broker.beginTransaction();
         broker.store(movie);
         broker.store(a_1);
@@ -1207,16 +1211,14 @@
         assertEquals(3, readMovie.getActors().size());
         assertEquals(0, readMovie.getActors2().size());
 
-        /*
-        we add 2 existing actor an movie object, thus we expect
-        3 movie, 6 actor, 5 role entries after store.
-        And next lookup of movie we expect 5 dependend actor objects
-        */
+        broker.beginTransaction();
+        // delete Actor
+        Object removeActor = movie.getActors().remove(0);
+        //we add 2 existing actor to the movie object
         movie.getActors().add(a_1);
         movie.getActors().add(a_2);
         // add new actor object
         movie.getActors().add(a_4);
-        broker.beginTransaction();
         broker.store(movie);
         broker.commitTransaction();
 
@@ -1232,13 +1234,13 @@
 
         queryRole = queryRole(null, movie);
         resultRole = broker.getCollectionByQuery(queryRole);
-        assertEquals(6, resultRole.size());
+        assertEquals(5, resultRole.size());
 
         broker.clearCache();
         oid = broker.serviceIdentity().buildIdentity(movie);
         readMovie = (Movie) broker.getObjectByIdentity(oid);
         assertNotNull(readMovie);
-        assertEquals(6, readMovie.getActors().size());
+        assertEquals(5, readMovie.getActors().size());
 
         /*
         on delete we expect that all entries are deleted except the single
@@ -1246,6 +1248,140 @@
         */
         broker.beginTransaction();
         broker.delete(movie);
+        // the previous unlinked Actor instance
+        broker.delete(removeActor);
+        broker.commitTransaction();
+
+        broker.clearCache();
+        resultMovie = broker.getCollectionByQuery(queryMovie);
+        assertEquals(0, resultMovie.size());
+
+        resultActor = broker.getCollectionByQuery(queryActor);
+        assertEquals(1, resultActor.size());
+
+        resultRole = broker.getCollectionByQuery(queryRole);
+        assertEquals(0, resultRole.size());
+    }
+
+    public void testAddNewEntriesArray()
+    {
+        ojbChangeReferenceSetting(MovieWithArrayImpl.class, "actors", true, OBJECT, OBJECT,
false);
+        ojbChangeReferenceSetting(ActorWithArray.class, "movies", true, OBJECT, OBJECT, false);
+        doTestAddNewEntriesArray();
+    }
+
+    public void testAddNewEntriesArrayProxyEnabled()
+    {
+        ojbChangeReferenceSetting(MovieWithArrayImpl.class, "actors", true, OBJECT, OBJECT,
true);
+        ojbChangeReferenceSetting(ActorWithArray.class, "movies", true, OBJECT, OBJECT, true);
+        doTestAddNewEntriesArray();
+    }
+
+    public void doTestAddNewEntriesArray()
+    {
+        ojbChangeReferenceSetting(MovieWithArrayImpl.class, "actors", true, OBJECT, OBJECT,
true);
+        ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, true);
+        // default proxy does not work for user defined collection
+        ojbChangeReferenceSetting(Actor.class, "movies", true, OBJECT, OBJECT, false);
+
+        String postfix = "doTestAddNewEntriesArray_" + System.currentTimeMillis();
+
+        /*
+        Returns 1 movie object with 3 actor objects and one actor
+        */
+        MovieWithArray movie = buildMovieWithActorsArray(postfix, 3);
+        ActorWithArray a_1 = new ActorWithArray("testAddNewEntries_"+postfix);
+        ActorWithArray a_2 = new ActorWithArray("testAddNewEntries_"+postfix);
+        ActorWithArray a_3 = new ActorWithArray("testAddNewEntries_"+postfix);
+        /*
+        all in all we expect 1 movie, 5 actor, 3 role entries after first
+        store.
+        */
+        broker.beginTransaction();
+        broker.store(movie);
+        broker.store(a_1);
+        broker.store(a_2);
+        broker.commitTransaction();
+
+        broker.clearCache();
+
+        Query queryMovie = queryMovieWithArray(postfix);
+        Collection resultMovie = broker.getCollectionByQuery(queryMovie);
+        assertEquals(1, resultMovie.size());
+
+        Query queryActor = queryActorWithArray(postfix);
+        Collection resultActor = broker.getCollectionByQuery(queryActor);
+        assertEquals(5, resultActor.size());
+
+        Query queryRole = queryRole(null, movie);
+        Collection resultRole = broker.getCollectionByQuery(queryRole);
+        assertEquals(3, resultRole.size());
+
+        broker.clearCache();
+        Identity oid = broker.serviceIdentity().buildIdentity(movie);
+        MovieWithArray readMovie = (MovieWithArray) broker.getObjectByIdentity(oid);
+        assertNotNull(readMovie);
+        assertEquals(3, readMovie.getActors().length);
+        ActorWithArray[] actors = readMovie.getActors();
+        for(int i = 0; i < actors.length; i++)
+        {
+            ActorWithArray actor = actors[i];
+            assertNotNull(actor.getMovies());
+            assertEquals(1, actor.getMovies().length);
+            MovieWithArray result = actor.getMovies()[0];
+            assertEquals(readMovie.getActors().length, result.getActors().length);
+        }
+
+
+        broker.beginTransaction();
+        actors = movie.getActors();
+        ActorWithArray deletedActor = actors[0];
+        // remove one actor
+        actors = (ActorWithArray[]) ArrayUtils.remove(actors, 0);
+        // add one new and one existing Actor
+        a_2.addMovie(movie);
+        a_3.addMovie(movie);
+        actors = (ActorWithArray[]) ArrayUtils.add(actors, a_2);
+        actors = (ActorWithArray[]) ArrayUtils.add(actors, a_3);
+        movie.setActors(actors);
+
+        broker.store(movie);
+        broker.commitTransaction();
+
+        broker.clearCache();
+
+        queryMovie = queryMovieWithArray(postfix);
+        resultMovie = broker.getCollectionByQuery(queryMovie);
+        assertEquals(1, resultMovie.size());
+
+        queryActor = queryActorWithArray(postfix);
+        resultActor = broker.getCollectionByQuery(queryActor);
+        assertEquals(6, resultActor.size());
+
+        queryRole = queryRole(null, movie);
+        resultRole = broker.getCollectionByQuery(queryRole);
+        assertEquals(4, resultRole.size());
+
+        //broker.clearCache();
+        oid = broker.serviceIdentity().buildIdentity(deletedActor);
+        ActorWithArray result = (ActorWithArray) broker.getObjectByIdentity(oid);
+        assertNotNull(result);
+        assertTrue(result.getMovies() == null || result.getMovies().length == 0);
+
+        //broker.clearCache();
+        oid = broker.serviceIdentity().buildIdentity(movie);
+        readMovie = (MovieWithArray) broker.getObjectByIdentity(oid);
+        assertNotNull(readMovie);
+        assertEquals(4, readMovie.getActors().length);
+
+        /*
+        on delete we expect that all entries are deleted except the single
+        actor which have no references to any movie object
+        */
+        broker.beginTransaction();
+        broker.delete(movie);
+        // the previous unlinked Actor instance
+        broker.delete(deletedActor);
         broker.commitTransaction();
 
         broker.clearCache();
@@ -1269,6 +1405,13 @@
         return QueryFactory.newQuery(Movie.class, c);
     }
 
+    QueryByCriteria queryMovieWithArray(String postfix)
+    {
+        Criteria c = new Criteria();
+        c.addLike("idStr", "%" + postfix + "%");
+        return QueryFactory.newQuery(MovieWithArray.class, c);
+    }
+
     Query queryActor(String postfix)
     {
         Criteria c = new Criteria();
@@ -1276,6 +1419,13 @@
         return QueryFactory.newQuery(Actor.class, c);
     }
 
+    Query queryActorWithArray(String postfix)
+    {
+        Criteria c = new Criteria();
+        c.addLike("name", "%" + postfix + "%");
+        return QueryFactory.newQuery(ActorWithArray.class, c);
+    }
+
     Query queryRole(Actor actor, Movie movie)
     {
         Criteria c = new Criteria();
@@ -1295,6 +1445,25 @@
         return QueryFactory.newQuery(Role.class, c);
     }
 
+    Query queryRole(ActorWithArray actor, MovieWithArray movie)
+    {
+        Criteria c = new Criteria();
+        if(actor != null) c.addEqualTo("actorId", actor.getId());
+        if(movie != null && actor != null)
+        {
+            Criteria c2 = new Criteria();
+            c2.addEqualTo("movieIntId", movie.getIdInt());
+            c2.addEqualTo("movieStrId", movie.getIdStr());
+            c.addOrCriteria(c2);
+        }
+        else if(movie != null)
+        {
+            c.addEqualTo("movieIntId", movie.getIdInt());
+            c.addEqualTo("movieStrId", movie.getIdStr());
+        }
+        return QueryFactory.newQuery(Role.class, c);
+    }
+
     Query roleQueryActorOrMovieMatch(Actor actor, Movie movie)
     {
         Criteria c_1 = new Criteria();
@@ -1377,11 +1546,10 @@
     {
         MovieWithArray m = new MovieWithArrayImpl(postfixId, "Movie with "+ actorCount+"
actors_" + postfixId, "none");
 
-        Actor[] actors = new Actor[actorCount];
+        ActorWithArray[] actors = new ActorWithArray[actorCount];
         for(int i = 0; i < actorCount; i++)
         {
-            Actor a = new Actor("A bad actor_" + postfixId);
-            actors[i] = a;
+            actors[i] = new ActorWithArray("A array actor_" + postfixId);
         }
         m.setActors(actors);
         return m;
@@ -1598,14 +1766,88 @@
     //===================================================================
     // inner class
     //===================================================================
+    public static class ActorWithArray
+    {
+        private Integer id;
+        private Integer id2;
+        private String name;
+        private MovieWithArray[] movies;
+
+        public ActorWithArray()
+        {
+        }
+
+        public ActorWithArray(String name)
+        {
+            this.name = name;
+        }
+
+        public MovieWithArray[] getMovies()
+        {
+            return movies;
+        }
+
+        public void setMovies(MovieWithArray[] movies)
+        {
+            this.movies = movies;
+        }
+
+        public void addMovie(MovieWithArray m)
+        {
+            if(movies == null)
+            {
+                movies = new MovieWithArray[]{};
+            }
+            movies = (MovieWithArray[]) ArrayUtils.add(movies, m);
+        }
+
+        public Integer getId()
+        {
+            return id;
+        }
+
+        public void setId(Integer id)
+        {
+            this.id = id;
+        }
+
+        public Integer getId2()
+        {
+            return id2;
+        }
+
+        public void setId2(Integer id2)
+        {
+            this.id2 = id2;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(String name)
+        {
+            this.name = name;
+        }
+
+        public String toString()
+        {
+            return ToStringBuilder.reflectionToString(this);
+        }
+    }
+
+    //===================================================================
+    // inner class
+    //===================================================================
     public static interface Movie
     {
-        public Collection getActors();
-        public void setActors(Collection actors);
+        public List getActors();
+        public void setActors(List actors);
         public void addActor(Actor a);
 
-        public Collection getActors2();
-        public void setActors2(Collection actors);
+        public List getActors2();
+        public void setActors2(List actors);
 
         public List getProducers();
         public void setProducers(List producers);
@@ -1632,9 +1874,9 @@
     //===================================================================
     public static interface MovieWithArray
     {
-        public Actor[] getActors();
+        public ActorWithArray[] getActors();
 
-        public void setActors(Actor[] actors);
+        public void setActors(ActorWithArray[] actors);
 
         public Integer getIdInt2();
         public Integer getIdInt();
@@ -1666,8 +1908,8 @@
 
         private String title;
         private String description;
-        private Collection actors;
-        private Collection actors2;
+        private List actors;
+        private List actors2;
         private List producers;
 
         public MovieImpl()
@@ -1704,12 +1946,12 @@
             }
         }
 
-        public Collection getActors()
+        public List getActors()
         {
             return actors;
         }
 
-        public void setActors(Collection actors)
+        public void setActors(List actors)
         {
             this.actors = actors;
         }
@@ -1723,12 +1965,12 @@
             actors.add(a);
         }
 
-        public Collection getActors2()
+        public List getActors2()
         {
             return actors2;
         }
 
-        public void setActors2(Collection actors)
+        public void setActors2(List actors)
         {
             this.actors2 = actors;
         }
@@ -1812,7 +2054,7 @@
         private String idStr;
         private String title;
         private String description;
-        private Actor[] actors;
+        private ActorWithArray[] actors;
 
         public MovieWithArrayImpl()
         {
@@ -1825,12 +2067,12 @@
             this.description = description;
         }
 
-        public Actor[] getActors()
+        public ActorWithArray[] getActors()
         {
             return actors;
         }
 
-        public void setActors(Actor[] actors)
+        public void setActors(ActorWithArray[] actors)
         {
             this.actors = actors;
         }



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