KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > test > NumberTypeValidatorTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator.test;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.inversoft.error.BasicError;
16 import com.inversoft.util.typevalidator.NumberTypeValidator;
17
18
19 /**
20  * <p>
21  * This class tests the number type validator.
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class NumberTypeValidatorTest extends TestCase {
27
28     /**
29      * Constructs a new <code>NumberTypeValidatorTest</code>.
30      *
31      * @param name The name of the test being run
32      */

33     public NumberTypeValidatorTest(String JavaDoc name) {
34         super(name);
35     }
36
37
38     /**
39      * Tests that a normal number does not fail.
40      */

41     public void testNumber() {
42         Double JavaDoc value = new Double JavaDoc(2.3d);
43         NumberTypeValidator validator = new NumberTypeValidator();
44
45         BasicError error = validator.validate(value, null, "fail", null);
46         assertNull(error);
47     }
48
49     /**
50      * Tests that a null conversion fails
51      */

52     public void testNull() {
53         String JavaDoc value = null;
54         NumberTypeValidator validator = new NumberTypeValidator();
55
56         BasicError error = validator.validate(value, null, "fail", null);
57         assertNotNull(error);
58     }
59
60     /**
61      * Tests that a String conversion works
62      */

63     public void testStringNumber() {
64         String JavaDoc value = "12";
65         NumberTypeValidator validator = new NumberTypeValidator();
66
67         BasicError error = validator.validate(value, null, "fail", null);
68         assertNull(error);
69     }
70
71     /**
72      * Tests a normal number and the min max test do not fail.
73      */

74     public void testNumberMinMax() {
75         Double JavaDoc value = new Double JavaDoc(2.3d);
76         NumberTypeValidator validator = new NumberTypeValidator();
77
78         Map JavaDoc params = new HashMap JavaDoc();
79         params.put("min", new Double JavaDoc(1));
80         params.put("max", new Double JavaDoc(4));
81
82         BasicError error = validator.validate(value, params, "fail", null);
83         assertNull(error);
84     }
85
86     /**
87      * Test that a String conversion and the min max work.
88      */

89     public void testStringNumberMinMax() {
90         String JavaDoc value = "2.3";
91         NumberTypeValidator validator = new NumberTypeValidator();
92
93         Map JavaDoc params = new HashMap JavaDoc();
94         params.put("min", new Double JavaDoc(1));
95         params.put("max", new Double JavaDoc(4));
96
97         BasicError error = validator.validate(value, params, "fail", null);
98         assertNull(error);
99     }
100
101     /**
102      * Test that a String fails.
103      */

104     public void testStringFailure() {
105         String JavaDoc value = "foo";
106         NumberTypeValidator validator = new NumberTypeValidator();
107
108         BasicError error = validator.validate(value, null, "fail", null);
109         assertNotNull(error);
110         assertEquals("fail", error.getMessage());
111     }
112
113     /**
114      * Tests that the min fail.
115      */

116     public void testNumberMinFailure() {
117         Double JavaDoc value = new Double JavaDoc(1);
118         NumberTypeValidator validator = new NumberTypeValidator();
119
120         Map JavaDoc params = new HashMap JavaDoc();
121         params.put("min", new Double JavaDoc(3));
122         params.put("max", new Double JavaDoc(4));
123
124         BasicError error = validator.validate(value, params, "fail", null);
125         assertNotNull(error);
126         assertEquals("fail", error.getMessage());
127     }
128
129     /**
130      * Tests that the max fail.
131      */

132     public void testNumberMaxFailure() {
133         Double JavaDoc value = new Double JavaDoc(23);
134         NumberTypeValidator validator = new NumberTypeValidator();
135
136         Map JavaDoc params = new HashMap JavaDoc();
137         params.put("min", new Double JavaDoc(1));
138         params.put("max", new Double JavaDoc(4));
139
140         BasicError error = validator.validate(value, params, "fail", null);
141         assertNotNull(error);
142         assertEquals("fail", error.getMessage());
143     }
144
145     /**
146      * Tests that the String conversion succeeds and min fails.
147      */

148     public void testStringNumberMinFailure() {
149         String JavaDoc value = "3";
150         NumberTypeValidator validator = new NumberTypeValidator();
151
152         Map JavaDoc params = new HashMap JavaDoc();
153         params.put("min", new Double JavaDoc(13));
154         params.put("max", new Double JavaDoc(42));
155
156         BasicError error = validator.validate(value, params, "fail", null);
157         assertNotNull(error);
158         assertEquals("fail", error.getMessage());
159     }
160
161     /**
162      * Tests that the String conversion succeeds and the max fails.
163      */

164     public void testStringNumberMaxFailure() {
165         String JavaDoc value = "300";
166         NumberTypeValidator validator = new NumberTypeValidator();
167
168         Map JavaDoc params = new HashMap JavaDoc();
169         params.put("min", new Double JavaDoc(13));
170         params.put("max", new Double JavaDoc(42));
171
172         BasicError error = validator.validate(value, params, "fail", null);
173         assertNotNull(error);
174         assertEquals("fail", error.getMessage());
175     }
176
177     /**
178      * Tests that empty params are okay
179      */

180     public void testEmptyParams() {
181         String JavaDoc value = "30";
182         NumberTypeValidator validator = new NumberTypeValidator();
183
184         Map JavaDoc params = new HashMap JavaDoc();
185         BasicError error = validator.validate(value, params, "fail", null);
186         assertNull(error);
187     }
188
189     /**
190      * Tests that string min and max get converted
191      */

192     public void testStringMinMax() {
193         String JavaDoc value = "30";
194         NumberTypeValidator validator = new NumberTypeValidator();
195
196         Map JavaDoc params = new HashMap JavaDoc();
197         params.put("min", "13");
198         params.put("max", "42");
199
200         BasicError error = validator.validate(value, params, "fail", null);
201         assertNull(error);
202     }
203
204     /**
205      * Tests that the bad params are ignored
206      */

207     public void testBadParams() {
208         String JavaDoc value = "30";
209         NumberTypeValidator validator = new NumberTypeValidator();
210
211         Map JavaDoc params = new HashMap JavaDoc();
212         params.put("min", "foo");
213
214         try {
215             validator.validate(value, params, "fail", null);
216             fail("Should have failed");
217         } catch (NumberFormatException JavaDoc nfe) {
218             // Expected
219
}
220
221         params = new HashMap JavaDoc();
222         params.put("max", "bar");
223
224         try {
225             validator.validate(value, params, "fail", null);
226             fail("Should have failed");
227         } catch (NumberFormatException JavaDoc nfe) {
228             // Expected
229
}
230     }
231 }
Popular Tags