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