db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1518002 - in /db/torque/torque4/trunk: torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/ torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/ torque-test/src/test/java/org/ap...
Date Tue, 27 Aug 2013 21:31:37 GMT
Author: tfischer
Date: Tue Aug 27 21:31:36 2013
New Revision: 1518002

URL: http://svn.apache.org/r1518002
Log:
TORQUE-299 In the getRelatedObject... methods in the Data objects, fill the reference to the
current object
Also fix NPE when using getRelatedObject methods with initialized caches
add tests for both fixes

Added:
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/backreferenceToReferencingObject.vm
Modified:
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getterReferencingObject.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsJoinTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsTest.java

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml?rev=1518002&r1=1518001&r2=1518002&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml
Tue Aug 27 21:31:36 2013
@@ -366,6 +366,14 @@
   <outlet name="torque.om.dbObject.base.getterReferencingObject"
       xsi:type="velocityOutlet"
       path="dbObject/base/getterReferencingObject.vm">
+    <mergepoint name="backreference">
+      <action xsi:type="applyAction"
+          outlet="torque.om.dbObject.base.backreferenceToReferencingObject"/>
+    </mergepoint>
+  </outlet>
+  <outlet name="torque.om.dbObject.base.backreferenceToReferencingObject"
+      xsi:type="velocityOutlet"
+      path="dbObject/base/backreferenceToReferencingObject.vm">
   </outlet>
 
   <outlet name="torque.om.dbObject.base.adderReferencingObject"
@@ -386,6 +394,12 @@
   <outlet name="torque.om.dbObject.base.joinGettersReferencingObject"
       xsi:type="velocityOutlet"
       path="dbObject/base/joinGetterReferencingObject.vm">
+    <mergepoint name="backreference">
+      <!-- only one element expected for the following traverseAllAction -->
+      <action xsi:type="traverseAllAction" 
+          element ="local/foreign-key/foreign-field"
+          outlet="torque.om.dbObject.base.backreferenceToReferencingObject"/>
+    </mergepoint>
   </outlet>
   
   <outlet name="torque.om.dbObject.base.getByNameMethods"

Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/backreferenceToReferencingObject.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/backreferenceToReferencingObject.vm?rev=1518002&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/backreferenceToReferencingObject.vm
(added)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/backreferenceToReferencingObject.vm
Tue Aug 27 21:31:36 2013
@@ -0,0 +1,42 @@
+## 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.
+##
+######
+##
+## version $Id: getterReferencingObject.vm 1369255 2012-08-04 03:41:26Z tfischer $
+##
+## This template creates the backreference code in the getter method 
+## in the object for the "foreign" part of a foreign key. 
+## The objects in the field represent the "local" part of the foreign key.  
+## The template expects as input a "foreign-field" element inside a 
+## "referencing-foreign-keys/foreign-key" element
+## The variable indent can be set by the calling template
+##
+#set ( $foreignKeyElement = $torqueGen.getParent() )
+#set ( $localFieldElement = $foreignKeyElement.getChild("local-field") )
+#set ( $localFieldSetter = $localFieldElement.getAttribute("setter") )
+#set ( $thisObjectClassName = $foreignKeyElement.getChild("table").getAttribute("dbObjectClassName")
)
+#set ( $otherTable = $foreignKeyElement.getParent() )
+#set ( $otherTableField = $otherTable.getAttribute("field") )
+#set ( $otherTableClassName = $otherTable.getAttribute("dbObjectClassName") )
+#if ( ! ${indent})
+  #set ( $indent = "        " )
+#end
+${indent}for (${otherTableClassName} ${otherTableField} : ${field})
+${indent}{
+${indent}    ${otherTableField}.${localFieldSetter}(($thisObjectClassName) this);
+${indent}}

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getterReferencingObject.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getterReferencingObject.vm?rev=1518002&r1=1518001&r2=1518002&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getterReferencingObject.vm
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getterReferencingObject.vm
Tue Aug 27 21:31:36 2013
@@ -74,6 +74,8 @@
         if ($field == null)
         {
             $field = ${getter}(new Criteria());
+$torqueGen.setVariable("indent", "            ")##
+$torqueGen.mergepoint("backreference")##
         }
   #end
         return $field;
