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