### Question:
TypeConverter { static Object convertType(final Object value, final Column targetColumn) throws IllegalArgumentException { if (value == null) { return null; } Object result = value; final ColumnType type = targetColumn.getType(); if (type.isLiteral()) { if (value instanceof Number || value instanceof Date) { result = value.toString(); } } else if (type.isNumber()) { final Number numberValue = ConvertToNumberTransformer.transformValue(value); if (numberValue == null && !"".equals(value)) { throw new IllegalArgumentException("Could not convert " + value + " to number"); } result = numberValue; } else if (type == ColumnType.BOOLEAN) { final Boolean booleanValue = ConvertToBooleanTransformer.transformValue(value); if (booleanValue == null && !"".equals(value)) { throw new IllegalArgumentException("Could not convert " + value + " to boolean"); } result = booleanValue; } return result; }    }### Answer:
@Test public void shouldReturnAStringTypeForLiteralColumn() { final Column literalColumn = new MutableColumn("foo", ColumnType.CHAR); Object result = TypeConverter.convertType("bla", literalColumn); assertTrue(result instanceof String); assertEquals("bla", result); result = TypeConverter.convertType(24, literalColumn); assertTrue(result instanceof String); assertEquals("24", result); result = TypeConverter.convertType(24.3d, literalColumn); assertTrue(result instanceof String); assertEquals("24.3", result); result = TypeConverter.convertType(dateValue, literalColumn); assertTrue(result instanceof String); assertEquals(dateValue.toString(), result); }
@Test public void shouldReturnNumberForNumberColumn() { final Column numberColumn = new MutableColumn("foo", ColumnType.NUMERIC); Object result = TypeConverter.convertType(24, numberColumn); assertTrue(result instanceof Number); assertEquals(24, result); result = TypeConverter.convertType(24.3d, numberColumn); assertTrue(result instanceof Number); assertEquals(24.3, result); result = TypeConverter.convertType("33", numberColumn); assertTrue(result instanceof Number); assertEquals(33L, result); result = TypeConverter.convertType("33.3", numberColumn); assertTrue(result instanceof Number); assertEquals(33.3, result); result = TypeConverter.convertType(true, numberColumn); assertTrue(result instanceof Number); assertEquals(1, result); result = TypeConverter.convertType(false, numberColumn); assertTrue(result instanceof Number); assertEquals(0, result); }
@Test public void shouldReturnBooleanForBooleanColumn() { final Column booleanColumn = new MutableColumn("foo", ColumnType.BOOLEAN); Object result = TypeConverter.convertType(true, booleanColumn); assertTrue(result instanceof Boolean); assertEquals(true, result); result = TypeConverter.convertType(false, booleanColumn); assertTrue(result instanceof Boolean); assertEquals(false, result); }
@Test public void shouldNotConvertBooleanForLiteralColumn() { final Column literalColumn = new MutableColumn("foo", ColumnType.CHAR); final Object result = TypeConverter.convertType(false, literalColumn); assertTrue(result instanceof Boolean); assertEquals(false, result); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowForStringAndNumberColumn() { final Column numberColumn = new MutableColumn("foo", ColumnType.NUMERIC); TypeConverter.convertType("bla", numberColumn); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowForIntegerAndBooleanColumn() { final Column booleanColumn = new MutableColumn("foo", ColumnType.BOOLEAN); TypeConverter.convertType(42, booleanColumn); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowForDoubleAndBooleanColumn() { final Column booleanColumn = new MutableColumn("foo", ColumnType.BOOLEAN); TypeConverter.convertType(42.3d, booleanColumn); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowForStringAndBooleanColumn() { final Column booleanColumn = new MutableColumn("foo", ColumnType.BOOLEAN); TypeConverter.convertType("bla", booleanColumn); }