db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r492886 - /db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java
Date Fri, 05 Jan 2007 03:12:42 GMT
Author: arminw
Date: Thu Jan  4 19:12:42 2007
New Revision: 492886

URL: http://svn.apache.org/viewvc?view=rev&rev=492886
Log:
add new test

Added:
    db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java

Added: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java?view=auto&rev=492886
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java
(added)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/OneToOneWithoutFKTest.java
Thu Jan  4 19:12:42 2007
@@ -0,0 +1,354 @@
+package org.apache.ojb.broker;
+
+/* Copyright 2002-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.Serializable;
+
+import org.apache.ojb.junit.PBTestCase;
+import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
+import org.apache.ojb.broker.util.ObjectModification;
+
+/**
+ * This class show handling of bidirectional 1:1 reference without
+ * using a separate FK column (the PK of the main object is the FK too and the both objects
use
+ * the same PK value).
+ * NOTE: It's not recommended to use 1:1 references without FK column.
+ *
+ * @version $Id: $
+ */
+public class OneToOneWithoutFKTest extends PBTestCase
+{
+    public static void main(String[] args)
+    {
+        String[] arr = {OneToOneWithoutFKTest.class.getName()};
+        junit.textui.TestRunner.main(arr);
+    }
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+    }
+
+    public void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    /**
+     * Handle Person object without using a reference to Passport class.
+     */
+    public void testInsertUpdateDeletePerson()
+    {
+        ojbChangeReferenceSetting(Passport.class, "person",
+                true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE,
true);
+        ojbChangeReferenceSetting(Person.class, "passport",
+                true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT,
true);
+        String t = "_" + System.currentTimeMillis();
+        String name = "Person_testInsertUpdateDeletePerson_" + t;
+        assertTrue(broker.getClassDescriptor(Person.class).getObjectReferenceDescriptorByName("passport").isLazy());
+        assertTrue(broker.getClassDescriptor(Passport.class).getObjectReferenceDescriptorByName("person").isLazy());
+
+
+        Person pers = new Person(name);
+
+        broker.beginTransaction();
+        // store plain Person
+        broker.store(pers, ObjectModification.INSERT);
+        broker.commitTransaction();
+        // clear cache to check DB entry
+        broker.clearCache();
+        // lookup
+        Identity oid = broker.serviceIdentity().buildIdentity(Person.class, pers.getId());
+        Person pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+
+        broker.beginTransaction();
+        pers_new.setName(name + "_updated");
+        // store plain Person
+        broker.store(pers_new, ObjectModification.UPDATE);
+        broker.commitTransaction();
+        // clear cache to check DB entry
+        broker.clearCache();
+        // lookup
+        oid = broker.serviceIdentity().buildIdentity(Person.class, pers_new.getId());
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        assertEquals(name+"_updated", pers_new.getName());
+        // NOTE: You try to access the none existing Passport object
+        // always null will be returned
+        assertNull(pers_new.getPassport().getId());
+        assertNull(pers_new.getPassport().getNumber());
+        assertNull(pers_new.getPassport().getPerson());
+
+
+        broker.beginTransaction();
+        // delete plain Person
+        broker.delete(pers);
+        broker.commitTransaction();
+        // lookup
+        oid = broker.serviceIdentity().buildIdentity(Person.class, pers_new.getId());
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNull(pers_new);
+    }
+
+    /**
+     * Handle Person <---> Passport objects with enabled proxy
+     */
+    public void testInsertUpdateDelete()
+    {
+        // metadata settings
+        // the main object Person use cascade update/delete, the dependend object not.
+        // For both 1:1 references proxy object are enabled.
+        ojbChangeReferenceSetting(Passport.class, "person",
+                true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE,
true);
+        ojbChangeReferenceSetting(Person.class, "passport",
+                true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT,
true);
+        String t = "_" + System.currentTimeMillis();
+        String name = "Person_testInsertUpdateDelete_" + t;
+        String number = "Passport_testInsertUpdateDelete_" + t;
+        assertTrue(broker.getClassDescriptor(Person.class).getObjectReferenceDescriptorByName("passport").isLazy());
+        assertTrue(broker.getClassDescriptor(Passport.class).getObjectReferenceDescriptorByName("person").isLazy());
+
+        // insert
+        Person pers = new Person(name);
+        Passport pass = new Passport(number);
+        broker.beginTransaction();
+        // store plain Person to assign PK
+        broker.store(pers);
+        // set PK/FK of Passport
+        pass.setId(pers.getId());
+        // set references
+        pers.setPassport(pass);
+        pass.setPerson(pers);
+        broker.store(pers);
+        broker.commitTransaction();
+
+        // lookup
+        Identity oid = broker.serviceIdentity().buildIdentity(pers);
+        Person pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        assertNotNull(pers_new.getPassport());
+
+        // update
+        broker.beginTransaction();
+        pers_new.setName(name + "_updated");
+        pers_new.getPassport().setNumber(number + "_updated");
+        broker.store(pers_new);
+        broker.commitTransaction();
+
+        // lookup
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        assertNotNull(pers_new.getPassport());
+        assertEquals(name + "_updated", pers_new.getName());
+        assertEquals(number + "_updated", pers_new.getPassport().getNumber());
+
+        // update nullify reference, with proxy references enabled, this result
+        // in unexpected proxy placeholder intead of null reference
+        broker.beginTransaction();
+        broker.delete(pers_new.getPassport());
+        pers_new.setPassport(null);
+        broker.store(pers_new);
+        broker.commitTransaction();
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        // we alway get a proxy placeholder object
+        // assertNull(pers_new.getPassport());
+        assertNull(pers_new.getPassport().getId());
+        assertEquals(name + "_updated", pers_new.getName());
+
+        // delete
+        broker.beginTransaction();
+        // first delete the object with constraint
+        broker.delete(pers_new.getPassport());
+        // not needed, but this reduce the number of generated queries
+        pers_new.setPassport(null);
+        broker.delete(pers_new);
+        broker.commitTransaction();
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNull(pers_new);
+    }
+
+    /**
+     * Handle Person <---> Passport objects withot proxy
+     */
+    public void testInsertUpdateDelete_2()
+    {
+        // metadata settings
+        // the main object Person use cascade update/delete, the dependend object not.
+        // For both 1:1 references proxy references are disabled.
+        ojbChangeReferenceSetting(Passport.class, "person",
+                true, ObjectReferenceDescriptor.CASCADE_NONE, ObjectReferenceDescriptor.CASCADE_NONE,
false);
+        ojbChangeReferenceSetting(Person.class, "passport",
+                true, ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT,
false);
+        String t = "_" + System.currentTimeMillis();
+        String name = "Person_testInsertUpdateDelete_2_" + t;
+        String number = "Passport_testInsertUpdateDelete_2_" + t;
+        assertFalse(broker.getClassDescriptor(Person.class).getObjectReferenceDescriptorByName("passport").isLazy());
+        assertFalse(broker.getClassDescriptor(Passport.class).getObjectReferenceDescriptorByName("person").isLazy());
+
+        // insert
+        Person pers = new Person(name);
+        Passport pass = new Passport(number);
+        broker.beginTransaction();
+        // store plain Person to assign PK
+        broker.store(pers);
+        // set PK/FK of Passport
+        pass.setId(pers.getId());
+        // set references
+        pers.setPassport(pass);
+        pass.setPerson(pers);
+        broker.store(pers);
+        broker.commitTransaction();
+
+        // lookup
+        Identity oid = broker.serviceIdentity().buildIdentity(pers);
+        Person pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        assertNotNull(pers_new.getPassport());
+        assertNotNull(pers_new.getPassport().getPerson());
+
+        // update
+        broker.beginTransaction();
+        pers_new.setName(name + "_updated");
+        pers_new.getPassport().setNumber(number + "_updated");
+        broker.store(pers_new);
+        broker.commitTransaction();
+
+        // lookup
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNotNull(pers_new);
+        assertNotNull(pers_new.getPassport());
+        assertEquals(name + "_updated", pers_new.getName());
+        assertEquals(number + "_updated", pers_new.getPassport().getNumber());
+
+        // update nullify reference, with proxy references enabled, this result
+        // in unexpected proxy placeholder intead of null reference
+        broker.beginTransaction();
+        broker.delete(pers_new.getPassport());
+        pers_new.setPassport(null);
+        broker.store(pers_new);
+        broker.commitTransaction();
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNull(pers_new.getPassport());
+        assertEquals(name + "_updated", pers_new.getName());
+
+        // delete
+        broker.beginTransaction();
+        // first delete the object with constraint
+        broker.delete(pers_new.getPassport());
+        // not needed, but this reduce the number of generated queries
+        pers_new.setPassport(null);
+        broker.delete(pers_new);
+        broker.commitTransaction();
+        pers_new = (Person) broker.getObjectByIdentity(oid);
+        assertNull(pers_new);
+    }
+
+
+
+    public static class Person implements Serializable
+    {
+        private Integer id;
+        private String name;
+        private Passport passport;
+
+        public Person()
+        {
+        }
+
+        public Person(String name)
+        {
+            this.name = name;
+        }
+
+        public Integer getId()
+        {
+            return id;
+        }
+
+        public void setId(Integer id)
+        {
+            this.id = id;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(String name)
+        {
+            this.name = name;
+        }
+
+        public Passport getPassport()
+        {
+            return passport;
+        }
+
+        public void setPassport(Passport passport)
+        {
+            this.passport = passport;
+        }
+    }
+
+    public static class Passport implements Serializable
+    {
+        private Integer id;
+        private String number;
+        private Person person;
+
+        public Passport()
+        {
+        }
+
+        public Passport(String number)
+        {
+            this.number = number;
+        }
+
+        public Integer getId()
+        {
+            return id;
+        }
+
+        public void setId(Integer id)
+        {
+            this.id = id;
+        }
+
+        public String getNumber()
+        {
+            return number;
+        }
+
+        public void setNumber(String number)
+        {
+            this.number = number;
+        }
+
+        public Person getPerson()
+        {
+            return person;
+        }
+
+        public void setPerson(Person person)
+        {
+            this.person = 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