struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1399395 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/ test/java/com/opensymphony/xwork2/validator/validators/
Date Wed, 17 Oct 2012 19:09:07 GMT
Author: lukaszlenart
Date: Wed Oct 17 19:09:07 2012
New Revision: 1399395

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

Added:
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
    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/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java?rev=1399395&r1=1399394&r2=1399395&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
Wed Oct 17 19:09:07 2012
@@ -15,7 +15,6 @@
  */
 package com.opensymphony.xwork2.validator.validators;
 
-
 /**
  * <!-- START SNIPPET: javadoc -->
  * Field Validator that checks if the short specified is within a certain range.
@@ -24,33 +23,47 @@ package com.opensymphony.xwork2.validato
  * 
  * <!-- START SNIPPET: parameters -->
  * <ul>
- *              <li>fieldName - The field name this validator is validating. Required
if using Plain-Validator Syntax otherwise not required</li>
- *              <li>min - the minimum value (if none is specified, it will not be checked)
</li>
- *              <li>max - the maximum value (if none is specified, it will not be checked)
</li>
+ *      <li>fieldName - The field name this validator is validating. Required if using
Plain-Validator Syntax otherwise not required</li>
+ *      <li>min - the minimum value (if none is specified, it will not be checked)
</li>
+ *      <li>max - the maximum value (if none is specified, it will not be checked)
</li>
  * </ul>
+ *
+ * The min / max value can be specified as an expression, but then you must also enable parsing
it by specifying <strong>parse</strong> param
+ * as in the example below.
+ * WARNING! Do not use ${min} and ${max} as an expression as this will turn into infinitive
loop!
+ *
  * <!-- END SNIPPET: parameters -->
- * 
- * 
+ *
  * <pre>
  * <!-- START SNIPPET: examples -->
- *              &lt;validators>
- *           &lt;!-- Plain Validator Syntax --&gt;
- *           &lt;validator type="short">
- *               &lt;param name="fieldName"&gt;age&lt;/param&gt;
- *               &lt;param name="min"&gt;20&lt;/param&gt;
- *               &lt;param name="max"&gt;50&lt;/param&gt;
- *               &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
- *           &lt;/validator&gt;
- *           
- *           &lt;!-- Field Validator Syntax --&gt;
- *           &lt;field name="age"&gt;
- *               &lt;field-validator type="short"&gt;
- *                   &lt;param name="min"&gt;20&lt;/param&gt;
- *                   &lt;param name="max"&gt;50&lt;/param&gt;
- *                   &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
- *               &lt;/field-validator&gt;
- *           &lt;/field&gt;
- *      &lt;/validators&gt;
+ *  &lt;validators>
+ *      &lt;!-- Plain Validator Syntax --&gt;
+ *      &lt;validator type="short">
+ *          &lt;param name="fieldName"&gt;age&lt;/param&gt;
+ *          &lt;param name="min"&gt;20&lt;/param&gt;
+ *          &lt;param name="max"&gt;50&lt;/param&gt;
+ *          &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *      &lt;/validator&gt;
+ *
+ *      &lt;!-- Field Validator Syntax --&gt;
+ *      &lt;field name="age"&gt;
+ *          &lt;field-validator type="short"&gt;
+ *              &lt;param name="min"&gt;20&lt;/param&gt;
+ *              &lt;param name="max"&gt;50&lt;/param&gt;
+ *              &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *          &lt;/field-validator&gt;
+ *      &lt;/field&gt;
+ *
+ *      &lt;!-- Field Validator Syntax with expression --&gt;
+ *      &lt;field name="age"&gt;
+ *          &lt;field-validator type="short"&gt;
+ *              &lt;param name="parse"&gt;true&lt;/param&gt;
+ *              &lt;param name="${minValue}"&gt;20&lt;/param&gt; &lt;!--
will be evaluated as: Short getMinValue() --&gt;
+ *              &lt;param name="${maxValue}"&gt;50&lt;/param&gt; &lt;!--
will be evaluated as: Short getMaxValue() --&gt;
+ *              &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *          &lt;/field-validator&gt;
+ *      &lt;/field&gt;
+ *  &lt;/validators&gt;
  * <!-- END SNIPPET: examples -->
  * </pre>
  * 
@@ -58,35 +71,43 @@ package com.opensymphony.xwork2.validato
  * 
  * @version $Date$
  */
