1 7 package com.inversoft.util.typeconverter.test; 8 9 10 import junit.framework.TestCase; 11 12 import com.inversoft.util.typeconverter.BaseTypeConverter; 13 import com.inversoft.util.typeconverter.TypeConversionException; 14 import com.inversoft.util.typeconverter.TypeConverter; 15 import com.inversoft.util.typeconverter.TypeConverterRegistry; 16 import com.inversoft.util.typeconverter.converters.BooleanTypeConverter; 17 import com.inversoft.util.typeconverter.converters.CharacterTypeConverter; 18 import com.inversoft.util.typeconverter.converters.NumberTypeConverter; 19 20 21 27 public class TypeConverterTest extends TestCase { 28 29 30 public TypeConverterTest(String name) { 31 super(name); 32 } 33 34 37 public void testLookups() { 38 TypeConverter tc = TypeConverterRegistry.lookup(Character .class); 39 assertEquals(CharacterTypeConverter.class, tc.getClass()); 40 tc = TypeConverterRegistry.lookup(Character.TYPE); 41 assertEquals(CharacterTypeConverter.class, tc.getClass()); 42 tc = TypeConverterRegistry.lookup(Character [].class); 43 assertEquals(CharacterTypeConverter.class, tc.getClass()); 44 tc = TypeConverterRegistry.lookup(char[].class); 45 assertEquals(CharacterTypeConverter.class, tc.getClass()); 46 47 tc = TypeConverterRegistry.lookup(Byte .class); 48 assertEquals(NumberTypeConverter.class, tc.getClass()); 49 tc = TypeConverterRegistry.lookup(Byte.TYPE); 50 assertEquals(NumberTypeConverter.class, tc.getClass()); 51 tc = TypeConverterRegistry.lookup(Byte [].class); 52 assertEquals(NumberTypeConverter.class, tc.getClass()); 53 tc = TypeConverterRegistry.lookup(byte[].class); 54 assertEquals(NumberTypeConverter.class, tc.getClass()); 55 56 tc = TypeConverterRegistry.lookup(Short .class); 57 assertEquals(NumberTypeConverter.class, tc.getClass()); 58 tc = TypeConverterRegistry.lookup(Short.TYPE); 59 assertEquals(NumberTypeConverter.class, tc.getClass()); 60 tc = TypeConverterRegistry.lookup(Short [].class); 61 assertEquals(NumberTypeConverter.class, tc.getClass()); 62 tc = TypeConverterRegistry.lookup(short[].class); 63 assertEquals(NumberTypeConverter.class, tc.getClass()); 64 65 tc = TypeConverterRegistry.lookup(Integer .class); 66 assertEquals(NumberTypeConverter.class, tc.getClass()); 67 tc = TypeConverterRegistry.lookup(Integer.TYPE); 68 assertEquals(NumberTypeConverter.class, tc.getClass()); 69 tc = TypeConverterRegistry.lookup(Integer [].class); 70 assertEquals(NumberTypeConverter.class, tc.getClass()); 71 tc = TypeConverterRegistry.lookup(int[].class); 72 assertEquals(NumberTypeConverter.class, tc.getClass()); 73 74 tc = TypeConverterRegistry.lookup(Long .class); 75 assertEquals(NumberTypeConverter.class, tc.getClass()); 76 tc = TypeConverterRegistry.lookup(Long.TYPE); 77 assertEquals(NumberTypeConverter.class, tc.getClass()); 78 tc = TypeConverterRegistry.lookup(Long [].class); 79 assertEquals(NumberTypeConverter.class, tc.getClass()); 80 tc = TypeConverterRegistry.lookup(long[].class); 81 assertEquals(NumberTypeConverter.class, tc.getClass()); 82 83 tc = TypeConverterRegistry.lookup(Float .class); 84 assertEquals(NumberTypeConverter.class, tc.getClass()); 85 tc = TypeConverterRegistry.lookup(Float.TYPE); 86 assertEquals(NumberTypeConverter.class, tc.getClass()); 87 tc = TypeConverterRegistry.lookup(Float [].class); 88 assertEquals(NumberTypeConverter.class, tc.getClass()); 89 tc = TypeConverterRegistry.lookup(float[].class); 90 assertEquals(NumberTypeConverter.class, tc.getClass()); 91 92 tc = TypeConverterRegistry.lookup(Double .class); 93 assertEquals(NumberTypeConverter.class, tc.getClass()); 94 tc = TypeConverterRegistry.lookup(Double.TYPE); 95 assertEquals(NumberTypeConverter.class, tc.getClass()); 96 tc = TypeConverterRegistry.lookup(Double [].class); 97 assertEquals(NumberTypeConverter.class, tc.getClass()); 98 tc = TypeConverterRegistry.lookup(double[].class); 99 assertEquals(NumberTypeConverter.class, tc.getClass()); 100 101 tc = TypeConverterRegistry.lookup(Boolean .class); 102 assertEquals(BooleanTypeConverter.class, tc.getClass()); 103 tc = TypeConverterRegistry.lookup(Boolean.TYPE); 104 assertEquals(BooleanTypeConverter.class, tc.getClass()); 105 tc = TypeConverterRegistry.lookup(Boolean [].class); 106 assertEquals(BooleanTypeConverter.class, tc.getClass()); 107 tc = TypeConverterRegistry.lookup(boolean[].class); 108 assertEquals(BooleanTypeConverter.class, tc.getClass()); 109 110 tc = TypeConverterRegistry.lookup(String .class); 111 assertEquals(BaseTypeConverter.class, tc.getClass()); 112 tc = TypeConverterRegistry.lookup(String [].class); 113 assertEquals(BaseTypeConverter.class, tc.getClass()); 114 } 115 116 119 public void testLookupFailures() { 120 TypeConverter tc = TypeConverterRegistry.lookup(this.getClass()); 121 assertNull(tc); 122 } 123 124 127 public void testRegistration() { 128 TypeConverter tc = new NumberTypeConverter(); 129 TypeConverterRegistry.register(this.getClass(), tc); 130 assertSame(tc, TypeConverterRegistry.lookup(this.getClass())); 131 132 TypeConverterRegistry.unregister(this.getClass()); 133 assertNull(TypeConverterRegistry.lookup(this.getClass())); 134 } 135 136 139 public void testConvertCallsStringToArray() { 140 141 try { 142 BaseTypeConverter c = new BaseTypeConverter(); 144 Object obj = c.convert("foo,bar,john", String [].class); 145 146 if (obj instanceof String []) { 147 String [] array = (String []) obj; 148 assertTrue("Length should be three", array.length == 3); 149 assertTrue("First indices should be foo", array[0].equals("foo")); 150 assertTrue("Second indices should be bar", array[1].equals("bar")); 151 assertTrue("Third indices should be john", array[2].equals("john")); 152 } else { 153 fail("Type is not correct: " + obj.getClass().getName()); 154 } 155 156 c = new BaseTypeConverter(); 157 obj = c.convert("", String [].class); 158 assertNotNull(obj); 159 assertEquals(0, ((Object []) obj).length); 160 } catch (TypeConversionException tce) { 161 fail(tce.toString()); 162 } 163 } 164 165 168 public void testConvertCallsString() { 169 170 try { 171 BaseTypeConverter c = new BaseTypeConverter(); 173 Object obj = c.convert("foo,bar,john", String .class); 174 175 if (obj instanceof String ) { 176 assertEquals(obj, "foo,bar,john"); 177 } else { 178 fail("Type is not correct: " + obj.getClass().getName()); 179 } 180 181 c = new BaseTypeConverter(); 182 obj = c.convert("", String .class); 183 assertEquals("", obj); 184 } catch (TypeConversionException tce) { 185 fail(tce.toString()); 186 } 187 } 188 189 192 public void testConvertToArray() { 193 194 try { 195 BaseTypeConverter c = new BaseTypeConverter(); 197 Object obj = c.convertToArray(null, String [].class); 198 assertNull(obj); 199 } catch (TypeConversionException tce) { 200 fail(tce.toString()); 201 } 202 } 203 204 205 208 public void testConvertStringCallsStringToArray() { 209 210 try { 211 BaseTypeConverter c = new BaseTypeConverter(); 213 Object [] obj = (Object []) c.convertString("john,foo,bar", String [].class); 214 assertNotNull(obj); 215 assertEquals(3, obj.length); 216 assertEquals("john", obj[0]); 217 assertEquals("foo", obj[1]); 218 assertEquals("bar", obj[2]); 219 } catch (TypeConversionException tce) { 220 fail(tce.toString()); 221 } 222 } 223 } | Popular Tags |