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.util.Locale;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.commons.beanutils.locale.BaseLocaleConverter;
25  
26  /**
27   * Base Test Case for the DecimalLocaleConverter classes. This class doesn't
28   * define any real tests; it just provides useful methods for the real
29   * test case classes to inherit.
30   *
31   * @version $Id$
32   */
33  
34  public class BaseLocaleConverterTestCase extends TestCase {
35  
36  
37      // Original Default Locale
38      protected Locale origLocale;
39  
40      // Converter
41      protected BaseLocaleConverter converter;
42      protected Object result;
43      protected Object defaultValue;
44      protected Object expectedValue;
45  
46  
47      // Localized values
48      protected Locale localizedLocale;
49      protected String localizedDecimalPattern;
50      protected String localizedIntegerPattern;
51      protected String localizedDecimalValue;
52      protected String localizedIntegerValue;
53  
54      // Locale values
55      protected Locale defaultLocale;
56      protected String defaultDecimalPattern;
57      protected String defaultIntegerPattern;
58      protected String defaultDecimalValue;
59      protected String defaultIntegerValue;
60  
61  
62      // Expected values
63      protected String expectedDecimalValue;
64      protected String expectedIntegerValue;
65  
66      // ---------------------------------------------------------- Constructors
67  
68      public BaseLocaleConverterTestCase(final String name) {
69          super(name);
70      }
71  
72      // -------------------------------------------------- Overall Test Methods
73  
74      /**
75       * Set up instance variables required by this test case.
76       */
77      @Override
78      public void setUp() throws Exception {
79  
80          // Default Locale (Use US)
81          defaultLocale           = Locale.US;
82          defaultDecimalPattern   = "#,###.00";
83          defaultIntegerPattern   = "#,###";
84          defaultDecimalValue     = "1,234.56";
85          defaultIntegerValue     = "1,234";
86  
87          // Use German Locale (uses different separators to US)
88          localizedLocale         = Locale.GERMAN;
89          localizedDecimalPattern = "#.###,00";
90          localizedIntegerPattern = "#.###";
91          localizedDecimalValue   = "1.234,56";
92          localizedIntegerValue   = "1.234";
93  
94          // Expected Values
95          expectedDecimalValue    = "1234.56";
96          expectedIntegerValue    = "1234";
97  
98          // Reset default to the one specified
99          origLocale = Locale.getDefault();
100 
101         // Initialize
102         converter = null;
103         result = null;
104         defaultValue = null;
105         expectedValue= null;
106 
107         if (defaultLocale.equals(origLocale)) {
108             origLocale = null;
109         } else {
110             // System.out.println("Changing default locale from " + origLocale + " to " + defaultLocale);
111             Locale.setDefault(defaultLocale);
112         }
113 
114 
115     }
116 
117     /**
118      * Tear down instance variables required by this test case.
119      */
120     @Override
121     public void tearDown() {
122 
123         converter = null;
124         result = null;
125         defaultValue = null;
126         expectedValue= null;
127 
128         // Set the Default Locale back to the original value
129         if (origLocale != null) {
130             // System.out.println("Restoring default locale to " + origLocale);
131             Locale.setDefault(origLocale);
132         }
133 
134     }
135 
136 
137     // -------------------------------------------------- Generic Test Methods
138 
139     /**
140      * Test Converting Value WITH a pattern
141      */
142     protected void convertValueWithPattern(final BaseLocaleConverter converter, final Object value, final String pattern, final Object expectedValue) {
143         convertValueWithPattern(converter, "", value, pattern, expectedValue);
144     }
145 
146     /**
147      * Test Converting Value WITH a pattern
148      */
149     protected void convertValueWithPattern(final BaseLocaleConverter converter, final String msgId, final Object value, final String pattern, final Object expectedValue) {
150 
151         // Convert value with no pattern
152         try {
153             result = converter.convert(value, pattern);
154         } catch (final Exception e) {
155             fail("Pattern conversion threw " + msgId + " threw " + e);
156         }
157         assertEquals("Check conversion value with pattern " + msgId, expectedValue, result);
158 
159     }
160 
161     /**
162      * Test Converting Value WITHOUT a pattern
163      */
164     protected void convertValueNoPattern(final BaseLocaleConverter converter, final Object value, final Object expectedValue) {
165         convertValueNoPattern(converter, "", value, expectedValue);
166     }
167 
168     /**
169      * Test Converting Value WITHOUT a pattern
170      */
171     protected void convertValueNoPattern(final BaseLocaleConverter converter, final String msgId, final Object value, final Object expectedValue) {
172 
173         // Convert value with no pattern
174         try {
175             result = converter.convert(value);
176         } catch (final Exception e) {
177             fail("No Pattern conversion threw " + msgId + " threw " + e);
178         }
179         assertEquals("Check conversion value without pattern " + msgId, expectedValue, result);
180 
181 
182     }
183 
184     /**
185      * Test Converting Value To a specified Type
186      */
187     protected void convertValueToType(final BaseLocaleConverter converter, final Class<?> clazz, final Object value, final String pattern, final Object expectedValue) {
188         convertValueToType(converter, "", clazz, value, pattern, expectedValue);
189     }
190 
191     /**
192      * Test Converting Value To a specified Type
193      */
194     protected void convertValueToType(final BaseLocaleConverter converter, final String msgId, final Class<?> clazz, final Object value, final String pattern, final Object expectedValue) {
195 
196         // Convert value with no pattern
197         try {
198             result = converter.convert(clazz, value, pattern);
199         } catch (final Exception e) {
200             fail("Type  conversion threw " + msgId + " threw " + e);
201         }
202         assertEquals("Check conversion value to type " + msgId, expectedValue, result);
203 
204     }
205 
206     /**
207      * Test Converting Null value.
208      */
209     protected void convertNull(final BaseLocaleConverter converter, final Object expectedValue) {
210         convertNull(converter, "", expectedValue);
211     }
212 
213     /**
214      * Test Converting Null value.
215      */
216     protected void convertNull(final BaseLocaleConverter converter, final String msgId, final Object expectedValue) {
217 
218         // Convert value with no pattern
219         try {
220             result = converter.convert(null);
221         } catch (final Exception e) {
222             fail("Null conversion " + msgId + " threw " + e);
223         }
224 
225         if (expectedValue == null) {
226             assertNull("Check null conversion is null " + msgId + " result="+result, result);
227         } else {
228             assertEquals("Check null conversion is default " + msgId, expectedValue, result);
229         }
230 
231     }
232 
233     /**
234      * Test Converting an invalid value.
235      */
236     protected void convertInvalid(final BaseLocaleConverter converter, final Object expectedValue) {
237         convertInvalid(converter, "", expectedValue);
238     }
239 
240     /**
241      * Test Converting an invalid value.
242      */
243     protected void convertInvalid(final BaseLocaleConverter converter, final String msgId, final Object expectedValue) {
244 
245         // Convert value with no pattern
246         try {
247             result = converter.convert("xyz");
248             if (expectedValue == null) {
249                 fail("Expected ConversionException if no default value " + msgId);
250             }
251         } catch (final Exception e) {
252             if (expectedValue != null) {
253                 fail("Expected default value " + msgId + " threw " + e);
254             }
255         }
256 
257         if (expectedValue != null) {
258             assertEquals("Check invalid conversion is default " + msgId, expectedValue, result);
259         }
260 
261     }
262 
263     /**
264      * This class isn't intended to perform any real tests; it just provides
265      * methods for the real test cases to inherit. However junit complains
266      * if a class named ..TestCase contains no test methods, so here we
267      * define a dummy one to keep it happy.
268      */
269     public void testNothing() {
270     }
271 }
272