summaryrefslogtreecommitdiffstats
path: root/server/tests/src/com/vaadin
diff options
context:
space:
mode:
authorHenri Sara <hesara@vaadin.com>2013-01-14 13:22:54 +0200
committerHenri Sara <hesara@vaadin.com>2013-01-14 13:22:54 +0200
commit1f3656e0dcdd59c323a9ab7d9b962e42250f0aa3 (patch)
tree2e24fc8c53c7696a5d8af297fac5edeebd2bb61b /server/tests/src/com/vaadin
parentcc9cc89865d7378a06497fa4ec08c30311b9f83a (diff)
downloadvaadin-framework-1f3656e0dcdd59c323a9ab7d9b962e42250f0aa3.tar.gz
vaadin-framework-1f3656e0dcdd59c323a9ab7d9b962e42250f0aa3.zip
Most validators should accept null or empty string (#10561, #10713).
Change-Id: I1e8d6015aefdc184af6383cb22cdf3e13be351cf
Diffstat (limited to 'server/tests/src/com/vaadin')
-rw-r--r--server/tests/src/com/vaadin/tests/data/validator/TestRegexpValidator.java44
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithConverterAndValidator.java53
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithValidator.java172
3 files changed, 269 insertions, 0 deletions
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
index 0000000000..bcff224451
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/validator/TestRegexpValidator.java
@@ -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
index 0000000000..89d21728e4
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithConverterAndValidator.java
@@ -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
index 0000000000..2a79d1fe27
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldWithValidator.java
@@ -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
+ }
+ }
+
+}