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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.util.Locale;
26  
27  import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
28  import org.junit.jupiter.api.AfterEach;
29  import org.junit.jupiter.api.BeforeEach;
30  
31  /**
32   * Base Test Case for the DecimalLocaleConverter classes. This class doesn't define any real tests; it just provides useful methods for the real test case
33   * classes to inherit.
34   *
35   * @param <T> The converter type.
36   */
37  public abstract class AbstractLocaleConverterTest<T> {
38  
39      // Original Default Locale
40      protected Locale origLocale;
41  
42      // Converter
43      protected BaseLocaleConverter<T> converter;
44      protected Object result;
45      protected T defaultValue;
46      protected Object expectedValue;
47  
48      // Localized values
49      protected Locale localizedLocale;
50      protected String localizedDecimalPattern;
51      protected String localizedIntegerPattern;
52      protected String localizedDecimalValue;
53      protected String localizedIntegerValue;
54  
55      // Locale values
56      protected Locale defaultLocale;
57      protected String defaultDecimalPattern;
58      protected String defaultIntegerPattern;
59      protected String defaultDecimalValue;
60      protected String defaultIntegerValue;
61  
62      // Expected values
63      protected String expectedDecimalValue;
64      protected String expectedIntegerValue;
65  
66      /**
67       * Test Converting an invalid value.
68       */
69      protected void convertInvalid(final BaseLocaleConverter<T> converter, final Object expectedValue) {
70          convertInvalid(converter, "", expectedValue);
71      }
72  
73      /**
74       * Test Converting an invalid value.
75       */
76      protected void convertInvalid(final BaseLocaleConverter<T> converter, final String msgId, final Object expectedValue) {
77          // Convert value with no pattern
78          try {
79              result = converter.convert("xyz");
80              if (expectedValue == null) {
81                  fail("Expected ConversionException if no default value " + msgId + ", converter = " + converter);
82              }
83          } catch (final Exception e) {
84              if (expectedValue != null) {
85                  fail("Expected default value '" + msgId + "' threw " + e + ", expectedValue = '" + expectedValue + "'");
86              }
87          }
88  
89          if (expectedValue != null) {
90              assertEquals(expectedValue, result, () -> "Check invalid conversion is default " + msgId);
91          }
92      }
93  
94      /**
95       * Test Converting Null value.
96       */
97      protected void convertNull(final BaseLocaleConverter<T> converter, final Object expectedValue) {
98          convertNull(converter, "", expectedValue);
99      }
100 
101     /**
102      * Test Converting Null value.
103      */
104     protected void convertNull(final BaseLocaleConverter<T> converter, final String msgId, final Object expectedValue) {
105         // Convert value with no pattern
106         result = assertDoesNotThrow(() -> converter.convert(null), () -> "Null conversion threw '" + msgId + "'");
107 
108         if (expectedValue == null) {
109             assertNull(result, () -> "Check null conversion is null '" + msgId + "' result=" + result);
110         } else {
111             assertEquals(expectedValue, result, () -> "Check null conversion is default " + msgId);
112         }
113     }
114 
115     /**
116      * Test Converting Value WITHOUT a pattern
117      */
118     protected void convertValueNoPattern(final BaseLocaleConverter<T> converter, final Object value, final Object expectedValue) {
119         convertValueNoPattern(converter, "", value, expectedValue);
120     }
121 
122     /**
123      * Test Converting Value WITHOUT a pattern
124      */
125     protected void convertValueNoPattern(final BaseLocaleConverter<T> converter, final String msgId, final Object value, final Object expectedValue) {
126         // Convert value with no pattern
127         result = assertDoesNotThrow(() -> converter.convert(value), () -> "No Pattern conversion threw '" + msgId + "'");
128         assertEquals(expectedValue, result, () -> "Check conversion value without pattern " + msgId);
129 
130     }
131 
132     /**
133      * Test Converting Value WITH a pattern
134      */
135     protected void convertValueWithPattern(final BaseLocaleConverter<T> converter, final Object value, final String pattern, final Object expectedValue) {
136         convertValueWithPattern(converter, "", value, pattern, expectedValue);
137     }
138 
139     /**
140      * Test Converting Value WITH a pattern
141      */
142     protected void convertValueWithPattern(final BaseLocaleConverter<T> converter, final String msgId, final Object value, final String pattern,
143             final Object expectedValue) {
144         // Convert value with no pattern
145         result = assertDoesNotThrow(() -> converter.convert(value, pattern), () -> "Pattern conversion threw '" + msgId + "'");
146         assertEquals(expectedValue, result, () -> "Check conversion value with pattern " + msgId);
147     }
148 
149     /**
150      * Sets up instance variables required by this test case.
151      */
152     @BeforeEach
153     public void setUp() throws Exception {
154 
155         // Default Locale (Use US)
156         defaultLocale = Locale.US;
157         defaultDecimalPattern = "#,###.00";
158         defaultIntegerPattern = "#,###";
159         defaultDecimalValue = "1,234.56";
160         defaultIntegerValue = "1,234";
161 
162         // Use German Locale (uses different separators to US)
163         localizedLocale = Locale.GERMAN;
164         localizedDecimalPattern = "#.###,00";
165         localizedIntegerPattern = "#.###";
166         localizedDecimalValue = "1.234,56";
167         localizedIntegerValue = "1.234";
168 
169         // Expected Values
170         expectedDecimalValue = "1234.56";
171         expectedIntegerValue = "1234";
172 
173         // Reset default to the one specified
174         origLocale = Locale.getDefault();
175 
176         // Initialize
177         converter = null;
178         result = null;
179         defaultValue = null;
180         expectedValue = null;
181 
182         if (defaultLocale.equals(origLocale)) {
183             origLocale = null;
184         } else {
185             // System.out.println("Changing default locale from " + origLocale + " to " + defaultLocale);
186             Locale.setDefault(defaultLocale);
187         }
188     }
189 
190     /**
191      * Tear down instance variables required by this test case.
192      */
193     @AfterEach
194     public void tearDown() {
195         converter = null;
196         result = null;
197         defaultValue = null;
198         expectedValue = null;
199 
200         // Set the Default Locale back to the original value
201         if (origLocale != null) {
202             // System.out.println("Restoring default locale to " + origLocale);
203             Locale.setDefault(origLocale);
204         }
205     }
206 }