JspTagUtils { public static boolean toBoolean(String attributeValue, boolean defaultValue, String attributeName) throws JspTagException { if (StringUtils.hasText(attributeValue)) { if ("true".equalsIgnoreCase(attributeValue) || "false" .equalsIgnoreCase(attributeValue)) { return Boolean.parseBoolean(attributeValue); } else { throw new JspTagException("The value of " + attributeName + " must be either true or false."); } } return defaultValue; } private  JspTagUtils(); static boolean toBoolean(String attributeValue, boolean defaultValue,
            String attributeName);  }
@Test public void toBoolean_valueIsTrue() throws JspException { assertThat(JspTagUtils.toBoolean("true", false, null), is(true)); assertThat(JspTagUtils.toBoolean("TRUE", false, null), is(true)); assertThat(JspTagUtils.toBoolean("TrUe", false, null), is(true)); }
@Test public void toBoolean_valueIsFalse() throws JspException { assertThat(JspTagUtils.toBoolean("false", true, null), is(false)); assertThat(JspTagUtils.toBoolean("FALSE", true, null), is(false)); assertThat(JspTagUtils.toBoolean("FaLsE", true, null), is(false)); }
@Test public void toBoolean_valueIsNullOrBlankOrWhitespace() throws JspException { assertThat(JspTagUtils.toBoolean(null, Boolean.TRUE, null), is(true)); assertThat(JspTagUtils.toBoolean("", Boolean.FALSE, null), is(false)); }
@Test public void toBoolean_valueIsNotTrueOrFalse() throws JspException { try { JspTagUtils.toBoolean("on", true, "field1"); fail("should be occurred JspTagException."); } catch (JspTagException e) { assertThat(e.getMessage(), is( "The value of field1 must be either true or false.")); } try { JspTagUtils.toBoolean("off", false, "field2"); fail("should be occurred JspTagException."); } catch (JspTagException e) { assertThat(e.getMessage(), is( "The value of field2 must be either true or false.")); } }