This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit af4b2a2ac010ae29ef3e219c5d5c7c26b7dd5acd Author: Gary Gregory <[email protected]> AuthorDate: Sat Oct 17 15:06:39 2026 -0400 Clarify ClassPathUtils unnamed-package behavior and expand tests Document unchanged resource names for unnamed packages and exception cases for class contexts. Add coverage for empty and null resource names, void, primitive arrays, and unnamed-package arrays. --- .../org/apache/commons/lang3/ClassPathUtils.java | 14 ++++- .../apache/commons/lang3/ClassPathUtilsTest.java | 72 ++++++++++++++++++---- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ClassPathUtils.java b/src/main/java/org/apache/commons/lang3/ClassPathUtils.java index 6fff8732d..4f380e3ba 100644 --- a/src/main/java/org/apache/commons/lang3/ClassPathUtils.java +++ b/src/main/java/org/apache/commons/lang3/ClassPathUtils.java @@ -61,6 +61,8 @@ public static String pathToPackage(final String path) { * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed. * </p> * + * <p>If the context class is in an unnamed package, returns {@code resourceName} unchanged.</p> + * * <pre> * ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties" * </pre> @@ -68,7 +70,8 @@ public static String pathToPackage(final String path) { * @param context The context for constructing the name. * @param resourceName The resource name to construct the fully qualified name for. * @return The fully qualified name of the resource with name {@code resourceName}. - * @throws NullPointerException Thrown if either {@code context} or {@code resourceName} is null. + * @throws NullPointerException Thrown if either {@code context} or {@code resourceName} is null, if {@code context} represents an array, a primitive + * type or {@code void}, or if it represents a named class whose package is unavailable. */ public static String toFullyQualifiedName(final Class<?> context, final String resourceName) { Objects.requireNonNull(context, "context"); @@ -85,6 +88,8 @@ public static String toFullyQualifiedName(final Class<?> context, final String r * Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed. * </p> * + * <p>If the context is an unnamed package, returns {@code resourceName} unchanged.</p> + * * <pre> * ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties" * </pre> @@ -108,6 +113,8 @@ public static String toFullyQualifiedName(final Package context, final String re * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed. * </p> * + * <p>If the context class is in an unnamed package, returns {@code resourceName} unchanged.</p> + * * <pre> * ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties" * </pre> @@ -115,7 +122,8 @@ public static String toFullyQualifiedName(final Package context, final String re * @param context The context for constructing the path. * @param resourceName The resource name to construct the fully qualified path for. * @return The fully qualified path of the resource with name {@code resourceName}. - * @throws NullPointerException Thrown if either {@code context} or {@code resourceName} is null. + * @throws NullPointerException Thrown if either {@code context} or {@code resourceName} is null, if {@code context} represents an array, a primitive + * type or {@code void}, or if it represents a named class whose package is unavailable. */ public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) { Objects.requireNonNull(context, "context"); @@ -132,6 +140,8 @@ public static String toFullyQualifiedPath(final Class<?> context, final String r * Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed. * </p> * + * <p>If the context is an unnamed package, returns {@code resourceName} unchanged.</p> + * * <pre> * ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties" * </pre> diff --git a/src/test/java/org/apache/commons/lang3/ClassPathUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassPathUtilsTest.java index 8dc62acd0..88d95f85a 100644 --- a/src/test/java/org/apache/commons/lang3/ClassPathUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ClassPathUtilsTest.java @@ -64,14 +64,18 @@ void testPathToPackage() { } @Test - void testToFullyQualifiedNameClassNull() { - assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(ClassPathUtils.class, null)); + void testToFullyQualifiedNameArrayAndPrimitiveClass() throws ClassNotFoundException { + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(String[].class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(int.class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(void.class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(int[].class, "Test.properties")); + final Class<?> unnamedPackageArray = Class.forName("[LClassPathUtilsDefaultPackage;"); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(unnamedPackageArray, "Test.properties")); } @Test - void testToFullyQualifiedNameArrayAndPrimitiveClass() { - assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(String[].class, "Test.properties")); - assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(int.class, "Test.properties")); + void testToFullyQualifiedNameClassNull() { + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(ClassPathUtils.class, null)); } @Test @@ -119,6 +123,37 @@ void testToFullyQualifiedNameUnnamedPackageClass() throws ClassNotFoundException Class.forName("ClassPathUtilsDefaultPackage"), "Test.properties")); } + @Test + void testToFullyQualifiedNameUnnamedPackageClassEmptyResource() throws ClassNotFoundException { + assertEquals("", ClassPathUtils.toFullyQualifiedName(Class.forName("ClassPathUtilsDefaultPackage"), "")); + } + + @Test + void testToFullyQualifiedNameUnnamedPackageClassNullResource() throws ClassNotFoundException { + final Class<?> context = Class.forName("ClassPathUtilsDefaultPackage"); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(context, null)); + } + + @Test + void testToFullyQualifiedNameUnnamedPackageEmptyResource() { + assertEquals("", ClassPathUtils.toFullyQualifiedName(unnamedPackage(), "")); + } + + @Test + void testToFullyQualifiedNameUnnamedPackageNullResource() { + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedName(unnamedPackage(), null)); + } + + @Test + void testToFullyQualifiedPathArrayAndPrimitiveClass() throws ClassNotFoundException { + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(String[].class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(int.class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(void.class, "Test.properties")); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(int[].class, "Test.properties")); + final Class<?> unnamedPackageArray = Class.forName("[LClassPathUtilsDefaultPackage;"); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(unnamedPackageArray, "Test.properties")); + } + @Test void testToFullyQualifiedPathClass() { final String expected = "org/apache/commons/lang3/Test.properties"; @@ -132,12 +167,6 @@ void testToFullyQualifiedPathClassNull() { assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(ClassPathUtils.class, null)); } - @Test - void testToFullyQualifiedPathArrayAndPrimitiveClass() { - assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(String[].class, "Test.properties")); - assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(int.class, "Test.properties")); - } - @Test void testToFullyQualifiedPathClassNullString() { assertNullPointerException( @@ -174,4 +203,25 @@ void testToFullyQualifiedPathUnnamedPackageClass() throws ClassNotFoundException assertEquals("Test.properties", ClassPathUtils.toFullyQualifiedPath( Class.forName("ClassPathUtilsDefaultPackage"), "Test.properties")); } + + @Test + void testToFullyQualifiedPathUnnamedPackageClassEmptyResource() throws ClassNotFoundException { + assertEquals("", ClassPathUtils.toFullyQualifiedPath(Class.forName("ClassPathUtilsDefaultPackage"), "")); + } + + @Test + void testToFullyQualifiedPathUnnamedPackageClassNullResource() throws ClassNotFoundException { + final Class<?> context = Class.forName("ClassPathUtilsDefaultPackage"); + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(context, null)); + } + + @Test + void testToFullyQualifiedPathUnnamedPackageEmptyResource() { + assertEquals("", ClassPathUtils.toFullyQualifiedPath(unnamedPackage(), "")); + } + + @Test + void testToFullyQualifiedPathUnnamedPackageNullResource() { + assertNullPointerException(() -> ClassPathUtils.toFullyQualifiedPath(unnamedPackage(), null)); + } }
