BooleanConverter implements Converter<Boolean> { public Boolean convert(String value, Class<? extends Boolean> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } value = value.toUpperCase(); if (matches(IS_TRUE, value)) { return true; } else if (matches(IS_FALSE, value)) { return false; } throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_boolean"), value)); }  Boolean convert(String value, Class<? extends Boolean> type, ResourceBundle bundle);  }
@Test public void shouldBeAbleToConvertTrueAndFalse(){ assertThat(converter.convert("true", Boolean.class, bundle), is(equalTo(true))); assertThat(converter.convert("false", Boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertEmptyToNull() { assertThat(converter.convert("", Boolean.class, bundle), is(nullValue())); }
@Test public void shouldNotComplainAboutNull() { assertThat(converter.convert(null, Boolean.class, bundle), is(nullValue())); }
@Test public void shouldConvertYesNo() { assertThat(converter.convert("yes", Boolean.class, bundle), is(equalTo(true))); assertThat(converter.convert("no", Boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertYN() { assertThat(converter.convert("y", Boolean.class, bundle), is(equalTo(true))); assertThat(converter.convert("n", Boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertOnOff() { assertThat(converter.convert("on", Boolean.class, bundle), is(equalTo(true))); assertThat(converter.convert("off", Boolean.class, bundle), is(equalTo(false))); }
@Test public void shouldConvertIgnoringCase() { assertThat(converter.convert("truE", Boolean.class, bundle), is(equalTo(true))); assertThat(converter.convert("FALSE", Boolean.class, bundle), is(equalTo(false))); assertThat(converter.convert("On", Boolean.class, bundle), is(equalTo(true))); assertThat(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"))); } }