struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1457672 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/ main/java/com/opensymphony/xwork2/validator/annotations/ main/java/com/opensymphony/xwork2/validator/validators/ test/java/com/opensymphony/xwo...
Date Mon, 18 Mar 2013 08:39:53 GMT
Author: lukaszlenart
Date: Mon Mar 18 08:39:53 2013
New Revision: 1457672

URL: http://svn.apache.org/r1457672
Log:
WW-4004 Improves DoubleRangeFieldValidator annotation to match DoubleRangeFieldValidator class

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/DoubleRangeFieldValidator.java
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
Mon Mar 18 08:39:53 2013
@@ -706,6 +706,20 @@ public class AnnotationValidationConfigu
             params.put("maxExclusive", v.maxExclusive());
         }
 
+        if (StringUtils.isNotEmpty(v.minInclusiveExpression())) {
+            params.put("minInclusiveExpression", v.minInclusiveExpression());
+        }
+        if (StringUtils.isNotEmpty(v.maxInclusiveExpression())) {
+            params.put("maxInclusiveExpression", v.maxInclusiveExpression());
+        }
+
+        if (StringUtils.isNotEmpty(v.minExclusiveExpression())) {
+            params.put("minExclusiveExpression", v.minExclusiveExpression());
+        }
+        if (StringUtils.isNotEmpty(v.maxExclusiveExpression())) {
+            params.put("maxExclusiveExpression", v.maxExclusiveExpression());
+        }
+
         validatorFactory.lookupRegisteredValidatorType(validatorType);
         return new ValidatorConfig.Builder(validatorType)
             .addParams(params)
@@ -713,6 +727,7 @@ public class AnnotationValidationConfigu
             .shortCircuit(v.shortCircuit())
             .defaultMessage(v.message())
             .messageKey(v.key())
+            .messageParams(v.messageParams())
             .build();
     }
 

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/DoubleRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/DoubleRangeFieldValidator.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/DoubleRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/DoubleRangeFieldValidator.java
Mon Mar 18 08:39:53 2013
@@ -56,6 +56,12 @@ import java.lang.annotation.Target;
  * <td class='confluenceTd'>i18n key from language specific properties file.</td>
  * </tr>
  * <tr>
+ * <td class='confluenceTd'>messageParams</td>
+ * <td class='confluenceTd'>no</td>
+ * <td class='confluenceTd'>&nbsp;</td>
+ * <td class='confluenceTd'>Additional params to be used to customize message - will
be evaluated against the Value Stack</td>
+ * </tr>
+ * <tr>
  * <td class='confluenceTd'>fieldName</td>
  * <td class='confluenceTd'>no</td>
  * <td class='confluenceTd'>&nbsp;</td>
@@ -125,21 +131,41 @@ public @interface DoubleRangeFieldValida
     String minInclusive() default "";
 
     /**
+     * The inclusive minimum the number must be defined as an expression
+     */
+    String minInclusiveExpression() default "";
+
+    /**
      *  Double property. The inclusive minimum the number must be.
      */
     String maxInclusive() default "";
 
     /**
+     *  The inclusive minimum the number must be defined as an expression
+     */
+    String maxInclusiveExpression() default "";
+
+    /**
      *  Double property. The exclusive maximum number can be.
      */
     String minExclusive() default "";
 
     /**
+     *  The exclusive maximum number can be defined as an expression
+     */
+    String minExclusiveExpression() default "";
+
+    /**
      *  Double property. The exclusive maximum number can be.
      */
     String maxExclusive() default "";
 
     /**
+     * The exclusive maximum number can be defined as an expression
+     */
+    String maxExclusiveExpression() default "";
+
+    /**
      * The default error message for this validator.
      * NOTE: It is required to set a message, if you are not using the message key for 18n
lookup!
      */
@@ -151,6 +177,11 @@ public @interface DoubleRangeFieldValida
     String key() default "";
 
     /**
+     * Additional params to be used to customize message - will be evaluated against the
Value Stack
+     */
+    String[] messageParams() default {};
+
+    /**
      * The optional fieldName for SIMPLE validator types.
      */
     String fieldName() default "";

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
Mon Mar 18 08:39:53 2013
@@ -17,6 +17,7 @@
 package com.opensymphony.xwork2.validator.validators;
 
 import com.opensymphony.xwork2.validator.ValidationException;
