1 15 package org.apache.tapestry.form.validator; 16 17 import org.apache.tapestry.IMarkupWriter; 18 import org.apache.tapestry.IRequestCycle; 19 import org.apache.tapestry.form.FormComponentContributorContext; 20 import org.apache.tapestry.form.IFormComponent; 21 import org.apache.tapestry.form.ValidationMessages; 22 import org.apache.tapestry.util.RegexpMatcher; 23 import org.apache.tapestry.valid.ValidationConstraint; 24 import org.apache.tapestry.valid.ValidationStrings; 25 import org.apache.tapestry.valid.ValidatorException; 26 import org.easymock.MockControl; 27 28 34 public class TestEmail extends BaseValidatorTestCase 35 { 36 public void testOK() throws Exception 37 { 38 IFormComponent field = newField(); 39 ValidationMessages messages = newMessages(); 40 41 replayControls(); 42 43 new Email().validate(field, messages, "hlship@apache.org"); 44 45 verifyControls(); 46 } 47 48 public void testFail() 49 { 50 IFormComponent field = newField("My Email"); 51 ValidationMessages messages = newMessages( 52 null, 53 ValidationStrings.INVALID_EMAIL, 54 new Object [] 55 { "My Email" }, 56 "default message"); 57 58 replayControls(); 59 60 try 61 { 62 new Email().validate(field, messages, "fred"); 63 unreachable(); 64 } 65 catch (ValidatorException ex) 66 { 67 assertEquals("default message", ex.getMessage()); 68 assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint()); 69 } 70 71 verifyControls(); 72 } 73 74 public void testFailCustomMessage() 75 { 76 IFormComponent field = newField("My Email"); 77 ValidationMessages messages = newMessages( 78 "custom", 79 ValidationStrings.INVALID_EMAIL, 80 new Object [] 81 { "My Email" }, 82 "custom message"); 83 84 replayControls(); 85 86 try 87 { 88 new Email("message=custom").validate(field, messages, "fred"); 89 unreachable(); 90 } 91 catch (ValidatorException ex) 92 { 93 assertEquals("custom message", ex.getMessage()); 94 assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint()); 95 } 96 97 verifyControls(); 98 } 99 100 public void testRenderContribution() 101 { 102 String pattern = new RegexpMatcher().getEscapedPatternString(Email.PATTERN); 103 104 IMarkupWriter writer = newWriter(); 105 IRequestCycle cycle = newCycle(); 106 107 MockControl contextc = newControl(FormComponentContributorContext.class); 108 FormComponentContributorContext context = (FormComponentContributorContext) contextc 109 .getMock(); 110 111 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js"); 112 113 IFormComponent field = newField("Fred"); 114 115 trainFormatMessage(contextc, context, null, ValidationStrings.INVALID_EMAIL, new Object [] 116 { "Fred" }, "default message"); 117 118 context.getFieldDOM(); 119 contextc.setReturnValue("document.fred.barney"); 120 121 context 122 .addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '" 123 + pattern + "', 'default message'); }"); 124 125 replayControls(); 126 127 new Email().renderContribution(writer, cycle, context, field); 128 129 verifyControls(); 130 } 131 132 public void testRenderContributionCustomMessage() 133 { 134 135 String pattern = new RegexpMatcher().getEscapedPatternString(Email.PATTERN); 136 137 IMarkupWriter writer = newWriter(); 138 IRequestCycle cycle = newCycle(); 139 140 MockControl contextc = newControl(FormComponentContributorContext.class); 141 FormComponentContributorContext context = (FormComponentContributorContext) contextc 142 .getMock(); 143 144 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js"); 145 146 IFormComponent field = newField("Fred"); 147 148 trainFormatMessage( 149 contextc, 150 context, 151 "custom", 152 ValidationStrings.INVALID_EMAIL, 153 new Object [] 154 { "Fred" }, 155 "custom message"); 156 157 context.getFieldDOM(); 158 contextc.setReturnValue("document.fred.barney"); 159 160 context 161 .addSubmitListener("function(event) { validate_regexp(event, document.fred.barney, '" 162 + pattern + "', 'custom message'); }"); 163 164 replayControls(); 165 166 new Email("message=custom").renderContribution(writer, cycle, context, field); 167 168 verifyControls(); 169 170 } 171 } 172 | Popular Tags |