mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-3996 Updates RegexFieldValidator annotation to match implementing class and adds test to verify that
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+1
-1
@@ -757,7 +757,7 @@ public class PackageBasedActionConfigBuilderTest extends TestCase {
|
||||
((ObjectFactory)obj).setReflectionProvider(new OgnlReflectionProvider() {
|
||||
|
||||
@Override
|
||||
public void setProperties(Map<String, String> properties, Object o) {
|
||||
public void setProperties(Map<String, ?> properties, Object o) {
|
||||
}
|
||||
|
||||
public void setProperties(Map<String, String> properties, Object o, Map<String, Object> context, boolean throwPropertyExceptions) throws ReflectionException {
|
||||
|
||||
@@ -250,7 +250,7 @@ public class ObjectFactory implements Serializable {
|
||||
* @param params property name -> value Map to set onto the Validator instance
|
||||
* @param extraContext a Map of extra context which uses the same keys as the {@link com.opensymphony.xwork2.ActionContext}
|
||||
*/
|
||||
public Validator buildValidator(String className, Map<String, String> params, Map<String, Object> extraContext) throws Exception {
|
||||
public Validator buildValidator(String className, Map<String, Object> params, Map<String, Object> extraContext) throws Exception {
|
||||
Validator validator = (Validator) buildBean(className, null);
|
||||
reflectionProvider.setProperties(params, validator);
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public class OgnlReflectionProvider implements ReflectionProvider {
|
||||
|
||||
}
|
||||
|
||||
public void setProperties(Map<String, String> properties, Object o) {
|
||||
public void setProperties(Map<String, ?> properties, Object o) {
|
||||
ognlUtil.setProperties(properties, o);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ public interface ReflectionProvider {
|
||||
* @param properties
|
||||
* @param o
|
||||
*/
|
||||
void setProperties(Map<String, String> properties, Object o);
|
||||
void setProperties(Map<String, ?> properties, Object o);
|
||||
|
||||
/**
|
||||
* This method returns a PropertyDescriptor for the given class and property name using
|
||||
|
||||
+22
-17
@@ -367,7 +367,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processExpressionValidatorAnnotation(ExpressionValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "expression";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -388,7 +388,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
|
||||
private ValidatorConfig processCustomValidatorAnnotation(CustomValidator v, String fieldName, String methodName) {
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -429,7 +429,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processRegexFieldValidatorAnnotation(RegexFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "regex";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -437,12 +437,17 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
params.put("fieldName", v.fieldName());
|
||||
}
|
||||
|
||||
params.put("expression", v.expression());
|
||||
params.put("regex", v.regex());
|
||||
params.put("regexExpression", v.regexExpression());
|
||||
|
||||
validatorFactory.lookupRegisteredValidatorType(validatorType);
|
||||
return new ValidatorConfig.Builder(validatorType)
|
||||
.addParams(params)
|
||||
.addParam("methodName", methodName)
|
||||
.addParam("trim", v.trim())
|
||||
.addParam("trimExpression", v.trimExpression())
|
||||
.addParam("caseSensitive", v.caseSensitive())
|
||||
.addParam("caseSensitiveExpression", v.caseSensitiveExpression())
|
||||
.shortCircuit(v.shortCircuit())
|
||||
.defaultMessage(v.message())
|
||||
.messageKey(v.key())
|
||||
@@ -452,7 +457,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processConditionalVisitorFieldValidatorAnnotation(ConditionalVisitorFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "conditionalvisitor";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -480,7 +485,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
|
||||
String validatorType = "visitor";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -504,7 +509,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processUrlValidatorAnnotation(UrlValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "url";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -525,7 +530,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processStringLengthFieldValidatorAnnotation(StringLengthFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "stringlength";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -572,7 +577,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processRequiredStringValidatorAnnotation(RequiredStringValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "requiredstring";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -595,7 +600,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processRequiredFieldValidatorAnnotation(RequiredFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "required";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -616,7 +621,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processIntRangeFieldValidatorAnnotation(IntRangeFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "int";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -644,7 +649,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processShortRangeFieldValidatorAnnotation(ShortRangeFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "short";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -672,7 +677,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processDoubleRangeFieldValidatorAnnotation(DoubleRangeFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "double";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -707,7 +712,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processFieldExpressionValidatorAnnotation(FieldExpressionValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "fieldexpression";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -730,7 +735,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processEmailValidatorAnnotation(EmailValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "email";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -751,7 +756,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processDateRangeFieldValidatorAnnotation(DateRangeFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "date";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
@@ -780,7 +785,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
private ValidatorConfig processConversionErrorFieldValidatorAnnotation(ConversionErrorFieldValidator v, String fieldName, String methodName) {
|
||||
String validatorType = "conversion";
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
|
||||
+5
-5
@@ -88,13 +88,13 @@ public class DefaultValidatorFileParser implements ValidatorFileParser {
|
||||
// it will not cause field-level validator to be kicked off.
|
||||
{
|
||||
NodeList validatorNodes = doc.getElementsByTagName("validator");
|
||||
addValidatorConfigs(validatorFactory, validatorNodes, new HashMap<String, String>(), validatorCfgs);
|
||||
addValidatorConfigs(validatorFactory, validatorNodes, new HashMap<String, Object>(), validatorCfgs);
|
||||
}
|
||||
|
||||
for (int i = 0; i < fieldNodes.getLength(); i++) {
|
||||
Element fieldElement = (Element) fieldNodes.item(i);
|
||||
String fieldName = fieldElement.getAttribute("name");
|
||||
Map<String, String> extraParams = new HashMap<String, String>();
|
||||
Map<String, Object> extraParams = new HashMap<String, Object>();
|
||||
extraParams.put("fieldName", fieldName);
|
||||
|
||||
NodeList validatorNodes = fieldElement.getElementsByTagName("field-validator");
|
||||
@@ -127,7 +127,7 @@ public class DefaultValidatorFileParser implements ValidatorFileParser {
|
||||
|
||||
try {
|
||||
// catch any problems here
|
||||
objectFactory.buildValidator(className, new HashMap<String, String>(), null);
|
||||
objectFactory.buildValidator(className, new HashMap<String, Object>(), null);
|
||||
validators.put(name, className);
|
||||
} catch (Exception e) {
|
||||
throw new ConfigurationException("Unable to load validator class " + className, e, validatorElement);
|
||||
@@ -166,11 +166,11 @@ public class DefaultValidatorFileParser implements ValidatorFileParser {
|
||||
return value.toString().trim();
|
||||
}
|
||||
|
||||
private void addValidatorConfigs(ValidatorFactory factory, NodeList validatorNodes, Map<String, String> extraParams, List<ValidatorConfig> validatorCfgs) {
|
||||
private void addValidatorConfigs(ValidatorFactory factory, NodeList validatorNodes, Map<String, Object> extraParams, List<ValidatorConfig> validatorCfgs) {
|
||||
for (int j = 0; j < validatorNodes.getLength(); j++) {
|
||||
Element validatorElement = (Element) validatorNodes.item(j);
|
||||
String validatorType = validatorElement.getAttribute("type");
|
||||
Map<String, String> params = new HashMap<String, String>(extraParams);
|
||||
Map<String, Object> params = new HashMap<String, Object>(extraParams);
|
||||
|
||||
params.putAll(XmlHelper.getParams(validatorElement));
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import java.util.Map;
|
||||
public class ValidatorConfig extends Located {
|
||||
|
||||
private String type;
|
||||
private Map<String,String> params;
|
||||
private Map<String, Object> params;
|
||||
private String defaultMessage;
|
||||
private String messageKey;
|
||||
private boolean shortCircuit;
|
||||
@@ -45,12 +45,12 @@ public class ValidatorConfig extends Located {
|
||||
*/
|
||||
protected ValidatorConfig(String validatorType) {
|
||||
this.type = validatorType;
|
||||
params = new LinkedHashMap<String, String>();
|
||||
params = new LinkedHashMap<String, Object>();
|
||||
}
|
||||
|
||||
protected ValidatorConfig(ValidatorConfig orig) {
|
||||
this.type = orig.type;
|
||||
this.params = new LinkedHashMap<String,String>(orig.params);
|
||||
this.params = new LinkedHashMap<String,Object>(orig.params);
|
||||
this.defaultMessage = orig.defaultMessage;
|
||||
this.messageKey = orig.messageKey;
|
||||
this.shortCircuit = orig.shortCircuit;
|
||||
@@ -82,7 +82,7 @@ public class ValidatorConfig extends Located {
|
||||
/**
|
||||
* @return Returns the configured params to set on the validator.
|
||||
*/
|
||||
public Map<String, String> getParams() {
|
||||
public Map<String, Object> getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
@@ -138,14 +138,14 @@ public class ValidatorConfig extends Located {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addParam(String name, String value) {
|
||||
public Builder addParam(String name, Object value) {
|
||||
if (value != null && name != null) {
|
||||
target.params.put(name, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addParams(Map<String,String> params) {
|
||||
public Builder addParams(Map<String,Object> params) {
|
||||
target.params.putAll(params);
|
||||
return this;
|
||||
}
|
||||
|
||||
+11
-1
@@ -113,7 +113,17 @@ public @interface RegexFieldValidator {
|
||||
String fieldName() default "";
|
||||
|
||||
|
||||
String expression();
|
||||
String regex() default "";
|
||||
|
||||
String regexExpression() default "";
|
||||
|
||||
boolean trim() default true;
|
||||
|
||||
String trimExpression() default "";
|
||||
|
||||
boolean caseSensitive() default true;
|
||||
|
||||
String caseSensitiveExpression() default "";
|
||||
|
||||
/**
|
||||
* If this is activated, the validator will be used as short-circuit.
|
||||
|
||||
+34
-10
@@ -16,7 +16,10 @@
|
||||
|
||||
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;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -77,16 +80,25 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RegexFieldValidator.class);
|
||||
|
||||
private String regex;
|
||||
private boolean caseSensitive = true;
|
||||
private boolean trim = true;
|
||||
private String regexExpression;
|
||||
private Boolean caseSensitive = true;
|
||||
private String caseSensitiveExpression = "";
|
||||
private Boolean trim = true;
|
||||
private String trimExpression = "";
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
String fieldName = getFieldName();
|
||||
Object value = this.getFieldValue(fieldName, object);
|
||||
// if there is no value - don't do comparison
|
||||
// if a value is required, a required validator should be added to the field
|
||||
if (value == null || regex == null) {
|
||||
String regexToUse = getRegex();
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Defined regexp as [#0]", regexToUse);
|
||||
}
|
||||
if (value == null || regexToUse == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,13 +116,13 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
// match against expression
|
||||
Pattern pattern;
|
||||
if (isCaseSensitive()) {
|
||||
pattern = Pattern.compile(regex);
|
||||
pattern = Pattern.compile(regexToUse);
|
||||
} else {
|
||||
pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
|
||||
pattern = Pattern.compile(regexToUse, Pattern.CASE_INSENSITIVE);
|
||||
}
|
||||
|
||||
String compare = (String) value;
|
||||
if ( trim ) {
|
||||
if ( isTrimed() ) {
|
||||
compare = compare.trim();
|
||||
}
|
||||
Matcher matcher = pattern.matcher( compare );
|
||||
@@ -124,7 +136,13 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* @return Returns the regular expression to be matched.
|
||||
*/
|
||||
public String getRegex() {
|
||||
return regex;
|
||||
if (StringUtils.isNotEmpty(regex)) {
|
||||
return regex;
|
||||
} else if (StringUtils.isNotEmpty(regexExpression)) {
|
||||
return (String) parse(regexExpression, String.class);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +156,7 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* Sets the regular expression as an OGNL expression to be matched
|
||||
*/
|
||||
public void setRegexExpression(String regexExpression) {
|
||||
this.regex = (String) parse(regexExpression, String.class);
|
||||
this.regexExpression = regexExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,6 +164,9 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* a case-sensitive way. Default is <code>true</code>.
|
||||
*/
|
||||
public boolean isCaseSensitive() {
|
||||
if (StringUtils.isNotEmpty(caseSensitiveExpression)) {
|
||||
return (Boolean) parse(caseSensitiveExpression, Boolean.class);
|
||||
}
|
||||
return caseSensitive;
|
||||
}
|
||||
|
||||
@@ -161,7 +182,7 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* Allows specify caseSensitive param as an OGNL expression
|
||||
*/
|
||||
public void setCaseSensitiveExpression(String caseSensitiveExpression) {
|
||||
this.caseSensitive = (Boolean) parse(caseSensitiveExpression, Boolean.class);
|
||||
this.caseSensitiveExpression = caseSensitiveExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,6 +190,9 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* Default is <code>true</code>.
|
||||
*/
|
||||
public boolean isTrimed() {
|
||||
if (StringUtils.isNotEmpty(trimExpression)) {
|
||||
return (Boolean) parse(trimExpression, Boolean.class);
|
||||
}
|
||||
return trim;
|
||||
}
|
||||
|
||||
@@ -184,7 +208,7 @@ public class RegexFieldValidator extends FieldValidatorSupport {
|
||||
* Allows specify trim param as an OGNL expression
|
||||
*/
|
||||
public void setTrimExpression(String trimExpression) {
|
||||
this.trim = (Boolean) parse(trimExpression, Boolean.class);
|
||||
this.trimExpression = trimExpression;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
@@ -40,6 +40,7 @@ import java.util.List;
|
||||
public abstract class ValidatorSupport implements Validator, ShortCircuitableValidator {
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
protected String defaultMessage = "";
|
||||
protected String messageKey;
|
||||
private ValidatorContext validatorContext;
|
||||
|
||||
+11
-4
@@ -19,7 +19,15 @@ package com.opensymphony.xwork2.spring;
|
||||
* Created on Mar 8, 2004
|
||||
*/
|
||||
|
||||
import com.opensymphony.xwork2.*;
|
||||
import com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionChainResult;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ModelDrivenAction;
|
||||
import com.opensymphony.xwork2.ObjectFactory;
|
||||
import com.opensymphony.xwork2.Result;
|
||||
import com.opensymphony.xwork2.SimpleAction;
|
||||
import com.opensymphony.xwork2.TestBean;
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
|
||||
@@ -39,7 +47,6 @@ import org.springframework.aop.interceptor.DebugInterceptor;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
@@ -107,7 +114,7 @@ public class SpringObjectFactoryTest extends XWorkTestCase {
|
||||
}
|
||||
|
||||
public void testFallsBackToDefaultObjectFactoryValidatorBuilding() throws Exception {
|
||||
Validator validator = objectFactory.buildValidator(RequiredStringValidator.class.getName(), new HashMap<String, String>(), null);
|
||||
Validator validator = objectFactory.buildValidator(RequiredStringValidator.class.getName(), new HashMap<String, Object>(), null);
|
||||
|
||||
assertEquals(RequiredStringValidator.class, validator.getClass());
|
||||
}
|
||||
@@ -143,7 +150,7 @@ public class SpringObjectFactoryTest extends XWorkTestCase {
|
||||
public void testObtainValidatorBySpringName() throws Exception {
|
||||
sac.registerPrototype("expression-validator", ExpressionValidator.class, new MutablePropertyValues());
|
||||
|
||||
Validator validator = objectFactory.buildValidator("expression-validator", new HashMap<String, String>(), null);
|
||||
Validator validator = objectFactory.buildValidator("expression-validator", new HashMap<String, Object>(), null);
|
||||
|
||||
assertEquals(ExpressionValidator.class, validator.getClass());
|
||||
}
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.opensymphony.xwork2.validator;
|
||||
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.Validations;
|
||||
|
||||
/**
|
||||
* Sets up all available validation annotations
|
||||
*/
|
||||
public class AnnotationValidationAction extends ActionSupport {
|
||||
|
||||
@Validations(
|
||||
regexFields = {
|
||||
@RegexFieldValidator(regex = "foo", message = "Foo doesn't match!", key = "regex.key",
|
||||
fieldName = "bar", shortCircuit = true, trim = false, caseSensitive = false),
|
||||
}
|
||||
)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package com.opensymphony.xwork2.validator;
|
||||
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import com.opensymphony.xwork2.DefaultActionInvocation;
|
||||
import com.opensymphony.xwork2.XWorkConstants;
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.ConfigurationProvider;
|
||||
import com.opensymphony.xwork2.config.entities.ActionConfig;
|
||||
import com.opensymphony.xwork2.config.entities.PackageConfig;
|
||||
import com.opensymphony.xwork2.inject.ContainerBuilder;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import com.opensymphony.xwork2.validator.validators.RegexFieldValidator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Simple test to check if validation Annotations match given validator class
|
||||
*/
|
||||
public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase{
|
||||
|
||||
public void testValidationAnnotation() throws Exception {
|
||||
// given
|
||||
AnnotationActionValidatorManager manager = createValidationManager(AnnotationValidationAction.class);
|
||||
|
||||
// when
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 1);
|
||||
for (Validator validator : validators) {
|
||||
if (validator.getValidatorType().equals("regex")) {
|
||||
validateRegexValidator((RegexFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testValidationAnnotationExpParams() throws Exception {
|
||||
// given
|
||||
AnnotationActionValidatorManager manager = createValidationManager(AnnotationValidationExpAction.class);
|
||||
|
||||
// when
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationExpAction.class, null);
|
||||
|
||||
// then
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 1);
|
||||
for (Validator validator : validators) {
|
||||
if (validator.getValidatorType().equals("regex")) {
|
||||
validator.setValueStack(valueStack);
|
||||
validateRegexValidator((RegexFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void validateRegexValidator(RegexFieldValidator validator) {
|
||||
assertEquals("foo", validator.getRegex());
|
||||
assertEquals("Foo doesn't match!", validator.getDefaultMessage());
|
||||
assertEquals("regex.key", validator.getMessageKey());
|
||||
assertEquals("bar", validator.getFieldName());
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
assertEquals(false, validator.isTrimed());
|
||||
assertEquals(false, validator.isCaseSensitive());
|
||||
}
|
||||
|
||||
private AnnotationActionValidatorManager createValidationManager(final Class<? extends ActionSupport> actionClass) throws Exception {
|
||||
loadConfigurationProviders(new ConfigurationProvider() {
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
public void init(Configuration configuration) throws ConfigurationException {
|
||||
configuration.addPackageConfig("default", new PackageConfig.Builder("default")
|
||||
.addActionConfig("annotation", new ActionConfig.Builder("", "annotation", actionClass.getName()).build())
|
||||
.build());
|
||||
}
|
||||
|
||||
public boolean needsReload() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void loadPackages() throws ConfigurationException {
|
||||
|
||||
}
|
||||
|
||||
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
|
||||
builder.constant(XWorkConstants.DEV_MODE, true);
|
||||
}
|
||||
});
|
||||
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
ActionInvocation invocation = new DefaultActionInvocation(context, true);
|
||||
container.inject(invocation);
|
||||
invocation.init(actionProxyFactory.createActionProxy("", "annotation", null, context));
|
||||
|
||||
AnnotationActionValidatorManager manager = new AnnotationActionValidatorManager();
|
||||
container.inject(manager);
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.opensymphony.xwork2.validator;
|
||||
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.Validations;
|
||||
|
||||
/**
|
||||
* Sets up all available validation annotations with params as expressions
|
||||
*/
|
||||
public class AnnotationValidationExpAction extends ActionSupport {
|
||||
|
||||
@Validations(
|
||||
regexFields = {
|
||||
@RegexFieldValidator(regexExpression = "${foo}", message = "Foo doesn't match!", key = "regex.key",
|
||||
fieldName = "bar", shortCircuit = true, trimExpression = "${trim}", caseSensitiveExpression = "${caseSensitive}"),
|
||||
}
|
||||
)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
public String getFoo() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
public boolean getTrim() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean getCaseSensitive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class EmailValidatorTest extends XWorkTestCase {
|
||||
assertFalse(verifyEmailValidity("aaa@aa.aaaaaaa"));
|
||||
assertFalse(verifyEmailValidity("+ferdamravenec@yahoo.com"));
|
||||
|
||||
assertFalse(verifyEmailValidityWithExpression("tmjee@yahoo.co", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b"));
|
||||
assertTrue(verifyEmailValidityWithExpression("tmjee@yahoo.co", "\\b^[a-z]+@[a-z]+(\\.[a-z]+)*\\.com$\\b"));
|
||||
}
|
||||
|
||||
protected boolean verifyEmailValidity(final String email) throws Exception {
|
||||
@@ -97,7 +97,6 @@ public class EmailValidatorTest extends XWorkTestCase {
|
||||
validator.setRegexExpression("${emailExpression}");
|
||||
|
||||
validator.validate(action);
|
||||
valueStack.pop();
|
||||
|
||||
return (action.getFieldErrors().size() == 0);
|
||||
}
|
||||
@@ -124,8 +123,6 @@ public class EmailValidatorTest extends XWorkTestCase {
|
||||
|
||||
validator.setCaseSensitiveExpression("${emailCaseSensitive}");
|
||||
|
||||
valueStack.pop();
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
@@ -151,8 +148,6 @@ public class EmailValidatorTest extends XWorkTestCase {
|
||||
|
||||
validator.setTrimExpression("${trimEmail}");
|
||||
|
||||
valueStack.pop();
|
||||
|
||||
return validator;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user