cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1237755 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/exp/parser/ASTEqual.java test/java/org/apache/cayenne/CayenneDataObjectTest.java test/java/org/apache/cayenne/exp/ExpressionTest.java
Date Mon, 30 Jan 2012 16:17:55 GMT
Author: oltka
Date: Mon Jan 30 16:17:54 2012
New Revision: 1237755

URL: http://svn.apache.org/viewvc?rev=1237755&view=rev
Log:
CAY-1657 Expression: in-memory comparison requires persistent to be in the same context as
exp parameters to match (added test)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/parser/ASTEqual.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/parser/ASTEqual.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/parser/ASTEqual.java?rev=1237755&r1=1237754&r2=1237755&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/parser/ASTEqual.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/parser/ASTEqual.java
Mon Jan 30 16:17:54 2012
@@ -22,6 +22,8 @@ package org.apache.cayenne.exp.parser;
 import java.math.BigDecimal;
 import java.util.List;
 
+import org.apache.cayenne.DataObject;
+import org.apache.cayenne.PersistenceState;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ValueInjector;
 import org.apache.commons.logging.Log;
@@ -121,6 +123,18 @@ public class ASTEqual extends ConditionN
             return false;
         }
 
+        if (o1 instanceof DataObject) {
+            if (o2 instanceof DataObject && ((DataObject)o1).getObjectId().equals(((DataObject)o2).getObjectId()))
{
+                if (((DataObject)o1).getObjectContext().equals(((DataObject)o2).getObjectContext()))
{
+                    return o1.equals(o2);
+                } else if (((DataObject)o1).getPersistenceState() == ((DataObject)o2).getPersistenceState())
{
+                    DataObject o1_context = (((DataObject)o2).getObjectContext()).localObject(((DataObject)o1));
+                    return ((DataObject)o2).equals(o1_context);
+                }
+            }
+            return false;
+        }
+        
         return o1.equals(o2);
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java?rev=1237755&r1=1237754&r2=1237755&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
Mon Jan 30 16:17:54 2012
@@ -37,7 +37,7 @@ public class CayenneDataObjectTest exten
 
     @Inject
     private ObjectContext context;
-
+    
     public void testSetObjectId() throws Exception {
         CayenneDataObject object = new CayenneDataObject();
         ObjectId oid = new ObjectId("T");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java?rev=1237755&r1=1237754&r2=1237755&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
Mon Jan 30 16:17:54 2012
@@ -22,13 +22,27 @@ package org.apache.cayenne.exp;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.Collections;
+import java.util.List;
 
-import junit.framework.TestCase;
-
+import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.ObjectId;
+import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.configuration.server.ServerRuntime;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.testdo.testmap.Artist;
-
-public class ExpressionTest extends TestCase {
+import org.apache.cayenne.testdo.testmap.Painting;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
+
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class ExpressionTest extends ServerCase {
+    
+    @Inject
+    private ObjectContext context;
+    
+    @Inject
+    private ServerRuntime runtime;
 
     public void testFromStringLong() {
         Expression e = Expression.fromString("216201000180L");
@@ -212,4 +226,69 @@ public class ExpressionTest extends Test
 
         assertEquals("a = enum:org.apache.cayenne.exp.ExpEnum1.TWO", buffer.toString());
     }
+    
+    public void testEqualsObjects() {
+        
+        assertTrue(context instanceof DataContext);
+        
+        DataContext context2 = (DataContext) runtime.getContext();
+        
+        Artist a1 = context.newObject(Artist.class);
+        a1.setArtistName("Equals");
+        Painting p1 = context.newObject(Painting.class);
+        p1.setToArtist(a1);
+        p1.setPaintingTitle("painting1");
+        
+        SelectQuery query = new SelectQuery(Painting.class);
+        Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1); 
+        query.setQualifier(e);
+        
+        context.commitChanges();
+        
+        assertNotSame(context2, context);
+        
+        List<Artist> objects = context2.performQuery(query);
+        assertEquals(1, objects.size());
+        
+        // 2 same objects in different contexts
+        assertTrue(e.match(objects.get(0))); 
+        
+        // we change one object - so the objects are different now (PersistenceState different)
+        a1.setArtistName("newName");
+        
+        SelectQuery q2 = new SelectQuery(Painting.class);
+        Expression ex2 = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1); 
+        q2.setQualifier(ex2);
+        
+        assertFalse(ex2.match(objects.get(0))); 
+        
+        Artist a2 = context.newObject(Artist.class);
+        a1.setArtistName("Equals");
+        
+        SelectQuery q = new SelectQuery(Painting.class);
+        Expression ex = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a2); 
+        q.setQualifier(ex);
+        
+        // 2 different objects in different contexts
+        assertFalse(ex.match(objects.get(0))); 
+    }
+    
+    @Override
+    protected void setUpAfterInjection() throws Exception {
+        
+        SelectQuery query = new SelectQuery(Artist.class);
+        Expression qual = ExpressionFactory.likeExp("artistName", "Equals");
+        query.setQualifier(qual);
+        List<?> objects = context.performQuery(query);
+        
+        if (objects.size() > 0) {
+            SelectQuery query1 = new SelectQuery(Painting.class);
+            Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, objects.get(0));

+            query.setQualifier(e);
+            objects.addAll(context.performQuery(query1));
+        }
+        
+        
+        context.deleteObjects(objects);
+    }
 }



Mime
View raw message