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 the {@link StringValidator}class.
023     * 
024     * @author Howard Lewis Ship
025     * @since 1.0.8
026     */
027    @Test(sequential=true)
028    public class TestStringValidator extends BaseValidatorTestCase
029    {
030        private StringValidator v = new StringValidator();
031    
032        @AfterMethod
033        public void resetValidator()
034        {
035            v.setMinimumLength(-1);
036            v.setMinimumLengthMessage(null);
037            v.setRequired(false);
038            v.setRequiredMessage(null);
039        }
040        
041        
042        public void testToString()
043        {
044            IFormComponent field = newField();
045    
046            replay();
047    
048            String in = "Foo";
049            String out = v.toString(field, in);
050    
051            assertEquals(in, out);
052    
053            verify();
054        }
055    
056        public void testToStringNull()
057        {
058            IFormComponent field = newField();
059    
060            replay();
061    
062            String out = v.toString(field, null);
063    
064            assertNull(out);
065    
066            verify();
067        }
068    
069        public void testToObjectRequiredFail()
070        {
071            IFormComponent field = newField("requiredField");
072    
073            replay();
074    
075            v.setRequired(true);
076    
077            try
078            {
079                v.toObject(field, "");
080    
081                unreachable();
082            }
083            catch (ValidatorException ex)
084            {
085                assertEquals("You must enter a value for requiredField.", ex.getMessage());
086                assertEquals(ValidationConstraint.REQUIRED, ex.getConstraint());
087            }
088    
089            verify();
090        }
091    
092        public void testOverrideRequiredMessage()
093        {
094            IFormComponent field = newField("overrideMessage");
095    
096            replay();
097    
098            v.setRequired(true);
099            v.setRequiredMessage("Gimme a value for {0} you bastard.");
100    
101            try
102            {
103                v.toObject(field, "");
104            }
105            catch (ValidatorException ex)
106            {
107                assertEquals("Gimme a value for overrideMessage you bastard.", ex.getMessage());
108            }
109    
110            verify();
111        }
112    
113        public void testToObjectRequiredPass() throws ValidatorException
114        {
115            IFormComponent field = newField();
116    
117            replay();
118            
119            v.setRequired(true);
120            
121            Object result = v.toObject(field, "stuff");
122    
123            assertEquals("stuff", result, "Result.");
124    
125            verify();
126        }
127    
128        public void testToObjectMinimumFail()
129        {
130            IFormComponent field = newField("minimumLength");
131    
132            replay();
133    
134            v.setMinimumLength(10);
135    
136            try
137            {
138                v.toObject(field, "abc");
139    
140                unreachable();
141            }
142            catch (ValidatorException ex)
143            {
144                assertEquals("You must enter at least 10 characters for minimumLength.", ex
145                        .getMessage());
146                assertEquals(ValidationConstraint.MINIMUM_WIDTH, ex.getConstraint());
147            }
148    
149            verify();
150        }
151    
152        public void testOverrideMinimumMessage()
153        {
154            IFormComponent field = newField("overrideMessage");
155    
156            replay();
157    
158            v.setMinimumLength(10);
159            v
160                    .setMinimumLengthMessage("You really think less than 10 characters for {0} is gonna cut it?");
161    
162            try
163            {
164                v.toObject(field, "");
165            }
166            catch (ValidatorException ex)
167            {
168                assertEquals(
169                        "You really think less than 10 characters for overrideMessage is gonna cut it?",
170                        ex.getMessage());
171            }
172        }
173    
174        public void testToObjectMinimumPass() throws ValidatorException
175        {
176            IFormComponent field = newField();
177    
178            replay();
179    
180            v.setMinimumLength(10);
181    
182            String in = "ambidexterous";
183    
184            Object out = v.toObject(field, in);
185    
186            assertEquals(in, out, "Result");
187    
188            verify();
189        }
190    
191        /**
192         * An empty string is not subject to the minimum length constraint.
193         */
194    
195        public void testToObjectMinimumNull() throws ValidatorException
196        {
197            IFormComponent field = newField();
198    
199            replay();
200    
201            v.setMinimumLength(10);
202    
203            String in = "";
204    
205            Object out = v.toObject(field, in);
206    
207            assertNull(out);
208    
209            verify();
210        }
211    }