TypeConverter { public Object convert(Object obj) { Object newObject = null; switch (conversion) { case NO_CONVERSION: newObject = obj; break; case DOUBLE_TO_FLOAT: newObject = ((Double) obj).floatValue(); break; case INT_TO_SHORT: newObject = ((Integer) obj).shortValue(); break; case INT_TO_BYTE: newObject = ((Integer) obj).byteValue(); break; case STRING_TO_CHAR: newObject = ((CharSequence) obj).charAt(0); break; case NUM_TO_LONG: newObject = Long.parseLong(obj.toString()); break; default: newObject = null; } return newObject; } TypeConverter();  TypeConverter(int conversion); Object convert(Object obj); int getConversion(); final static int NO_CONVERSION; final static int DOUBLE_TO_FLOAT; final static int INT_TO_SHORT; final static int INT_TO_BYTE; final static int STRING_TO_CHAR; final static int NUM_TO_LONG; final static TypeConverter NO_CONVERTER; final static TypeConverter FLOAT_CONVERTER; final static TypeConverter SHORT_CONVERTER; final static TypeConverter BYTE_CONVERTER; final static TypeConverter CHAR_CONVERTER; final static TypeConverter LONG_CONVERTER; }
@Test public void testIntConversion() { TypeConverter converter = new TypeConverter(TypeConverter.INT_TO_SHORT); assertEquals((short) 100, converter.convert(100)); assertTrue(converter.convert(100) instanceof Short); converter = new TypeConverter(TypeConverter.INT_TO_BYTE); assertEquals((byte) 100, converter.convert(100)); assertTrue(converter.convert(102) instanceof Byte); }
@Test public void testStringConversion() { TypeConverter converter = new TypeConverter(TypeConverter.STRING_TO_CHAR); assertEquals('c', converter.convert("c")); assertTrue(converter.convert("c") instanceof Character); }
@Test public void testTypeConversion() { TypeConverter converter = new TypeConverter(); assertEquals("test", converter.convert("test")); assertTrue(converter.convert("test") instanceof String); }
@Test public void testDoubleConversion() { TypeConverter converter = new TypeConverter(TypeConverter.DOUBLE_TO_FLOAT); assertEquals(1.2f, converter.convert(1.2)); assertTrue(converter.convert(1.2) instanceof Float); }