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 java.text.DecimalFormat;
021: import org.apache.commons.beanutils.ConversionException;
022:
023: /**
024: * Test Case for the FloatLocaleConverter class.
025: *
026: * @author Niall Pemberton
027: * @version $Revision: 555489 $ $Date: 2007-07-12 05:53:26 +0100 (Thu, 12 Jul 2007) $
028: */
029:
030: public class FloatLocaleConverterTestCase extends
031: BaseLocaleConverterTestCase {
032:
033: // ---------------------------------------------------------- Constructors
034:
035: public FloatLocaleConverterTestCase(String name) {
036: super (name);
037: }
038:
039: // -------------------------------------------------- Overall Test Methods
040:
041: /**
042: * Set up instance variables required by this test case.
043: */
044: public void setUp() throws Exception {
045:
046: super .setUp();
047:
048: defaultValue = new Float("9.99");
049: expectedValue = new Float(expectedDecimalValue);
050:
051: }
052:
053: /**
054: * Tear down instance variables required by this test case.
055: */
056: public void tearDown() {
057: super .tearDown();
058: }
059:
060: // ------------------------------------------------------------------------
061:
062: /**
063: * Test Converter(defaultValue, locale, pattern, localizedPattern) constructor
064: */
065: public void testConstructorMain() {
066:
067: // ------------- Construct with localized pattern ------------
068: converter = new FloatLocaleConverter(defaultValue,
069: localizedLocale, localizedDecimalPattern, true);
070:
071: convertValueNoPattern(converter, "(A)", localizedDecimalValue,
072: expectedValue);
073: convertValueWithPattern(converter, "(A)",
074: localizedDecimalValue, localizedDecimalPattern,
075: expectedValue);
076: convertInvalid(converter, "(A)", defaultValue);
077: convertNull(converter, "(A)", defaultValue);
078:
079: // **************************************************************************
080: // Convert value in the wrong format - maybe you would expect it to throw an
081: // exception and return the default - it doesn't, DecimalFormat parses it
082: // quite happily turning "1,234.56" into "1.234"
083: // I guess this is one of the limitations of DecimalFormat
084: // **************************************************************************
085: convertValueNoPattern(converter, "(B)", defaultDecimalValue,
086: new Float("1.234"));
087:
088: // **************************************************************************
089: // Convert with non-localized pattern - this causes an exception in parse()
090: // but it gets swallowed in convert() method and returns default.
091: // **** IS THIS THE EXPECTED BEHAVIOUR? ****
092: // Maybe if the pattern is no good, we should use a default pattern rather
093: // than just returning the default value.
094: // **************************************************************************
095: convertValueWithPattern(converter, "(B)",
096: localizedDecimalValue, defaultDecimalPattern,
097: defaultValue);
098:
099: // **************************************************************************
100: // Convert with specified type
101: //
102: // BaseLocaleConverter completely ignores the type - so even if we specify
103: // Float.class here it still returns a Float.
104: // **** SHOULD IMPLEMENT THIS BEHAVIOUR ****
105: // **************************************************************************
106: convertValueToType(converter, "(B)", Integer.class,
107: localizedDecimalValue, localizedDecimalPattern,
108: expectedValue);
109:
110: // ------------- Construct with non-localized pattern ------------
111: converter = new FloatLocaleConverter(defaultValue,
112: localizedLocale, defaultDecimalPattern, false);
113:
114: convertValueNoPattern(converter, "(C)", localizedDecimalValue,
115: expectedValue);
116: convertValueWithPattern(converter, "(C)",
117: localizedDecimalValue, defaultDecimalPattern,
118: expectedValue);
119: convertInvalid(converter, "(C)", defaultValue);
120: convertNull(converter, "(C)", defaultValue);
121:
122: }
123:
124: /**
125: * Test Converter() constructor
126: *
127: * Uses the default locale, no default value
128: *
129: */
130: public void testConstructor_2() {
131:
132: // ------------- Construct using default locale ------------
133: converter = new FloatLocaleConverter();
134:
135: // Perform Tests
136: convertValueNoPattern(converter, defaultDecimalValue,
137: expectedValue);
138: convertValueWithPattern(converter, defaultDecimalValue,
139: defaultDecimalPattern, expectedValue);
140: convertInvalid(converter, null);
141: convertNull(converter, null);
142:
143: }
144:
145: /**
146: * Test Converter(locPattern) constructor
147: *
148: * Uses the default locale, no default value
149: *
150: */
151: public void testConstructor_3() {
152:
153: // ------------- Construct using localized pattern (default locale) --------
154: converter = new FloatLocaleConverter(true);
155:
156: // Perform Tests
157: convertValueNoPattern(converter, defaultDecimalValue,
158: expectedValue);
159: convertValueWithPattern(converter, defaultDecimalValue,
160: defaultDecimalPattern, expectedValue);
161: convertInvalid(converter, null);
162: convertNull(converter, null);
163:
164: }
165:
166: /**
167: * Test Converter(Locale) constructor
168: */
169: public void testConstructor_4() {
170:
171: // ------------- Construct using specified Locale --------
172: converter = new FloatLocaleConverter(localizedLocale);
173:
174: // Perform Tests
175: convertValueNoPattern(converter, localizedDecimalValue,
176: expectedValue);
177: convertValueWithPattern(converter, localizedDecimalValue,
178: defaultDecimalPattern, expectedValue);
179: convertInvalid(converter, null);
180: convertNull(converter, null);
181:
182: }
183:
184: /**
185: * Test Converter(Locale, locPattern) constructor
186: */
187: public void testConstructor_5() {
188:
189: // ------------- Construct using specified Locale --------
190: converter = new FloatLocaleConverter(localizedLocale, true);
191:
192: // Perform Tests
193: convertValueNoPattern(converter, localizedDecimalValue,
194: expectedValue);
195: convertValueWithPattern(converter, localizedDecimalValue,
196: localizedDecimalPattern, expectedValue);
197: convertInvalid(converter, null);
198: convertNull(converter, null);
199:
200: }
201:
202: /**
203: * Test Converter(Locale, pattern) constructor
204: */
205: public void testConstructor_6() {
206:
207: // ------------- Construct using specified Locale --------
208: converter = new FloatLocaleConverter(localizedLocale,
209: defaultDecimalPattern);
210:
211: // Perform Tests
212: convertValueNoPattern(converter, localizedDecimalValue,
213: expectedValue);
214: convertValueWithPattern(converter, localizedDecimalValue,
215: defaultDecimalPattern, expectedValue);
216: convertInvalid(converter, null);
217: convertNull(converter, null);
218:
219: }
220:
221: /**
222: * Test Converter(Locale, pattern, locPattern) constructor
223: */
224: public void testConstructor_7() {
225:
226: // ------------- Construct using specified Locale --------
227: converter = new FloatLocaleConverter(localizedLocale,
228: localizedDecimalPattern, true);
229:
230: // Perform Tests
231: convertValueNoPattern(converter, localizedDecimalValue,
232: expectedValue);
233: convertValueWithPattern(converter, localizedDecimalValue,
234: localizedDecimalPattern, expectedValue);
235: convertInvalid(converter, null);
236: convertNull(converter, null);
237:
238: }
239:
240: /**
241: * Test Converter(defaultValue) constructor
242: */
243: public void testConstructor_8() {
244:
245: // ------------- Construct using specified Locale --------
246: converter = new FloatLocaleConverter(defaultValue);
247:
248: // Perform Tests
249: convertValueNoPattern(converter, defaultDecimalValue,
250: expectedValue);
251: convertValueWithPattern(converter, defaultDecimalValue,
252: defaultDecimalPattern, expectedValue);
253: convertInvalid(converter, defaultValue);
254: convertNull(converter, defaultValue);
255:
256: }
257:
258: /**
259: * Test Converter(defaultValue, locPattern) constructor
260: */
261: public void testConstructor_9() {
262:
263: // ------------- Construct using specified Locale --------
264: converter = new FloatLocaleConverter(defaultValue, true);
265:
266: // Perform Tests
267: convertValueNoPattern(converter, defaultDecimalValue,
268: expectedValue);
269: convertValueWithPattern(converter, defaultDecimalValue,
270: defaultDecimalPattern, expectedValue);
271: convertInvalid(converter, defaultValue);
272: convertNull(converter, defaultValue);
273:
274: }
275:
276: /**
277: * Test Float limits
278: */
279: public void testFloatLimits() {
280:
281: converter = new FloatLocaleConverter(defaultLocale,
282: defaultDecimalPattern);
283: DecimalFormat fmt = new DecimalFormat(
284: "#.#############################################################");
285:
286: assertEquals(new Float(-0.12), converter.convert("-0.12"));
287: assertEquals("Positive Float.MAX_VALUE", new Float(
288: Float.MAX_VALUE), converter.convert(fmt
289: .format(Float.MAX_VALUE)));
290: assertEquals("Positive Float.MIN_VALUE", new Float(
291: Float.MIN_VALUE), converter.convert(fmt
292: .format(Float.MIN_VALUE)));
293:
294: assertEquals("Negative Float.MAX_VALUE", new Float(
295: Float.MAX_VALUE * -1), converter.convert(fmt
296: .format(Float.MAX_VALUE * -1)));
297: assertEquals("Negative Float.MIN_VALUE", new Float(
298: Float.MIN_VALUE * -1), converter.convert(fmt
299: .format(Float.MIN_VALUE * -1)));
300:
301: try {
302: converter.convert(fmt.format((double) Float.MAX_VALUE
303: * (double) 10));
304: fail("Positive Too Large should throw ConversionException");
305: } catch (ConversionException e) {
306: // expected result
307: }
308: try {
309: converter.convert(fmt.format((double) Float.MAX_VALUE
310: * (double) -10));
311: fail("Negative Too Large should throw ConversionException");
312: } catch (ConversionException e) {
313: // expected result
314: }
315:
316: try {
317: converter.convert(fmt.format((double) Float.MIN_VALUE
318: / (double) 10));
319: fail("Positive Too Small should throw ConversionException");
320: } catch (ConversionException e) {
321: // expected result
322: }
323: try {
324: converter.convert(fmt.format((double) Float.MIN_VALUE
325: / (double) -10));
326: fail("Negative Too Small should throw ConversionException");
327: } catch (ConversionException e) {
328: // expected result
329: }
330: }
331:
332: }
|