1 15 package org.apache.tapestry.valid; 16 17 import org.apache.tapestry.form.IFormComponent; 18 import org.apache.tapestry.valid.EmailValidator; 19 import org.apache.tapestry.valid.ValidationConstraint; 20 import org.apache.tapestry.valid.ValidatorException; 21 22 28 29 public class TestEmailValidator extends BaseValidatorTestCase 30 { 31 private EmailValidator v = new EmailValidator(); 32 33 public void testValidEmail() throws ValidatorException 34 { 35 IFormComponent field = newField(); 36 37 replayControls(); 38 39 Object result = v.toObject(field, "foo@bar.com"); 40 41 assertEquals("foo@bar.com", result); 42 43 verifyControls(); 44 } 45 46 public void testInvalidEmail() 47 { 48 IFormComponent field = newField("email"); 49 50 replayControls(); 51 52 try 53 { 54 v.toObject(field, "fred"); 55 unreachable(); 56 } 57 catch (ValidatorException ex) 58 { 59 assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint()); 60 assertEquals("Invalid email format for email. Format is user@hostname.", ex 61 .getMessage()); 62 } 63 64 verifyControls(); 65 } 66 67 public void testOverrideInvalidEmailFormatMessage() 68 { 69 IFormComponent field = newField("email"); 70 71 replayControls(); 72 73 v 74 .setInvalidEmailFormatMessage("Try a valid e-mail address (for {0}), like ''dick@wad.com.''"); 75 76 try 77 { 78 v.toObject(field, "fred"); 79 unreachable(); 80 } 81 catch (ValidatorException ex) 82 { 83 assertEquals("Try a valid e-mail address (for email), like 'dick@wad.com.'", ex 84 .getMessage()); 85 } 86 87 verifyControls(); 88 } 89 90 public void testTooShort() 91 { 92 IFormComponent field = newField("short"); 93 94 replayControls(); 95 96 v.setMinimumLength(20); 97 98 try 99 { 100 v.toObject(field, "foo@bar.com"); 101 unreachable(); 102 } 103 catch (ValidatorException ex) 104 { 105 assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint()); 106 assertEquals("You must enter at least 20 characters for short.", ex.getMessage()); 107 } 108 109 verifyControls(); 110 } 111 112 public void testOverrideMinimumLengthMessage() 113 { 114 IFormComponent field = newField("short"); 115 116 replayControls(); 117 118 v.setMinimumLength(20); 119 v.setMinimumLengthMessage("E-mail addresses must be at least 20 characters."); 120 121 try 122 { 123 v.toObject(field, "foo@bar.com"); 124 unreachable(); 125 } 126 catch (ValidatorException ex) 127 { 128 assertEquals("E-mail addresses must be at least 20 characters.", ex.getMessage()); 129 } 130 131 verifyControls(); 132 } 133 } | Popular Tags |