From 4b5e96578da9adc93359e199c40926a1e4240f28 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 23 Feb 2024 12:50:22 +0100 Subject: [PATCH] Extract runningInEclipse() into IdeUtils test fixture --- .../core/annotation/AnnotationUtilsTests.java | 9 +++-- .../annotation/MergedAnnotationsTests.java | 11 +++--- .../core/testfixture/ide/IdeUtils.java | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 spring-core/src/testFixtures/java/org/springframework/core/testfixture/ide/IdeUtils.java diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java index d984379166..4185df3c4a 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java @@ -38,6 +38,7 @@ import org.junit.jupiter.api.Test; import org.springframework.core.Ordered; import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass; +import org.springframework.core.testfixture.ide.IdeUtils; import org.springframework.core.testfixture.stereotype.Component; import org.springframework.lang.NonNullApi; @@ -159,17 +160,15 @@ class AnnotationUtilsTests { assertThat(getAnnotation(bridgeMethod, Order.class)).isNull(); assertThat(findAnnotation(bridgeMethod, Order.class)).isNotNull(); - boolean runningInEclipse = StackWalker.getInstance().walk(stream -> - stream.anyMatch(stackFrame -> stackFrame.getClassName().startsWith("org.eclipse.jdt"))); // As of JDK 8, invoking getAnnotation() on a bridge method actually finds an - // annotation on its 'bridged' method [1]; however, the Eclipse compiler will not - // support this until Eclipse 4.9 [2]. Thus, we effectively ignore the following + // annotation on its 'bridged' method [1]; however, the Eclipse compiler does + // not support this [2]. Thus, we effectively ignore the following // assertion if the test is currently executing within the Eclipse IDE. // // [1] https://bugs.openjdk.java.net/browse/JDK-6695379 // [2] https://bugs.eclipse.org/bugs/show_bug.cgi?id=495396 // - if (!runningInEclipse) { + if (!IdeUtils.runningInEclipse()) { assertThat(bridgeMethod.getAnnotation(Transactional.class)).isNotNull(); } assertThat(getAnnotation(bridgeMethod, Transactional.class)).isNotNull(); diff --git a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java index 90dbbe9478..161ee2de20 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java @@ -46,6 +46,7 @@ import org.springframework.core.annotation.MergedAnnotation.Adapt; import org.springframework.core.annotation.MergedAnnotations.Search; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass; +import org.springframework.core.testfixture.ide.IdeUtils; import org.springframework.core.testfixture.stereotype.Component; import org.springframework.core.testfixture.stereotype.Indexed; import org.springframework.lang.Nullable; @@ -891,15 +892,15 @@ class MergedAnnotationsTests { assertThat(MergedAnnotations.from(method).get(Order.class).getDistance()).isEqualTo(-1); assertThat(MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY).get( Order.class).getDistance()).isEqualTo(0); - boolean runningInEclipse = StackWalker.getInstance().walk(stream -> - stream.anyMatch(stackFrame -> stackFrame.getClassName().startsWith("org.eclipse.jdt"))); // As of JDK 8, invoking getAnnotation() on a bridge method actually finds an - // annotation on its 'bridged' method [1]; however, the Eclipse compiler - // does not support this [2]. Thus, we effectively ignore the following + // annotation on its 'bridged' method [1]; however, the Eclipse compiler does + // not support this [2]. Thus, we effectively ignore the following // assertion if the test is currently executing within the Eclipse IDE. + // // [1] https://bugs.openjdk.java.net/browse/JDK-6695379 // [2] https://bugs.eclipse.org/bugs/show_bug.cgi?id=495396 - if (!runningInEclipse) { + // + if (!IdeUtils.runningInEclipse()) { assertThat(method.getAnnotation(Transactional.class)).isNotNull(); } assertThat(MergedAnnotations.from(method).get( diff --git a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/ide/IdeUtils.java b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/ide/IdeUtils.java new file mode 100644 index 0000000000..0c5a21a3dc --- /dev/null +++ b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/ide/IdeUtils.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.testfixture.ide; + +/** + * Test utilities related to IDEs. + * + * @author Sam Brannen + * @since 6.2 + */ +public class IdeUtils { + + /** + * Determine if the current code is running in the Eclipse IDE. + */ + public static boolean runningInEclipse() { + return StackWalker.getInstance().walk(stream -> stream.anyMatch( + stackFrame -> stackFrame.getClassName().startsWith("org.eclipse.jdt"))); + } + +}