incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r939404 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/util/ test/java/org/apache/bval/jsr303/
Date Thu, 29 Apr 2010 17:57:13 GMT
Author: dwoods
Date: Thu Apr 29 17:57:13 2010
New Revision: 939404

URL: http://svn.apache.org/viewvc?rev=939404&view=rev
Log:
BVAL-29 Defer node creation in implementation of ConstraintViolationBuilder API.  Contributed
by Carlos Vara.

Added:
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
  (with props)
Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java?rev=939404&r1=939403&r2=939404&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderCustomizableContextImpl.java
Thu Apr 29 17:57:13 2010
@@ -20,7 +20,6 @@ package org.apache.bval.jsr303.util;
 
 
 import javax.validation.ConstraintValidatorContext;
-import javax.validation.Path;
 
 import org.apache.bval.jsr303.ConstraintValidatorContextImpl;
 
@@ -32,27 +31,41 @@ final class NodeBuilderCustomizableConte
     private final ConstraintValidatorContextImpl parent;
     private final String messageTemplate;
     private final PathImpl propertyPath;
+    // The name of the last "added" node, it will only be added if it has a non-null name
+    // The actual incorporation in the path will take place when the definition of the current
leaf node is complete
+    private String lastNodeName; // Not final as it can be re-used
 
     NodeBuilderCustomizableContextImpl(ConstraintValidatorContextImpl contextImpl, String
template,
-                              PathImpl path) {
+                              PathImpl path, String name) {
         parent = contextImpl;
         messageTemplate = template;
         propertyPath = path;
+        lastNodeName = name;
     }
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeContextBuilder inIterable()
{
-        return new NodeContextBuilderImpl(parent, messageTemplate, propertyPath);
+        // Modifies the "previous" node in the path
+        this.propertyPath.getLeafNode().setInIterable( true );
+        return new NodeContextBuilderImpl(parent, messageTemplate, propertyPath, lastNodeName);
     }
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext
addNode(
           String name) {
-        Path.Node node = new NodeImpl(name);
-        propertyPath.addNode(node);
-        return this;
+        addLastNodeIfNeeded();
+        lastNodeName = name;
+        return this; // Re-use this instance
     }
 
     public ConstraintValidatorContext addConstraintViolation() {
+        addLastNodeIfNeeded();
         parent.addError(messageTemplate, propertyPath);
         return parent;
     }
+    
+    private void addLastNodeIfNeeded() {
+        if (lastNodeName != null) {
+            NodeImpl node = new NodeImpl(lastNodeName);
+            propertyPath.addNode(node);
+        }
+    }
 }

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java?rev=939404&r1=939403&r2=939404&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeBuilderDefinedContextImpl.java
Thu Apr 29 17:57:13 2010
@@ -41,9 +41,8 @@ public final class NodeBuilderDefinedCon
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext
addNode(
           String name) {
-        NodeImpl node = new NodeImpl(name);
-        propertyPath.addNode(node);
-        return new NodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath);
+        // Node not yet added, wait until more information is provided
+        return new NodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath,
name);
     }
 
     public ConstraintValidatorContext addConstraintViolation() {

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java?rev=939404&r1=939403&r2=939404&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NodeContextBuilderImpl.java
Thu Apr 29 17:57:13 2010
@@ -20,7 +20,6 @@ package org.apache.bval.jsr303.util;
 
 
 import javax.validation.ConstraintValidatorContext;
-import javax.validation.Path;
 
 import org.apache.bval.jsr303.ConstraintValidatorContextImpl;
 
@@ -32,36 +31,50 @@ final class NodeContextBuilderImpl
     private final ConstraintValidatorContextImpl parent;
     private final String messageTemplate;
     private final PathImpl propertyPath;
+    // The name of the last "added" node, it will only be added if it has a non-null name
+    // The actual incorporation in the path will take place when the definition of the current
leaf node is complete
+    private final String lastNodeName;
 
     NodeContextBuilderImpl(ConstraintValidatorContextImpl contextImpl,
-                                    String template, PathImpl path) {
+                                    String template, PathImpl path, String name) {
         parent = contextImpl;
         messageTemplate = template;
         propertyPath = path;
-        propertyPath.getLeafNode().setInIterable(true);
+        lastNodeName = name;
     }
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext
atKey(
           Object key) {
+        // Modifies the "previous" node in the path
         propertyPath.getLeafNode().setKey(key);
+        addLastNodeIfNeeded();
         return new NodeBuilderDefinedContextImpl(parent, messageTemplate, propertyPath);
     }
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext
atIndex(
           Integer index) {
+        // Modifies the "previous" node in the path
         propertyPath.getLeafNode().setIndex(index);
+        addLastNodeIfNeeded();
         return new NodeBuilderDefinedContextImpl(parent, messageTemplate, propertyPath);
     }
 
     public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext
addNode(
           String name) {
-        Path.Node node = new NodeImpl(name);
-        propertyPath.addNode(node);
-        return new NodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath);
+        addLastNodeIfNeeded();
+        return new NodeBuilderCustomizableContextImpl(parent, messageTemplate, propertyPath,
lastNodeName);
     }
 
     public ConstraintValidatorContext addConstraintViolation() {
+        addLastNodeIfNeeded();
         parent.addError(messageTemplate, propertyPath);
         return parent;
     }
