001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale.converters;
019:
020: import junit.framework.TestCase;
021: import java.util.Locale;
022: import org.apache.commons.beanutils.locale.BaseLocaleConverter;
023:
024: /**
025: * Base Test Case for the DecimalLocaleConverter classes. This class doesn't
026: * define any real tests; it just provides useful methods for the real
027: * test case classes to inherit.
028: *
029: * @author Niall Pemberton
030: * @version $Revision: 469728 $ $Date: 2006-11-01 01:08:34 +0000 (Wed, 01 Nov 2006) $
031: */
032:
033: public class BaseLocaleConverterTestCase extends TestCase {
034:
035: // Original Default Locale
036: protected Locale origLocale;
037:
038: // Converter
039: protected BaseLocaleConverter converter;
040: protected Object result;
041: protected Object defaultValue;
042: protected Object expectedValue;
043:
044: // Localized values
045: protected Locale localizedLocale;
046: protected String localizedDecimalPattern;
047: protected String localizedIntegerPattern;
048: protected String localizedDecimalValue;
049: protected String localizedIntegerValue;
050:
051: // Locale values
052: protected Locale defaultLocale;
053: protected String defaultDecimalPattern;
054: protected String defaultIntegerPattern;
055: protected String defaultDecimalValue;
056: protected String defaultIntegerValue;
057:
058: // Expected values
059: protected String expectedDecimalValue;
060: protected String expectedIntegerValue;
061:
062: // ---------------------------------------------------------- Constructors
063:
064: public BaseLocaleConverterTestCase(String name) {
065: super (name);
066: }
067:
068: // -------------------------------------------------- Overall Test Methods
069:
070: /**
071: * Set up instance variables required by this test case.
072: */
073: public void setUp() throws Exception {
074:
075: // Default Locale (Use US)
076: defaultLocale = Locale.US;
077: defaultDecimalPattern = "#,###.00";
078: defaultIntegerPattern = "#,###";
079: defaultDecimalValue = "1,234.56";
080: defaultIntegerValue = "1,234";
081:
082: // Use German Locale (uses different separators to US)
083: localizedLocale = Locale.GERMAN;
084: localizedDecimalPattern = "#.###,00";
085: localizedIntegerPattern = "#.###";
086: localizedDecimalValue = "1.234,56";
087: localizedIntegerValue = "1.234";
088:
089: // Expected Values
090: expectedDecimalValue = "1234.56";
091: expectedIntegerValue = "1234";
092:
093: // Reset default to the one specified
094: origLocale = Locale.getDefault();
095:
096: // Initialize
097: converter = null;
098: result = null;
099: defaultValue = null;
100: expectedValue = null;
101:
102: if (defaultLocale.equals(origLocale)) {
103: origLocale = null;
104: } else {
105: // System.out.println("Changing default locale from " + origLocale + " to " + defaultLocale);
106: Locale.setDefault(defaultLocale);
107: }
108:
109: }
110:
111: /**
112: * Tear down instance variables required by this test case.
113: */
114: public void tearDown() {
115:
116: converter = null;
117: result = null;
118: defaultValue = null;
119: expectedValue = null;
120:
121: // Set the Default Locale back to the original value
122: if (origLocale != null) {
123: // System.out.println("Restoring default locale to " + origLocale);
124: Locale.setDefault(origLocale);
125: }
126:
127: }
128:
129: // -------------------------------------------------- Generic Test Methods
130:
131: /**
132: * Test Converting Value WITH a pattern
133: */
134: protected void convertValueWithPattern(
135: BaseLocaleConverter converter, Object value,
136: String pattern, Object expectedValue) {
137: convertValueWithPattern(converter, "", value, pattern,
138: expectedValue);
139: }
140:
141: /**
142: * Test Converting Value WITH a pattern
143: */
144: protected void convertValueWithPattern(
145: BaseLocaleConverter converter, String msgId, Object value,
146: 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,
155: expectedValue, result);
156:
157: }
158:
159: /**
160: * Test Converting Value WITHOUT a pattern
161: */
162: protected void convertValueNoPattern(BaseLocaleConverter converter,
163: Object value, Object expectedValue) {
164: convertValueNoPattern(converter, "", value, expectedValue);
165: }
166:
167: /**
168: * Test Converting Value WITHOUT a pattern
169: */
170: protected void convertValueNoPattern(BaseLocaleConverter converter,
171: String msgId, Object value, Object expectedValue) {
172:
173: // Convert value with no pattern
174: try {
175: result = converter.convert(value);
176: } catch (Exception e) {
177: fail("No Pattern conversion threw " + msgId + " threw " + e);
178: }
179: assertEquals("Check conversion value without pattern " + msgId,
180: expectedValue, result);
181:
182: }
183:
184: /**
185: * Test Converting Value To a spcified Type
186: */
187: protected void convertValueToType(BaseLocaleConverter converter,
188: Class clazz, Object value, String pattern,
189: Object expectedValue) {
190: convertValueToType(converter, "", clazz, value, pattern,
191: expectedValue);
192: }
193:
194: /**
195: * Test Converting Value To a spcified Type
196: */
197: protected void convertValueToType(BaseLocaleConverter converter,
198: String msgId, Class clazz, Object value, String pattern,
199: Object expectedValue) {
200:
201: // Convert value with no pattern
202: try {
203: result = converter.convert(clazz, value, pattern);
204: } catch (Exception e) {
205: fail("Type conversion threw " + msgId + " threw " + e);
206: }
207: assertEquals("Check conversion value to type " + msgId,
208: expectedValue, result);
209:
210: }
211:
212: /**
213: * Test Converting Null value.
214: */
215: protected void convertNull(BaseLocaleConverter converter,
216: Object expectedValue) {
217: convertNull(converter, "", expectedValue);
218: }
219:
220: /**
221: * Test Converting Null value.
222: */
223: protected void convertNull(BaseLocaleConverter converter,
224: String msgId, Object expectedValue) {
225:
226: // Convert value with no pattern
227: try {
228: result = converter.convert(null);
229: } catch (Exception e) {
230: fail("Null conversion " + msgId + " threw " + e);
231: }
232:
233: if (expectedValue == null) {
234: assertNull("Check null conversion is null " + msgId
235: + " result=" + result, result);
236: } else {
237: assertEquals("Check null conversion is default " + msgId,
238: expectedValue, result);
239: }
240:
241: }
242:
243: /**
244: * Test Converting an invalid value.
245: */
246: protected void convertInvalid(BaseLocaleConverter converter,
247: Object expectedValue) {
248: convertInvalid(converter, "", expectedValue);
249: }
250:
251: /**
252: * Test Converting an invalid value.
253: */
254: protected void convertInvalid(BaseLocaleConverter converter,
255: String msgId, Object expectedValue) {
256:
257: // Convert value with no pattern
258: try {
259: result = converter.convert("xyz");
260: if (expectedValue == null) {
261: fail("Expected ConversionException if no default value "
262: + msgId);
263: }
264: } catch (Exception e) {
265: if (expectedValue != null) {
266: fail("Expected default value " + msgId + " threw " + e);
267: }
268: }
269:
270: if (expectedValue != null) {
271: assertEquals(
272: "Check invalid conversion is default " + msgId,
273: expectedValue, result);
274: }
275:
276: }
277:
278: /**
279: * This class isn't intended to perform any real tests; it just provides
280: * methods for the real test cases to inherit. However junit complains
281: * if a class named ..TestCase contains no test methods, so here we
282: * define a dummy one to keep it happy.
283: */
284: public void testNothing() {
285: }
286: }
|