From 7a700edaa7ccee1786f64ba71688a69119c87d4f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 1 Oct 2009 11:18:48 +0000 Subject: [PATCH] understand "on"/"off", "yes"/"no", "1"/"0" as boolean values (analogous to CustomBooleanEditor) --- .../support/StringToBooleanConverter.java | 34 ++++++++++++++++--- .../DefaultConversionServiceTests.java | 11 ++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java index 77610d45ed..ad28c7926c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/StringToBooleanConverter.java @@ -16,27 +16,53 @@ package org.springframework.core.convert.support; +import java.util.HashSet; +import java.util.Set; + import org.springframework.core.convert.converter.Converter; +import org.springframework.util.StringUtils; /** * Converts String to a Boolean. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 */ final class StringToBooleanConverter implements Converter { + private static final Set trueValues = new HashSet(4); + + private static final Set falseValues = new HashSet(4); + + static { + trueValues.add("true"); + falseValues.add("false"); + + trueValues.add("on"); + falseValues.add("off"); + + trueValues.add("yes"); + falseValues.add("no"); + + trueValues.add("1"); + falseValues.add("0"); + } + + public Boolean convert(String source) { - if (source.equals("")) { + String value = (source != null ? source.trim() : null); + if (!StringUtils.hasLength(value)) { return null; - } else if (source.equals("true")) { + } + else if (trueValues.contains(value)) { return Boolean.TRUE; } - else if (source.equals("false")) { + else if (falseValues.contains(value)) { return Boolean.FALSE; } else { - throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected \"\", 'true', or 'false'"); + throw new IllegalArgumentException("Invalid boolean value '" + source + "'"); } } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java index e1c2f958c6..dfb075ef1e 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java @@ -16,14 +16,13 @@ package org.springframework.core.convert.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.math.BigDecimal; import java.math.BigInteger; import java.util.Locale; +import static org.junit.Assert.*; import org.junit.Test; + import org.springframework.core.convert.converter.Converter; /** @@ -56,12 +55,18 @@ public class DefaultConversionServiceTests { public void testStringToBooleanTrue() { StringToBooleanConverter c = new StringToBooleanConverter(); assertEquals(Boolean.valueOf(true), c.convert("true")); + assertEquals(Boolean.valueOf(true), c.convert("on")); + assertEquals(Boolean.valueOf(true), c.convert("yes")); + assertEquals(Boolean.valueOf(true), c.convert("1")); } @Test public void testStringToBooleanFalse() { StringToBooleanConverter c = new StringToBooleanConverter(); assertEquals(Boolean.valueOf(false), c.convert("false")); + assertEquals(Boolean.valueOf(false), c.convert("off")); + assertEquals(Boolean.valueOf(false), c.convert("no")); + assertEquals(Boolean.valueOf(false), c.convert("0")); } @Test