-public class ShortRangeFieldValidator extends AbstractRangeValidator {
-
-    Short max = null;
-    Short min = null;
+public class ShortRangeFieldValidator extends AbstractRangeValidator<Short> {
 
+    String max = null;
+    String min = null;
 
-    public void setMax(Short max) {
+    public void setMax(String max) {
         this.max = max;
     }
 
-    public Short getMax() {
-        return max;
+    public String getMax() {
+        return safeConditionalParse(max);
     }
 
     @Override
-    public Comparable getMaxComparatorValue() {
-        return max;
+    public Short getMaxComparatorValue() {
+        return parseShort(getMax());
     }
 
-    public void setMin(Short min) {
+    public void setMin(String min) {
         this.min = min;
     }
 
-    public Short getMin() {
-        return min;
+    public String getMin() {
+        return safeConditionalParse(min);
     }
 
     @Override
-    public Comparable getMinComparatorValue() {
-        return min;
+    public Short getMinComparatorValue() {
+        return parseShort(getMin());
     }
+
+    private Short parseShort(String value) {
+        if (value != null) {
+            return Short.parseShort(value);
+        } else {
+            return null;
+        }
+    }
+
 }

Modified: 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=1399395&r1=1399394&r2=1399395&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
Wed Oct 17 19:09:07 2012
@@ -51,23 +51,23 @@ public class IntRangeFieldValidatorTest 
 
     private ValidationAction prepareAction(int intRange) {
         ValidationAction action = new ValidationAction();
-        action.setMaxValue(101);
-        action.setMinValue(99);
+        action.setIntMaxValue(101);
+        action.setIntMinValue(99);
         action.setIntRange(intRange);
         return action;
     }
 
     private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext
context) {
         IntRangeFieldValidator validator = new IntRangeFieldValidator();
-        validator.setMax("${maxValue}");
-        validator.setMin("${minValue}");
+        validator.setMax("${intMaxValue}");
+        validator.setMin("${intMinValue}");
         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}");
+        validator.setDefaultMessage("Max is ${intMaxValue}, min is ${intMinValue} but value
is ${intRange}");
         return validator;
     }
 

Added: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java?rev=1399395&view=auto
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
(added)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
Wed Oct 17 19:09:07 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 ShortRangeFieldValidatorTest extends XWorkTestCase {
+
+    public void testPassValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction((short) 5);
+        ValidatorContext context = new GenericValidatorContext(action);
+        ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 0);
+    }
+
+    public void testMinValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction((short) 1);
+        ValidatorContext context = new GenericValidatorContext(action);
+        ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 1);
+        assertEquals("Max is 10, min is 2 but value is 1", context.getFieldErrors().get("shortRange").get(0));
+    }
+
+    public void testMaxValidation() throws Exception {
+        // given
+        ValidationAction action = prepareAction((short) 11);
+        ValidatorContext context = new GenericValidatorContext(action);
+        ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+        // when
+        validator.validate(action);
+
+        // then
+        assertTrue(context.getFieldErrors().size() == 1);
+        assertEquals("Max is 10, min is 2 but value is 11", context.getFieldErrors().get("shortRange").get(0));
+    }
+
+    private ValidationAction prepareAction(short range) {
+        ValidationAction action = new ValidationAction();
+        action.setShortMaxValue((short) 10);
+        action.setShortMinValue((short) 2);
+        action.setShortRange(range);
+        return action;
+    }
+
+    private ShortRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext
context) {
+        ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
+        validator.setMax("${shortMaxValue}");
+        validator.setMin("${shortMinValue}");
+        ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
+        valueStack.push(action);
+        validator.setValueStack(valueStack);
+        validator.setValidatorContext(context);
+        validator.setFieldName("shortRange");
+        validator.setParse(true);
+        validator.setDefaultMessage("Max is ${shortMaxValue}, min is ${shortMinValue} but
value is ${shortRange}");
+        return validator;
+    }
+
+}

Modified: 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=1399395&r1=1399394&r2=1399395&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
Wed Oct 17 19:09:07 2012
@@ -3,8 +3,12 @@ package com.opensymphony.xwork2.validato
 public class ValidationAction {
 
     private Integer intRange;
-    private Integer minValue;
-    private Integer maxValue;
+    private Integer intMinValue;
+    private Integer intMaxValue;
+
+    private Short shortRange;
+    private Short shortMinValue;
+    private Short shortMaxValue;
 
     public Integer getIntRange() {
         return intRange;
@@ -14,19 +18,43 @@ public class ValidationAction {
         this.intRange = intRange;
     }
 
-    public Integer getMinValue() {
-        return minValue;
+    public Integer getIntMinValue() {
+        return intMinValue;
+    }
+
+    public void setIntMinValue(Integer intMinValue) {
+        this.intMinValue = intMinValue;
+    }
+
+    public Integer getIntMaxValue() {
+        return intMaxValue;
+    }
+
+    public void setIntMaxValue(Integer intMaxValue) {
+        this.intMaxValue = intMaxValue;
+    }
+
+    public Short getShortRange() {
+        return shortRange;
+    }
+
+    public void setShortRange(Short shortRange) {
+        this.shortRange = shortRange;
+    }
+
+    public Short getShortMinValue() {
+        return shortMinValue;
     }
 
-    public void setMinValue(Integer minValue) {
-        this.minValue = minValue;
+    public void setShortMinValue(Short shortMinValue) {
+        this.shortMinValue = shortMinValue;
     }
 
-    public Integer getMaxValue() {
-        return maxValue;
+    public Short getShortMaxValue() {
+        return shortMaxValue;
     }
 
-    public void setMaxValue(Integer maxValue) {
-        this.maxValue = maxValue;
+    public void setShortMaxValue(Short shortMaxValue) {
+        this.shortMaxValue = shortMaxValue;
     }
 }



Mime
View raw message