WW-4578 Converts double range validator to support collections
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/01a56ca1 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/01a56ca1 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/01a56ca1 Branch: refs/heads/master Commit: 01a56ca117532c63a89bec6464109e81d9273d62 Parents: e66fd53 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Mon Apr 10 21:54:03 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Mon Apr 10 21:54:03 2017 +0200 ---------------------------------------------------------------------- .../validators/DoubleRangeFieldValidator.java | 73 ++-- .../DoubleRangeFieldValidatorTest.java | 363 +++++++++++++++++++ .../validator/DoubleRangeValidatorTest.java | 279 -------------- 3 files changed, 413 insertions(+), 302 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/01a56ca1/core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java index 8ed4e4c..f968b97 100644 --- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java +++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java @@ -18,6 +18,11 @@ package com.opensymphony.xwork2.validator.validators; import com.opensymphony.xwork2.validator.ValidationException; import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Arrays; +import java.util.Collection; /** * <!-- START SNIPPET: javadoc --> @@ -27,15 +32,15 @@ import org.apache.commons.lang3.StringUtils; * * <!-- START SNIPPET: parameters --> * <ul> - * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li> - * <li>minInclusive - the minimum inclusive value in FloatValue format specified by Java language (if none is specified, it will not be checked) </li> - * <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> + * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li> + * <li>minInclusive - the minimum inclusive value in FloatValue format specified by Java language (if none is specified, it will not be checked) </li> + * <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 either minInclusive, maxInclusive, minExclusive and maxExclusive or minInclusiveExpression, maxInclusiveExpression, @@ -86,7 +91,9 @@ import org.apache.commons.lang3.StringUtils; * @author Rene Gielen */ public class DoubleRangeFieldValidator extends FieldValidatorSupport { - + + private static final Logger LOG = LogManager.getLogger(DoubleRangeFieldValidator.class); + private Double maxInclusive = null; private Double minInclusive = null; private Double minExclusive = null; @@ -99,14 +106,8 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport { public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); - Double value; - try { - Object obj = this.getFieldValue(fieldName, object); - if (obj == null) { - return; - } - value = Double.valueOf(obj.toString()); - } catch (NumberFormatException e) { + Object obj = this.getFieldValue(fieldName, object); + if (obj == null) { return; } @@ -115,11 +116,37 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport { Double maxExclusiveToUse = getMaxExclusive(); Double minExclusiveToUse = getMinExclusive(); - if ((maxInclusiveToUse != null && value.compareTo(maxInclusiveToUse) > 0) || - (minInclusiveToUse != null && value.compareTo(minInclusiveToUse) < 0) || - (maxExclusiveToUse != null && value.compareTo(maxExclusiveToUse) >= 0) || - (minExclusiveToUse != null && value.compareTo(minExclusiveToUse) <= 0)) { - addFieldError(fieldName, object); + if (obj.getClass().isArray()) { + Object[] values = (Object[]) obj; + validateCollection(maxInclusiveToUse, minInclusiveToUse, maxExclusiveToUse, minExclusiveToUse, Arrays.asList(values)); + } else if (Collection.class.isAssignableFrom(obj.getClass())) { + Collection values = (Collection) obj; + validateCollection(maxInclusiveToUse, minInclusiveToUse, maxExclusiveToUse, minExclusiveToUse, values); + } else { + validateValue(obj, maxInclusiveToUse, minInclusiveToUse, maxExclusiveToUse, minExclusiveToUse); + } + } + + protected void validateCollection(Double maxInclusiveToUse, Double minInclusiveToUse, Double maxExclusiveToUse, Double minExclusiveToUse, Collection values) { + for (Object objValue : values) { + validateValue(objValue, maxInclusiveToUse, minInclusiveToUse, maxExclusiveToUse, minExclusiveToUse); + } + } + + protected void validateValue(Object obj, Double maxInclusiveToUse, Double minInclusiveToUse, Double maxExclusiveToUse, Double minExclusiveToUse) { + try { + setCurrentValue(obj); + Double value = Double.valueOf(obj.toString()); + if ((maxInclusiveToUse != null && value.compareTo(maxInclusiveToUse) > 0) || + (minInclusiveToUse != null && value.compareTo(minInclusiveToUse) < 0) || + (maxExclusiveToUse != null && value.compareTo(maxExclusiveToUse) >= 0) || + (minExclusiveToUse != null && value.compareTo(minExclusiveToUse) <= 0)) { + addFieldError(getFieldName(), value); + } + } catch (NumberFormatException e) { + LOG.debug("Cannot validate value {} - not a Double", e); + } finally { + setCurrentValue(null); } } http://git-wip-us.apache.org/repos/asf/struts/blob/01a56ca1/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java new file mode 100644 index 0000000..68b1a82 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java @@ -0,0 +1,363 @@ +package com.opensymphony.xwork2.validator; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.TextProviderFactory; +import com.opensymphony.xwork2.ValidationAwareSupport; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; +import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import com.opensymphony.xwork2.interceptor.ValidationAware; +import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator; +import org.apache.struts2.dispatcher.HttpParameters; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Unit test for {@link DoubleRangeFieldValidator}. + * + * @author <a href="mailto:herma...@aixcept.de">Rainer Hermanns</a> + * @author Claus Ibsen + * @version $Id$ + */ +public class DoubleRangeFieldValidatorTest extends XWorkTestCase { + + private DoubleRangeFieldValidator val; + private TextProviderFactory tpf; + + public void testRangeValidationWithError() throws Exception { + //Explicitly set an out-of-range double for DoubleRangeValidatorTest + Map<String, Object> context = new HashMap<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 100.0123d); + context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, 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); + } + + public void testRangeValidationNoError() throws Exception { + Map<String, Object> context = new HashMap<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 1.234567d); + context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", "percentage", null, context); + proxy.execute(); + assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); + + Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); + List<String> errorMessages = errors.get("percentage"); + assertNull("Expected no double range validation error message.", errorMessages); + } + + public void testRangeNoExclusiveAndNoValueInStack() throws Exception { + val.setFieldName("hello"); + val.validate("world"); + } + + public void testRangeSimpleDoubleValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(5.99); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(10d); + val.setFieldName("price"); + val.validate(prod); + } + + public void testRangeRealDoubleValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(5.99); + prod.setVolume(12.34d); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(30d); + val.setFieldName("volume"); + val.validate(prod); + } + + public void testRangeNotADoubleObjectValueInStack() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setMinInclusive(0d); + val.setMaxInclusive(10d); + val.setFieldName("name"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); + val.setValidatorContext(context); + + val.validate(prod); + + assertEquals(0d, val.getMinInclusive()); + assertEquals(10d, val.getMaxInclusive()); + } + + public void testEdgeOfMaxRange() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(9.95); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); + val.setValidatorContext(context); + + val.setMaxInclusive(9.95d); + val.validate(prod); // should pass + assertTrue(!context.hasErrors()); + assertEquals(9.95d, val.getMaxInclusive()); + + val.setMaxExclusive(9.95d); + val.validate(prod); // should not pass + assertTrue(context.hasErrors()); + assertEquals(9.95d, val.getMaxExclusive()); + } + + public void testEdgeOfMinRange() throws Exception { + MyTestProduct prod = new MyTestProduct(); + prod.setName("coca cola"); + prod.setPrice(9.95); + + ValueStack stack = ActionContext.getContext().getValueStack(); + stack.push(prod); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); + val.setValidatorContext(context); + + val.setMinInclusive(9.95d); + val.validate(prod); // should pass + assertTrue(!context.hasErrors()); + + val.setMinExclusive(9.95d); + val.validate(prod); // should not pass + assertTrue(context.hasErrors()); + } + + public void testNoValue() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionContext.getContext().setValueStack(stack); + + val.setFieldName("price"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); + val.setValidatorContext(context); + + val.setMinInclusive(9.95d); + val.validate(null); + assertFalse(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<>(); + HashMap<String, Object> params = new HashMap<>(); + params.put("percentage", 100.0123d); + context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); + + ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, null, 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); + } + + public void testExpressionParams() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + ActionSupport action = new ActionSupport() { + + public Double getMinInclusiveValue() { + return 10d; + } + + public Double getMaxInclusiveValue() { + return 11d; + } + + public Double getMinExclusiveValue() { + return 13d; + } + + public Double getMaxExclusiveValue() { + return 14d; + } + + public Double getPrice() { + return 15d; + } + }; + + stack.push(action); + + val.setMinInclusiveExpression("${minInclusiveValue}"); + val.setMaxInclusiveExpression("${maxInclusiveValue}"); + val.setMinExclusiveExpression("${minExclusiveValue}"); + val.setMaxExclusiveExpression("${maxExclusiveValue}"); + + val.setFieldName("price"); + val.setDefaultMessage("Price is wrong!"); + + DelegatingValidatorContext context = new DelegatingValidatorContext(action, tpf); + val.setValidatorContext(context); + + val.validate(action); + assertTrue(action.getFieldErrors().get("price").size() == 1); + } + + public void testArrayOfDoubles() throws Exception { + val.setMinInclusive(10d); + val.setMaxInclusive(14d); + + val.setFieldName("doubleArray"); + val.setDefaultMessage("Value ${currentValue} not in scope!"); + + MyTestProduct object = new MyTestProduct(); + object.setDoubleArray(new Double[]{11d, 15d}); + + DummyValidatorContext context = new DummyValidatorContext(object, tpf); + val.setValidatorContext(context); + + val.validate(object); + + assertTrue(context.hasFieldErrors()); + assertEquals(1, context.getFieldErrors().size()); + assertEquals(1, context.getFieldErrors().get("doubleArray").size()); + assertEquals("Value 15.0 not in scope!", context.getFieldErrors().get("doubleArray").get(0)); + } + + public void testCollectionOfDoubles() throws Exception { + val.setMinInclusive(10d); + val.setMaxInclusive(14d); + + val.setFieldName("doubleCollection"); + val.setDefaultMessage("Value ${currentValue} not in scope!"); + + MyTestProduct object = new MyTestProduct(); + object.setDoubleCollection(Arrays.asList(11d, 15d)); + + DummyValidatorContext context = new DummyValidatorContext(object, tpf); + val.setValidatorContext(context); + + val.validate(object); + + assertTrue(context.hasFieldErrors()); + assertEquals(1, context.getFieldErrors().size()); + assertEquals(1, context.getFieldErrors().get("doubleCollection").size()); + assertEquals("Value 15.0 not in scope!", context.getFieldErrors().get("doubleCollection").get(0)); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-default.xml"); + container.inject(provider); + loadConfigurationProviders(provider, new MockConfigurationProvider()); + val = new DoubleRangeFieldValidator(); + val.setValueStack(ActionContext.getContext().getValueStack()); + ActionContext.getContext().setParameters(HttpParameters.create().build()); + tpf = container.getInstance(TextProviderFactory.class); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + val = null; + } + + private class MyTestProduct { + private double price; + private Double volume; + private String name; + + private Double[] doubleArray; + private Collection<Double> doubleCollection; + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getVolume() { + return volume; + } + + public void setVolume(Double volume) { + this.volume = volume; + } + + public Double[] getDoubleArray() { + return doubleArray; + } + + public void setDoubleArray(Double[] doubleArray) { + this.doubleArray = doubleArray; + } + + public Collection<Double> getDoubleCollection() { + return doubleCollection; + } + + public void setDoubleCollection(Collection<Double> doubleCollection) { + this.doubleCollection = doubleCollection; + } + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/01a56ca1/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java deleted file mode 100644 index e60d247..0000000 --- a/core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java +++ /dev/null @@ -1,279 +0,0 @@ -package com.opensymphony.xwork2.validator; - -import com.opensymphony.xwork2.*; -import com.opensymphony.xwork2.config.providers.MockConfigurationProvider; -import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; -import com.opensymphony.xwork2.interceptor.ValidationAware; -import com.opensymphony.xwork2.util.ValueStack; -import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator; -import org.apache.struts2.dispatcher.HttpParameters; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Unit test for {@link DoubleRangeFieldValidator}. - * - * @author <a href="mailto:herma...@aixcept.de">Rainer Hermanns</a> - * @author Claus Ibsen - * @version $Id$ - */ -public class DoubleRangeValidatorTest extends XWorkTestCase { - private DoubleRangeFieldValidator val; - private TextProviderFactory tpf; - - public void testRangeValidationWithError() throws Exception { - //Explicitly set an out-of-range double for DoubleRangeValidatorTest - Map<String, Object> context = new HashMap<>(); - HashMap<String, Object> params = new HashMap<>(); - params.put("percentage", 100.0123d); - context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); - - ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, 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); - } - - public void testRangeValidationNoError() throws Exception { - Map<String, Object> context = new HashMap<>(); - HashMap<String, Object> params = new HashMap<>(); - params.put("percentage", 1.234567d); - context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); - - ActionProxy proxy = actionProxyFactory.createActionProxy("", "percentage", null, context); - proxy.execute(); - assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors()); - - Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors(); - List<String> errorMessages = errors.get("percentage"); - assertNull("Expected no double range validation error message.", errorMessages); - } - - public void testRangeNoExclusiveAndNoValueInStack() throws Exception { - val.setFieldName("hello"); - val.validate("world"); - } - - public void testRangeSimpleDoubleValueInStack() throws Exception { - MyTestProduct prod = new MyTestProduct(); - prod.setName("coca cola"); - prod.setPrice(5.99); - - ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(prod); - ActionContext.getContext().setValueStack(stack); - - val.setMinInclusive(0d); - val.setMaxInclusive(10d); - val.setFieldName("price"); - val.validate(prod); - } - - public void testRangeRealDoubleValueInStack() throws Exception { - MyTestProduct prod = new MyTestProduct(); - prod.setName("coca cola"); - prod.setPrice(5.99); - prod.setVolume(12.34d); - - ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(prod); - ActionContext.getContext().setValueStack(stack); - - val.setMinInclusive(0d); - val.setMaxInclusive(30d); - val.setFieldName("volume"); - val.validate(prod); - } - - public void testRangeNotADoubleObjectValueInStack() throws Exception { - MyTestProduct prod = new MyTestProduct(); - prod.setName("coca cola"); - - ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(prod); - ActionContext.getContext().setValueStack(stack); - - val.setMinInclusive(0d); - val.setMaxInclusive(10d); - val.setFieldName("name"); - - DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); - val.setValidatorContext(context); - - val.validate(prod); - - assertEquals(0d, val.getMinInclusive()); - assertEquals(10d, val.getMaxInclusive()); - } - - public void testEdgeOfMaxRange() throws Exception { - MyTestProduct prod = new MyTestProduct(); - prod.setName("coca cola"); - prod.setPrice(9.95); - - ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(prod); - ActionContext.getContext().setValueStack(stack); - - val.setFieldName("price"); - - DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); - val.setValidatorContext(context); - - val.setMaxInclusive(9.95d); - val.validate(prod); // should pass - assertTrue(!context.hasErrors()); - assertEquals(9.95d, val.getMaxInclusive()); - - val.setMaxExclusive(9.95d); - val.validate(prod); // should not pass - assertTrue(context.hasErrors()); - assertEquals(9.95d, val.getMaxExclusive()); - } - - public void testEdgeOfMinRange() throws Exception { - MyTestProduct prod = new MyTestProduct(); - prod.setName("coca cola"); - prod.setPrice(9.95); - - ValueStack stack = ActionContext.getContext().getValueStack(); - stack.push(prod); - ActionContext.getContext().setValueStack(stack); - - val.setFieldName("price"); - - DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); - val.setValidatorContext(context); - - val.setMinInclusive(9.95d); - val.validate(prod); // should pass - assertTrue(!context.hasErrors()); - - val.setMinExclusive(9.95d); - val.validate(prod); // should not pass - assertTrue(context.hasErrors()); - } - - public void testNoValue() throws Exception { - ValueStack stack = ActionContext.getContext().getValueStack(); - ActionContext.getContext().setValueStack(stack); - - val.setFieldName("price"); - - DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf); - val.setValidatorContext(context); - - 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<>(); - HashMap<String, Object> params = new HashMap<>(); - params.put("percentage", 100.0123d); - context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); - - ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, null, 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); - } - - public void testExpressionParams() throws Exception { - ValueStack stack = ActionContext.getContext().getValueStack(); - ActionSupport action = new ActionSupport() { - - public Double getMinInclusiveValue() {return 10d;} - public Double getMaxInclusiveValue() {return 11d;} - public Double getMinExclusiveValue() {return 13d;} - public Double getMaxExclusiveValue() {return 14d;} - public Double getPrice() {return 15d;} - }; - - stack.push(action); - - val.setMinInclusiveExpression("${minInclusiveValue}"); - val.setMaxInclusiveExpression("${maxInclusiveValue}"); - val.setMinExclusiveExpression("${minExclusiveValue}"); - val.setMaxExclusiveExpression("${maxExclusiveValue}"); - - val.setFieldName("price"); - val.setDefaultMessage("Price is wrong!"); - - DelegatingValidatorContext context = new DelegatingValidatorContext(action, tpf); - val.setValidatorContext(context); - - val.validate(action); - assertTrue(action.getFieldErrors().get("price").size() == 1); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - XmlConfigurationProvider provider = new XmlConfigurationProvider("xwork-default.xml"); - container.inject(provider); - loadConfigurationProviders(provider, new MockConfigurationProvider()); - val = new DoubleRangeFieldValidator(); - val.setValueStack(ActionContext.getContext().getValueStack()); - ActionContext.getContext().setParameters(HttpParameters.create().build()); - tpf = container.getInstance(TextProviderFactory.class); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - val = null; - } - - private class MyTestProduct { - private double price; - private Double volume; - private String name; - - public double getPrice() { - return price; - } - - public void setPrice(double price) { - this.price = price; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Double getVolume() { - return volume; - } - - public void setVolume(Double volume) { - this.volume = volume; - } - } - -}