From 16fade2857cc1ad8cd576ca4bb91457d4c7a2cb4 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sat, 16 Mar 2013 15:14:04 +0000 Subject: [PATCH] WW-4000 Improves ConditionalVisitorFieldValidator annotation to match validator class ConditionalVisitorFieldValidator git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457262 13f79535-47bb-0310-9956-ffa450edef68 --- ...otationValidationConfigurationBuilder.java | 2 + .../ConditionalVisitorFieldValidator.java | 13 +++++- .../annotations/RegexFieldValidator.java | 44 ++++++++++++++++++- .../validator/AnnotationValidationAction.java | 14 +++--- ...ionValidationConfigurationBuilderTest.java | 36 ++++++++++----- .../AnnotationValidationExpAction.java | 14 +++--- 6 files changed, 97 insertions(+), 26 deletions(-) diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java index 027c1fa75..30f10fb77 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java @@ -451,6 +451,7 @@ public class AnnotationValidationConfigurationBuilder { .shortCircuit(v.shortCircuit()) .defaultMessage(v.message()) .messageKey(v.key()) + .messageParams(v.messageParams()) .build(); } @@ -476,6 +477,7 @@ public class AnnotationValidationConfigurationBuilder { .shortCircuit(v.shortCircuit()) .defaultMessage(v.message()) .messageKey(v.key()) + .messageParams(v.messageParams()) .build(); } diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/ConditionalVisitorFieldValidator.java b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/ConditionalVisitorFieldValidator.java index 63408280a..29607ce6e 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/ConditionalVisitorFieldValidator.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/ConditionalVisitorFieldValidator.java @@ -52,6 +52,12 @@ import java.lang.annotation.Target; * i18n key from language specific properties file. * * + * messageParams + * no + *   + * Additional params to be used to customize message - will be evaluated against the Value Stack + * + * * fieldName * no *   @@ -67,7 +73,7 @@ import java.lang.annotation.Target; * context * no * action alias - * Determines the context to use for validating the Object property. If not defined, the context of the Action validation is propogated to the Object property validation. In the case of Action validation, this context is the Action alias. + * Determines the context to use for validating the Object property. If not defined, the context of the Action validation is propagated to the Object property validation. In the case of Action validation, this context is the Action alias. * * * appendPrefix @@ -129,6 +135,11 @@ public @interface ConditionalVisitorFieldValidator { */ 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. */ diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java index 40e13f6bd..bcd48110d 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java @@ -55,6 +55,12 @@ import java.lang.annotation.Target; * i18n key from language specific properties file. * * + * messageParams + * no + *   + * Additional params to be used to customize message - will be evaluated against the Value Stack + * + * * fieldName * no *   @@ -102,6 +108,11 @@ public @interface RegexFieldValidator { */ String message() default ""; + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + /** * The message key to lookup for i18n. */ @@ -112,17 +123,47 @@ public @interface RegexFieldValidator { */ String fieldName() default ""; - + /** + * Regex used to evaluate field against it + * + * @return String regular expression + */ String regex() default ""; + /** + * Defines regex as an expression which first will be evaluated against the Value Stack to get proper regex. + * Thus allow to dynamically change regex base on user actions. + * + * @return String an expression which starts with '$' or '%' + */ String regexExpression() default ""; + /** + * To trim or not the value, default true - trim + * + * @return boolean trim or not the value before validation + */ boolean trim() default true; + /** + * Allows specify trim as an expression which will be evaluated during validation + * + * @return String an expression which starts with '$' or '%' + */ String trimExpression() default ""; + /** + * Match the value in case sensitive manner, default true + * + * @return boolean use case sensitive match or not + */ boolean caseSensitive() default true; + /** + * Allows specify caseSensitive as an expression which will be evaluated during validation + * + * @return boolean use case sensitive match or not + */ String caseSensitiveExpression() default ""; /** @@ -137,4 +178,5 @@ public @interface RegexFieldValidator { * The validation type for this field/method. */ ValidatorType type() default ValidatorType.FIELD; + } diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java index b7d74468b..b42141ae2 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java @@ -1,20 +1,20 @@ package com.opensymphony.xwork2.validator; import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldValidator; 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), - } - ) + @RegexFieldValidator(regex = "foo", message = "Foo doesn't match!", key = "regex.key", + fieldName = "bar", shortCircuit = true, trim = false, caseSensitive = false, + messageParams = {"one", "two", "three"}) + @ConditionalVisitorFieldValidator(expression = "foo+bar", context = "some", appendPrefix = false, fieldName = "bar", + key = "conditional.key", message = "Foo doesn't match!", shortCircuit = true, + messageParams = {"one", "two", "three"}) public String execute() { return SUCCESS; } diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java index 141dd3a57..b2b538e47 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilderTest.java @@ -14,8 +14,10 @@ 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.ConditionalVisitorFieldValidator; import com.opensymphony.xwork2.validator.validators.RegexFieldValidator; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,7 +25,7 @@ import java.util.Map; /** * Simple test to check if validation Annotations match given validator class */ -public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase{ +public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase { public void testValidationAnnotation() throws Exception { // given @@ -33,11 +35,9 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase{ List validators = manager.getValidators(AnnotationValidationAction.class, null); // then - assertEquals(validators.size(), 1); + assertEquals(validators.size(), 2); for (Validator validator : validators) { - if (validator.getValidatorType().equals("regex")) { - validateRegexValidator((RegexFieldValidator) validator); - } + validate(validator); } } @@ -52,15 +52,30 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase{ ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack(); valueStack.push(new AnnotationValidationExpAction()); - assertEquals(validators.size(), 1); + assertEquals(validators.size(), 2); for (Validator validator : validators) { - if (validator.getValidatorType().equals("regex")) { - validator.setValueStack(valueStack); - validateRegexValidator((RegexFieldValidator) validator); - } + validator.setValueStack(valueStack); + validate(validator); } } + private void validate(Validator validator) { + if (validator.getValidatorType().equals("regex")) { + validateRegexValidator((RegexFieldValidator) validator); + } else if (validator.getValidatorType().equals("conditionalvisitor")) { + validateConditionalFieldVisitorValidator((ConditionalVisitorFieldValidator) validator); + } + } + + private void validateConditionalFieldVisitorValidator(ConditionalVisitorFieldValidator validator) { + assertEquals("foo+bar", validator.getExpression()); + assertEquals("some", validator.getContext()); + assertEquals("Foo doesn't match!", validator.getDefaultMessage()); + assertEquals("bar", validator.getFieldName()); + assertEquals("conditional.key", validator.getMessageKey()); + assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters())); + } + private void validateRegexValidator(RegexFieldValidator validator) { assertEquals("foo", validator.getRegex()); assertEquals("Foo doesn't match!", validator.getDefaultMessage()); @@ -69,6 +84,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase{ assertEquals(true, validator.isShortCircuit()); assertEquals(false, validator.isTrimed()); assertEquals(false, validator.isCaseSensitive()); + assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters())); } private AnnotationActionValidatorManager createValidationManager(final Class actionClass) throws Exception { diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java index bfaf1bcca..61d7b5be1 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationExpAction.java @@ -1,20 +1,20 @@ package com.opensymphony.xwork2.validator; import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldValidator; 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}"), - } - ) + @RegexFieldValidator(regexExpression = "${foo}", message = "Foo doesn't match!", key = "regex.key", + fieldName = "bar", shortCircuit = true, trimExpression = "${trim}", caseSensitiveExpression = "${caseSensitive}", + messageParams = {"one", "two", "three"}) + @ConditionalVisitorFieldValidator(expression = "foo+bar", context = "some", appendPrefix = false, fieldName = "bar", + key = "conditional.key", message = "Foo doesn't match!", shortCircuit = true, + messageParams = {"one", "two", "three"}) public String execute() { return SUCCESS; }