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