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: /**
021: * Test Case for the DoubleLocaleConverter class.
022: *
023: * @author Niall Pemberton
024: * @version $Revision: 469728 $ $Date: 2006-11-01 01:08:34 +0000 (Wed, 01 Nov 2006) $
025: */
026:
027: public class DoubleLocaleConverterTestCase extends
028: BaseLocaleConverterTestCase {
029:
030: // ---------------------------------------------------------- Constructors
031:
032: public DoubleLocaleConverterTestCase(String name) {
033: super (name);
034: }
035:
036: // -------------------------------------------------- Overall Test Methods
037:
038: /**
039: * Set up instance variables required by this test case.
040: */
041: public void setUp() throws Exception {
042:
043: super .setUp();
044:
045: defaultValue = new Double("9.99");
046: expectedValue = new Double(expectedDecimalValue);
047:
048: }
049:
050: /**
051: * Tear down instance variables required by this test case.
052: */
053: public void tearDown() {
054: super .tearDown();
055: }
056:
057: // ------------------------------------------------------------------------
058:
059: /**
060: * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
061: */
062: public void testConstructorMain() {
063:
064: // ------------- Construct with localized pattern ------------
065: converter = new DoubleLocaleConverter(defaultValue,
066: localizedLocale, localizedDecimalPattern, true);
067:
068: convertValueNoPattern(converter, "(A)", localizedDecimalValue,
069: expectedValue);
070: convertValueWithPattern(converter, "(A)",
071: localizedDecimalValue, localizedDecimalPattern,
072: expectedValue);
073: convertInvalid(converter, "(A)", defaultValue);
074: convertNull(converter, "(A)", defaultValue);
075:
076: // **************************************************************************
077: // Convert value in the wrong format - maybe you would expect it to throw an
078: // exception and return the default - it doesn't, DecimalFormat parses it
079: // quite happily turning "1,234.56" into "1.234"
080: // I guess this is one of the limitations of DecimalFormat
081: // **************************************************************************
082: convertValueNoPattern(converter, "(B)", defaultDecimalValue,
083: new Double("1.234"));
084:
085: // **************************************************************************
086: // Convert with non-localized pattern - this causes an exception in parse()
087: // but it gets swallowed in convert() method and returns default.
088: // **** IS THIS THE EXPECTED BEHAVIOUR? ****
089: // Maybe if the pattern is no good, we should use a default pattern rather
090: // than just returning the default value.
091: // **************************************************************************
092: convertValueWithPattern(converter, "(B)",
093: localizedDecimalValue, defaultDecimalPattern,
094: defaultValue);
095:
096: // **************************************************************************
097: // Convert with specified type
098: //
099: // BaseLocaleConverter completely ignores the type - so even if we specify
100: // Double.class here it still returns a Double.
101: // **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
102: // **************************************************************************
103: convertValueToType(converter, "(B)", Integer.class,
104: localizedDecimalValue, localizedDecimalPattern,
105: expectedValue);
106:
107: // ------------- Construct with non-localized pattern ------------
108: converter = new DoubleLocaleConverter(defaultValue,
109: localizedLocale, defaultDecimalPattern, false);
110:
111: convertValueNoPattern(converter, "(C)", localizedDecimalValue,
112: expectedValue);
113: convertValueWithPattern(converter, "(C)",
114: localizedDecimalValue, defaultDecimalPattern,
115: expectedValue);
116: convertInvalid(converter, "(C)", defaultValue);
117: convertNull(converter, "(C)", defaultValue);
118:
119: }
120:
121: /**
122: * Test Converter() constructor
123: *
124: * Uses the default locale, no default value
125: *
126: */
127: public void testConstructor_2() {
128:
129: // ------------- Construct using default locale ------------
130: converter = new DoubleLocaleConverter();
131:
132: // Perform Tests
133: convertValueNoPattern(converter, defaultDecimalValue,
134: expectedValue);
135: convertValueWithPattern(converter, defaultDecimalValue,
136: defaultDecimalPattern, expectedValue);
137: convertInvalid(converter, null);
138: convertNull(converter, null);
139:
140: }
141:
142: /**
143: * Test Converter(locPattern) constructor
144: *
145: * Uses the default locale, no default value
146: *
147: */
148: public void testConstructor_3() {
149:
150: // ------------- Construct using localized pattern (default locale) --------
151: converter = new DoubleLocaleConverter(true);
152:
153: // Perform Tests
154: convertValueNoPattern(converter, defaultDecimalValue,
155: expectedValue);
156: convertValueWithPattern(converter, defaultDecimalValue,
157: defaultDecimalPattern, expectedValue);
158: convertInvalid(converter, null);
159: convertNull(converter, null);
160:
161: }
162:
163: /**
164: * Test Converter(Locale) constructor
165: */
166: public void testConstructor_4() {
167:
168: // ------------- Construct using specified Locale --------
169: converter = new DoubleLocaleConverter(localizedLocale);
170:
171: // Perform Tests
172: convertValueNoPattern(converter, localizedDecimalValue,
173: expectedValue);
174: convertValueWithPattern(converter, localizedDecimalValue,
175: defaultDecimalPattern, expectedValue);
176: convertInvalid(converter, null);
177: convertNull(converter, null);
178:
179: }
180:
181: /**
182: * Test Converter(Locale, locPattern) constructor
183: */
184: public void testConstructor_5() {
185:
186: // ------------- Construct using specified Locale --------
187: converter = new DoubleLocaleConverter(localizedLocale, true);
188:
189: // Perform Tests
190: convertValueNoPattern(converter, localizedDecimalValue,
191: expectedValue);
192: convertValueWithPattern(converter, localizedDecimalValue,
193: localizedDecimalPattern, expectedValue);
194: convertInvalid(converter, null);
195: convertNull(converter, null);
196:
197: }
198:
199: /**
200: * Test Converter(Locale, pattern) constructor
201: */
202: public void testConstructor_6() {
203:
204: // ------------- Construct using specified Locale --------
205: converter = new DoubleLocaleConverter(localizedLocale,
206: defaultDecimalPattern);
207:
208: // Perform Tests
209: convertValueNoPattern(converter, localizedDecimalValue,
210: expectedValue);
211: convertValueWithPattern(converter, localizedDecimalValue,
212: defaultDecimalPattern, expectedValue);
213: convertInvalid(converter, null);
214: convertNull(converter, null);
215:
216: }
217:
218: /**
219: * Test Converter(Locale, pattern, locPattern) constructor
220: */
221: public void testConstructor_7() {
222:
223: // ------------- Construct using specified Locale --------
224: converter = new DoubleLocaleConverter(localizedLocale,
225: localizedDecimalPattern, true);
226:
227: // Perform Tests
228: convertValueNoPattern(converter, localizedDecimalValue,
229: expectedValue);
230: convertValueWithPattern(converter, localizedDecimalValue,
231: localizedDecimalPattern, expectedValue);
232: convertInvalid(converter, null);
233: convertNull(converter, null);
234:
235: }
236:
237: /**
238: * Test Converter(defaultValue) constructor
239: */
240: public void testConstructor_8() {
241:
242: // ------------- Construct using specified Locale --------
243: converter = new DoubleLocaleConverter(defaultValue);
244:
245: // Perform Tests
246: convertValueNoPattern(converter, defaultDecimalValue,
247: expectedValue);
248: convertValueWithPattern(converter, defaultDecimalValue,
249: defaultDecimalPattern, expectedValue);
250: convertInvalid(converter, defaultValue);
251: convertNull(converter, defaultValue);
252:
253: }
254:
255: /**
256: * Test Converter(defaultValue, locPattern) constructor
257: */
258: public void testConstructor_9() {
259:
260: // ------------- Construct using specified Locale --------
261: converter = new DoubleLocaleConverter(defaultValue, true);
262:
263: // Perform Tests
264: convertValueNoPattern(converter, defaultDecimalValue,
265: expectedValue);
266: convertValueWithPattern(converter, defaultDecimalValue,
267: defaultDecimalPattern, expectedValue);
268: convertInvalid(converter, defaultValue);
269: convertNull(converter, defaultValue);
270:
271: }
272:
273: }
|