Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 93082D1A7 for ; Wed, 17 Oct 2012 19:09:56 +0000 (UTC) Received: (qmail 89815 invoked by uid 500); 17 Oct 2012 19:09:56 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 89778 invoked by uid 500); 17 Oct 2012 19:09:56 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 89771 invoked by uid 99); 17 Oct 2012 19:09:56 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Oct 2012 19:09:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Oct 2012 19:09:52 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B6B4223888E4 for ; Wed, 17 Oct 2012 19:09:07 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@struts.apache.org From: lukaszlenart@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121017190907.B6B4223888E4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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; - /** * * Field Validator that checks if the short specified is within a certain range. @@ -24,33 +23,47 @@ package com.opensymphony.xwork2.validato * * *
    - *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • - *
  • min - the minimum value (if none is specified, it will not be checked)
  • - *
  • max - the maximum value (if none is specified, it will not be checked)
  • + *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • + *
  • min - the minimum value (if none is specified, it will not be checked)
  • + *
  • max - the maximum value (if none is specified, it will not be checked)
  • *
+ * + * The min / max value can be specified as an expression, but then you must also enable parsing it by specifying parse param + * as in the example below. + * WARNING! Do not use ${min} and ${max} as an expression as this will turn into infinitive loop! + * * - * - * + * *
  * 
- *              <validators>
- *           <!-- Plain Validator Syntax -->
- *           <validator type="short">
- *               <param name="fieldName">age</param>
- *               <param name="min">20</param>
- *               <param name="max">50</param>
- *               <message>Age needs to be between ${min} and ${max}</message>
- *           </validator>
- *           
- *           <!-- Field Validator Syntax -->
- *           <field name="age">
- *               <field-validator type="short">
- *                   <param name="min">20</param>
- *                   <param name="max">50</param>
- *                   <message>Age needs to be between ${min} and ${max}</message>
- *               </field-validator>
- *           </field>
- *      </validators>
+ *  <validators>
+ *      <!-- Plain Validator Syntax -->
+ *      <validator type="short">
+ *          <param name="fieldName">age</param>
+ *          <param name="min">20</param>
+ *          <param name="max">50</param>
+ *          <message>Age needs to be between ${min} and ${max}</message>
+ *      </validator>
+ *
+ *      <!-- Field Validator Syntax -->
+ *      <field name="age">
+ *          <field-validator type="short">
+ *              <param name="min">20</param>
+ *              <param name="max">50</param>
+ *              <message>Age needs to be between ${min} and ${max}</message>
+ *          </field-validator>
+ *      </field>
+ *
+ *      <!-- Field Validator Syntax with expression -->
+ *      <field name="age">
+ *          <field-validator type="short">
+ *              <param name="parse">true</param>
+ *              <param name="${minValue}">20</param> <!-- will be evaluated as: Short getMinValue() -->
+ *              <param name="${maxValue}">50</param> <!-- will be evaluated as: Short getMaxValue() -->
+ *              <message>Age needs to be between ${min} and ${max}</message>
+ *          </field-validator>
+ *      </field>
+ *  </validators>
  * 
  * 
* @@ -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 { + 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; } }