KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2004, 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.math.BigDecimal JavaDoc;
18 import java.math.BigInteger JavaDoc;
19
20 import org.apache.tapestry.form.IFormComponent;
21 import org.apache.tapestry.valid.NumberValidator;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidatorException;
24
25 /**
26  * Test the {@link NumberValidator}.
27  *
28  * @author Howard Lewis Ship
29  * @since 1.0.8
30  */

31
32 public class TestNumberValidator extends BaseValidatorTestCase
33 {
34     private NumberValidator v = new NumberValidator();
35
36     private void testPassThru(Class JavaDoc valueTypeClass, Number JavaDoc input) throws ValidatorException
37     {
38         IFormComponent field = newField();
39
40         replayControls();
41
42         testPassThru(field, valueTypeClass, input);
43
44         verifyControls();
45     }
46
47     private void testPassThru(IFormComponent field, Class JavaDoc valueTypeClass, Number JavaDoc input)
48             throws ValidatorException
49     {
50         v.setValueTypeClass(valueTypeClass);
51
52         String JavaDoc s = v.toString(field, input);
53
54         Object JavaDoc o = v.toObject(field, s);
55
56         assertEquals("Input and output.", input, o);
57     }
58
59     public void testShort() throws ValidatorException
60     {
61         testPassThru(Short JavaDoc.class, new Short JavaDoc((short) 1000));
62     }
63
64     public void testInteger() throws ValidatorException
65     {
66         testPassThru(Integer JavaDoc.class, new Integer JavaDoc(373));
67     }
68
69     public void testByte() throws ValidatorException
70     {
71         testPassThru(Byte JavaDoc.class, new Byte JavaDoc((byte) 131));
72     }
73
74     public void testFloat() throws ValidatorException
75     {
76         testPassThru(Float JavaDoc.class, new Float JavaDoc(3.1415));
77     }
78
79     public void testDouble() throws ValidatorException
80     {
81         testPassThru(Double JavaDoc.class, new Double JavaDoc(348348.484854848));
82     }
83
84     public void testLong() throws ValidatorException
85     {
86         testPassThru(Long JavaDoc.class, new Long JavaDoc(37373218723l));
87     }
88
89     public void testInRange() throws ValidatorException
90     {
91         v.setMinimum(new Integer JavaDoc(100));
92         v.setMaximum(new Integer JavaDoc(200));
93
94         testPassThru(Integer JavaDoc.class, new Integer JavaDoc(150));
95     }
96
97     public void testUnderMinimum()
98     {
99         IFormComponent field = newField("testUnderMinimum");
100
101         replayControls();
102
103         v.setMinimum(new Integer JavaDoc(100));
104         v.setMaximum(new Integer JavaDoc(200));
105
106         try
107         {
108             testPassThru(field, Integer JavaDoc.class, new Integer JavaDoc(50));
109
110             unreachable();
111         }
112         catch (ValidatorException ex)
113         {
114             assertEquals("testUnderMinimum must not be smaller than 100.", ex.getMessage());
115             assertEquals(ValidationConstraint.TOO_SMALL, ex.getConstraint());
116         }
117
118         verifyControls();
119     }
120
121     public void testOverrideNumberTooSmallMessage()
122     {
123         IFormComponent field = newField("underMinimum");
124
125         replayControls();
126
127         v.setMinimum(new Integer JavaDoc(100));
128         v.setNumberTooSmallMessage("Anything under 100 for {0} is worth jack.");
129
130         try
131         {
132             testPassThru(field, Integer JavaDoc.class, new Integer JavaDoc(50));
133             unreachable();
134         }
135         catch (ValidatorException ex)
136         {
137             assertEquals("Anything under 100 for underMinimum is worth jack.", ex.getMessage());
138         }
139
140         verifyControls();
141     }
142
143     public void testOverMaximum()
144     {
145         IFormComponent field = newField("overMaximum");
146
147         replayControls();
148
149         v.setMinimum(new Integer JavaDoc(100));
150         v.setMaximum(new Integer JavaDoc(200));
151
152         try
153         {
154             testPassThru(field, Integer JavaDoc.class, new Integer JavaDoc(250));
155
156             unreachable();
157         }
158         catch (ValidatorException ex)
159         {
160             assertEquals("overMaximum must not be larger than 200.", ex.getMessage());
161             assertEquals(ValidationConstraint.TOO_LARGE, ex.getConstraint());
162         }
163
164         verifyControls();
165     }
166
167     public void testOverrideNumberTooLargeMessage()
168     {
169         IFormComponent field = newField("overMaximum");
170
171         replayControls();
172
173         v.setMaximum(new Integer JavaDoc(200));
174         v.setNumberTooLargeMessage("You think I want a value larger than {1} for {0}?");
175
176         try
177         {
178             testPassThru(field, Integer JavaDoc.class, new Integer JavaDoc(1000));
179             unreachable();
180         }
181         catch (ValidatorException ex)
182         {
183             assertEquals("You think I want a value larger than 200 for overMaximum?", ex
184                     .getMessage());
185         }
186
187         verifyControls();
188     }
189
190     public void testInvalidFormat()
191     {
192         v.setValueTypeClass(Integer JavaDoc.class);
193         IFormComponent field = newField("invalidFormat");
194
195         replayControls();
196
197         try
198         {
199             v.toObject(field, "xyz");
200             unreachable();
201         }
202         catch (ValidatorException ex)
203         {
204             assertEquals("invalidFormat must be a numeric value.", ex.getMessage());
205             assertEquals(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint());
206         }
207
208         verifyControls();
209     }
210
211     public void testOverrideInvalidNumericFormatMessage()
212     {
213         v.setValueTypeClass(Integer JavaDoc.class);
214         v.setInvalidNumericFormatMessage("Dude, gimme a number for {0}.");
215
216         IFormComponent field = newField("invalidFormat");
217
218         replayControls();
219
220         try
221         {
222             v.toObject(field, "xyz");
223             unreachable();
224         }
225         catch (ValidatorException ex)
226         {
227             assertEquals("Dude, gimme a number for invalidFormat.", ex.getMessage());
228             assertEquals(ValidationConstraint.NUMBER_FORMAT, ex.getConstraint());
229         }
230
231         verifyControls();
232     }
233
234     public void testBigInteger() throws ValidatorException
235     {
236         testPassThru(BigInteger JavaDoc.class, new BigInteger JavaDoc(
237                 "234905873490587234905724908252390487590234759023487523489075"));
238     }
239
240     public void testBigDecimal() throws ValidatorException
241     {
242         testPassThru(BigDecimal JavaDoc.class, new BigDecimal JavaDoc(
243                 "-29574923857342908744.19058734289734907543289752345897234590872349085"));
244     }
245
246     /** @since 3.0 * */
247
248     private void checkAdaptorType(int expectedType, Class JavaDoc numberType)
249     {
250         NumberValidator.NumberStrategy a = NumberValidator.getStrategy(numberType);
251
252         assertEquals(expectedType, a.getNumberType());
253     }
254
255     /** @since 3.0 * */
256
257     public void testAdaptorTypes() throws Exception JavaDoc
258     {
259         checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Byte JavaDoc.class);
260         checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Short JavaDoc.class);
261         checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Integer JavaDoc.class);
262         checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Long JavaDoc.class);
263         checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, BigInteger JavaDoc.class);
264         checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Float JavaDoc.class);
265         checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Double JavaDoc.class);
266         checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, BigDecimal JavaDoc.class);
267     }
268
269     /** @since 3.0 * */
270
271     private void checkCompare(Number JavaDoc left, Number JavaDoc right)
272     {
273         NumberValidator.NumberStrategy a = NumberValidator.getStrategy(left.getClass());
274
275         assertEquals(0, a.compare(left, right));
276     }
277
278     public void testByteCompare()
279     {
280         checkCompare(new Byte JavaDoc((byte) 3), new Long JavaDoc(3));
281     }
282
283     public void testShortCompare()
284     {
285         checkCompare(new Short JavaDoc((short) 14), new Double JavaDoc(14.0));
286     }
287
288     public void testIntegerCompare()
289     {
290         checkCompare(new Integer JavaDoc(19), new Long JavaDoc(19));
291     }
292
293     public void testLongCompare()
294     {
295         checkCompare(new Long JavaDoc(-22), new Short JavaDoc((short) -22));
296     }
297
298     public void testBigIntegerCompare()
299     {
300         checkCompare(new BigInteger JavaDoc("300"), new Long JavaDoc("300"));
301     }
302
303     public void testFloatCompare()
304     {
305         checkCompare(new Float JavaDoc("0"), new Double JavaDoc("0"));
306     }
307
308     public void testDoubleCompare()
309     {
310         checkCompare(new Double JavaDoc("0"), new Float JavaDoc("0"));
311     }
312
313     public void testBigDecimalCompare()
314     {
315         checkCompare(new BigDecimal JavaDoc("-137.75"), new Double JavaDoc("-137.75"));
316     }
317 }
Popular Tags