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