cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r407107 - in /incubator/cayenne/main/trunk/cayenne/cayenne-java/src: cayenne/java/org/objectstyle/cayenne/access/ tests/java/org/objectstyle/cayenne/ tests/java/org/objectstyle/cayenne/access/
Date Wed, 17 May 2006 00:04:52 GMT
Author: aadamchik
Date: Tue May 16 17:04:52 2006
New Revision: 407107

URL: http://svn.apache.org/viewcvs?rev=407107&view=rev
Log:
CAY-548 - unit test and fix

Modified:
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectDiff.java
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneDataObjectFlattenedRelTst.java
    incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/MockDataNode.java

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectDiff.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectDiff.java?rev=407107&r1=407106&r2=407107&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectDiff.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectDiff.java
Tue May 16 17:04:52 2006
@@ -92,18 +92,18 @@
     }
 
     ObjectId getArcSnapshotValue(String propertyName) {
-        Object value = arcSnapshot != null
-                ? arcSnapshot.get(propertyName)
-                : null;
+        Object value = arcSnapshot != null ? arcSnapshot.get(propertyName) : null;
 
         if (value instanceof Fault) {
             Persistent source = (Persistent) objectStore.getNode(nodeId);
-            Persistent target = (Persistent) ((Fault) value).resolveFault(source, propertyName);
-            
+            Persistent target = (Persistent) ((Fault) value).resolveFault(
+                    source,
+                    propertyName);
+
             value = target != null ? target.getObjectId() : null;
             arcSnapshot.put(propertyName, value);
         }
-        
+
         return (ObjectId) value;
     }
 
@@ -133,20 +133,12 @@
     }
 
     void addDiff(GraphDiff diff) {
-        if (otherDiffs == null) {
-            otherDiffs = new ArrayList(3);
-        }
 
-        otherDiffs.add(diff);
+        boolean addDiff = true;
 
-        if (diff instanceof ArcOperation) {
+        if (diff instanceof ArcOperation && snapshot != null) {
 
             ArcOperation arcDiff = (ArcOperation) diff;
-
-            if (snapshot == null) {
-                return;
-            }
-
             Object targetId = arcDiff.getTargetNodeId();
             String arcId = arcDiff.getArcId().toString();
 
@@ -175,7 +167,12 @@
 
                     // "delete" cancels "create" and vice versa...
                     if (oldOp != null && oldOp.isDelete() != arcDiff.isDelete())
{
+                        addDiff = false;
                         flatIds.remove(arcDiff);
+
+                        if (otherDiffs != null) {
+                            otherDiffs.remove(oldOp);
+                        }
                     }
                 }
             }
@@ -193,6 +190,14 @@
                         : "Unrecognized property for arcId " + arcId + ": " + property;
                 throw new CayenneRuntimeException(message);
             }
+        }
+
+        if (addDiff) {
+            if (otherDiffs == null) {
+                otherDiffs = new ArrayList(3);
+            }
+
+            otherDiffs.add(diff);
         }
     }
 

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneDataObjectFlattenedRelTst.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneDataObjectFlattenedRelTst.java?rev=407107&r1=407106&r2=407107&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneDataObjectFlattenedRelTst.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneDataObjectFlattenedRelTst.java
Tue May 16 17:04:52 2006
@@ -60,8 +60,10 @@
 import org.objectstyle.art.ArtGroup;
 import org.objectstyle.art.Artist;
 import org.objectstyle.cayenne.access.DataContext;
+import org.objectstyle.cayenne.access.MockDataNode;
 import org.objectstyle.cayenne.exp.ExpressionFactory;
 import org.objectstyle.cayenne.query.SelectQuery;
