incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romanst...@apache.org
Subject svn commit: r936297 - in /incubator/bval/trunk: ./ bval-core/src/main/java/org/apache/bval/util/ bval-jsr303/src/main/java/org/apache/bval/jsr303/ bval-jsr303/src/test/java/org/apache/bval/jsr303/
Date Wed, 21 Apr 2010 13:32:48 GMT
Author: romanstumm
Date: Wed Apr 21 13:32:47 2010
New Revision: 936297

URL: http://svn.apache.org/viewvc?rev=936297&view=rev
Log:
TCK test testOnlyFirstGroupInSequenceGetEvaluated now also running (single property validation
on non-public properties)
+ commons-beanutils 1.8.3 instead of 1.8.2
stop validation when errors found

Added:
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/TckReproducerTest.java
Modified:
    incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
    incubator/bval/trunk/pom.xml

Modified: incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java?rev=936297&r1=936296&r2=936297&view=diff
==============================================================================
--- incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
(original)
+++ incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
Wed Apr 21 13:32:47 2010
@@ -30,122 +30,131 @@ import java.util.Map;
  * field to access the value<br/>
  */
 public class PropertyAccess extends AccessStrategy {
-    private final Class<?> beanClass;
-    private final String propertyName;
-    private Field rememberField;
-
-    public PropertyAccess(Class<?> clazz, String propertyName) {
-        this.beanClass = clazz;
-        this.propertyName = propertyName;
-    }
-
-    public ElementType getElementType() {
-        return ElementType.METHOD;
-    }
-
-    public static Object getProperty(Object bean, String property) throws
-          InvocationTargetException, NoSuchMethodException, IllegalAccessException {
-        if (bean instanceof Map<?, ?>) {
-            return ((Map<?, ?>) bean).get(property);
-        } else { // supports DynaBean and standard Objects
-            return PropertyUtils.getSimpleProperty(bean, property);
-        }
-    }
-
-    public String toString() {
-        return "Property{" + beanClass.getName() + '.' + propertyName + '}';
-    }
-
-    public Type getJavaType() {
-        /*if(Map.class.isAssignableFrom(beanClass)) {
-            return beanClass. 
-        }*/
-        if (rememberField != null) {  // use cached field of previous access
-            return rememberField.getGenericType();
-        }
-        for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) {
-            if (each.getName().equals(propertyName) && each.getReadMethod() != null)
{
-                return each.getReadMethod().getGenericReturnType();
-            }
-        }
-        try { // try public field
-            return beanClass.getField(propertyName).getGenericType();
-        } catch (NoSuchFieldException ex2) {
-            // search for private/protected field up the hierarchy
-            Class<?> theClass = beanClass;
-            while (theClass != null) {
-                try {
-                    return theClass.getDeclaredField(propertyName).getGenericType();
-                } catch (NoSuchFieldException ex3) {
-                    // do nothing
-                }
-                theClass = theClass.getSuperclass();
-            }
+  private final Class<?> beanClass;
+  private final String propertyName;
+  private Field rememberField;
+
+  public PropertyAccess(Class<?> clazz, String propertyName) {
+    this.beanClass = clazz;
+    this.propertyName = propertyName;
+  }
+
+  public ElementType getElementType() {
+    return ElementType.METHOD;
+  }
+
+  private static Object getPublicProperty(Object bean, String property) throws
+      InvocationTargetException, NoSuchMethodException, IllegalAccessException {
+    if (bean instanceof Map<?, ?>) {
+      return ((Map<?, ?>) bean).get(property);
+    } else { // supports DynaBean and standard Objects
+      return PropertyUtils.getSimpleProperty(bean, property);
+    }
+  }
+
+  public static Object getProperty(Object bean, String propertyName) throws
+      InvocationTargetException, NoSuchMethodException, IllegalAccessException {
+    return new PropertyAccess(bean.getClass(), propertyName).get(bean);
+  }
+
+  public String toString() {
+    return "Property{" + beanClass.getName() + '.' + propertyName + '}';
+  }
+
+  public Type getJavaType() {
+    /*if(Map.class.isAssignableFrom(beanClass)) {
+        return beanClass.
+    }*/
+    if (rememberField != null) {  // use cached field of previous access
+      return rememberField.getGenericType();
+    }
+    for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) {
+      if (each.getName().equals(propertyName) && each.getReadMethod() != null) {
+        return each.getReadMethod().getGenericReturnType();
+      }
+    }
+    try { // try public field
+      return beanClass.getField(propertyName).getGenericType();
+    } catch (NoSuchFieldException ex2) {
+      // search for private/protected field up the hierarchy
+      Class<?> theClass = beanClass;
+      while (theClass != null) {
+        try {
+          return theClass.getDeclaredField(propertyName).getGenericType();
+        } catch (NoSuchFieldException ex3) {
+          // do nothing
         }
-        return Object.class; // unknown type: allow any type?? 
-    }
-
-    public String getPropertyName() {
-        return propertyName;
+        theClass = theClass.getSuperclass();
+      }
     }
+    return Object.class; // unknown type: allow any type??
+  }
 
-    public Object get(Object bean) {
+  public String getPropertyName() {
+    return propertyName;
+  }
+
+  public Object get(Object bean) {
+    try {
+      if (rememberField != null) {  // cache field of previous access
+        return rememberField.get(bean);
+      }
+
+      try {   // try public method
+        return getPublicProperty(bean, propertyName);
+      } catch (NoSuchMethodException ex) {
+        return getFieldValue(bean);
+      }
+    } catch (IllegalArgumentException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new IllegalArgumentException("cannot access " + propertyName, e);
+    }
+  }
+
+  private Object getFieldValue(Object bean) throws IllegalAccessException {
+    Object value;
+    try { // try public field
+      Field aField = bean.getClass().getField(propertyName);
+      value = aField.get(bean);
+      rememberField = aField;
+      return value;
+    } catch (NoSuchFieldException ex2) {
+      // search for private/protected field up the hierarchy
+      Class<?> theClass = bean.getClass();
+      while (theClass != null) {
         try {
-            if (rememberField != null) {  // cache field of previous access
-                return rememberField.get(bean);
-            }
-
-            try {   // try public method
-                return getProperty(bean, propertyName);
-            } catch (NoSuchMethodException ex) {
-                Object value;
-                try { // try public field
-                    Field aField = bean.getClass().getField(propertyName);
-                    value = aField.get(bean);
-                    rememberField = aField;
-                    return value;
-                } catch (NoSuchFieldException ex2) {
-                    // search for private/protected field up the hierarchy
-                    Class<?> theClass = bean.getClass();
-                    while (theClass != null) {
-                        try {
-                            Field aField = theClass
-                                  .getDeclaredField(propertyName);
-                            if (!aField.isAccessible()) {
-                                aField.setAccessible(true);
-                            }
-                            value = aField.get(bean);
-                            rememberField = aField;
-                            return value;
-                        } catch (NoSuchFieldException ex3) {
-                            // do nothing
-                        }
-                        theClass = theClass.getSuperclass();
-                    }
-                    throw new IllegalArgumentException(
-                          "cannot access field " + propertyName);
-                }
-            }
-        } catch (IllegalArgumentException e) {
-            throw e;
-        } catch (Exception e) {
-            throw new IllegalArgumentException("cannot access " + propertyName, e);
+          Field aField = theClass
+              .getDeclaredField(propertyName);
+          if (!aField.isAccessible()) {
+            aField.setAccessible(true);
+          }
+          value = aField.get(bean);
+          rememberField = aField;
+          return value;
+        } catch (NoSuchFieldException ex3) {
+          // do nothing
         }
-    }
-
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-
-        PropertyAccess that = (PropertyAccess) o;
-
-        return beanClass.equals(that.beanClass) && propertyName.equals(that.propertyName);
-    }
-
-    public int hashCode() {
-        int result;
-        result = beanClass.hashCode();
-        result = 31 * result + propertyName.hashCode();
-        return result;
-    }
+        theClass = theClass.getSuperclass();
+      }
+      throw new IllegalArgumentException(
+          "cannot access field " + propertyName);
+    }
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    PropertyAccess that = (PropertyAccess) o;
+
+    return beanClass.equals(that.beanClass) && propertyName.equals(that.propertyName);
+  }
+
+  public int hashCode() {
+    int result;
+    result = beanClass.hashCode();
+    result = 31 * result + propertyName.hashCode();
+    return result;
+  }
 }

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java?rev=936297&r1=936296&r2=936297&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
Wed Apr 21 13:32:47 2010
@@ -89,7 +89,7 @@ public class ClassValidator extends Bean
                      */
                     if (!result.isEmpty()) break;
                 }
