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>null is returned if there is no match.</p>
*
* <pre>
* BooleanUtils.toBoolean("true", "true", "false") = true
* BooleanUtils.toBoolean("false", "true", "false") = false
@ -712,17 +710,16 @@ public class BooleanUtils {
* @throws IllegalArgumentException if the String doesn't match
*/
public static boolean toBoolean(String str, String trueString, String falseString) {
if (str == null) {
if (trueString == null) {
if (str == trueString) {
return true;
} else if (str == falseString) {
return false;
} else if (str != null) {
if (str.equals(trueString)) {
return true;
}
if (falseString == null) {
} else if (str.equals(falseString)) {
return false;
}
} else if (str.equals(trueString)) {
return true;
} else if (str.equals(falseString)) {
return false;
}
// no match
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() {
assertEquals(true, BooleanUtils.toBoolean((String) null, null, "N"));
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", new String("Y"), new String("N")));
assertEquals(false, BooleanUtils.toBoolean("N", "Y", "N"));
assertEquals(false, BooleanUtils.toBoolean("N", new String("Y"), new String("N")));
try {
BooleanUtils.toBoolean(null, "Y", "N");
fail();
@ -342,6 +339,9 @@ public class BooleanUtilsTest extends TestCase {
BooleanUtils.toBoolean("X", "Y", "N");
fail();
} 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")));
}
//-----------------------------------------------------------------------