Skip to content

Commit 2a6b3d3

Browse files
committed
HHH-19884 Custom @ClassTemplate lifecycle annotations and extension
1 parent 23d12a6 commit 2a6b3d3

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
/**
15+
* Similar to JUnit's {@link org.junit.jupiter.api.AfterAll} but called after
16+
* each template invocation in a {@link @ClassTemplate} test class.
17+
*/
18+
@Target(ElementType.METHOD)
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Incubating
21+
public @interface AfterClassTemplate {
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
9+
import java.lang.annotation.ElementType;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
/**
15+
* Similar to JUnit's {@link org.junit.jupiter.api.BeforeAll} but called before
16+
* each template invocation in a {@link @ClassTemplate} test class.
17+
*/
18+
@Target(ElementType.METHOD)
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Incubating
21+
public @interface BeforeClassTemplate {
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)