]> source.dussan.org Git - vaadin-framework.git/commitdiff
Most validators should accept null or empty string (#10561, #10713). 34/634/1
authorHenri Sara <hesara@vaadin.com>
Mon, 14 Jan 2013 11:22:54 +0000 (13:22 +0200)
committerHenri Sara <hesara@vaadin.com>
Mon, 14 Jan 2013 11:22:54 +0000 (13:22 +0200)
Change-Id: I1e8d6015aefdc184af6383cb22cdf3e13be351cf

server/src/com/vaadin/data/validator/EmailValidator.java
server/src/com/vaadin/data/validator/RangeValidator.java
server/src/com/vaadin/data/validator/RegexpValidator.java
server/tests/src/com/vaadin/tests/data/validator/TestRegexpValidator.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithConverterAndValidator.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithValidator.java [new file with mode: 0644]

index 2217b3cf2c5f392f229980ef8ea257f8ede371a9..aa7a921d51c43bf900683a75834af602b3c61000 100644 (file)
@@ -23,6 +23,12 @@ package com.vaadin.data.validator;
  * See {@link com.vaadin.data.validator.AbstractStringValidator} for more
  * information.
  * 
+ * <p>
+ * An empty string or a null is always accepted - use the required flag on
+ * fields or a separate validator (or override {@link #isValidValue(String)}) to
+ * fail on empty values.
+ * </p>
+ * 
  * @author Vaadin Ltd.
  * @since 5.4
  */
@@ -41,19 +47,4 @@ public class EmailValidator extends RegexpValidator {
                 "^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$",
                 true, errorMessage);
     }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see
-     * com.vaadin.data.validator.AbstractValidator#isValidValue(java.lang.Object
-     * )
-     */
-    @Override
-    protected boolean isValidValue(String value) {
-        if (value == null || value.isEmpty()) {
-            return true;
-        }
-        return super.isValidValue(value);
-    }
 }
