Author: tfischer
Date: Fri Jul 8 09:04:45 2011
New Revision: 1144201
URL: http://svn.apache.org/viewvc?rev=1144201&view=rev
Log:
fixed null key handling in foreign key getters and added test case
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyGetterTest.java
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/foreignKeyGetter.vm
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.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/dbObject/base/foreignKeyGetter.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/foreignKeyGetter.vm?rev=1144201&r1=1144200&r2=1144201&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/foreignKeyGetter.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/foreignKeyGetter.vm
Fri Jul 8 09:04:45 2011
@@ -29,20 +29,21 @@
/**
* Returns an id that differentiates the referenced
${foreignDbObjectClassName} object
* from all other ${foreignDbObjectClassName} objects.
+ *
+ * @return an ObjectKey.
*/
public ObjectKey ${foreignKeyGetter}()
{
#if ($referenceElements.size() == 1)
#set ( $columnElement =
$referenceElements.get(0).getChild("local-column").getChild("column") )
- #set ( $columnDefaultValue = $columnElement.getAttribute("defaultValue") )
#set ( $columnType = $columnElement.getAttribute("fieldType") )
#set ( $columnIsPrimitive = $columnElement.getAttribute("primitive") )
#set ( $getter = $columnElement.getAttribute("getter") )
- #if ($columnIsPrimitive)
+ #if ($columnIsPrimitive == "true")
${columnType} foreignKey = ${getter}();
- if (foreignKey == ${columnDefaultValue})
+ if (foreignKey == 0)
{
- return null;
+ return SimpleKey.keyFor((Number) null);
}
return SimpleKey.keyFor(foreignKey);
#else
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm?rev=1144201&r1=1144200&r2=1144201&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/fillReferencedObject.vm
Fri Jul 8 09:04:45 2011
@@ -189,7 +189,7 @@
for (${localDbObjectClassName} objectToFill : toFill)
{
ObjectKey foreignKey = objectToFill.${foreignKeyGetter}();
- if (foreignKey == null)
+ if (foreignKey == null || foreignKey.getValue() == null)
{
continue;
}
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=1144201&r1=1144200&r2=1144201&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
Fri Jul 8 09:04:45 2011
@@ -58,6 +58,18 @@
</foreign-key>
</table>
+ <table name="P_INTEGER_FK_WITH_DEFAULT"
+ description="table with a primitive integer foreign key
+ which references a primary key column in the foreign table
+ and has a default value">
+ <column name="ID" primaryKey="true" type="INTEGER" javaType="primitive"/>
+ <column name="FK" required="true" type="INTEGER" javaType="primitive"
default="2"/>
+ <column name="NAME" type="VARCHAR" size="100"/>
+ <foreign-key foreignTable="P_INTEGER_PK">
+ <reference local="FK" foreign="ID"/>
+ </foreign-key>
+ </table>
+
<table name="NON_PK_P_INTEGER_FK"
description="table with a non-nullable integer primitive foreign key
which references a non-primary-key column in the foreign table">
@@ -98,6 +110,18 @@
</foreign-key>
</table>
+ <table name="O_INTEGER_FK_WITH_DEFAULT"
+ description="table with a non-nullable integer object foreign key
+ which references a primary key column in the foreign table
+ and has a default value">
+ <column name="ID" primaryKey="true" type="INTEGER" javaType="object"/>
+ <column name="FK" required="true" type="INTEGER" javaType="object"
default="2"/>
+ <column name="NAME" type="VARCHAR" size="100"/>
+ <foreign-key foreignTable="O_INTEGER_PK">
+ <reference local="FK" foreign="ID"/>
+ </foreign-key>
+ </table>
+
<table name="NON_PK_O_INTEGER_FK"
description="table with a nullable integer object foreign key
which references a non-primary-key column in the foreign table">
Added:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyGetterTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyGetterTest.java?rev=1144201&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyGetterTest.java
(added)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/ForeignKeyGetterTest.java
Fri Jul 8 09:04:45 2011
@@ -0,0 +1,216 @@
+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.om.NumberKey;
+import org.apache.torque.om.ObjectKey;
+import org.apache.torque.test.NullableOIntegerFk;
+import org.apache.torque.test.NullablePIntegerFk;
+import org.apache.torque.test.OIntegerFkWithDefault;
+import org.apache.torque.test.PIntegerFkWithDefault;
+
+/**
+ * Tests the foreign key getter methods in the generated data object classes.
+ *
+ * @version $Id: DataTest.java 1103512 2011-05-15 19:37:41Z tfischer $
+ */
+public class ForeignKeyGetterTest extends TestCase
+{
+ /**
+ * Creates a new instance.
+ */
+ public ForeignKeyGetterTest(String name)
+ {
+ super(name);
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the primitive foreign
+ * key is 0.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForPrimitiveIntZero() throws Exception
+ {
+ NullablePIntegerFk nullablePIntegerFk = new NullablePIntegerFk();
+ nullablePIntegerFk.setFk(0);
+ ObjectKey foreignKey = nullablePIntegerFk.getForeignKeyForPIntegerPk();
+ assertNull(foreignKey.getValue());
+ }
+
+ /**
+ * Tests that the foreign key getter returns an Integer key
+ * if the primitive foreign key is not zero.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForPrimitiveIntNotZero() throws Exception
+ {
+ NullablePIntegerFk nullablePIntegerFk = new NullablePIntegerFk();
+ nullablePIntegerFk.setFk(3);
+ ObjectKey foreignKey = nullablePIntegerFk.getForeignKeyForPIntegerPk();
+ assertEquals(new NumberKey(3), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the primitive foreign
+ * key column has a default value and the column value is 0.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedPrimitiveIntZero() throws
Exception
+ {
+ PIntegerFkWithDefault pIntegerFk = new PIntegerFkWithDefault();
+ pIntegerFk.setFk(0);
+ ObjectKey foreignKey = pIntegerFk.getForeignKeyForPIntegerPk();
+ assertNull(foreignKey.getValue());
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the primitive foreign
+ * key column has a default value and the column value is the default
value.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedPrimitiveIntDefault()
+ throws Exception
+ {
+ PIntegerFkWithDefault pIntegerFk = new PIntegerFkWithDefault();
+ pIntegerFk.setFk(2);
+ ObjectKey foreignKey = pIntegerFk.getForeignKeyForPIntegerPk();
+ assertEquals(new NumberKey(2), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns an Integer key
+ * if the primitive foreign key column has a default value
+ * and the column value is not zero.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedPrimitiveIntNotZero() throws
Exception
+ {
+ PIntegerFkWithDefault pIntegerFk = new PIntegerFkWithDefault();
+ pIntegerFk.setFk(3);
+ ObjectKey foreignKey = pIntegerFk.getForeignKeyForPIntegerPk();
+ assertEquals(new NumberKey(3), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the object foreign
+ * key is null.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForObjectIntNull() throws Exception
+ {
+ NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+ nullableOIntegerFk.setFk(null);
+ ObjectKey foreignKey = nullableOIntegerFk.getForeignKeyForOIntegerPk();
+ assertNull(foreignKey.getValue());
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the object foreign
+ * key is 0.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForObjectIntZero() throws Exception
+ {
+ NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+ nullableOIntegerFk.setFk(0);
+ ObjectKey foreignKey = nullableOIntegerFk.getForeignKeyForOIntegerPk();
+ assertEquals(new NumberKey(0), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns an Integer key
+ * if the object foreign key is not zero.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForObjectIntNotZero() throws Exception
+ {
+ NullableOIntegerFk nullableOIntegerFk = new NullableOIntegerFk();
+ nullableOIntegerFk.setFk(3);
+ ObjectKey foreignKey = nullableOIntegerFk.getForeignKeyForOIntegerPk();
+ assertEquals(new NumberKey(3), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the object foreign
+ * key column has a default value and the column value is null.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedObjectIntNull() throws Exception
+ {
+ OIntegerFkWithDefault oIntegerFk = new OIntegerFkWithDefault();
+ oIntegerFk.setFk(null);
+ ObjectKey foreignKey = oIntegerFk.getForeignKeyForOIntegerPk();
+ assertNull(foreignKey.getValue());
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the object foreign
+ * key column has a default value and the column value is 0.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedObjectIntZero() throws Exception
+ {
+ OIntegerFkWithDefault oIntegerFk = new OIntegerFkWithDefault();
+ oIntegerFk.setFk(0);
+ ObjectKey foreignKey = oIntegerFk.getForeignKeyForOIntegerPk();
+ assertEquals(new NumberKey(0), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns null if the object foreign
+ * key column has a default value and the column value is the default
value.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedObjectIntDefault() throws
Exception
+ {
+ OIntegerFkWithDefault oIntegerFk = new OIntegerFkWithDefault();
+ oIntegerFk.setFk(2);
+ ObjectKey foreignKey = oIntegerFk.getForeignKeyForOIntegerPk();
+ assertEquals(new NumberKey(2), foreignKey);
+ }
+
+ /**
+ * Tests that the foreign key getter returns an Integer key
+ * if the object foreign key column has a default value
+ * and the column value is not zero.
+ *
+ * @throws Exception if a database error occurs.
+ */
+ public void testGetForeignKeyForDefaultedObjectIntNotZero() throws
Exception
+ {
+ OIntegerFkWithDefault oIntegerFk = new OIntegerFkWithDefault();
+ oIntegerFk.setFk(3);
+ ObjectKey foreignKey = oIntegerFk.getForeignKeyForOIntegerPk();
+ assertEquals(new NumberKey(3), foreignKey);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]