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 ShortConverter class.
025     *
026     * @author Rodney Waldhoff
027     * @version $Id: ShortConverterTestCase.java 155441 2005-02-26 13:19:22Z dirkv $
028     */
029    
030    public class ShortConverterTestCase extends NumberConverterTestBase {
031    
032        private Converter converter = null;
033    
034        // ------------------------------------------------------------------------
035    
036        public ShortConverterTestCase(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(ShortConverterTestCase.class);        
048        }
049    
050        public void tearDown() throws Exception {
051            converter = null;
052        }
053    
054        // ------------------------------------------------------------------------
055        
056        protected Converter makeConverter() {
057            return new ShortConverter();
058        }
059        
060        protected Class getExpectedType() {
061            return Short.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(Short.MIN_VALUE),
085                "-17",
086                "-1",
087                "0",
088                "1",
089                "17",
090                String.valueOf(Short.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            Short[] expected = { 
100                new Short(Short.MIN_VALUE),
101                new Short((short)-17),
102                new Short((short)-1),
103                new Short((short)0),
104                new Short((short)1),
105                new Short((short)17),
106                new Short(Short.MAX_VALUE),
107                new Short((short)7),
108                new Short((short)8),
109                new Short((short)9),
110                new Short((short)10),
111                new Short((short)11),
112                new Short((short)12)
113            };
114            
115            for(int i=0;i<expected.length;i++) {
116                assertEquals(message[i] + " to Short",expected[i],converter.convert(Short.class,input[i]));
117                assertEquals(message[i] + " to short",expected[i],converter.convert(Short.TYPE,input[i]));
118                assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
119            }
120        }
121        
122    }
123