-//                if (!result.isEmpty()) break; // ?? TODO RSt - clarify!
+                if (!result.isEmpty()) break; 
             }
             return result.getConstaintViolations();
         } catch (RuntimeException ex) {
@@ -190,7 +190,7 @@ public class ClassValidator extends Bean
                      */
                     if (!result.isEmpty()) break;
                 }
-//                if (!result.isEmpty()) break; // ?? TODO RSt - clarify!
+                if (!result.isEmpty()) break;
             }
             return result.getConstaintViolations();
         } catch (RuntimeException ex) {
@@ -267,7 +267,7 @@ public class ClassValidator extends Bean
                      */
                     if (!result.isEmpty()) break;
                 }
-//                if (!result.isEmpty()) break; // ?? TODO RSt - clarify!
+                if (!result.isEmpty()) break;
             }
             return result.getConstaintViolations();
         } catch (RuntimeException ex) {

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java?rev=936297&r1=936296&r2=936297&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
Wed Apr 21 13:32:47 2010
@@ -65,8 +65,7 @@ final class NestedMetaProperty {
                                 "unknown property '" + token + "' in " + getMetaBean().getId());
                     }
                     if (getValue() != null) {
-                        setValue(
-                              PropertyAccess.getProperty(getValue(), token));
+                        setValue(PropertyAccess.getProperty(getValue(), token));
                     }
                     setMetaProperty(mp);
                     resolveMetaBean();

