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