001    /*
002     *  Copyright 2003-2004 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.apache.commons.convert1.string;
017    
018    import junit.framework.TestSuite;
019    
020    import org.apache.commons.convert1.Converter;
021    
022    
023    /**
024     * Test Case for the IntegerConverter class.
025     *
026     * @author Rodney Waldhoff
027     * @version $Id: IntegerConverterTestCase.java 155441 2005-02-26 13:19:22Z dirkv $
028     */
029    
030    public class IntegerConverterTestCase extends NumberConverterTestBase {
031    
032        private Converter converter = null;
033    
034        // ------------------------------------------------------------------------
035    
036        public IntegerConverterTestCase(String name) {
037            super(name);
038        }
039        
040        // ------------------------------------------------------------------------
041    
042        public void setUp() throws Exception {
043            converter = makeConverter();
044        }
045    
046        public static TestSuite suite() {
047            return new TestSuite(IntegerConverterTestCase.class);        
048        }
049    
050        public void tearDown() throws Exception {
051            converter = null;
052        }
053    
054        // ------------------------------------------------------------------------
055        
056        protected Converter makeConverter() {
057            return new IntegerConverter();
058        }
059        
060        protected Class getExpectedType() {
061            return Integer.class;
062        }
063    
064        // ------------------------------------------------------------------------
065    
066        public void testSimpleConversion() throws Exception {
067            String[] message= { 
068                "from String",
069                "from String",
070                "from String",
071                "from String",
072                "from String",
073                "from String",
074                "from String",
075                "from Byte",
076                "from Short",
077                "from Integer",
078                "from Long",
079                "from Float",
080                "from Double"
081            };
082            
083            Object[] input = { 
084                String.valueOf(Integer.MIN_VALUE),
085                "-17",
086                "-1",
087                "0",
088                "1",
089                "17",
090                String.valueOf(Integer.MAX_VALUE),
091                new Byte((byte)7),
092                new Short((short)8),
093                new Integer(9),
094                new Long(10),
095                new Float(11.1),
096                new Double(12.2)
097            };
098            
099            Integer[] expected = { 
100                new Integer(Integer.MIN_VALUE),
101                new Integer(-17),
102                new Integer(-1),
103                new Integer(0),
104                new Integer(1),
105                new Integer(17),
106                new Integer(Integer.MAX_VALUE),
107                new Integer(7),
108                new Integer(8),
109                new Integer(9),
110                new Integer(10),
111                new Integer(11),
112                new Integer(12)
113            };
114            
115            for(int i=0;i<expected.length;i++) {
116                assertEquals(message[i] + " to Integer",expected[i],converter.convert(Integer.class,input[i]));
117                assertEquals(message[i] + " to int",expected[i],converter.convert(Integer.TYPE,input[i]));
118                assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
119            }
120        }
121        
122    }
123