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.valid;
016    
017    import java.util.Map;
018    
019    import org.apache.tapestry.form.IFormComponent;
020    import org.testng.annotations.Test;
021    
022    /**
023     * Tests for {@link org.apache.tapestry.valid.IntValidator}.
024     * 
025     * @author Howard M. Lewis Ship
026     * @since 4.0
027     */
028    @Test
029    public class TestIntValidator extends BaseValidatorTestCase
030    {
031        public void testToStringNull()
032        {
033            IValidator validator = new IntValidator();
034    
035            assertNull(validator.toString(null, null));
036        }
037    
038        public void testToStringNonZero()
039        {
040            Number number = new Integer(37);
041    
042            IValidator validator = new IntValidator();
043    
044            assertEquals("37", validator.toString(null, number));
045        }
046    
047        public void testToStringZero()
048        {
049            Number number = new Integer(0);
050    
051            IValidator validator = new IntValidator();
052    
053            assertEquals("0", validator.toString(null, number));
054        }
055    
056        public void testToStringZeroAsNull()
057        {
058            Number number = new Integer(0);
059    
060            IValidator validator = new IntValidator("zeroIsNull");
061    
062            assertNull(validator.toString(null, number));
063        }
064    
065        public void testToObjectNull() throws Exception
066        {
067            IValidator validator = new IntValidator();
068    
069            assertNull(validator.toObject(null, ""));
070        }
071    
072        public void testToObjectSucess() throws Exception
073        {
074            IValidator validator = new IntValidator();
075    
076            Number expected = new Integer("-12345");
077    
078            assertEquals(expected, validator.toObject(null, "-12345"));
079        }
080    
081        public void testToObjectFailure()
082        {
083            IFormComponent field = newField("BamBam");
084    
085            replay();
086    
087            IValidator validator = new IntValidator();
088    
089            try
090            {
091                validator.toObject(field, "abcdef");
092                unreachable();
093            }
094            catch (ValidatorException ex)
095            {
096                assertEquals(ex.getMessage(), "BamBam must be a numeric value. ");
097                assertSame(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint());
098            }
099        }
100    
101        public void testToObjectTooSmall()
102        {
103            IFormComponent field = newField("Fred");
104    
105            replay();
106    
107            IValidator validator = new IntValidator("minimum=10");
108    
109            try
110            {
111                validator.toObject(field, "9");
112                unreachable();
113            }
114            catch (ValidatorException ex)
115            {
116                assertEquals("Fred must not be smaller than 10.", ex.getMessage());
117                assertSame(ValidationConstraint.TOO_SMALL, ex.getConstraint());
118            }
119        }
120    
121        public void testToObjectTooLarge()
122        {
123            IFormComponent field = newField("Barney");
124    
125            replay();
126    
127            IValidator validator = new IntValidator("maximum=10");
128    
129            try
130            {
131                validator.toObject(field, "207");
132                unreachable();
133            }
134            catch (ValidatorException ex)
135            {
136                assertEquals("Barney must not be larger than 10.", ex.getMessage());
137                assertSame(ValidationConstraint.TOO_LARGE, ex.getConstraint());
138            }
139        }
140    
141        public void testScriptSymbolsJustFormat()
142        {
143            IFormComponent field = newField("Fred");
144    
145            replay();
146    
147            IntValidator validator = new IntValidator();
148    
149            Map map = validator.buildSymbols(field);
150    
151            assertEquals(1, map.size());
152    
153            assertEquals(map.get("formatMessage"), "Fred must be an integer value. ");
154    
155            verify();
156        }
157    
158        public void testScriptSymbolsRequired()
159        {
160            IFormComponent field = newField("Barney", 2);
161    
162            replay();
163    
164            IntValidator validator = new IntValidator("required");
165    
166            Map map = validator.buildSymbols(field);
167    
168            assertEquals(2, map.size());
169    
170            assertEquals("You must enter a value for Barney.", map.get("requiredMessage"));
171            assertEquals(map.get("formatMessage"), "Barney must be an integer value. ");
172    
173            verify();
174        }
175    
176        public void testScriptSymbolsMinimum()
177        {
178            IFormComponent field = newField("Barney", 2);
179    
180            replay();
181    
182            IntValidator validator = new IntValidator("minimum=5");
183    
184            Map map = validator.buildSymbols(field);
185    
186            assertEquals(4, map.size());
187    
188            assertEquals(map.get("minimum"), new Integer(5));
189            assertNull(map.get("maximum"));
190            assertEquals("Barney must not be smaller than 5.", map.get("rangeMessage"));
191            assertEquals(map.get("formatMessage"), "Barney must be an integer value. ");
192    
193            verify();
194        }
195        
196        public void testScriptSymbolsMaximum()
197        {
198            IFormComponent field = newField("Barney", 2);
199    
200            replay();
201    
202            IntValidator validator = new IntValidator("maximum=5");
203    
204            Map map = validator.buildSymbols(field);
205    
206            assertEquals(4, map.size());
207    
208            assertEquals(new Integer(5), map.get("maximum"));
209            assertNull(map.get("minimum"));
210            assertEquals("Barney must not be larger than 5.", map.get("rangeMessage"));
211            assertEquals(map.get("formatMessage"), "Barney must be an integer value. ");
212    
213            verify();
214        }    
215        
216        public void testScriptSymbolsRange()
217        {
218            IFormComponent field = newField("Barney", 2);
219    
220            replay();
221    
222            IntValidator validator = new IntValidator("maximum=5,minimum=1");
223    
224            Map map = validator.buildSymbols(field);
225    
226            assertEquals(4, map.size());
227    
228            assertEquals(new Integer(1), map.get("minimum"));
229            assertEquals(new Integer(5), map.get("maximum"));
230            assertEquals("Barney must be between 1 and 5.", map.get("rangeMessage"));
231            assertEquals(map.get("formatMessage"), "Barney must be an integer value. ");
232    
233            verify();
234        }
235    }