mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-2923 refactors validators to use dedicated params to define expressions
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1431115 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -98,10 +98,10 @@ END SNIPPET: supported-validators
|
||||
}
|
||||
<#elseif validator.validatorType = "int">
|
||||
if (continueValidation && field.value != null) {
|
||||
if (<#if validator.minComparatorValue??>parseInt(field.value) <
|
||||
${validator.minComparatorValue?c}<#else>false</#if> ||
|
||||
<#if validator.maxComparatorValue??>parseInt(field.value) >
|
||||
${validator.maxComparatorValue?c}<#else>false</#if>) {
|
||||
if (<#if validator.min??>parseInt(field.value) <
|
||||
${validator.min?c}<#else>false</#if> ||
|
||||
<#if validator.max??>parseInt(field.value) >
|
||||
${validator.max?c}<#else>false</#if>) {
|
||||
addError(field, error);
|
||||
errors = true;
|
||||
<#if validator.shortCircuit>continueValidation = false;</#if>
|
||||
@@ -110,10 +110,10 @@ END SNIPPET: supported-validators
|
||||
<#elseif validator.validatorType = "double">
|
||||
if (continueValidation && field.value != null) {
|
||||
var value = parseFloat(field.value);
|
||||
if (<#if validator.minInclusive??>value < ${validator.minInclusive}<#else>false</#if> ||
|
||||
<#if validator.maxInclusive??>value > ${validator.maxInclusive}<#else>false</#if> ||
|
||||
<#if validator.minExclusive??>value <= ${validator.minExclusive}<#else>false</#if> ||
|
||||
<#if validator.maxExclusive??>value >= ${validator.maxExclusive}<#else>false</#if>) {
|
||||
if (<#if validator.minInclusive??>value < ${validator.minInclusive?c}<#else>false</#if> ||
|
||||
<#if validator.maxInclusive??>value > ${validator.maxInclusive?c}<#else>false</#if> ||
|
||||
<#if validator.minExclusive??>value <= ${validator.minExclusive?c}<#else>false</#if> ||
|
||||
<#if validator.maxExclusive??>value >= ${validator.maxExclusive?c}<#else>false</#if>) {
|
||||
addError(field, error);
|
||||
errors = true;
|
||||
<#if validator.shortCircuit>continueValidation = false;</#if>
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
<validators>
|
||||
<field name="myUpDownSelectTag">
|
||||
<field-validator type="double">
|
||||
<param name="minInclusive">6000.10</param>
|
||||
<param name="maxInclusive">10000.10</param>
|
||||
<param name="minInclusive">6000.1</param>
|
||||
<param name="maxInclusive">10000.1</param>
|
||||
<message>bar must be between ${minInclusive} and ${maxInclusive}.</message>
|
||||
</field-validator>
|
||||
</field>
|
||||
|
||||
@@ -42,10 +42,10 @@
|
||||
//validatorname:double
|
||||
if(form.elements['myUpDownSelectTag']){
|
||||
field=form.elements['myUpDownSelectTag'];
|
||||
var error="bar must be between 6000.10 and 10000.10.";
|
||||
var error="bar must be between 6000.1 and 10000.1.";
|
||||
if(continueValidation && field.value!=null){
|
||||
var value = parseFloat(field.value);
|
||||
if(value<6000.10||value>10000.10||false||false){
|
||||
if(value<6000.1||value>10000.1||false||false){
|
||||
addError(field,error);
|
||||
errors=true;
|
||||
}
|
||||
|
||||
+14
-29
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import com.opensymphony.xwork2.validator.ValidationException;
|
||||
|
||||
|
||||
@@ -26,14 +28,13 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
*/
|
||||
public abstract class AbstractRangeValidator<T extends Comparable> extends FieldValidatorSupport {
|
||||
|
||||
protected final Class<T> type;
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractRangeValidator.class);
|
||||
|
||||
private final Class<T> type;
|
||||
|
||||
private T min;
|
||||
private T max;
|
||||
|
||||
private String minExpression;
|
||||
private String maxExpression;
|
||||
|
||||
protected AbstractRangeValidator(Class<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
@@ -49,25 +50,18 @@ public abstract class AbstractRangeValidator<T extends Comparable> extends Field
|
||||
}
|
||||
|
||||
// only check for a minimum value if the min parameter is set
|
||||
T minComparatorValue = getMinComparatorValue();
|
||||
T minComparatorValue = getMin();
|
||||
if ((minComparatorValue != null) && (value.compareTo(minComparatorValue) < 0)) {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
|
||||
// only check for a maximum value if the max parameter is set
|
||||
T maxComparatorValue = getMaxComparatorValue();
|
||||
T maxComparatorValue = getMax();
|
||||
if ((maxComparatorValue != null) && (value.compareTo(maxComparatorValue) > 0)) {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
}
|
||||
|
||||
public T getMinComparatorValue() {
|
||||
if (parse) {
|
||||
return (T) parse(getMinExpression(), type);
|
||||
}
|
||||
return getMin();
|
||||
}
|
||||
|
||||
public void setMin(T min) {
|
||||
this.min = min;
|
||||
}
|
||||
@@ -76,12 +70,11 @@ public abstract class AbstractRangeValidator<T extends Comparable> extends Field
|
||||
return min;
|
||||
}
|
||||
|
||||
public String getMinExpression() {
|
||||
return minExpression;
|
||||
}
|
||||
|
||||
public void setMinExpression(String minExpression) {
|
||||
this.minExpression = minExpression;
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("${minExpression} was defined as [#0]", minExpression);
|
||||
}
|
||||
this.min = (T) parse(minExpression, type);
|
||||
}
|
||||
|
||||
public void setMax(T max) {
|
||||
@@ -92,19 +85,11 @@ public abstract class AbstractRangeValidator<T extends Comparable> extends Field
|
||||
return max;
|
||||
}
|
||||
|
||||
public String getMaxExpression() {
|
||||
return maxExpression;
|
||||
}
|
||||
|
||||
public void setMaxExpression(String maxExpression) {
|
||||
this.maxExpression = maxExpression;
|
||||
}
|
||||
|
||||
public T getMaxComparatorValue() {
|
||||
if (parse) {
|
||||
return (T) parse(getMaxExpression(), type);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("${maxExpression} was defined as [#0]", maxExpression);
|
||||
}
|
||||
return getMax();
|
||||
this.max = (T) parse(maxExpression, type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -73,10 +73,9 @@ import java.util.Date;
|
||||
* <!-- Field Validator Syntax with expression -->
|
||||
* <field name="birthday">
|
||||
* <field-validator type="date">
|
||||
* <param name="parse">true</param>
|
||||
* <param name="minExpression">${minValue}</param> <!-- will be evaluated as: Date getMinValue() -->
|
||||
* <param name="maxExpression">${maxValue}</param> <!-- will be evaluated as: Date getMaxValue() -->
|
||||
* <message>Age needs to be between ${minExpression} and ${maxExpression}</message>
|
||||
* <message>Age needs to be between ${min} and ${max}</message>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
|
||||
+43
-55
@@ -31,14 +31,21 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
* <li>maxInclusive - the maximum inclusive value in FloatValue format specified by Java language (if none is specified, it will not be checked) </li>
|
||||
* <li>minExclusive - the minimum exclusive value in FloatValue format specified by Java language (if none is specified, it will not be checked) </li>
|
||||
* <li>maxExclusive - the maximum exclusive value in FloatValue format specified by Java language (if none is specified, it will not be checked) </li>
|
||||
* <li>minInclusiveExpression - the minimum inclusive value specified as a OGNL expression (if none is specified, it will not be checked) </li>
|
||||
* <li>maxInclusiveExpression - the maximum inclusive value specified as a OGNL expression (if none is specified, it will not be checked) </li>
|
||||
* <li>minExclusiveExpression - the minimum exclusive value specified as a OGNL expression (if none is specified, it will not be checked) </li>
|
||||
* <li>maxExclusiveExpression - the maximum exclusive value specified as a OGNL expression (if none is specified, it will not be checked) </li>
|
||||
* </ul>
|
||||
*
|
||||
* You can specify minInclusive, maxInclusive, minExclusive and maxExclusive as a OGNL expression, see example below.
|
||||
* You can specify either minInclusive, maxInclusive, minExclusive and maxExclusive or minInclusiveExpression, maxInclusiveExpression,
|
||||
* minExclusiveExpression and maxExclusiveExpression as a OGNL expression, see example below. You can always try to mix params
|
||||
* but be aware that such behaviour was not tested.
|
||||
* <!-- END SNIPPET: parameters -->
|
||||
*
|
||||
*
|
||||
* <!-- START SNIPPET: parameters-warning -->
|
||||
* Do not use ${minInclusive}, ${maxInclusive}, ${minExclusive} and ${maxExclusive} as an expression as this will turn into infinitive loop!
|
||||
* Do not use ${minInclusiveExpression}, ${maxInclusiveExpression}, ${minExclusiveExpressionExpression} and ${maxExclusive}
|
||||
* as an expression as this will turn into infinitive loop!
|
||||
* <!-- END SNIPPET: parameters-warning -->
|
||||
*
|
||||
*
|
||||
@@ -65,9 +72,8 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
* <!-- Field Validator Syntax with expression -->
|
||||
* <field name="percentage">
|
||||
* <field-validator type="double">
|
||||
* <param name="parse">true</param>
|
||||
* <param name="minExclusive">${minExclusiveValue}</param> <!-- will be evaluated as: Double getMinExclusiveValue() -->
|
||||
* <param name="maxExclusive">${maxExclusive}</param> <!-- will be evaluated as: Double getMaxExclusive() -->
|
||||
* <param name="minExclusiveExpression">${minExclusiveValue}</param> <!-- will be evaluated as: Double getMinExclusiveValue() -->
|
||||
* <param name="maxExclusiveExpression">${maxExclusiveValue}</param> <!-- will be evaluated as: Double getMaxExclusiveValue() -->
|
||||
* <message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
@@ -82,15 +88,10 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
*/
|
||||
public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
|
||||
String maxInclusive = null;
|
||||
String minInclusive = null;
|
||||
String minExclusive = null;
|
||||
String maxExclusive = null;
|
||||
|
||||
Double maxInclusiveValue = null;
|
||||
Double minInclusiveValue = null;
|
||||
Double minExclusiveValue = null;
|
||||
Double maxExclusiveValue = null;
|
||||
private Double maxInclusive = null;
|
||||
private Double minInclusive = null;
|
||||
private Double minExclusive = null;
|
||||
private Double maxExclusive = null;
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
String fieldName = getFieldName();
|
||||
@@ -105,73 +106,60 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
return;
|
||||
}
|
||||
|
||||
parseParameterValues();
|
||||
if ((maxInclusiveValue != null && value.compareTo(maxInclusiveValue) > 0) ||
|
||||
(minInclusiveValue != null && value.compareTo(minInclusiveValue) < 0) ||
|
||||
(maxExclusiveValue != null && value.compareTo(maxExclusiveValue) >= 0) ||
|
||||
(minExclusiveValue != null && value.compareTo(minExclusiveValue) <= 0)) {
|
||||
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)) {
|
||||
addFieldError(fieldName, object);
|
||||
}
|
||||
}
|
||||
|
||||
protected void parseParameterValues() {
|
||||
this.minInclusiveValue = parseValue(minInclusive);
|
||||
this.maxInclusiveValue = parseValue(maxInclusive);
|
||||
this.minExclusiveValue = parseValue(minExclusive);
|
||||
this.maxExclusiveValue = parseValue(maxExclusive);
|
||||
}
|
||||
|
||||
protected Double parseValue(String value) {
|
||||
if (parse) {
|
||||
return (Double) parse(value, Double.class);
|
||||
} else {
|
||||
return parseDouble(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected Double parseDouble (String value) {
|
||||
if (value != null) {
|
||||
try {
|
||||
return Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("DoubleRangeFieldValidator - [parseDouble]: Unable to parse given double parameter " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMaxInclusive(String maxInclusive) {
|
||||
public void setMaxInclusive(Double maxInclusive) {
|
||||
this.maxInclusive = maxInclusive;
|
||||
}
|
||||
|
||||
public String getMaxInclusive() {
|
||||
public Double getMaxInclusive() {
|
||||
return maxInclusive;
|
||||
}
|
||||
|
||||
public void setMinInclusive(String minInclusive) {
|
||||
public void setMinInclusive(Double minInclusive) {
|
||||
this.minInclusive = minInclusive;
|
||||
}
|
||||
|
||||
public String getMinInclusive() {
|
||||
public Double getMinInclusive() {
|
||||
return minInclusive;
|
||||
}
|
||||
|
||||
public String getMinExclusive() {
|
||||
public Double getMinExclusive() {
|
||||
return minExclusive;
|
||||
}
|
||||
|
||||
public void setMinExclusive(String minExclusive) {
|
||||
public void setMinExclusive(Double minExclusive) {
|
||||
this.minExclusive = minExclusive;
|
||||
}
|
||||
|
||||
public String getMaxExclusive() {
|
||||
public Double getMaxExclusive() {
|
||||
return maxExclusive;
|
||||
}
|
||||
|
||||
public void setMaxExclusive(String maxExclusive) {
|
||||
public void setMaxExclusive(Double maxExclusive) {
|
||||
this.maxExclusive = maxExclusive;
|
||||
}
|
||||
|
||||
public void setMinInclusiveExpression(String minInclusiveExpression) {
|
||||
this.minInclusive = (Double) parse(minInclusiveExpression, Double.class);
|
||||
}
|
||||
|
||||
public void setMaxInclusiveExpression(String maxInclusiveExpression) {
|
||||
this.maxInclusive = (Double) parse(maxInclusiveExpression, Double.class);
|
||||
}
|
||||
|
||||
public void setMinExclusiveExpression(String minExclusiveExpression) {
|
||||
this.minExclusive = (Double) parse(minExclusiveExpression, Double.class);
|
||||
}
|
||||
|
||||
public void setMaxExclusiveExpression(String maxExclusiveExpression) {
|
||||
this.maxExclusive = (Double) parse(maxExclusiveExpression, Double.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-2
@@ -62,10 +62,9 @@ package com.opensymphony.xwork2.validator.validators;
|
||||
* <!-- Field Validator Syntax with expression -->
|
||||
* <field name="age">
|
||||
* <field-validator type="int">
|
||||
* <param name="parse">true</param>
|
||||
* <param name="minExpression">${minValue}</param> <!-- will be evaluated as: Integer getMinValue() -->
|
||||
* <param name="maxExpression">${maxValue}</param> <!-- will be evaluated as: Integer getMaxValue() -->
|
||||
* <message>Age needs to be between ${minExpression} and ${maxExpression}</message>
|
||||
* <message>Age needs to be between ${min} and ${max}</message>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
|
||||
+1
-2
@@ -62,10 +62,9 @@ package com.opensymphony.xwork2.validator.validators;
|
||||
* <!-- Field Validator Syntax with expression -->
|
||||
* <field name="age">
|
||||
* <field-validator type="long">
|
||||
* <param name="parse">true</param>
|
||||
* <param name="minExpression">${minValue}</param> <!-- will be evaluated as: Long getMinValue() -->
|
||||
* <param name="maxExpression">${maxValue}</param> <!-- will be evaluated as: Long getMaxValue() -->
|
||||
* <message>Age needs to be between ${minExpression} and ${maxExpression}</message>
|
||||
* <message>Age needs to be between ${min} and ${max}</message>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
|
||||
+15
-16
@@ -25,8 +25,7 @@ import java.util.regex.Pattern;
|
||||
* <!-- START SNIPPET: javadoc -->
|
||||
* Validates a string field using a regular expression.
|
||||
* <!-- END SNIPPET: javadoc -->
|
||||
* <p/>
|
||||
*
|
||||
*
|
||||
*
|
||||
* <!-- START SNIPPET: parameters -->
|
||||
* <ul>
|
||||
@@ -40,20 +39,20 @@ import java.util.regex.Pattern;
|
||||
*
|
||||
* <pre>
|
||||
* <!-- START SNIPPET: example -->
|
||||
* <validators>
|
||||
* <!-- Plain Validator Syntax -->
|
||||
* <validator type="regex">
|
||||
* <param name="fieldName">myStrangePostcode</param>
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]<>/param>
|
||||
* </validator>
|
||||
*
|
||||
* <!-- Field Validator Syntax -->
|
||||
* <field name="myStrangePostcode">
|
||||
* <field-validator type="regex">
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]></param>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
* <validators>
|
||||
* <!-- Plain Validator Syntax -->
|
||||
* <validator type="regex">
|
||||
* <param name="fieldName">myStrangePostcode</param>
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]<>/param>
|
||||
* </validator>
|
||||
*
|
||||
* <!-- Field Validator Syntax -->
|
||||
* <field name="myStrangePostcode">
|
||||
* <field-validator type="regex">
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]></param>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
* <!-- END SNIPPET: example -->
|
||||
* </pre>
|
||||
*
|
||||
|
||||
+1
-2
@@ -62,10 +62,9 @@ package com.opensymphony.xwork2.validator.validators;
|
||||
* <!-- Field Validator Syntax with expression -->
|
||||
* <field name="age">
|
||||
* <field-validator type="short">
|
||||
* <param name="parse">true</param>
|
||||
* <param name="minExpression">${minValue}</param> <!-- will be evaluated as: Short getMinValue() -->
|
||||
* <param name="maxExpression">${maxValue}</param> <!-- will be evaluated as: Short getMaxValue() -->
|
||||
* <message>Age needs to be between ${minExpression} and ${maxExpression}</message>
|
||||
* <message>Age needs to be between ${min} and ${max}</message>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
|
||||
+7
@@ -59,6 +59,7 @@ public class MockConfigurationProvider implements ConfigurationProvider {
|
||||
public static final String VALIDATION_ACTION_NAME = "validationInterceptorTest";
|
||||
public static final String VALIDATION_ALIAS_NAME = "validationAlias";
|
||||
public static final String VALIDATION_SUBPROPERTY_NAME = "subproperty";
|
||||
public static final String EXPRESSION_VALIDATION_ACTION = "expressionValidationAction";
|
||||
|
||||
private static final Map<String,String> EMPTY_STRING_MAP = Collections.emptyMap();
|
||||
|
||||
@@ -177,6 +178,12 @@ public class MockConfigurationProvider implements ConfigurationProvider {
|
||||
.build();
|
||||
defaultPackageContext.addActionConfig(barActionConfig.getName(), barActionConfig);
|
||||
|
||||
ActionConfig expressionValidationActionConfig = new ActionConfig.Builder("defaultPackage", EXPRESSION_VALIDATION_ACTION, SimpleAction.class.getName())
|
||||
.addInterceptors(interceptors)
|
||||
.addResultConfigs(results)
|
||||
.build();
|
||||
defaultPackageContext.addActionConfig(EXPRESSION_VALIDATION_ACTION, expressionValidationActionConfig);
|
||||
|
||||
configuration.addPackageConfig("defaultPackage", defaultPackageContext.build());
|
||||
}
|
||||
|
||||
|
||||
+47
-21
@@ -38,7 +38,6 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
|
||||
|
||||
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
|
||||
Iterator it = errors.entrySet().iterator();
|
||||
|
||||
List<String> errorMessages = errors.get("percentage");
|
||||
assertNotNull("Expected double range validation error message.", errorMessages);
|
||||
@@ -82,8 +81,8 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
stack.push(prod);
|
||||
ActionContext.getContext().setValueStack(stack);
|
||||
|
||||
val.setMinInclusive("0");
|
||||
val.setMaxInclusive("10");
|
||||
val.setMinInclusive(0d);
|
||||
val.setMaxInclusive(10d);
|
||||
val.setFieldName("price");
|
||||
val.validate(prod);
|
||||
}
|
||||
@@ -92,14 +91,14 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
MyTestProduct prod = new MyTestProduct();
|
||||
prod.setName("coca cola");
|
||||
prod.setPrice(5.99);
|
||||
prod.setVolume(new Double(12.34));
|
||||
prod.setVolume(12.34d);
|
||||
|
||||
ValueStack stack = ActionContext.getContext().getValueStack();
|
||||
stack.push(prod);
|
||||
ActionContext.getContext().setValueStack(stack);
|
||||
|
||||
val.setMinInclusive("0");
|
||||
val.setMaxInclusive("30");
|
||||
val.setMinInclusive(0d);
|
||||
val.setMaxInclusive(30d);
|
||||
val.setFieldName("volume");
|
||||
val.validate(prod);
|
||||
}
|
||||
@@ -112,8 +111,8 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
stack.push(prod);
|
||||
ActionContext.getContext().setValueStack(stack);
|
||||
|
||||
val.setMinInclusive("0");
|
||||
val.setMaxInclusive("10");
|
||||
val.setMinInclusive(0d);
|
||||
val.setMaxInclusive(10d);
|
||||
val.setFieldName("name");
|
||||
|
||||
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
|
||||
@@ -121,8 +120,8 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
|
||||
val.validate(prod);
|
||||
|
||||
assertEquals("0", val.getMinInclusive());
|
||||
assertEquals("10", val.getMaxInclusive());
|
||||
assertEquals(0d, val.getMinInclusive());
|
||||
assertEquals(10d, val.getMaxInclusive());
|
||||
}
|
||||
|
||||
public void testEdgeOfMaxRange() throws Exception {
|
||||
@@ -139,15 +138,15 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
|
||||
val.setValidatorContext(context);
|
||||
|
||||
val.setMaxInclusive("9.95");
|
||||
val.setMaxInclusive(9.95d);
|
||||
val.validate(prod); // should pass
|
||||
assertTrue(!context.hasErrors());
|
||||
assertEquals("9.95", val.getMaxInclusive());
|
||||
assertEquals(9.95d, val.getMaxInclusive());
|
||||
|
||||
val.setMaxExclusive("9.95");
|
||||
val.setMaxExclusive(9.95d);
|
||||
val.validate(prod); // should not pass
|
||||
assertTrue(context.hasErrors());
|
||||
assertEquals("9.95", val.getMaxExclusive());
|
||||
assertEquals(9.95d, val.getMaxExclusive());
|
||||
}
|
||||
|
||||
public void testEdgeOfMinRange() throws Exception {
|
||||
@@ -164,11 +163,11 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
|
||||
val.setValidatorContext(context);
|
||||
|
||||
val.setMinInclusive("9.95");
|
||||
val.setMinInclusive(9.95d);
|
||||
val.validate(prod); // should pass
|
||||
assertTrue(!context.hasErrors());
|
||||
|
||||
val.setMinExclusive("9.95");
|
||||
val.setMinExclusive(9.95d);
|
||||
val.validate(prod); // should not pass
|
||||
assertTrue(context.hasErrors());
|
||||
}
|
||||
@@ -182,11 +181,38 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
|
||||
val.setValidatorContext(context);
|
||||
|
||||
val.setMinInclusive("9.95");
|
||||
val.setMinInclusive(9.95d);
|
||||
val.validate(null);
|
||||
assertTrue(!context.hasErrors()); // should pass as null value passed in
|
||||
}
|
||||
|
||||
public void testRangeValidationWithExpressionsFail() throws Exception {
|
||||
//Explicitly set an out-of-range double for DoubleRangeValidatorTest
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
HashMap<String, Object> params = new HashMap<String, Object>();
|
||||
params.put("percentage", 100.0123d);
|
||||
context.put(ActionContext.PARAMETERS, params);
|
||||
|
||||
// must set a locale to US as error message contains a locale dependent number (see XW-490)
|
||||
Locale defLocale = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, context);
|
||||
proxy.execute();
|
||||
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
|
||||
|
||||
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
|
||||
List<String> errorMessages = errors.get("percentage");
|
||||
assertNotNull("Expected double range validation error message.", errorMessages);
|
||||
assertEquals(1, errorMessages.size());
|
||||
|
||||
String errorMessage = errorMessages.get(0);
|
||||
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
|
||||
Locale.setDefault(defLocale);
|
||||
}
|
||||
|
||||
public void testExpressionParams() throws Exception {
|
||||
ValueStack stack = ActionContext.getContext().getValueStack();
|
||||
ActionSupport action = new ActionSupport() {
|
||||
@@ -201,10 +227,10 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
|
||||
stack.push(action);
|
||||
|
||||
val.setParse(true);
|
||||
val.setMinInclusive("${minInclusiveValue}");
|
||||
val.setMaxInclusive("${maxInclusiveValue}");
|
||||
val.setMinExclusive("${minExclusiveValue}");
|
||||
val.setMaxExclusive("${maxExclusiveValue}");
|
||||
val.setMinInclusiveExpression("${minInclusiveValue}");
|
||||
val.setMaxInclusiveExpression("${maxInclusiveValue}");
|
||||
val.setMinExclusiveExpression("${minExclusiveValue}");
|
||||
val.setMaxExclusiveExpression("${maxExclusiveValue}");
|
||||
|
||||
val.setFieldName("price");
|
||||
val.setDefaultMessage("Price is wrong!");
|
||||
|
||||
+7
-4
@@ -67,16 +67,19 @@ public class DateRangeFieldValidatorTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
private DateRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
|
||||
DateRangeFieldValidator validator = new DateRangeFieldValidator();
|
||||
validator.setMaxExpression("${dateMaxValue}");
|
||||
validator.setMinExpression("${dateMinValue}");
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(action);
|
||||
|
||||
DateRangeFieldValidator validator = new DateRangeFieldValidator();
|
||||
validator.setValueStack(valueStack);
|
||||
validator.setParse(true);
|
||||
|
||||
validator.setMaxExpression("${dateMaxValue}");
|
||||
validator.setMinExpression("${dateMinValue}");
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("dateRange");
|
||||
validator.setParse(true);
|
||||
validator.setDefaultMessage("Max is ${dateMaxValue}, min is ${dateMinValue} but value is ${dateRange}");
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -58,16 +58,19 @@ public class IntRangeFieldValidatorTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
|
||||
IntRangeFieldValidator validator = new IntRangeFieldValidator();
|
||||
validator.setMaxExpression("${intMaxValue}");
|
||||
validator.setMinExpression("${intMinValue}");
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(action);
|
||||
|
||||
IntRangeFieldValidator validator = new IntRangeFieldValidator();
|
||||
validator.setValueStack(valueStack);
|
||||
validator.setParse(true);
|
||||
|
||||
validator.setMaxExpression("${intMaxValue}");
|
||||
validator.setMinExpression("${intMinValue}");
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("intRange");
|
||||
validator.setParse(true);
|
||||
validator.setDefaultMessage("Max is ${intMaxValue}, min is ${intMinValue} but value is ${intRange}");
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -58,16 +58,19 @@ public class LongRangeFieldValidatorTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
private LongRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
|
||||
LongRangeFieldValidator validator = new LongRangeFieldValidator();
|
||||
validator.setMaxExpression("${longMaxValue}");
|
||||
validator.setMinExpression("${longMinValue}");
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(action);
|
||||
|
||||
LongRangeFieldValidator validator = new LongRangeFieldValidator();
|
||||
validator.setValueStack(valueStack);
|
||||
validator.setParse(true);
|
||||
|
||||
validator.setMaxExpression("${longMaxValue}");
|
||||
validator.setMinExpression("${longMinValue}");
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("longRange");
|
||||
validator.setParse(true);
|
||||
validator.setDefaultMessage("Max is ${longMaxValue}, min is ${longMinValue} but value is ${longRange}");
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -58,16 +58,19 @@ public class ShortRangeFieldValidatorTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
private ShortRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
|
||||
ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
|
||||
validator.setMaxExpression("${shortMaxValue}");
|
||||
validator.setMinExpression("${shortMinValue}");
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(action);
|
||||
|
||||
ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
|
||||
validator.setValueStack(valueStack);
|
||||
validator.setParse(true);
|
||||
|
||||
validator.setMaxExpression("${shortMaxValue}");
|
||||
validator.setMinExpression("${shortMinValue}");
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("shortRange");
|
||||
validator.setParse(true);
|
||||
validator.setDefaultMessage("Max is ${shortMaxValue}, min is ${shortMinValue} but value is ${shortRange}");
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user