001    // Copyright 2004, 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.valid;
016    
017    import org.apache.tapestry.form.IFormComponent;
018    import org.testng.annotations.AfterMethod;
019    import org.testng.annotations.Test;
020    
021    /**
022     * Tests for {@link org.apache.tapestry.valid.EmailValidator}.
023     * 
024     * @author Howard Lewis Ship
025     * @since 3.0
026     */
027    @Test(sequential=true)
028    public class TestEmailValidator extends BaseValidatorTestCase
029    {
030        private EmailValidator v = new EmailValidator();
031    
032        @AfterMethod
033        public void reset()
034        {
035            v.setClientScriptingEnabled(false);
036            v.setInvalidEmailFormatMessage(null);
037            v.setMinimumLength(-1);
038            v.setMinimumLengthMessage(null);
039            v.setRequired(false);
040            v.setRequiredMessage(null);
041        }
042        
043        public void testValidEmail() throws ValidatorException
044        {
045            IFormComponent field = newField();
046    
047            replay();
048    
049            Object result = v.toObject(field, "foo@bar.com");
050    
051            assertEquals("foo@bar.com", result);
052    
053            verify();
054        }
055    
056        public void testInvalidEmail()
057        {
058            IFormComponent field = newField("email");
059    
060            replay();
061    
062            try
063            {
064                v.toObject(field, "fred");
065                unreachable();
066            }
067            catch (ValidatorException ex)
068            {
069                assertEquals(ValidationConstraint.EMAIL_FORMAT, ex.getConstraint());
070                assertEquals("Invalid email format for email.  Format is user@hostname.", ex
071                        .getMessage());
072            }
073    
074            verify();
075        }
076    
077        public void testOverrideInvalidEmailFormatMessage()
078        {
079            IFormComponent field = newField("email");
080    
081            replay();
082    
083            v
084                    .setInvalidEmailFormatMessage("Try a valid e-mail address (for {0}), like ''dick@wad.com.''");
085    
086            try
087            {
088                v.toObject(field, "fred");
089                unreachable();
090            }
091            catch (ValidatorException ex)
092            {
093                assertEquals("Try a valid e-mail address (for email), like 'dick@wad.com.'", ex
094                        .getMessage());
095            }
096    
097            verify();
098        }
099    
100        public void testTooShort()
101        {
102            IFormComponent field = newField("short");
103    
104            replay();
105    
106            v.setMinimumLength(20);
107    
108            try
109            {
110                v.toObject(field, "foo@bar.com");
111                unreachable();
112            }
113            catch (ValidatorException ex)
114            {
115                assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
116                assertEquals("You must enter at least 20 characters for short.", ex.getMessage());
117            }
118    
119            verify();
120        }
121    
122        public void testOverrideMinimumLengthMessage()
123        {
124            IFormComponent field = newField("short");
125    
126            replay();
127    
128            v.setMinimumLength(20);
129            v.setMinimumLengthMessage("E-mail addresses must be at least 20 characters.");
130    
131            try
132            {
133                v.toObject(field, "foo@bar.com");
134                unreachable();
135            }
136            catch (ValidatorException ex)
137            {
138                assertEquals("E-mail addresses must be at least 20 characters.", ex.getMessage());
139            }
140    
141            verify();
142        }
143    }