From 43c105a246c0130d08145335cb9986236e2a8465 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 15 Jan 2024 18:13:46 +0100 Subject: [PATCH] Revert "Apply method predicate before searching type hierarchy" This commit reverts the functional changes from commit 64ed21a2c9 and disables the associated tests for the time being. See #3498 See #3500 See #3553 Closes #3600 (cherry picked from commit c2f49f6943f8dc79883ae3b029ebecb7386c712d) --- .../release-notes/release-notes-5.10.2.adoc | 33 ++++++++++++------- .../commons/util/ReflectionUtils.java | 32 +++++++++--------- .../commons/util/AnnotationUtilsTests.java | 5 +-- .../commons/util/ReflectionUtilsTests.java | 5 +-- ...sWithStaticPackagePrivateBeforeMethod.java | 2 +- ...thNonStaticPackagePrivateBeforeMethod.java | 2 +- 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.10.2.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.10.2.adoc index 183431a5a83..fca360dd4c7 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.10.2.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.10.2.adoc @@ -21,12 +21,16 @@ on GitHub. ==== Deprecations and Breaking Changes -* Field predicates are no longer applied eagerly while searching the type hierarchy. This - reverts changes made in 5.10.1 that affected `findFields(...)` and `streamFields(...)` - in `ReflectionSupport` as well as `findAnnotatedFields(...)` and - `findAnnotatedFieldValues(...)` in `AnnotationSupport`. - - See issues link:https://github.com/junit-team/junit5/issues/3638[#3638] and - link:https://github.com/junit-team/junit5/issues/3553[#3553] for details. +* Field predicates are no longer applied eagerly while searching the type hierarchy. + - This reverts changes made in 5.10.1 that affected `findFields(...)` and + `streamFields(...)` in `ReflectionSupport` as well as `findAnnotatedFields(...)` and + `findAnnotatedFieldValues(...)` in `AnnotationSupport`. + - See issue link:https://github.com/junit-team/junit5/issues/3638[#3638] for details. +* Method predicates are no longer applied eagerly while searching the type hierarchy. + - This reverts changes made in 5.10.1 that affected `findMethods(...)` and + `streamMethods(...)` in `ReflectionSupport` as well as `findAnnotatedMethods(...)` in + `AnnotationSupport`. + - See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details. [[release-notes-5.10.2-junit-jupiter]] @@ -34,16 +38,23 @@ on GitHub. ==== Bug Fixes -* ❓ +* JUnit Jupiter once again properly detects when a `@Test` method is overridden in a + subclass. + - See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details. ==== Deprecations and Breaking Changes * A package-private static field annotated with `@TempDir` is once again _shadowed_ by a non-static field annotated with `@TempDir` when the non-static field resides in a - different package and has the same name as the static field. This reverts changes made - in 5.10.1. - - See issues link:https://github.com/junit-team/junit5/issues/3638[#3638] and - link:https://github.com/junit-team/junit5/issues/3553[#3553] for details. + different package and has the same name as the static field. + - This reverts changes made in 5.10.1. + - See issue link:https://github.com/junit-team/junit5/issues/3638[#3638] for details. +* A package-private class-level lifecycle method annotated with `@BeforeAll` or + `@AfterAll` is once again _shadowed_ by a method-level lifecycle method annotated with + `@BeforeEach` or `@AfterEach` when the method-level lifecycle method resides in a + different package and has the same name as the class-level lifecycle method. + - This reverts changes made in 5.10.1. + - See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details. [[release-notes-5.10.2-junit-vintage]] diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java index 40e4d67d9df..7994ceff65a 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java @@ -1490,27 +1490,29 @@ public static Stream streamMethods(Class clazz, Predicate pre Preconditions.notNull(predicate, "Predicate must not be null"); Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null"); - return findAllMethodsInHierarchy(clazz, predicate, traversalMode).stream().distinct(); + // @formatter:off + return findAllMethodsInHierarchy(clazz, traversalMode).stream() + .filter(predicate) + .distinct(); + // @formatter:on } /** * Find all non-synthetic methods in the superclass and interface hierarchy, - * excluding Object, that match the specified {@code predicate}. + * excluding Object. */ - private static List findAllMethodsInHierarchy(Class clazz, Predicate predicate, - HierarchyTraversalMode traversalMode) { - + private static List findAllMethodsInHierarchy(Class clazz, HierarchyTraversalMode traversalMode) { Preconditions.notNull(clazz, "Class must not be null"); Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null"); // @formatter:off List localMethods = getDeclaredMethods(clazz, traversalMode).stream() - .filter(predicate.and(method -> !method.isSynthetic())) + .filter(method -> !method.isSynthetic()) .collect(toList()); - List superclassMethods = getSuperclassMethods(clazz, predicate, traversalMode).stream() + List superclassMethods = getSuperclassMethods(clazz, traversalMode).stream() .filter(method -> !isMethodShadowedByLocalMethods(method, localMethods)) .collect(toList()); - List interfaceMethods = getInterfaceMethods(clazz, predicate, traversalMode).stream() + List interfaceMethods = getInterfaceMethods(clazz, traversalMode).stream() .filter(method -> !isMethodShadowedByLocalMethods(method, localMethods)) .collect(toList()); // @formatter:on @@ -1646,18 +1648,16 @@ private static int defaultMethodSorter(Method method1, Method method2) { return comparison; } - private static List getInterfaceMethods(Class clazz, Predicate predicate, - HierarchyTraversalMode traversalMode) { - + private static List getInterfaceMethods(Class clazz, HierarchyTraversalMode traversalMode) { List allInterfaceMethods = new ArrayList<>(); for (Class ifc : clazz.getInterfaces()) { // @formatter:off List localInterfaceMethods = getMethods(ifc).stream() - .filter(predicate.and(method -> !isAbstract(method))) + .filter(m -> !isAbstract(m)) .collect(toList()); - List superinterfaceMethods = getInterfaceMethods(ifc, predicate, traversalMode).stream() + List superinterfaceMethods = getInterfaceMethods(ifc, traversalMode).stream() .filter(method -> !isMethodShadowedByLocalMethods(method, localInterfaceMethods)) .collect(toList()); // @formatter:on @@ -1707,14 +1707,12 @@ private static boolean isFieldShadowedByLocalFields(Field field, List loc return localFields.stream().anyMatch(local -> local.getName().equals(field.getName())); } - private static List getSuperclassMethods(Class clazz, Predicate predicate, - HierarchyTraversalMode traversalMode) { - + private static List getSuperclassMethods(Class clazz, HierarchyTraversalMode traversalMode) { Class superclass = clazz.getSuperclass(); if (!isSearchable(superclass)) { return Collections.emptyList(); } - return findAllMethodsInHierarchy(superclass, predicate, traversalMode); + return findAllMethodsInHierarchy(superclass, traversalMode); } private static boolean isMethodShadowedByLocalMethods(Method method, List localMethods) { diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java index d3f94b7b752..ec9959c553f 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java @@ -391,10 +391,11 @@ void findAnnotatedMethodsForAnnotationUsedInClassAndSuperclassHierarchyDown() th } /** - * @see https://github.com/junit-team/junit5/issues/3498 + * @see https://github.com/junit-team/junit5/issues/3553 */ + @Disabled("Until #3553 is resolved") @Test - void findAnnotatedMethodsAppliesPredicateBeforeSearchingTypeHierarchy() throws Exception { + void findAnnotatedMethodsDoesNotAllowInstanceMethodToHideStaticMethod() throws Exception { final String BEFORE = "before"; Class superclass = SuperclassWithStaticPackagePrivateBeforeMethod.class; Method beforeAllMethod = superclass.getDeclaredMethod(BEFORE); diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java index b1fc048cec9..24f54828aca 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java @@ -1352,10 +1352,11 @@ void findMethodsIgnoresBridgeMethods() throws Exception { } /** - * @see https://github.com/junit-team/junit5/issues/3498 + * @see https://github.com/junit-team/junit5/issues/3553 */ + @Disabled("Until #3553 is resolved") @Test - void findMethodsAppliesPredicateBeforeSearchingTypeHierarchy() throws Exception { + void findMethodsDoesNotAllowInstanceMethodToHideStaticMethod() throws Exception { final String BEFORE = "before"; Class superclass = SuperclassWithStaticPackagePrivateBeforeMethod.class; Method staticMethod = superclass.getDeclaredMethod(BEFORE); diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/SuperclassWithStaticPackagePrivateBeforeMethod.java b/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/SuperclassWithStaticPackagePrivateBeforeMethod.java index 2895f2b4980..3b5a2333d41 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/SuperclassWithStaticPackagePrivateBeforeMethod.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/SuperclassWithStaticPackagePrivateBeforeMethod.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.BeforeAll; /** - * @see https://github.com/junit-team/junit5/issues/3498 + * @see https://github.com/junit-team/junit5/issues/3553 */ public class SuperclassWithStaticPackagePrivateBeforeMethod { diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/subpkg/SubclassWithNonStaticPackagePrivateBeforeMethod.java b/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/subpkg/SubclassWithNonStaticPackagePrivateBeforeMethod.java index c7c6d1e0ac2..6ce5bfb4ff7 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/subpkg/SubclassWithNonStaticPackagePrivateBeforeMethod.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/pkg1/subpkg/SubclassWithNonStaticPackagePrivateBeforeMethod.java @@ -14,7 +14,7 @@ import org.junit.platform.commons.util.pkg1.SuperclassWithStaticPackagePrivateBeforeMethod; /** - * @see https://github.com/junit-team/junit5/issues/3498 + * @see https://github.com/junit-team/junit5/issues/3553 */ public class SubclassWithNonStaticPackagePrivateBeforeMethod extends SuperclassWithStaticPackagePrivateBeforeMethod {