incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r962877 - in /incubator/bval/trunk: bval-core/src/main/java/org/apache/bval/routines/ bval-jsr303/src/main/java/org/apache/bval/constraints/ bval-jsr303/src/test/java/org/apache/bval/constraints/
Date Sat, 10 Jul 2010 16:32:43 GMT
Author: carlosvara
Date: Sat Jul 10 16:32:43 2010
New Revision: 962877

URL: http://svn.apache.org/viewvc?rev=962877&view=rev
Log:
BVAL-79: email validation for CharSequence, by Matt Benson

Modified:
    incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/constraints/EmailValidator.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/constraints/EmailValidatorTest.java

Modified: incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java?rev=962877&r1=962876&r2=962877&view=diff
==============================================================================
--- incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java
(original)
+++ incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java
Sat Jul 10 16:32:43 2010
@@ -61,11 +61,10 @@ public class EMailValidationUtils {
   //TODO it would seem to make sense to move or reduce the visibility of this method as it
is more general than email.
   public static boolean isValid(Object value, Pattern aPattern) {
     if (value == null) return true;
-    //TODO could effortlessly validate any CharSequence
-    if (!(value instanceof String)) return false;
-    String string = (String) value;
-    if (string.length() == 0) return true;
-    Matcher m = aPattern.matcher(string);
+    if (!(value instanceof CharSequence)) return false;
+    CharSequence seq = (CharSequence) value;
+    if (seq.length() == 0) return true;
+    Matcher m = aPattern.matcher(seq);
     return m.matches();
   }
 

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/constraints/EmailValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/constraints/EmailValidator.java?rev=962877&r1=962876&r2=962877&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/constraints/EmailValidator.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/constraints/EmailValidator.java
Sat Jul 10 16:32:43 2010
@@ -27,9 +27,9 @@ import javax.validation.ConstraintValida
 /**
  * Description: <br/>
  */
-public class EmailValidator implements ConstraintValidator<Email, String> {
+public class EmailValidator implements ConstraintValidator<Email, CharSequence> {
 
-    public boolean isValid(String value, ConstraintValidatorContext context) {
+    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
         return EMailValidationUtils.isValid(value);
     }
 

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/constraints/EmailValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/constraints/EmailValidatorTest.java?rev=962877&r1=962876&r2=962877&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/constraints/EmailValidatorTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/constraints/EmailValidatorTest.java
Sat Jul 10 16:32:43 2010
@@ -36,12 +36,33 @@ import org.apache.bval.jsr303.example.Cu
  * @since <pre>10/14/2008</pre>
  */
 public class EmailValidatorTest extends TestCase {
+    public static class EmailAddressBuilder {
+        @Email
+        private StringBuilder buffer = new StringBuilder();
+
+        /**
+         * Get the buffer.
+         * @return StringBuilder
+         */
+        public StringBuilder getBuffer() {
+            return buffer;
+        }
+
+    }
+
+    private Validator validator;
+
     public EmailValidatorTest(String name) {
         super(name);
     }
 
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        validator = ApacheValidatorFactory.getDefault().getValidator();
+    }
+
     public void testEmail() {
-        Validator validator = ApacheValidatorFactory.getDefault().getValidator();
         Customer customer = new Customer();
         customer.setCustomerId("id-1");
         customer.setFirstName("Mary");
@@ -57,6 +78,20 @@ public class EmailValidatorTest extends 
         Assert.assertEquals(0, validator.validate(customer).size());
     }
 
+    public void testEmailCharSequence() {
+        EmailAddressBuilder emailAddressBuilder = new EmailAddressBuilder();
+        Assert.assertEquals(0, validator.validate(emailAddressBuilder).size());
+        emailAddressBuilder.getBuffer().append("foo");
+        Assert.assertEquals(1, validator.validate(emailAddressBuilder).size());
+        emailAddressBuilder.getBuffer().append('@');
+        Assert.assertEquals(1, validator.validate(emailAddressBuilder).size());
+        emailAddressBuilder.getBuffer().append("bar");
+        Assert.assertEquals(0, validator.validate(emailAddressBuilder).size());
+        emailAddressBuilder.getBuffer().append('.');
+        Assert.assertEquals(1, validator.validate(emailAddressBuilder).size());
+        emailAddressBuilder.getBuffer().append("baz");
+        Assert.assertEquals(0, validator.validate(emailAddressBuilder).size());
+    }
 
     public static Test suite() {
         return new TestSuite(EmailValidatorTest.class);



Mime
View raw message