2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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>DoubleConverter</code>.</p>
30   */
31  public class DoubleConverterTestCase extends AbstractJsfTestCase {
32  
33  
34      // ------------------------------------------------------------ Constructors
35  
36  
37      // Construct a new instance of this test case.
38      public DoubleConverterTestCase(String name) {
39          super(name);
40      }
41  
42  
43      // ---------------------------------------------------- Overall Test Methods
44  
45  
46      // Set up instance variables required by this test case.
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 DoubleConverter();
64          input.setConverter(converter);
65  
66      }
67  
68  
69      // Tear down instance variables required by this test case.
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      // -------------------------------------------------------- Static Variables
85  
86  
87      // Valid object values to be tested
88      private static final Double[] VALID_OBJECTS =
89      { new Double(Double.MIN_VALUE),
90        new Double(-123456789),
91        new Double(-123456.789),
92        new Double(-123456),
93        new Double(-123.456),
94        new Double(-123),
95        new Double(0),
96        new Double(123),
97        new Double(123.456),
98        new Double(123456),
99        new Double(123456.789),
100       new Double(123456789),
101       new Double(Double.MAX_VALUE),
102     };
103 
104 
105     // Valid string values to be tested
106     private static final String[] VALID_STRINGS =
107     { String.valueOf(Double.MIN_VALUE),
108       "-123456789",
109       "-123456.789",
110       "-123456",
111       "-123.456",
112       "-123",
113       "0",
114       "123",
115       "123.456",
116       "123456",
117       "123456.789",
118       "123456789",
119       String.valueOf(Double.MAX_VALUE),
120     };
121 
122 
123     // Valid string values with grouping (Locale.GERMANY)
124     private static final String[] VALID_STRINGS_DE =
125     { "-123.456.789",
126       "-123.456,789",
127       "-123.456",
128       "-123,456",
129       "123",
130       "123",
131       "123,456",
132       "123.456",
133       "123.456,789",
134       "123.456.789",
135     };
136 
137 
138     // Valid string values with grouping (Locale.US)
139     private static final String[] VALID_STRINGS_US =
140     { "-123,456,789",
141       "-123,456.789",
142       "-123,456",
143       "-123.456",
144       "123",
145       "123",
146       "123.456",
147       "123,456",
148       "123,456.789",
149       "123,456,789",
150     };
151 
152 
153     // ------------------------------------------------------ Instance Variables
154 
155 
156     /***
157      * <p>Converter instance under test.</p>
158      */
159     private DoubleConverter converter = null;
160 
161 
162     /***
163      * <p>The form component for our input form.</p>
164      */
165     private UIForm form = null;
166 
167 
168     /***
169      * <p>The text field component for our input form.</p>
170      */
171     private UIInput input = null;
172 
173 
174     /***
175      * <p>ValidatorLifecycleListener used to load configuration resources</p>
176      */
177     private ValidatorLifecycleListener listener = null;
178 
179 
180     // ------------------------------------------------- Individual Test Methods
181 
182 
183     /***
184      * <p>Tests for invalid Object input.</p>
185      */
186     public void testInvalidObject() {
187 
188 
189     }
190 
191 
192     /***
193      * <p>Tests for valid Object input.</p>
194      */
195     public void testValidObject() {
196 
197         for (int i = 0; i < VALID_OBJECTS.length; i++) {
198             Double value = VALID_OBJECTS[i];
199             try {
200                 String string = converter.getAsString(facesContext, input, value);
201                 assertEquals("Double value " + value
202                              + " equals String value " + string,
203                   value.doubleValue(), Double.valueOf(strip(string)).doubleValue(),
204                   (double) 0.001);
205             } catch (ConverterException e) {
206                 fail("Should not have thrown ConverterException for value " + value);
207             }
208         }
209 
210     }
211 
212 
213     /***
214      * <p>Tests for invalid String input.</p>
215      */
216     public void testInvalidString() {
217 
218 
219     }
220 
221 
222     /***
223      * <p>Tests for valid String input.</p>
224      */
225     public void testValidString() {
226 
227         for (int i = 0; i < VALID_STRINGS.length; i++) {
228             String string = VALID_STRINGS[i];
229             try {
230                 Object value = converter.getAsObject(facesContext, input, string);
231                 assertTrue(value instanceof Double);
232                 assertEquals("String value " + string
233                              + " equals Double value " + value,
234                              Double.parseDouble(string),
235                              ((Double) value).doubleValue(),
236                              (double) 0.001);
237             } catch (ConverterException e) {
238                 fail("Should not have thrown ConverterException for value " + string);
239             }
240         }
241 
242     }
243 
244 
245     /***
246      * <p>Tests for valid String input with grouping (Locale.GERMANY).</p>
247      */
248     public void testValidStringDE() {
249 
250         facesContext.getViewRoot().setLocale(Locale.GERMANY);
251         for (int i = 0; i < VALID_STRINGS_DE.length; i++) {
252             String string = VALID_STRINGS_DE[i];
253             Object value = converter.getAsObject(facesContext, input, string);
254             assertTrue(value instanceof Double);
255             assertEquals("String value " + string
256                          + " equals Double value " + value,
257                          Double.parseDouble(strip(string)),
258                          ((Double) value).doubleValue(),
259                          (double) 0.001);
260         }
261 
262     }
263 
264 
265     /***
266      * <p>Tests for valid String input with grouping (Locale.US).</p>
267      */
268     public void testValidStringUS() {
269 
270         facesContext.getViewRoot().setLocale(Locale.US);
271         for (int i = 0; i < VALID_STRINGS_US.length; i++) {
272             String string = VALID_STRINGS_US[i];
273             Object value = converter.getAsObject(facesContext, input, string);
274             assertTrue(value instanceof Double);
275             assertEquals("String value " + string
276                          + " equals Double value " + value,
277                          Double.parseDouble(strip(string)),
278                          ((Double) value).doubleValue(),
279                          (double) 0.001);
280         }
281 
282     }
283 
284 
285     private String strip(String value) {
286         char grouping =
287           Locale.US == facesContext.getViewRoot().getLocale() ? ',' : '.';
288         while (true) {
289             int index = value.indexOf(grouping);
290             if (index < 0) {
291                 break;
292             }
293             value = value.substring(0, index) + value.substring(index + 1);
294         }
295         // Switch the decimal point back as well if not US
296         if (Locale.US != facesContext.getViewRoot().getLocale()) {
297             int index = value.indexOf(',');
298             if (index >= 0) {
299                 value = value.substring(0, index) + '.' + value.substring(index + 1);
300             }
301         }
302         return value;
303     }
304 
305 
306 }