--- /dev/null
+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"));
+ }
+}
--- /dev/null
+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
+}
--- /dev/null
+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
+ }
+ }
+
+}