OptionsHelper { public static boolean objectToBoolean(Object optValue) throws OptionsException { throwOptionsExceptionIfConditionFails(nonNull(optValue), ".option value can not be NULL"); if (optValue instanceof String) { String value = String.valueOf(optValue); if ("true".equalsIgnoreCase(value)) { return true; } else if ("false".equalsIgnoreCase(value)) { return false; } else { throwOptionsException("Invalid boolean type options value: %s. \nBoolean type options value is only 'true' or 'false' (case insensitive).", Objects.toString(optValue)); } } if (optValue instanceof Boolean) { return (boolean) optValue; } throw new OptionsException("Invalid boolean type options value: " + Objects.toString(optValue)); } private  OptionsHelper(); static String applyRule(String ruleName, String serverOptStr, boolean value); static String applyRule(String ruleName, String serverOptStr, int value); static String applyRule(String ruleName, String serverOptStr, long value); static String applyRule(String ruleName, String serverOptStr, String value); static boolean objectToBoolean(Object optValue); static final String OPTPFX; }
@Test public void objectToBoolean() throws Exception { assertThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(null)); assertThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(EMPTY)); assertThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(SPACE)); assertThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(1L)); assertThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean("not boolean")); Object optValue = true; boolean actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = false; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); optValue = "true"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = "TruE"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = "false"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); optValue = "fALse"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); }
@Test public void objectToBoolean() throws Exception { try { OptionsHelper.objectToBoolean(null); fail("Expected Exception!"); } catch (Throwable e) { assertEquals(OptionsException.class, e.getClass()); } try { OptionsHelper.objectToBoolean(""); fail("Expected Exception!"); } catch (Throwable e) { assertEquals(OptionsException.class, e.getClass()); } try { OptionsHelper.objectToBoolean(" "); fail("Expected Exception!"); } catch (Throwable e) { assertEquals(OptionsException.class, e.getClass()); } try { OptionsHelper.objectToBoolean(1L); fail("Expected Exception!"); } catch (Throwable e) { assertEquals(OptionsException.class, e.getClass()); } try { OptionsHelper.objectToBoolean("not boolean"); fail("Expected Exception!"); } catch (Throwable e) { assertEquals(OptionsException.class, e.getClass()); } Object optValue = true; boolean actual = OptionsHelper.objectToBoolean(optValue); assertEquals(actual, true); optValue = false; actual = OptionsHelper.objectToBoolean(optValue); assertEquals(false, actual); optValue = "true"; actual = OptionsHelper.objectToBoolean(optValue); assertEquals(true, actual); optValue = "TruE"; actual = OptionsHelper.objectToBoolean(optValue); assertEquals(true, actual); optValue = "false"; actual = OptionsHelper.objectToBoolean(optValue); assertEquals(false, actual); optValue = "fALse"; actual = OptionsHelper.objectToBoolean(optValue); assertEquals(false, actual); }
@Test public void objectToBoolean() throws Exception { expectThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(null)); expectThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(EMPTY)); expectThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(SPACE)); expectThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean(1L)); expectThrows(OptionsException.class, () -> OptionsHelper.objectToBoolean("not boolean")); Object optValue = true; boolean actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = false; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); optValue = "true"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = "TruE"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(true)); optValue = "false"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); optValue = "fALse"; actual = OptionsHelper.objectToBoolean(optValue); assertThat(actual, is(false)); }