From fb784907a9143d20985fb4a14da76ba39ec846b9 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 31 Dec 2023 17:54:44 +0100 Subject: [PATCH] =?UTF-8?q?Support=20@=E2=81=A0AutoClose=20as=20a=20meta-a?= =?UTF-8?q?nnotation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See #3367 See #3592 --- .../src/main/java/org/junit/jupiter/api/AutoClose.java | 6 +++++- .../jupiter/engine/extension/AutoCloseExtension.java | 3 ++- .../junit/jupiter/engine/extension/AutoCloseTests.java | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoClose.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoClose.java index 217c0cea1c0f..a703c455ee9b 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoClose.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AutoClose.java @@ -34,6 +34,10 @@ * {@link #value} attribute. For example, {@code @AutoClose("shutdown")} instructs * JUnit to look for a {@code shutdown()} method to close the resource. * + *
{@code @AutoClose} may be used as a meta-annotation in order to create a + * custom composed annotation that inherits the semantics of + * {@code @AutoClose}. + * *
{@code @AutoClose} fields are inherited from superclasses as long as they @@ -50,7 +54,7 @@ * * @since 5.11 */ -@Target(ElementType.FIELD) +@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @API(status = EXPERIMENTAL, since = "5.11") diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/AutoCloseExtension.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/AutoCloseExtension.java index 2c16890eef4d..0a7f3dfc3333 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/AutoCloseExtension.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/AutoCloseExtension.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.extension.TestInstancePreDestroyCallback; import org.junit.platform.commons.logging.Logger; import org.junit.platform.commons.logging.LoggerFactory; +import org.junit.platform.commons.util.AnnotationUtils; import org.junit.platform.commons.util.ExceptionUtils; import org.junit.platform.commons.util.Preconditions; import org.junit.platform.commons.util.ReflectionUtils; @@ -66,7 +67,7 @@ private static void closeFields(Class> testClass, Object testInstance) { } private static void closeField(Field field, Object testInstance) throws Exception { - String methodName = field.getAnnotation(AutoClose.class).value(); + String methodName = AnnotationUtils.findAnnotation(field, AutoClose.class).get().value(); Class> fieldType = field.getType(); checkCondition(StringUtils.isNotBlank(methodName), "@AutoClose on field %s must specify a method name.", field); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/AutoCloseTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/AutoCloseTests.java index 2bd97539c130..712e58607324 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/AutoCloseTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/AutoCloseTests.java @@ -18,6 +18,8 @@ import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -264,9 +266,14 @@ static class NoCloseMethodTestCase implements TestInterface { private final String field = ""; } + @Retention(RetentionPolicy.RUNTIME) + @AutoClose("shutdown") + @interface AutoShutdown { + } + static class NoShutdownMethodTestCase implements TestInterface { - @AutoClose("shutdown") + @AutoShutdown private final String field = ""; }