cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r479849 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/access/ test/java/org/apache/art/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/unit/util/
Date Tue, 28 Nov 2006 02:19:14 GMT
Author: aadamchik
Date: Mon Nov 27 18:19:13 2006
New Revision: 479849

URL: http://svn.apache.org/viewvc?view=rev&rev=479849
Log:
CAY-712: Allow modifying objects in validate* methods
(fix for 3.0)

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/unit/util/ValidationDelegate.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectDiff.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectStoreGraphDiff.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/Artist.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextValidationTest.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectDiff.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectDiff.java?view=diff&rev=479849&r1=479848&r2=479849
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectDiff.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectDiff.java
Mon Nov 27 18:19:13 2006
@@ -118,6 +118,10 @@
             });
         }
     }
+    
+    Object getObject() {
+        return object;
+    }
 
     ClassDescriptor getClassDescriptor() {
         // class descriptor is initiated in constructor, but is nullified on serialization

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectStoreGraphDiff.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectStoreGraphDiff.java?view=diff&rev=479849&r1=479848&r2=479849
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectStoreGraphDiff.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/ObjectStoreGraphDiff.java
Mon Nov 27 18:19:13 2006
@@ -20,6 +20,7 @@
 package org.apache.cayenne.access;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
@@ -64,39 +65,55 @@
             return true;
         }
 
-        ValidationResult result = new ValidationResult();
         boolean noop = true;
 
-        Iterator it = getChangesByObjectId().entrySet().iterator();
+        // build a new collection for validation as validation methods may result in
+        // ObjectStore modifications
+
+        Collection objectsToValidate = null;
+
+        Iterator it = getChangesByObjectId().values().iterator();
         while (it.hasNext()) {
 
-            Map.Entry entry = (Map.Entry) it.next();
+            ObjectDiff diff = (ObjectDiff) it.next();
 
-            if (!((ObjectDiff) entry.getValue()).isNoop()) {
+            if (!diff.isNoop()) {
 
                 noop = false;
 
-                // accessing objectMap directly to avoid unneeded synchronization.
-                Persistent object = (Persistent) objectStore.getNodeNoSync(entry.getKey());
-                
-                if (object instanceof Validating) {
-                    switch (object.getPersistenceState()) {
-                        case PersistenceState.NEW:
-                            ((Validating) object).validateForInsert(result);
-                            break;
-                        case PersistenceState.MODIFIED:
-                            ((Validating) object).validateForUpdate(result);
-                            break;
-                        case PersistenceState.DELETED:
-                            ((Validating) object).validateForDelete(result);
-                            break;
+                if (diff.getObject() instanceof Validating) {
+                    if (objectsToValidate == null) {
+                        objectsToValidate = new ArrayList();
                     }
+
+                    objectsToValidate.add(diff.getObject());
                 }
+
             }
         }
 
-        if (result.hasFailures()) {
-            throw new ValidationException(result);
+        if (objectsToValidate != null) {
+            ValidationResult result = new ValidationResult();
+
+            Iterator validationIt = objectsToValidate.iterator();
+            while (validationIt.hasNext()) {
+                Validating object = (Validating) validationIt.next();
+                switch (((Persistent) object).getPersistenceState()) {
+                    case PersistenceState.NEW:
+                        object.validateForInsert(result);
+                        break;
+                    case PersistenceState.MODIFIED:
+                        object.validateForUpdate(result);
+                        break;
+                    case PersistenceState.DELETED:
+                        object.validateForDelete(result);
+                        break;
+                }
+            }
+
+            if (result.hasFailures()) {
+                throw new ValidationException(result);
+            }
         }
 
         return noop;

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/Artist.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/Artist.java?view=diff&rev=479849&r1=479848&r2=479849
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/Artist.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/Artist.java
Mon Nov 27 18:19:13 2006
@@ -1,10 +1,12 @@
 package org.apache.art;
 
 import org.apache.art.auto._Artist;
+import org.apache.cayenne.unit.util.ValidationDelegate;
 import org.apache.cayenne.validation.ValidationResult;
 
 public class Artist extends _Artist {
 
+    protected transient ValidationDelegate validationDelegate;
     protected boolean validateForSaveCalled;
     protected boolean prePersisted;
     protected boolean preRemoved;
@@ -22,6 +24,10 @@
         validateForSaveCalled = false;
     }
 
+    public void setValidationDelegate(ValidationDelegate validationDelegate) {
+        this.validationDelegate = validationDelegate;
+    }
+
     public void resetCallbackFlags() {
         prePersisted = false;
         preRemoved = false;
@@ -34,6 +40,9 @@
 
     public void validateForSave(ValidationResult validationResult) {
         validateForSaveCalled = true;
+        if (validationDelegate != null) {
+            validationDelegate.validateForSave(this, validationResult);
+        }
         super.validateForSave(validationResult);
     }
 

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextValidationTest.java?view=diff&rev=479849&r1=479848&r2=479849
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextValidationTest.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DataContextValidationTest.java
Mon Nov 27 18:19:13 2006
@@ -20,7 +20,11 @@
 package org.apache.cayenne.access;
 
 import org.apache.art.Artist;
+import org.apache.art.Painting;
+import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.unit.CayenneCase;
+import org.apache.cayenne.unit.util.ValidationDelegate;
+import org.apache.cayenne.validation.ValidationResult;
 
 /**
  * @author Andrus Adamchik
@@ -53,5 +57,36 @@
         a2.setArtistName("a2");
         context.commitChanges();
         assertFalse(a2.isValidateForSaveCalled());
+    }
+
+    public void testValidationModifyingContext() throws Exception {
+        deleteTestData();
+
+        ValidationDelegate delegate = new ValidationDelegate() {
+
+            public void validateForSave(Object object, ValidationResult validationResult)
{
+
+                Artist a = (Artist) object;
+                Painting p = (Painting) a.getObjectContext().newObject(Painting.class);
+                p.setPaintingTitle("XXX");
+                p.setToArtist(a);
+            }
+        };
+
+        DataContext context = createDataContext();
+
+        context.setValidatingObjectsOnCommit(true);
+        Artist a1 = (Artist) context.newObject(Artist.class);
+        a1.setValidationDelegate(delegate);
+        a1.setArtistName("a1");
+
+        // add another artist to ensure that modifying context works when more than one
+        // object is committed
+        Artist a2 = (Artist) context.newObject(Artist.class);
+        a2.setValidationDelegate(delegate);
+        a2.setArtistName("a2");
+        context.commitChanges();
+
+        assertEquals(2, context.performQuery(new SelectQuery(Painting.class)).size());
     }
 }

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/unit/util/ValidationDelegate.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/unit/util/ValidationDelegate.java?view=auto&rev=479849
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/unit/util/ValidationDelegate.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/unit/util/ValidationDelegate.java
Mon Nov 27 18:19:13 2006
@@ -0,0 +1,31 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+package org.apache.cayenne.unit.util;
+
+import org.apache.cayenne.validation.ValidationResult;
+
+/**
+ * A callback interface used by unit tests to implement custom validation logic.
+ * 
+ * @author Andrus Adamchik
+ */
+public interface ValidationDelegate {
+
+    void validateForSave(Object object, ValidationResult validationResult);
+}



Mime
View raw message