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>ShortConverter</code>.</p>
30   */
31  public class ShortConverterTestCase extends AbstractJsfTestCase {
32  
33  
34      // ------------------------------------------------------------ Constructors
35  
36  
37      // Construct a new instance of this test case.
38      public ShortConverterTestCase(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 ShortConverter();
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 Short[] VALID_OBJECTS =
89      { new Short(Short.MIN_VALUE),
90        new Short((short) -12345),
91        new Short((short) -123),
92        new Short((short) 0),
93        new Short((short) 123),
94        new Short((short) 12345),
95        new Short(Short.MAX_VALUE),
96      };
97  
98  
99      // Valid string values to be tested
100     private static final String[] VALID_STRINGS =
101     { String.valueOf(Short.MIN_VALUE),
102       "-12345",
103       "-123",
104       "0",
105       "123",
106       "12345",
107       String.valueOf(Short.MAX_VALUE),
108     };
109 
110 
111     // Valid string values with grouping (Locale.GERMANY)
112     private static final String[] VALID_STRINGS_DE =
113     { "-12.345",
114       "12.345",
115     };
116 
117 
118     // Valid string values with grouping (Locale.US)
119     private static final String[] VALID_STRINGS_US =
120     { "-12,345",
121       "12,345",
122     };
123 
124 
125     // ------------------------------------------------------ Instance Variables
126 
127 
128     /***
129      * <p>Converter instance under test.</p>
130      */
131     private ShortConverter converter = null;
132 
133 
134     /***
135      * <p>The form component for our input form.</p>
136      */
137     private UIForm form = null;
138 
139 
140     /***
141      * <p>The text field component for our input form.</p>
142      */
143     private UIInput input = null;
144 
145 
146     /***
147      * <p>ValidatorLifecycleListener used to load configuration resources</p>
148      */
149     private ValidatorLifecycleListener listener = null;
150 
151 
152     // ------------------------------------------------- Individual Test Methods
153 
154 
155     /***
156      * <p>Tests for invalid Object input.</p>
157      */
158     public void testInvalidObject() {
159 
160 
161     }
162 
163 
164     /***
165      * <p>Tests for valid Object input.</p>
166      */
167     public void testValidObject() {
168 
169         for (int i = 0; i < VALID_OBJECTS.length; i++) {
170             Short value = VALID_OBJECTS[i];
171             try {
172                 String string = converter.getAsString(facesContext, input, value);
173                 assertEquals("Short value " + value
174                              + " equals String value " + string,
175                   value.shortValue(), Short.valueOf(strip(string)).shortValue());
176             } catch (ConverterException e) {
177                 fail("Should not have thrown ConverterException for value " + value);
178             }
179         }
180 
181     }
182 
183 
184     /***
185      * <p>Tests for invalid String input.</p>
186      */
187     public void testInvalidString() {
188 
189 
190     }
191 
192 
193     /***
194      * <p>Tests for valid String input.</p>
195      */
196     public void testValidString() {
197 
198         for (int i = 0; i < VALID_STRINGS.length; i++) {
199             String string = VALID_STRINGS[i];
200             try {
201                 Object value = converter.getAsObject(facesContext, input, string);
202                 assertTrue(value instanceof Short);
203                 assertEquals("String value " + string
204                              + " equals Short value " + value,
205                              Short.parseShort(string),
206                              ((Short) value).shortValue());
207             } catch (ConverterException e) {
208                 fail("Should not have thrown ConverterException for value " + string);
209             }
210         }
211 
212     }
213 
214 
215     /***
216      * <p>Tests for valid String input with grouping (Locale.GERMANY).</p>
217      */
218     public void testValidStringDE() {
219 
220         facesContext.getViewRoot().setLocale(Locale.GERMANY);
221         for (int i = 0; i < VALID_STRINGS_DE.length; i++) {
222             String string = VALID_STRINGS_DE[i];
223             Object value = converter.getAsObject(facesContext, input, string);
224             assertTrue(value instanceof Short);
225             assertEquals("String value " + string
226                          + " equals Short value " + value,
227                          Short.parseShort(strip(string)),
228                          ((Short) value).shortValue());
229         }
230 
231     }
232 
233 
234     /***
235      * <p>Tests for valid String input with grouping (Locale.US).</p>
236      */
237     public void testValidStringUS() {
238 
239         facesContext.getViewRoot().setLocale(Locale.US);
240         for (int i = 0; i < VALID_STRINGS_US.length; i++) {
241             String string = VALID_STRINGS_US[i];
242             Object value = converter.getAsObject(facesContext, input, string);
243             assertTrue(value instanceof Short);
244             assertEquals("String value " + string
245                          + " equals Short value " + value,
246                          Short.parseShort(strip(string)),
247                          ((Short) value).shortValue());
248         }
249 
250     }
251 
252 
253     private String strip(String value) {
254         char grouping =
255           Locale.US == facesContext.getViewRoot().getLocale() ? ',' : '.';
256         while (true) {
257             int index = value.indexOf(grouping);
258             if (index < 0) {
259                 break;
260             }
261             value = value.substring(0, index) + value.substring(index + 1);
262         }
263         return value;
264     }
265 
266 
267 }