@Test public void testCustomBooleanEditorWithEmptyAsNull() { CustomBooleanEditor editor = new CustomBooleanEditor(true); editor.setAsText("true"); assertEquals(Boolean.TRUE, editor.getValue()); assertEquals("true", editor.getAsText()); editor.setAsText("false"); assertEquals(Boolean.FALSE, editor.getValue()); assertEquals("false", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); }
/** * Gets a property value as a boolean. * * @param configurationValue the boolean configuration value * @param environment the environment containing the property * * @return the boolean property value */ public Boolean getBooleanProperty(ConfigurationValue configurationValue, Environment environment) { String booleanStringValue = getProperty(configurationValue, environment); // Use custom boolean editor without allowed empty strings to convert the value of the argument to a boolean value. CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(false); try { customBooleanEditor.setAsText(booleanStringValue); } catch (IllegalArgumentException e) { logErrorAndThrowIllegalStateException(configurationValue, "boolean", booleanStringValue, e); } // Return the boolean value. return (Boolean) customBooleanEditor.getValue(); }
this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
@Override public void setAsText(@Nullable String text) throws IllegalArgumentException { String input = (text != null ? text.trim() : null); if (this.allowEmpty && !StringUtils.hasLength(input)) { // Treat empty String as null value. setValue(null); } else if (this.trueString != null && this.trueString.equalsIgnoreCase(input)) { setValue(Boolean.TRUE); } else if (this.falseString != null && this.falseString.equalsIgnoreCase(input)) { setValue(Boolean.FALSE); } else if (this.trueString == null && (VALUE_TRUE.equalsIgnoreCase(input) || VALUE_ON.equalsIgnoreCase(input) || VALUE_YES.equalsIgnoreCase(input) || VALUE_1.equals(input))) { setValue(Boolean.TRUE); } else if (this.falseString == null && (VALUE_FALSE.equalsIgnoreCase(input) || VALUE_OFF.equalsIgnoreCase(input) || VALUE_NO.equalsIgnoreCase(input) || VALUE_0.equals(input))) { setValue(Boolean.FALSE); } else { throw new IllegalArgumentException("Invalid boolean value [" + text + "]"); } }
@Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.isBlank(text)) { setValue(Boolean.FALSE); } else { super.setAsText(text); } }
/** * Gets a property value as a boolean. * * @param configurationValue the boolean configuration value * @param environment the environment containing the property * * @return the boolean property value */ public Boolean getBooleanProperty(ConfigurationValue configurationValue, Environment environment) { String booleanStringValue = getProperty(configurationValue, environment); // Use custom boolean editor without allowed empty strings to convert the value of the argument to a boolean value. CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(false); try { customBooleanEditor.setAsText(booleanStringValue); } catch (IllegalArgumentException e) { logErrorAndThrowIllegalStateException(configurationValue, "boolean", booleanStringValue, e); } // Return the boolean value. return (Boolean) customBooleanEditor.getValue(); }
this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
@Override public void setAsText(@Nullable String text) throws IllegalArgumentException { String input = (text != null ? text.trim() : null); if (this.allowEmpty && !StringUtils.hasLength(input)) { // Treat empty String as null value. setValue(null); } else if (this.trueString != null && this.trueString.equalsIgnoreCase(input)) { setValue(Boolean.TRUE); } else if (this.falseString != null && this.falseString.equalsIgnoreCase(input)) { setValue(Boolean.FALSE); } else if (this.trueString == null && (VALUE_TRUE.equalsIgnoreCase(input) || VALUE_ON.equalsIgnoreCase(input) || VALUE_YES.equalsIgnoreCase(input) || VALUE_1.equals(input))) { setValue(Boolean.TRUE); } else if (this.falseString == null && (VALUE_FALSE.equalsIgnoreCase(input) || VALUE_OFF.equalsIgnoreCase(input) || VALUE_NO.equalsIgnoreCase(input) || VALUE_0.equals(input))) { setValue(Boolean.FALSE); } else { throw new IllegalArgumentException("Invalid boolean value [" + text + "]"); } }
CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(attributeRequired); try customBooleanEditor.setAsText(booleanStringValue); return (Boolean) customBooleanEditor.getValue();
@Test public void testCustomBooleanEditor() { CustomBooleanEditor editor = new CustomBooleanEditor(false); editor.setAsText("true"); assertEquals(Boolean.TRUE, editor.getValue()); assertEquals("true", editor.getAsText()); editor.setAsText("false"); assertEquals(Boolean.FALSE, editor.getValue()); assertEquals("false", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); try { editor.setAsText(null); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } }
@Test public void testCustomBooleanEditorWithAllowEmpty() { BooleanTestBean tb = new BooleanTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true)); bw.setPropertyValue("bool2", "true"); assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "false"); assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "on"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "off"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "yes"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "no"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "1"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "0"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", ""); assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null); assertTrue("Correct bool2 value", tb.getBool2() == null); }
@Override public void setAsText(String text) throws IllegalArgumentException { String input = (text != null ? text.trim() : null); if (this.allowEmpty && !StringUtils.hasLength(input)) { // Treat empty String as null value. setValue(null); } else if (this.trueString != null && input.equalsIgnoreCase(this.trueString)) { setValue(Boolean.TRUE); } else if (this.falseString != null && input.equalsIgnoreCase(this.falseString)) { setValue(Boolean.FALSE); } else if (this.trueString == null && (input.equalsIgnoreCase(VALUE_TRUE) || input.equalsIgnoreCase(VALUE_ON) || input.equalsIgnoreCase(VALUE_YES) || input.equals(VALUE_1))) { setValue(Boolean.TRUE); } else if (this.falseString == null && (input.equalsIgnoreCase(VALUE_FALSE) || input.equalsIgnoreCase(VALUE_OFF) || input.equalsIgnoreCase(VALUE_NO) || input.equals(VALUE_0))) { setValue(Boolean.FALSE); } else { throw new IllegalArgumentException("Invalid boolean value [" + text + "]"); } }
CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(attributeRequired); try customBooleanEditor.setAsText(booleanStringValue); return (Boolean) customBooleanEditor.getValue();
@Test public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Exception { String trueString = "pechorin"; String falseString = "nash"; CustomBooleanEditor editor = new CustomBooleanEditor(trueString, falseString, false); editor.setAsText(trueString); assertTrue(((Boolean) editor.getValue()).booleanValue()); assertEquals(trueString, editor.getAsText()); editor.setAsText(falseString); assertFalse(((Boolean) editor.getValue()).booleanValue()); assertEquals(falseString, editor.getAsText()); editor.setAsText(trueString.toUpperCase()); assertTrue(((Boolean) editor.getValue()).booleanValue()); assertEquals(trueString, editor.getAsText()); editor.setAsText(falseString.toUpperCase()); assertFalse(((Boolean) editor.getValue()).booleanValue()); assertEquals(falseString, editor.getAsText()); try { editor.setAsText(null); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } }
this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false)); this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));
public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (this.trueString != null && text.equalsIgnoreCase(this.trueString)) { setValue(Boolean.TRUE); } else if (this.falseString != null && text.equalsIgnoreCase(this.falseString)) { setValue(Boolean.FALSE); } else if (this.trueString == null && (text.equalsIgnoreCase(VALUE_TRUE) || text.equalsIgnoreCase(VALUE_ON) || text.equalsIgnoreCase(VALUE_YES) || text.equals(VALUE_1))) { setValue(Boolean.TRUE); } else if (this.falseString == null && (text.equalsIgnoreCase(VALUE_FALSE) || text.equalsIgnoreCase(VALUE_OFF) || text.equalsIgnoreCase(VALUE_NO) || text.equals(VALUE_0))) { setValue(Boolean.FALSE); } else { throw new IllegalArgumentException("Invalid boolean value [" + text + "]"); } }