123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package com.vaadin.tests.data.validator;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
-
- import org.junit.Before;
- import org.junit.Test;
-
- import com.vaadin.v7.data.Validator;
- import com.vaadin.v7.data.validator.LegacyCompositeValidator;
- import com.vaadin.v7.data.validator.LegacyCompositeValidator.CombinationMode;
- import com.vaadin.v7.data.validator.LegacyEmailValidator;
- import com.vaadin.v7.data.validator.LegacyRegexpValidator;
-
- public class CompositeValidatorTest {
-
- LegacyCompositeValidator and = new LegacyCompositeValidator(
- CombinationMode.AND, "One validator not valid");
- LegacyCompositeValidator or = new LegacyCompositeValidator(
- CombinationMode.OR, "No validators are valid");
- LegacyEmailValidator email = new LegacyEmailValidator("Faulty email");
- LegacyRegexpValidator regex = new LegacyRegexpValidator("@mail.com", false,
- "Partial match validator error");
-
- @Before
- public void setUp() {
- and.addValidator(email);
- and.addValidator(regex);
-
- or.addValidator(email);
- or.addValidator(regex);
- }
-
- @Test
- public void testCorrectValue() {
- String testString = "user@mail.com";
- assertTrue(email.isValid(testString));
- assertTrue(regex.isValid(testString));
- try {
- // notNull.validate(null);
- // fail("expected null to fail with an exception");
- and.validate(testString);
- } catch (Validator.InvalidValueException ex) {
- // assertEquals("Null not accepted", ex.getMessage());
- fail("And validator should be valid");
- }
- try {
- or.validate(testString);
- } catch (Validator.InvalidValueException ex) {
- // assertEquals("Null not accepted", ex.getMessage());
- fail("And validator should be valid");
- }
- }
-
- @Test
- public void testCorrectRegex() {
-
- String testString = "@mail.com";
- assertFalse(testString + " should not validate",
- email.isValid(testString));
- assertTrue(testString + "should validate", regex.isValid(testString));
- try {
- // notNull.validate(null);
- and.validate(testString);
- fail("expected and to fail with an exception");
- } catch (Validator.InvalidValueException ex) {
- assertEquals("Faulty email", ex.getMessage());
- // fail("And validator should be valid");
- }
- try {
- or.validate(testString);
- } catch (Validator.InvalidValueException ex) {
- // assertEquals("Null not accepted", ex.getMessage());
- fail("Or validator should be valid");
- }
- }
-
- @Test
- public void testCorrectEmail() {
-
- String testString = "user@gmail.com";
-
- assertTrue(testString + " should validate", email.isValid(testString));
- assertFalse(testString + " should not validate",
- regex.isValid(testString));
- try {
- and.validate(testString);
- fail("expected and to fail with an exception");
- } catch (Validator.InvalidValueException ex) {
- assertEquals("Partial match validator error", ex.getMessage());
- }
- try {
- or.validate(testString);
- } catch (Validator.InvalidValueException ex) {
- fail("Or validator should be valid");
- }
- }
-
- @Test
- public void testBothFaulty() {
-
- String testString = "gmail.com";
-
- assertFalse(testString + " should not validate",
- email.isValid(testString));
- assertFalse(testString + " should not validate",
- regex.isValid(testString));
- try {
- and.validate(testString);
- fail("expected and to fail with an exception");
- } catch (Validator.InvalidValueException ex) {
- assertEquals("Faulty email", ex.getMessage());
- }
- try {
- or.validate(testString);
- fail("expected or to fail with an exception");
- } catch (Validator.InvalidValueException ex) {
- assertEquals("No validators are valid", ex.getMessage());
- }
- }
-
- }
|