ParsingUtils { public static boolean convertToBoolean(final String str, final boolean defaultValueIsTrue) { if (null == str) { return defaultValueIsTrue; } final String safeStr = trim(str).toLowerCase(); if (defaultValueIsTrue) { return !FALSES.contains(safeStr); } else { return TRUES.contains(safeStr); } }  static boolean convertToBoolean(final String str, final boolean defaultValueIsTrue); static boolean convertToBoolean(final String val); static int convertToInt(final String val); static int convertToInt(final String val, final int defaultValue); static long convertToLong(final String val); static long convertToLong(final String val, final int defaultValue); static long convertToLong(final String val, final long defaultValue); static double convertToDouble(final String val); static double convertToDouble(final String val, final double defaultValue); static long convertToCapacity(
      final String str,
      final long defaultValue); static long convertToTime(final String val); static long parse(
      final String val,
      final TimeUnit unit); static final List<ScaleUnit> INTERVALS; }
@Test public void testConvertToBooleanOnDefaultValue() { final Object[][] testParameters = new Object[][] {{"no", false, false}, {"n", false, false}, {"false", false, false}, {"0", false, false}, {"No", false, false}, {"False", false, false}, {"no", true, false}, {"n", true, false}, {"false", true, false}, {"0", true, false}, {"No", true, false}, {"False", true, false}, {"yes", false, true}, {"y", false, true}, {"true", false, true}, {"1", false, true}, {"Yes", false, true}, {"True", false, true}, {"yes", true, true}, {"y", true, true}, {"true", true, true}, {"1", true, true}, {"Yes", true, true}, {"True", true, true}}; assertTrue(ParsingUtils.convertToBoolean(null, true)); assertFalse(ParsingUtils.convertToBoolean(null, false)); for (final Object[] testParameter : testParameters) { boolean expected = (Boolean) testParameter[2]; String str = (String) testParameter[0]; boolean defaultValue = (Boolean) testParameter[1]; assertEquals(expected, ParsingUtils.convertToBoolean(str, defaultValue)); } }
@Test public void testConvertToBoolean() { final Object[][] testParameters = new Object[][] {{"no", false}, {"n", false}, {"false", false}, {"0", false}, {"No", false}, {"False", false}, {"yes", true}, {"y", true}, {"true", true}, {"1", true}, {"Yes", true}, {"True", true}}; for (final Object[] testParameter : testParameters) { boolean expected = (Boolean) testParameter[1]; String val = (String) testParameter[0]; assertEquals(expected, ParsingUtils.convertToBoolean(val)); } }
@Test public void shouldBeFalseOnNull() { assertFalse(ParsingUtils.convertToBoolean(null)); }