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