index d92e259cf60994a0aab65413ebf56858ff297728..5e82a37b5145e704bdfc7a3cb78c6b81a050effd 100644 (file)
@@ -154,7 +154,8 @@ public class RangeValidator<T extends Comparable> extends AbstractValidator<T> {
      */
     @Override
     protected boolean isValidValue(T value) {
-        if (value == null) {
+        if (value == null
+                || (String.class.equals(getType()) && "".equals(value))) {
             return true;
         }
 
index ea659fcfe27abfb6a06537a327453973954b48f1..f85bce92812bba6d7161fc35e2744d59782890a3 100644 (file)
@@ -30,6 +30,11 @@ import java.util.regex.Pattern;
  * See {@link com.vaadin.data.validator.AbstractStringValidator} for more
  * information.
  * </p>
+ * <p>
+ * An empty string or a null is always accepted - use the required flag on
+ * fields or a separate validator (or override {@link #isValidValue(String)}) to
+ * fail on empty values.
+ * </p>
  * 
  * @author Vaadin Ltd.
  * @since 5.4
@@ -81,6 +86,9 @@ public class RegexpValidator extends AbstractStringValidator {
      */
     @Override
     protected boolean isValidValue(String value) {
+        if (value == null || value.isEmpty()) {
+            return true;
+        }
         if (complete) {
             return getMatcher(value).matches();
         } else {
diff --git a/server/tests/src/com/vaadin/tests/data/validator/TestRegexpValidator.java b/server/tests/src/com/vaadin/tests/data/validator/TestRegexpValidator.java
new file mode 100644 (file)
index 0000000..bcff224
--- /dev/null
@@ -0,0 +1,44 @@
+package com.vaadin.tests.data.validator;
+
+import junit.framework.TestCase;
+
+import com.vaadin.data.validator.RegexpValidator;
+
+public class TestRegexpValidator extends TestCase {
+
+    private RegexpValidator completeValidator = new RegexpValidator("pattern",
+            true, "Complete match validator error");
+    private RegexpValidator partialValidator = new RegexpValidator("pattern",
+            false, "Partial match validator error");
+
+    public void testRegexpValidatorWithNull() {
+        assertTrue(completeValidator.isValid(null));
+        assertTrue(partialValidator.isValid(null));
+    }
+
+    public void testRegexpValidatorWithEmptyString() {
+        assertTrue(completeValidator.isValid(""));
+        assertTrue(partialValidator.isValid(""));
+    }
+
+    public void testCompleteRegexpValidatorWithFaultyString() {
+        assertFalse(completeValidator.isValid("mismatch"));
+        assertFalse(completeValidator.isValid("pattern2"));
+        assertFalse(completeValidator.isValid("1pattern"));
+    }
+
+    public void testCompleteRegexpValidatorWithOkString() {
+        assertTrue(completeValidator.isValid("pattern"));
+    }
+
+    public void testPartialRegexpValidatorWithFaultyString() {
+        assertFalse(partialValidator.isValid("mismatch"));
+    }
+
+    public void testPartialRegexpValidatorWithOkString() {
+        assertTrue(partialValidator.isValid("pattern"));
+        assertTrue(partialValidator.isValid("1pattern"));
+        assertTrue(partialValidator.isValid("pattern2"));
+        assertTrue(partialValidator.isValid("1pattern2"));
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithConverterAndValidator.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithConverterAndValidator.java
new file mode 100644 (file)
index 0000000..89d2172
--- /dev/null
@@ -0,0 +1,53 @@
+package com.vaadin.tests.server.component.textfield;
+
+import junit.framework.TestCase;
+
+import com.vaadin.data.Validator.InvalidValueException;
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.validator.RangeValidator;
+import com.vaadin.tests.data.converter.ConverterFactory.ConvertTo42;
+import com.vaadin.ui.TextField;
+
+public class TextFieldWithConverterAndValidator extends TestCase {
+
+    private TextField field;
+    private ObjectProperty<Integer> property;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        field = new TextField();
+        field.setInvalidAllowed(false);
+    }
+
+    public void testConvert42AndValidator() {
+        property = new ObjectProperty<Integer>(123);
+        field.setConverter(new ConvertTo42());
+        field.setPropertyDataSource(property);
+
+        field.addValidator(new RangeValidator<Integer>("Incorrect value",
+                Integer.class, 42, 42));
+
+        // succeeds
+        field.setValue("a");
+        // succeeds
+        field.setValue("42");
+        // succeeds - no validation
+        property.setValue(42);
+
+        // nulls
+
+        // fails
+        try {
+            property.setValue(null);
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+        // succeeds
+        field.setValue(null);
+    }
+
+    // TODO test converter changing value to null with validator
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithValidator.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithValidator.java
new file mode 100644 (file)
index 0000000..2a79d1f
--- /dev/null
@@ -0,0 +1,172 @@
+package com.vaadin.tests.server.component.textfield;
+
+import junit.framework.TestCase;
+
+import com.vaadin.data.Validator;
+import com.vaadin.data.Validator.InvalidValueException;
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.validator.EmailValidator;
+import com.vaadin.data.validator.RegexpValidator;
+import com.vaadin.data.validator.StringLengthValidator;
+import com.vaadin.ui.TextField;
+
+public class TextFieldWithValidator extends TestCase {
+
+    private TextField field;
+    private ObjectProperty<String> property;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        field = new TextField();
+        field.setInvalidAllowed(false);
+        property = new ObjectProperty<String>("original");
+        field.setPropertyDataSource(property);
+    }
+
+    public void testMultipleValidators() {
+        field.addValidator(new StringLengthValidator(
+                "Length not between 1 and 3", 1, 3, false));
+        field.addValidator(new StringLengthValidator(
+                "Length not between 2 and 4", 2, 4, false));
+
+        // fails
+        try {
+            field.setValue("a");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+        // succeeds
+        field.setValue("ab");
+        // fails
+        try {
+            field.setValue("abcd");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+    }
+
+    public void testRemoveValidator() {
+        Validator validator1 = new StringLengthValidator(
+                "Length not between 1 and 3", 1, 3, false);
+        Validator validator2 = new StringLengthValidator(
+                "Length not between 2 and 4", 2, 4, false);
+
+        field.addValidator(validator1);
+        field.addValidator(validator2);
+        field.removeValidator(validator1);
+
+        // fails
+        try {
+            field.setValue("a");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+        // succeeds
+        field.setValue("ab");
+        // succeeds
+        field.setValue("abcd");
+    }
+
+    public void testRemoveAllValidators() {
+        Validator validator1 = new StringLengthValidator(
+                "Length not between 1 and 3", 1, 3, false);
+        Validator validator2 = new StringLengthValidator(
+                "Length not between 2 and 4", 2, 4, false);
+
+        field.addValidator(validator1);
+        field.addValidator(validator2);
+        field.removeAllValidators();
+
+        // all should succeed now
+        field.setValue("a");
+        field.setValue("ab");
+        field.setValue("abcd");
+    }
+
+    public void testEmailValidator() {
+        field.addValidator(new EmailValidator("Invalid e-mail address"));
+
+        // not required
+
+        field.setRequired(false);
+        // succeeds
+        field.setValue("");
+        // needed as required flag not checked by setValue()
+        field.validate();
+        // succeeds
+        field.setValue(null);
+        // needed as required flag not checked by setValue()
+        field.validate();
+        // succeeds
+        field.setValue("test@example.com");
+        // fails
+        try {
+            field.setValue("invalid e-mail");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+
+        // required
+
+        field.setRequired(true);
+        // fails
+        try {
+            field.setValue("");
+            // needed as required flag not checked by setValue()
+            field.validate();
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+        // fails
+        try {
+            field.setValue(null);
+            // needed as required flag not checked by setValue()
+            field.validate();
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+        // succeeds
+        field.setValue("test@example.com");
+        // fails
+        try {
+            field.setValue("invalid e-mail");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+    }
+
+    public void testRegexpValidator() {
+        field.addValidator(new RegexpValidator("pattern", true,
+                "Validation failed"));
+        field.setRequired(false);
+
+        // succeeds
+        field.setValue("");
+        // needed as required flag not checked by setValue()
+        field.validate();
+        // succeeds
+        field.setValue(null);
+        // needed as required flag not checked by setValue()
+        field.validate();
+        // succeeds
+        field.setValue("pattern");
+
+        // fails
+        try {
+            field.setValue("mismatch");
+            fail();
+        } catch (InvalidValueException e) {
+            // should fail
+        }
+    }
+
+}