+    
+    private void addLastNodeIfNeeded() {
+        if (lastNodeName != null) {
+            NodeImpl node = new NodeImpl(lastNodeName);
+            propertyPath.addNode(node);
+        }
+    }
 }
\ No newline at end of file

Added: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java?rev=939404&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
Thu Apr 29 17:57:13 2010
@@ -0,0 +1,228 @@
+/*
+ * 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.bval.jsr303;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.bval.jsr303.groups.Group;
+import org.apache.bval.jsr303.groups.Groups;
+import org.apache.bval.jsr303.util.PathImpl;
+import org.apache.bval.model.MetaBean;
+import org.apache.bval.model.MetaProperty;
+import org.apache.bval.model.ValidationListener;
+import org.apache.bval.model.ValidationListener.Error;
+import org.apache.bval.util.AccessStrategy;
+
+
+/**
+ * Checks to validate the correct implementation of
+ * {@link ConstraintValidatorContext} and its sub-interfaces.
+ * 
+ * @author Carlos Vara
+ */
+public class ConstraintValidatorContextTest extends TestCase {
+
+    private ConstraintValidatorContextImpl cvc;
+    private ConstraintViolationBuilder cvb;
+
+    private void resetConstraintValidatorContext() {
+        this.cvc = new ConstraintValidatorContextImpl(
+                new DummyContext<ValidationListener>(), null);
+        this.cvc.disableDefaultConstraintViolation();
+        this.cvb = cvc.buildConstraintViolationWithTemplate("dummy.msg.tpl");
+    }
+
+    // Test that builds a path and checks it against the expected one
+    public void testPathBuilding() {
+
+        resetConstraintValidatorContext();
+
+        // persons[1]
+        cvb.addNode("person").addNode(null).inIterable().atIndex(1)
+                .addConstraintViolation();
+        Error error = cvc.getErrorMessages().iterator().next();
+        PathImpl errorPath = (PathImpl) error.getOwner();
+        Assert.assertEquals("Incorrect path created", "person[1]", errorPath.toString());
+
+        resetConstraintValidatorContext();
+
+        // persons[lawyer].name
+        cvb.addNode("person").addNode("name").inIterable().atKey("john")
+                .addConstraintViolation();
+        error = cvc.getErrorMessages().iterator().next();
+        errorPath = (PathImpl) error.getOwner();
+        Assert.assertEquals("Incorrect path created", "person[john].name", errorPath.toString());
+
+        resetConstraintValidatorContext();
+
+        // [0].name[]
+        cvb.addNode(null).addNode("name").inIterable().atIndex(0).addNode(null)
+                .inIterable().addConstraintViolation();
+        error = cvc.getErrorMessages().iterator().next();
+        errorPath = (PathImpl) error.getOwner();
+        Assert.assertEquals("Incorrect path created", "[0].name[]", errorPath.toString());
+
+        resetConstraintValidatorContext();
+
+        // []
+        cvb.addNode(null).addNode(null).inIterable().addConstraintViolation();
+        error = cvc.getErrorMessages().iterator().next();
+        errorPath = (PathImpl) error.getOwner();
+        Assert.assertEquals("Incorrect path created", "[]", errorPath.toString());
+        
+        resetConstraintValidatorContext();
+        
+        // Adding only nulls should still give a root path
+        cvb.addNode(null).addNode(null).addNode(null).addNode(null).addConstraintViolation();
+        error = cvc.getErrorMessages().iterator().next();
+        errorPath = (PathImpl) error.getOwner();
+        Assert.assertTrue("Created path must be a root path", errorPath.isRootPath());
+
+    }
+
+    // TODO: mock
+    public static class DummyContext<T extends ValidationListener> implements
+            GroupValidationContext<T> {
+
+        public boolean collectValidated(ConstraintValidator constraint) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public ConstraintValidation getConstraintValidation() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Group getCurrentGroup() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Groups getGroups() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public MessageInterpolator getMessageResolver() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public PathImpl getPropertyPath() {
+            return PathImpl.createPathFromString("");
+        }
+
+        public MetaBean getRootMetaBean() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public TraversableResolver getTraversableResolver() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Object getValidatedValue() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setConstraintValidation(ConstraintValidation constraint) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setCurrentGroup(Group group) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setFixedValue(Object value) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public boolean collectValidated() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public AccessStrategy getAccess() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Object getBean() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public T getListener() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public MetaBean getMetaBean() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public MetaProperty getMetaProperty() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public String getPropertyName() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Object getPropertyValue() {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public Object getPropertyValue(AccessStrategy access) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void moveDown(MetaProperty prop, AccessStrategy access) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void moveUp(Object bean, MetaBean metaBean) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setBean(Object bean) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setBean(Object aBean, MetaBean aMetaBean) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setCurrentIndex(Integer index) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setCurrentKey(Object key) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setMetaBean(MetaBean metaBean) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+        public void setMetaProperty(MetaProperty metaProperty) {
+            throw new IllegalStateException("Unexpected call");
+        }
+
+    }
+
+}

Propchange: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message