struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1424652 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/ test/java/com/opensymphony/xwork2/validator/validators/
Date Thu, 20 Dec 2012 19:47:02 GMT
Author: lukaszlenart
Date: Thu Dec 20 19:47:02 2012
New Revision: 1424652

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

Added:
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidatorTest.java
      - copied, changed from r1424643, struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.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/IntRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java?rev=1424652&r1=1424651&r2=1424652&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 Dec 20 19:47:02 2012
@@ -68,8 +68,6 @@ package com.opensymphony.xwork2.validato
  * <!-- END SNIPPET: examples -->
  * </pre>
  * 
- *
- *
  * @author Jason Carreira
  * @version $Date$ $Id$
  */

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.java?rev=1424652&r1=1424651&r2=1424652&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidator.java
Thu Dec 20 19:47:02 2012
@@ -15,7 +15,6 @@
  */
 package com.opensymphony.xwork2.validator.validators;
 
-
 /**
  * <!-- START SNIPPET: javadoc -->
  * Field Validator that checks if the long specified is within a certain range.
@@ -24,69 +23,90 @@ 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="long">
- *               &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="long"&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="long">
+ *          &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="long"&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="long"&gt;
+ *              &lt;param name="parse"&gt;true&lt;/param&gt;
+ *              &lt;param name="${minValue}"&gt;20&lt;/param&gt; &lt;!--
will be evaluated as: Long getMinValue() --&gt;
+ *              &lt;param name="${maxValue}"&gt;50&lt;/param&gt; &lt;!--
will be evaluated as: Long 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>
- * 
- * 
- * 
+ *
  * @version $Date$
  */
-public class LongRangeFieldValidator extends AbstractRangeValidator {
+public class LongRangeFieldValidator extends AbstractRangeValidator<Long> {
 
-    Long max = null;
-    Long min = null;
+    String max = null;
+    String min = null;
 
-
-    public void setMax(Long max) {
+    public void setMax(String max) {
         this.max = max;
     }
 
-    public Long getMax() {
-        return max;
+    public String getMax() {
+        return safeConditionalParse(max);
     }
 
     @Override
-    public Comparable getMaxComparatorValue() {
-        return max;
+    public Long getMaxComparatorValue() {
+        return parseLong(getMax());
     }
 
-    public void setMin(Long min) {
+    public void setMin(String min) {
         this.min = min;
     }
 
-    public Long getMin() {
-        return min;
+    public String getMin() {
+        return safeConditionalParse(min);
     }
 
     @Override
-    public Comparable getMinComparatorValue() {
-        return min;
+    public Long getMinComparatorValue() {
+        return parseLong(getMin());
+    }
+
+    private Long parseLong(String value) {
+        if (value != null) {
+            return Long.parseLong(value);
+        } else {
+            return null;
+        }
     }
+
 }

Copied: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidatorTest.java
(from r1424643, 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/LongRangeFieldValidatorTest.java?p2=struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/LongRangeFieldValidatorTest.java&p1=struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java&r1=1424643&r2=1424652&rev=1424652&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/LongRangeFieldValidatorTest.java
Thu Dec 20 19:47:02 2012
@@ -6,13 +6,13 @@ import com.opensymphony.xwork2.util.Valu
 import com.opensymphony.xwork2.validator.GenericValidatorContext;
 import com.opensymphony.xwork2.validator.ValidatorContext;
 
-public class IntRangeFieldValidatorTest extends XWorkTestCase {
+public class LongRangeFieldValidatorTest extends XWorkTestCase {
 
     public void testPassValidation() throws Exception {
         // given
         ValidationAction action = prepareAction(100);
         ValidatorContext context = new GenericValidatorContext(action);
-        IntRangeFieldValidator validator = prepareValidator(action, context);
+        LongRangeFieldValidator validator = prepareValidator(action, context);
 
         // when
         validator.validate(action);
@@ -25,49 +25,49 @@ public class IntRangeFieldValidatorTest 
         // given
         ValidationAction action = prepareAction(98);
         ValidatorContext context = new GenericValidatorContext(action);
-        IntRangeFieldValidator validator = prepareValidator(action, context);
+        LongRangeFieldValidator 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));
+        assertEquals("Max is 101, min is 99 but value is 98", context.getFieldErrors().get("longRange").get(0));
     }
 
     public void testMaxValidation() throws Exception {
         // given
         ValidationAction action = prepareAction(102);
         ValidatorContext context = new GenericValidatorContext(action);
-        IntRangeFieldValidator validator = prepareValidator(action, context);
+        LongRangeFieldValidator 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));
+        assertEquals("Max is 101, min is 99 but value is 102", context.getFieldErrors().get("longRange").get(0));
     }
 
-    private ValidationAction prepareAction(int intRange) {
+    private ValidationAction prepareAction(long longRange) {
         ValidationAction action = new ValidationAction();
-        action.setIntMaxValue(101);
-        action.setIntMinValue(99);
-        action.setIntRange(intRange);
+        action.setLongMaxValue(101L);
+        action.setLongMinValue(99L);
+        action.setLongRange(longRange);
         return action;
     }
 
-    private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext
context) {
-        IntRangeFieldValidator validator = new IntRangeFieldValidator();
-        validator.setMax("${intMaxValue}");
-        validator.setMin("${intMinValue}");
+    private LongRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext
context) {
+        LongRangeFieldValidator validator = new LongRangeFieldValidator();
+        validator.setMax("${longMaxValue}");
+        validator.setMin("${longMinValue}");
         ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
         valueStack.push(action);
         validator.setValueStack(valueStack);
         validator.setValidatorContext(context);
-        validator.setFieldName("intRange");
+        validator.setFieldName("longRange");
         validator.setParse(true);
-        validator.setDefaultMessage("Max is ${intMaxValue}, min is ${intMinValue} but value
is ${intRange}");
+        validator.setDefaultMessage("Max is ${longMaxValue}, min is ${longMinValue} but value
is ${longRange}");
         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=1424652&r1=1424651&r2=1424652&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
Thu Dec 20 19:47:02 2012
@@ -10,6 +10,10 @@ public class ValidationAction {
     private Short shortMinValue;
     private Short shortMaxValue;
 
+    private Long longRange;
+    private Long longMinValue;
+    private Long longMaxValue;
+
     public Integer getIntRange() {
         return intRange;
     }
@@ -57,4 +61,29 @@ public class ValidationAction {
     public void setShortMaxValue(Short shortMaxValue) {
         this.shortMaxValue = shortMaxValue;
     }
+
+    public Long getLongRange() {
+        return longRange;
+    }
+
+    public void setLongRange(Long longRange) {
+        this.longRange = longRange;
+    }
+
+    public Long getLongMinValue() {
+        return longMinValue;
+    }
+
+    public void setLongMinValue(Long longMinValue) {
+        this.longMinValue = longMinValue;
+    }
+
+    public Long getLongMaxValue() {
+        return longMaxValue;
+    }
+
+    public void setLongMaxValue(Long longMaxValue) {
+        this.longMaxValue = longMaxValue;
+    }
+
 }



Mime
View raw message