001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form.validator; 016 017 import static org.easymock.EasyMock.expect; 018 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.form.FormComponentContributorContext; 022 import org.apache.tapestry.form.IFormComponent; 023 import org.apache.tapestry.form.ValidationMessages; 024 import org.apache.tapestry.json.JSONObject; 025 import org.apache.tapestry.valid.ValidationConstraint; 026 import org.apache.tapestry.valid.ValidationStrings; 027 import org.apache.tapestry.valid.ValidatorException; 028 import org.testng.annotations.Test; 029 import org.testng.annotations.DataProvider; 030 031 /** 032 * Tests for {@link org.apache.tapestry.form.validator.Email}. 033 * 034 * @author Howard Lewis Ship 035 * @since 4.0 036 */ 037 @Test 038 public class TestEmail extends BaseValidatorTestCase 039 { 040 @Test(dataProvider = "validEmails", timeOut = 1000) 041 public void test_OK(String email) throws Exception 042 { 043 IFormComponent field = newField(); 044 ValidationMessages messages = newMessages(); 045 046 replay(); 047 048 new Email().validate(field, messages, email); 049 050 verify(); 051 } 052 053 @Test(dataProvider = "invalidEmails", timeOut = 1000) 054 public void test_Fail(String email) 055 { 056 IFormComponent field = newField("My Email"); 057 ValidationMessages messages = newMessages( 058 null, 059 ValidationStrings.INVALID_EMAIL, 060 new Object[] 061 { "My Email" }, 062 "default message"); 063 064 replay(); 065 066 try 067 { 068 new Email().validate(field, messages, email); 069 unreachable(); 070 } 071 catch (ValidatorException ex) 072 { 073 assertEquals("default message", ex.getMessage()); 074 assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint()); 075 } 076 077 verify(); 078 } 079 080 public void test_Fail_Custom_Message() 081 { 082 IFormComponent field = newField("My Email"); 083 ValidationMessages messages = newMessages( 084 "custom", 085 ValidationStrings.INVALID_EMAIL, 086 new Object[] 087 { "My Email" }, 088 "custom message"); 089 090 replay(); 091 092 try 093 { 094 new Email("message=custom").validate(field, messages, "fred"); 095 unreachable(); 096 } 097 catch (ValidatorException ex) 098 { 099 assertEquals("custom message", ex.getMessage()); 100 assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint()); 101 } 102 103 verify(); 104 } 105 106 public void test_Render_Contribution() 107 { 108 IMarkupWriter writer = newWriter(); 109 IRequestCycle cycle = newCycle(); 110 JSONObject json = new JSONObject(); 111 112 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 113 114 IFormComponent field = newField("Fred", "myfield"); 115 116 expect(context.getProfile()).andReturn(json); 117 118 trainFormatMessage(context, null, ValidationStrings.INVALID_EMAIL, 119 new Object[] { "Fred" }, "default\\message"); 120 121 replay(); 122 123 new Email().renderContribution(writer, cycle, context, field); 124 125 verify(); 126 127 assertEquals("{\"constraints\":{\"myfield\":[[tapestry.form.validation.isEmailAddress,false,true]]}," 128 +"\"myfield\":{\"constraints\":[\"default\\\\message\"]}}", 129 json.toString()); 130 } 131 132 public void test_Render_Contribution_Custom_Message() 133 { 134 IMarkupWriter writer = newWriter(); 135 IRequestCycle cycle = newCycle(); 136 JSONObject json = new JSONObject(); 137 138 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 139 140 IFormComponent field = newField("Fred", "barney"); 141 142 expect(context.getProfile()).andReturn(json); 143 144 trainFormatMessage( 145 context, 146 "custom", 147 ValidationStrings.INVALID_EMAIL, 148 new Object[] 149 { "Fred" }, 150 "custom message"); 151 152 replay(); 153 154 new Email("message=custom").renderContribution(writer, cycle, context, field); 155 156 verify(); 157 158 assertEquals("{\"constraints\":{\"barney\":[[tapestry.form.validation.isEmailAddress,false,true]]}," 159 + "\"barney\":{\"constraints\":[\"custom message\"]}}", 160 json.toString()); 161 } 162 163 @DataProvider(name="validEmails") 164 protected Object[][] getValidEmails() { 165 return new Object[][] { 166 {"hlship@apache.org"}, 167 {"j@apache.org"}, 168 {"jkuhnert@a.org"}, 169 {"J@A.oRg"}, 170 {"foo@example-bar.domain.com"}, 171 {"FOO@EXample-bAr.domain.com"}, 172 {"_foo@example.com"}, 173 {"$user+mailbox_@example-domain.com"}, 174 }; 175 } 176 177 @DataProvider(name="invalidEmails") 178 protected Object[][] getInvalidEmails() { 179 return new Object[][] { 180 {"fred"}, 181 {"foooooooooooooooooooooo"}, 182 {"foooooooooooooooooooooooooooo"}, 183 {"LASKFODSKFO@$#)DJMZCV)TQKALAD"}, 184 {""}, 185 {"aa@foooooooooooooooooooooooooooooooooooooooooooooooooooooooo"}, 186 {"aa@.foooooooooooooooooooooooooooooooooooooooooooooooooooooooo"}, 187 }; 188 } 189 }