StringToNumberDecoder implements RpcDecoder { @Override public <T> T decode(JsonValue value, Class<T> type) throws RpcDecodeException { String stringValue = value.asString(); try { Number number = parseNumber(stringValue); if (Number.class.equals(type)) { return type.cast(number); } Field requiredType = type.getField("TYPE"); Class<?> primitiveRequiredType = (Class<?>) requiredType.get(null); Method method = Number.class .getMethod(primitiveRequiredType + "Value"); T result = type.cast(method.invoke(number)); if (number.equals(parseNumber(result.toString()))) { return result; } else { throw new RpcDecodeException( String.format("Can't decode '%s' to type '%s'", stringValue, type.getName())); } } catch (ParseException exception) { throw new RpcDecodeException(exception); } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | InvocationTargetException exception) { throw new RuntimeException(exception); } }  @Override boolean isApplicable(JsonValue value, Class<?> type); @Override T decode(JsonValue value, Class<T> type);  }
@Test public void stringToInteger_convertableString_valueIsConverted() throws RpcDecodeException { Integer expected = 37; Integer value = decoder.decode(Json.create(String.valueOf(expected)), Integer.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToInteger_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Integer.class); }
@Test(expected = RpcDecodeException.class) public void stringToInteger_doubleString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("4.2"), Integer.class); }
@Test(expected = RpcDecodeException.class) public void stringToInteger_longString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create(String.valueOf(Long.MAX_VALUE)), Integer.class); }
@Test public void stringToLong_convertableString_valueIsConverted() throws RpcDecodeException { Long expected = 37l; Long value = decoder.decode(Json.create(String.valueOf(expected)), Long.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToLong_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Long.class); }
@Test(expected = RpcDecodeException.class) public void stringToLong_doubleString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("4.2"), Long.class); }
@Test public void stringToShort_convertableString_valueIsConverted() throws RpcDecodeException { Short expected = 37; Short value = decoder.decode(Json.create(String.valueOf(expected)), Short.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToShort_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Short.class); }
@Test(expected = RpcDecodeException.class) public void stringToShort_intString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create(Integer.MAX_VALUE), Short.class); }
@Test(expected = RpcDecodeException.class) public void stringToShort_doubleString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("4.2"), Short.class); }
@Test public void stringToByte_convertableString_valueIsConverted() throws RpcDecodeException { Byte expected = 37; Byte value = decoder.decode(Json.create(String.valueOf(expected)), Byte.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToByte_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Byte.class); }
@Test(expected = RpcDecodeException.class) public void stringToByte_intString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create(Short.MAX_VALUE), Byte.class); }
@Test(expected = RpcDecodeException.class) public void stringToByte_doubleString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("4.2"), Byte.class); }
@Test public void stringToFloat_convertableString_valueIsConverted() throws RpcDecodeException { Float expected = 37.72f; Float value = decoder.decode(Json.create(String.valueOf(expected)), Float.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToFloat_doubleString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create(String.valueOf(Double.MIN_NORMAL)), Float.class); }
@Test(expected = RpcDecodeException.class) public void stringToFloat_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Float.class); }
@Test public void stringToDouble_convertableString_valueIsConverted() throws RpcDecodeException { Double expected = 823.6349d; Double value = decoder.decode(Json.create(String.valueOf(expected)), Double.class); Assert.assertEquals(expected, value); }
@Test public void stringToDouble_minDoubleString_valueIsConverted() throws RpcDecodeException { Double expected = Double.MIN_NORMAL; Double value = decoder.decode(Json.create(String.valueOf(expected)), Double.class); Assert.assertEquals(expected, value); }
@Test(expected = RpcDecodeException.class) public void stringToDoublet_nonConvertableString_exceptionIsThrown() throws RpcDecodeException { decoder.decode(Json.create("abc"), Double.class); }
@Test public void stringToNumber_convertableString_valueIsConverted() throws RpcDecodeException { Double expected = 823.6349d; Number value = decoder.decode(Json.create(String.valueOf(expected)), Number.class); Assert.assertEquals(expected, value); }