1 16 17 package org.apache.commons.beanutils.converters; 18 19 import junit.framework.TestCase; 20 21 import org.apache.commons.beanutils.ConversionException; 22 import org.apache.commons.beanutils.Converter; 23 24 25 31 32 public abstract class NumberConverterTestBase extends TestCase { 33 34 36 public NumberConverterTestBase(String name) { 37 super(name); 38 } 39 40 42 protected abstract Converter makeConverter(); 43 protected abstract Class getExpectedType(); 44 45 47 50 public void testConvertNull() throws Exception { 51 try { 52 makeConverter().convert(getExpectedType(),null); 53 fail("Expected ConversionException"); 54 } catch(ConversionException e) { 55 } 57 } 58 59 63 public void testConvertNumber() throws Exception { 64 String [] message= { 65 "from Byte", 66 "from Short", 67 "from Integer", 68 "from Long", 69 "from Float", 70 "from Double" 71 }; 72 73 Object [] number = { 74 new Byte ((byte)7), 75 new Short ((short)8), 76 new Integer (9), 77 new Long (10), 78 new Float (11.1), 79 new Double (12.2) 80 }; 81 82 for(int i=0;i<number.length;i++) { 83 Object val = makeConverter().convert(getExpectedType(),number[i]); 84 assertNotNull("Convert " + message[i] + " should not be null",val); 85 assertTrue( 86 "Convert " + message[i] + " should return a " + getExpectedType().getName(), 87 getExpectedType().isInstance(val)); 88 } 89 } 90 } 91 92 | Popular Tags |