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.commons.beanutils.locale.converters;
19  
20  
21  /**
22   * Test Case for the ShortLocaleConverter class.
23   *
24   * @version $Id$
25   */
26  
27  public class ShortLocaleConverterTestCase extends BaseLocaleConverterTestCase {
28  
29  
30  
31      // ---------------------------------------------------------- Constructors
32  
33      public ShortLocaleConverterTestCase(final String name) {
34          super(name);
35      }
36  
37      // -------------------------------------------------- Overall Test Methods
38  
39      /**
40       * Set up instance variables required by this test case.
41       */
42      @Override
43      public void setUp() throws Exception {
44  
45          super.setUp();
46  
47          defaultValue  = new Short("999");
48          expectedValue = new Short(expectedIntegerValue);
49  
50      }
51  
52      /**
53       * Tear down instance variables required by this test case.
54       */
55      @Override
56      public void tearDown() {
57          super.tearDown();
58      }
59  
60  
61      // ------------------------------------------------------------------------
62  
63      /**
64       * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
65       */
66      public void testConstructorMain() {
67  
68          // ------------- Construct with localized pattern ------------
69          converter = new ShortLocaleConverter(defaultValue,
70                                                    localizedLocale,
71                                                    localizedIntegerPattern,
72                                                    true);
73  
74  
75          convertValueNoPattern(converter, "(A)", localizedIntegerValue, expectedValue);
76          convertValueWithPattern(converter, "(A)", localizedIntegerValue, localizedIntegerPattern, expectedValue);
77          convertInvalid(converter, "(A)", defaultValue);
78          convertNull(converter, "(A)", defaultValue);
79  
80  
81          // **************************************************************************
82          // Convert value in the wrong format - maybe you would expect it to throw an
83          // exception and return the default - it doesn't, DecimalFormat parses it
84          // quite happily turning "1,234" into "1"
85          // I guess this is one of the limitations of DecimalFormat
86          // **************************************************************************
87          convertValueNoPattern(converter, "(B)", defaultIntegerValue, new Short("1"));
88  
89  
90          // **************************************************************************
91          // Convert with non-localized pattern - unlike the equivalent BigDecimal Test Case
92          // it doesn't causes an exception in parse() - DecimalFormat parses it
93          // quite happily turning "1,234" into "1"
94          // Again this is one of the limitations of DecimalFormat
95          // **************************************************************************
96          convertValueWithPattern(converter, "(B)", localizedIntegerValue, defaultIntegerPattern, new Short("1"));
97  
98  
99          // **************************************************************************
100         // Convert with specified type
101         //
102         // BaseLocaleConverter completely ignores the type - so even if we specify
103         // Double.class here it still returns a Short.
104         //  **** This has been changed due to BEANUTILS-449 ****
105         // **************************************************************************
106         //convertValueToType(converter, "(B)", Double.class, localizedIntegerValue, localizedIntegerPattern, expectedValue);
107 
108 
109         // ------------- Construct with non-localized pattern ------------
110         converter = new ShortLocaleConverter(defaultValue,
111                                                   localizedLocale,
112                                                   defaultIntegerPattern,
113                                                   false);
114 
115 
116         convertValueNoPattern(converter, "(C)", localizedIntegerValue, expectedValue);
117         convertValueWithPattern(converter, "(C)", localizedIntegerValue, defaultIntegerPattern, expectedValue);
118         convertInvalid(converter, "(C)", defaultValue);
119         convertNull(converter, "(C)", defaultValue);
120 
121     }
122 
123     /**
124      * Test Converter() constructor
125      *
126      * Uses the default locale, no default value
127      *
128      */
129     public void testConstructor_2() {
130 
131         // ------------- Construct using default locale ------------
132         converter = new ShortLocaleConverter();
133 
134         // Perform Tests
135         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
136         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
137         convertInvalid(converter, null);
138         convertNull(converter, null);
139 
140     }
141 
142     /**
143      * Test Converter(locPattern) constructor
144      *
145      * Uses the default locale, no default value
146      *
147      */
148     public void testConstructor_3() {
149 
150         // ------------- Construct using localized pattern (default locale) --------
151         converter = new ShortLocaleConverter(true);
152 
153         // Perform Tests
154         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
155         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
156         convertInvalid(converter, null);
157         convertNull(converter, null);
158 
159 
160     }
161 
162     /**
163      * Test Converter(Locale) constructor
164      */
165     public void testConstructor_4() {
166 
167         // ------------- Construct using specified Locale --------
168         converter = new ShortLocaleConverter(localizedLocale);
169 
170         // Perform Tests
171         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
172         convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, expectedValue);
173         convertInvalid(converter, null);
174         convertNull(converter, null);
175 
176 
177     }
178 
179 
180     /**
181      * Test Converter(Locale, locPattern) constructor
182      */
183     public void testConstructor_5() {
184 
185         // ------------- Construct using specified Locale --------
186         converter = new ShortLocaleConverter(localizedLocale, true);
187 
188         // Perform Tests
189         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
190         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, expectedValue);
191         convertInvalid(converter, null);
192         convertNull(converter, null);
193 
194 
195     }
196 
197     /**
198      * Test Converter(Locale, pattern) constructor
199      */
200     public void testConstructor_6() {
201 
202         // ------------- Construct using specified Locale --------
203         converter = new ShortLocaleConverter(localizedLocale, defaultIntegerPattern);
204 
205         // Perform Tests
206         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
207         convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, expectedValue);
208         convertInvalid(converter, null);
209         convertNull(converter, null);
210 
211     }
212 
213     /**
214      * Test Converter(Locale, pattern, locPattern) constructor
215      */
216     public void testConstructor_7() {
217 
218         // ------------- Construct using specified Locale --------
219         converter = new ShortLocaleConverter(localizedLocale, localizedIntegerPattern, true);
220 
221         // Perform Tests
222         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
223         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, expectedValue);
224         convertInvalid(converter, null);
225         convertNull(converter, null);
226 
227     }
228 
229     /**
230      * Test Converter(defaultValue) constructor
231      */
232     public void testConstructor_8() {
233 
234         // ------------- Construct using specified Locale --------
235         converter = new ShortLocaleConverter(defaultValue);
236 
237         // Perform Tests
238         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
239         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
240         convertInvalid(converter, defaultValue);
241         convertNull(converter, defaultValue);
242 
243     }
244 
245     /**
246      * Test Converter(defaultValue, locPattern) constructor
247      */
248     public void testConstructor_9() {
249 
250         // ------------- Construct using specified Locale --------
251         converter = new ShortLocaleConverter(defaultValue, true);
252 
253         // Perform Tests
254         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
255         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, expectedValue);
256         convertInvalid(converter, defaultValue);
257         convertNull(converter, defaultValue);
258 
259     }
260 
261 
262 
263 }
264