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