SchemaValueConverter { public static Object convert(Schema.Type type, Object value) { if (value == null) { return null; } switch (type) { case BOOLEAN: if (value instanceof Boolean) { return value; } else if (value instanceof String) { return Boolean.valueOf((String) value); } break; case BYTE: case SHORT: case INTEGER: case LONG: case FLOAT: case DOUBLE: return convertNumber(type, value); case STRING: if (value instanceof String) { return value; } break; case BINARY: if (value instanceof byte[]) { return value; } break; case NESTED: if (value instanceof Map) { return value; } break; case ARRAY: if (value instanceof List) { return value; } break; } throw new IllegalArgumentException("Cannot convert value " + value + " with Java type class " + value.getClass() + " to type " + type); } private  SchemaValueConverter(); static Map<String, Object> convertMap(Schema schema, Map<String, Object> value); static Object convert(Schema.Type type, Object value);  }
@Test public void convertBooleanToBoolean() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.BOOLEAN, true); Assert.assertTrue(Schema.Type.BOOLEAN.valueOfSameType(converted)); Assert.assertEquals(Boolean.TRUE, converted); converted = SchemaValueConverter.convert(Schema.Type.BOOLEAN, false); Assert.assertTrue(Schema.Type.BOOLEAN.valueOfSameType(converted)); Assert.assertEquals(Boolean.FALSE, converted); }
@Test public void convertStringToBoolean() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.BOOLEAN, "true"); Assert.assertTrue(Schema.Type.BOOLEAN.valueOfSameType(converted)); Assert.assertEquals(Boolean.TRUE, converted); converted = SchemaValueConverter.convert(Schema.Type.BOOLEAN, "False"); Assert.assertTrue(Schema.Type.BOOLEAN.valueOfSameType(converted)); Assert.assertEquals(Boolean.FALSE, converted); converted = SchemaValueConverter.convert(Schema.Type.BOOLEAN, "hello"); Assert.assertTrue(Schema.Type.BOOLEAN.valueOfSameType(converted)); Assert.assertEquals(Boolean.FALSE, converted); }
@Test(expected = IllegalArgumentException.class) public void convertIntegerToBoolean() { SchemaValueConverter.convert(Schema.Type.BOOLEAN, 123); }
@Test public void convertByteToByte() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.BYTE, Byte.MAX_VALUE); Assert.assertTrue(Schema.Type.BYTE.valueOfSameType(converted)); Assert.assertEquals(Byte.MAX_VALUE, converted); converted = SchemaValueConverter.convert(Schema.Type.BYTE, Byte.MIN_VALUE); Assert.assertTrue(Schema.Type.BYTE.valueOfSameType(converted)); Assert.assertEquals(Byte.MIN_VALUE, converted); }
@Test(expected = IllegalArgumentException.class) public void convertBiggerThanByteToByte() { SchemaValueConverter.convert(Schema.Type.BYTE, Byte.MAX_VALUE + 1); }
@Test(expected = IllegalArgumentException.class) public void convertLessThanByteToByte() { SchemaValueConverter.convert(Schema.Type.BYTE, Byte.MIN_VALUE - 1); }
@Test public void convertShortToShort() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.SHORT, Short.MAX_VALUE); Assert.assertTrue(Schema.Type.SHORT.valueOfSameType(converted)); Assert.assertEquals(Short.MAX_VALUE, converted); converted = SchemaValueConverter.convert(Schema.Type.SHORT, Short.MIN_VALUE); Assert.assertTrue(Schema.Type.SHORT.valueOfSameType(converted)); Assert.assertEquals(Short.MIN_VALUE, converted); }
@Test(expected = IllegalArgumentException.class) public void convertBiggerThanShortToShort() { SchemaValueConverter.convert(Schema.Type.SHORT, Short.MAX_VALUE + 1); }
@Test(expected = IllegalArgumentException.class) public void convertLessThanShortToShort() { SchemaValueConverter.convert(Schema.Type.SHORT, Short.MIN_VALUE - 1); }
@Test public void convertIntegerToInteger() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.INTEGER, Integer.MAX_VALUE); Assert.assertTrue(Schema.Type.INTEGER.valueOfSameType(converted)); Assert.assertEquals(Integer.MAX_VALUE, converted); converted = SchemaValueConverter.convert(Schema.Type.INTEGER, Integer.MIN_VALUE); Assert.assertTrue(Schema.Type.INTEGER.valueOfSameType(converted)); Assert.assertEquals(Integer.MIN_VALUE, converted); }
@Test(expected = IllegalArgumentException.class) public void convertBiggerThanIntegerToInteger() { SchemaValueConverter.convert(Schema.Type.INTEGER, Integer.MAX_VALUE + 1L); }
@Test(expected = IllegalArgumentException.class) public void convertLessThanIntegerToInteger() { SchemaValueConverter.convert(Schema.Type.INTEGER, Integer.MIN_VALUE - 1L); }
@Test public void convertLongToLong() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.LONG, Long.MAX_VALUE); Assert.assertTrue(Schema.Type.LONG.valueOfSameType(converted)); Assert.assertEquals(Long.MAX_VALUE, converted); converted = SchemaValueConverter.convert(Schema.Type.LONG, Long.MIN_VALUE); Assert.assertTrue(Schema.Type.LONG.valueOfSameType(converted)); Assert.assertEquals(Long.MIN_VALUE, converted); }
@Test public void convertFloatToFloat() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.FLOAT, Float.MAX_VALUE); Assert.assertTrue(Schema.Type.FLOAT.valueOfSameType(converted)); Assert.assertEquals(Float.MAX_VALUE, (float) converted, 0.00001); converted = SchemaValueConverter.convert(Schema.Type.FLOAT, Float.MIN_VALUE); Assert.assertTrue(Schema.Type.FLOAT.valueOfSameType(converted)); Assert.assertEquals(Float.MIN_VALUE, (float) converted, 0.00001); }
@Test public void convertFloatToDouble() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Float.MAX_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Float.MAX_VALUE, (double) converted, 0.00001); converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Float.MIN_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Float.MIN_VALUE, (double) converted, 0.00001); }
@Test public void convertDoubleToDouble() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Double.MAX_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Double.MAX_VALUE, (double) converted, 0.00001); converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Double.MIN_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Double.MIN_VALUE, (double) converted, 0.00001); }
@Test public void convertIntegerToDouble() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Integer.MAX_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Integer.MAX_VALUE * 1.0, (double) converted, 0.00001); converted = SchemaValueConverter.convert(Schema.Type.DOUBLE, Integer.MIN_VALUE); Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted)); Assert.assertEquals(Integer.MIN_VALUE * 1.0, (double) converted, 0.00001); }
@Test public void convertStringToString() throws ParserException { Object converted = SchemaValueConverter.convert(Schema.Type.STRING, "hello"); Assert.assertTrue(Schema.Type.STRING.valueOfSameType(converted)); Assert.assertEquals("hello", converted); }
@Test(expected = IllegalArgumentException.class) public void convertNonStringToString() { SchemaValueConverter.convert(Schema.Type.STRING, 123); }
@Test public void convertByteArrayToBinary() throws ParserException { byte[] bytes = {0x01, 0x02}; Object converted = SchemaValueConverter.convert(Schema.Type.BINARY, bytes); Assert.assertTrue(Schema.Type.BINARY.valueOfSameType(converted)); Assert.assertEquals(bytes, converted); }
@Test(expected = IllegalArgumentException.class) public void convertStringArrayToBinary() { String[] bytes = { "0x01", "0x02" }; SchemaValueConverter.convert(Schema.Type.BINARY, bytes); }
@Test public void convertMapToNested() throws ParserException { Map<String, String> value = Collections.singletonMap("hello", "world"); Object converted = SchemaValueConverter.convert(Schema.Type.NESTED, value); Assert.assertTrue(Schema.Type.NESTED.valueOfSameType(converted)); Assert.assertEquals(value, converted); }
@Test(expected = IllegalArgumentException.class) public void convertListToNested() { List<String> value = Collections.singletonList("hello"); SchemaValueConverter.convert(Schema.Type.NESTED, value); }
@Test public void convertListToArray() throws ParserException { List<String> value = Collections.singletonList("hello"); Object converted = SchemaValueConverter.convert(Schema.Type.ARRAY, value); Assert.assertTrue(Schema.Type.ARRAY.valueOfSameType(converted)); Assert.assertEquals(value, converted); }
@Test(expected = IllegalArgumentException.class) public void convertMapToArray() { Map<String, String> value = Collections.singletonMap("hello", "world"); SchemaValueConverter.convert(Schema.Type.ARRAY, value); }