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 static org.easymock.EasyMock.expect;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.Location;
021    import org.apache.tapestry.form.IFormComponent;
022    import org.testng.annotations.AfterMethod;
023    import org.testng.annotations.Test;
024    
025    /**
026     * Test cases for PatternValidator.
027     * 
028     * @author Harish Krishnaswamy
029     * @since 3.0
030     */
031    @Test(sequential=true)
032    public class TestPatternValidator extends BaseValidatorTestCase
033    {
034        PatternValidator pv = new PatternValidator();
035    
036        @AfterMethod
037        public void reset()
038        {
039            pv.setClientScriptingEnabled(false);
040            pv.setPatternDelegate(null);
041            pv.setPatternNotMatchedMessage(null);
042            pv.setPatternString("");
043            pv.setRequired(false);
044            pv.setRequiredMessage(null);
045        }
046        
047        private void positiveTest(IFormComponent field, String input) throws ValidatorException
048        {
049            Object result = pv.toObject(field, input);
050            assertEquals(input, result);
051        }
052    
053        public void testFulfillingPatterns() throws ValidatorException
054        {
055            IFormComponent field = newField();
056    
057            pv.setPatternString("foo|foot");
058            positiveTest(field, "xfooty");
059    
060            pv.setPatternString("^(\\d{5}(-\\d{4})?)$");
061            positiveTest(field, "06514");
062            positiveTest(field, "06514-3149");
063        }
064    
065        private void assertValidatorException(ValidatorException e)
066        {
067            assertEquals(ValidationConstraint.PATTERN_MISMATCH, e.getConstraint());
068            assertEquals("PatternField does not fulfill the required pattern " + pv.getPatternString()
069                    + ".", e.getMessage());
070        }
071    
072        private void negativeTest(String input)
073        {
074            IFormComponent field = newField("PatternField");
075    
076            replay();
077    
078            try
079            {
080                pv.toObject(field, input);
081                unreachable();
082            }
083            catch (ValidatorException e)
084            {
085                assertValidatorException(e);
086            }
087    
088            verify();
089        }
090    
091        public void testUnfulfillingPatterns()
092        {
093            pv.setPatternString("foo|foot");
094            negativeTest("fot");
095    
096            pv.setPatternString("^(\\d{5}(-\\d{4})?)$");
097            negativeTest("065143");
098            negativeTest("06514-314");
099        }
100    
101        public void testMalformedPattern() throws ValidatorException
102        {
103            Location l = fabricateLocation(11);
104            
105            IFormComponent field = newMock(IFormComponent.class);
106    
107            expect(field.getDisplayName()).andReturn("badPattern");
108    
109            expect(field.getLocation()).andReturn(l);
110    
111            replay();
112    
113            pv.setPatternString("^(\\d{5}(-\\d{4})?$");
114    
115            try
116            {
117                pv.toObject(field, "06514");
118                unreachable();
119            }
120            catch (ApplicationRuntimeException e)
121            {
122                checkException(e, "Unable to match pattern " + pv.getPatternString()
123                        + " for field badPattern.");
124                assertSame(l, e.getLocation());
125            }
126    
127            verify();
128        }
129    
130        public void testOverridePatternNotMatchedMessage()
131        {
132            IFormComponent field = newField("PatternField");
133            replay();
134    
135            pv.setPatternNotMatchedMessage("Field: {0}, Pattern: {1}, you figure!");
136            pv.setPatternString("^(\\d{5}(-\\d{4})?)$");
137    
138            try
139            {
140                pv.toObject(field, "xyz");
141                unreachable();
142            }
143            catch (ValidatorException e)
144            {
145                assertEquals(ValidationConstraint.PATTERN_MISMATCH, e.getConstraint());
146                assertEquals("Field: PatternField, Pattern: ^(\\d{5}(-\\d{4})?)$, you figure!", e
147                        .getMessage());
148            }
149    
150            verify();
151        }
152    
153        public void testOverridePatternMatcher()
154        {
155            class MatcherDelegate implements PatternDelegate
156            {
157                public boolean contains(String value, String patternString)
158                {
159                    return false;
160                }
161    
162                public String getEscapedPatternString(String patternString)
163                {
164                    return null;
165                }
166            }
167    
168            pv.setPatternDelegate(new MatcherDelegate());
169            pv.setPatternString("^(\\d{5}(-\\d{4})?)$");
170            negativeTest("06514-3149");
171    
172            assertNull(pv.getEscapedPatternString());
173        }
174    
175        public void testGetEscapedPatternString()
176        {
177            pv.setPatternString("^(\\d{5}(-\\d{4})?)$");
178            assertEquals("\\^\\(\\\\d\\{5\\}\\(\\-\\\\d\\{4\\}\\)\\?\\)\\$", pv
179                    .getEscapedPatternString());
180        }
181    
182    }