|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.testing.orm.junit; |
| 6 | + |
| 7 | +import org.hibernate.Incubating; |
| 8 | +import org.junit.jupiter.api.extension.AfterClassTemplateInvocationCallback; |
| 9 | +import org.junit.jupiter.api.extension.BeforeClassTemplateInvocationCallback; |
| 10 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 11 | + |
| 12 | +import static org.junit.platform.commons.support.AnnotationSupport.findAnnotatedMethods; |
| 13 | +import static org.junit.platform.commons.support.HierarchyTraversalMode.BOTTOM_UP; |
| 14 | + |
| 15 | +/** |
| 16 | + * JUnit extension to provide hooks for methods annotated with {@link BeforeClassTemplate} |
| 17 | + * and {@link AfterClassTemplate} to be called before and after each class template invocation |
| 18 | + * <p> |
| 19 | + * Provides native support for methods with zero parameters or a single parameter typed either |
| 20 | + * as {@link EntityManagerFactoryScope} or {@link SessionFactoryScope}, for easy integration |
| 21 | + * with the {@link EntityManagerFactoryExtension entity manager} and {@link SessionFactoryExtension |
| 22 | + * session factory} extensions. |
| 23 | + */ |
| 24 | +@Incubating |
| 25 | +public class ClassTemplateInvocationListenersExtension |
| 26 | + implements BeforeClassTemplateInvocationCallback, AfterClassTemplateInvocationCallback { |
| 27 | + @Override |
| 28 | + public void beforeClassTemplateInvocation(ExtensionContext context) throws Exception { |
| 29 | + final var testClass = context.getRequiredTestClass(); |
| 30 | + final var annotatedMethods = findAnnotatedMethods( testClass, BeforeClassTemplate.class, BOTTOM_UP ); |
| 31 | + for ( final var method : annotatedMethods ) { |
| 32 | + context.getExecutableInvoker().invoke( method, context.getRequiredTestInstance() ); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void afterClassTemplateInvocation(ExtensionContext context) throws Exception { |
| 38 | + final var testClass = context.getRequiredTestClass(); |
| 39 | + final var annotatedMethods = findAnnotatedMethods( testClass, AfterClassTemplate.class, BOTTOM_UP ); |
| 40 | + for ( final var method : annotatedMethods ) { |
| 41 | + context.getExecutableInvoker().invoke( method, context.getRequiredTestInstance() ); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments