1 7 package com.inversoft.util.typeconverter.converters.test; 8 9 10 import junit.framework.TestCase; 11 12 import com.inversoft.util.typeconverter.TypeConversionException; 13 import com.inversoft.util.typeconverter.converters.NumberTypeConverter; 14 15 16 22 public class NumberTypeConverterTest extends TestCase { 23 24 25 public NumberTypeConverterTest(String name) { 26 super(name); 27 } 28 29 32 public void testStringNull() { 33 34 try { 35 NumberTypeConverter c = new NumberTypeConverter(); 36 Object obj; 37 38 obj = c.convertString(null, Byte .class); 39 assertNull(obj); 40 obj = c.convertString(null, Byte.TYPE); 41 assertEquals(new Byte ((byte) 0), obj); 42 43 obj = c.convertString(null, Short .class); 44 assertNull(obj); 45 obj = c.convertString(null, Short.TYPE); 46 assertEquals(new Short ((short)0), obj); 47 48 obj = c.convertString(null, Integer .class); 49 assertNull(obj); 50 obj = c.convertString(null, Integer.TYPE); 51 assertEquals(new Integer (0), obj); 52 53 obj = c.convertString(null, Long .class); 54 assertNull(obj); 55 obj = c.convertString(null, Long.TYPE); 56 assertEquals(new Long (0), obj); 57 58 obj = c.convertString(null, Float .class); 59 assertNull(obj); 60 obj = c.convertString(null, Float.TYPE); 61 assertEquals(new Float (0), obj); 62 63 obj = c.convertString(null, Double .class); 64 assertNull(obj); 65 obj = c.convertString(null, Double.TYPE); 66 assertEquals(new Double (0), obj); 67 } catch (TypeConversionException tce) { 68 fail(tce.toString()); 69 } 70 } 71 72 75 public void testString() { 76 77 try { 78 NumberTypeConverter c = new NumberTypeConverter(); 79 Object obj; 80 81 obj = c.convertString("42", Byte .class); 82 assertEquals(new Byte ((byte) 42), obj); 83 assertTrue(obj instanceof Byte ); 84 obj = c.convertString("42", Byte.TYPE); 85 assertEquals(new Byte ((byte) 42), obj); 86 assertTrue(obj instanceof Byte ); 87 88 obj = c.convertString("1", Short .class); 89 assertEquals(new Short ((short) 1), obj); 90 assertTrue(obj instanceof Short ); 91 obj = c.convertString("1", Short.TYPE); 92 assertEquals(new Short ((short) 1), obj); 93 assertTrue(obj instanceof Short ); 94 95 obj = c.convertString("2", Integer .class); 96 assertEquals(new Integer (2), obj); 97 assertTrue(obj instanceof Integer ); 98 obj = c.convertString("2", Integer.TYPE); 99 assertEquals(new Integer (2), obj); 100 assertTrue(obj instanceof Integer ); 101 102 obj = c.convertString("3", Long .class); 103 assertEquals(new Long (3), obj); 104 assertTrue(obj instanceof Long ); 105 obj = c.convertString("3", Long.TYPE); 106 assertEquals(new Long (3), obj); 107 assertTrue(obj instanceof Long ); 108 109 obj = c.convertString("2.5", Float .class); 110 assertEquals(new Float (2.5f), obj); 111 assertTrue(obj instanceof Float ); 112 obj = c.convertString("2.5", Float.TYPE); 113 assertEquals(new Float (2.5f), obj); 114 assertTrue(obj instanceof Float ); 115 116 obj = c.convertString("3.14", Double .class); 117 assertEquals(new Double (3.14), obj); 118 assertTrue(obj instanceof Double ); 119 obj = c.convertString("3.14", Double.TYPE); 120 assertEquals(new Double (3.14), obj); 121 assertTrue(obj instanceof Double ); 122 } catch (TypeConversionException tce) { 123 fail(tce.toString()); 124 } 125 } 126 127 130 public void testStringFail() { 131 try { 132 NumberTypeConverter c = new NumberTypeConverter(); 133 c.convertString("3.14", Object .class); 134 fail("Should have failed"); 135 } catch (TypeConversionException tce) { 136 } 138 } 139 140 143 public void testStringDelegates() { 144 145 try { 146 NumberTypeConverter c = new NumberTypeConverter(); 147 Object [] obj; 148 149 obj = (Object []) c.convertString("42,41", Byte [].class); 150 assertEquals(new Byte ((byte) 42), obj[0]); 151 assertEquals(new Byte ((byte) 41), obj[1]); 152 assertTrue(obj instanceof Byte []); 153 } catch (TypeConversionException tce) { 154 fail(tce.toString()); 155 } 156 } 157 158 161 public void testStringEmpty() { 162 163 try { 164 NumberTypeConverter c = new NumberTypeConverter(); 165 Object obj; 166 167 obj = c.convertString(" ", Byte .class); 168 assertNull(obj); 169 obj = c.convertString(" ", Byte.TYPE); 170 assertEquals(new Byte ((byte) 0), obj); 171 172 obj = c.convertString(" ", Short .class); 173 assertNull(obj); 174 obj = c.convertString(" ", Short.TYPE); 175 assertEquals(new Short ((short)0), obj); 176 177 obj = c.convertString(" ", Integer .class); 178 assertNull(obj); 179 obj = c.convertString(" ", Integer.TYPE); 180 assertEquals(new Integer (0), obj); 181 182 obj = c.convertString(" ", Long .class); 183 assertNull(obj); 184 obj = c.convertString(" ", Long.TYPE); 185 assertEquals(new Long (0), obj); 186 187 obj = c.convertString("", Float .class); 188 assertNull(obj); 189 obj = c.convertString("", Float.TYPE); 190 assertEquals(new Float (0), obj); 191 192 obj = c.convertString("", Double .class); 193 assertNull(obj); 194 obj = c.convertString("", Double.TYPE); 195 assertEquals(new Double (0), obj); 196 } catch (TypeConversionException tce) { 197 fail(tce.toString()); 198 } 199 } 200 201 204 public void testArrayNull() { 205 206 try { 207 NumberTypeConverter c = new NumberTypeConverter(); 208 Object obj; 209 210 obj = c.convertArray(null, Byte .class); 211 assertEquals(null, obj); 212 obj = c.convertArray(null, Byte.TYPE); 213 assertEquals(new Byte ((byte) 0), obj); 214 assertTrue(obj instanceof Byte ); 215 216 obj = c.convertArray(null, Short .class); 217 assertEquals(null, obj); 218 obj = c.convertArray(null, Short.TYPE); 219 assertEquals(new Short ((short) 0), obj); 220 assertTrue(obj instanceof Short ); 221 222 obj = c.convertArray(null, Integer .class); 223 assertEquals(null, obj); 224 obj = c.convertArray(null, Integer.TYPE); 225 assertEquals(new Integer (0), obj); 226 assertTrue(obj instanceof Integer ); 227 228 obj = c.convertArray(null, Long .class); 229 assertEquals(null, obj); 230 obj = c.convertArray(null, Long.TYPE); 231 assertEquals(new Long (0), obj); 232 assertTrue(obj instanceof Long ); 233 234 obj = c.convertArray(null, Float .class); 235 assertEquals(null, obj); 236 obj = c.convertArray(null, Float.TYPE); 237 assertEquals(new Float (0), obj); 238 assertTrue(obj instanceof Float ); 239 240 obj = c.convertString(null, Double .class); 241 assertEquals(null, obj); 242 obj = c.convertString(null, Double.TYPE); 243 assertEquals(new Double (0), obj); 244 assertTrue(obj instanceof Double ); 245 } catch (TypeConversionException tce) { 246 fail(tce.toString()); 247 } 248 } 249 250 253 public void testArray() { 254 255 try { 256 NumberTypeConverter c = new NumberTypeConverter(); 257 Object obj; 258 259 obj = c.convertArray(new String []{"42"}, Byte .class); 260 assertEquals(new Byte ((byte) 42), obj); 261 assertTrue(obj instanceof Byte ); 262 obj = c.convertString("42", Byte.TYPE); 263 assertEquals(new Byte ((byte) 42), obj); 264 assertTrue(obj instanceof Byte ); 265 266 obj = c.convertString("1", Short .class); 267 assertEquals(new Short ((short) 1), obj); 268 assertTrue(obj instanceof Short ); 269 obj = c.convertString("1", Short.TYPE); 270 assertEquals(new Short ((short) 1), obj); 271 assertTrue(obj instanceof Short ); 272 273 obj = c.convertString("2", Integer .class); 274 assertEquals(new Integer (2), obj); 275 assertTrue(obj instanceof Integer ); 276 obj = c.convertString("2", Integer.TYPE); 277 assertEquals(new Integer (2), obj); 278 assertTrue(obj instanceof Integer ); 279 280 obj = c.convertString("3", Long .class); 281 assertEquals(new Long (3), obj); 282 assertTrue(obj instanceof Long ); 283 obj = c.convertString("3", Long.TYPE); 284 assertEquals(new Long (3), obj); 285 assertTrue(obj instanceof Long ); 286 287 obj = c.convertString("2.5", Float .class); 288 assertEquals(new Float (2.5f), obj); 289 assertTrue(obj instanceof Float ); 290 obj = c.convertString("2.5", Float.TYPE); 291 assertEquals(new Float (2.5f), obj); 292 assertTrue(obj instanceof Float ); 293 294 obj = c.convertString("3.14", Double .class); 295 assertEquals(new Double (3.14), obj); 296 assertTrue(obj instanceof Double ); 297 obj = c.convertString("3.14", Double.TYPE); 298 assertEquals(new Double (3.14), obj); 299 assertTrue(obj instanceof Double ); 300 } catch (TypeConversionException tce) { 301 fail(tce.toString()); 302 } 303 } 304 } | Popular Tags |