2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.validator.converter;
19
20 import java.util.Locale;
21 import javax.faces.component.UIForm;
22 import javax.faces.component.UIInput;
23 import javax.faces.convert.ConverterException;
24 import javax.servlet.ServletContextEvent;
25 import org.apache.shale.test.base.AbstractJsfTestCase;
26 import org.apache.shale.validator.faces.ValidatorLifecycleListener;
27
28 /***
29 * <p>Test case for <code>IntegerConverter</code>.</p>
30 */
31 public class IntegerConverterTestCase extends AbstractJsfTestCase {
32
33
34
35
36
37
38 public IntegerConverterTestCase(String name) {
39 super(name);
40 }
41
42
43
44
45
46
47 protected void setUp() throws Exception {
48
49 super.setUp();
50 facesContext.getViewRoot().setLocale(Locale.US);
51
52 listener = new ValidatorLifecycleListener();
53 listener.contextInitialized(new ServletContextEvent(servletContext));
54
55 form = new UIForm();
56 form.setId("form");
57 facesContext.getViewRoot().getChildren().add(form);
58
59 input = new UIInput();
60 input.setId("input");
61 form.getChildren().add(input);
62
63 converter = new IntegerConverter();
64 input.setConverter(converter);
65
66 }
67
68
69
70 protected void tearDown() throws Exception {
71
72 converter = null;
73 input = null;
74 form = null;
75
76 listener.contextDestroyed(new ServletContextEvent(servletContext));
77 listener = null;
78
79 super.tearDown();
80
81 }
82
83
84
85
86
87
88 private static final Integer[] VALID_OBJECTS =
89 { new Integer(Integer.MIN_VALUE),
90 new Integer(-123456789),
91 new Integer(-123456),
92 new Integer(-123),
93 new Integer(0),
94 new Integer(123),
95 new Integer(123456),
96 new Integer(123456789),
97 new Integer(Integer.MAX_VALUE),
98 };
99
100
101
102 private static final String[] VALID_STRINGS =
103 { String.valueOf(Integer.MIN_VALUE),
104 "-123456789",
105 "-123456",
106 "-123",
107 "0",
108 "123",
109 "123456",
110 "123456789",
111 String.valueOf(Integer.MAX_VALUE),
112 };
113
114
115
116 private static final String[] VALID_STRINGS_DE =
117 { "-123.456.789",
118 "-123.456",
119 "123.456",
120 "123.456.789",
121 };
122
123
124
125 private static final String[] VALID_STRINGS_US =
126 { "-123,456,789",
127 "-123,456",
128 "123,456",
129 "123,456,789",
130 };
131
132
133
134
135
136 /***
137 * <p>Converter instance under test.</p>
138 */
139 private IntegerConverter converter = null;
140
141
142 /***
143 * <p>The form component for our input form.</p>
144 */
145 private UIForm form = null;
146
147
148 /***
149 * <p>The text field component for our input form.</p>
150 */
151 private UIInput input = null;
152
153
154 /***
155 * <p>ValidatorLifecycleListener used to load configuration resources</p>
156 */
157 private ValidatorLifecycleListener listener = null;
158
159
160
161
162
163 /***
164 * <p>Tests for invalid Object input.</p>
165 */
166 public void testInvalidObject() {
167
168
169 }
170
171
172 /***
173 * <p>Tests for valid Object input.</p>
174 */
175 public void testValidObject() {
176
177 for (int i = 0; i < VALID_OBJECTS.length; i++) {
178 Integer value = VALID_OBJECTS[i];
179 try {
180 String string = converter.getAsString(facesContext, input, value);
181 assertEquals("Integer value " + value
182 + " equals String value " + string,
183 value.intValue(), Integer.valueOf(strip(string)).intValue());
184 } catch (ConverterException e) {
185 fail("Should not have thrown ConverterException for value " + value);
186 }
187 }
188
189 }
190
191
192 /***
193 * <p>Tests for invalid String input.</p>
194 */
195 public void testInvalidString() {
196
197
198 }
199
200
201 /***
202 * <p>Tests for valid String input.</p>
203 */
204 public void testValidString() {
205
206 for (int i = 0; i < VALID_STRINGS.length; i++) {
207 String string = VALID_STRINGS[i];
208 try {
209 Object value = converter.getAsObject(facesContext, input, string);
210 assertTrue(value instanceof Integer);
211 assertEquals("String value " + string
212 + " equals Integer value " + value,
213 Integer.parseInt(string),
214 ((Integer) value).intValue());
215 } catch (ConverterException e) {
216 fail("Should not have thrown ConverterException for value " + string);
217 }
218 }
219
220 }
221
222
223 /***
224 * <p>Tests for valid String input with grouping (Locale.GERMANY).</p>
225 */
226 public void testValidStringDE() {
227
228 facesContext.getViewRoot().setLocale(Locale.GERMANY);
229 for (int i = 0; i < VALID_STRINGS_DE.length; i++) {
230 String string = VALID_STRINGS_DE[i];
231 Object value = converter.getAsObject(facesContext, input, string);
232 assertTrue(value instanceof Integer);
233 assertEquals("String value " + string
234 + " equals Integer value " + value,
235 Integer.parseInt(strip(string)),
236 ((Integer) value).intValue());
237 }
238
239 }
240
241
242 /***
243 * <p>Tests for valid String input with grouping (Locale.US).</p>
244 */
245 public void testValidStringUS() {
246
247 facesContext.getViewRoot().setLocale(Locale.US);
248 for (int i = 0; i < VALID_STRINGS_US.length; i++) {
249 String string = VALID_STRINGS_US[i];
250 Object value = converter.getAsObject(facesContext, input, string);
251 assertTrue(value instanceof Integer);
252 assertEquals("String value " + string
253 + " equals Integer value " + value,
254 Integer.parseInt(strip(string)),
255 ((Integer) value).intValue());
256 }
257
258 }
259
260
261 private String strip(String value) {
262 char grouping =
263 Locale.US == facesContext.getViewRoot().getLocale() ? ',' : '.';
264 while (true) {
265 int index = value.indexOf(grouping);
266 if (index < 0) {
267 break;
268 }
269 value = value.substring(0, index) + value.substring(index + 1);
270 }
271 return value;
272 }
273
274
275 }