Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 62196 invoked from network); 26 Sep 2006 11:24:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Sep 2006 11:24:33 -0000 Received: (qmail 35501 invoked by uid 500); 26 Sep 2006 11:24:32 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 35467 invoked by uid 500); 26 Sep 2006 11:24:32 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 35456 invoked by uid 500); 26 Sep 2006 11:24:32 -0000 Received: (qmail 35453 invoked by uid 99); 26 Sep 2006 11:24:32 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Sep 2006 04:24:32 -0700 Authentication-Results: idunn.apache.osuosl.org smtp.mail=arminw@apache.org; spf=permerror X-ASF-Spam-Status: No, hits=-9.4 required=5.0 tests=ALL_TRUSTED,NO_REAL_NAME Received-SPF: error (idunn.apache.osuosl.org: domain apache.org from 140.211.166.113 cause and error) Received: from [140.211.166.113] ([140.211.166.113:63887] helo=eris.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id 6F/80-08646-7ED09154 for ; Tue, 26 Sep 2006 04:24:25 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id ABEC31A981A; Tue, 26 Sep 2006 04:24:19 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r449989 - in /db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb: broker/M2NTest.java repository_junit_reference.xml Date: Tue, 26 Sep 2006 11:24:19 -0000 To: ojb-commits@db.apache.org From: arminw@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060926112419.ABEC31A981A@eris.apache.org> X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: arminw Date: Tue Sep 26 04:24:18 2006 New Revision: 449989 URL: http://svn.apache.org/viewvc?view=rev&rev=449989 Log: add new test using arrays as object collection for m:n reference Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/M2NTest.java db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/repository_junit_reference.xml Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/M2NTest.java URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/M2NTest.java?view=diff&rev=449989&r1=449988&r2=449989 ============================================================================== --- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/M2NTest.java (original) +++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/M2NTest.java Tue Sep 26 04:24:18 2006 @@ -6,6 +6,7 @@ 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; @@ -15,7 +16,9 @@ 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.broker.util.ObjectModification; +import org.apache.ojb.broker.core.proxy.ProxyHelper; import org.apache.ojb.junit.PBTestCase; /** @@ -25,7 +28,6 @@ * are NOT recommended in multithreaded environments, because they are global * and each thread will be affected. * - * @author Armin Waibel * @version $Id$ */ public class M2NTest extends PBTestCase @@ -177,6 +179,71 @@ assertEquals(true, ord.getCascadeDelete()); } + public void testRetrieveProxiedCollection() + { + String postfix = "testRetrieveProxiedCollection_" + System.currentTimeMillis(); + ojbChangeReferenceSetting(MovieImpl.class, "actors", true, OBJECT, OBJECT, true); + ojbChangeReferenceSetting(MovieImpl.class, "actors2", true, OBJECT, OBJECT, true); + + Movie m1 = buildMovieWithActors(postfix + "_1", 10); + Movie m2 = buildMovieWithActors(postfix + "_2", 20); + + broker.beginTransaction(); + broker.store(m1); + broker.store(m2); + broker.commitTransaction(); + broker.clearCache(); + + QueryByCriteria q = queryMovie(postfix); + q.addOrderByAscending("idStr"); + + Collection movies = broker.getCollectionByQuery(q); + assertEquals(2, movies.size()); + + Movie[] moviesArray = new Movie[2]; + movies.toArray(moviesArray); + + for (int i = 0;i < moviesArray.length; i++) + { + Object result = moviesArray[i].getActors(); + assertTrue(ProxyHelper.isCollectionProxy(result)); + // when prefetching for proxy is used the second object has an already materialized proxy + if(i==0) assertFalse(ProxyHelper.getCollectionProxy(result).isLoaded()); + moviesArray[i].getActors().get(0); + assertTrue(ProxyHelper.getCollectionProxy(result).isLoaded()); + } + + assertEquals(10, moviesArray[0].getActors().size()); + assertEquals(20, moviesArray[1].getActors().size()); + } + + public void testRetrieveArray() + { + String postfix = "testRetrieveArray_" + System.currentTimeMillis(); + ojbChangeReferenceSetting(MovieWithArrayImpl.class, "actors", true, OBJECT, OBJECT, false); + + MovieWithArray m1 = buildMovieWithActorsArray(postfix + "_1", 10); + MovieWithArray m2 = buildMovieWithActorsArray(postfix + "_2", 20); + + broker.beginTransaction(); + broker.store(m1); + broker.store(m2); + broker.commitTransaction(); + broker.clearCache(); + + QueryByCriteria q = movieWithArrayQuery(postfix); + q.addOrderByAscending("idStr"); + + Collection movies = broker.getCollectionByQuery(q); + assertEquals(2, movies.size()); + + MovieWithArray[] moviesArray = new MovieWithArray[2]; + movies.toArray(moviesArray); + + assertEquals(10, moviesArray[0].getActors().length); + assertEquals(20, moviesArray[1].getActors().length); + } + public void testMassStoreUpdateAutomatic() { @@ -650,6 +717,7 @@ broker.serviceBrokerHelper().link(movie, true); */ broker.commitTransaction(); + broker.clearCache(); /* now we expect all stored objects @@ -1113,7 +1181,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); @@ -1142,16 +1209,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(); @@ -1167,13 +1232,145 @@ 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 + actor which have no references to any movie object + */ + 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 @@ -1181,6 +1378,8 @@ */ broker.beginTransaction(); broker.delete(movie); + // the previous unlinked Actor instance + broker.delete(deletedActor); broker.commitTransaction(); broker.clearCache(); @@ -1197,13 +1396,20 @@ //======================================================================= // helper methods //======================================================================= - Query queryMovie(String postfix) + QueryByCriteria queryMovie(String postfix) { Criteria c = new Criteria(); c.addLike("idStr", "%" + postfix + "%"); 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(); @@ -1211,6 +1417,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(); @@ -1230,6 +1443,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(); @@ -1251,6 +1483,13 @@ return QueryFactory.newQuery(Role.class, c_2); } + QueryByCriteria movieWithArrayQuery(String postfix) + { + Criteria c = new Criteria(); + c.addLike("idStr", "%" + postfix + "%"); + return QueryFactory.newQuery(MovieWithArray.class, c); + } + /** * Returns 1 movie object with 3 actor objects in actors-collection * and 2 actor objects in actors2-collection @@ -1301,6 +1540,22 @@ /** * Returns 1 movie object with 3 actor objects */ + MovieWithArray buildMovieWithActorsArray(String postfixId, int actorCount) + { + MovieWithArray m = new MovieWithArrayImpl(postfixId, "Movie with "+ actorCount+" actors_" + postfixId, "none"); + + ActorWithArray[] actors = new ActorWithArray[actorCount]; + for(int i = 0; i < actorCount; i++) + { + actors[i] = new ActorWithArray("A array actor_" + postfixId); + } + m.setActors(actors); + return m; + } + + /** + * Returns 1 movie object with 3 actor objects + */ Actor buildActorWithMovies(String postfixId, int movieCount) { Actor a = new Actor(postfixId+"_Actor play in "+ movieCount+" movies"); @@ -1513,14 +1768,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); @@ -1545,6 +1874,34 @@ //=================================================================== // inner class //=================================================================== + public static interface MovieWithArray + { + public ActorWithArray[] getActors(); + + public void setActors(ActorWithArray[] actors); + + public Integer getIdInt2(); + public Integer getIdInt(); + + public void setIdInt2(Integer id2Int); + public void setIdInt(Integer idInt); + + public String getIdStr(); + + public void setIdStr(String idStr); + + public String getTitle(); + + public void setTitle(String title); + + public String getDescription(); + + public void setDescription(String description); + } + + //=================================================================== + // inner class + //=================================================================== public static class MovieImpl implements Movie { private Integer idInt; @@ -1553,8 +1910,8 @@ private String title; private String description; - private Collection actors; - private Collection actors2; + private List actors; + private List actors2; private List producers; public MovieImpl() @@ -1591,12 +1948,12 @@ } } - public Collection getActors() + public List getActors() { return actors; } - public void setActors(Collection actors) + public void setActors(List actors) { this.actors = actors; } @@ -1610,14 +1967,116 @@ actors.add(a); } - public Collection getActors2() + public List getActors2() { return actors2; } - public void setActors2(Collection actors) + public void setActors2(List actors) { this.actors2 = actors; + } + + public Integer getIdInt() + { + return idInt; + } + + public void setIdInt(Integer idInt) + { + this.idInt = idInt; + } + + public Integer getIdInt2() + { + return idInt2; + } + + public void setIdInt2(Integer idInt2) + { + this.idInt2 = idInt2; + } + + public String getIdStr() + { + return idStr; + } + + public void setIdStr(String idStr) + { + this.idStr = idStr; + } + + public String getTitle() + { + return title; + } + + public void setTitle(String title) + { + this.title = title; + } + + public String getDescription() + { + return description; + } + + public void setDescription(String description) + { + this.description = description; + } + + public int hashCode() + { + return new HashCodeBuilder().append(idInt).append(idInt2).append(idStr).hashCode(); + } + + public boolean equals(Object obj) + { + boolean result = false; + if(obj instanceof MovieImpl) + { + MovieImpl other = (MovieImpl) obj; + result = new EqualsBuilder().append(idInt, other.idInt).append(idInt2, other.idInt2).append(idStr, other.idStr).isEquals(); + } + return result; + } + + public String toString() + { + return ToStringBuilder.reflectionToString(this).toString(); + } + } + + public static class MovieWithArrayImpl implements MovieWithArray + { + private Integer idInt; + private Integer idInt2; + private String idStr; + private String title; + private String description; + private ActorWithArray[] actors; + + public MovieWithArrayImpl() + { + } + + public MovieWithArrayImpl(String idStr, String title, String description) + { + this.idStr = idStr; + this.title = title; + this.description = description; + } + + public ActorWithArray[] getActors() + { + return actors; + } + + public void setActors(ActorWithArray[] actors) + { + this.actors = actors; } public Integer getIdInt() Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/repository_junit_reference.xml URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/repository_junit_reference.xml?view=diff&rev=449989&r1=449988&r2=449989 ============================================================================== --- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/repository_junit_reference.xml (original) +++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/repository_junit_reference.xml Tue Sep 26 04:24:18 2006 @@ -1507,6 +1507,10 @@ + + + + + + + + + + + + + + + + + + + + + + + + @@ -1632,6 +1692,48 @@ name="movies" collection-class="org.apache.ojb.broker.M2NTest$MovieManageableCollection" element-class-ref="org.apache.ojb.broker.M2NTest$MovieImpl" + auto-retrieve="false" + auto-update="false" + auto-delete="false" + indirection-table="M2N_ROLE" + > + + + + + + + + + + + + + + + +