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