From 763d1764cdcbc460fbc97a8c0d0bf8133a36c92d Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 16 Jun 2022 03:24:52 -0500 Subject: [PATCH] HHH-15344 - Ability to apply testing annotations at method-level - `@DomainModel` --- .../orm/junit/BootstrapServiceRegistry.java | 4 +- .../testing/orm/junit/DomainModel.java | 6 +- .../orm/junit/DomainModelExtension.java | 141 +++++++++++------- .../junit/DomainModelFunctionalTesting.java | 5 +- .../testing/orm/junit/FailureExpected.java | 3 +- .../orm/junit/FailureExpectedGroup.java | 3 +- ...FunctionalEntityManagerFactoryTesting.java | 2 +- .../testing/orm/junit/LoggingInspections.java | 2 +- .../orm/junit/MessageKeyInspection.java | 2 +- .../testing/orm/junit/RequiresDialect.java | 2 +- .../orm/junit/RequiresDialectFeature.java | 2 +- .../junit/RequiresDialectFeatureGroup.java | 2 +- .../testing/orm/junit/RequiresDialects.java | 2 +- .../testing/orm/junit/ServiceRegistry.java | 2 +- .../ServiceRegistryFunctionalTesting.java | 2 +- .../testing/orm/junit/SessionFactory.java | 2 +- .../SessionFactoryFunctionalTesting.java | 2 +- .../testing/orm/junit/SkipForDialect.java | 2 +- .../testing/annotations/AnotherEntity.java | 43 ++++++ .../methods/DomainModelTesting.java | 83 +++++++++++ .../annotations/methods/package-info.java | 15 ++ 21 files changed, 248 insertions(+), 79 deletions(-) create mode 100644 hibernate-testing/src/test/java/org/hibernate/testing/annotations/AnotherEntity.java create mode 100644 hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/DomainModelTesting.java create mode 100644 hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/package-info.java diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BootstrapServiceRegistry.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BootstrapServiceRegistry.java index 1b04d38649..177f09a63f 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BootstrapServiceRegistry.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/BootstrapServiceRegistry.java @@ -18,8 +18,8 @@ import org.hibernate.integrator.spi.Integrator; * Used to define the bootstrap ServiceRegistry to be used for testing. */ @Inherited -@Target( ElementType.TYPE ) -@Retention( RetentionPolicy.RUNTIME ) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) @ServiceRegistryFunctionalTesting public @interface BootstrapServiceRegistry { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModel.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModel.java index 95cfa433ee..6d66c23586 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModel.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModel.java @@ -12,8 +12,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import jakarta.persistence.SharedCacheMode; - import org.hibernate.boot.model.TypeContributor; import org.hibernate.cache.spi.access.AccessType; @@ -22,6 +20,8 @@ import org.hibernate.testing.orm.domain.StandardDomainModel; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.ExtendWith; +import jakarta.persistence.SharedCacheMode; + /** * @asciidoc * @@ -81,7 +81,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * @author Steve Ebersole */ @Inherited -@Target(ElementType.TYPE) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelExtension.java index 8b20568152..804271c99c 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelExtension.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelExtension.java @@ -26,7 +26,7 @@ import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.testing.orm.domain.DomainModelDescriptor; import org.hibernate.testing.orm.domain.StandardDomainModel; -import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import org.junit.jupiter.api.extension.TestInstancePostProcessor; @@ -44,22 +44,67 @@ import jakarta.persistence.SharedCacheMode; * @author Steve Ebersole */ public class DomainModelExtension - implements TestInstancePostProcessor, AfterAllCallback, TestExecutionExceptionHandler { + implements TestInstancePostProcessor, BeforeEachCallback, TestExecutionExceptionHandler { private static final String MODEL_KEY = MetadataImplementor.class.getName(); + @Override + public void postProcessTestInstance(Object testInstance, ExtensionContext context) { + assert context.getTestClass().isPresent(); + + final Optional domainModelAnnRef = AnnotationSupport.findAnnotation( + context.getElement().get(), + DomainModel.class + ); + + if ( domainModelAnnRef.isPresent() + || DomainModelProducer.class.isAssignableFrom( context.getRequiredTestClass() ) ) { + final DomainModelScope created = createDomainModelScope( testInstance, domainModelAnnRef, context ); + locateExtensionStore( testInstance, context ).put( MODEL_KEY, created ); + } + } + + @Override + public void beforeEach(ExtensionContext context) { + assert context.getTestMethod().isPresent(); + assert context.getRequiredTestMethod() == context.getElement().get(); + assert context.getTestInstance().isPresent(); + + final Optional domainModelAnnRef = AnnotationSupport.findAnnotation( + context.getElement().get(), + DomainModel.class + ); + + if ( domainModelAnnRef.isEmpty() ) { + // assume the annotations are defined on the class-level... + // will be validated by the parameter-resolver or SFS-extension + return; + } + + final DomainModelScope created = createDomainModelScope( context.getRequiredTestInstance(), domainModelAnnRef, context ); + final ExtensionContext.Store extensionStore = locateExtensionStore( context.getRequiredTestInstance(), context ); + extensionStore.put( MODEL_KEY, created ); + } + + @Override + public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { + final ExtensionContext.Store store = locateExtensionStore( context.getRequiredTestInstance(), context ); + final DomainModelScopeImpl scope = (DomainModelScopeImpl) store.get( MODEL_KEY ); + + if ( scope != null ) { + scope.releaseModel(); + } + + throw throwable; + } + private static ExtensionContext.Store locateExtensionStore(Object testInstance, ExtensionContext context) { return JUnitHelper.locateExtensionStore( ServiceRegistryExtension.class, context, testInstance ); } - public static DomainModelScope findDomainModelScope(Object testInstance, ExtensionContext context) { - final ExtensionContext.Store store = locateExtensionStore( testInstance, context ); - final DomainModelScope existing = (DomainModelScope) store.get( MODEL_KEY ); - if ( existing != null ) { - return existing; - } - - + private static DomainModelScope createDomainModelScope( + Object testInstance, Optional domainModelAnnRef, + ExtensionContext context) { final ServiceRegistryScope serviceRegistryScope = ServiceRegistryExtension.findServiceRegistryScope( testInstance, context @@ -71,21 +116,14 @@ public class DomainModelExtension modelProducer = (DomainModelProducer) testInstance; } else { + assert domainModelAnnRef != null && domainModelAnnRef.isPresent(); + modelProducer = serviceRegistry -> { - if ( !context.getElement().isPresent() ) { + if ( context.getElement().isEmpty() ) { throw new RuntimeException( "Unable to determine how to handle given ExtensionContext : " + context.getDisplayName() ); } - final Optional domainModelAnnotationWrapper = AnnotationSupport.findAnnotation( - context.getElement().get(), - DomainModel.class - ); - - if ( !domainModelAnnotationWrapper.isPresent() ) { - throw new RuntimeException( "Could not locate @DomainModel annotation : " + context.getDisplayName() ); - } - - final DomainModel domainModelAnnotation = domainModelAnnotationWrapper.get(); + final DomainModel domainModelAnnotation = domainModelAnnRef.get(); final MetadataSources metadataSources = new MetadataSources( serviceRegistry ); final ManagedBeanRegistry managedBeanRegistry = serviceRegistry.getService( ManagedBeanRegistry.class ); @@ -108,7 +146,7 @@ public class DomainModelExtension } } - for ( Class annotatedClass : domainModelAnnotation.annotatedClasses() ) { + for ( Class annotatedClass : domainModelAnnotation.annotatedClasses() ) { metadataSources.addAnnotatedClass( annotatedClass ); } @@ -157,12 +195,34 @@ public class DomainModelExtension ( (DomainModelScopeAware) testInstance ).injectTestModelScope( scope ); } - locateExtensionStore( testInstance, context ).put( MODEL_KEY, scope ); - return scope; } - protected static final void applyCacheSettings(Metadata metadata, boolean overrideCacheStrategy, String cacheConcurrencyStrategy) { + public static DomainModelScope findDomainModelScope(Object testInstance, ExtensionContext context) { + // todo : allow for method-level + + final ExtensionContext.Store store = locateExtensionStore( testInstance, context ); + final DomainModelScope existing = (DomainModelScope) store.get( MODEL_KEY ); + if ( existing != null ) { + return existing; + } + + final Optional domainModelAnnRef = AnnotationSupport.findAnnotation( + context.getElement().get(), + DomainModel.class + ); + + if ( domainModelAnnRef.isEmpty() ) { + throw new RuntimeException( "Could not locate @DomainModel annotation : " + context.getDisplayName() ); + } + + final DomainModelScope created = createDomainModelScope( testInstance, domainModelAnnRef, context ); + locateExtensionStore( testInstance, context ).put( MODEL_KEY, created ); + + return created; + } + + protected static void applyCacheSettings(Metadata metadata, boolean overrideCacheStrategy, String cacheConcurrencyStrategy) { if ( !overrideCacheStrategy ) { return; } @@ -178,9 +238,9 @@ public class DomainModelExtension boolean hasLob = false; - final Iterator props = entityBinding.getPropertyClosureIterator(); + final Iterator props = entityBinding.getPropertyClosureIterator(); while ( props.hasNext() ) { - final Property prop = (Property) props.next(); + final Property prop = props.next(); if ( prop.getValue().isSimpleValue() ) { if ( isLob( (SimpleValue) prop.getValue() ) ) { hasLob = true; @@ -226,33 +286,6 @@ public class DomainModelExtension return false; } - @Override - public void postProcessTestInstance(Object testInstance, ExtensionContext context) { - findDomainModelScope( testInstance, context ); - } - - @Override - public void afterAll(ExtensionContext context) { - final ExtensionContext.Store store = locateExtensionStore( context.getRequiredTestInstance(), context ); - final DomainModelScopeImpl scope = (DomainModelScopeImpl) store.remove( MODEL_KEY ); - - if ( scope != null ) { - scope.close(); - } - } - - @Override - public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { - final ExtensionContext.Store store = locateExtensionStore( context.getRequiredTestInstance(), context ); - final DomainModelScopeImpl scope = (DomainModelScopeImpl) store.get( MODEL_KEY ); - - if ( scope != null ) { - scope.releaseModel(); - } - - throw throwable; - } - public static class DomainModelScopeImpl implements DomainModelScope, ExtensionContext.Store.CloseableResource { private final ServiceRegistryScope serviceRegistryScope; private final DomainModelProducer producer; diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelFunctionalTesting.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelFunctionalTesting.java index 9a847b462e..ea63c5fa67 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelFunctionalTesting.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DomainModelFunctionalTesting.java @@ -20,13 +20,10 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) -//@ExtendWith( ServiceRegistryExtension.class ) -//@ExtendWith( ServiceRegistryParameterResolver.class ) - @ServiceRegistryFunctionalTesting @ExtendWith( ExpectedExceptionExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpected.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpected.java index 7bd82a27e9..878158dfa4 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpected.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpected.java @@ -13,7 +13,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.extension.ExtendWith; /** @@ -24,7 +23,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * @author Steve Ebersole */ @Inherited -@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Repeatable( FailureExpectedGroup.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpectedGroup.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpectedGroup.java index 5f1565981b..96cf3850e9 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpectedGroup.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FailureExpectedGroup.java @@ -12,14 +12,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.extension.ExtendWith; /** * @author Steve Ebersole */ @Inherited -@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @ExtendWith( FailureExpectedExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FunctionalEntityManagerFactoryTesting.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FunctionalEntityManagerFactoryTesting.java index 8bc9f85150..1258e71304 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FunctionalEntityManagerFactoryTesting.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/FunctionalEntityManagerFactoryTesting.java @@ -32,7 +32,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * @author Chris Cranford */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) +@Target({ElementType.TYPE, ElementType.METHOD}) @Inherited @TestInstance(TestInstance.Lifecycle.PER_CLASS ) @ExtendWith(EntityManagerFactoryScopeExtension.class) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/LoggingInspections.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/LoggingInspections.java index 1309131df5..95b469cac4 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/LoggingInspections.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/LoggingInspections.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * better option. */ @Inherited -@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @ExtendWith( LoggingInspectionsExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/MessageKeyInspection.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/MessageKeyInspection.java index 843b155eeb..80220e93a1 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/MessageKeyInspection.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/MessageKeyInspection.java @@ -20,7 +20,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * For watching a multiple message-keys, see {@link LoggingInspections} */ @Inherited -@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD }) +@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @ExtendWith( MessageKeyInspectionExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialect.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialect.java index 64a2272a99..9608c8e50f 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialect.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialect.java @@ -25,7 +25,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Repeatable( RequiresDialects.class ) @ExtendWith( DialectFilterExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeature.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeature.java index dbb5dab02e..a437214175 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeature.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeature.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.TYPE, ElementType.METHOD}) @Repeatable( RequiresDialectFeatureGroup.class ) @ExtendWith( DialectFilterExtension.class ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeatureGroup.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeatureGroup.java index 425be3e677..77341e7ffa 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeatureGroup.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialectFeatureGroup.java @@ -22,7 +22,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ ElementType.TYPE, ElementType.METHOD}) @ExtendWith( DialectFilterExtension.class ) public @interface RequiresDialectFeatureGroup { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialects.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialects.java index 263c505bfd..aee5be80a0 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialects.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/RequiresDialects.java @@ -19,7 +19,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE }) +@Target({ ElementType.TYPE, ElementType.METHOD}) @ExtendWith( DialectFilterExtension.class ) public @interface RequiresDialects { diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistry.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistry.java index dc01a9b235..8d62639c2c 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistry.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistry.java @@ -62,7 +62,7 @@ import org.hibernate.service.spi.ServiceContributor; * @author Steve Ebersole */ @Inherited -@Target( ElementType.TYPE ) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention( RetentionPolicy.RUNTIME ) @ServiceRegistryFunctionalTesting diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryFunctionalTesting.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryFunctionalTesting.java index 65ebd558ce..2483805941 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryFunctionalTesting.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryFunctionalTesting.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactory.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactory.java index 70593594e1..95b3f9d552 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactory.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactory.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.extension.ExtendWith; * @author Steve Ebersole */ @Inherited -@Target( ElementType.TYPE ) +@Target({ElementType.TYPE, ElementType.METHOD}) @Retention( RetentionPolicy.RUNTIME ) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryFunctionalTesting.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryFunctionalTesting.java index 138ad4af1d..44ba4604ac 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryFunctionalTesting.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryFunctionalTesting.java @@ -33,7 +33,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @TestInstance( TestInstance.Lifecycle.PER_CLASS ) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SkipForDialect.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SkipForDialect.java index 736de3150b..3082ae5e43 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SkipForDialect.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SkipForDialect.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.extension.ExtendWith; */ @Inherited @Retention( RetentionPolicy.RUNTIME ) -@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +@Target({ElementType.TYPE, ElementType.METHOD}) @Repeatable( SkipForDialectGroup.class ) @ExtendWith( DialectFilterExtension.class ) diff --git a/hibernate-testing/src/test/java/org/hibernate/testing/annotations/AnotherEntity.java b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/AnotherEntity.java new file mode 100644 index 0000000000..114eda7623 --- /dev/null +++ b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/AnotherEntity.java @@ -0,0 +1,43 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ +package org.hibernate.testing.annotations; + +import jakarta.persistence.Basic; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +/** + * @author Steve Ebersole + */ +@Entity +public class AnotherEntity { + @Id + private Integer id; + @Basic + private String name; + + private AnotherEntity() { + // for Hibernate use + } + + public AnotherEntity(Integer id, String name) { + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/DomainModelTesting.java b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/DomainModelTesting.java new file mode 100644 index 0000000000..79b084863a --- /dev/null +++ b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/DomainModelTesting.java @@ -0,0 +1,83 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ +package org.hibernate.testing.annotations.methods; + +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.engine.config.spi.ConfigurationService; + +import org.hibernate.testing.annotations.AnEntity; +import org.hibernate.testing.annotations.AnotherEntity; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.DomainModelScope; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Steve Ebersole + */ +@ServiceRegistry( settings = @Setting( name = "simple", value = "simple-value")) +@DomainModel( annotatedClasses = AnEntity.class ) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class DomainModelTesting { + private int executingTestOrder; + + @Test + @Order( 2 ) + public void testClassLevelAnnotations(DomainModelScope scope) { + executingTestOrder = 2; + classLevelAssertions( scope ); + } + + private void classLevelAssertions(DomainModelScope scope) { + assertThat( scope.getDomainModel().getEntityBinding( AnEntity.class.getName() ) ).isNotNull(); + assertThat( scope.getDomainModel().getEntityBinding( AnotherEntity.class.getName() ) ).isNull(); + + settingAssertions( scope ); + } + + private void settingAssertions(DomainModelScope scope) { + final org.hibernate.service.ServiceRegistry serviceRegistry = ( (MetadataImplementor) scope.getDomainModel() ) + .getTypeConfiguration() + .getServiceRegistry(); + final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class ); + assertThat( configurationService.getSettings().get( "simple" ) ).isEqualTo( "simple-value" ); + } + + @Test + @DomainModel( annotatedClasses = AnotherEntity.class ) + @Order( 1 ) + public void testMethodLevelAnnotations(DomainModelScope scope) { + executingTestOrder = 1; + methodLevelAssertions( scope ); + } + + private void methodLevelAssertions(DomainModelScope scope) { + assertThat( scope.getDomainModel().getEntityBinding( AnEntity.class.getName() ) ).isNull(); + assertThat( scope.getDomainModel().getEntityBinding( AnotherEntity.class.getName() ) ).isNotNull(); + settingAssertions( scope ); + } + + @AfterEach + public void doStuffAfter(DomainModelScope scope) { + if ( executingTestOrder == 1 ) { + methodLevelAssertions( scope ); + } + else if ( executingTestOrder == 2 ) { + classLevelAssertions( scope ); + } + else { + throw new RuntimeException( "boom" ); + } + } +} diff --git a/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/package-info.java b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/package-info.java new file mode 100644 index 0000000000..87c6970245 --- /dev/null +++ b/hibernate-testing/src/test/java/org/hibernate/testing/annotations/methods/package-info.java @@ -0,0 +1,15 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. + */ + +/** + * Testing the ability to apply testing annotations at method-level + * + * See https://hibernate.atlassian.net/browse/HHH-15344 + * + * @author Steve Ebersole + */ +package org.hibernate.testing.annotations.methods; \ No newline at end of file