Added: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/TckReproducerTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/TckReproducerTest.java?rev=936297&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/TckReproducerTest.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/TckReproducerTest.java
Wed Apr 21 13:32:47 2010
@@ -0,0 +1,71 @@
+package org.apache.bval.jsr303;
+
+import junit.framework.TestCase;
+import org.apache.bval.util.PropertyAccess;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.constraints.Pattern;
+import java.util.Set;
+
+/**
+ * Description: <br>
+ * User: roman.stumm<br>
+ * Date: 21.04.2010<br>
+ * Time: 14:21:45<br>
+ * viaboxx GmbH, 2010
+ */
+public class TckReproducerTest extends TestCase {
+
+  public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>>
violations,
+                                                         int expectedViolations) {
+    assertEquals(
+        "Wrong number of constraint violations. Expected: " + expectedViolations + " Actual:
" + violations.size(),
+        expectedViolations, violations.size());
+  }
+
+  private Validator getValidator() {
+    return ApacheValidatorFactory.getDefault().getValidator();
+  }
+
+  public void testPropertyAccessOnNonPublicClass()
+      throws Exception {
+    Validator validator = getValidator();
+    Car car = new Car("USd-298");
+    assertEquals(car.getLicensePlateNumber(), PropertyAccess.getProperty(car, "licensePlateNumber"));
+
+    Set<ConstraintViolation<Car>> violations = validator.validateProperty(
+        car, "licensePlateNumber", First.class, org.apache.bval.jsr303.example.Second.class
+    );
+    assertCorrectNumberOfViolations(violations, 1);
+
+    car.setLicensePlateNumber("USD-298");
+    violations = validator.validateProperty(
+        car, "licensePlateNumber", First.class, org.apache.bval.jsr303.example.Second.class
+    );
+    assertCorrectNumberOfViolations(violations, 0);
+  }
+
+  class Car {
+    @Pattern(regexp = "[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]", groups = {First.class, Second.class})
+    private String licensePlateNumber;
+
+    Car(String licensePlateNumber) {
+      this.licensePlateNumber = licensePlateNumber;
+    }
+
+    public String getLicensePlateNumber() {
+      return licensePlateNumber;
+    }
+
+    public void setLicensePlateNumber(String licensePlateNumber) {
+      this.licensePlateNumber = licensePlateNumber;
+    }
+  }
+
+  interface First {
+  }
+
+  interface Second {
+  }
+}

Modified: incubator/bval/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/pom.xml?rev=936297&r1=936296&r2=936297&view=diff
==============================================================================
--- incubator/bval/trunk/pom.xml (original)
+++ incubator/bval/trunk/pom.xml Wed Apr 21 13:32:47 2010
@@ -200,7 +200,7 @@
             <dependency>
                 <groupId>commons-beanutils</groupId>
                 <artifactId>commons-beanutils</artifactId>
-                <version>1.8.2</version>
+                <version>1.8.3</version>
                 <!-- 1.8.0 is also compatible -->
             </dependency>
             <dependency>



Mime
View raw message