Skip to content

Commit

Permalink
Support @⁠AutoClose as a meta-annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 31, 2023
1 parent a00eed8 commit fb78490
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>{@code @AutoClose} may be used as a meta-annotation in order to create a
* custom <em>composed annotation</em> that inherits the semantics of
* {@code @AutoClose}.
*
* <h2>Inheritance</h2>
*
* <p>{@code @AutoClose} fields are inherited from superclasses as long as they
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "";
}

Expand Down

0 comments on commit fb78490

Please sign in to comment.