MapValueGetter { public static BigDecimal getAsBigDecimal(Map<String, Object> map, String key) { Object value = map.get(key); if (value instanceof BigDecimal) { return (BigDecimal) value; } if (value == null) { return BigDecimal.ZERO; } if (value instanceof String) { return new BigDecimal((String) value); } throw new WrongTypeException(key, BigDecimal.class, value); } private  MapValueGetter(); static T getAsType(Map<String, Object> map, String key, Class<T> objectType); static BigDecimal getAsBigDecimal(Map<String, Object> map, String key); static BigInteger getAsBigInteger(Map<String, Object> map, String key); static boolean getAsBoolean(Map<String, Object> map, String key); static byte getAsByte(Map<String, Object> map, String key); static char getAsChar(Map<String, Object> map, String key); static double getAsDouble(Map<String, Object> map, String key); static float getAsFloat(Map<String, Object> map, String key); static int getAsInt(Map<String, Object> map, String key); static long getAsLong(Map<String, Object> map, String key); static short getAsShort(Map<String, Object> map, String key); static String getAsString(Map<String, Object> map, String key);  }
@Test public void testBigDecimalAsBigDecimal() { map.put("value", BigDecimal.valueOf(1.1)); BigDecimal actual = MapValueGetter.getAsBigDecimal(map, "value"); BigDecimal expected = BigDecimal.valueOf(1.1); assertEquals(expected, actual); }
@Test public void testBigDecimalFromString() { map.put("value", "1.1"); BigDecimal actual = MapValueGetter.getAsBigDecimal(map, "value"); BigDecimal expected = BigDecimal.valueOf(1.1); assertEquals(expected, actual); }
@Test public void testBigDecimalFromNull() { BigDecimal actual = MapValueGetter.getAsBigDecimal(map, "value"); BigDecimal expected = BigDecimal.ZERO; assertEquals(expected, actual); }
@Test(expected = WrongTypeException.class) public void testBigDecimalWrongType() { map.put("value", true); MapValueGetter.getAsBigDecimal(map, "value"); }