@@ -113,9 +115,11 @@
     #set ( $thisTableColumnElement = $referenceElement.getChild("foreign-column").getChild("column")
)
     #set ( $otherTablePeerColumnName = $otherTableColumnElement.getAttribute("peerColumnName")
)
     #set ( $thisTableColumnGetter = $thisTableColumnElement.getAttribute("getter") )
-                criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}()
);
+                criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
   #end
                 $field = ${otherTablePeerName}.doSelect(criteria);
+$torqueGen.setVariable("indent", "                ")##
+$torqueGen.mergepoint("backreference")##
             }
         }
         else
@@ -132,18 +136,19 @@
   #set ( $thisTableColumnElement = $referenceElement.getChild("foreign-column").getChild("column")
)
   #set ( $otherTablePeerColumnName = $otherTableColumnElement.getAttribute("peerColumnName")
)
   #set ( $thisTableColumnGetter = $thisTableColumnElement.getAttribute("getter") )
-                criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}()
);
+                criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
 #end
 #if ($objectIsCaching)
-                if (${criteriaCacheField} == null 
+                if (${criteriaCacheField} == null
                         || !${criteriaCacheField}.equals(criteria))
                 {
                     $field = ${otherTablePeerName}.doSelect(criteria);
+$torqueGen.setVariable("indent", "                    ")##
+$torqueGen.mergepoint("backreference")##
                 }
             }
         }
         $criteriaCacheField = criteria;
-
         return $field;
 #else
         return ${otherTablePeerName}.doSelect(criteria);
@@ -168,6 +173,8 @@
         if ($field == null)
         {
             $field = ${getter}(new Criteria(), con);
+$torqueGen.setVariable("indent", "            ")##
+$torqueGen.mergepoint("backreference")##
         }
         return $field;
 #else
@@ -183,7 +190,7 @@
 #else
      * If this $thisObjectClassName has previously
 #end
-     * been saved, it will retrieve the related $fieldContainedType Objects 
+     * been saved, it will retrieve the related $fieldContainedType Objects
      * from storage.
      * If this $thisObjectClassName is new, it will return
      * an empty collection or the current collection, the criteria
@@ -212,6 +219,8 @@
                 criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
   #end
                 $field = ${otherTablePeerName}.doSelect(criteria, con);
+$torqueGen.setVariable("indent", "                ")##
+$torqueGen.mergepoint("backreference")##
             }
         }
         else
@@ -231,9 +240,12 @@
                 criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
 #end
 #if ($objectIsCaching)
-                if (!${criteriaCacheField}.equals(criteria))
+                if (${criteriaCacheField} == null
+                        || !${criteriaCacheField}.equals(criteria))
                 {
                     $field = ${otherTablePeerName}.doSelect(criteria, con);
+$torqueGen.setVariable("indent", "                    ")##
+$torqueGen.mergepoint("backreference")##
                 }
             }
         }

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm?rev=1518002&r1=1518001&r2=1518002&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/joinGetterReferencingObject.vm
Tue Aug 27 21:31:36 2013
@@ -109,6 +109,8 @@
                 criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
 #end
                 $field = ${otherTablePeerName}.${otherPeerJoinSelectMethod}(criteria, connection);
+$torqueGen.setVariable("indent", "                ")##
+$torqueGen.mergepoint("backreference")##
             }
         }
         else
@@ -123,9 +125,12 @@
   #set ( $thisTableColumnGetter = $thisTableColumnElement.getAttribute("getter") )
             criteria.and(${otherTablePeerName}.${otherTablePeerColumnName}, ${thisTableColumnGetter}());
 #end
