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.TestCase;
019
020 import org.apache.commons.convert1.ConversionException;
021 import org.apache.commons.convert1.Converter;
022
023
024 /**
025 * Abstract base for <Number>Converter classes.
026 *
027 * @author Rodney Waldhoff
028 * @version $Id: NumberConverterTestBase.java 155441 2005-02-26 13:19:22Z dirkv $
029 */
030
031 public abstract class NumberConverterTestBase extends TestCase {
032
033 // ------------------------------------------------------------------------
034
035 public NumberConverterTestBase(String name) {
036 super(name);
037 }
038
039 // ------------------------------------------------------------------------
040
041 protected abstract Converter makeConverter();
042 protected abstract Class getExpectedType();
043
044 // ------------------------------------------------------------------------
045
046 /**
047 * Assumes ConversionException in response to covert(getExpectedType(),null).
048 */
049 public void testConvertNull() throws Exception {
050 try {
051 makeConverter().convert(getExpectedType(),null);
052 fail("Expected ConversionException");
053 } catch(ConversionException e) {
054 // expected
055 }
056 }
057
058 /**
059 * Assumes convert(getExpectedType(),Number) returns some non-null
060 * instance of getExpectedType().
061 */
062 public void testConvertNumber() throws Exception {
063 String[] message= {
064 "from Byte",
065 "from Short",
066 "from Integer",
067 "from Long",
068 "from Float",
069 "from Double"
070 };
071
072 Object[] number = {
073 new Byte((byte)7),
074 new Short((short)8),
075 new Integer(9),
076 new Long(10),
077 new Float(11.1),
078 new Double(12.2)
079 };
080
081 for(int i=0;i<number.length;i++) {
082 Object val = makeConverter().convert(getExpectedType(),number[i]);
083 assertNotNull("Convert " + message[i] + " should not be null",val);
084 assertTrue(
085 "Convert " + message[i] + " should return a " + getExpectedType().getName(),
086 getExpectedType().isInstance(val));
087 }
088 }
089 }
090