incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1062004 - in /incubator/bval/sandbox/lang3-work/bval-jsr303d/src: main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java
Date Fri, 21 Jan 2011 20:13:24 GMT
Author: mbenson
Date: Fri Jan 21 20:13:23 2011
New Revision: 1062004

URL: http://svn.apache.org/viewvc?rev=1062004&view=rev
Log:
add the ability to validate CharSequence values case-insensitively, and/or after trimming
values

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java?rev=1062004&r1=1062003&r2=1062004&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/StringValuesValidator.java
Fri Jan 21 20:13:23 2011
@@ -17,23 +17,77 @@
 package org.apache.bval.constraints.dynamic;
 
 import javax.validation.ConstraintValidator;
+import javax.validation.Payload;
 
 import org.apache.commons.lang3.StringUtils;
 
 /**
- * {@link ConstraintValidator} for {@link Values} constraints on
- * {@link CharSequence} types.
+ * {@link ConstraintValidator} for {@link Values} constraints on {@link CharSequence} types.
  * 
  * @version $Rev$ $Date$
  */
 public class StringValuesValidator extends AbstractValuesValidator.Plain<CharSequence>
{
+    /**
+     * Payload to indicate that {@link CharSequence} values should be compared without case-sensitivity.
Definition as
+     * payload leaves the base constraints type-agnostic.
+     */
+    public interface IgnoreCase extends Payload {
+    }
+
+    /**
+     * Payload to indicate that {@link CharSequence} values should be trimmed prior to comparison
with constrained
+     * values. Definition as payload leaves the base constraints type-agnostic.
+     */
+    public interface Trim extends Payload {
+    }
+
+    private static class Test {
+        private final boolean ignoreCase;
+        private final boolean trim;
+
+        /**
+         * Create a new Test instance.
+         * 
+         * @param payload
+         */
+        Test(Class<? extends Payload>[] payload) {
+            this.ignoreCase = hasPayload(IgnoreCase.class, payload);
+            this.trim = hasPayload(Trim.class, payload);
+        }
+
+        /**
+         * Learn whether a value matches an available choice.
+         * 
+         * @param value
+         * @param choice
+         * @return boolean
+         */
+        boolean matches(CharSequence value, String choice) {
+            if (trim) {
+                value = StringUtils.trim(value.toString());
+            }
+            return ignoreCase ? StringUtils.equalsIgnoreCase(value.toString(), choice) :
StringUtils.equals(value,
+                choice);
+        }
+    }
+
+    private Test test;
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void initialize(Values constraintAnnotation) {
+        super.initialize(constraintAnnotation);
+        this.test = new Test(constraintAnnotation.payload());
+    }
 
     /**
      * {@inheritDoc}
      */
     @Override
     protected boolean matches(CharSequence value, String choice) {
-        return StringUtils.equals(value, choice);
+        return test.matches(value, choice);
     }
 
     /**
@@ -42,13 +96,33 @@ public class StringValuesValidator exten
      */
     public static class Labeled extends AbstractValuesValidator.Labeled<CharSequence>
{
 
+        private Test test;
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void initialize(Values.Labeled constraintAnnotation) {
+            super.initialize(constraintAnnotation);
+            this.test = new Test(constraintAnnotation.payload());
+        }
+
         /**
          * {@inheritDoc}
          */
         @Override
         protected boolean matches(CharSequence value, String choice) {
-            return StringUtils.equals(value, choice);
+            return test.matches(value, choice);
         }
 
     }
+
+    private static boolean hasPayload(Class<? extends Payload> target, Class<? extends
Payload>[] payload) {
+        for (Class<? extends Payload> p : payload) {
+            if (target.isAssignableFrom(p)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java?rev=1062004&r1=1062003&r2=1062004&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java
Fri Jan 21 20:13:23 2011
@@ -26,6 +26,8 @@ import javax.validation.Validation;
 import javax.validation.Validator;
 import javax.validation.groups.Default;
 
+import org.apache.bval.constraints.dynamic.StringValuesValidator.IgnoreCase;
+import org.apache.bval.constraints.dynamic.StringValuesValidator.Trim;
 import org.apache.bval.constraints.dynamic.Values.Labeled;
 import org.apache.bval.constraints.dynamic.Values.Strategy;
 import org.apache.bval.constraints.dynamic.Values.Rule;
@@ -78,10 +80,55 @@ public class ValuesConstraintValidationT
         ElementType noneOfAnnotationOrConstructor;
     }
 
+    public static class StringBean {
+        @Values({ "a", "b", "c" })
+        String plain;
+        @Values(value = { "a", "b", "c" }, payload = IgnoreCase.class)
+        String ignoreCase;
+        @Values(value = { "a", "b", "c" }, payload = Trim.class)
+        String trim;
+        @Values(value = { "a", "b", "c" }, payload = { IgnoreCase.class, Trim.class })
+        String ignoreCaseTrim;
+        @Values(value = { "a", "b", "c" }, rule = Rule.EXCLUDE)
+        String plainExclude;
+        @Values(value = { "a", "b", "c" }, rule = Rule.EXCLUDE, payload = IgnoreCase.class)
+        String ignoreCaseExclude;
+        @Values(value = { "a", "b", "c" }, rule = Rule.EXCLUDE, payload = Trim.class)
+        String trimExclude;
+        @Values(value = { "a", "b", "c" }, rule = Rule.EXCLUDE, payload = { IgnoreCase.class,
Trim.class })
+        String ignoreCaseTrimExclude;
+
+        @Values.Labeled({ @Item(label = "A", value = "a"), @Item(label = "B", value = "b"),
+            @Item(label = "C", value = "c") })
+        String labeledPlain;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, payload = IgnoreCase.class)
+        String labeledIgnoreCase;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, payload = Trim.class)
+        String labeledTrim;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, payload = { IgnoreCase.class, Trim.class })
+        String labeledIgnoreCaseTrim;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, rule = Rule.EXCLUDE)
+        String labeledPlainExclude;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, rule = Rule.EXCLUDE, payload = IgnoreCase.class)
+        String labeledIgnoreCaseExclude;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, rule = Rule.EXCLUDE, payload = Trim.class)
+        String labeledTrimExclude;
+        @Values.Labeled(value = { @Item(label = "A", value = "a"), @Item(label = "B", value
= "b"),
+            @Item(label = "C", value = "c") }, rule = Rule.EXCLUDE, payload = { IgnoreCase.class,
Trim.class })
+        String labeledIgnoreCaseTrimExclude;
+    }
+
     private Validator validator;
     private DynamicValidatorContext validatorContext;
     private final AnnotationFactory annotationFactory = new AnnotationFactory();
     private TestBean testBean;
