KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > TestIntValidator


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.valid;
16
17 import java.util.Map JavaDoc;
18
19 import org.apache.tapestry.form.IFormComponent;
20
21 /**
22  * Tests for {@link org.apache.tapestry.valid.IntValidator}.
23  *
24  * @author Howard M. Lewis Ship
25  * @since 4.0
26  */

27 public class TestIntValidator extends BaseValidatorTestCase
28 {
29     public void testToStringNull()
30     {
31         IValidator validator = new IntValidator();
32
33         assertNull(validator.toString(null, null));
34     }
35
36     public void testToStringNonZero()
37     {
38         Number JavaDoc number = new Integer JavaDoc(37);
39
40         IValidator validator = new IntValidator();
41
42         assertEquals("37", validator.toString(null, number));
43     }
44
45     public void testToStringZero()
46     {
47         Number JavaDoc number = new Integer JavaDoc(0);
48
49         IValidator validator = new IntValidator();
50
51         assertEquals("0", validator.toString(null, number));
52     }
53
54     public void testToStringZeroAsNull()
55     {
56         Number JavaDoc number = new Integer JavaDoc(0);
57
58         IValidator validator = new IntValidator("zeroIsNull");
59
60         assertNull(validator.toString(null, number));
61     }
62
63     public void testToObjectNull() throws Exception JavaDoc
64     {
65         IValidator validator = new IntValidator();
66
67         assertNull(validator.toObject(null, ""));
68     }
69
70     public void testToObjectSucess() throws Exception JavaDoc
71     {
72         IValidator validator = new IntValidator();
73
74         Number JavaDoc expected = new Integer JavaDoc("-12345");
75
76         assertEquals(expected, validator.toObject(null, "-12345"));
77     }
78
79     public void testToObjectFailure()
80     {
81         IFormComponent field = newField("BamBam");
82
83         replayControls();
84
85         IValidator validator = new IntValidator();
86
87         try
88         {
89             validator.toObject(field, "abcdef");
90             unreachable();
91         }
92         catch (ValidatorException ex)
93         {
94             assertEquals("BamBam must be a numeric value.", ex.getMessage());
95             assertSame(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint());
96         }
97     }
98
99     public void testToObjectTooSmall()
100     {
101         IFormComponent field = newField("Fred");
102
103         replayControls();
104
105         IValidator validator = new IntValidator("minimum=10");
106
107         try
108         {
109             validator.toObject(field, "9");
110             unreachable();
111         }
112         catch (ValidatorException ex)
113         {
114             assertEquals("Fred must not be smaller than 10.", ex.getMessage());
115             assertSame(ValidationConstraint.TOO_SMALL, ex.getConstraint());
116         }
117     }
118
119     public void testToObjectTooLarge()
120     {
121         IFormComponent field = newField("Barney");
122
123         replayControls();
124
125         IValidator validator = new IntValidator("maximum=10");
126
127         try
128         {
129             validator.toObject(field, "207");
130             unreachable();
131         }
132         catch (ValidatorException ex)
133         {
134             assertEquals("Barney must not be larger than 10.", ex.getMessage());
135             assertSame(ValidationConstraint.TOO_LARGE, ex.getConstraint());
136         }
137     }
138
139     public void testScriptSymbolsJustFormat()
140     {
141         IFormComponent field = newField("Fred");
142
143         replayControls();
144
145         IntValidator validator = new IntValidator();
146
147         Map JavaDoc map = validator.buildSymbols(field);
148
149         assertEquals(1, map.size());
150
151         assertEquals("Fred must be an integer value.", map.get("formatMessage"));
152
153         verifyControls();
154     }
155
156     public void testScriptSymbolsRequired()
157     {
158         IFormComponent field = newField("Barney", 2);
159
160         replayControls();
161
162         IntValidator validator = new IntValidator("required");
163
164         Map JavaDoc map = validator.buildSymbols(field);
165
166         assertEquals(2, map.size());
167
168         assertEquals("You must enter a value for Barney.", map.get("requiredMessage"));
169         assertEquals("Barney must be an integer value.", map.get("formatMessage"));
170
171         verifyControls();
172     }
173
174     public void testScriptSymbolsMinimum()
175     {
176         IFormComponent field = newField("Barney", 2);
177
178         replayControls();
179
180         IntValidator validator = new IntValidator("minimum=5");
181
182         Map JavaDoc map = validator.buildSymbols(field);
183
184         assertEquals(4, map.size());
185
186         assertEquals(new Integer JavaDoc(5), map.get("minimum"));
187         assertNull(map.get("maximum"));
188         assertEquals("Barney must not be smaller than 5.", map.get("rangeMessage"));
189         assertEquals("Barney must be an integer value.", map.get("formatMessage"));
190
191         verifyControls();
192     }
193     
194     public void testScriptSymbolsMaximum()
195     {
196         IFormComponent field = newField("Barney", 2);
197
198         replayControls();
199
200         IntValidator validator = new IntValidator("maximum=5");
201
202         Map JavaDoc map = validator.buildSymbols(field);
203
204         assertEquals(4, map.size());
205
206         assertEquals(new Integer JavaDoc(5), map.get("maximum"));
207         assertNull(map.get("minimum"));
208         assertEquals("Barney must not be larger than 5.", map.get("rangeMessage"));
209         assertEquals("Barney must be an integer value.", map.get("formatMessage"));
210
211         verifyControls();
212     }
213     
214     public void testScriptSymbolsRange()
215     {
216         IFormComponent field = newField("Barney", 2);
217
218         replayControls();
219
220         IntValidator validator = new IntValidator("maximum=5,minimum=1");
221
222         Map JavaDoc map = validator.buildSymbols(field);
223
224         assertEquals(4, map.size());
225
226         assertEquals(new Integer JavaDoc(1), map.get("minimum"));
227         assertEquals(new Integer JavaDoc(5), map.get("maximum"));
228         assertEquals("Barney must be between 1 and 5.", map.get("rangeMessage"));
229         assertEquals("Barney must be an integer value.", map.get("formatMessage"));
230
231         verifyControls();
232     }
233 }
Popular Tags