db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r472101 - in /db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker: BidirectionalAssociationTest.java CollectionTest2.java MtoNMapping.java
Date Tue, 07 Nov 2006 13:30:26 GMT
Author: arminw
Date: Tue Nov  7 05:30:26 2006
New Revision: 472101

URL: http://svn.apache.org/viewvc?view=rev&rev=472101
Log:
improve tests

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/BidirectionalAssociationTest.java
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/CollectionTest2.java
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/MtoNMapping.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/BidirectionalAssociationTest.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/BidirectionalAssociationTest.java?view=diff&rev=472101&r1=472100&r2=472101
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/BidirectionalAssociationTest.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/BidirectionalAssociationTest.java
Tue Nov  7 05:30:26 2006
@@ -275,6 +275,9 @@
 
         broker.beginTransaction();
         broker.delete(b);
+        // unlink deleted object B from object A 
+        broker.serviceBrokerHelper().unlink(a, "relatedB");
+        broker.store(a, ObjectModification.UPDATE);
         broker.commitTransaction();
 
         crit = new Criteria();

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/CollectionTest2.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/CollectionTest2.java?view=diff&rev=472101&r1=472100&r2=472101
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/CollectionTest2.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/CollectionTest2.java Tue
Nov  7 05:30:26 2006
@@ -15,6 +15,7 @@
 import org.apache.ojb.broker.query.Query;
 import org.apache.ojb.broker.query.QueryByCriteria;
 import org.apache.ojb.broker.query.QueryFactory;
+import org.apache.ojb.broker.core.proxy.ProxyFactory;
 import org.apache.ojb.junit.PBTestCase;
 
 /**
@@ -24,7 +25,6 @@
  * are NOT recommended in multithreaded environments, because they are global
  * and each thread will be affected.
  *
- * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  * @version $Id$
  */
 public class CollectionTest2 extends PBTestCase
@@ -185,7 +185,7 @@
         result = broker.getCollectionByQuery(queryDeveloper);
         assertEquals(2, result.size());
 
-        Identity proOid = new Identity(pro, broker);
+        Identity proOid = broker.serviceIdentity().buildIdentity(pro);
         Project newPro = (Project) broker.getObjectByIdentity(proOid);
         assertNull(newPro.getDevelopers());
         assertNull(newPro.getSubProjects());
@@ -266,7 +266,7 @@
         changeAutoSetting(Project.class, "subProjects", true, true, true, false);
         changeAutoSetting(Project.class, "developers", true, true, true, false);
         changeAutoSetting(SubProject.class, "project", true, true, true, false);
-        doTestStoreUpdateDelete_2();
+        doTestStoreUpdateDelete_2(false);
     }
 
     public void testStoreUpdateDelete_2b()
@@ -274,10 +274,10 @@
         changeAutoSetting(Project.class, "subProjects", true, true, true, true);
         changeAutoSetting(Project.class, "developers", true, true, true, true);
         changeAutoSetting(SubProject.class, "project", true, true, true, true);
-        doTestStoreUpdateDelete_2();
+        doTestStoreUpdateDelete_2(true);
     }
 