+import org.apache.commons.lang3.StringUtils;
 
 /**
  * <!-- START SNIPPET: javadoc -->
@@ -93,6 +94,11 @@ public class DoubleRangeFieldValidator e
     private Double minExclusive = null;
     private Double maxExclusive = null;
 
+    private String minInclusiveExpression;
+    private String maxInclusiveExpression;
+    private String minExclusiveExpression;
+    private String maxExclusiveExpression;
+
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         Double value;
@@ -106,10 +112,15 @@ public class DoubleRangeFieldValidator e
             return;
         }
 
-        if ((maxInclusive != null && value.compareTo(maxInclusive) > 0) ||
-                (minInclusive != null && value.compareTo(minInclusive) < 0) ||
-                (maxExclusive != null && value.compareTo(maxExclusive) >= 0) ||
-                (minExclusive != null && value.compareTo(minExclusive) <= 0))
{
+        Double maxInclusiveToUse = getMaxInclusive();
+        Double minInclusiveToUse = getMinInclusive();
+        Double maxExclusiveToUse = getMaxExclusive();
+        Double minExclusiveToUse = getMinExclusive();
+
+        if ((maxInclusiveToUse != null && value.compareTo(maxInclusiveToUse) >
0) ||
+                (minInclusiveToUse != null && value.compareTo(minInclusiveToUse)
< 0) ||
+                (maxExclusiveToUse != null && value.compareTo(maxExclusiveToUse)
>= 0) ||
+                (minExclusiveToUse != null && value.compareTo(minExclusiveToUse)
<= 0)) {
             addFieldError(fieldName, object);
         }
     }
@@ -119,6 +130,11 @@ public class DoubleRangeFieldValidator e
     }
 
     public Double getMaxInclusive() {
+        if (maxInclusive != null) {
+            return maxInclusive;
+        } else if (StringUtils.isNotEmpty(maxInclusiveExpression)) {
+            return (Double) parse(maxInclusiveExpression, Double.class);
+        }
         return maxInclusive;
     }
 
@@ -127,39 +143,54 @@ public class DoubleRangeFieldValidator e
     }
 
     public Double getMinInclusive() {
-        return minInclusive;
-    }
-
-    public Double getMinExclusive() {
-        return minExclusive;
+        if (minInclusive != null) {
+            return minInclusive;
+        } else if (StringUtils.isNotEmpty(minInclusiveExpression)) {
+            return (Double) parse(minInclusiveExpression, Double.class);
+        }
+        return null;
     }
 
     public void setMinExclusive(Double minExclusive) {
         this.minExclusive = minExclusive;
     }
 
-    public Double getMaxExclusive() {
-        return maxExclusive;
+    public Double getMinExclusive() {
+        if (minExclusive != null) {
+            return minExclusive;
+        } else if (StringUtils.isNotEmpty(minExclusiveExpression)) {
+            return (Double) parse(minExclusiveExpression, Double.class);
+        }
+        return null;
     }
 
     public void setMaxExclusive(Double maxExclusive) {
         this.maxExclusive = maxExclusive;
     }
 
+    public Double getMaxExclusive() {
+        if (maxExclusive != null) {
+            return maxExclusive;
+        } else if (StringUtils.isNotEmpty(maxExclusiveExpression)) {
+            return (Double) parse(maxExclusiveExpression, Double.class);
+        }
+        return null;
+    }
+
     public void setMinInclusiveExpression(String minInclusiveExpression) {
-        this.minInclusive = (Double) parse(minInclusiveExpression, Double.class);
+        this.minInclusiveExpression = minInclusiveExpression;
     }
 
     public void setMaxInclusiveExpression(String maxInclusiveExpression) {
-        this.maxInclusive = (Double) parse(maxInclusiveExpression, Double.class);
+        this.maxInclusiveExpression = maxInclusiveExpression;
     }
 
     public void setMinExclusiveExpression(String minExclusiveExpression) {
-        this.minExclusive = (Double) parse(minExclusiveExpression, Double.class);
+        this.minExclusiveExpression = minExclusiveExpression;
     }
 
     public void setMaxExclusiveExpression(String maxExclusiveExpression) {
-        this.maxExclusive = (Double) parse(maxExclusiveExpression, Double.class);
+        this.maxExclusiveExpression = maxExclusiveExpression;
     }
 
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java
Mon Mar 18 08:39:53 2013
@@ -5,6 +5,7 @@ import com.opensymphony.xwork2.validator
 import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.CustomValidator;
 import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
+import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
 
@@ -29,6 +30,9 @@ public class AnnotationValidationAction 
     )
     @DateRangeFieldValidator(fieldName = "foo", key = "date.foo", max = "2012", min = "2011",
dateFormat = "yyyy",
             message = "Foo isn't in range!", shortCircuit = true, messageParams = {"one",
"two", "three"})
+    @DoubleRangeFieldValidator(minExclusive = "1.2", maxExclusive = "1.4", minInclusive =
"0", maxInclusive = "0.1",
+            fieldName = "foo", key = "double.key", message = "Foo is out of range!", shortCircuit
= true,
+            messageParams = {"one", "two", "three"})
     public String execute() {
         return SUCCESS;
     }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java
Mon Mar 18 08:39:53 2013
@@ -17,6 +17,7 @@ import com.opensymphony.xwork2.util.loca
 import com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator;
 import com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator;
 import com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator;
+import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator;
 import com.opensymphony.xwork2.validator.validators.RegexFieldValidator;
 
 import java.text.ParseException;
@@ -39,7 +40,7 @@ public class AnnotationValidationConfigu
         List<Validator> validators = manager.getValidators(AnnotationValidationAction.class,
null);
 
         // then
-        assertEquals(validators.size(), 5);
+        assertEquals(validators.size(), 6);
         for (Validator validator : validators) {
             validate(validator);
         }
@@ -56,7 +57,7 @@ public class AnnotationValidationConfigu
         ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
         valueStack.push(new AnnotationValidationExpAction());
 
-        assertEquals(validators.size(), 5);
+        assertEquals(validators.size(), 6);
         for (Validator validator : validators) {
             validator.setValueStack(valueStack);
             validate(validator);
@@ -74,9 +75,23 @@ public class AnnotationValidationConfigu
             validateMyValidator((MyValidator) validator);
         } else if (validator.getValidatorType().equals("date")) {
             validateDateRangeFieldValidator((DateRangeFieldValidator) validator);
+        } else if (validator.getValidatorType().equals("double")) {
+            validateDoubleRangeFieldValidator((DoubleRangeFieldValidator) validator);
         }
     }
 
+    private void validateDoubleRangeFieldValidator(DoubleRangeFieldValidator validator) {
+        assertEquals("foo", validator.getFieldName());
+        assertEquals("double.key", validator.getMessageKey());
+        assertEquals("Foo is out of range!", validator.getDefaultMessage());
+        assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
+        assertEquals(true, validator.isShortCircuit());
+        assertEquals(1.4, validator.getMaxExclusive());
+        assertEquals(1.2, validator.getMinExclusive());
+        assertEquals(0.1, validator.getMaxInclusive());
+        assertEquals(0.0, validator.getMinInclusive());
+    }
+
     private void validateDateRangeFieldValidator(DateRangeFieldValidator validator) throws
ParseException {
         assertEquals("foo", validator.getFieldName());
         assertEquals("Foo isn't in range!", validator.getDefaultMessage());

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java?rev=1457672&r1=1457671&r2=1457672&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java
Mon Mar 18 08:39:53 2013
@@ -5,6 +5,7 @@ import com.opensymphony.xwork2.validator
 import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.CustomValidator;
 import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
+import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
 import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
 
@@ -33,6 +34,10 @@ public class AnnotationValidationExpActi
     )
     @DateRangeFieldValidator(fieldName = "foo", key = "date.foo", maxExpression = "${dateMax}",
minExpression = "${dateMin}", dateFormat = "yyyy",
             message = "Foo isn't in range!", shortCircuit = true, messageParams = {"one",
"two", "three"})
+    @DoubleRangeFieldValidator(minExclusiveExpression = "${doubleMinExclusiveExpression}",
maxExclusiveExpression = "${doubleMaxExclusiveExpression}",
+            minInclusiveExpression = "${doubleMinInclusiveExpression}", maxInclusiveExpression
= "${doubleMaxInclusiveExpression}",
+            fieldName = "foo", key = "double.key", message = "Foo is out of range!", shortCircuit
= true,
+            messageParams = {"one", "two", "three"})
     public String execute() {
         return SUCCESS;
     }
@@ -57,4 +62,20 @@ public class AnnotationValidationExpActi
         return new SimpleDateFormat("yyyy").parse("2012");
     }
 
+    public Double getDoubleMinExclusiveExpression() {
+        return 1.2;
+    }
+
+    public Double getDoubleMaxExclusiveExpression() {
+        return 1.4;
+    }
+
+    public Double getDoubleMinInclusiveExpression() {
+        return 0.0;
+    }
+
+    public Double getDoubleMaxInclusiveExpression() {
+        return 0.1;
+    }
+
 }



Mime
View raw message