PrimitiveBooleanConverter implements Converter<Boolean> { public Boolean convert(String value, Class<? extends Boolean> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return false; } return booleanConverter.convert(value, type, bundle); }  Boolean convert(String value, Class<? extends Boolean> type, ResourceBundle bundle);  }
@Test public void shouldBeAbleToConvertNumbers() { assertThat((Boolean) converter.convert("", boolean.class, bundle), is(equalTo(false))); assertThat((Boolean) converter.convert("false", boolean.class, bundle), is(equalTo(false))); assertThat((Boolean) converter.convert("true", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("True", boolean.class, bundle), is(equalTo(true))); }
@Test public void shouldConvertToZeroWhenNull() { assertThat((Boolean) converter.convert(null, boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertToZeroWhenEmpty() { assertThat((Boolean) converter.convert("", boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertYesNo() { assertThat((Boolean) converter.convert("yes", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("no", boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertYN() { assertThat((Boolean) converter.convert("y", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("n", boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertOnOff() { assertThat((Boolean) converter.convert("on", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("off", boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertIgnoringCase() { assertThat((Boolean) converter.convert("truE", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("FALSE", boolean.class, bundle), is(equalTo(false))); assertThat((Boolean) converter.convert("On", boolean.class, bundle), is(equalTo(true))); assertThat((Boolean) converter.convert("oFf", boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldThrowExceptionForInvalidString() { try { converter.convert("not a boolean!", boolean.class, bundle); Assert.assertTrue(false); } catch (ConversionError e) { assertThat(e.getMessage(), is(equalTo("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off"))); } }