You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompositeValidatorTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.vaadin.tests.data.validator;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import static org.junit.Assert.fail;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import com.vaadin.v7.data.Validator;
  9. import com.vaadin.v7.data.validator.LegacyCompositeValidator;
  10. import com.vaadin.v7.data.validator.LegacyCompositeValidator.CombinationMode;
  11. import com.vaadin.v7.data.validator.LegacyEmailValidator;
  12. import com.vaadin.v7.data.validator.LegacyRegexpValidator;
  13. public class CompositeValidatorTest {
  14. LegacyCompositeValidator and = new LegacyCompositeValidator(
  15. CombinationMode.AND, "One validator not valid");
  16. LegacyCompositeValidator or = new LegacyCompositeValidator(
  17. CombinationMode.OR, "No validators are valid");
  18. LegacyEmailValidator email = new LegacyEmailValidator("Faulty email");
  19. LegacyRegexpValidator regex = new LegacyRegexpValidator("@mail.com", false,
  20. "Partial match validator error");
  21. @Before
  22. public void setUp() {
  23. and.addValidator(email);
  24. and.addValidator(regex);
  25. or.addValidator(email);
  26. or.addValidator(regex);
  27. }
  28. @Test
  29. public void testCorrectValue() {
  30. String testString = "user@mail.com";
  31. assertTrue(email.isValid(testString));
  32. assertTrue(regex.isValid(testString));
  33. try {
  34. // notNull.validate(null);
  35. // fail("expected null to fail with an exception");
  36. and.validate(testString);
  37. } catch (Validator.InvalidValueException ex) {
  38. // assertEquals("Null not accepted", ex.getMessage());
  39. fail("And validator should be valid");
  40. }
  41. try {
  42. or.validate(testString);
  43. } catch (Validator.InvalidValueException ex) {
  44. // assertEquals("Null not accepted", ex.getMessage());
  45. fail("And validator should be valid");
  46. }
  47. }
  48. @Test
  49. public void testCorrectRegex() {
  50. String testString = "@mail.com";
  51. assertFalse(testString + " should not validate",
  52. email.isValid(testString));
  53. assertTrue(testString + "should validate", regex.isValid(testString));
  54. try {
  55. // notNull.validate(null);
  56. and.validate(testString);
  57. fail("expected and to fail with an exception");
  58. } catch (Validator.InvalidValueException ex) {
  59. assertEquals("Faulty email", ex.getMessage());
  60. // fail("And validator should be valid");
  61. }
  62. try {
  63. or.validate(testString);
  64. } catch (Validator.InvalidValueException ex) {
  65. // assertEquals("Null not accepted", ex.getMessage());
  66. fail("Or validator should be valid");
  67. }
  68. }
  69. @Test
  70. public void testCorrectEmail() {
  71. String testString = "user@gmail.com";
  72. assertTrue(testString + " should validate", email.isValid(testString));
  73. assertFalse(testString + " should not validate",
  74. regex.isValid(testString));
  75. try {
  76. and.validate(testString);
  77. fail("expected and to fail with an exception");
  78. } catch (Validator.InvalidValueException ex) {
  79. assertEquals("Partial match validator error", ex.getMessage());
  80. }
  81. try {
  82. or.validate(testString);
  83. } catch (Validator.InvalidValueException ex) {
  84. fail("Or validator should be valid");
  85. }
  86. }
  87. @Test
  88. public void testBothFaulty() {
  89. String testString = "gmail.com";
  90. assertFalse(testString + " should not validate",
  91. email.isValid(testString));
  92. assertFalse(testString + " should not validate",
  93. regex.isValid(testString));
  94. try {
  95. and.validate(testString);
  96. fail("expected and to fail with an exception");
  97. } catch (Validator.InvalidValueException ex) {
  98. assertEquals("Faulty email", ex.getMessage());
  99. }
  100. try {
  101. or.validate(testString);
  102. fail("expected or to fail with an exception");
  103. } catch (Validator.InvalidValueException ex) {
  104. assertEquals("No validators are valid", ex.getMessage());
  105. }
  106. }
  107. }