+import org.objectstyle.cayenne.unit.CayenneTestCase;
 
 /**
  * Test case for objects with flattened relationships.
@@ -71,20 +73,29 @@
 // TODO: redefine all test cases in terms of entities in "relationships" map
 // and merge this test case with FlattenedRelationshipsTst that inherits
 // from RelationshipTestCase.
-public class CayenneDataObjectFlattenedRelTst extends CayenneDOTestBase {
+public class CayenneDataObjectFlattenedRelTst extends CayenneTestCase {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        deleteTestData();
+    }
 
     public void testReadFlattenedRelationship() throws Exception {
+        DataContext context = createDataContext();
+
         createTestData("testReadFlattenedRelationship");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
         List groupList = a1.getGroupArray();
         assertNotNull(groupList);
         assertEquals(0, groupList.size());
     }
 
     public void testReadFlattenedRelationship2() throws Exception {
+        DataContext context = createDataContext();
+
         createTestData("testReadFlattenedRelationship2");
 
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
         List groupList = a1.getGroupArray();
         assertNotNull(groupList);
         assertEquals(1, groupList.size());
@@ -96,20 +107,21 @@
     public void testAddToFlattenedRelationship() throws Exception {
 
         createTestData("testAddToFlattenedRelationship");
+        DataContext context = createDataContext();
 
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
         assertEquals(0, a1.getGroupArray().size());
 
         SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                 "name",
                 "g1"));
-        List results = ctxt.performQuery(q);
+        List results = context.performQuery(q);
         assertEquals(1, results.size());
 
-        assertFalse(ctxt.hasChanges());
+        assertFalse(context.hasChanges());
         ArtGroup group = (ArtGroup) results.get(0);
         a1.addToGroupArray(group);
-        assertTrue(ctxt.hasChanges());
+        assertTrue(context.hasChanges());
 
         List groupList = a1.getGroupArray();
         assertEquals(1, groupList.size());
@@ -119,11 +131,11 @@
         a1.getDataContext().commitChanges();
 
         // and check again
-        assertFalse(ctxt.hasChanges());
+        assertFalse(context.hasChanges());
 
         // refetch artist with a different context
-        ctxt = createDataContext();
-        a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        context = createDataContext();
+        a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
         groupList = a1.getGroupArray();
         assertEquals(1, groupList.size());
         assertEquals("g1", ((ArtGroup) groupList.get(0)).getName());
@@ -132,12 +144,14 @@
     // Test case to show up a bug in committing more than once
     public void testDoubleCommitAddToFlattenedRelationship() throws Exception {
         createTestData("testDoubleCommitAddToFlattenedRelationship");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        DataContext context = createDataContext();
+
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
 
         SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                 "name",
                 "g1"));
-        List results = ctxt.performQuery(q);
+        List results = context.performQuery(q);
         assertEquals(1, results.size());
 
         ArtGroup group = (ArtGroup) results.get(0);
@@ -164,7 +178,9 @@
 
     public void testRemoveFromFlattenedRelationship() throws Exception {
         createTestData("testRemoveFromFlattenedRelationship");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        DataContext context = createDataContext();
+
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
 
         ArtGroup group = (ArtGroup) a1.getGroupArray().get(0);
         a1.removeFromGroupArray(group);
@@ -184,8 +200,9 @@
     // record is deleted
     // at the same time (same transaction) as one of the record to which it links.
     public void testRemoveFlattenedRelationshipAndRootRecord() throws Exception {
+        DataContext context = createDataContext();
         createTestData("testRemoveFlattenedRelationshipAndRootRecord");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
         DataContext dc = a1.getDataContext();
 
         ArtGroup group = (ArtGroup) a1.getGroupArray().get(0);
@@ -203,13 +220,14 @@
     }
 
     public void testAddRemoveFlattenedRelationship1() throws Exception {
+        DataContext context = createDataContext();
         createTestData("testAddRemoveFlattenedRelationship1");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
 
         SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                 "name",
                 "g1"));
-        List results = ctxt.performQuery(q);
+        List results = context.performQuery(q);
         assertEquals(1, results.size());
 
         ArtGroup group = (ArtGroup) results.get(0);
@@ -218,7 +236,7 @@
 
         blockQueries();
         try {
-            ctxt.commitChanges();
+            context.commitChanges();
         }
         finally {
             unblockQueries();
@@ -226,20 +244,33 @@
     }
 
     public void testAddRemoveFlattenedRelationship2() throws Exception {
-
         createTestData("testAddRemoveFlattenedRelationship2");
-        Artist a1 = (Artist) DataObjectUtils.objectForPK(ctxt, Artist.class, 33001);
+
+        DataContext context = createDataContext();
+
+        Artist a1 = (Artist) DataObjectUtils.objectForPK(context, Artist.class, 33001);
 
         SelectQuery q = new SelectQuery(ArtGroup.class);
-        List results = ctxt.performQuery(q);
+        List results = context.performQuery(q);
         assertEquals(2, results.size());
 
         ArtGroup g1 = (ArtGroup) results.get(0);
         ArtGroup g2 = (ArtGroup) results.get(1);
         a1.addToGroupArray(g1);
         a1.addToGroupArray(g2);
+
+        // test that there is no delete query issued when a flattened join is first
+        // added
+        // and then deleted AND there are some other changes (CAY-548)
         a1.removeFromGroupArray(g1);
 
-        ctxt.commitChanges();
+        MockDataNode engine = MockDataNode.interceptNode(getDomain(), getNode());
+        try {
+            context.commitChanges();
+            assertEquals(1, engine.getRunCount());
+        }
+        finally {
+            engine.stopInterceptNode();
+        }
     }
 }

Modified: incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/MockDataNode.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/MockDataNode.java?rev=407107&r1=407106&r2=407107&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/MockDataNode.java
(original)
+++ incubator/cayenne/main/trunk/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/MockDataNode.java
Tue May 16 17:04:52 2006
@@ -75,6 +75,7 @@
     protected DataNode node;
 
     // mockup the actual results
+    protected boolean replaceResults;
     protected Map results = new HashMap();
     protected int runCount;
 
@@ -117,18 +118,24 @@
     }
 
     public void addExpectedResult(Query query, List result) {
+        replaceResults = true;
         results.put(query, result);
     }
 
     public void performQueries(Collection queries, OperationObserver resultConsumer) {
-        initWithPresetResults(queries, resultConsumer);
+        runCount += queries.size();
+
+        if (replaceResults) {
+            initWithPresetResults(queries, resultConsumer);
+        }
+        else {
+            node.performQueries(queries, resultConsumer);
+        }
     }
 
     private void initWithPresetResults(
             Collection queries,
             OperationObserver resultConsumer) {
-
-        runCount++;
 
         // stick preset results to the consumer
         Iterator it = queries.iterator();



Mime
View raw message