SafeParse { public static Long stringToLong(String input) { Long result = 0L; input = input.replace(",", "."); try { result = Long.parseLong(input); } catch (Exception e) { } return result; }  static Double stringToDouble(String input); static Integer stringToInt(String input); static Long stringToLong(String input); static Float stringToFloat(String input);  }
@Test public void stringToLongTest() { assertThat(1052934L, is(SafeParse.stringToLong("1052934"))); assertThat(-42L, is(SafeParse.stringToLong("-42"))); assertThat(2147483647L, is(SafeParse.stringToLong("2147483647"))); assertThat(-2147483648L, is(SafeParse.stringToLong("-2147483648"))); assertThat(1000500100900L, is(SafeParse.stringToLong("1000500100900"))); assertThat(2147483648L, is(SafeParse.stringToLong("2147483648"))); assertThat(-2147483649L, is(SafeParse.stringToLong("-2147483649"))); assertThat(0L, is(SafeParse.stringToLong("12 U/h"))); assertThat(0L, is(SafeParse.stringToLong("0.1234"))); assertThat(0L, is(SafeParse.stringToLong("0,1234"))); assertThat(0L, is(SafeParse.stringToLong(".5436564812"))); assertThat(0L, is(SafeParse.stringToLong(",5436564812"))); assertThat(0L, is(SafeParse.stringToLong("42.1234"))); assertThat(0L, is(SafeParse.stringToLong("42,1234"))); assertThat(0L, is(SafeParse.stringToLong("3212.5436564812"))); assertThat(0L, is(SafeParse.stringToLong("3212,5436564812"))); assertThat(0L, is(SafeParse.stringToLong("ala ma kota"))); assertThat(0L, is(SafeParse.stringToLong(""))); assertThat(0L, is(SafeParse.stringToLong(" "))); assertThat(0L, is(SafeParse.stringToLong("\n\r"))); }
@Test(expected=NullPointerException.class) public void stringToLongNullTest() { SafeParse.stringToLong(null); }