From f19416ae375e46c4e8220141f55f8d3a334f4104 Mon Sep 17 00:00:00 2001 From: laurentschoelens <61973605+laurentschoelens@users.noreply.github.com> Date: Mon, 17 Apr 2023 14:58:49 +0200 Subject: [LANG-1681] - fix some javadoc on FieldUtils methods (#1047) * [LANG-1681] - fix some javadoc on FieldUtils methods * [LANG-1681] - fixing javadoc on FieldUtils methods and adding tests [LANG-1681] - fixing javadoc on DiffBuilder [LANG-1681] - fixing javadoc on HashCodeBuilder [LANG-1681] - adding `@throws` javadoc on ClassPathUtils [LANG-1681] - fixing javadoc on Fraction [LANG-1681] - add missing javadoc and tests on RandomStringUtils [LANG-1681] - one fix and add missing javadoc/tests on MethodUtils [LANG-1681] - add missing javadoc/tests on TypeUtils --------- Co-authored-by: Laurent SCHOELENS Co-authored-by: Diego Marcilio --- .../commons/lang3/RandomStringUtilsTest.java | 5 + .../commons/lang3/builder/DiffBuilderTest.java | 10 + .../commons/lang3/reflect/FieldUtilsTest.java | 235 +++++++++++++++++---- .../commons/lang3/reflect/MethodUtilsTest.java | 33 ++- .../commons/lang3/reflect/TypeUtilsTest.java | 14 ++ 5 files changed, 257 insertions(+), 40 deletions(-) (limited to 'src/test/java/org/apache') diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index 0d051e5c5..81ac519e0 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -180,6 +180,11 @@ public class RandomStringUtilsTest extends AbstractLangTest { () -> RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY, new Random())); assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 48, false, true)); assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.random(8, 32, 65, true, false)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAlphabetic(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomAscii(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomGraph(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomNumeric(-1)); + assertThrows(IllegalArgumentException.class, () -> RandomStringUtils.randomPrint(-1)); } /** diff --git a/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java index 705fee26f..003d2abad 100644 --- a/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/DiffBuilderTest.java @@ -317,6 +317,16 @@ public class DiffBuilderTest extends AbstractLangTest { assertThrows(NullPointerException.class, () -> new DiffBuilder<>(this, null, ToStringStyle.DEFAULT_STYLE)); } + @Test + public void testNullLhs_4args() { + assertThrows(NullPointerException.class, () -> new DiffBuilder<>(null, this, ToStringStyle.DEFAULT_STYLE, true)); + } + + @Test + public void testNullRhs_4args() { + assertThrows(NullPointerException.class, () -> new DiffBuilder<>(this, null, ToStringStyle.DEFAULT_STYLE, true)); + } + @Test public void testObject() { final TypeTestClass class1 = new TypeTestClass(); diff --git a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java index b68a994f3..d7eb8cb23 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java @@ -107,22 +107,22 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetFieldIllegalArgumentException1() { + public void testGetFieldNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.getField(null, "none")); } @Test - public void testGetFieldIllegalArgumentException2() { + public void testGetFieldIllegalArgumentException1() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, null)); } @Test - public void testGetFieldIllegalArgumentException3() { + public void testGetFieldIllegalArgumentException2() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, "")); } @Test - public void testGetFieldIllegalArgumentException4() { + public void testGetFieldIllegalArgumentException3() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, " ")); } @@ -146,22 +146,22 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetFieldForceAccessIllegalArgumentException1() { + public void testGetFieldForceAccessNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.getField(null, "none", true)); } @Test - public void testGetFieldForceAccessIllegalArgumentException2() { + public void testGetFieldForceAccessIllegalArgumentException1() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, null, true)); } @Test - public void testGetFieldForceAccessIllegalArgumentException3() { + public void testGetFieldForceAccessIllegalArgumentException2() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, "", true)); } @Test - public void testGetFieldForceAccessIllegalArgumentException4() { + public void testGetFieldForceAccessIllegalArgumentException3() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getField(PublicChild.class, " ", true)); } @@ -183,6 +183,11 @@ public class FieldUtilsTest extends AbstractLangTest { assertEquals(expected, allFields.length, Arrays.toString(allFields)); } + @Test + public void testGetAllFieldsNullPointerException() { + assertThrows(NullPointerException.class, () -> FieldUtils.getAllFields(null)); + } + private Field[] sort(final Field[] fields) { // Field does not implement Comparable, so we use a KISS solution here. return ArraySorter.sort(fields, ObjectToStringComparator.INSTANCE); @@ -209,6 +214,11 @@ public class FieldUtilsTest extends AbstractLangTest { } + @Test + public void testGetAllFieldListsNullPointerException() { + assertThrows(NullPointerException.class, () -> FieldUtils.getAllFieldsList(null)); + } + @Test public void testGetFieldsWithAnnotation() throws NoSuchFieldException { assertArrayEquals(new Field[0], FieldUtils.getFieldsWithAnnotation(Object.class, Annotated.class)); @@ -220,17 +230,17 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetFieldsWithAnnotationIllegalArgumentException1() { + public void testGetFieldsWithAnnotationNullPointerException1() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsWithAnnotation(FieldUtilsTest.class, null)); } @Test - public void testGetFieldsWithAnnotationIllegalArgumentException2() { + public void testGetFieldsWithAnnotationNullPointerException2() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsWithAnnotation(null, Annotated.class)); } @Test - public void testGetFieldsWithAnnotationIllegalArgumentException3() { + public void testGetFieldsWithAnnotationNullPointerException3() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsWithAnnotation(null, null)); } @@ -248,17 +258,17 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetFieldsListWithAnnotationIllegalArgumentException1() { + public void testGetFieldsListWithAnnotationNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsListWithAnnotation(FieldUtilsTest.class, null)); } @Test - public void testGetFieldsListWithAnnotationIllegalArgumentException2() { + public void testGetFieldsListWithAnnotationNullPointerException2() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsListWithAnnotation(null, Annotated.class)); } @Test - public void testGetFieldsListWithAnnotationIllegalArgumentException3() { + public void testGetFieldsListWithAnnotationNullPointerException3() { assertThrows(NullPointerException.class, () -> FieldUtils.getFieldsListWithAnnotation(null, null)); } @@ -282,22 +292,22 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetDeclaredFieldAccessIllegalArgumentException1() { + public void testGetDeclaredFieldAccessNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.getDeclaredField(null, "none")); } @Test - public void testGetDeclaredFieldAccessIllegalArgumentException2() { + public void testGetDeclaredFieldAccessIllegalArgumentException1() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, null)); } @Test - public void testGetDeclaredFieldAccessIllegalArgumentException3() { + public void testGetDeclaredFieldAccessIllegalArgumentException2() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, "")); } @Test - public void testGetDeclaredFieldAccessIllegalArgumentException4() { + public void testGetDeclaredFieldAccessIllegalArgumentException3() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, " ")); } @@ -321,22 +331,22 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testGetDeclaredFieldForceAccessIllegalArgumentException1() { + public void testGetDeclaredFieldForceAccessNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.getDeclaredField(null, "none", true)); } @Test - public void testGetDeclaredFieldForceAccessIllegalArgumentException2() { + public void testGetDeclaredFieldForceAccessIllegalArgumentException1() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, null, true)); } @Test - public void testGetDeclaredFieldForceAccessIllegalArgumentException3() { + public void testGetDeclaredFieldForceAccessIllegalArgumentException2() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, "", true)); } @Test - public void testGetDeclaredFieldForceAccessIllegalArgumentException4() { + public void testGetDeclaredFieldForceAccessIllegalArgumentException3() { assertThrows(IllegalArgumentException.class, () -> FieldUtils.getDeclaredField(PublicChild.class, " ", true)); } @@ -346,12 +356,12 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testReadStaticFieldIllegalArgumentException1() { + public void testReadStaticFieldNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.readStaticField(null)); } @Test - public void testReadStaticFieldIllegalArgumentException2() throws Exception { + public void testReadStaticFieldIllegalArgumentException() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); assumeTrue(nonStaticField != null); @@ -365,15 +375,15 @@ public class FieldUtilsTest extends AbstractLangTest { } @Test - public void testReadStaticFieldForceAccessIllegalArgumentException1() { + public void testReadStaticFieldForceAccessNullPointerException() { assertThrows(NullPointerException.class, () -> FieldUtils.readStaticField(null, true)); } @Test - public void testReadStaticFieldForceAccessIllegalArgumentException2() { + public void testReadStaticFieldForceAccessIllegalArgumentException() { final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true); assumeTrue(nonStaticField != null); - assertThrows(IllegalArgumentException.class, () -> FieldUtils.readStaticField(nonStaticField)); + assertThrows(IllegalArgumentException.class, () -> FieldUtils.readStaticField(nonStaticField, true)); } @Test @@ -386,7 +396,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readStaticField(null, "none"), - "null class should cause an IllegalArgumentException"); + "null class should cause an NullPointerException"); assertThrows( IllegalArgumentException.class, @@ -406,7 +416,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readStaticField(Foo.class, "does_not_exist"), - "a field that doesn't exist should cause an IllegalArgumentException"); + "a field that doesn't exist should cause an NullPointerException"); assertThrows( IllegalArgumentException.class, @@ -424,7 +434,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readStaticField(null, "none", true), - "null class should cause an IllegalArgumentException"); + "null class should cause an NullPointerException"); assertThrows( IllegalArgumentException.class, @@ -444,7 +454,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readStaticField(Foo.class, "does_not_exist", true), - "a field that doesn't exist should cause an IllegalArgumentException"); + "a field that doesn't exist should cause an NullPointerException"); assertThrows( IllegalArgumentException.class, @@ -455,6 +465,9 @@ public class FieldUtilsTest extends AbstractLangTest { @Test public void testReadDeclaredNamedStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE")); + assertThrows( + NullPointerException.class, () -> + FieldUtils.readDeclaredField(null, "VALUE")); assertThrows( NullPointerException.class, () -> FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE")); assertThrows( @@ -463,6 +476,15 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, null)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, "")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PublicChild.class, " ")); } @Test @@ -475,6 +497,15 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, null, true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, " ", true)); } @Test @@ -499,7 +530,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readField(null, publicChild), - "a null field should cause an IllegalArgumentException"); + "a null field should cause an NullPointerException"); } @Test @@ -528,7 +559,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readField(null, publicChild, true), - "a null field should cause an IllegalArgumentException"); + "a null field should cause an NullPointerException"); } @Test @@ -555,7 +586,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readField((Object) null, "none"), - "a null target should cause an IllegalArgumentException"); + "a null target should cause an NullPointerException"); assertThrows(IllegalArgumentException.class, () -> FieldUtils.readField(publicChild, "b")); @@ -602,7 +633,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readField((Object) null, "none", true), - "a null target should cause an IllegalArgumentException"); + "a null target should cause an NullPointerException"); } @Test @@ -625,7 +656,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readDeclaredField(null, "none"), - "a null target should cause an IllegalArgumentException"); + "a null target should cause an NullPointerException"); assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s")); assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s")); @@ -661,7 +692,7 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.readDeclaredField(null, "none", true), - "a null target should cause an IllegalArgumentException"); + "a null target should cause an NullPointerException"); assertThrows(IllegalArgumentException.class, () -> FieldUtils.readDeclaredField(publicChild, "s", true)); assertEquals("ss", FieldUtils.readDeclaredField(publiclyShadowedChild, "s", true)); @@ -703,6 +734,10 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "new")); } @Test @@ -731,6 +766,10 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE"), "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "new", true)); } @Test @@ -758,6 +797,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new")); } @Test @@ -782,6 +834,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalAccessException.class, () -> FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeStaticField(null, "IMMUTABLE_PRIVATE", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, null, "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, "", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeStaticField(StaticContainerChild.class, " ", "new", true)); } @Test @@ -809,6 +874,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( NullPointerException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new")); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new")); } @Test @@ -833,6 +911,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalAccessException.class, () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredStaticField(null, "mutablePublic", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, null, "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, "", "new", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredStaticField(StaticContainer.class, " ", "new", true)); } @Test @@ -849,6 +940,10 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalAccessException.class, () -> FieldUtils.writeField(parentClass.getDeclaredField("d"), publicChild, Double.valueOf(Double.MAX_VALUE))); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField(null, publicChild, "S")); } @Test @@ -865,6 +960,10 @@ public class FieldUtilsTest extends AbstractLangTest { field = parentClass.getDeclaredField("d"); FieldUtils.writeField(field, publicChild, Double.valueOf(Double.MAX_VALUE), true); assertEquals(Double.valueOf(Double.MAX_VALUE), field.get(publicChild)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField(null, publicChild, "S", true)); } @Test @@ -896,6 +995,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalArgumentException.class, () -> FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(1.0))); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField((Object) null, "s", "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, null, "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, "", "s")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, " ", "s")); } @Test @@ -926,6 +1038,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertEquals(Integer.valueOf(0), FieldUtils.readField(privatelyShadowedChild, "i", true)); FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(privatelyShadowedChild, "d", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeField((Object) null, "s", "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, null, "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, "", "s", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeField(publicChild, " ", "s", true)); } @Test @@ -960,6 +1085,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertThrows( IllegalArgumentException.class, () -> FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(1.0))); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredField(null, "s", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "null", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "", "S")); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, " ", "S")); } @Test @@ -992,6 +1130,19 @@ public class FieldUtilsTest extends AbstractLangTest { assertEquals(Integer.valueOf(0), FieldUtils.readDeclaredField(privatelyShadowedChild, "i", true)); FieldUtils.writeDeclaredField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true)); + + assertThrows( + NullPointerException.class, + () -> FieldUtils.writeDeclaredField(null, "s", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "null", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, "", "S", true)); + assertThrows( + IllegalArgumentException.class, + () -> FieldUtils.writeDeclaredField(publicChild, " ", "S", true)); } @Test @@ -1011,6 +1162,11 @@ public class FieldUtilsTest extends AbstractLangTest { } } + @Test + public void testRemoveFinalModifierNullPointerException() { + assertThrows(NullPointerException.class, () -> FieldUtils.removeFinalModifier(null)); + } + @Test public void testRemoveFinalModifierWithAccess() throws Exception { final Field field = StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE_2"); @@ -1023,6 +1179,11 @@ public class FieldUtilsTest extends AbstractLangTest { } } + @Test + public void testRemoveFinalModifierWithAccessNullPointerException() { + assertThrows(NullPointerException.class, () -> FieldUtils.removeFinalModifier(null, true)); + } + @Test public void testRemoveFinalModifierWithoutAccess() throws Exception { final Field field = StaticContainer.class.getDeclaredField("IMMUTABLE_PRIVATE_2"); diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java index ad9265712..8a5030f88 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -447,6 +447,9 @@ public class MethodUtilsTest extends AbstractLangTest { MethodUtils.invokeMethod(testBean, "varOverloadEcho", "x", "y")); TestBean.verify(new ImmutablePair<>("Number...", new Number[]{17, 23, 42}), MethodUtils.invokeMethod(testBean, "varOverloadEcho", 17, 23, 42)); + + assertThrows(NullPointerException.class, () -> MethodUtils.invokeMethod(null, "foo", 1, 2)); + assertThrows(NullPointerException.class, () -> MethodUtils.invokeMethod(testBean, null, 1, 2)); } @Test @@ -492,6 +495,22 @@ public class MethodUtilsTest extends AbstractLangTest { NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE)); assertThrows(NoSuchMethodException.class, () -> MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE)); + + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(null, "foo", NumberUtils.BYTE_ONE)); + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(testBean, null, NumberUtils.BYTE_ONE)); + + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(null, "foo", new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); + assertThrows( + NullPointerException.class, + () -> MethodUtils.invokeExactMethod(testBean, null, new Object[]{NumberUtils.DOUBLE_ONE}, + new Class[]{Double.TYPE})); } @Test @@ -893,17 +912,17 @@ public class MethodUtilsTest extends AbstractLangTest { } @Test - public void testGetMethodsListWithAnnotationIllegalArgumentException1() { + public void testGetMethodsListWithAnnotationNullPointerException1() { assertThrows(NullPointerException.class, () -> MethodUtils.getMethodsListWithAnnotation(FieldUtilsTest.class, null)); } @Test - public void testGetMethodsListWithAnnotationIllegalArgumentException2() { + public void testGetMethodsListWithAnnotationNullPointerException2() { assertThrows(NullPointerException.class, () -> MethodUtils.getMethodsListWithAnnotation(null, Annotated.class)); } @Test - public void testGetMethodsListWithAnnotationIllegalArgumentException3() { + public void testGetMethodsListWithAnnotationNullPointerException3() { assertThrows(NullPointerException.class, () -> MethodUtils.getMethodsListWithAnnotation(null, null)); } @@ -1007,6 +1026,11 @@ public class MethodUtilsTest extends AbstractLangTest { assertEquals("privateStringStuff(double)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", 5.0d)); assertEquals("privateStringStuff(String)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", "Hi There")); assertEquals("privateStringStuff(Object)", MethodUtils.invokeMethod(testBean, true, "privateStringStuff", new Date())); + + assertThrows(NullPointerException.class, + () -> MethodUtils.invokeMethod(null, true, "privateStringStuff", "Hi There")); + assertThrows(NullPointerException.class, + () -> MethodUtils.invokeMethod(testBean, true, null, "Hi There")); } @Test @@ -1056,6 +1080,9 @@ public class MethodUtilsTest extends AbstractLangTest { assertEquals(MethodUtils.getMatchingMethod(GetMatchingMethodImpl.class, "testMethod5", RuntimeException.class), GetMatchingMethodImpl.class.getMethod("testMethod5", Exception.class)); + + assertThrows(NullPointerException.class, + () -> MethodUtils.getMatchingMethod(null, "testMethod5", RuntimeException.class)); } private static final class GetMatchingMethodClass { 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 49f2c0d7d..fb7a41206 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -282,6 +282,11 @@ public class TypeUtilsTest extends AbstractLangTest { assertTrue(typeVarAssigns.containsKey(treeSetTypeVar)); assertEquals(iterableType.getActualTypeArguments()[0], typeVarAssigns .get(treeSetTypeVar)); + + assertThrows(NullPointerException.class, + () -> TypeUtils.determineTypeArguments(TreeSet.class, null)); + assertThrows(NullPointerException.class, + () -> TypeUtils.determineTypeArguments(null, iterableType)); } @Test @@ -984,6 +989,8 @@ public class TypeUtilsTest extends AbstractLangTest { @Test public void testToLongString() { assertEquals(getClass().getName() + ":B", TypeUtils.toLongString(getClass().getTypeParameters()[0])); + + assertThrows(NullPointerException.class, () -> TypeUtils.toLongString(null)); } @Test @@ -1007,6 +1014,8 @@ public class TypeUtilsTest extends AbstractLangTest { typeVarAssigns.clear(); typeVarAssigns.put(getClass().getMethod("stub3").getTypeParameters()[0], Integer.class); assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns)); + + assertThrows(NullPointerException.class, () -> TypeUtils.typesSatisfyVariables(null)); } @Test @@ -1017,6 +1026,11 @@ public class TypeUtilsTest extends AbstractLangTest { assertArrayEquals(new Type[] { null }, TypeUtils.getImplicitLowerBounds(unbounded)); assertEquals("?", TypeUtils.toString(unbounded)); assertEquals("?", unbounded.toString()); + + assertThrows(NullPointerException.class, + () -> TypeUtils.getImplicitLowerBounds(null)); + assertThrows(NullPointerException.class, + () -> TypeUtils.getImplicitUpperBounds(null)); } @Test -- cgit v1.2.3