Hide NPE from ArrayUtils as IAE

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137546 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-31 23:55:57 +00:00
parent bcfc9c2602
commit 87b920c44b
2 changed files with 23 additions and 4 deletions

View File

@ -66,7 +66,7 @@ import org.apache.commons.lang.math.NumberUtils;
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @since 2.0 * @since 2.0
* @version $Id: BooleanUtils.java,v 1.10 2003/07/31 22:30:07 scolebourne Exp $ * @version $Id: BooleanUtils.java,v 1.11 2003/07/31 23:55:57 scolebourne Exp $
*/ */
public class BooleanUtils { public class BooleanUtils {
@ -654,12 +654,23 @@ public class BooleanUtils {
* *
* @param array an array of <code>Boolean<code>s * @param array an array of <code>Boolean<code>s
* @return <code>true</code> if the xor is successful. * @return <code>true</code> if the xor is successful.
* @throws NullPointerException if <code>array</code> contains a <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is <code>null</code> * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty. * @throws IllegalArgumentException if <code>array</code> is empty.
* @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
*/ */
public static Boolean xor(Boolean[] array) { public static Boolean xor(Boolean[] array) {
return (xor(ArrayUtils.toPrimitive(array)) ? Boolean.TRUE : Boolean.FALSE); if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
boolean[] primitive = null;
try {
primitive = ArrayUtils.toPrimitive(array);
} catch (NullPointerException ex) {
throw new IllegalArgumentException("The array must not conatin any null elements");
}
return (xor(primitive) ? Boolean.TRUE : Boolean.FALSE);
} }
} }

View File

@ -66,7 +66,7 @@ import junit.textui.TestRunner;
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @version $Id: BooleanUtilsTest.java,v 1.5 2003/07/30 22:21:39 scolebourne Exp $ * @version $Id: BooleanUtilsTest.java,v 1.6 2003/07/31 23:55:57 scolebourne Exp $
*/ */
public class BooleanUtilsTest extends TestCase { public class BooleanUtilsTest extends TestCase {
@ -467,6 +467,13 @@ public class BooleanUtilsTest extends TestCase {
} catch (IllegalArgumentException ex) {} } catch (IllegalArgumentException ex) {}
} }
public void testXor_object_nullElementInput() {
try {
BooleanUtils.xor(new Boolean[] {null});
fail("Exception was not thrown for null element input.");
} catch (IllegalArgumentException ex) {}
}
public void testXor_object_validInput_2items() { public void testXor_object_validInput_2items() {
assertTrue( assertTrue(
"True result for (true, true)", "True result for (true, true)",
@ -565,6 +572,7 @@ public class BooleanUtilsTest extends TestCase {
Boolean.TRUE, Boolean.TRUE,
Boolean.TRUE }) Boolean.TRUE })
.booleanValue()); .booleanValue());
} }
} }