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.converters;
019:
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.beanutils.Converter;
023:
024: /**
025: * Test Case for the FloatConverter class.
026: *
027: * @author Rodney Waldhoff
028: * @version $Revision: 471348 $ $Date: 2006-11-05 02:59:26 +0000 (Sun, 05 Nov 2006) $
029: */
030:
031: public class FloatConverterTestCase extends NumberConverterTestBase {
032:
033: private Converter converter = null;
034:
035: // ------------------------------------------------------------------------
036:
037: public FloatConverterTestCase(String name) {
038: super (name);
039: }
040:
041: // ------------------------------------------------------------------------
042:
043: public void setUp() throws Exception {
044: converter = makeConverter();
045: numbers[0] = new Float("-12");
046: numbers[1] = new Float("13");
047: numbers[2] = new Float("-22");
048: numbers[3] = new Float("23");
049: }
050:
051: public static TestSuite suite() {
052: return new TestSuite(FloatConverterTestCase.class);
053: }
054:
055: public void tearDown() throws Exception {
056: converter = null;
057: }
058:
059: // ------------------------------------------------------------------------
060:
061: protected NumberConverter makeConverter() {
062: return new FloatConverter();
063: }
064:
065: protected NumberConverter makeConverter(Object defaultValue) {
066: return new FloatConverter(defaultValue);
067: }
068:
069: protected Class getExpectedType() {
070: return Float.class;
071: }
072:
073: // ------------------------------------------------------------------------
074:
075: public void testSimpleConversion() throws Exception {
076: String[] message = { "from String", "from String",
077: "from String", "from String", "from String",
078: "from String", "from String", "from Byte",
079: "from Short", "from Integer", "from Long",
080: "from Float", "from Double" };
081:
082: Object[] input = { String.valueOf(Float.MIN_VALUE), "-17.2",
083: "-1.1", "0.0", "1.1", "17.2",
084: String.valueOf(Float.MAX_VALUE), new Byte((byte) 7),
085: new Short((short) 8), new Integer(9), new Long(10),
086: new Float(11.1), new Double(12.2), };
087:
088: Float[] expected = { new Float(Float.MIN_VALUE),
089: new Float(-17.2), new Float(-1.1), new Float(0.0),
090: new Float(1.1), new Float(17.2),
091: new Float(Float.MAX_VALUE), new Float(7), new Float(8),
092: new Float(9), new Float(10), new Float(11.1),
093: new Float(12.2) };
094:
095: for (int i = 0; i < expected.length; i++) {
096: assertEquals(message[i] + " to Float", expected[i]
097: .floatValue(), ((Float) (converter.convert(
098: Float.class, input[i]))).floatValue(), 0.00001);
099: assertEquals(message[i] + " to float", expected[i]
100: .floatValue(), ((Float) (converter.convert(
101: Float.TYPE, input[i]))).floatValue(), 0.00001);
102: assertEquals(message[i] + " to null type", expected[i]
103: .floatValue(), ((Float) (converter.convert(null,
104: input[i]))).floatValue(), 0.00001);
105: }
106: }
107:
108: /**
109: * Test Invalid Amounts (too big/small)
110: */
111: public void testInvalidAmount() {
112: Converter converter = makeConverter();
113: Class clazz = Float.class;
114:
115: Double max = new Double(Float.MAX_VALUE);
116: Double tooBig = new Double(Double.MAX_VALUE);
117:
118: // Maximum
119: assertEquals("Maximum", new Float(Float.MAX_VALUE), converter
120: .convert(clazz, max));
121:
122: // Too Large
123: try {
124: assertEquals("Too Big", null, converter.convert(clazz,
125: tooBig));
126: fail("More than maximum, expected ConversionException");
127: } catch (Exception e) {
128: // expected result
129: }
130: }
131: }
|