+    private StringBean stringBean;
 
     @Before
     public void setup() {
@@ -96,6 +143,7 @@ public class ValuesConstraintValidationT
         validatorContext = factory.usingContext();
         validator = validatorContext.getValidator();
         testBean = new TestBean();
+        stringBean = new StringBean();
     }
 
     @Test
@@ -106,6 +154,48 @@ public class ValuesConstraintValidationT
         assertEquals(0, validator.validate(testBean).size());
         testBean.fooBarOrBaz = "oops";
         assertEquals(1, validator.validate(testBean).size());
+
+        assertNull(stringBean.plain);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.plain = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.plain = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.plain = null;
+
+        assertNull(stringBean.ignoreCase);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCase = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCase = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCase = "z";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCase = null;
+
+        assertNull(stringBean.trim);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trim = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trim = " a ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trim = " z ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.trim = null;
+
+        assertNull(stringBean.ignoreCaseTrim);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = " a ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = " A ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = " z ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrim = null;
     }
 
     @Test
@@ -116,6 +206,54 @@ public class ValuesConstraintValidationT
         assertEquals(0, validator.validate(testBean).size());
         testBean.noneOfFooBarOrBaz = "foo";
         assertEquals(1, validator.validate(testBean).size());
+
+        assertNull(stringBean.plainExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.plainExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.plainExclude = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.plainExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.plainExclude = null;
+
+        assertNull(stringBean.ignoreCaseExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseExclude = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseExclude = null;
+
+        assertNull(stringBean.trimExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trimExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trimExclude = " z ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.trimExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.trimExclude = " a ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.trimExclude = null;
+
+        assertNull(stringBean.ignoreCaseTrimExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = " z ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = " a ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = " A ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.ignoreCaseTrimExclude = null;
     }
 
     @Test
@@ -126,6 +264,48 @@ public class ValuesConstraintValidationT
         assertEquals(0, validator.validate(testBean).size());
         testBean.oneTwoOrThree = "0";
         assertEquals(1, validator.validate(testBean).size());
+
+        assertNull(stringBean.labeledPlain);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledPlain = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledPlain = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledPlain = null;
+
+        assertNull(stringBean.labeledIgnoreCase);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCase = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCase = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCase = "z";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCase = null;
+
+        assertNull(stringBean.labeledTrim);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledTrim = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledTrim = " a ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledTrim = " z ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledTrim = null;
+
+        assertNull(stringBean.labeledIgnoreCaseTrim);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = "a";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = " a ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = " A ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = " z ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrim = null;
     }
 
     @Test
@@ -136,6 +316,50 @@ public class ValuesConstraintValidationT
         assertEquals(0, validator.validate(testBean).size());
         testBean.noneOfOneTwoOrThree = "1";
         assertEquals(1, validator.validate(testBean).size());
+
+        assertNull(stringBean.labeledPlainExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledPlainExclude = "A";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledPlainExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledPlainExclude = null;
+
+        assertNull(stringBean.labeledIgnoreCaseExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseExclude = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseExclude = null;
+
+        assertNull(stringBean.labeledTrimExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledTrimExclude = " z ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledTrimExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledTrimExclude = " a ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledTrimExclude = null;
+
+        assertNull(stringBean.labeledIgnoreCaseTrimExclude);
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = "z";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = " z ";
+        assertEquals(0, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = "A";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = "a";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = " a ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = " A ";
+        assertEquals(1, validator.validate(stringBean).size());
+        stringBean.labeledIgnoreCaseTrimExclude = null;
     }
 
     @Test



Mime
View raw message