-    public void doTestStoreUpdateDelete_2()
+    public void doTestStoreUpdateDelete_2(boolean isLazy)
     {
         String name = "testStoreUpdateDelete_2" + System.currentTimeMillis();
 
@@ -322,7 +322,7 @@
         result = broker.getCollectionByQuery(queryDeveloper);
         assertEquals(2, result.size());
 
-        Identity proOid = new Identity(pro, broker);
+        Identity proOid = broker.serviceIdentity().buildIdentity(pro);
         Project newPro = (Project) broker.getObjectByIdentity(proOid);
         assertNotNull(newPro.getDevelopers());
         assertNotNull(newPro.getSubProjects());
@@ -338,6 +338,17 @@
         //*****************************************
         broker.clearCache();
         newPro = (Project) broker.getObjectByIdentity(proOid);
+
+        if (isLazy)
+        {
+            ClassDescriptor cld = broker.getClassDescriptor(newPro.getClass());
+            CollectionDescriptor cod = cld.getCollectionDescriptorByName("subProjects");
+            Object col = cod.getPersistentField().get(newPro);
+            ProxyFactory proxyFactory = ((PersistenceBrokerInternal) broker).getProxyFactory();
+            assertTrue(proxyFactory.isCollectionProxy(col));
+            assertFalse(proxyFactory.getCollectionProxy(col).isLoaded());
+        }
+
         newPro.setName("updated_" + name);
         broker.beginTransaction();
         Iterator it = newPro.getSubProjects().iterator();
@@ -440,7 +451,7 @@
         result = broker.getCollectionByQuery(queryDeveloper);
         assertEquals(2, result.size());
 
-        Identity proOid = new Identity(pro, broker);
+        Identity proOid = broker.serviceIdentity().buildIdentity(pro);
         Project newPro = (Project) broker.getObjectByIdentity(proOid);
         assertNotNull(newPro.getDevelopers());
         assertNotNull(newPro.getSubProjects());
@@ -492,7 +503,7 @@
         //*****************************************
         // de-/serialize object
         //*****************************************
-        newPro = (Project) SerializationUtils.deserialize(SerializationUtils.serialize(newPro));
+        newPro = (Project) SerializationUtils.clone(newPro);
 
         //*****************************************
         // delete
@@ -726,29 +737,29 @@
         changeAutoSetting(SubProjectSelfRef.class, "developers", true,
                 CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_NONE, false);
 
-        String name = "testSelfReference" + System.currentTimeMillis();
+        String name = "testSelfReference_" + System.currentTimeMillis();
 
-        DeveloperSelf dev1 = new DeveloperSelf(name + "A");
-        DeveloperSelf dev2 = new DeveloperSelf(name + "B");
-        DeveloperSelf dev3 = new DeveloperSelf(name + "C");
+        DeveloperSelf dev1 = new DeveloperSelf(name + "_A");
+        DeveloperSelf dev2 = new DeveloperSelf(name + "_B");
+        DeveloperSelf dev3 = new DeveloperSelf(name + "_C");
         ArrayList devList = new ArrayList();
         devList.add(dev1);
         devList.add(dev2);
         ArrayList sub1devList = new ArrayList();
         sub1devList.add(dev3);
 
-        SubProjectSelfRef sub1 = new SubProjectSelfRef(name + "A");
-        SubProjectSelfRef sub2 = new SubProjectSelfRef(name + "B");
+        SubProjectSelfRef sub1 = new SubProjectSelfRef(name + "_A");
+        SubProjectSelfRef sub2 = new SubProjectSelfRef(name + "_B");
         ArrayList subList = new ArrayList();
         subList.add(sub1);
         subList.add(sub2);
 
-        SubProjectSelfRef pro = new SubProjectSelfRef(name + "MAIN");
+        SubProjectSelfRef pro = new SubProjectSelfRef(name + "_MAIN");
         pro.setSubProjects(subList);
         pro.setDevelopers(devList);
         sub1.setDevelopers(sub1devList);
 
-        Query queryProject = createQueryFor(SubProjectSelfRef.class, "name", name + "MAIN");
+        Query queryProject = createQueryFor(SubProjectSelfRef.class, "name", name + "_MAIN");
         Query querySubProject = createQueryFor(SubProjectSelfRef.class, "name", name);
         Query queryDeveloper = createQueryFor(DeveloperSelf.class, "name", name);
 
@@ -784,6 +795,7 @@
         result = broker.getCollectionByQuery(queryBySubDeveloper);
         assertEquals(1, result.size());
     }
+
 
     //============================================================================
     // helper methods

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/MtoNMapping.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/MtoNMapping.java?view=diff&rev=472101&r1=472100&r2=472101
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/MtoNMapping.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/MtoNMapping.java Tue Nov
 7 05:30:26 2006
