db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1296798 - in /db/torque/torque4/trunk: torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/ torque-test/src/main/schema/ torque-test/src/test/java/org/apache/torque/generated/dataobject/
Date Sun, 04 Mar 2012 13:08:13 GMT
Author: tfischer
Date: Sun Mar  4 13:08:13 2012
New Revision: 1296798

URL: http://svn.apache.org/viewvc?rev=1296798&view=rev
Log:
Fixed a possible NullPointerException if a primitive foreign key column references an object
column

Added:
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyAdderTest.java
Modified:
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/setterReferencedObject.vm
    db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/setterReferencedObject.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/setterReferencedObject.vm?rev=1296798&r1=1296797&r2=1296798&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/setterReferencedObject.vm
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/general/setterReferencedObject.vm
Sun Mar  4 13:08:13 2012
@@ -49,7 +49,13 @@
   #set ( $localColumnDefaultValue = $localColumnElement.getAttribute("defaultValue") )
   #set ( $foreignColumnElement = $referenceElement.getChild("foreign-column").getChild("column")
)
   #set ( $foreignColumnGetter = $foreignColumnElement.getAttribute("getter") )
+  #set ( $localColumnPrimitive = $localColumnElement.getAttribute("primitive") )
+  #set ( $foreignColumnPrimitive = $foreignColumnElement.getAttribute("primitive") )
+  #if ($localColumnPrimitive == "true" && $foreignColumnPrimitive != "true")
+        if (v == null || v.${foreignColumnGetter}() == null)
+  #else
         if (v == null)
+  #end
         {
             ${localColumnSetter}(${localColumnDefaultValue});
         }
@@ -57,6 +63,6 @@
         {
             ${localColumnSetter}(v.${foreignColumnGetter}());
         }
-    #end
+#end
         $field = v;
     }

