struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1397242 - in /struts/struts2/trunk: core/src/main/resources/template/xhtml/ xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/
Date Thu, 11 Oct 2012 19:20:22 GMT
Author: lukaszlenart
Date: Thu Oct 11 19:20:22 2012
New Revision: 1397242

URL: http://svn.apache.org/viewvc?rev=1397242&view=rev
Log:
WW-3888 adds support for expression in specifying min and max constraints

Added:
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
Modified:
    struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/AbstractRangeValidator.java
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java

Modified: struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl?rev=1397242&r1=1397241&r2=1397242&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl (original)
+++ struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl Thu
Oct 11 19:20:22 2012
@@ -99,9 +99,9 @@ END SNIPPET: supported-validators
             <#elseif validator.validatorType = "int">
             if (continueValidation && field.value != null) {
                 if (<#if validator.min??>parseInt(field.value) <
-                     ${validator.min?c}<#else>false</#if> ||
+                     ${validator.min}<#else>false</#if> ||
                         <#if validator.max??>parseInt(field.value) >
-                           ${validator.max?c}<#else>false</#if>) {
+                           ${validator.max}<#else>false</#if>) {
                     addError(field, error);
                     errors = true;
                     <#if validator.shortCircuit>continueValidation = false;</#if>

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/AbstractRangeValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/AbstractRangeValidator.java?rev=1397242&r1=1397241&r2=1397242&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/AbstractRangeValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/AbstractRangeValidator.java
Thu Oct 11 19:20:22 2012
@@ -24,11 +24,11 @@ import com.opensymphony.xwork2.validator
  * @author Jason Carreira
  * @author Cameron Braid
  */
-public abstract class AbstractRangeValidator extends FieldValidatorSupport {
+public abstract class AbstractRangeValidator<T extends Comparable> extends FieldValidatorSupport
{
 
     public void validate(Object object) throws ValidationException {
         Object obj = getFieldValue(getFieldName(), object);
-        Comparable value = (Comparable) obj;
+        Comparable<T> value = (Comparable<T>) obj;
 
         // if there is no value - don't do comparison
         // if a value is required, a required validator should be added to the field
@@ -37,17 +37,28 @@ public abstract class AbstractRangeValid
         }
 
         // only check for a minimum value if the min parameter is set
-        if ((getMinComparatorValue() != null) && (value.compareTo(getMinComparatorValue())
< 0)) {
+        T minComparatorValue = getMinComparatorValue();
+        if ((minComparatorValue != null) && (value.compareTo(minComparatorValue)
< 0)) {
             addFieldError(getFieldName(), object);
         }
 
         // only check for a maximum value if the max parameter is set
-        if ((getMaxComparatorValue() != null) && (value.compareTo(getMaxComparatorValue())
> 0)) {
+        T maxComparatorValue = getMaxComparatorValue();
+        if ((maxComparatorValue != null) && (value.compareTo(maxComparatorValue)
> 0)) {
             addFieldError(getFieldName(), object);
         }
     }
 
-    protected abstract Comparable getMaxComparatorValue();
+    protected abstract T getMaxComparatorValue();
 
-    protected abstract Comparable getMinComparatorValue();
+    protected abstract T getMinComparatorValue();
+
+    protected String safeConditionalParse(String expression) {
+        Object value = conditionalParse(expression);
+        if (value != null) {
+            return value.toString();
+        } else {
+            return null;
+        }
+    }
 }

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java?rev=1397242&r1=1397241&r2=1397242&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
Thu Oct 11 19:20:22 2012
@@ -59,35 +59,43 @@ package com.opensymphony.xwork2.validato
  * @author Jason Carreira
  * @version $Date$ $Id$
  */
-public class IntRangeFieldValidator extends AbstractRangeValidator {
+public class IntRangeFieldValidator extends AbstractRangeValidator<Integer> {
 
-    Integer max = null;
-    Integer min = null;
+    String max = null;
+    String min = null;
 
-
-    public void setMax(Integer max) {
+    public void setMax(String max) {
         this.max = max;
     }
 
-    public Integer getMax() {
-        return max;
+    public String getMax() {
+        return safeConditionalParse(max);
     }
 
     @Override
-    public Comparable getMaxComparatorValue() {
-        return max;
+    public Integer getMaxComparatorValue() {
+        return parseInt(getMax());
     }
 
-    public void setMin(Integer min) {
+    public void setMin(String min) {
         this.min = min;
     }
 
-    public Integer getMin() {
-        return min;
+    public String getMin() {
+        return safeConditionalParse(min);
     }
 
     @Override
-    public Comparable getMinComparatorValue() {
-        return min;
+    public Integer getMinComparatorValue() {
+        return parseInt(getMin());
+    }
+
+    private Integer parseInt(String value) {
+        if (value != null) {
+            return Integer.parseInt(value);
+        } else {
+            return null;
+        }
     }
+
 }

Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java?rev=1397242&view=auto
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
(added)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
Thu Oct 11 19:20:22 2012
@@ -0,0 +1,74 @@
+package com.opensymphony.xwork2.validator.validators;
+
+import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.ValueStackFactory;
+import com.opensymphony.xwork2.validator.GenericValidatorContext;
+import com.opensymphony.xwork2.validator.ValidatorContext;
+
+public class IntRangeFieldValidatorTest extends XWorkTestCase {
+
+    public void testPassValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction(100);
+        ValidatorContext context = new GenericValidatorContext(action);
+        IntRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 0);
+    }
+
+    public void testMinValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction(98);
+        ValidatorContext context = new GenericValidatorContext(action);
+        IntRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 1);
+        assertEquals("Max is 101, min is 99 but value is 98", context.getFieldErrors().get("intRange").get(0));
+    }
+
+    public void testMaxValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction(102);
+        ValidatorContext context = new GenericValidatorContext(action);
+        IntRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 1);
+        assertEquals("Max is 101, min is 99 but value is 102", context.getFieldErrors().get("intRange").get(0));
+    }
+
+    private ValidationAction prepareAction(int intRange) {
+        ValidationAction action = new ValidationAction();
+        action.setMaxValue(101);
+        action.setMinValue(99);
+        action.setIntRange(intRange);
+        return action;
+    }
+
+    private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext
context) {
+        IntRangeFieldValidator validator = new IntRangeFieldValidator();
+        validator.setMax("${maxValue}");
+        validator.setMin("${minValue}");
+        ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
+        valueStack.push(action);
+        validator.setValueStack(valueStack);
+        validator.setValidatorContext(context);
+        validator.setFieldName("intRange");
+        validator.setParse(true);
+        validator.setDefaultMessage("Max is ${maxValue}, min is ${minValue} but value is
${intRange}");
+        return validator;
+    }
+
+}

Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java?rev=1397242&view=auto
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
(added)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
Thu Oct 11 19:20:22 2012
@@ -0,0 +1,32 @@
+package com.opensymphony.xwork2.validator.validators;
+
+public class ValidationAction {
+
+    private Integer intRange;
+    private Integer minValue;
+    private Integer maxValue;
+
+    public Integer getIntRange() {
+        return intRange;
+    }
+
+    public void setIntRange(Integer intRange) {
+        this.intRange = intRange;
+    }
+
+    public Integer getMinValue() {
+        return minValue;
+    }
+
+    public void setMinValue(Integer minValue) {
+        this.minValue = minValue;
+    }
+
+    public Integer getMaxValue() {
+        return maxValue;
+    }
+
+    public void setMaxValue(Integer maxValue) {
+        this.maxValue = maxValue;
+    }
+}



Mime
View raw message