@@ -2,6 +2,7 @@
 
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 import org.apache.ojb.broker.metadata.CollectionDescriptor;
+import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
 import org.apache.ojb.broker.query.Criteria;
 import org.apache.ojb.broker.query.Query;
 import org.apache.ojb.broker.query.QueryByCriteria;
@@ -36,9 +37,7 @@
     public void testPolymorphMToN()
     {
         Gourmet james = new Gourmet("james");
-        Identity jamesId = new Identity(james, broker);
         Gourmet doris = new Gourmet("doris");
-        Identity dorisId = new Identity(doris, broker);
 
         Fish tuna = new Fish("tuna", 242, "salt");
         Fish trout = new Fish("trout", 52, "fresh water");
@@ -58,6 +57,9 @@
         broker.store(doris);
         broker.commitTransaction();
 
+        Identity jamesId = broker.serviceIdentity().buildIdentity(james);
+        Identity dorisId = broker.serviceIdentity().buildIdentity(doris);
+
         broker.clearCache();
 
         Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId);
@@ -73,9 +75,7 @@
     {
         long timestamp = System.currentTimeMillis();
         Gourmet james = new Gourmet("james");
-        Identity jamesId = new Identity(james, broker);
         Gourmet doris = new Gourmet("doris");
-        Identity dorisId = new Identity(doris, broker);
 
         Fish tuna = new Fish("tuna", 242, "salt");
         Fish trout = new Fish("trout", 52, "fresh water");
@@ -96,6 +96,9 @@
         broker.store(doris);
         broker.commitTransaction();
 
+        Identity jamesId = broker.serviceIdentity().buildIdentity(james);
+        Identity dorisId = broker.serviceIdentity().buildIdentity(doris);
+
         broker.clearCache();
 
         Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId);
@@ -125,9 +128,7 @@
     {
         long timestamp = System.currentTimeMillis();
         Gourmet james = new Gourmet("james_" + timestamp);
-        Identity jamesId = new Identity(james, broker);
         Gourmet doris = new Gourmet("doris_" + timestamp);
-        Identity dorisId = new Identity(doris, broker);
 
         Fish tuna = new Fish("tuna_" + timestamp, 242, "salt");
         Fish trout = new Fish("trout_" + timestamp, 52, "fresh water");
@@ -147,9 +148,11 @@
         broker.beginTransaction();
         broker.store(james);
         broker.store(doris);
-
         broker.commitTransaction();
 
+        Identity jamesId = broker.serviceIdentity().buildIdentity(james);
+        Identity dorisId = broker.serviceIdentity().buildIdentity(doris);
+
         broker.clearCache();
 
         Gourmet loadedJames = (Gourmet) broker.getObjectByIdentity(jamesId);
@@ -244,7 +247,7 @@
 
             assertEquals("Same Title", p1.getTitle(), p2.getTitle());
             assertEquals("Same Number of Persons", p1.getPersons().size(), p2.getPersons().size());
-            assertEquals("Same toString", p1.toString(), p2.toString());
+            assertEquals("Same id", p1.getId(), p2.getId());
         }
     }
 
@@ -272,7 +275,7 @@
         Query q = QueryFactory.newQuery(p);
         p = (Person) broker.getObjectByQuery(q);
 
-        Vector roles = (Vector) p.getRoles();
+        Collection roles = p.getRoles();
         assertNotNull(roles);
         //System.out.println(roles);
 