Modified: db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml?rev=1296798&r1=1296797&r2=1296798&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml (original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/foreign-key-schema.xml Sun Mar  4
13:08:13 2012
@@ -81,6 +81,17 @@
     </foreign-key>
   </table>
 
+  <table name="O_INTEGER_FK_TO_P_PK"
+      description="table with a nullable integer object foreign key
+          which references a primitive primary key column in the foreign table">
+    <column name="ID" primaryKey="true" type="INTEGER" javaType="object"/>
+    <column name="FK" type="INTEGER" javaType="object"/>
+    <column name="NAME" type="VARCHAR" size="100"/>
+    <foreign-key foreignTable="P_INTEGER_PK">
+      <reference local="FK" foreign="ID"/>
+    </foreign-key>
+  </table>
+
   <table name="O_INTEGER_PK"
       description="table with a object integer pk">
     <column name="ID" primaryKey="true" type="INTEGER" javaType="object"/>
@@ -133,6 +144,17 @@
     </foreign-key>
   </table>
   
+  <table name="P_INTEGER_FK_TO_O_PK"
+      description="table with a nullable primitive int foreign key
+          which references a primitive primary key column in the foreign table">
+    <column name="ID" primaryKey="true" type="INTEGER" javaType="primitive"/>
+    <column name="FK" type="INTEGER" javaType="primitive"/>
+    <column name="NAME" type="VARCHAR" size="100"/>
+    <foreign-key foreignTable="O_INTEGER_PK">
+      <reference local="FK" foreign="ID"/>
+    </foreign-key>
+  </table>
+
   <table name="COMP_INTEGER_VARCHAR_PK" idMethod="none"
       description="table with a composite primary key consisting of an integer
           and a varchar column">

Added: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyAdderTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyAdderTest.java?rev=1296798&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyAdderTest.java
(added)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyAdderTest.java
Sun Mar  4 13:08:13 2012
@@ -0,0 +1,312 @@
+package org.apache.torque.generated.dataobject;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 junit.framework.TestCase;
+
+import org.apache.torque.test.NullableOIntegerFk;
+import org.apache.torque.test.NullablePIntegerFk;
+import org.apache.torque.test.OIntegerFkToPPk;
+import org.apache.torque.test.OIntegerPk;
+import org.apache.torque.test.PIntegerFkToOPk;
+import org.apache.torque.test.PIntegerPk;
+
+/**
+ * Tests the foreign key adder methods in the generated data object classes.
+ *
+ * @version $Id: DataTest.java 1103512 2011-05-15 19:37:41Z tfischer $
+ */
+public class ForeignKeyAdderTest extends TestCase
+{
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by primitive ints and the referenced object has an id of zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddPrimitiveIntFkZero() throws Exception
+    {
+        // prepare
+        PIntegerPk pIntegerPk = new PIntegerPk();
+        NullablePIntegerFk nullablePIntegerFk = new NullablePIntegerFk();
+
+        // execute
+        pIntegerPk.addNullablePIntegerFk(nullablePIntegerFk);
+
+        // verify
+        assertEquals(1, pIntegerPk.getNullablePIntegerFks().size());
+        assertSame(
+                nullablePIntegerFk,
+                pIntegerPk.getNullablePIntegerFks().get(0));
+
+        assertEquals(0, nullablePIntegerFk.getFk());
+        assertSame(pIntegerPk, nullablePIntegerFk.getPIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by primitive ints and the referenced object has an id
+     * which is not equal to zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddPrimitiveIntFkNonZero() throws Exception
+    {
+        // prepare
+        PIntegerPk pIntegerPk = new PIntegerPk();
+        pIntegerPk.setId(23);
+        NullablePIntegerFk nullablePIntegerFk = new NullablePIntegerFk();
+
+        // execute
+        pIntegerPk.addNullablePIntegerFk(nullablePIntegerFk);
+
+        // verify
+        assertEquals(1, pIntegerPk.getNullablePIntegerFks().size());
+        assertSame(
+                nullablePIntegerFk,
+                pIntegerPk.getNullablePIntegerFks().get(0));
+
+        assertEquals(23, nullablePIntegerFk.getFk());
+        assertSame(pIntegerPk, nullablePIntegerFk.getPIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if the referenced object has an
+     * primitive PK but the referencing object has an object FK,
+     * and the referenced object has an id of null.
+     *
+     * This case is questionable, because 0 has a different meaning in both
+     * cases.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddObjectToPrimitiveFkNull() throws Exception
+    {
+        // prepare
+        PIntegerPk pIntegerPk = new PIntegerPk();
+        OIntegerFkToPPk oIntegerFkToPPk = new OIntegerFkToPPk();
+
+        // execute
+        pIntegerPk.addOIntegerFkToPPk(oIntegerFkToPPk);
+
+        // verify
+        assertEquals(1, pIntegerPk.getOIntegerFkToPPks().size());
+        assertSame(
+                oIntegerFkToPPk,
+                pIntegerPk.getOIntegerFkToPPks().get(0));
+
+        assertEquals(new Integer(0), oIntegerFkToPPk.getFk());
+        assertSame(pIntegerPk, oIntegerFkToPPk.getPIntegerPk());
+    }
+
+
+    /**
+     * Tests that the add method works if the referenced object has an
+     * primitive PK but the referencing object has an object FK,
+     * which is not equal to zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddObjectToPrimitiveFkNonZero() throws Exception
+    {
+        // prepare
+        PIntegerPk pIntegerPk = new PIntegerPk();
+        pIntegerPk.setId(4);
+        OIntegerFkToPPk oIntegerFkToPPk = new OIntegerFkToPPk();
+
+        // execute
+        pIntegerPk.addOIntegerFkToPPk(oIntegerFkToPPk);
+
+        // verify
+        assertEquals(1, pIntegerPk.getOIntegerFkToPPks().size());
+        assertSame(
+                oIntegerFkToPPk,
+                pIntegerPk.getOIntegerFkToPPks().get(0));
+
+        assertEquals(new Integer(4), oIntegerFkToPPk.getFk());
+        assertSame(pIntegerPk, oIntegerFkToPPk.getPIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by object integers and the referenced object has an id of null.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddObjectIntegerFkNull() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+
+        // execute
+        oIntegerPk.addNullableOIntegerFk(nullableOIntegerFk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getNullableOIntegerFks().size());
+        assertSame(
+                nullableOIntegerFk,
+                oIntegerPk.getNullableOIntegerFks().get(0));
+
+        assertEquals(null, nullableOIntegerFk.getFk());
+        assertSame(oIntegerPk, nullableOIntegerFk.getOIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by object integers and the referenced object has an id of zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddObjectIntegerFkZero() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        oIntegerPk.setId(0);
+        NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+
+        // execute
+        oIntegerPk.addNullableOIntegerFk(nullableOIntegerFk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getNullableOIntegerFks().size());
+        assertSame(
+                nullableOIntegerFk,
+                oIntegerPk.getNullableOIntegerFks().get(0));
+
+        assertEquals(new Integer(0), nullableOIntegerFk.getFk());
+        assertSame(oIntegerPk, nullableOIntegerFk.getOIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by object integers and the referenced object has an id
+     * which is not equal to zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddObjectIntegerFkNonZero() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        oIntegerPk.setId(13);
+        NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+
+        // execute
+        oIntegerPk.addNullableOIntegerFk(nullableOIntegerFk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getNullableOIntegerFks().size());
+        assertSame(
+                nullableOIntegerFk,
+                oIntegerPk.getNullableOIntegerFks().get(0));
+
+        assertEquals(new Integer(13), nullableOIntegerFk.getFk());
+        assertSame(oIntegerPk, nullableOIntegerFk.getOIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if the referenced object has an
+     * Object PK but the referencing object has a primitive FK,
+     * and the referenced object has an id of null.
+     *
+     * This case is questionable, because the related fields in referencing
+     * and referenced object are different.
+     * However, both values mean that the final id does not yet exist.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddPrimitiveToObjectFkNull() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        PIntegerFkToOPk pIntegerFkToOPk = new PIntegerFkToOPk();
+
+        // execute
+        oIntegerPk.addPIntegerFkToOPk(pIntegerFkToOPk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getPIntegerFkToOPks().size());
+        assertSame(
+                pIntegerFkToOPk,
+                oIntegerPk.getPIntegerFkToOPks().get(0));
+
+        assertEquals(0, pIntegerFkToOPk.getFk());
+        assertSame(oIntegerPk, pIntegerFkToOPk.getOIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if the referenced object has an
+     * Object PK but the referencing object has a primitive FK,
+     * and the referenced object has an id of zero.
+     *
+     * This case is questionable, because 0 means that the id is not yet
+     * determined for a primitive key. But in practice ids will start at 1
+     * so this should not happen.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddPrimitiveToObjectFkZero() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        oIntegerPk.setId(0);
+        PIntegerFkToOPk pIntegerFkToOPk = new PIntegerFkToOPk();
+
+        // execute
+        oIntegerPk.addPIntegerFkToOPk(pIntegerFkToOPk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getPIntegerFkToOPks().size());
+        assertSame(
+                pIntegerFkToOPk,
+                oIntegerPk.getPIntegerFkToOPks().get(0));
+
+        assertEquals(0, pIntegerFkToOPk.getFk());
+        assertSame(oIntegerPk, pIntegerFkToOPk.getOIntegerPk());
+    }
+
+    /**
+     * Tests that the add method works if referenced and referencing are
+     * related by primitive ints and the referenced object has an id
+     * which is not equal to zero.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testAddPrimitiveToObjectFkNonZero() throws Exception
+    {
+        // prepare
+        OIntegerPk oIntegerPk = new OIntegerPk();
+        oIntegerPk.setId(4);
+        PIntegerFkToOPk pIntegerFkToOPk = new PIntegerFkToOPk();
+
+        // execute
+        oIntegerPk.addPIntegerFkToOPk(pIntegerFkToOPk);
+
+        // verify
+        assertEquals(1, oIntegerPk.getPIntegerFkToOPks().size());
+        assertSame(
+                pIntegerFkToOPk,
+                oIntegerPk.getPIntegerFkToOPks().get(0));
+
+        assertEquals(4, pIntegerFkToOPk.getFk());
+        assertSame(oIntegerPk, pIntegerFkToOPk.getOIntegerPk());
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message