From 680f9d5492e2c9c27e5c1178744a9edf36a0272b Mon Sep 17 00:00:00 2001 From: Diego Marcilio Date: Sat, 11 Feb 2023 16:38:50 +0100 Subject: [PATCH] Add missing javadoc/tests for some null arguments (#869) --- .../org/apache/commons/lang3/ArrayUtils.java | 1 + .../java/org/apache/commons/lang3/Range.java | 2 ++ .../lang3/builder/ToStringBuilder.java | 1 + .../commons/lang3/reflect/TypeUtils.java | 5 ++++ .../lang3/builder/ToStringBuilderTest.java | 7 +++++ .../commons/lang3/reflect/TypeUtilsTest.java | 27 +++++++++++++++++++ .../commons/lang3/time/DurationUtilsTest.java | 5 ++++ 7 files changed, 48 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 25d576546..95bf83964 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -3772,6 +3772,7 @@ public class ArrayUtils { * @param comparator the {@link Comparator} to compare over * @param the datatype of the array * @return whether the array is sorted + * @throws NullPointerException if {@code comparator} is {@code null} * @since 3.4 */ public static boolean isSorted(final T[] array, final Comparator comparator) { diff --git a/src/main/java/org/apache/commons/lang3/Range.java b/src/main/java/org/apache/commons/lang3/Range.java index b93a557d3..66cb5ddf4 100644 --- a/src/main/java/org/apache/commons/lang3/Range.java +++ b/src/main/java/org/apache/commons/lang3/Range.java @@ -274,6 +274,7 @@ public class Range implements Serializable { * * @param element the element to check for, not null * @return -1, 0 or +1 depending on the element's location relative to the range + * @throws NullPointerException if {@code element} is {@code null} */ public int elementCompareTo(final T element) { // Comparable API says throw NPE on null @@ -329,6 +330,7 @@ public class Range implements Serializable { * * @param element the element to check for, not null * @return the minimum, the element, or the maximum depending on the element's location relative to the range + * @throws NullPointerException if {@code element} is {@code null} * @since 3.10 */ public T fit(final T element) { diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java index 020ca4475..231d3fc7a 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringBuilder.java @@ -907,6 +907,7 @@ public class ToStringBuilder implements Builder { * * @param srcObject the {@link Object} whose class name and id to output * @return this + * @throws NullPointerException if {@code srcObject} is {@code null} * @since 2.0 */ public ToStringBuilder appendAsObjectToString(final Object srcObject) { diff --git a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java index c985643c2..83beab013 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java @@ -1543,6 +1543,7 @@ public class TypeUtils { * @param rawClass the raw class to create a parameterized type instance for * @param typeVariableMap the map used for parameterization * @return {@link ParameterizedType} + * @throws NullPointerException if either {@code rawClass} or {@code typeVariableMap} is {@code null} * @since 3.2 */ public static final ParameterizedType parameterize(final Class rawClass, @@ -1559,6 +1560,7 @@ public class TypeUtils { * @param rawClass the raw class to create a parameterized type instance for * @param typeArguments the types used for parameterization * @return {@link ParameterizedType} + * @throws NullPointerException if {@code rawClass} is {@code null} * @since 3.2 */ public static final ParameterizedType parameterize(final Class rawClass, final Type... typeArguments) { @@ -1607,6 +1609,8 @@ public class TypeUtils { * @param rawClass the raw class to create a parameterized type instance for * @param typeVariableMap the map used for parameterization * @return {@link ParameterizedType} + * @throws NullPointerException if either {@code rawClass} or {@code typeVariableMap} + * is {@code null} * @since 3.2 */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class rawClass, @@ -1625,6 +1629,7 @@ public class TypeUtils { * @param typeArguments the types used for parameterization * * @return {@link ParameterizedType} + * @throws NullPointerException if {@code rawClass} is {@code null} * @since 3.2 */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class rawClass, diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java index 46a2a3d70..64a244764 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringBuilderTest.java @@ -609,6 +609,13 @@ public class ToStringBuilderTest extends AbstractLangTest { new ToStringBuilder(base).appendAsObjectToString(objectToAppend3).toString()); } + @Test + public void testAppendAsObjectToStringNullPointerException() { + ToStringBuilder builder = new ToStringBuilder(1); + assertThrows(NullPointerException.class, () -> builder.appendAsObjectToString(null)); + builder.toString(); + } + @Test public void testAppendBooleanArrayWithFieldName() { final boolean[] array = { true, false, false }; diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java index 7dd4affed..49f2c0d7d 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.awt.Insets; @@ -947,6 +948,18 @@ public class TypeUtilsTest extends AbstractLangTest { assertEquals(TypeUtils.parameterize(ArrayList.class, String.class), unrolled); } + @Test + public void testParameterizeNullPointerException() { + assertThrows(NullPointerException.class, () -> TypeUtils.parameterize(null, Collections.emptyMap())); + final Map, Type> nullTypeVariableMap = null; + assertThrows(NullPointerException.class, () -> TypeUtils.parameterize(String.class, nullTypeVariableMap)); + } + + @Test + public void testParameterizeVarArgsNullPointerException() { + assertThrows(NullPointerException.class, () -> TypeUtils.parameterize(null)); + } + @Test public void testParameterizeWithOwner() throws Exception { final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class); @@ -954,6 +967,20 @@ public class TypeUtilsTest extends AbstractLangTest { assertTrue(TypeUtils.equals(getClass().getField("dat2").getGenericType(), dat2Type)); } + @Test + public void testParameterizeWithOwner3ArgsNullPointerException() { + final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class); + assertThrows(NullPointerException.class, () -> TypeUtils.parameterizeWithOwner(owner, null, String.class)); + final Map, Type> nullTypeVariableMap = null; + assertThrows(NullPointerException.class, () -> TypeUtils.parameterizeWithOwner(owner, That.class, nullTypeVariableMap)); + } + + @Test + public void testParameterizeWithOwnerVarArgsNullPointerException() { + final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class); + assertThrows(NullPointerException.class, () -> TypeUtils.parameterizeWithOwner(owner, null)); + } + @Test public void testToLongString() { assertEquals(getClass().getName() + ":B", TypeUtils.toLongString(getClass().getTypeParameters()[0])); diff --git a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java index d3d8f3126..af258c79a 100644 --- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java @@ -143,6 +143,11 @@ public class DurationUtilsTest extends AbstractLangTest { assertEquals(Integer.MAX_VALUE, DurationUtils.toMillisInt(Duration.ofNanos(Long.MAX_VALUE))); } + @Test + public void testToMillisIntNullDuration() { + assertThrows(NullPointerException.class, () -> DurationUtils.toMillisInt(null)); + } + @Test public void testZeroIfNull() { assertEquals(Duration.ZERO, DurationUtils.zeroIfNull(null));