Fix Javadoc bug; Improve implementation and tests

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1133336 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2011-06-08 10:51:27 +00:00
parent 6887d0f09c
commit 734d218eb5
2 changed files with 12 additions and 15 deletions

View File

@ -698,8 +698,6 @@ public class BooleanUtils {
/** /**
* <p>Converts a String to a Boolean throwing an exception if no match found.</p> * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
* *
* <p>null is returned if there is no match.</p>
*
* <pre> * <pre>
* BooleanUtils.toBoolean("true", "true", "false") = true * BooleanUtils.toBoolean("true", "true", "false") = true
* BooleanUtils.toBoolean("false", "true", "false") = false * BooleanUtils.toBoolean("false", "true", "false") = false
@ -712,18 +710,17 @@ public class BooleanUtils {
* @throws IllegalArgumentException if the String doesn't match * @throws IllegalArgumentException if the String doesn't match
*/ */
public static boolean toBoolean(String str, String trueString, String falseString) { public static boolean toBoolean(String str, String trueString, String falseString) {
if (str == null) { if (str == trueString) {
if (trueString == null) {
return true; return true;
} } else if (str == falseString) {
if (falseString == null) {
return false; return false;
} } else if (str != null) {
} else if (str.equals(trueString)) { if (str.equals(trueString)) {
return true; return true;
} else if (str.equals(falseString)) { } else if (str.equals(falseString)) {
return false; return false;
} }
}
// no match // no match
throw new IllegalArgumentException("The String did not match either specified value"); throw new IllegalArgumentException("The String did not match either specified value");
} }

View File

@ -327,13 +327,10 @@ public class BooleanUtilsTest extends TestCase {
public void test_toBoolean_String_String_String() { public void test_toBoolean_String_String_String() {
assertEquals(true, BooleanUtils.toBoolean((String) null, null, "N")); assertEquals(true, BooleanUtils.toBoolean((String) null, null, "N"));
assertEquals(false, BooleanUtils.toBoolean((String) null, "Y", null)); assertEquals(false, BooleanUtils.toBoolean((String) null, "Y", null));
try {
BooleanUtils.toBooleanObject((String) null, "Y", "N", "U");
fail();
} catch (IllegalArgumentException ex) {}
assertEquals(true, BooleanUtils.toBoolean("Y", "Y", "N")); assertEquals(true, BooleanUtils.toBoolean("Y", "Y", "N"));
assertEquals(true, BooleanUtils.toBoolean("Y", new String("Y"), new String("N")));
assertEquals(false, BooleanUtils.toBoolean("N", "Y", "N")); assertEquals(false, BooleanUtils.toBoolean("N", "Y", "N"));
assertEquals(false, BooleanUtils.toBoolean("N", new String("Y"), new String("N")));
try { try {
BooleanUtils.toBoolean(null, "Y", "N"); BooleanUtils.toBoolean(null, "Y", "N");
fail(); fail();
@ -342,6 +339,9 @@ public class BooleanUtilsTest extends TestCase {
BooleanUtils.toBoolean("X", "Y", "N"); BooleanUtils.toBoolean("X", "Y", "N");
fail(); fail();
} catch (IllegalArgumentException ex) {} } catch (IllegalArgumentException ex) {}
assertEquals(true, BooleanUtils.toBoolean((String) null, null, null));
assertEquals(true, BooleanUtils.toBoolean("Y", "Y", "Y"));
assertEquals(true, BooleanUtils.toBoolean("Y", new String("Y"), new String("Y")));
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------