Numbers { public static Double parseDouble(final Object value) throws ClassCastException { if (value instanceof Number) { return ((Number) value).doubleValue(); } else if (value instanceof String) { try { return Double.valueOf((String) value); } catch (NumberFormatException e) { throw new ClassCastException("Unable to parse " + value + " as double."); } } else { throw new ClassCastException("Unable to parse " + value + " as double."); } } private  Numbers(); static Double parseDouble(final Object value);  }
@Test public void parseDouble_withNumberValue_shouldReturnDouble() throws Exception { int anInt = 2; assertThat(Numbers.parseDouble(anInt)).isEqualTo(anInt); double aDouble = 2.1; assertThat(Numbers.parseDouble(aDouble)).isEqualTo(aDouble); float aFloat = 2.2f; assertThat(Numbers.parseDouble(aFloat)).isEqualTo(aFloat); double nan = Double.NaN; assertThat(Numbers.parseDouble(nan)).isEqualTo(nan); }
@Test public void parseDouble_withStringValue_shouldReturnDouble() throws Exception { assertThat(Numbers.parseDouble("0.01")).isEqualTo(0.01); assertThat(Numbers.parseDouble("-1015")).isEqualTo(-1015); }
@Test public void parseDouble_withInvalidStringValue_shouldThrowClassCastException() throws Exception { try { Numbers.parseDouble("dog"); fail("Expected ClassCastException"); } catch (ClassCastException e) { } try { Numbers.parseDouble("123a"); fail("Expected ClassCastException"); } catch (ClassCastException e) { } }
@Test public void parseDouble_withInvalidObjectType_shouldThrowClassCastException() throws Exception { try { Numbers.parseDouble(mock(Activity.class)); fail("Expected ClassCastException"); } catch (ClassCastException e) { } }