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.BigInteger;
21  
22  import org.apache.commons.beanutils2.locale.converters.BigIntegerLocaleConverter;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test Case for the BigIntegerLocaleConverter class.
28   */
29  public class BigIntegerLocaleConverterTest extends AbstractLocaleConverterTest<BigInteger> {
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 BigInteger("999");
39          expectedValue = new BigInteger(expectedIntegerValue);
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 = BigIntegerLocaleConverter.builder().get();
52  
53          // Perform Tests
54          convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
55          convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setLocalizedPattern(true).get();
71  
72          // Perform Tests
73          convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
74          convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setLocale(localizedLocale).get();
88  
89          // Perform Tests
90          convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
91          convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setLocale(localizedLocale).setLocalizedPattern(true).get();
105 
106         // Perform Tests
107         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
108         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setLocale(localizedLocale).setPattern(defaultIntegerPattern).get();
122 
123         // Perform Tests
124         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
125         convertValueWithPattern(converter, localizedIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setLocale(localizedLocale).setPattern(localizedIntegerPattern).setLocalizedPattern(true).get();
139 
140         // Perform Tests
141         convertValueNoPattern(converter, localizedIntegerValue, expectedValue);
142         convertValueWithPattern(converter, localizedIntegerValue, localizedIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setDefault(defaultValue).get();
156 
157         // Perform Tests
158         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
159         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setDefault(defaultValue).setLocalizedPattern(true).get();
173 
174         // Perform Tests
175         convertValueNoPattern(converter, defaultIntegerValue, expectedValue);
176         convertValueWithPattern(converter, defaultIntegerValue, defaultIntegerPattern, 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 = BigIntegerLocaleConverter.builder().setDefault(defaultValue).setLocale(localizedLocale).setPattern(localizedIntegerPattern)
190                 .setLocalizedPattern(true).get();
191 
192         convertValueNoPattern(converter, "(A)", localizedIntegerValue, expectedValue);
193         convertValueWithPattern(converter, "(A)", localizedIntegerValue, localizedIntegerPattern, expectedValue);
194         convertInvalid(converter, "(A)", defaultValue);
195         convertNull(converter, "(A)", defaultValue);
196 
197         // **************************************************************************
198         // Convert value in the wrong format - maybe you would expect it to throw an
199         // exception and return the default - it doesn't, DecimalFormat parses it
200         // quite happily turning "1,234" into "1"
201         // I guess this is one of the limitations of DecimalFormat
202         // **************************************************************************
203         convertValueNoPattern(converter, "(B)", defaultIntegerValue, new BigInteger("1"));
204 
205         // **************************************************************************
206         // Convert with non-localized pattern - unlike the equivalent BigDecimal Test Case
207         // it doesn't causes an exception in parse() - DecimalFormat parses it
208         // quite happily turning "1,234" into "1"
209         // Again this is one of the limitations of DecimalFormat
210         // **************************************************************************
211         convertValueWithPattern(converter, "(B)", localizedIntegerValue, defaultIntegerPattern, new BigInteger("1"));
212 
213         // **************************************************************************
214         // Convert with specified type
215         //
216         // BaseLocaleConverter completely ignores the type - so even if we specify
217         // Double.class here it still returns a BigInteger.
218         // **** This has been changed due to BEANUTILS-449 ****
219         // **************************************************************************
220         // convertValueToType(converter, "(B)", Double.class, localizedIntegerValue, localizedIntegerPattern, expectedValue);
221 
222         // Construct with non-localized pattern
223         converter = BigIntegerLocaleConverter.builder().setDefault(defaultValue).setLocale(localizedLocale).setPattern(defaultIntegerPattern)
224                 .setLocalizedPattern(false).get();
225 
226         convertValueNoPattern(converter, "(C)", localizedIntegerValue, expectedValue);
227         convertValueWithPattern(converter, "(C)", localizedIntegerValue, defaultIntegerPattern, expectedValue);
228         convertInvalid(converter, "(C)", defaultValue);
229         convertNull(converter, "(C)", defaultValue);
230 
231     }
232 
233 }