@@ -325,37 +328,67 @@
     /** Add a new Project, delete an existing Project */
     public void testInsertAndDelete() throws Exception
     {
+        int projectsCount = 2;
+        String name = "testInsertAndDelete_" + System.currentTimeMillis();
         Person pers = new Person();
-        pers.setId(7);
-        Query query = QueryFactory.newQuery(pers);
-        pers = (Person) broker.getObjectByQuery(query);
-        Collection projects = pers.getProjects();
-        Project[] projectArray = (Project[]) projects.toArray(new Project[0]);
-        Project oldProj, newProj;
-        int count = projects.size();
+        pers.setFirstname(name);
+
+        List projects = new ArrayList();
+        for(int i = 0;i<projectsCount;i++)
+        {
+            Project p = new Project();
+            p.setTitle(name);
+            projects.add(p);
+        }
 
-        oldProj = projectArray[0];
-        projects.remove(oldProj);
+        pers.setProjects(projects);
 
-        newProj = new Project();
-        newProj.setTitle("Test Project1 for Person 7");
-        newProj.setDescription("This is a Test Project1 for Person 7");
+        broker.beginTransaction();
+        broker.store(pers);
+        broker.commitTransaction();
+
+        Identity oid = broker.serviceIdentity().buildIdentity(pers);
+        broker.clearCache();
+
+        pers = (Person) broker.getObjectByIdentity(oid);
+        projects = new ArrayList(pers.getProjects());
+        assertEquals(projectsCount, projects.size());
+        // remove one project
+        projects.remove(0);
+
+        // add two new projects
+        Project newProj = new Project();
+        newProj.setTitle(name);
+        newProj.setDescription("New test Project for Person_" + pers.getId());
         projects.add(newProj);
 
         newProj = new Project();
-        newProj.setTitle("Test Project2 for Person 7");
-        newProj.setDescription("This is a Test Project2 for Person 7");
+        newProj.setTitle(name);
+        newProj.setDescription("New test Project for Person_" + pers.getId());
         projects.add(newProj);
 
+        pers.setProjects(projects);
+
         broker.beginTransaction();
         broker.store(pers);
         broker.commitTransaction();
 
         broker.clearCache();
+        pers = (Person) broker.getObjectByIdentity(oid);
+        assertEquals(projectsCount + 1 , pers.getProjects().size());
 
-        pers = (Person) broker.getObjectByQuery(query);
-        assertEquals(count + 1 , pers.getProjects().size());
+        projects = new ArrayList(pers.getProjects());
 
+        int match  = 0;
+        for(int i = 0; i < projects.size(); i++)
+        {
+            Project project = (Project) projects.get(i);
+            if(project.getDescription() != null && project.getDescription().indexOf("_"+pers.getId())
> -1)
+            {
+                ++match;
+            }
+        }
+        assertEquals("Expect two matches", 2, match);
     }
 
     /**
@@ -416,15 +449,15 @@
     {
         ClassDescriptor cldProject = broker.getClassDescriptor(Project.class);
         CollectionDescriptor codPersons =cldProject.getCollectionDescriptorByName("persons");
-        boolean cascadeStorePersons = codPersons.getCascadeStore();
+        int cascadeStorePersons = codPersons.getCascadingStore();
 
         ClassDescriptor cldPerson = broker.getClassDescriptor(Person.class);
         CollectionDescriptor codProjects =cldPerson.getCollectionDescriptorByName("projects");
-        boolean cascadeStoreProjects = codProjects.getCascadeStore();
+        int cascadeStoreProjects = codProjects.getCascadingStore();
 
         // temporarily set auto-update = true
-        codPersons.setCascadeStore(true);
-        codProjects.setCascadeStore(true);
+        codPersons.setCascadingStore(ObjectReferenceDescriptor.CASCADE_OBJECT);
+        codProjects.setCascadingStore(ObjectReferenceDescriptor.CASCADE_OBJECT);
 
         // create new project
         Project project = new Project();
@@ -456,8 +489,8 @@
         broker.commitTransaction();
 
         // reset original value
-        codPersons.setCascadeStore(cascadeStorePersons);
-        codProjects.setCascadeStore(cascadeStoreProjects);
+        codPersons.setCascadingStore(cascadeStorePersons);
+        codProjects.setCascadingStore(cascadeStoreProjects);
 
         return project;
     }
@@ -688,11 +721,7 @@
         assertEquals(count + 1, p.getProjects().size());
 
         broker.beginTransaction();
-
-        projects = p.getProjects();
-        projects.clear();
-        p.setProjects(projects);
-
+        p.getProjects().clear();
         broker.store(p);
         broker.commitTransaction();
 



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