-            if (!${criteriaCacheField}.equals(criteria))
+            if (${criteriaCacheField} == null
+                        || !${criteriaCacheField}.equals(criteria))
             {
                 $field = ${otherTablePeerName}.${otherPeerJoinSelectMethod}(criteria, connection);
+$torqueGen.setVariable("indent", "                ")##
+$torqueGen.mergepoint("backreference")##
             }
         }
         $criteriaCacheField = criteria;

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsJoinTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsJoinTest.java?rev=1518002&r1=1518001&r2=1518002&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsJoinTest.java
(original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsJoinTest.java
Tue Aug 27 21:31:36 2013
@@ -31,6 +31,8 @@ import org.apache.torque.test.dbobject.M
 import org.apache.torque.test.dbobject.OIntegerPk;
 import org.apache.torque.test.peer.MultiRefPeer;
 import org.apache.torque.test.peer.MultiRefSameTablePeer;
+import org.apache.torque.test.peer.OIntegerPkPeer;
+import org.apache.torque.test.peer.PIntegerPkPeer;
 
 /**
  * Tests the fjoin getter methods for related objects in the generated
@@ -53,10 +55,14 @@ public class GetRelatedObjectsJoinTest e
 
     /**
      * Checks that the test principle for switching silent db fetch works.
+     * This test does not check the functionality to test, but rather ensures
+     * that we can switch of silent db fetching to check whether objects
+     * are already loaded or not.
      */
     public void testSwitchSilentFetchingOff() throws Exception
     {
         OIntegerPk baseObject = testData.getOIntegerPkList().get(0);
+        baseObject = OIntegerPkPeer.doSelectSingleRecord(baseObject);
         List<MultiRef> multiRefs
             = baseObject.getMultiRefs();
         // switch silent fetching off
@@ -88,9 +94,61 @@ public class GetRelatedObjectsJoinTest e
     public void testJoinGetter() throws Exception
     {
         OIntegerPk baseObject = testData.getOIntegerPkList().get(0);
+        baseObject = OIntegerPkPeer.doSelectSingleRecord(baseObject);
         List<MultiRef> multiRefs
             = baseObject.getMultiRefsJoinPIntegerPk(
                     new Criteria().addAscendingOrderByColumn(MultiRefPeer.ID));
+
+        // verify
+        assertEquals(3, multiRefs.size());
+        assertEquals("multiRef111a", multiRefs.get(0).getName());
+        assertEquals("multiRef111b", multiRefs.get(1).getName());
+        assertEquals("multiRef120", multiRefs.get(2).getName());
+        // switch silent fetching off
+        Adapter adapter = Torque.getOrCreateDatabase(Torque.getDefaultDB()).getAdapter();
+        try
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(null);
+            // check that the join has worked
+            assertEquals(
+                    testData.getPIntegerPkList().get(0),
+                    multiRefs.get(0).getPIntegerPk());
+            assertEquals(
+                    testData.getPIntegerPkList().get(0),
+                    multiRefs.get(1).getPIntegerPk());
+            assertEquals(
+                    testData.getPIntegerPkList().get(1),
+                    multiRefs.get(2).getPIntegerPk());
+            // check that the back reference to the base object
+            // is set in the joined objects
+            assertEquals(baseObject, multiRefs.get(0).getOIntegerPk());
+            assertEquals(baseObject, multiRefs.get(1).getOIntegerPk());
+            assertEquals(baseObject, multiRefs.get(2).getOIntegerPk());
+            assertFalse(multiRefs.get(0).isModified());
+            assertFalse(multiRefs.get(1).isModified());
+            assertFalse(multiRefs.get(2).isModified());
+        }
+        finally
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(adapter);
+        }
+    }
+
+    /**
+     * Checks that the joinGetter works if different tables are joined.
+     *
+     * @throws Exception if the database fails
+     */
+    public void testJoinGetterAfterInit() throws Exception
+    {
+        OIntegerPk baseObject = testData.getOIntegerPkList().get(0);
+        baseObject = OIntegerPkPeer.doSelectSingleRecord(baseObject);
+        baseObject.initMultiRefs();
+        List<MultiRef> multiRefs
+            = baseObject.getMultiRefsJoinPIntegerPk(
+                    new Criteria().addAscendingOrderByColumn(MultiRefPeer.ID));
+
+        // verify
         assertEquals(3, multiRefs.size());
         assertEquals("multiRef111a", multiRefs.get(0).getName());
         assertEquals("multiRef111b", multiRefs.get(1).getName());
@@ -100,6 +158,7 @@ public class GetRelatedObjectsJoinTest e
         try
         {
             Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(null);
+            // check that the join has worked
             assertEquals(
                     testData.getPIntegerPkList().get(0),
                     multiRefs.get(0).getPIntegerPk());
@@ -109,6 +168,61 @@ public class GetRelatedObjectsJoinTest e
             assertEquals(
                     testData.getPIntegerPkList().get(1),
                     multiRefs.get(2).getPIntegerPk());
+            // check that the back reference to the base object
+            // is set in the joined objects
+            assertEquals(baseObject, multiRefs.get(0).getOIntegerPk());
+            assertEquals(baseObject, multiRefs.get(1).getOIntegerPk());
+            assertEquals(baseObject, multiRefs.get(2).getOIntegerPk());
+            assertFalse(multiRefs.get(0).isModified());
+            assertFalse(multiRefs.get(1).isModified());
+            assertFalse(multiRefs.get(2).isModified());
+        }
+        finally
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(adapter);
+        }
+    }
+
+    /**
+     * Checks that the joinGetter works if different tables are joined
+     * and the collection was already loaded with another criteria.
+     *
+     * @throws Exception if the database fails
+     */
+    public void testJoinGetterWithOtherCriteria() throws Exception
+    {
+        OIntegerPk baseObject = testData.getOIntegerPkList().get(0);
+        baseObject = OIntegerPkPeer.doSelectSingleRecord(baseObject);
+        baseObject.getMultiRefsJoinPIntegerPk(
+                    new Criteria().addAscendingOrderByColumn(MultiRefPeer.ID));
+        List<MultiRef> multiRefs
+            = baseObject.getMultiRefsJoinPIntegerPk(
+                    new Criteria().addAscendingOrderByColumn(MultiRefPeer.ID)
+                        .where(PIntegerPkPeer.ID,
+                            testData.getPIntegerPkList().get(0).getId()));
+
+        // verify
+        assertEquals(2, multiRefs.size());
+        assertEquals("multiRef111a", multiRefs.get(0).getName());
+        assertEquals("multiRef111b", multiRefs.get(1).getName());
+        // switch silent fetching off
+        Adapter adapter = Torque.getOrCreateDatabase(Torque.getDefaultDB()).getAdapter();
+        try
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(null);
+            // check that the join has worked
+            assertEquals(
+                    testData.getPIntegerPkList().get(0),
+                    multiRefs.get(0).getPIntegerPk());
+            assertEquals(
+                    testData.getPIntegerPkList().get(0),
+                    multiRefs.get(1).getPIntegerPk());
+            // check that the back reference to the base object
+            // is set in the joined objects
+            assertEquals(baseObject, multiRefs.get(0).getOIntegerPk());
+            assertEquals(baseObject, multiRefs.get(1).getOIntegerPk());
+            assertFalse(multiRefs.get(0).isModified());
+            assertFalse(multiRefs.get(1).isModified());
         }
         finally
         {
@@ -124,6 +238,7 @@ public class GetRelatedObjectsJoinTest e
     public void testJoinGetterSameObject() throws Exception
     {
         OIntegerPk baseObject = testData.getOIntegerPkList().get(0);
+        baseObject = OIntegerPkPeer.doSelectSingleRecord(baseObject);
         List<MultiRefSameTable> multiRefSameTables
             = baseObject.getMultiRefSameTableRelatedByReference1sJoinOIntegerPkRelatedByReference2(
                     new Criteria().addAscendingOrderByColumn(MultiRefSameTablePeer.ID));
@@ -145,6 +260,21 @@ public class GetRelatedObjectsJoinTest e
             assertEquals(
                     testData.getOIntegerPkList().get(1),
                     multiRefSameTables.get(2).getOIntegerPkRelatedByReference2());
+            // check that the back reference to the base object
+            // is set in the joined objects
+            assertEquals(
+                    baseObject,
+                    multiRefSameTables.get(0).getOIntegerPkRelatedByReference1());
+            assertEquals(
+                    baseObject,
+                    multiRefSameTables.get(1).getOIntegerPkRelatedByReference1());
+            assertEquals(
+                    baseObject,
+                    multiRefSameTables.get(2).getOIntegerPkRelatedByReference1());
+            // check that adding the backreference did not lead to changed objects
+            assertFalse(multiRefSameTables.get(0).isModified());
+            assertFalse(multiRefSameTables.get(1).isModified());
+            assertFalse(multiRefSameTables.get(2).isModified());
         }
         finally
         {

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsTest.java?rev=1518002&r1=1518001&r2=1518002&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsTest.java
(original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/dataobject/GetRelatedObjectsTest.java
Tue Aug 27 21:31:36 2013
@@ -19,14 +19,19 @@ package org.apache.torque.generated.data
  * under the License.
  */
 
+import java.sql.Connection;
 import java.util.List;
 
 import org.apache.torque.BaseDatabaseTestCase;
+import org.apache.torque.Torque;
+import org.apache.torque.TorqueException;
+import org.apache.torque.adapter.Adapter;
 import org.apache.torque.criteria.Criteria;
 import org.apache.torque.test.dbobject.Author;
 import org.apache.torque.test.dbobject.Book;
 import org.apache.torque.test.peer.AuthorPeer;
 import org.apache.torque.test.peer.BookPeer;
+import org.apache.torque.util.Transaction;
 
 /**
  * Tests whether the getRelatedObjects methods works
@@ -46,6 +51,37 @@ public class GetRelatedObjectsTest exten
     }
 
     /**
+     * Checks that the test principle for switching silent db fetch works.
+     * This test does not check the functionality to test, but rather ensures
+     * that we can switch of silent db fetching to check whether objects
+     * are already loaded or not.
+     */
+    public void testSwitchSilentFetchingOff() throws Exception
+    {
+        Author baseObject = authorList.get(0);
+        baseObject = AuthorPeer.doSelectSingleRecord(baseObject);
+        // switch silent fetching off
+        Adapter adapter = Torque.getOrCreateDatabase(Torque.getDefaultDB()).getAdapter();
+        try
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(null);
+            try
+            {
+                baseObject.getBooks();
+                fail("Exception excpected");
+            }
+            catch (Exception e)
+            {
+                // expected
+            }
+        }
+        finally
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(adapter);
+        }
+    }
+
+    /**
      * Tests getters of related objects.
      *
      * @throws Exception if the test fails
@@ -63,6 +99,7 @@ public class GetRelatedObjectsTest exten
         // verify
         assertEquals(10, books.size());
         assertTrue(books.get(0).getTitle().endsWith("- Author 1"));
+        checkBackreferenceExists(author, books);
     }
 
     /**
@@ -106,6 +143,7 @@ public class GetRelatedObjectsTest exten
         // verify
         assertEquals(1, books.size());
         assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
     }
 
     /**
@@ -130,6 +168,7 @@ public class GetRelatedObjectsTest exten
         // verify
         assertEquals(1, books.size());
         assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
     }
 
     /**
@@ -154,5 +193,162 @@ public class GetRelatedObjectsTest exten
         // verify
         assertEquals(1, books.size());
         assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
+    }
+
+    /**
+     * Tests getters of related objects.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testGetRelatedObjectsWithConnection() throws Exception
+    {
+        Criteria criteria = new Criteria().where(
+                AuthorPeer.AUTHOR_ID,
+                authorList.get(0).getAuthorId());
+        Author author = AuthorPeer.doSelect(criteria).get(0);
+
+        // execute loading books
+        Connection connection = Transaction.begin();
+        List<Book> books = author.getBooks(connection);
+        Transaction.commit(connection);
+
+        // verify
+        assertEquals(10, books.size());
+        assertTrue(books.get(0).getTitle().endsWith("- Author 1"));
+        checkBackreferenceExists(author, books);
+    }
+
+    /**
+     * Tests getters of related objects, after the collection was initialized.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testGetRelatedObjectsWithConnectionAfterInit() throws Exception
+    {
+        Criteria criteria = new Criteria().where(
+                AuthorPeer.AUTHOR_ID,
+                authorList.get(0).getAuthorId());
+        Author author = AuthorPeer.doSelect(criteria).get(0);
+
+        // execute : init, then load books
+        author.initBooks();
+        Connection connection = Transaction.begin();
+        List<Book> books = author.getBooks(connection);
+        Transaction.commit(connection);
+
+        // verify
+        // Books should not be loaded from db but cache should be used
+        assertEquals(0, books.size());
     }
+
+    /**
+     * Tests getters of related objects with a Criteria object
+     *
+     * @throws Exception if the test fails
+     */
+    public void testGetRelatedObjectsWithConnectionAndCriteria() throws Exception
+    {
+        Criteria criteria = new Criteria().where(
+                AuthorPeer.AUTHOR_ID,
+                authorList.get(0).getAuthorId());
+        Author author = AuthorPeer.doSelect(criteria).get(0);
+        Book book = authorList.get(0).getBooks().get(2);
+
+        // execute : load books
+        criteria = new Criteria().where(BookPeer.BOOK_ID, book.getBookId());
+        Connection connection = Transaction.begin();
+        List<Book> books = author.getBooks(criteria, connection);
+        Transaction.commit(connection);
+
+        // verify
+        assertEquals(1, books.size());
+        assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
+    }
+
+    /**
+     * Tests getters of related objects with a Criteria object
+     * after the related objects were loaded with another criteria.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testGetRelatedObjectsWithConneectionAndOtherCriteria() throws Exception
+    {
+        Criteria criteria = new Criteria().where(
+                AuthorPeer.AUTHOR_ID,
+                authorList.get(0).getAuthorId());
+        Author author = AuthorPeer.doSelect(criteria).get(0);
+        author.getBooks();
+        Book book = authorList.get(0).getBooks().get(2);
+
+        // execute : load books
+        criteria = new Criteria().where(BookPeer.BOOK_ID, book.getBookId());
+        Connection connection = Transaction.begin();
+        List<Book> books = author.getBooks(criteria, connection);
+        Transaction.commit(connection);
+
+        // verify
+        assertEquals(1, books.size());
+        assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
+    }
+
+    /**
+     * Tests getters of related objects with a Criteria object
+     * after the init method was called on the collection.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testGetRelatedObjectsWithConnectionAndCriteriaAfterInit() throws Exception
+    {
+        Criteria criteria = new Criteria().where(
+                AuthorPeer.AUTHOR_ID,
+                authorList.get(0).getAuthorId());
+        Author author = AuthorPeer.doSelect(criteria).get(0);
+        author.initBooks();
+        Book book = authorList.get(0).getBooks().get(2);
+
+        // execute : load books
+        criteria = new Criteria().where(BookPeer.BOOK_ID, book.getBookId());
+        Connection connection = Transaction.begin();
+        List<Book> books = author.getBooks(criteria, connection);
+        Transaction.commit(connection);
+
+        // verify
+        assertEquals(1, books.size());
+        assertEquals(book.getBookId(), books.get(0).getBookId());
+        checkBackreferenceExists(author, books);
+    }
+
+
+    /**
+     * Switches silent fetching off and checks that a backreference
+     * to the author is set on each book in the list.
+     *
+     * @param author the author to which the backreference should be made
+     * @param books the books to check.
+     *
+     * @throws TorqueException if a database operation fails
+     */
+    private void checkBackreferenceExists(final Author author, final List<Book> books)
+            throws TorqueException
+    {
+        Adapter adapter = Torque.getOrCreateDatabase(Torque.getDefaultDB()).getAdapter();
+        try
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(null);
+            for (Book book : books)
+            {
+                assertEquals(author, book.getAuthor());
+                // check that adding the backreference did not lead to changed objects
+                assertFalse(book.isModified());
+            }
+        }
+        finally
+        {
+            Torque.getOrCreateDatabase(Torque.getDefaultDB()).setAdapter(adapter);
+        }
+    }
+
 }



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