mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-4004 Improves DoubleRangeFieldValidator annotation to match DoubleRangeFieldValidator class
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457672 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+15
@@ -706,6 +706,20 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
params.put("maxExclusive", v.maxExclusive());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(v.minInclusiveExpression())) {
|
||||
params.put("minInclusiveExpression", v.minInclusiveExpression());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.maxInclusiveExpression())) {
|
||||
params.put("maxInclusiveExpression", v.maxInclusiveExpression());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(v.minExclusiveExpression())) {
|
||||
params.put("minExclusiveExpression", v.minExclusiveExpression());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.maxExclusiveExpression())) {
|
||||
params.put("maxExclusiveExpression", v.maxExclusiveExpression());
|
||||
}
|
||||
|
||||
validatorFactory.lookupRegisteredValidatorType(validatorType);
|
||||
return new ValidatorConfig.Builder(validatorType)
|
||||
.addParams(params)
|
||||
@@ -713,6 +727,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
.shortCircuit(v.shortCircuit())
|
||||
.defaultMessage(v.message())
|
||||
.messageKey(v.key())
|
||||
.messageParams(v.messageParams())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
+31
@@ -56,6 +56,12 @@ import java.lang.annotation.Target;
|
||||
* <td class='confluenceTd'>i18n key from language specific properties file.</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td class='confluenceTd'>messageParams</td>
|
||||
* <td class='confluenceTd'>no</td>
|
||||
* <td class='confluenceTd'> </td>
|
||||
* <td class='confluenceTd'>Additional params to be used to customize message - will be evaluated against the Value Stack</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td class='confluenceTd'>fieldName</td>
|
||||
* <td class='confluenceTd'>no</td>
|
||||
* <td class='confluenceTd'> </td>
|
||||
@@ -124,21 +130,41 @@ public @interface DoubleRangeFieldValidator {
|
||||
*/
|
||||
String minInclusive() default "";
|
||||
|
||||
/**
|
||||
* The inclusive minimum the number must be defined as an expression
|
||||
*/
|
||||
String minInclusiveExpression() default "";
|
||||
|
||||
/**
|
||||
* Double property. The inclusive minimum the number must be.
|
||||
*/
|
||||
String maxInclusive() default "";
|
||||
|
||||
/**
|
||||
* The inclusive minimum the number must be defined as an expression
|
||||
*/
|
||||
String maxInclusiveExpression() default "";
|
||||
|
||||
/**
|
||||
* Double property. The exclusive maximum number can be.
|
||||
*/
|
||||
String minExclusive() default "";
|
||||
|
||||
/**
|
||||
* The exclusive maximum number can be defined as an expression
|
||||
*/
|
||||
String minExclusiveExpression() default "";
|
||||
|
||||
/**
|
||||
* Double property. The exclusive maximum number can be.
|
||||
*/
|
||||
String maxExclusive() default "";
|
||||
|
||||
/**
|
||||
* The exclusive maximum number can be defined as an expression
|
||||
*/
|
||||
String maxExclusiveExpression() default "";
|
||||
|
||||
/**
|
||||
* The default error message for this validator.
|
||||
* NOTE: It is required to set a message, if you are not using the message key for 18n lookup!
|
||||
@@ -150,6 +176,11 @@ public @interface DoubleRangeFieldValidator {
|
||||
*/
|
||||
String key() default "";
|
||||
|
||||
/**
|
||||
* Additional params to be used to customize message - will be evaluated against the Value Stack
|
||||
*/
|
||||
String[] messageParams() default {};
|
||||
|
||||
/**
|
||||
* The optional fieldName for SIMPLE validator types.
|
||||
*/
|
||||
|
||||
+46
-15
@@ -17,6 +17,7 @@
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.validator.ValidationException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: javadoc -->
|
||||
@@ -93,6 +94,11 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
private Double minExclusive = null;
|
||||
private Double maxExclusive = null;
|
||||
|
||||
private String minInclusiveExpression;
|
||||
private String maxInclusiveExpression;
|
||||
private String minExclusiveExpression;
|
||||
private String maxExclusiveExpression;
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
String fieldName = getFieldName();
|
||||
Double value;
|
||||
@@ -106,10 +112,15 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
return;
|
||||
}
|
||||
|
||||
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)) {
|
||||
Double maxInclusiveToUse = getMaxInclusive();
|
||||
Double minInclusiveToUse = getMinInclusive();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -119,6 +130,11 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
public Double getMaxInclusive() {
|
||||
if (maxInclusive != null) {
|
||||
return maxInclusive;
|
||||
} else if (StringUtils.isNotEmpty(maxInclusiveExpression)) {
|
||||
return (Double) parse(maxInclusiveExpression, Double.class);
|
||||
}
|
||||
return maxInclusive;
|
||||
}
|
||||
|
||||
@@ -127,39 +143,54 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
public Double getMinInclusive() {
|
||||
return minInclusive;
|
||||
}
|
||||
|
||||
public Double getMinExclusive() {
|
||||
return minExclusive;
|
||||
if (minInclusive != null) {
|
||||
return minInclusive;
|
||||
} else if (StringUtils.isNotEmpty(minInclusiveExpression)) {
|
||||
return (Double) parse(minInclusiveExpression, Double.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMinExclusive(Double minExclusive) {
|
||||
this.minExclusive = minExclusive;
|
||||
}
|
||||
|
||||
public Double getMaxExclusive() {
|
||||
return maxExclusive;
|
||||
public Double getMinExclusive() {
|
||||
if (minExclusive != null) {
|
||||
return minExclusive;
|
||||
} else if (StringUtils.isNotEmpty(minExclusiveExpression)) {
|
||||
return (Double) parse(minExclusiveExpression, Double.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMaxExclusive(Double maxExclusive) {
|
||||
this.maxExclusive = maxExclusive;
|
||||
}
|
||||
|
||||
public Double getMaxExclusive() {
|
||||
if (maxExclusive != null) {
|
||||
return maxExclusive;
|
||||
} else if (StringUtils.isNotEmpty(maxExclusiveExpression)) {
|
||||
return (Double) parse(maxExclusiveExpression, Double.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setMinInclusiveExpression(String minInclusiveExpression) {
|
||||
this.minInclusive = (Double) parse(minInclusiveExpression, Double.class);
|
||||
this.minInclusiveExpression = minInclusiveExpression;
|
||||
}
|
||||
|
||||
public void setMaxInclusiveExpression(String maxInclusiveExpression) {
|
||||
this.maxInclusive = (Double) parse(maxInclusiveExpression, Double.class);
|
||||
this.maxInclusiveExpression = maxInclusiveExpression;
|
||||
}
|
||||
|
||||
public void setMinExclusiveExpression(String minExclusiveExpression) {
|
||||
this.minExclusive = (Double) parse(minExclusiveExpression, Double.class);
|
||||
this.minExclusiveExpression = minExclusiveExpression;
|
||||
}
|
||||
|
||||
public void setMaxExclusiveExpression(String maxExclusiveExpression) {
|
||||
this.maxExclusive = (Double) parse(maxExclusiveExpression, Double.class);
|
||||
this.maxExclusiveExpression = maxExclusiveExpression;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
@@ -5,6 +5,7 @@ import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldVali
|
||||
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
@@ -29,6 +30,9 @@ public class AnnotationValidationAction extends ActionSupport {
|
||||
)
|
||||
@DateRangeFieldValidator(fieldName = "foo", key = "date.foo", max = "2012", min = "2011", dateFormat = "yyyy",
|
||||
message = "Foo isn't in range!", shortCircuit = true, messageParams = {"one", "two", "three"})
|
||||
@DoubleRangeFieldValidator(minExclusive = "1.2", maxExclusive = "1.4", minInclusive = "0", maxInclusive = "0.1",
|
||||
fieldName = "foo", key = "double.key", message = "Foo is out of range!", shortCircuit = true,
|
||||
messageParams = {"one", "two", "three"})
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
+17
-2
@@ -17,6 +17,7 @@ import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.RegexFieldValidator;
|
||||
|
||||
import java.text.ParseException;
|
||||
@@ -39,7 +40,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 5);
|
||||
assertEquals(validators.size(), 6);
|
||||
for (Validator validator : validators) {
|
||||
validate(validator);
|
||||
}
|
||||
@@ -56,7 +57,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 5);
|
||||
assertEquals(validators.size(), 6);
|
||||
for (Validator validator : validators) {
|
||||
validator.setValueStack(valueStack);
|
||||
validate(validator);
|
||||
@@ -74,9 +75,23 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
validateMyValidator((MyValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("date")) {
|
||||
validateDateRangeFieldValidator((DateRangeFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("double")) {
|
||||
validateDoubleRangeFieldValidator((DoubleRangeFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateDoubleRangeFieldValidator(DoubleRangeFieldValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("double.key", validator.getMessageKey());
|
||||
assertEquals("Foo is out of range!", validator.getDefaultMessage());
|
||||
assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
assertEquals(1.4, validator.getMaxExclusive());
|
||||
assertEquals(1.2, validator.getMinExclusive());
|
||||
assertEquals(0.1, validator.getMaxInclusive());
|
||||
assertEquals(0.0, validator.getMinInclusive());
|
||||
}
|
||||
|
||||
private void validateDateRangeFieldValidator(DateRangeFieldValidator validator) throws ParseException {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("Foo isn't in range!", validator.getDefaultMessage());
|
||||
|
||||
+21
@@ -5,6 +5,7 @@ import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldVali
|
||||
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
@@ -33,6 +34,10 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
)
|
||||
@DateRangeFieldValidator(fieldName = "foo", key = "date.foo", maxExpression = "${dateMax}", minExpression = "${dateMin}", dateFormat = "yyyy",
|
||||
message = "Foo isn't in range!", shortCircuit = true, messageParams = {"one", "two", "three"})
|
||||
@DoubleRangeFieldValidator(minExclusiveExpression = "${doubleMinExclusiveExpression}", maxExclusiveExpression = "${doubleMaxExclusiveExpression}",
|
||||
minInclusiveExpression = "${doubleMinInclusiveExpression}", maxInclusiveExpression = "${doubleMaxInclusiveExpression}",
|
||||
fieldName = "foo", key = "double.key", message = "Foo is out of range!", shortCircuit = true,
|
||||
messageParams = {"one", "two", "three"})
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -57,4 +62,20 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
return new SimpleDateFormat("yyyy").parse("2012");
|
||||
}
|
||||
|
||||
public Double getDoubleMinExclusiveExpression() {
|
||||
return 1.2;
|
||||
}
|
||||
|
||||
public Double getDoubleMaxExclusiveExpression() {
|
||||
return 1.4;
|
||||
}
|
||||
|
||||
public Double getDoubleMinInclusiveExpression() {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
public Double getDoubleMaxInclusiveExpression() {
|
||||
return 0.1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user