Skip to content

Commit a3192bd

Browse files
committed
Fix package name comparison on Java 8 (#4077)
`Class.getPackage()` returns `null` for the default package on Java 8. This is now handled by inspecting the fully qualified class name rather than throwing a `NullPointerException`. Fixes #4076.
1 parent fcb7b01 commit a3192bd

File tree

7 files changed

+98
-2
lines changed

7 files changed

+98
-2
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.11.3.adoc

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ on GitHub.
1616
[[release-notes-5.11.3-junit-platform-bug-fixes]]
1717
==== Bug Fixes
1818

19-
* ❓
19+
* Fixed a regression in method search algorithms introduced in 5.11.0 when classes reside
20+
in the default package and using a Java 8 runtime.
2021

2122
[[release-notes-5.11.3-junit-platform-deprecations-and-breaking-changes]]
2223
==== Deprecations and Breaking Changes

junit-platform-commons/junit-platform-commons.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tasks.jar {
2929

3030
tasks.codeCoverageClassesJar {
3131
exclude("org/junit/platform/commons/util/ModuleUtils.class")
32+
exclude("org/junit/platform/commons/util/PackageNameUtils.class")
3233
}
3334

3435
eclipse {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.util;
12+
13+
import static org.junit.platform.commons.util.PackageUtils.DEFAULT_PACKAGE_NAME;
14+
15+
/**
16+
* Collection of utilities for working with package names.
17+
*
18+
* <h2>DISCLAIMER</h2>
19+
*
20+
* <p>These utilities are intended solely for usage within the JUnit framework
21+
* itself. <strong>Any usage by external parties is not supported.</strong>
22+
* Use at your own risk!
23+
*
24+
* @since 1.11.3
25+
*/
26+
class PackageNameUtils {
27+
28+
static String getPackageName(Class<?> clazz) {
29+
Package p = clazz.getPackage();
30+
if (p != null) {
31+
return p.getName();
32+
}
33+
String className = clazz.getName();
34+
int index = className.lastIndexOf('.');
35+
return index == -1 ? DEFAULT_PACKAGE_NAME : className.substring(0, index);
36+
}
37+
38+
}

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.apiguardian.api.API.Status.INTERNAL;
2020
import static org.apiguardian.api.API.Status.STABLE;
2121
import static org.junit.platform.commons.util.CollectionUtils.toUnmodifiableList;
22+
import static org.junit.platform.commons.util.PackageNameUtils.getPackageName;
2223
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.BOTTOM_UP;
2324
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.TOP_DOWN;
2425

@@ -1899,7 +1900,7 @@ private static boolean isPackagePrivate(Member member) {
18991900
}
19001901

19011902
private static boolean declaredInSamePackage(Method m1, Method m2) {
1902-
return m1.getDeclaringClass().getPackage().getName().equals(m2.getDeclaringClass().getPackage().getName());
1903+
return getPackageName(m1.getDeclaringClass()).equals(getPackageName(m2.getDeclaringClass()));
19031904
}
19041905

19051906
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.util;
12+
13+
/**
14+
* Collection of utilities for working with package names.
15+
*
16+
* <h2>DISCLAIMER</h2>
17+
*
18+
* <p>These utilities are intended solely for usage within the JUnit framework
19+
* itself. <strong>Any usage by external parties is not supported.</strong>
20+
* Use at your own risk!
21+
*
22+
* @since 1.11.3
23+
*/
24+
class PackageNameUtils {
25+
26+
static String getPackageName(Class<?> clazz) {
27+
return clazz.getPackageName();
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*
3+
* Copyright 2015-2024 the original author or authors.
4+
*
5+
* All rights reserved. This program and the accompanying materials are
6+
* made available under the terms of the Eclipse Public License v2.0 which
7+
* accompanies this distribution and is available at
8+
*
9+
* https://www.eclipse.org/legal/epl-v20.html
10+
*/
11+
import com.example.vintage.VintageTest;
12+
13+
import org.junit.Ignore;
14+
15+
/**
16+
* Reproducer for https://github.com/junit-team/junit5/issues/4076
17+
*/
18+
@Ignore
19+
public class DefaultPackageTest extends VintageTest {
20+
void packagePrivateMethod() {
21+
}
22+
}

platform-tooling-support-tests/projects/vintage/src/test/java/com/example/vintage/VintageTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.junit.Test;
1616

1717
public class VintageTest {
18+
void packagePrivateMethod() {
19+
}
20+
1821
@Test
1922
public void success() {
2023
// pass

0 commit comments

Comments
 (0)