diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java index 05d2b286a8..8ddb8f7215 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java @@ -6,82 +6,74 @@ */ package org.hibernate.orm.test.annotations.collectionelement.recreate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.annotations.collectionelement.recreate.BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.MyEntity; +import static org.junit.Assert.assertTrue; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OrderColumn; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertTrue; - -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MyEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced(testEnhancedClasses = MyEntity.class) @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-14387") -public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest - extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14387") +public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyEntity.class - }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @Before - public void check() { - inSession( + @BeforeEach + public void check(SessionFactoryScope scope) { + scope.inSession( session -> assertTrue( session.getSessionFactory().getSessionFactoryOptions() - .isCollectionsInDefaultFetchGroupEnabled() ) + .isCollectionsInDefaultFetchGroupEnabled() ) ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "delete from myentity" ).executeUpdate() ); } @Test - public void testRecreateCollection() { - inTransaction( session -> { + public void testRecreateCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -89,21 +81,21 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testRecreateCollectionFind() { - inTransaction( session -> { + public void testRecreateCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -111,20 +103,20 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testDeleteCollection() { - inTransaction( session -> { + public void testDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -132,20 +124,20 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testDeleteCollectionFind() { - inTransaction( session -> { + public void testDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -153,19 +145,19 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollection() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.get( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.get( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); @@ -174,19 +166,19 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.find( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.find( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java index 4bcc9b6d31..7ecbd98b5e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java @@ -6,80 +6,73 @@ */ package org.hibernate.orm.test.annotations.collectionelement.recreate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.annotations.collectionelement.recreate.BytecodeEnhancementElementCollectionRecreateTest.MyEntity; +import static org.junit.Assert.assertFalse; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OrderColumn; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; - -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MyEntity.class, + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced(testEnhancedClasses = MyEntity.class) @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-14387") -public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14387") +public class BytecodeEnhancementElementCollectionRecreateTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyEntity.class - }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( false ); - } - - @Before - public void check() { - inSession( + @BeforeEach + public void check(SessionFactoryScope scope) { + scope.inSession( session -> assertFalse( session.getSessionFactory().getSessionFactoryOptions() - .isCollectionsInDefaultFetchGroupEnabled() ) + .isCollectionsInDefaultFetchGroupEnabled() ) ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "delete from myentity" ).executeUpdate() ); } @Test - public void testRecreateCollection() { - inTransaction( session -> { + public void testRecreateCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -87,21 +80,21 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testRecreateCollectionFind() { - inTransaction( session -> { + public void testRecreateCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -109,20 +102,20 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testDeleteCollection() { - inTransaction( session -> { + public void testDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -130,20 +123,20 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testDeleteCollectionFind() { - inTransaction( session -> { + public void testDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -151,19 +144,19 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollection() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.get( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.get( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); @@ -172,19 +165,19 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.find( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.find( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndMappedSuperclassTest.java index 08b31fbf7c..e50855bc3a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndMappedSuperclassTest.java @@ -6,23 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy; -import java.lang.reflect.Method; -import jakarta.persistence.Column; -import jakarta.persistence.Embeddable; -import jakarta.persistence.Embedded; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.MappedSuperclass; - -import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; -import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.junit.Test; -import org.junit.runner.RunWith; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.extractor.Extractors.resultOf; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.ENTITY_ENTRY_FIELD_NAME; @@ -44,10 +27,29 @@ import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_FIELD import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_GET_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_HAS_CHANGED_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_SUSPEND_NAME; +import static org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy.DirtyCheckingWithEmbeddableAndMappedSuperclassTest.CardGame; -@TestForIssue(jiraKey = "HHH-13759") -@RunWith(BytecodeEnhancerRunner.class) +import java.lang.reflect.Method; + +import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; +import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; + +import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; + + +@BytecodeEnhanced(testEnhancedClasses = CardGame.class) @EnhancementOptions(inlineDirtyChecking = true) +@JiraKey("HHH-13759") public class DirtyCheckingWithEmbeddableAndMappedSuperclassTest { @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest.java index 12d8009110..9cafb15701 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest.java @@ -34,18 +34,17 @@ import java.lang.reflect.Method; import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; import jakarta.persistence.Entity; import jakarta.persistence.Id; -@RunWith(BytecodeEnhancerRunner.class) +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest { @@ -99,7 +98,7 @@ public class DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassTest assertThat( entity ) .extracting( TRACKER_FIELD_NAME ).isInstanceOf( SimpleFieldTracker.class ); assertThat( entity.getEmbedded() ) - .extracting( TRACKER_COMPOSITE_FIELD_NAME ).isInstanceOf( CompositeOwnerTracker.class); + .extracting( TRACKER_COMPOSITE_FIELD_NAME ).isInstanceOf( CompositeOwnerTracker.class ); assertThat( entity ).extracting( resultOf( TRACKER_HAS_CHANGED_NAME ) ).isEqualTo( true ); assertThat( entity ).extracting( resultOf( TRACKER_GET_NAME ) ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassWithDifferentGenericParameterNameTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassWithDifferentGenericParameterNameTest.java index e3bcb78c56..b4f050f835 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassWithDifferentGenericParameterNameTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassWithDifferentGenericParameterNameTest.java @@ -20,11 +20,10 @@ import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_GET_N import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_HAS_CHANGED_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_SUSPEND_NAME; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; @@ -33,7 +32,7 @@ import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; @JiraKey("HHH-17035") -@RunWith(BytecodeEnhancerRunner.class) +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddableAndNonVisibleGenericMappedSuperclassWithDifferentGenericParameterNameTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java index 3e86efed34..31b0045e57 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java @@ -11,11 +11,10 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; @@ -23,7 +22,7 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; -@RunWith(BytecodeEnhancerRunner.class) +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtedingAnotherEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtedingAnotherEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java index e24aa20f26..29f2ebad9f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtedingAnotherEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtedingAnotherEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest.java @@ -6,11 +6,10 @@ */ package org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; @@ -23,7 +22,7 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX; -@RunWith(BytecodeEnhancerRunner.class) +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddableExtedingAnotherEmbeddableAndTwiceRemovedNonVisibleGenericMappedSuperclassTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest.java index 134a32a694..03dafc4b3c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest.java @@ -8,12 +8,13 @@ package org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy; import java.util.List; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -23,29 +24,25 @@ import jakarta.persistence.MappedSuperclass; import jakarta.persistence.Tuple; import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy.DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest.*; -@RunWith(BytecodeEnhancerRunner.class) + +@DomainModel(annotatedClasses = { MyEntity.class, }) +@SessionFactory +@BytecodeEnhanced(testEnhancedClasses = MyEntity.class) @EnhancementOptions(inlineDirtyChecking = true) -public class DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest extends - BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - MyEntity.class - }; - } +public class DirtyCheckingWithEmbeddableExtendingMappedSuperclassTest { @JiraKey("HHH-17041") @Test - public void testQueryEmbeddableFields() { - inTransaction( + public void testQueryEmbeddableFields(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity myEntity = new MyEntity(1, "one"); session.persist( myEntity ); } ); - inTransaction( + scope.inTransaction( session -> { List result = session.createQuery( "select m.embedded.text, m.embedded.name from MyEntity m", Tuple.class ).list(); assertThat( result.size() ).isEqualTo( 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest.java index dc62ae2f76..65d0a808cf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest.java @@ -9,44 +9,43 @@ package org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy; import java.io.Serializable; import java.util.List; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; -import jakarta.persistence.Embedded; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; import jakarta.persistence.Tuple; import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy.DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest.*; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MyEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) -public class DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest extends - BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - MyEntity.class - }; - } +public class DirtyCheckingWithEmbeddableNonVisibleGenericExtendsSerializableMappedSuperclassTest { @JiraKey("HHH-17041") @Test - public void testQueryEmbeddableFields() { - inTransaction( + public void testQueryEmbeddableFields(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity myEntity = new MyEntity(1, "one"); session.persist( myEntity ); } ); - inTransaction( + scope.inTransaction( session -> { List result = session.createQuery( "select m.embedded.text, m.embedded.name from MyEntity m", Tuple.class ).list(); assertThat( result.size() ).isEqualTo( 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedOnGetterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedOnGetterTest.java index eb85770158..f1354c4191 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedOnGetterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedOnGetterTest.java @@ -16,11 +16,10 @@ import jakarta.persistence.Id; import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; import org.assertj.core.api.Assertions; @@ -46,8 +45,8 @@ import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_GET_N import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_HAS_CHANGED_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_SUSPEND_NAME; -@TestForIssue(jiraKey = "HHH-13764") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-13764") +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddedOnGetterTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedTest.java index 85e4528886..a56f34ea7d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithEmbeddedTest.java @@ -16,13 +16,12 @@ import jakarta.persistence.Id; import org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker; import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.extractor.Extractors.resultOf; @@ -46,8 +45,8 @@ import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_GET_N import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_HAS_CHANGED_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_SUSPEND_NAME; -@TestForIssue(jiraKey = "HHH-13764") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-13764") +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithEmbeddedTest { @@ -64,7 +63,8 @@ public class DirtyCheckingWithEmbeddedTest { .hasDeclaredMethods( PERSISTENT_FIELD_READER_PREFIX + "name", PERSISTENT_FIELD_WRITER_PREFIX + "name" ) .hasDeclaredMethods( ENTITY_INSTANCE_GETTER_NAME, ENTITY_ENTRY_GETTER_NAME ) .hasDeclaredMethods( PREVIOUS_GETTER_NAME, PREVIOUS_SETTER_NAME, NEXT_GETTER_NAME, NEXT_SETTER_NAME ) - .hasDeclaredMethods( TRACKER_HAS_CHANGED_NAME, TRACKER_CLEAR_NAME, TRACKER_SUSPEND_NAME, TRACKER_GET_NAME ); + .hasDeclaredMethods( + TRACKER_HAS_CHANGED_NAME, TRACKER_CLEAR_NAME, TRACKER_SUSPEND_NAME, TRACKER_GET_NAME ); } @Test @@ -76,7 +76,8 @@ public class DirtyCheckingWithEmbeddedTest { @Test public void shouldDeclareMethodsInEmbeddedClass() { assertThat( Component.class ) - .hasDeclaredMethods( PERSISTENT_FIELD_READER_PREFIX + "component", PERSISTENT_FIELD_WRITER_PREFIX + "component" ) + .hasDeclaredMethods( + PERSISTENT_FIELD_READER_PREFIX + "component", PERSISTENT_FIELD_WRITER_PREFIX + "component" ) .hasDeclaredMethods( TRACKER_COMPOSITE_SET_OWNER, TRACKER_COMPOSITE_CLEAR_OWNER ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithMappedsuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithMappedsuperclassTest.java index 9c620404ec..f6de84cf26 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithMappedsuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhance/internal/bytebuddy/DirtyCheckingWithMappedsuperclassTest.java @@ -6,20 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhance.internal.bytebuddy; -import java.lang.reflect.Method; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.MappedSuperclass; -import org.assertj.core.extractor.Extractors; - -import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.junit.Test; -import org.junit.runner.RunWith; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.extractor.Extractors.resultOf; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.ENTITY_ENTRY_FIELD_NAME; @@ -39,8 +25,21 @@ import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_GET_N import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_HAS_CHANGED_NAME; import static org.hibernate.bytecode.enhance.spi.EnhancerConstants.TRACKER_SUSPEND_NAME; -@TestForIssue(jiraKey = "HHH-13759") -@RunWith(BytecodeEnhancerRunner.class) +import java.lang.reflect.Method; + +import org.hibernate.bytecode.enhance.internal.tracker.SimpleFieldTracker; + +import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; + +@JiraKey("HHH-13759") +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) public class DirtyCheckingWithMappedsuperclassTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/MixedAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/MixedAccessTest.java index 41024ddcde..5e9a659885 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/MixedAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/MixedAccessTest.java @@ -1,16 +1,17 @@ package org.hibernate.orm.test.bytecode.enhancement.access; import org.hibernate.bytecode.enhance.spi.UnloadedClass; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; + import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Access; import jakarta.persistence.AccessType; @@ -27,7 +28,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.util.stream.Collectors.joining; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Requires a custom enhancement context to disable dirty checking as bytecode enhancement is not expected to fully work with AccessType.PROPERTY @@ -35,23 +37,24 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; * * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10851" ) -@RunWith( BytecodeEnhancerRunner.class ) -@CustomEnhancementContext( MixedAccessTest.NoDirtyCheckingContext.class ) -public class MixedAccessTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-10851" ) +@DomainModel( + annotatedClasses = { + MixedAccessTest.TestEntity.class, MixedAccessTest.TestOtherEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced +@CustomEnhancementContext(MixedAccessTest.NoDirtyCheckingContext.class) +public class MixedAccessTest { private static final Pattern PARAM_PATTERN = Pattern.compile( "\\{\\\"(.*)\\\"\\:\\\"(.*)\\\"\\}" ); private static final Function MAPPING_FUNCTION = e -> "\"" + e.getKey() + "\":\"" + e.getValue() + "\""; private static final String ID = "foo", PARAM_KEY = "paramName", PARAM_VAL = "paramValue", PARAMS_AS_STR = "{\"" + PARAM_KEY + "\":\"" + PARAM_VAL + "\"}"; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{TestEntity.class, TestOtherEntity.class}; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity testEntity = new TestEntity( ID ); testEntity.setParamsAsString( PARAMS_AS_STR ); s.persist( testEntity ); @@ -63,13 +66,13 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity testEntity = s.get( TestEntity.class, ID ); - Assert.assertEquals( PARAMS_AS_STR, testEntity.getParamsAsString() ); + assertEquals( PARAMS_AS_STR, testEntity.getParamsAsString() ); TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID ); - Assert.assertEquals( PARAMS_AS_STR, testOtherEntity.getParamsAsString() ); + assertEquals( PARAMS_AS_STR, testOtherEntity.getParamsAsString() ); // Clean parameters testEntity.setParamsAsString( "{}" ); @@ -77,14 +80,14 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase { } ); } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity testEntity = s.get( TestEntity.class, ID ); - Assert.assertTrue( testEntity.getParams().isEmpty() ); + assertTrue( testEntity.getParams().isEmpty() ); TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID ); - Assert.assertTrue( testOtherEntity.getParams().isEmpty() ); + assertTrue( testOtherEntity.getParams().isEmpty() ); } ); } @@ -92,7 +95,7 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "TEST_ENTITY" ) - private static class TestEntity { + static class TestEntity { @Id String name; @@ -137,7 +140,7 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "OTHER_ENTITY" ) @Access( AccessType.FIELD ) - private static class TestOtherEntity { + static class TestOtherEntity { @Id String name; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/PropertyAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/PropertyAccessTest.java index fdb7aec4e9..f94f014a19 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/PropertyAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/access/PropertyAccessTest.java @@ -1,14 +1,14 @@ package org.hibernate.orm.test.bytecode.enhancement.access; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Access; import jakarta.persistence.AccessType; @@ -17,44 +17,46 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; +@DomainModel( + annotatedClasses = { + PropertyAccessTest.SomeEntity.class, + } +) +@SessionFactory @JiraKey("HHH-16799") -@RunWith(BytecodeEnhancerRunner.class) -public class PropertyAccessTest extends BaseCoreFunctionalTestCase { +@BytecodeEnhanced +public class PropertyAccessTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { SomeEntity.class }; - } @Test - public void test() { - doInHibernate( this::sessionFactory, session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.persist( new SomeEntity( 1L, "field", "property" ) ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { SomeEntity entity = session.get( SomeEntity.class, 1L ); assertThat( entity.property ).isEqualTo( "from getter: property" ); entity.setProperty( "updated" ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { SomeEntity entity = session.get( SomeEntity.class, 1L ); assertThat( entity.property ).isEqualTo( "from getter: updated" ); } ); } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, session -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.remove( session.get( SomeEntity.class, 1L ) ); } ); } @Entity @Table(name = "SOME_ENTITY") - private static class SomeEntity { + static class SomeEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/BidirectionalOneToOneWithNonAggregateIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/BidirectionalOneToOneWithNonAggregateIdTest.java index 23b6f5270f..d6076f1fe6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/BidirectionalOneToOneWithNonAggregateIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/BidirectionalOneToOneWithNonAggregateIdTest.java @@ -6,12 +6,13 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.LazyToOne; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -25,25 +26,24 @@ import static jakarta.persistence.CascadeType.ALL; import static jakarta.persistence.FetchType.LAZY; import static org.hibernate.annotations.FetchMode.SELECT; import static org.hibernate.annotations.LazyToOneOption.NO_PROXY; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + BidirectionalOneToOneWithNonAggregateIdTest.Entity1.class, + BidirectionalOneToOneWithNonAggregateIdTest.Entity2.class + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-17519") -public class BidirectionalOneToOneWithNonAggregateIdTest extends BaseCoreFunctionalTestCase { +public class BidirectionalOneToOneWithNonAggregateIdTest { static final int ENTITY_ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Entity1.class, - Entity2.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Entity1 e1 = new Entity1( ENTITY_ID ); Entity2 e2 = new Entity2(); @@ -60,8 +60,8 @@ public class BidirectionalOneToOneWithNonAggregateIdTest extends BaseCoreFunctio @Test - public void testRemovingChild() { - inTransaction( + public void testRemovingChild(SessionFactoryScope scope) { + scope.inTransaction( session -> { Entity1 e1 = session.byId( Entity1.class ).load( ENTITY_ID ); Entity2 child = e1.getChild(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/InheritedAttributeAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/InheritedAttributeAssociationTest.java index 14d0800331..1fe4b2d705 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/InheritedAttributeAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/InheritedAttributeAssociationTest.java @@ -6,11 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import java.util.List; + import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; import jakarta.persistence.DiscriminatorColumn; import jakarta.persistence.DiscriminatorType; @@ -24,56 +25,55 @@ import jakarta.persistence.InheritanceType; import jakarta.persistence.ManyToOne; import jakarta.persistence.MappedSuperclass; import jakarta.persistence.OneToMany; -import java.util.List; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-11050" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey("HHH-11050") +@BytecodeEnhanced public class InheritedAttributeAssociationTest { - @Test - public void test() { - // The mapping is wrong but the point is that the enhancement phase does not need to fail. See JIRA for further detail + @Test + public void test() { + // The mapping is wrong but the point is that the enhancement phase does not need to fail. See JIRA for further detail - // If enhancement of 'items' attribute fails, 'name' won't be enhanced - Author author = new Author(); - author.name = "Bernardo Soares"; - EnhancerTestUtils.checkDirtyTracking( author, "name" ); - } + // If enhancement of 'items' attribute fails, 'name' won't be enhanced + Author author = new Author(); + author.name = "Bernardo Soares"; + EnhancerTestUtils.checkDirtyTracking( author, "name" ); + } - // --- // + // --- // - @Entity - private static class Author { + @Entity + private static class Author { - @Id - @GeneratedValue - Long id; + @Id + @GeneratedValue + Long id; - @OneToMany( fetch = FetchType.LAZY, mappedBy = "author" ) - List items; + @OneToMany(fetch = FetchType.LAZY, mappedBy = "author") + List items; - // keep this field after 'items' - String name; - } + // keep this field after 'items' + String name; + } - @MappedSuperclass - @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) - @DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING ) - private static abstract class Item { + @MappedSuperclass + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING) + private static abstract class Item { - @Id - @GeneratedValue - Long id; + @Id + @GeneratedValue + Long id; - @ManyToOne( fetch = FetchType.LAZY ) - Author author; - } + @ManyToOne(fetch = FetchType.LAZY) + Author author; + } - @Entity - @DiscriminatorValue( "child" ) - private static class ChildItem extends Item { - } + @Entity + @DiscriminatorValue("child") + private static class ChildItem extends Item { + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationListTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationListTest.java index 9b0dae57b7..500135cb18 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationListTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationListTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -15,15 +17,14 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.ManyToMany; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class ManyToManyAssociationListTest { @Test public void testBidirectionalExisting() { @@ -36,8 +37,8 @@ public class ManyToManyAssociationListTest { user.setGroups( new ArrayList<>( Collections.singleton( group ) ) ); user.setGroups( new ArrayList<>( Arrays.asList( group, anotherGroup ) ) ); - Assert.assertEquals( 1, group.getUsers().size() ); - Assert.assertEquals( 1, anotherGroup.getUsers().size() ); + assertEquals( 1, group.getUsers().size() ); + assertEquals( 1, anotherGroup.getUsers().size() ); } // -- // diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationTest.java index 01ca51c50d..428deb7fc9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/ManyToManyAssociationTest.java @@ -6,10 +6,11 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.junit.jupiter.api.Assertions.*; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -22,7 +23,7 @@ import java.util.Set; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class ManyToManyAssociationTest { @Test @@ -37,34 +38,34 @@ public class ManyToManyAssociationTest { user.addGroup( anotherGroup ); anotherUser.addGroup( group ); - Assert.assertEquals( 2, group.getUsers().size() ); - Assert.assertEquals( 1, anotherGroup.getUsers().size() ); + assertEquals( 2, group.getUsers().size() ); + assertEquals( 1, anotherGroup.getUsers().size() ); group.resetUsers(); - Assert.assertEquals( 1, user.getGroups().size() ); - Assert.assertEquals( 0, anotherUser.getGroups().size() ); + assertEquals( 1, user.getGroups().size() ); + assertEquals( 0, anotherUser.getGroups().size() ); // Test remove user.addGroup( group ); anotherUser.addGroup( group ); - Assert.assertEquals( 2, group.getUsers().size() ); - Assert.assertEquals( 1, anotherGroup.getUsers().size() ); + assertEquals( 2, group.getUsers().size() ); + assertEquals( 1, anotherGroup.getUsers().size() ); Set groups = new HashSet<>( user.getGroups() ); groups.remove( group ); user.setGroups( groups ); - Assert.assertEquals( 1, group.getUsers().size() ); - Assert.assertEquals( 1, anotherGroup.getUsers().size() ); + assertEquals( 1, group.getUsers().size() ); + assertEquals( 1, anotherGroup.getUsers().size() ); groups.remove( anotherGroup ); user.setGroups( groups ); - Assert.assertEquals( 1, group.getUsers().size() ); + assertEquals( 1, group.getUsers().size() ); // This happens (and is expected) because there was no snapshot taken before remove - Assert.assertEquals( 1, anotherGroup.getUsers().size() ); + assertEquals( 1, anotherGroup.getUsers().size() ); } // -- // diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationTest.java index 6be9af71f8..1d558531a8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyAssociationTest.java @@ -6,10 +6,13 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -24,43 +27,43 @@ import java.util.List; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class OneToManyAssociationTest { @Test public void test() { Customer customer = new Customer(); - Assert.assertTrue( customer.getInventories().isEmpty() ); + assertTrue( customer.getInventories().isEmpty() ); CustomerInventory customerInventory = new CustomerInventory(); customerInventory.setCustomer( customer ); - Assert.assertEquals( 1, customer.getInventories().size() ); - Assert.assertTrue( customer.getInventories().contains( customerInventory ) ); + assertEquals( 1, customer.getInventories().size() ); + assertTrue( customer.getInventories().contains( customerInventory ) ); Customer anotherCustomer = new Customer(); - Assert.assertTrue( anotherCustomer.getInventories().isEmpty() ); + assertTrue( anotherCustomer.getInventories().isEmpty() ); customerInventory.setCustomer( anotherCustomer ); - Assert.assertTrue( customer.getInventories().isEmpty() ); - Assert.assertEquals( 1, anotherCustomer.getInventories().size() ); - Assert.assertSame( customerInventory, anotherCustomer.getInventories().get( 0 ) ); + assertTrue( customer.getInventories().isEmpty() ); + assertEquals( 1, anotherCustomer.getInventories().size() ); + assertSame( customerInventory, anotherCustomer.getInventories().get( 0 ) ); customer.addInventory( customerInventory ); - Assert.assertSame( customer, customerInventory.getCustomer() ); - Assert.assertTrue( anotherCustomer.getInventories().isEmpty() ); - Assert.assertEquals( 1, customer.getInventories().size() ); + assertSame( customer, customerInventory.getCustomer() ); + assertTrue( anotherCustomer.getInventories().isEmpty() ); + assertEquals( 1, customer.getInventories().size() ); customer.addInventory( new CustomerInventory() ); - Assert.assertEquals( 2, customer.getInventories().size() ); + assertEquals( 2, customer.getInventories().size() ); // Test remove customer.removeInventory( customerInventory ); - Assert.assertEquals( 1, customer.getInventories().size() ); + assertEquals( 1, customer.getInventories().size() ); // This happens (and is expected) because there was no snapshot taken before remove - Assert.assertNotNull( customerInventory.getCustomer() ); + assertNotNull( customerInventory.getCustomer() ); } // --- // diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest.java index 454ca7e272..a3de48568d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest.java @@ -10,14 +10,15 @@ import java.util.HashSet; import java.util.Set; import org.hibernate.Hibernate; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -34,21 +35,21 @@ import static org.junit.Assert.assertTrue; * @author Marco Belladelli * @author Tomas Cerskus */ -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16136") -public class OneToManyLazyAndEagerTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - Order.class, - OrderItem.class - }; - } +@DomainModel( + annotatedClasses = { + OneToManyLazyAndEagerTest.User.class, + OneToManyLazyAndEagerTest.Order.class, + OneToManyLazyAndEagerTest.OrderItem.class + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16136") +public class OneToManyLazyAndEagerTest { - @Before - public void prepare() { - doInJPA( this::entityManagerFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { final User user = new User( "User 1", "Marco" ); final User targetUser = new User( "User 2", "Andrea" ); final Order order = new Order( "Order 1", user, targetUser ); @@ -61,9 +62,9 @@ public class OneToManyLazyAndEagerTest extends BaseEntityManagerFunctionalTestCa } ); } - @After - public void tearDown() { - doInJPA( this::entityManagerFactory, em -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.createQuery( "delete from OrderItem" ).executeUpdate(); em.createQuery( "delete from Order" ).executeUpdate(); em.createQuery( "delete from User" ).executeUpdate(); @@ -71,8 +72,8 @@ public class OneToManyLazyAndEagerTest extends BaseEntityManagerFunctionalTestCa } @Test - public void testQuery() { - doInJPA( this::entityManagerFactory, em -> { + public void testQuery(SessionFactoryScope scope) { + scope.inTransaction( em -> { final Order order = em.createQuery( "select o from Order o", Order.class ) .getResultList() .get( 0 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java index 78ba5dc4a5..8200054105 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToManyLazyAndEagerTest2.java @@ -6,42 +6,46 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Marco Belladelli * @author Tomas Cerskus */ -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16477") -public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - Coupon.class, - Order.class - }; - } +@DomainModel( + annotatedClasses = { + OneToManyLazyAndEagerTest2.User.class, + OneToManyLazyAndEagerTest2.Coupon.class, + OneToManyLazyAndEagerTest2.Order.class + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16477") +public class OneToManyLazyAndEagerTest2 { - @Before - public void prepare() { - doInJPA( this::entityManagerFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { final User user = new User( "User 1", "Marco" ); final User targetUser = new User( "User 2", "Andrea" ); final Coupon coupon = new Coupon( "Coupon 1", targetUser ); @@ -53,9 +57,9 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC } ); } - @After - public void tearDown() { - doInJPA( this::entityManagerFactory, em -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.createQuery( "delete from Order" ).executeUpdate(); em.createQuery( "delete from Coupon" ).executeUpdate(); em.createQuery( "delete from User" ).executeUpdate(); @@ -63,22 +67,22 @@ public class OneToManyLazyAndEagerTest2 extends BaseEntityManagerFunctionalTestC } @Test - public void testQuery() { - doInJPA( this::entityManagerFactory, em -> { + public void testQuery(SessionFactoryScope scope) { + scope.inTransaction( em -> { final Order order = em.createQuery( "select o from Order o", Order.class ) .getResultList() .get( 0 ); final User user = order.getUser(); - assertTrue( "Proxy should be initialized", Hibernate.isInitialized( user ) ); + assertTrue( Hibernate.isInitialized( user ), "Proxy should be initialized" ); assertEquals( "Marco", user.getName() ); final User targetUser = order.getTargetUser(); - assertTrue( "Proxy should be initialized", Hibernate.isInitialized( targetUser ) ); + assertTrue( Hibernate.isInitialized( targetUser ), "Proxy should be initialized" ); assertEquals( "Andrea", targetUser.getName() ); final Coupon coupon = order.getCoupon(); - assertTrue( "Proxy should be initialized", Hibernate.isInitialized( coupon ) ); + assertTrue( Hibernate.isInitialized( coupon ), "Proxy should be initialized" ); assertThat( coupon.getTargetUser() ).isSameAs( targetUser ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOnEnhancedEntityLoadedAsReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOnEnhancedEntityLoadedAsReferenceTest.java index 0c704821f2..c93b54a361 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOnEnhancedEntityLoadedAsReferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOnEnhancedEntityLoadedAsReferenceTest.java @@ -2,14 +2,16 @@ package org.hibernate.orm.test.bytecode.enhancement.association; import org.hibernate.Hibernate; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -18,30 +20,30 @@ import jakarta.persistence.Id; import jakarta.persistence.OneToOne; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; @JiraKey("HHH-17173") -@RunWith(BytecodeEnhancerRunner.class) -public class OneToOnEnhancedEntityLoadedAsReferenceTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + OneToOnEnhancedEntityLoadedAsReferenceTest.ContainingEntity.class, OneToOnEnhancedEntityLoadedAsReferenceTest.ContainedEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10"), + @Setting(name = AvailableSettings.MAX_FETCH_DEPTH, value = "0") + } +) +@SessionFactory +@BytecodeEnhanced +public class OneToOnEnhancedEntityLoadedAsReferenceTest { private long entityId; private long entityId2; private long containedEntityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { ContainingEntity.class, ContainedEntity.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, 10 ); - configuration.setProperty( AvailableSettings.MAX_FETCH_DEPTH, 0 ); - } - - @Before - public void prepare() { - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { ContainingEntity entity = new ContainingEntity(); ContainedEntity containedEntity = new ContainedEntity(); containedEntity.setValue( "value" ); @@ -60,8 +62,8 @@ public class OneToOnEnhancedEntityLoadedAsReferenceTest extends BaseCoreFunction } @Test - public void test() { - doInJPA( this::sessionFactory, em -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( em -> { ContainingEntity entity2 = em.getReference( ContainingEntity.class, entityId2 ); ContainingEntity entity = em.getReference( ContainingEntity.class, entityId ); ContainedEntity containedEntity = em.getReference( ContainedEntity.class, containedEntityId ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOneAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOneAssociationTest.java index cd47f2e2dd..4cd35d5e8c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOneAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/association/OneToOneAssociationTest.java @@ -6,22 +6,23 @@ */ package org.hibernate.orm.test.bytecode.enhancement.association; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class OneToOneAssociationTest { @Test @@ -32,7 +33,7 @@ public class OneToOneAssociationTest { Customer customer = new Customer(); customer.setUser( user ); - Assert.assertEquals( customer, user.getCustomer() ); + assertEquals( customer, user.getCustomer() ); // check dirty tracking is set automatically with bi-directional association management EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" ); @@ -42,12 +43,12 @@ public class OneToOneAssociationTest { customer.setUser( anotherUser ); - Assert.assertNull( user.getCustomer() ); - Assert.assertEquals( customer, anotherUser.getCustomer() ); + assertNull( user.getCustomer() ); + assertEquals( customer, anotherUser.getCustomer() ); user.setCustomer( new Customer() ); - Assert.assertEquals( user, user.getCustomer().getUser() ); + assertEquals( user, user.getCustomer().getUser() ); } @Test @@ -58,15 +59,15 @@ public class OneToOneAssociationTest { Customer customer = new Customer(); customer.setUser( user ); - Assert.assertEquals( customer, user.getCustomer() ); + assertEquals( customer, user.getCustomer() ); // check dirty tracking is set automatically with bi-directional association management EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" ); user.setCustomer( null ); - Assert.assertNull( user.getCustomer() ); - Assert.assertNull( customer.getUser() ); + assertNull( user.getCustomer() ); + assertNull( customer.getUser() ); } // --- // diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/BagAndSetFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/BagAndSetFetchTest.java index f4f7e58e49..6096df1da2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/BagAndSetFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/BagAndSetFetchTest.java @@ -1,40 +1,39 @@ package org.hibernate.orm.test.bytecode.enhancement.bag; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.junit.Assert.assertTrue; - - -@RunWith(BytecodeEnhancerRunner.class) -public class BagAndSetFetchTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EntityA.class, - EntityB.class, - EntityC.class, - EntityD.class, - }; - } +@DomainModel( + annotatedClasses = { + BagAndSetFetchTest.EntityA.class, + BagAndSetFetchTest.EntityB.class, + BagAndSetFetchTest.EntityC.class, + BagAndSetFetchTest.EntityD.class, + } +) +@SessionFactory +@BytecodeEnhanced +public class BagAndSetFetchTest { @Test - public void testIt() { - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityB b = new EntityB( 1l, "b" ); @@ -85,7 +84,7 @@ public class BagAndSetFetchTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { EntityA entityA = session.find( EntityA.class, 1l ); Collection attributes = entityA.attributes; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/EagerBagsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/EagerBagsTest.java index e2c7d838f2..8b6c3b5c64 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/EagerBagsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/bag/EagerBagsTest.java @@ -1,38 +1,39 @@ package org.hibernate.orm.test.bytecode.enhancement.bag; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.Collection; import java.util.LinkedList; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.junit.Assert.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) -public class EagerBagsTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EntityA.class, - EntityB.class, - EntityC.class, - EntityD.class, - }; - } +@DomainModel( + annotatedClasses = { + EagerBagsTest.EntityA.class, + EagerBagsTest.EntityB.class, + EagerBagsTest.EntityC.class, + EagerBagsTest.EntityD.class, + } +) +@SessionFactory +@BytecodeEnhanced +public class EagerBagsTest { @Test - public void testIt() { - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityB b = new EntityB( 1l, "b" ); @@ -70,7 +71,7 @@ public class EagerBagsTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { EntityA entityA = session.find( EntityA.class, 1l ); Collection attributes = entityA.attributes; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java index b5fc8c19ff..b2a20a03a3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicEnhancementTest.java @@ -10,14 +10,11 @@ import org.hibernate.Version; import org.hibernate.bytecode.enhance.spi.EnhancementInfo; import org.hibernate.engine.spi.ManagedEntity; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.orm.test.legacy.Simple; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.Jira; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -26,18 +23,18 @@ import java.util.Collections; import java.util.List; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class BasicEnhancementTest { @Test @@ -62,10 +59,10 @@ public class BasicEnhancementTest { } @Test - @TestForIssue(jiraKey = "HHH-13439") + @Jira("HHH-13439") public void enhancementInfoTest() { EnhancementInfo info = SimpleEntity.class.getAnnotation( EnhancementInfo.class ); - assertNotNull( "EnhancementInfo was not applied", info ); + assertNotNull( info, "EnhancementInfo was not applied" ); assertEquals( Version.getVersionString(), info.version() ); } @@ -114,7 +111,7 @@ public class BasicEnhancementTest { ( (PersistentAttributeInterceptable) entity ).$$_hibernate_setInterceptor( null ); entity.id = 1234567890L; - Assert.assertEquals( 1234567890L, (long) entity.getId() ); + assertEquals( 1234567890L, (long) entity.getId() ); entity.name = "Entity Name"; assertSame( "Entity Name", entity.name ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicSessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicSessionTest.java index c9aae9149d..7d7cfb54a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/BasicSessionTest.java @@ -8,42 +8,44 @@ package org.hibernate.orm.test.bytecode.enhancement.basic; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.ManagedEntity; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Transient; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) -public class BasicSessionTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ MyEntity.class}; - } +@DomainModel( + annotatedClasses = { + BasicSessionTest.MyEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced +public class BasicSessionTest { @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.save( new MyEntity( 1L ) ); s.save( new MyEntity( 2L ) ); } ); MyEntity[] entities = new MyEntity[2]; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entities[0] = s.get( MyEntity.class, 1L ); entities[1] = s.get( MyEntity.class, 2L ); @@ -70,7 +72,7 @@ public class BasicSessionTest extends BaseCoreFunctionalTestCase { @Entity( name = "MyEntity" ) @Table( name = "MY_ENTITY" ) - private static class MyEntity implements ManagedEntity { + static class MyEntity implements ManagedEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/CrossEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/CrossEnhancementTest.java index 95fe6d3a25..aa977c1d0b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/CrossEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/CrossEnhancementTest.java @@ -6,11 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.basic; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.EmbeddedId; @@ -21,44 +22,45 @@ import jakarta.persistence.MapsId; import jakarta.persistence.Table; import java.io.Serializable; -import org.hibernate.SessionFactory; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-9529" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class CrossEnhancementTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class, ChildKey.class}; - } +@JiraKey( "HHH-9529" ) +@DomainModel( + annotatedClasses = { + CrossEnhancementTest.Parent.class, CrossEnhancementTest.Child.class, CrossEnhancementTest.ChildKey.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CrossEnhancementTest { @Test - public void test() { - sessionFactory().close(); - buildSessionFactory(); - } + public void test(SessionFactoryScope scope) { + // sessionFactory().close(); + // buildSessionFactory(); + scope.getSessionFactory().close(); + // TODO: I do not get this test ^ and not sure how to update it ... + } // --- // @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id String id; } @Embeddable - private static class ChildKey implements Serializable { + static class ChildKey implements Serializable { String parent; String type; } @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @EmbeddedId ChildKey id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedAssociationManagementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedAssociationManagementTest.java index f7dfc0f7a3..f68fceda71 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedAssociationManagementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedAssociationManagementTest.java @@ -6,26 +6,25 @@ */ package org.hibernate.orm.test.bytecode.enhancement.basic; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.getFieldByReflection; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class ExtendedAssociationManagementTest { @Test @@ -46,7 +45,7 @@ public class ExtendedAssociationManagementTest { customer.user = anotherUser; - Assert.assertNull( user.customer ); + assertNull( user.customer ); assertEquals( customer, getFieldByReflection( anotherUser, "customer" ) ); user.customer = new Customer(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedEnhancementNonStandardAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedEnhancementNonStandardAccessTest.java index fa83abd512..2d3b596696 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedEnhancementNonStandardAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/ExtendedEnhancementNonStandardAccessTest.java @@ -4,11 +4,12 @@ import static org.assertj.core.api.Assertions.assertThat; import org.hibernate.Hibernate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -21,19 +22,18 @@ import jakarta.persistence.Id; * static accessors, accessors defined in a subclass, * or accessors defined in an inner class. */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ExtendedEnhancementNonStandardAccessTest.MyAbstractEntity.class, ExtendedEnhancementNonStandardAccessTest.MyAbstractConfusingEntity.class, ExtendedEnhancementNonStandardAccessTest.MyConcreteEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, extendedEnhancement = true) -public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyAbstractEntity.class, MyAbstractConfusingEntity.class, MyConcreteEntity.class - }; - } +public class ExtendedEnhancementNonStandardAccessTest { @Test - public void nonStandardInstanceGetterSetterPublicField() { + public void nonStandardInstanceGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -44,11 +44,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForPublicField(); } - } ); + }, scope ); } @Test - public void nonStandardInstanceGetterSetterProtectedField() { + public void nonStandardInstanceGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -59,11 +59,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForProtectedField(); } - } ); + }, scope ); } @Test - public void nonStandardInstanceGetterSetterPackagePrivateField() { + public void nonStandardInstanceGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -74,11 +74,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForPackagePrivateField(); } - } ); + }, scope ); } @Test - public void nonStandardInstanceGetterSetterPrivateField() { + public void nonStandardInstanceGetterSetterPrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -89,11 +89,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForPrivateField(); } - } ); + }, scope ); } @Test - public void staticGetterSetterPublicField() { + public void staticGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -104,11 +104,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.staticGetPublicField( entity ); } - } ); + }, scope ); } @Test - public void staticGetterSetterProtectedField() { + public void staticGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -119,11 +119,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.staticGetProtectedField( entity ); } - } ); + }, scope ); } @Test - public void staticGetterSetterPackagePrivateField() { + public void staticGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -134,11 +134,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.staticGetPackagePrivateField( entity ); } - } ); + }, scope ); } @Test - public void staticGetterSetterPrivateField() { + public void staticGetterSetterPrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -149,11 +149,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.staticGetPrivateField( entity ); } - } ); + }, scope ); } @Test - public void innerClassStaticGetterSetterPublicField() { + public void innerClassStaticGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -164,11 +164,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.InnerClassAccessors.staticGetPublicField( entity ); } - } ); + }, scope ); } @Test - public void innerClassStaticGetterSetterProtectedField() { + public void innerClassStaticGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -179,11 +179,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.InnerClassAccessors.staticGetProtectedField( entity ); } - } ); + }, scope ); } @Test - public void innerClassStaticGetterSetterPackagePrivateField() { + public void innerClassStaticGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -194,11 +194,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.InnerClassAccessors.staticGetPackagePrivateField( entity ); } - } ); + }, scope ); } @Test - public void innerClassStaticGetterSetterPrivateField() { + public void innerClassStaticGetterSetterPrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -209,11 +209,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return MyConcreteEntity.InnerClassAccessors.staticGetPrivateField( entity ); } - } ); + }, scope ); } @Test - public void innerClassInstanceGetterSetterPublicField() { + public void innerClassInstanceGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -224,11 +224,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new MyConcreteEntity.InnerClassAccessors().instanceGetPublicField( entity ); } - } ); + }, scope ); } @Test - public void innerClassInstanceGetterSetterProtectedField() { + public void innerClassInstanceGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -239,11 +239,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new MyConcreteEntity.InnerClassAccessors().instanceGetProtectedField( entity ); } - } ); + }, scope ); } @Test - public void innerClassInstanceGetterSetterPackagePrivateField() { + public void innerClassInstanceGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -254,11 +254,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new MyConcreteEntity.InnerClassAccessors().instanceGetPackagePrivateField( entity ); } - } ); + }, scope ); } @Test - public void innerClassInstanceGetterSetterPrivateField() { + public void innerClassInstanceGetterSetterPrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -269,11 +269,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new MyConcreteEntity.InnerClassAccessors().instanceGetPrivateField( entity ); } - } ); + }, scope ); } @Test - public void externalClassStaticGetterSetterPublicField() { + public void externalClassStaticGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -284,11 +284,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return ExternalClassAccessors.staticGetPublicField( entity ); } - } ); + }, scope ); } @Test - public void externalClassStaticGetterSetterProtectedField() { + public void externalClassStaticGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -299,11 +299,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return ExternalClassAccessors.staticGetProtectedField( entity ); } - } ); + }, scope ); } @Test - public void externalClassStaticGetterSetterPackagePrivateField() { + public void externalClassStaticGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -314,11 +314,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return ExternalClassAccessors.staticGetPackagePrivateField( entity ); } - } ); + }, scope ); } @Test - public void externalClassInstanceGetterSetterPublicField() { + public void externalClassInstanceGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -329,11 +329,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new ExternalClassAccessors().instanceGetPublicField( entity ); } - } ); + }, scope ); } @Test - public void externalClassInstanceGetterSetterProtectedField() { + public void externalClassInstanceGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -344,11 +344,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new ExternalClassAccessors().instanceGetProtectedField( entity ); } - } ); + }, scope ); } @Test - public void externalClassInstanceGetterSetterPackagePrivateField() { + public void externalClassInstanceGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -359,11 +359,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return new ExternalClassAccessors().instanceGetPackagePrivateField( entity ); } - } ); + }, scope ); } @Test - public void subClassInstanceGetterSetterPublicField() { + public void subClassInstanceGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -374,11 +374,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.getAbstractEntityPublicField(); } - } ); + }, scope ); } @Test - public void subClassInstanceGetterSetterProtectedField() { + public void subClassInstanceGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -389,11 +389,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.getAbstractEntityProtectedField(); } - } ); + }, scope ); } @Test - public void subClassInstanceGetterSetterPackagePrivateField() { + public void subClassInstanceGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -404,11 +404,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.getAbstractEntityPackagePrivateField(); } - } ); + }, scope ); } @Test - public void subClassNonStandardInstanceGetterSetterPublicField() { + public void subClassNonStandardInstanceGetterSetterPublicField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -419,11 +419,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForAbstractEntityPublicField(); } - } ); + }, scope ); } @Test - public void subClassNonStandardInstanceGetterSetterProtectedField() { + public void subClassNonStandardInstanceGetterSetterProtectedField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -434,11 +434,11 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForAbstractEntityProtectedField(); } - } ); + }, scope ); } @Test - public void subClassNonStandardInstanceGetterSetterPackagePrivateField() { + public void subClassNonStandardInstanceGetterSetterPackagePrivateField(SessionFactoryScope scope) { doTestFieldAccess( new AccessDelegate() { @Override public void setValue(MyConcreteEntity entity, Long value) { @@ -449,33 +449,33 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional public Long getValue(MyConcreteEntity entity) { return entity.nonStandardGetterForAbstractEntityPackagePrivateField(); } - } ); + }, scope ); } // Ideally we'd make this a @ParameterizedTest and pass the access delegate as parameter, // but we cannot do that due to JUnit using a different classloader than the test. - private void doTestFieldAccess(AccessDelegate delegate) { - Long id = fromTransaction( em -> { + private void doTestFieldAccess(AccessDelegate delegate, SessionFactoryScope scope) { + Long id = scope.fromTransaction( em -> { var entity = new MyConcreteEntity(); em.persist( entity ); return entity.id; } ); - inTransaction( em -> { + scope.inTransaction( em -> { var entity = em.find( MyConcreteEntity.class, id ); assertThat( delegate.getValue( entity ) ) .as( "Loaded value before update" ) .isNull(); } ); - inTransaction( em -> { + scope.inTransaction( em -> { var entity = em.getReference( MyConcreteEntity.class, id ); // Since field access is replaced with accessor calls, // we expect this change to be detected by dirty tracking and persisted. delegate.setValue( entity, 42L ); } ); - inTransaction( em -> { + scope.inTransaction( em -> { var entity = em.find( MyConcreteEntity.class, id ); // We're working on an initialized entity. assertThat( entity ) @@ -487,7 +487,7 @@ public class ExtendedEnhancementNonStandardAccessTest extends BaseCoreFunctional .isEqualTo( 42L ); } ); - inTransaction( em -> { + scope.inTransaction( em -> { var entity = em.getReference( MyConcreteEntity.class, id ); // We're working on an uninitialized entity. assertThat( entity ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/FinalFieldEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/FinalFieldEnhancementTest.java index 6c7ebb2b8f..1d3b5bf8f0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/FinalFieldEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/FinalFieldEnhancementTest.java @@ -18,24 +18,25 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import org.hibernate.annotations.Immutable; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; -@RunWith(BytecodeEnhancerRunner.class) -public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EntityWithFinalField.class, - EntityWithEmbeddedIdWithFinalField.class, EntityWithEmbeddedIdWithFinalField.EmbeddableId.class, - EntityWithEmbeddedNonIdWithFinalField.class, EntityWithEmbeddedNonIdWithFinalField.EmbeddableNonId.class - }; - } +@DomainModel( + annotatedClasses = { + FinalFieldEnhancementTest.EntityWithFinalField.class, + FinalFieldEnhancementTest.EntityWithEmbeddedIdWithFinalField.class, FinalFieldEnhancementTest.EntityWithEmbeddedIdWithFinalField.EmbeddableId.class, + FinalFieldEnhancementTest.EntityWithEmbeddedNonIdWithFinalField.class, FinalFieldEnhancementTest.EntityWithEmbeddedNonIdWithFinalField.EmbeddableNonId.class + } +) +@SessionFactory +@BytecodeEnhanced +public class FinalFieldEnhancementTest { @Test public void entityWithFinalField_constructor() { @@ -45,14 +46,14 @@ public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase { // Just test that the embedded non-ID works correctly over a persist/retrieve cycle @Test - public void entityWithFinalField_smokeTest() { + public void entityWithFinalField_smokeTest(SessionFactoryScope scope) { EntityWithFinalField persistedEntity = new EntityWithFinalField( "foo" ); persistedEntity.setName( "Some name" ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( persistedEntity ); } ); - inTransaction( s -> { + scope.inTransaction( s -> { EntityWithFinalField entity = s.find( EntityWithFinalField.class, persistedEntity.getId() ); assertThat( entity ).extracting( EntityWithFinalField::getImmutableProperty ) .isEqualTo( persistedEntity.getImmutableProperty() ); @@ -61,22 +62,22 @@ public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase { // Just test that the embedded ID works correctly over a persist/retrieve cycle @Test - public void embeddableIdWithFinalField_smokeTest() { + public void embeddableIdWithFinalField_smokeTest(SessionFactoryScope scope) { EntityWithEmbeddedIdWithFinalField persistedEntity = new EntityWithEmbeddedIdWithFinalField(); persistedEntity.setName( "Some name" ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( persistedEntity ); } ); // Read with the same ID instance - inTransaction( s -> { + scope.inTransaction( s -> { EntityWithEmbeddedIdWithFinalField entity = s.find( EntityWithEmbeddedIdWithFinalField.class, persistedEntity.getId() ); assertThat( entity ).extracting( EntityWithEmbeddedIdWithFinalField::getId ).extracting( i -> i.id ) .isEqualTo( persistedEntity.getId().id ); } ); // Read with a new ID instance - inTransaction( s -> { + scope.inTransaction( s -> { EntityWithEmbeddedIdWithFinalField entity = s.find( EntityWithEmbeddedIdWithFinalField.class, EntityWithEmbeddedIdWithFinalField.EmbeddableId.of( persistedEntity.getId().id ) ); assertThat( entity ).extracting( EntityWithEmbeddedIdWithFinalField::getId ).extracting( i -> i.id ) .isEqualTo( persistedEntity.getId().id ); @@ -87,7 +88,7 @@ public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase { // we know Hibernate ORM *has to* instantiate the EmbeddableIdType itself: // it cannot reuse the ID we passed. // And since the EmbeddableIdType has a final field, instantiation will not be able to use a no-arg constructor... - inTransaction( s -> { + scope.inTransaction( s -> { EntityWithEmbeddedIdWithFinalField entity = s.createQuery( "from embidwithfinal e where e.name = :name", EntityWithEmbeddedIdWithFinalField.class ) .setParameter( "name", persistedEntity.getName() ) @@ -106,15 +107,15 @@ public class FinalFieldEnhancementTest extends BaseCoreFunctionalTestCase { // Just test that the embedded non-ID works correctly over a persist/retrieve cycle @Test - public void embeddableNonIdWithFinalField_smokeTest() { + public void embeddableNonIdWithFinalField_smokeTest(SessionFactoryScope scope) { EntityWithEmbeddedNonIdWithFinalField persistedEntity = new EntityWithEmbeddedNonIdWithFinalField(); persistedEntity.setName( "Some name" ); persistedEntity.setEmbedded( new EntityWithEmbeddedNonIdWithFinalField.EmbeddableNonId( "foo" ) ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( persistedEntity ); } ); - inTransaction( s -> { + scope.inTransaction( s -> { EntityWithEmbeddedNonIdWithFinalField entity = s.find( EntityWithEmbeddedNonIdWithFinalField.class, persistedEntity.getId() ); assertThat( entity ).extracting( EntityWithEmbeddedNonIdWithFinalField::getEmbedded ) .extracting( EntityWithEmbeddedNonIdWithFinalField.EmbeddableNonId::getImmutableProperty ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/GenericReturnValueMappedSuperclassEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/GenericReturnValueMappedSuperclassEnhancementTest.java index e15aba2606..53ac0df23e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/GenericReturnValueMappedSuperclassEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/GenericReturnValueMappedSuperclassEnhancementTest.java @@ -6,7 +6,8 @@ */ package org.hibernate.orm.test.bytecode.enhancement.basic; -import static org.junit.Assert.assertEquals; + +import static org.junit.jupiter.api.Assertions.assertEquals; import jakarta.persistence.Access; import jakarta.persistence.AccessType; @@ -17,16 +18,16 @@ import jakarta.persistence.MappedSuperclass; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.Test; -import org.junit.runner.RunWith; -@RunWith(BytecodeEnhancerRunner.class) +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; + +@BytecodeEnhanced public class GenericReturnValueMappedSuperclassEnhancementTest { @Test - @TestForIssue(jiraKey = "HHH-12579") + @JiraKey("HHH-12579") public void enhanceClassWithGenericReturnValueOnMappedSuperclass() { SimpleEntity implementation = new SimpleEntity(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/InheritedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/InheritedTest.java index 2096f502bc..b0e04f9e7b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/InheritedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/InheritedTest.java @@ -4,13 +4,12 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.UnloadedClass; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -18,16 +17,16 @@ import jakarta.persistence.Version; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.clearDirtyTracking; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * @author Luis Barreiro * @author Craig Andrews */ -@TestForIssue( jiraKey = "HHH-11284" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11284" ) +@BytecodeEnhanced @CustomEnhancementContext( {EnhancerTestContext.class, InheritedTest.EagerEnhancementContext.class} ) public class InheritedTest { @@ -62,7 +61,7 @@ public class InheritedTest { // Adapted from BasicEnhancementTest#basicExtendedEnhancementTest @Test - @TestForIssue(jiraKey = "HHH-14006") + @JiraKey("HHH-14006") public void extendedEnhancementTest() { // This test only works if lazy loading bytecode enhancement is enabled, // otherwise extended bytecode enhancement does not do anything we can check. diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/MappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/MappedSuperclassTest.java index d8200915b4..61cc41b9ce 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/MappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/MappedSuperclassTest.java @@ -4,13 +4,12 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.UnloadedClass; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -19,15 +18,15 @@ import jakarta.persistence.Version; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.clearDirtyTracking; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10646" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-10646" ) +@BytecodeEnhanced @CustomEnhancementContext( {EnhancerTestContext.class, MappedSuperclassTest.EagerEnhancementContext.class} ) public class MappedSuperclassTest { @@ -49,7 +48,7 @@ public class MappedSuperclassTest { // Adapted from BasicEnhancementTest#basicExtendedEnhancementTest @Test - @TestForIssue(jiraKey = "HHH-14006") + @JiraKey("HHH-14006") public void extendedEnhancementTest() { // This test only works if lazy loading bytecode enhancement is enabled, // otherwise extended bytecode enhancement does not do anything we can check. diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/OverriddenFieldTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/OverriddenFieldTest.java index 996915dc9f..57b8d969bd 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/OverriddenFieldTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/basic/OverriddenFieldTest.java @@ -1,14 +1,13 @@ package org.hibernate.orm.test.bytecode.enhancement.basic; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -21,31 +20,28 @@ import jakarta.persistence.Table; * when the entity has the same field defined twice: once in a mappedsuperclass (should be ignored) * and once in the concrete entity class. */ -@TestForIssue(jiraKey = "HHH-15505") -@RunWith(BytecodeEnhancerRunner.class) -public class OverriddenFieldTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { AbstractEntity.class, Fruit.class }; - } - - @Before - public void prepare() { - } +@JiraKey("HHH-15505") +@DomainModel( + annotatedClasses = { + OverriddenFieldTest.AbstractEntity.class, OverriddenFieldTest.Fruit.class + } +) +@SessionFactory +@BytecodeEnhanced +public class OverriddenFieldTest { @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Fruit testEntity = new Fruit(); testEntity.setId( 1 ); testEntity.setName( "John" ); s.persist( testEntity ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Fruit testEntity = s.get( Fruit.class, 1 ); - Assert.assertEquals( "John", testEntity.getName() ); + assertEquals( "John", testEntity.getName() ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyTest.java index 35fe560916..63304f8f59 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyTest.java @@ -7,17 +7,18 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.annotations.Proxy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -33,21 +34,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @JiraKey("HHH-16890") -@RunWith(BytecodeEnhancerRunner.class) -public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + BatchEntityOneToManyTest.Order.class, BatchEntityOneToManyTest.Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class BatchEntityOneToManyTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Order.class, Product.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setupData() { + @BeforeEach + public void setupData(SessionFactoryScope scope) { Product cheese1 = new Product( 1l, "Cheese 1" ); Product cheese2 = new Product( 2l, "Cheese 2" ); Product cheese3 = new Product( 3l, "Cheese 3" ); @@ -57,7 +59,7 @@ public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { order.addProduct( cheese1 ); order.addProduct( cheese2 ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( cheese1 ); s.persist( cheese2 ); s.persist( cheese3 ); @@ -65,9 +67,9 @@ public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { } ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Order" ).executeUpdate(); session.createMutationQuery( "delete from Product" ).executeUpdate(); @@ -76,8 +78,8 @@ public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { } @Test - public void testGetOrder() { - inSession( s -> { + public void testGetOrder(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Order o = s.get( Order.class, 1 ); @@ -89,8 +91,8 @@ public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { @Test - public void testGetProduct() { - inSession( s -> { + public void testGetProduct(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product = s.getReference( Product.class, 3l ); @@ -102,8 +104,8 @@ public class BatchEntityOneToManyTest extends BaseCoreFunctionalTestCase { } @Test - public void testCriteriaQuery() { - inSession( s -> { + public void testCriteriaQuery(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); CriteriaBuilder cb = s.getCriteriaBuilder(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyWithDisabledProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyWithDisabledProxyTest.java index 104f5a155d..0fef2c94a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyWithDisabledProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityOneToManyWithDisabledProxyTest.java @@ -9,15 +9,17 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Proxy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -33,21 +35,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @JiraKey("HHH-16890") -@RunWith(BytecodeEnhancerRunner.class) -public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + BatchEntityOneToManyWithDisabledProxyTest.Order.class, BatchEntityOneToManyWithDisabledProxyTest.Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class BatchEntityOneToManyWithDisabledProxyTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Order.class, Product.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setupData() { + @BeforeEach + public void setupData(SessionFactoryScope scope) { Product cheese1 = new Product( 1l, "Cheese 1" ); Product cheese2 = new Product( 2l, "Cheese 2" ); Product cheese3 = new Product( 3l, "Cheese 3" ); @@ -57,7 +60,7 @@ public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctiona order.addProduct( cheese1 ); order.addProduct( cheese2 ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( cheese1 ); s.persist( cheese2 ); s.persist( cheese3 ); @@ -65,9 +68,9 @@ public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctiona } ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Order" ).executeUpdate(); session.createMutationQuery( "delete from Product" ).executeUpdate(); @@ -76,8 +79,8 @@ public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctiona } @Test - public void testGetOrder() { - inSession( s -> { + public void testGetOrder(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Order o = s.get( Order.class, 1 ); @@ -89,8 +92,8 @@ public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctiona @Test - public void testGetProduct() { - inSession( s -> { + public void testGetProduct(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product = s.getReference( Product.class, 3l ); @@ -102,8 +105,8 @@ public class BatchEntityOneToManyWithDisabledProxyTest extends BaseCoreFunctiona } @Test - public void testCriteriaQuery() { - inSession( s -> { + public void testCriteriaQuery(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); CriteriaBuilder cb = s.getCriteriaBuilder(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchTest.java index 0f63ad71fa..c5583e18e8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchTest.java @@ -7,15 +7,17 @@ import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -33,24 +35,23 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @JiraKey("HHH-16890") -@RunWith( BytecodeEnhancerRunner.class ) -public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + BatchEntityWithSelectFetchTest.Order.class, + BatchEntityWithSelectFetchTest.Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class BatchEntityWithSelectFetchTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Order.class, - Product.class - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setupData() { + @BeforeEach + public void setupData(SessionFactoryScope scope) { Product cheese1 = new Product( 1l, "Cheese 1" ); Product cheese2 = new Product( 2l, "Cheese 2" ); Product cheese3 = new Product( 3l, "Cheese 3" ); @@ -61,7 +62,7 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { order.setProduct( cheese2 ); order2.setProduct( cheese1 ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( cheese1 ); s.persist( cheese2 ); s.persist( cheese3 ); @@ -70,9 +71,9 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { } ); } - @After - public void tearDown(){ - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createMutationQuery( "delete from Order" ).executeUpdate(); session.createMutationQuery( "delete from Product" ).executeUpdate(); @@ -81,8 +82,8 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { } @Test - public void testGetOrder() { - inSession( s -> { + public void testGetOrder(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product1 = s.getReference( Product.class, 1l ); @@ -96,8 +97,8 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { } @Test - public void testGetOrder2() { - inSession( s -> { + public void testGetOrder2(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product = s.getReference( Product.class, 2l ); @@ -110,8 +111,8 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { } @Test - public void testGetProduct() { - inSession( s -> { + public void testGetProduct(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product3 = s.getReference( Product.class, 3l ); @@ -125,8 +126,8 @@ public class BatchEntityWithSelectFetchTest extends BaseCoreFunctionalTestCase { } @Test - public void testCriteriaQuery() { - inSession( s -> { + public void testCriteriaQuery(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product1 = s.getReference( Product.class, 1l ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchWithDisableProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchWithDisableProxyTest.java index b2e85caf14..b259726f1b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchWithDisableProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/batch/BatchEntityWithSelectFetchWithDisableProxyTest.java @@ -8,15 +8,17 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Proxy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -34,24 +36,23 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @JiraKey("HHH-16890") -@RunWith( BytecodeEnhancerRunner.class ) -public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + BatchEntityWithSelectFetchWithDisableProxyTest.Order.class, + BatchEntityWithSelectFetchWithDisableProxyTest.Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class BatchEntityWithSelectFetchWithDisableProxyTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Order.class, - Product.class - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setupData() { + @BeforeEach + public void setupData(SessionFactoryScope scope) { Product cheese1 = new Product( 1l, "Cheese 1" ); Product cheese2 = new Product( 2l, "Cheese 2" ); Product cheese3 = new Product( 3l, "Cheese 3" ); @@ -62,7 +63,7 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc order.setProduct( cheese2 ); order2.setProduct( cheese1 ); - inTransaction( s -> { + scope.inTransaction( s -> { s.persist( cheese1 ); s.persist( cheese2 ); s.persist( cheese3 ); @@ -71,9 +72,9 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc } ); } - @After - public void tearDown(){ - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createMutationQuery( "delete from Order" ).executeUpdate(); session.createMutationQuery( "delete from Product" ).executeUpdate(); @@ -82,8 +83,8 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc } @Test - public void testGetOrder() { - inSession( s -> { + public void testGetOrder(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product1 = s.getReference( Product.class, 1l ); @@ -97,8 +98,8 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc } @Test - public void testGetOrder2() { - inSession( s -> { + public void testGetOrder2(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product = s.getReference( Product.class, 2l ); @@ -111,8 +112,8 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc } @Test - public void testGetProduct() { - inSession( s -> { + public void testGetProduct(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product3 = s.getReference( Product.class, 3l ); @@ -126,8 +127,8 @@ public class BatchEntityWithSelectFetchWithDisableProxyTest extends BaseCoreFunc } @Test - public void testCriteriaQuery() { - inSession( s -> { + public void testCriteriaQuery(SessionFactoryScope scope) { + scope.inSession( s -> { s.getSessionFactory().getCache().evictAllRegions(); Product product1 = s.getReference( Product.class, 1l ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/AbstractManyToOneNoProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/AbstractManyToOneNoProxyTest.java index c874171ddd..ab8ec93653 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/AbstractManyToOneNoProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/AbstractManyToOneNoProxyTest.java @@ -7,14 +7,16 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Proxy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.DiscriminatorColumn; @@ -33,31 +35,30 @@ import jakarta.persistence.Table; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) @JiraKey("HHH-16473") -public class AbstractManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + AbstractManyToOneNoProxyTest.Actor.class, + AbstractManyToOneNoProxyTest.User.class, + AbstractManyToOneNoProxyTest.UserGroup.class, + AbstractManyToOneNoProxyTest.ActorGroup.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class AbstractManyToOneNoProxyTest { private static final String ENTITY_A_NAME = "Alice"; private static final String ENTITY_B_NAME = "Bob"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Actor.class, - User.class, - UserGroup.class, - ActorGroup.class - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user1 = new User(); @@ -87,7 +88,7 @@ public class AbstractManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { session.getSessionFactory().getCache().evictAllRegions(); } @@ -95,8 +96,8 @@ public class AbstractManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { } @Test - public void testSelect() { - inTransaction( + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = session.getReference( User.class, 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneNoProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneNoProxyTest.java index d3b6211b10..0b389303f8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneNoProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneNoProxyTest.java @@ -7,14 +7,16 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.Proxy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.DiscriminatorColumn; @@ -33,31 +35,31 @@ import jakarta.persistence.Table; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) @JiraKey("HHH-16473") -public class ManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + ManyToOneNoProxyTest.Actor.class, + ManyToOneNoProxyTest.User.class, + ManyToOneNoProxyTest.UserGroup.class, + ManyToOneNoProxyTest.ActorGroup.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class ManyToOneNoProxyTest { private static final String ENTITY_A_NAME = "Alice"; private static final String ENTITY_B_NAME = "Bob"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Actor.class, - User.class, - UserGroup.class, - ActorGroup.class - }; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user1 = new User(); @@ -87,7 +89,7 @@ public class ManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { session.getSessionFactory().getCache().evictAllRegions(); } @@ -95,8 +97,8 @@ public class ManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { } @Test - public void testSelect() { - inTransaction( + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = session.getReference( User.class, 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTest.java index aa0a8d7980..ba6a5b52e2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTest.java @@ -6,14 +6,16 @@ import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -26,28 +28,27 @@ import jakarta.persistence.NamedQuery; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue( jiraKey = "HHH-16193") -public class ManyToOneTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-16193") +@DomainModel( + annotatedClasses = { + ManyToOneTest.EntityA.class, + ManyToOneTest.EntityB.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class ManyToOneTest { private static final String ENTITY_B_NAME = "B1"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EntityA.class, - EntityB.class - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityB b1 = new EntityB( ENTITY_B_NAME ); session.persist( b1 ); @@ -58,8 +59,8 @@ public class ManyToOneTest extends BaseCoreFunctionalTestCase { } @Test - public void testSelect() { - List entities = fromTransaction( + public void testSelect(SessionFactoryScope scope) { + List entities = scope.fromTransaction( session -> session.createNamedQuery( "PersonType.selectAll", EntityA.class ) .getResultList() diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTestReusedColumn.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTestReusedColumn.java index 1a5d7de61d..a08f11ba97 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTestReusedColumn.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cache/ManyToOneTestReusedColumn.java @@ -9,14 +9,16 @@ import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.DiscriminatorColumn; @@ -34,31 +36,30 @@ import jakarta.persistence.OneToMany; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) @JiraKey("HHH-16744") -public class ManyToOneTestReusedColumn extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + ManyToOneTestReusedColumn.Fridge.class, + ManyToOneTestReusedColumn.Container.class, + ManyToOneTestReusedColumn.CheeseContainer.class, + ManyToOneTestReusedColumn.FruitContainer.class, + ManyToOneTestReusedColumn.Food.class, + ManyToOneTestReusedColumn.Fruit.class, + ManyToOneTestReusedColumn.Cheese.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class ManyToOneTestReusedColumn { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Fridge.class, - Container.class, - CheeseContainer.class, - FruitContainer.class, - Food.class, - Fruit.class, - Cheese.class - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Fridge fridge = new Fridge(); FruitContainer fruitContainer = new FruitContainer(); @@ -91,8 +92,8 @@ public class ManyToOneTestReusedColumn extends BaseCoreFunctionalTestCase { } @Test - public void testSelect() { - inTransaction( + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( session -> { Fridge fridge = session.getReference( Fridge.class, 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PostLoadLazyListenerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PostLoadLazyListenerTest.java index 37ce3405b0..981e9b7b17 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PostLoadLazyListenerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PostLoadLazyListenerTest.java @@ -9,12 +9,13 @@ package org.hibernate.orm.test.bytecode.enhancement.callbacks; import java.util.ArrayList; import java.util.Collection; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.EntityListeners; @@ -27,17 +28,18 @@ import jakarta.persistence.PostLoad; import static org.assertj.core.api.Assertions.assertThat; @JiraKey("HHH-17019") -@RunWith(BytecodeEnhancerRunner.class) -public class PostLoadLazyListenerTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + PostLoadLazyListenerTest.Person.class, PostLoadLazyListenerTest.Tag.class + } +) +@SessionFactory +@BytecodeEnhanced +public class PostLoadLazyListenerTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class, Tag.class }; - } - - @After - public void tearDown() { - inTransaction( session -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Tag" ).executeUpdate(); session.createQuery( "delete from Person" ).executeUpdate(); } @@ -45,8 +47,8 @@ public class PostLoadLazyListenerTest extends BaseCoreFunctionalTestCase { } @Test - public void smoke() { - inTransaction( + public void smoke(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1, "name" ); Tag tag = new Tag( 100, person ); @@ -57,7 +59,7 @@ public class PostLoadLazyListenerTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Tag tag = session.find( Tag.class, 100 ); assertThat( tag ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PreUpdateBytecodeEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PreUpdateBytecodeEnhancementTest.java index 85db0f02aa..b2b76531b4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PreUpdateBytecodeEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/callbacks/PreUpdateBytecodeEnhancementTest.java @@ -6,9 +6,11 @@ */ package org.hibernate.orm.test.bytecode.enhancement.callbacks; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.time.Instant; import java.util.List; -import java.util.Map; import jakarta.persistence.Basic; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; @@ -20,42 +22,44 @@ import jakarta.persistence.PrePersist; import jakarta.persistence.PreUpdate; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; @JiraKey("HHH-12718") -@RunWith(BytecodeEnhancerRunner.class) -public class PreUpdateBytecodeEnhancementTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class }; - } - - @Override - protected void addConfigOptions(Map options) { - options.put( AvailableSettings.CLASSLOADERS, getClass().getClassLoader() ); - options.put( AvailableSettings.ENHANCER_ENABLE_LAZY_INITIALIZATION, "true" ); - options.put( AvailableSettings.ENHANCER_ENABLE_DIRTY_TRACKING, "true" ); - } +@DomainModel( + annotatedClasses = { + PreUpdateBytecodeEnhancementTest.Person.class + } +) +@ServiceRegistry( + settings = { + // TODO: how to convert this, or even is it needed? + // options.put( AvailableSettings.CLASSLOADERS, getClass().getClassLoader() ); + @Setting( name = AvailableSettings.ENHANCER_ENABLE_LAZY_INITIALIZATION, value = "true" ), + @Setting( name = AvailableSettings.ENHANCER_ENABLE_DIRTY_TRACKING, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class PreUpdateBytecodeEnhancementTest { @Test - public void testPreUpdateModifications() { + public void testPreUpdateModifications(SessionFactoryScope scope) { Person person = new Person(); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { entityManager.persist( person ); } ); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { Person p = entityManager.find( Person.class, person.id ); assertNotNull( p ); assertNotNull( p.createdAt ); @@ -64,14 +68,14 @@ public class PreUpdateBytecodeEnhancementTest extends BaseEntityManagerFunctiona p.setName( "Changed Name" ); } ); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { Person p = entityManager.find( Person.class, person.id ); assertNotNull( p.lastUpdatedAt ); } ); } @Entity(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue private int id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionTest.java index 4594db477e..46a8dfdbef 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionTest.java @@ -6,12 +6,18 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.CascadeType; @@ -25,35 +31,30 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10252" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10252" ) +@DomainModel( + annotatedClasses = { + CascadeDeleteCollectionTest.Parent.class, CascadeDeleteCollectionTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CascadeDeleteCollectionTest { private Parent originalParent; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create a Parent with one Child - originalParent = doInHibernate( this::sessionFactory, s -> { + originalParent = scope.fromTransaction( s -> { Parent p = new Parent(); p.setName( "PARENT" ); p.setLazy( "LAZY" ); @@ -65,13 +66,13 @@ public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { } @Test - public void testManagedWithUninitializedAssociation() { + public void testManagedWithUninitializedAssociation(SessionFactoryScope scope) { // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) .setParameter( "name", "PARENT" ) .uniqueResult(); - checkInterceptor( loadedParent, false ); + checkInterceptor( scope, loadedParent, false ); assertFalse( Hibernate.isInitialized( loadedParent.getChildren() ) ); s.delete( loadedParent ); } ); @@ -79,14 +80,14 @@ public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testManagedWithInitializedAssociation() { + @JiraKey("HHH-13129") + public void testManagedWithInitializedAssociation(SessionFactoryScope scope) { // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) .setParameter( "name", "PARENT" ) .uniqueResult(); - checkInterceptor( loadedParent, false ); + checkInterceptor( scope, loadedParent, false ); loadedParent.getChildren().size(); assertTrue( Hibernate.isInitialized( loadedParent.getChildren() ) ); s.delete( loadedParent ); @@ -95,27 +96,27 @@ public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedWithUninitializedAssociation() { - final Parent detachedParent = doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-13129") + public void testDetachedWithUninitializedAssociation(SessionFactoryScope scope) { + final Parent detachedParent = scope.fromTransaction( s -> { return s.get( Parent.class, originalParent.getId() ); } ); assertFalse( Hibernate.isInitialized( detachedParent.getChildren() ) ); - checkInterceptor( detachedParent, false ); + checkInterceptor( scope, detachedParent, false ); // Delete the detached Parent with uninitialized children - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( detachedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedWithInitializedAssociation() { - final Parent detachedParent = doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-13129") + public void testDetachedWithInitializedAssociation(SessionFactoryScope scope) { + final Parent detachedParent = scope.fromTransaction( s -> { Parent parent = s.get( Parent.class, originalParent.getId() ); // initialize collection before detaching parent.getChildren().size(); @@ -124,33 +125,33 @@ public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { assertTrue( Hibernate.isInitialized( detachedParent.getChildren() ) ); - checkInterceptor( detachedParent, false ); + checkInterceptor( scope, detachedParent, false ); // Delete the detached Parent with initialized children - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( detachedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedOriginal() { + @JiraKey("HHH-13129") + public void testDetachedOriginal(SessionFactoryScope scope) { // originalParent#children should be initialized assertTrue( Hibernate.isPropertyInitialized( originalParent, "children" ) ); - checkInterceptor( originalParent, true ); + checkInterceptor( scope, originalParent, true ); // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( originalParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } - private void checkInterceptor(Parent parent, boolean isNullExpected) { - final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = sessionFactory().getRuntimeMetamodels() + private void checkInterceptor(SessionFactoryScope scope, Parent parent, boolean isNullExpected) { + final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Parent.class ) .getBytecodeEnhancementMetadata(); @@ -223,7 +224,7 @@ public class CascadeDeleteCollectionTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.java index 019d6a9efb..c8701fb41c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -17,18 +16,15 @@ import java.util.Collections; import java.util.List; import org.hibernate.Hibernate; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.CascadeType; @@ -50,37 +46,25 @@ import jakarta.persistence.Table; * * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10252" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10252" ) +@DomainModel( + annotatedClasses = { + CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.Parent.class, CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest.Child.class + } +) +@SessionFactory( + // We want to test with this setting set to false explicitly, + // because another test already takes care of the default. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest { private Parent originalParent; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create a Parent with one Child - originalParent = doInHibernate( this::sessionFactory, s -> { + originalParent = scope.fromTransaction( s -> { Parent p = new Parent(); p.setName( "PARENT" ); p.setLazy( "LAZY" ); @@ -92,13 +76,13 @@ public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest e } @Test - public void testManagedWithUninitializedAssociation() { + public void testManagedWithUninitializedAssociation(SessionFactoryScope scope) { // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) .setParameter( "name", "PARENT" ) .uniqueResult(); - checkInterceptor( loadedParent, false ); + checkInterceptor( scope, loadedParent, false ); assertFalse( Hibernate.isPropertyInitialized( loadedParent, "children" ) ); s.delete( loadedParent ); } ); @@ -106,14 +90,14 @@ public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest e } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testManagedWithInitializedAssociation() { + @JiraKey("HHH-13129") + public void testManagedWithInitializedAssociation(SessionFactoryScope scope) { // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) .setParameter( "name", "PARENT" ) .uniqueResult(); - checkInterceptor( loadedParent, false ); + checkInterceptor( scope, loadedParent, false ); loadedParent.getChildren(); assertTrue( Hibernate.isPropertyInitialized( loadedParent, "children" ) ); s.delete( loadedParent ); @@ -122,27 +106,27 @@ public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest e } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedWithUninitializedAssociation() { - final Parent detachedParent = doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-13129") + public void testDetachedWithUninitializedAssociation(SessionFactoryScope scope) { + final Parent detachedParent = scope.fromTransaction( s -> { return s.get( Parent.class, originalParent.getId() ); } ); assertFalse( Hibernate.isPropertyInitialized( detachedParent, "children" ) ); - checkInterceptor( detachedParent, false ); + checkInterceptor( scope, detachedParent, false ); // Delete the detached Parent with uninitialized children - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( detachedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedWithInitializedAssociation() { - final Parent detachedParent = doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-13129") + public void testDetachedWithInitializedAssociation(SessionFactoryScope scope) { + final Parent detachedParent = scope.fromTransaction( s -> { Parent parent = s.get( Parent.class, originalParent.getId() ); assertFalse( Hibernate.isPropertyInitialized( parent, "children" ) ); @@ -153,33 +137,33 @@ public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest e assertTrue( Hibernate.isPropertyInitialized( detachedParent, "children" ) ); - checkInterceptor( detachedParent, false ); + checkInterceptor( scope, detachedParent, false ); // Delete the detached Parent with initialized children - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( detachedParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } @Test - @TestForIssue(jiraKey = "HHH-13129") - public void testDetachedOriginal() { + @JiraKey("HHH-13129") + public void testDetachedOriginal(SessionFactoryScope scope) { // originalParent#children should be initialized assertTrue( Hibernate.isPropertyInitialized( originalParent, "children" ) ); - checkInterceptor( originalParent, true ); + checkInterceptor( scope, originalParent, true ); // Delete the Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( originalParent ); } ); // If the lazy relation is not fetch on cascade there is a constraint violation on commit } - private void checkInterceptor(Parent parent, boolean isNullExpected) { - final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = sessionFactory().getRuntimeMetamodels() + private void checkInterceptor(SessionFactoryScope scope, Parent parent, boolean isNullExpected) { + final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Parent.class ) .getBytecodeEnhancementMetadata(); @@ -252,7 +236,7 @@ public class CascadeDeleteCollectionWithCollectionInDefaultFetchGroupFalseTest e @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteManyToOneTest.java index 9f106443df..8212bd10b8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteManyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDeleteManyToOneTest.java @@ -21,52 +21,42 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.Configuration; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.testing.jdbc.SQLStatementInspector.extractFromSession; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Luis Barreiro */ -@TestForIssue(jiraKey = "HHH-10252") -@RunWith(BytecodeEnhancerRunner.class) -public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { - private SQLStatementInterceptor sqlInterceptor; +@JiraKey("HHH-10252") +@DomainModel( + annotatedClasses = { + CascadeDeleteManyToOneTest.Parent.class, CascadeDeleteManyToOneTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CascadeDeleteManyToOneTest { private Child originalChild; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - sqlInterceptor = new SQLStatementInterceptor( configuration ); - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create a Parent with one Child - originalChild = doInHibernate( - this::sessionFactory, s -> { + originalChild = scope.fromTransaction( s -> { Child c = new Child(); c.setName( "CHILD" ); c.setLazy( "LAZY" ); @@ -78,21 +68,22 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { } @Test - public void testManagedWithInitializedAssociation() { - sqlInterceptor.clear(); - + public void testManagedWithInitializedAssociation(SessionFactoryScope scope) { // Delete the Child - inTransaction( + scope.inTransaction( (s) -> { + final SQLStatementInspector statementInspector = extractFromSession( s ); + statementInspector.clear(); + final Child managedChild = (Child) s.createQuery( "SELECT c FROM Child c WHERE name=:name" ) .setParameter( "name", "CHILD" ) .uniqueResult(); - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + statementInspector.assertExecutedCount( 1 ); // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( managedChild, "parent" ) ); - assertThat( managedChild.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( managedChild.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( managedChild.getParent() ) ); s.delete( managedChild ); @@ -100,8 +91,7 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { ); // Explicitly check that both got deleted - doInHibernate( - this::sessionFactory, s -> { + scope.inTransaction( s -> { assertNull( s.createQuery( "FROM Child c" ).uniqueResult() ); assertNull( s.createQuery( "FROM Parent p" ).uniqueResult() ); } @@ -109,18 +99,18 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { } @Test - public void testDetachedWithInitializedAssociation() { - sqlInterceptor.clear(); - - final Child detachedChild = fromTransaction( + public void testDetachedWithInitializedAssociation(SessionFactoryScope scope) { + final Child detachedChild = scope.fromTransaction( (s) -> { + final SQLStatementInspector statementInspector = extractFromSession( s ); + statementInspector.clear(); Child child = s.get( Child.class, originalChild.getId() ); - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + statementInspector.assertExecutedCount( 1 ); // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( child, "parent" ) ); - assertThat( child.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( child.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( child.getParent() ) ); return child; @@ -129,15 +119,15 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { assertTrue( Hibernate.isPropertyInitialized( detachedChild, "parent" ) ); - checkInterceptor( detachedChild, false ); + checkInterceptor( scope, detachedChild, false ); // Delete the detached Child with initialized parent - inTransaction( + scope.inTransaction( (s) -> s.delete( detachedChild ) ); // Explicitly check that both got deleted - inTransaction( + scope.inTransaction( (s) -> { assertNull( s.createQuery( "FROM Child c" ).uniqueResult() ); assertNull( s.createQuery( "FROM Parent p" ).uniqueResult() ); @@ -146,30 +136,28 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { } @Test - public void testDetachedOriginal() { + public void testDetachedOriginal(SessionFactoryScope scope) { // originalChild#parent should be initialized assertTrue( Hibernate.isPropertyInitialized( originalChild, "parent" ) ); - checkInterceptor( originalChild, true ); + checkInterceptor( scope, originalChild, true ); // Delete the Child - doInHibernate( - this::sessionFactory, s -> { + scope.inTransaction( s -> { s.delete( originalChild ); } ); // Explicitly check that both got deleted - doInHibernate( - this::sessionFactory, s -> { + scope.inTransaction( s -> { assertNull( s.createQuery( "FROM Child c" ).uniqueResult() ); assertNull( s.createQuery( "FROM Parent p" ).uniqueResult() ); } ); } - private void checkInterceptor(Child child, boolean isNullExpected) { - final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = sessionFactory() + private void checkInterceptor(SessionFactoryScope scope, Child child, boolean isNullExpected) { + final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = scope.getSessionFactory() .getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Child.class ) @@ -206,7 +194,7 @@ public class CascadeDeleteManyToOneTest extends BaseCoreFunctionalTestCase { @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDetachedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDetachedTest.java index 49ec8d54be..4f7c1ddd6a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDetachedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeDetachedTest.java @@ -6,11 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.CascadeType; @@ -26,32 +27,31 @@ import jakarta.persistence.Table; import java.util.ArrayList; import java.util.List; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10254" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class CascadeDetachedTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{Author.class, Book.class}; - } +@JiraKey( "HHH-10254" ) +@DomainModel( + annotatedClasses = { + CascadeDetachedTest.Author.class, CascadeDetachedTest.Book.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CascadeDetachedTest { @Test - public void test() { + public void test(SessionFactoryScope scope) { Book book = new Book( "978-1118063330", "Operating System Concepts 9th Edition" ); book.addAuthor( new Author( "Abraham", "Silberschatz", new char[] { 'a', 'b' } ) ); book.addAuthor( new Author( "Peter", "Galvin", new char[] { 'c', 'd' } ) ); book.addAuthor( new Author( "Greg", "Gagne", new char[] { 'e', 'f' } ) ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { em.persist( book ); } ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { em.merge( book ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedTest.java index 79ad055b42..6ffdc7a6a2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedTest.java @@ -1,7 +1,6 @@ package org.hibernate.orm.test.bytecode.enhancement.cascade; import java.util.HashSet; -import java.util.Map; import java.util.Set; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -20,120 +19,133 @@ import org.hibernate.annotations.LazyToOneOption; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; -import static junit.framework.TestCase.assertEquals; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.testing.jdbc.SQLStatementInspector.extractFromSession; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Bolek Ziobrowski * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-13129") -public class CascadeOnUninitializedTest extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlInterceptor; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class, Address.class }; - } - - @Override - protected void addSettings(Map settings) { - super.addSettings( settings ); - settings.put( AvailableSettings.FORMAT_SQL, "true" ); - sqlInterceptor = new SQLStatementInterceptor( settings ); - } +@JiraKey("HHH-13129") +@DomainModel( + annotatedClasses = { + CascadeOnUninitializedTest.Person.class, CascadeOnUninitializedTest.Address.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class CascadeOnUninitializedTest { @Test - public void testMergeDetachedEnhancedEntityWithUninitializedManyToOne() { - final Person person = persistPersonWithManyToOne(); - - sqlInterceptor.clear(); + public void testMergeDetachedEnhancedEntityWithUninitializedManyToOne(SessionFactoryScope scope) { + final Person person = persistPersonWithManyToOne(scope); // get a detached Person - final Person detachedPerson = fromTransaction( - session -> session.get( Person.class, person.getId() ) + final Person detachedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + Person p = session.get( Person.class, person.getId() ); + // loading Person should lead to one SQL + statementInspector.assertExecutedCount( 1 ); + return p; + } ); - // loading Person should lead to one SQL - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); - // primaryAddress should be "initialized" as an enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson.getPrimaryAddress(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson.getPrimaryAddress() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); // alter the detached reference detachedPerson.setName( "newName" ); - final Person mergedPerson = fromTransaction( - session -> (Person) session.merge( detachedPerson ) + final Person mergedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + Person merge = session.merge( detachedPerson ); + + // 1) select Person#addresses + // 2) select Person#primaryAddress + // 3) update Person + session.flush(); + statementInspector.assertExecutedCount( 2 ); + return merge; + } ); - // 1) select Person#addresses - // 2) select Person#primaryAddress - // 3) update Person - - assertThat( sqlInterceptor.getQueryCount(), is( 3 ) ); - // primaryAddress should not be initialized assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson.getPrimaryAddress(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson.getPrimaryAddress() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); assertEquals( "newName", mergedPerson.getName() ); } @Test - public void testDeleteEnhancedEntityWithUninitializedManyToOne() { - Person person = persistPersonWithManyToOne(); - - sqlInterceptor.clear(); + public void testDeleteEnhancedEntityWithUninitializedManyToOne(SessionFactoryScope scope) { + Person person = persistPersonWithManyToOne(scope); // get a detached Person - Person detachedPerson = fromTransaction( - session -> session.get( Person.class, person.getId() ) - ); + Person detachedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); - // loading Person should lead to one SQL - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + Person p = session.get( Person.class, person.getId() ); + + // loading Person should lead to one SQL + statementInspector.assertExecutedCount( 1 ); + + return p; + } + ); // primaryAddress should be initialized as an enhance-proxy assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson, not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); - sqlInterceptor.clear(); - // deleting detachedPerson should result in detachedPerson.primaryAddress being initialized, // so that the DELETE operation can be cascaded to it. - inTransaction( - session -> session.delete( detachedPerson ) + scope.inTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + + session.delete( detachedPerson ); + + // 1) select Person#addresses + // 2) select Person#primaryAddress + // 3) delete Person + // 4) select primary Address + session.flush(); + statementInspector.assertExecutedCount( 4 ); + } ); - // 1) select Person#addresses - // 2) select Person#primaryAddress - // 3) delete Person - // 4) select primary Address - - assertThat( sqlInterceptor.getQueryCount(), is( 4 ) ); - // both the Person and its Address should be deleted - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Person.class, person.getId() ) ); assertNull( session.get( Person.class, person.getPrimaryAddress().getId() ) ); @@ -142,26 +154,18 @@ public class CascadeOnUninitializedTest extends BaseNonConfigCoreFunctionalTestC } @Test - public void testMergeDetachedEnhancedEntityWithUninitializedOneToMany() { + public void testMergeDetachedEnhancedEntityWithUninitializedOneToMany(SessionFactoryScope scope) { - Person person = persistPersonWithOneToMany(); + Person person = persistPersonWithOneToMany( scope ); // get a detached Person - Person detachedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - return session.get( Person.class, person.getId() ); - } - ); + Person detachedPerson = scope.fromTransaction( session -> session.get( Person.class, person.getId() ) ); // address should not be initialized in order to reproduce the problem assertFalse( Hibernate.isInitialized( detachedPerson.getAddresses() ) ); detachedPerson.setName( "newName" ); - Person mergedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - return (Person) session.merge( detachedPerson ); - } - ); + Person mergedPerson = scope.fromTransaction( session -> session.merge( detachedPerson ) ); // address still shouldn't be initialized: there's no reason for it to be initialized by a merge. assertFalse( Hibernate.isInitialized( detachedPerson.getAddresses() ) ); @@ -169,37 +173,28 @@ public class CascadeOnUninitializedTest extends BaseNonConfigCoreFunctionalTestC } @Test - public void testDeleteEnhancedEntityWithUninitializedOneToMany() { - Person person = persistPersonWithOneToMany(); + public void testDeleteEnhancedEntityWithUninitializedOneToMany(SessionFactoryScope scope) { + Person person = persistPersonWithOneToMany( scope ); // get a detached Person - Person detachedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - return session.get( Person.class, person.getId() ); - } - ); + Person detachedPerson = scope.fromTransaction( session -> session.get( Person.class, person.getId() ) ); // address should not be initialized in order to reproduce the problem assertFalse( Hibernate.isInitialized( detachedPerson.getAddresses() ) ); // deleting detachedPerson should result in detachedPerson.address being initialized, // so that the DELETE operation can be cascaded to it. - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.delete( detachedPerson ); - } - ); + scope.inTransaction( session -> session.delete( detachedPerson ) ); // both the Person and its Address should be deleted - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { assertNull( session.get( Person.class, person.getId() ) ); assertNull( session.get( Person.class, person.getAddresses().iterator().next().getId() ) ); } ); } - public Person persistPersonWithManyToOne() { + public Person persistPersonWithManyToOne(SessionFactoryScope scope) { Address address = new Address(); address.setDescription( "ABC" ); @@ -207,16 +202,12 @@ public class CascadeOnUninitializedTest extends BaseNonConfigCoreFunctionalTestC person.setName( "John Doe" ); person.setPrimaryAddress( address ); - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.persist( person ); - } - ); + scope.inTransaction( session -> session.persist( person ) ); return person; } - public Person persistPersonWithOneToMany() { + public Person persistPersonWithOneToMany(SessionFactoryScope scope) { Address address = new Address(); address.setDescription( "ABC" ); @@ -224,11 +215,7 @@ public class CascadeOnUninitializedTest extends BaseNonConfigCoreFunctionalTestC person.setName( "John Doe" ); person.getAddresses().add( address ); - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.persist( person ); - } - ); + scope.inTransaction( session -> session.persist( person ) ); return person; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.java index 7de4299175..1393bb47a0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.java @@ -1,35 +1,31 @@ package org.hibernate.orm.test.bytecode.enhancement.cascade; -import static junit.framework.TestCase.assertEquals; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.testing.jdbc.SQLStatementInspector.extractFromSession; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; -import java.util.Map; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -51,116 +47,117 @@ import jakarta.persistence.Table; * @author Bolek Ziobrowski * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-13129") -public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlInterceptor; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class, Address.class }; - } - - @Override - protected void addSettings(Map settings) { - super.addSettings( settings ); - settings.put( AvailableSettings.FORMAT_SQL, "true" ); - sqlInterceptor = new SQLStatementInterceptor( settings ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } +@JiraKey("HHH-13129") +@DomainModel( + annotatedClasses = { + CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.Person.class, CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest.Address.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "true" ), + } +) +@SessionFactory( + // We want to test with this setting set to false explicitly, + // because another test already takes care of the default. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest { @Test - public void testMergeDetachedEnhancedEntityWithUninitializedManyToOne() { - final Person person = persistPersonWithManyToOne(); - - sqlInterceptor.clear(); + public void testMergeDetachedEnhancedEntityWithUninitializedManyToOne(SessionFactoryScope scope) { + final Person person = persistPersonWithManyToOne(scope); // get a detached Person - final Person detachedPerson = fromTransaction( - session -> session.get( Person.class, person.getId() ) - ); + final Person detachedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); - // loading Person should lead to one SQL - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + Person p = session.get( Person.class, person.getId() ); + + // loading Person should lead to one SQL + statementInspector.assertExecutedCount( 1 ); + return p; + } + ); // primaryAddress should be "initialized" as an enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson.getPrimaryAddress(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson.getPrimaryAddress() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); // alter the detached reference detachedPerson.setName( "newName" ); - final Person mergedPerson = fromTransaction( - session -> (Person) session.merge( detachedPerson ) + final Person mergedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + Person merged = session.merge( detachedPerson ); + + // 1) select Person#addresses + // 2) select Person#primaryAddress + // 3) update Person + session.flush(); + statementInspector.assertExecutedCount( 2 ); + return merged; + } ); - // 1) select Person#addresses - // 2) select Person#primaryAddress - // 3) update Person - - assertThat( sqlInterceptor.getQueryCount(), is( 3 ) ); - // primaryAddress should not be initialized assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson.getPrimaryAddress(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson.getPrimaryAddress() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); assertEquals( "newName", mergedPerson.getName() ); } @Test - public void testDeleteEnhancedEntityWithUninitializedManyToOne() { - Person person = persistPersonWithManyToOne(); - - sqlInterceptor.clear(); + public void testDeleteEnhancedEntityWithUninitializedManyToOne(SessionFactoryScope scope) { + Person person = persistPersonWithManyToOne(scope); // get a detached Person - Person detachedPerson = fromTransaction( - session -> session.get( Person.class, person.getId() ) - ); + Person detachedPerson = scope.fromTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + Person p = session.get( Person.class, person.getId() ); - // loading Person should lead to one SQL - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + // loading Person should lead to one SQL + statementInspector.assertExecutedCount( 1 ); + + return p; + } + ); // primaryAddress should be initialized as an enhance-proxy assertTrue( Hibernate.isPropertyInitialized( detachedPerson, "primaryAddress" ) ); - assertThat( detachedPerson, not( instanceOf( HibernateProxy.class ) ) ); + assertThat( detachedPerson ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( detachedPerson.getPrimaryAddress() ) ); - sqlInterceptor.clear(); - // deleting detachedPerson should result in detachedPerson.primaryAddress being initialized, // so that the DELETE operation can be cascaded to it. - inTransaction( - session -> session.delete( detachedPerson ) + scope.inTransaction( + session -> { + final SQLStatementInspector statementInspector = extractFromSession( session ); + statementInspector.clear(); + + session.delete( detachedPerson ); + + // 1) select Person#addresses + // 2) select Person#primaryAddress + // 3) delete Person + // 4) select primary Address + session.flush(); + statementInspector.assertExecutedCount( 4 ); + } ); - // 1) select Person#addresses - // 2) select Person#primaryAddress - // 3) delete Person - // 4) select primary Address - - assertThat( sqlInterceptor.getQueryCount(), is( 4 ) ); - // both the Person and its Address should be deleted - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Person.class, person.getId() ) ); assertNull( session.get( Person.class, person.getPrimaryAddress().getId() ) ); @@ -169,26 +166,18 @@ public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest ex } @Test - public void testMergeDetachedEnhancedEntityWithUninitializedOneToMany() { + public void testMergeDetachedEnhancedEntityWithUninitializedOneToMany(SessionFactoryScope scope) { - Person person = persistPersonWithOneToMany(); + Person person = persistPersonWithOneToMany(scope); // get a detached Person - Person detachedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - return session.get( Person.class, person.getId() ); - } - ); + Person detachedPerson = scope.fromTransaction( session -> session.get( Person.class, person.getId() ) ); // address should not be initialized assertFalse( Hibernate.isPropertyInitialized( detachedPerson, "addresses" ) ); detachedPerson.setName( "newName" ); - Person mergedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - return (Person) session.merge( detachedPerson ); - } - ); + Person mergedPerson = scope.fromTransaction( session -> session.merge( detachedPerson ) ); // address should be initialized assertTrue( Hibernate.isPropertyInitialized( mergedPerson, "addresses" ) ); @@ -196,12 +185,11 @@ public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest ex } @Test - public void testDeleteEnhancedEntityWithUninitializedOneToMany() { - Person person = persistPersonWithOneToMany(); + public void testDeleteEnhancedEntityWithUninitializedOneToMany(SessionFactoryScope scope) { + Person person = persistPersonWithOneToMany(scope); // get a detached Person - Person detachedPerson = TransactionUtil.doInHibernate( - this::sessionFactory, session -> { + Person detachedPerson = scope.fromTransaction( session -> { return session.get( Person.class, person.getId() ); } ); @@ -211,22 +199,17 @@ public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest ex // deleting detachedPerson should result in detachedPerson.address being initialized, // so that the DELETE operation can be cascaded to it. - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.delete( detachedPerson ); - } - ); + scope.inTransaction( session -> session.delete( detachedPerson ) ); // both the Person and its Address should be deleted - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { assertNull( session.get( Person.class, person.getId() ) ); assertNull( session.get( Person.class, person.getAddresses().iterator().next().getId() ) ); } ); } - public Person persistPersonWithManyToOne() { + public Person persistPersonWithManyToOne(SessionFactoryScope scope) { Address address = new Address(); address.setDescription( "ABC" ); @@ -234,16 +217,12 @@ public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest ex person.setName( "John Doe" ); person.setPrimaryAddress( address ); - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.persist( person ); - } - ); + scope.inTransaction( session -> session.persist( person ) ); return person; } - public Person persistPersonWithOneToMany() { + public Person persistPersonWithOneToMany(SessionFactoryScope scope) { Address address = new Address(); address.setDescription( "ABC" ); @@ -251,11 +230,7 @@ public class CascadeOnUninitializedWithCollectionInDefaultFetchGroupFalseTest ex person.setName( "John Doe" ); person.getAddresses().add( address ); - TransactionUtil.doInHibernate( - this::sessionFactory, session -> { - session.persist( person ); - } - ); + scope.inTransaction( session -> session.persist( person ) ); return person; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeWithFkConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeWithFkConstraintTest.java index 73381c8918..41856e574a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeWithFkConstraintTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/CascadeWithFkConstraintTest.java @@ -6,13 +6,13 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -22,28 +22,31 @@ import jakarta.persistence.Table; import java.util.HashSet; import java.util.Set; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10252" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class CascadeWithFkConstraintTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10252" ) +@DomainModel( + annotatedClasses = { + CascadeWithFkConstraintTest.Garage.class, CascadeWithFkConstraintTest.Car.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CascadeWithFkConstraintTest { private String garageId, car1Id, car2Id; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Garage.class, Car.class}; - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create garage, add 2 cars to garage - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { Garage garage = new Garage(); Car car1 = new Car(); @@ -62,23 +65,23 @@ public class CascadeWithFkConstraintTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { + public void test(SessionFactoryScope scope) { // Remove garage - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { Garage toRemoveGarage = em.find( Garage.class, garageId ); em.remove( toRemoveGarage ); } ); // Check if there is no garage but cars are still present - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { Garage foundGarage = em.find( Garage.class, garageId ); - Assert.assertNull( foundGarage ); + assertNull( foundGarage ); Car foundCar1 = em.find( Car.class, car1Id ); - Assert.assertEquals( car1Id, foundCar1.id ); + assertEquals( car1Id, foundCar1.id ); Car foundCar2 = em.find( Car.class, car2Id ); - Assert.assertEquals( car2Id, foundCar2.id ); + assertEquals( car2Id, foundCar2.id ); } ); } @@ -86,7 +89,7 @@ public class CascadeWithFkConstraintTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "GARAGE" ) - private static class Garage { + static class Garage { @Id String id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/AbstractMultiPathCircleCascadeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/AbstractMultiPathCircleCascadeTest.java index e39be583c9..c27fa9e102 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/AbstractMultiPathCircleCascadeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/AbstractMultiPathCircleCascadeTest.java @@ -17,9 +17,10 @@ import org.hibernate.orm.test.cascade.circle.Route; import org.hibernate.orm.test.cascade.circle.Tour; import org.hibernate.orm.test.cascade.circle.Transport; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import jakarta.persistence.PersistenceException; @@ -33,7 +34,7 @@ import static org.junit.Assert.fail; /** * @author Andrea Boriero */ -public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctionalTestCase { +public abstract class AbstractMultiPathCircleCascadeTest { private interface EntityOperation { boolean isLegacy(); @@ -80,23 +81,23 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio }; @Test - public void testMergeEntityWithNonNullableTransientEntity() { - testEntityWithNonNullableTransientEntity( MERGE_OPERATION ); + public void testMergeEntityWithNonNullableTransientEntity(SessionFactoryScope scope) { + testEntityWithNonNullableTransientEntity( scope, MERGE_OPERATION ); } @Test - public void testSaveEntityWithNonNullableTransientEntity() { - testEntityWithNonNullableTransientEntity( SAVE_OPERATION ); + public void testSaveEntityWithNonNullableTransientEntity(SessionFactoryScope scope) { + testEntityWithNonNullableTransientEntity( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateEntityWithNonNullableTransientEntity() { - testEntityWithNonNullableTransientEntity( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateEntityWithNonNullableTransientEntity(SessionFactoryScope scope) { + testEntityWithNonNullableTransientEntity( scope, SAVE_UPDATE_OPERATION ); } - private void testEntityWithNonNullableTransientEntity( EntityOperation operation) { + private void testEntityWithNonNullableTransientEntity(SessionFactoryScope scope, EntityOperation operation) { - Route route = getUpdatedDetachedEntity(); + Route route = getUpdatedDetachedEntity( scope ); Node node = (Node) route.getNodes().iterator().next(); route.getNodes().remove( node ); @@ -106,7 +107,7 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio routeNew.getNodes().add( node ); node.setRoute( routeNew ); - inSession( + scope.inSession( session -> { session.beginTransaction(); try { @@ -131,28 +132,28 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeEntityWithNonNullableEntityNull() { - testEntityWithNonNullableEntityNull( MERGE_OPERATION ); + public void testMergeEntityWithNonNullableEntityNull(SessionFactoryScope scope) { + testEntityWithNonNullableEntityNull( scope, MERGE_OPERATION ); } @Test - public void testSaveEntityWithNonNullableEntityNull() { - testEntityWithNonNullableEntityNull( SAVE_OPERATION ); + public void testSaveEntityWithNonNullableEntityNull(SessionFactoryScope scope) { + testEntityWithNonNullableEntityNull( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateEntityWithNonNullableEntityNull() { - testEntityWithNonNullableEntityNull( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateEntityWithNonNullableEntityNull(SessionFactoryScope scope) { + testEntityWithNonNullableEntityNull( scope, SAVE_UPDATE_OPERATION ); } - private void testEntityWithNonNullableEntityNull( EntityOperation operation) { - Route route = getUpdatedDetachedEntity(); + private void testEntityWithNonNullableEntityNull(SessionFactoryScope scope, EntityOperation operation) { + Route route = getUpdatedDetachedEntity( scope ); Node node = (Node) route.getNodes().iterator().next(); route.getNodes().remove( node ); node.setRoute( null ); - inSession( + scope.inSession( session -> { session.beginTransaction(); try { @@ -176,26 +177,26 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeEntityWithNonNullablePropSetToNull() { - testEntityWithNonNullablePropSetToNull( MERGE_OPERATION ); + public void testMergeEntityWithNonNullablePropSetToNull(SessionFactoryScope scope) { + testEntityWithNonNullablePropSetToNull( scope, MERGE_OPERATION ); } @Test - public void testSaveEntityWithNonNullablePropSetToNull() { - testEntityWithNonNullablePropSetToNull( SAVE_OPERATION ); + public void testSaveEntityWithNonNullablePropSetToNull(SessionFactoryScope scope) { + testEntityWithNonNullablePropSetToNull( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateEntityWithNonNullablePropSetToNull() { - testEntityWithNonNullablePropSetToNull( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateEntityWithNonNullablePropSetToNull(SessionFactoryScope scope) { + testEntityWithNonNullablePropSetToNull( scope, SAVE_UPDATE_OPERATION ); } - private void testEntityWithNonNullablePropSetToNull( EntityOperation operation) { - Route route = getUpdatedDetachedEntity(); + private void testEntityWithNonNullablePropSetToNull(SessionFactoryScope scope, EntityOperation operation) { + Route route = getUpdatedDetachedEntity( scope ); Node node = (Node) route.getNodes().iterator().next(); node.setName( null ); - inSession( + scope.inSession( session -> { session.beginTransaction(); @@ -221,31 +222,31 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeRoute() { - testRoute( MERGE_OPERATION ); + public void testMergeRoute(SessionFactoryScope scope) { + testRoute( MERGE_OPERATION, scope ); } // skip SAVE_OPERATION since Route is not transient @Test - public void testSaveUpdateRoute() { - testRoute( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateRoute(SessionFactoryScope scope) { + testRoute( SAVE_UPDATE_OPERATION, scope ); } - private void testRoute( EntityOperation operation) { + private void testRoute( EntityOperation operation, SessionFactoryScope scope) { - Route r = getUpdatedDetachedEntity(); + Route r = getUpdatedDetachedEntity( scope ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> operation.doEntityOperation( r, session ) ); - assertInsertCount( 4 ); - assertUpdateCount( 1 ); + assertInsertCount( scope, 4 ); + assertUpdateCount( scope, 1 ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); checkResults( route, true ); @@ -254,27 +255,27 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergePickupNode() { - testPickupNode( MERGE_OPERATION ); + public void testMergePickupNode(SessionFactoryScope scope) { + testPickupNode( scope, MERGE_OPERATION ); } @Test - public void testSavePickupNode() { - testPickupNode( SAVE_OPERATION ); + public void testSavePickupNode(SessionFactoryScope scope) { + testPickupNode( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdatePickupNode() { - testPickupNode( SAVE_UPDATE_OPERATION ); + public void testSaveUpdatePickupNode(SessionFactoryScope scope) { + testPickupNode( scope, SAVE_UPDATE_OPERATION ); } - private void testPickupNode( EntityOperation operation) { + private void testPickupNode(SessionFactoryScope scope, EntityOperation operation) { - Route r = getUpdatedDetachedEntity(); + Route r = getUpdatedDetachedEntity( scope ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> { Iterator it = r.getNodes().iterator(); Node node = (Node) it.next(); @@ -292,10 +293,10 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } ); - assertInsertCount( 4 ); - assertUpdateCount( 0 ); + assertInsertCount( scope, 4 ); + assertUpdateCount( scope, 0 ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); checkResults( route, false ); @@ -304,27 +305,27 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeDeliveryNode() { - testDeliveryNode( MERGE_OPERATION ); + public void testMergeDeliveryNode(SessionFactoryScope scope) { + testDeliveryNode( scope, MERGE_OPERATION ); } @Test - public void testSaveDeliveryNode() { - testDeliveryNode( SAVE_OPERATION ); + public void testSaveDeliveryNode(SessionFactoryScope scope) { + testDeliveryNode( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateDeliveryNode() { - testDeliveryNode( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateDeliveryNode(SessionFactoryScope scope) { + testDeliveryNode( scope, SAVE_UPDATE_OPERATION ); } - private void testDeliveryNode( EntityOperation operation) { + private void testDeliveryNode(SessionFactoryScope scope, EntityOperation operation) { - Route r = getUpdatedDetachedEntity(); + Route r = getUpdatedDetachedEntity( scope ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> { Iterator it = r.getNodes().iterator(); Node node = (Node) it.next(); @@ -343,10 +344,10 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio ); - assertInsertCount( 4 ); - assertUpdateCount( 0 ); + assertInsertCount( scope, 4 ); + assertUpdateCount( scope, 0 ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); checkResults( route, false ); @@ -355,35 +356,35 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeTour() { - testTour( MERGE_OPERATION ); + public void testMergeTour(SessionFactoryScope scope) { + testTour( scope, MERGE_OPERATION ); } @Test - public void testSaveTour() { - testTour( SAVE_OPERATION ); + public void testSaveTour(SessionFactoryScope scope) { + testTour( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateTour() { - testTour( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateTour(SessionFactoryScope scope) { + testTour( scope, SAVE_UPDATE_OPERATION ); } - private void testTour( EntityOperation operation) { + private void testTour(SessionFactoryScope scope, EntityOperation operation) { - Route r = getUpdatedDetachedEntity(); + Route r = getUpdatedDetachedEntity( scope ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> operation.doEntityOperation( ( (Node) r.getNodes().toArray()[0] ).getTour(), session ) ); - assertInsertCount( 4 ); - assertUpdateCount( 0 ); + assertInsertCount( scope, 4 ); + assertUpdateCount( scope, 0 ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); checkResults( route, false ); @@ -392,27 +393,27 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeTransport() { - testTransport( MERGE_OPERATION ); + public void testMergeTransport(SessionFactoryScope scope) { + testTransport( scope, MERGE_OPERATION ); } @Test - public void testSaveTransport() { - testTransport( SAVE_OPERATION ); + public void testSaveTransport(SessionFactoryScope scope) { + testTransport( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateTransport() { - testTransport( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateTransport(SessionFactoryScope scope) { + testTransport( scope, SAVE_UPDATE_OPERATION ); } - private void testTransport( EntityOperation operation) { + private void testTransport(SessionFactoryScope scope, EntityOperation operation) { - Route r = getUpdatedDetachedEntity(); + Route r = getUpdatedDetachedEntity( scope ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> { Transport transport; Node node = ( (Node) r.getNodes().toArray()[0] ); @@ -427,10 +428,10 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } ); - assertInsertCount( 4 ); - assertUpdateCount( 0 ); + assertInsertCount( scope, 4 ); + assertUpdateCount( scope, 0 ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); checkResults( route, false ); @@ -445,10 +446,10 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio return deliveryNode; } - private Route getUpdatedDetachedEntity() { + private Route getUpdatedDetachedEntity(SessionFactoryScope scope) { Route route = new Route(); - inTransaction( + scope.inTransaction( session -> { route.setName( "routeA" ); @@ -494,9 +495,9 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio return route; } - @After - public void cleanup() { - inTransaction( + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Transport" ); session.createQuery( "delete from Tour" ); @@ -563,24 +564,24 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } @Test - public void testMergeData3Nodes() { - testData3Nodes( MERGE_OPERATION ); + public void testMergeData3Nodes(SessionFactoryScope scope) { + testData3Nodes( scope, MERGE_OPERATION ); } @Test - public void testSaveData3Nodes() { - testData3Nodes( SAVE_OPERATION ); + public void testSaveData3Nodes(SessionFactoryScope scope) { + testData3Nodes( scope, SAVE_OPERATION ); } @Test - public void testSaveUpdateData3Nodes() { - testData3Nodes( SAVE_UPDATE_OPERATION ); + public void testSaveUpdateData3Nodes(SessionFactoryScope scope) { + testData3Nodes( scope, SAVE_UPDATE_OPERATION ); } - private void testData3Nodes( EntityOperation operation) { + private void testData3Nodes(SessionFactoryScope scope, EntityOperation operation) { Route r = new Route(); - inTransaction( + scope.inTransaction( session -> { r.setName( "routeA" ); @@ -588,9 +589,9 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } ); - clearCounts(); + clearCounts( scope ); - inTransaction( + scope.inTransaction( session -> { Route route = session.get( Route.class, r.getRouteID() ); route.setName( "new routA" ); @@ -651,8 +652,8 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } ); - assertInsertCount( 6 ); - assertUpdateCount( 1 ); + assertInsertCount( scope, 6 ); + assertUpdateCount( scope, 1 ); } protected void checkExceptionFromNullValueForNonNullable( @@ -668,7 +669,7 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } } else { - assertTrue( ( ex instanceof JDBCException ) || ( ex.getCause() instanceof JDBCException ) ); + Assertions.assertTrue( ( ex instanceof JDBCException ) || ( ex.getCause() instanceof JDBCException ) ); } } else { @@ -681,22 +682,22 @@ public abstract class AbstractMultiPathCircleCascadeTest extends BaseCoreFunctio } } - protected void clearCounts() { - sessionFactory().getStatistics().clear(); + protected void clearCounts(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); } - protected void assertInsertCount(int expected) { - int inserts = (int) sessionFactory().getStatistics().getEntityInsertCount(); - assertEquals( "unexpected insert count", expected, inserts ); + protected void assertInsertCount(SessionFactoryScope scope, int expected) { + int inserts = (int) scope.getSessionFactory().getStatistics().getEntityInsertCount(); + Assertions.assertEquals( expected, inserts, "unexpected insert count" ); } - protected void assertUpdateCount(int expected) { - int updates = (int) sessionFactory().getStatistics().getEntityUpdateCount(); - assertEquals( "unexpected update counts", expected, updates ); + protected void assertUpdateCount(SessionFactoryScope scope, int expected) { + int updates = (int) scope.getSessionFactory().getStatistics().getEntityUpdateCount(); + Assertions.assertEquals( expected, updates, "unexpected update counts" ); } - protected void assertDeleteCount(int expected) { - int deletes = (int) sessionFactory().getStatistics().getEntityDeleteCount(); - assertEquals( "unexpected delete counts", expected, deletes ); + protected void assertDeleteCount(SessionFactoryScope scope, int expected) { + int deletes = (int) scope.getSessionFactory().getStatistics().getEntityDeleteCount(); + Assertions.assertEquals( expected, deletes, "unexpected delete counts" ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullFalseDelayedInsertTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullFalseDelayedInsertTest.java index 35104fbc4e..e5702a6103 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullFalseDelayedInsertTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullFalseDelayedInsertTest.java @@ -6,32 +6,34 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + @Setting( name = AvailableSettings.CHECK_NULLABILITY, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeCheckNullFalseDelayedInsertTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - configuration.setProperty( Environment.CHECK_NULLABILITY, false ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullTrueDelayedInsertTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullTrueDelayedInsertTest.java index 8e994c4303..c4d97bdecc 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullTrueDelayedInsertTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullTrueDelayedInsertTest.java @@ -6,32 +6,35 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + @Setting( name = AvailableSettings.CHECK_NULLABILITY, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeCheckNullTrueDelayedInsertTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" - }; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - configuration.setProperty( Environment.CHECK_NULLABILITY, true ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityFalseTest.java index 4908532c7a..c924c02687 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityFalseTest.java @@ -6,33 +6,35 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + @Setting( name = AvailableSettings.CHECK_NULLABILITY, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeCheckNullibilityFalseTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" - }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - configuration.setProperty( Environment.CHECK_NULLABILITY, false ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityTrueTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityTrueTest.java index cb55545581..69dcf20bf0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityTrueTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeCheckNullibilityTrueTest.java @@ -6,32 +6,35 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + @Setting( name = AvailableSettings.CHECK_NULLABILITY, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeCheckNullibilityTrueTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" - }; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - configuration.setProperty( Environment.CHECK_NULLABILITY, true ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeDelayedInsertTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeDelayedInsertTest.java index 789b899e83..1ae009c042 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeDelayedInsertTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeDelayedInsertTest.java @@ -6,31 +6,34 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeDelayedInsertTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascadeDelayedInsert.hbm.xml" - }; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeTest.java index 1c9199562d..50168685d5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/cascade/circle/MultiPathCircleCascadeTest.java @@ -6,14 +6,16 @@ */ package org.hibernate.orm.test.bytecode.enhancement.cascade.circle; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.Setting; /** * The test case uses the following model: @@ -37,19 +39,20 @@ import org.junit.runner.RunWith; * * @author Pavol Zibrita, Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "0" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) public class MultiPathCircleCascadeTest extends AbstractMultiPathCircleCascadeTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[] { - "org/hibernate/orm/test/cascade/circle/MultiPathCircleCascade.hbm.xml" - }; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.GENERATE_STATISTICS, true ); - configuration.setProperty( Environment.STATEMENT_BATCH_SIZE, 0 ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/flush/ElementCollectionFlushAfterQueryTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/flush/ElementCollectionFlushAfterQueryTest.java index 284be4273c..99e4731edf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/flush/ElementCollectionFlushAfterQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/flush/ElementCollectionFlushAfterQueryTest.java @@ -3,11 +3,11 @@ package org.hibernate.orm.test.bytecode.enhancement.collectionelement.flush; import java.util.HashSet; import java.util.Set; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; @@ -16,24 +16,26 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16337") -public class ElementCollectionFlushAfterQueryTest extends BaseCoreFunctionalTestCase { +import org.junit.jupiter.api.Test; + + +@DomainModel( + annotatedClasses = { + ElementCollectionFlushAfterQueryTest.MyEntity.class, + ElementCollectionFlushAfterQueryTest.MyOtherEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16337") +public class ElementCollectionFlushAfterQueryTest { private static final Long MY_ENTITY_ID = 1l; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyEntity.class, - MyOtherEntity.class - }; - } - @Test - public void testAutoFlush() { - inTransaction( + public void testAutoFlush(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity myEntity = new MyEntity( MY_ENTITY_ID, "my entity" ); myEntity.addRedirectUris( "1" ); @@ -41,7 +43,7 @@ public class ElementCollectionFlushAfterQueryTest extends BaseCoreFunctionalTest } ); - inTransaction( + scope.inTransaction( session -> { MyEntity myEntity = session.find( MyEntity.class, MY_ENTITY_ID ); @@ -53,7 +55,7 @@ public class ElementCollectionFlushAfterQueryTest extends BaseCoreFunctionalTest } ); - inTransaction( + scope.inTransaction( session -> { MyEntity myEntity = session.find( MyEntity.class, MY_ENTITY_ID ); Set redirectUris = myEntity.getRedirectUris(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java index 553d0078ef..80dd33641c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.java @@ -6,82 +6,74 @@ */ package org.hibernate.orm.test.bytecode.enhancement.collectionelement.recreate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.bytecode.enhancement.collectionelement.recreate.BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest.MyEntity; +import static org.junit.Assert.assertTrue; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OrderColumn; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertTrue; - -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MyEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced(testEnhancedClasses = MyEntity.class) @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-14387") -public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest - extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14387") +public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGroupTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyEntity.class - }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @Before - public void check() { - inSession( + @BeforeEach + public void check(SessionFactoryScope scope) { + scope.inSession( session -> assertTrue( session.getSessionFactory().getSessionFactoryOptions() - .isCollectionsInDefaultFetchGroupEnabled() ) + .isCollectionsInDefaultFetchGroupEnabled() ) ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "delete from myentity" ).executeUpdate() ); } @Test - public void testRecreateCollection() { - inTransaction( session -> { + public void testRecreateCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -89,21 +81,21 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testRecreateCollectionFind() { - inTransaction( session -> { + public void testRecreateCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -111,20 +103,20 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testDeleteCollection() { - inTransaction( session -> { + public void testDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -132,20 +124,20 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testDeleteCollectionFind() { - inTransaction( session -> { + public void testDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -153,19 +145,19 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollection() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.get( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.get( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); @@ -174,19 +166,19 @@ public class BytecodeEnhancementElementCollectionRecreateCollectionsInDefaultGro } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.find( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.find( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java index 13da5e2f13..dabec704ab 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/collectionelement/recreate/BytecodeEnhancementElementCollectionRecreateTest.java @@ -6,80 +6,74 @@ */ package org.hibernate.orm.test.bytecode.enhancement.collectionelement.recreate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.bytecode.enhancement.collectionelement.recreate.BytecodeEnhancementElementCollectionRecreateTest.MyEntity; +import static org.junit.Assert.assertFalse; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OrderColumn; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; - -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MyEntity.class, + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-14387") -public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14387") +public class BytecodeEnhancementElementCollectionRecreateTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MyEntity.class - }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( false ); - } - - @Before - public void check() { - inSession( + @BeforeEach + public void check(SessionFactoryScope scope) { + scope.inSession( session -> assertFalse( session.getSessionFactory().getSessionFactoryOptions() - .isCollectionsInDefaultFetchGroupEnabled() ) + .isCollectionsInDefaultFetchGroupEnabled() ) ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "delete from myentity" ).executeUpdate() ); } @Test - public void testRecreateCollection() { - inTransaction( session -> { + public void testRecreateCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -87,21 +81,21 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testRecreateCollectionFind() { - inTransaction( session -> { + public void testRecreateCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( Arrays.asList( "two", "three" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "two", "three" ); @@ -109,20 +103,20 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testDeleteCollection() { - inTransaction( session -> { + public void testDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -130,20 +124,20 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testDeleteCollectionFind() { - inTransaction( session -> { + public void testDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); entity.setElements( new ArrayList<>() ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .isEmpty(); @@ -151,19 +145,19 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollection() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollection(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.get( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.get( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.get( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); @@ -172,19 +166,19 @@ public class BytecodeEnhancementElementCollectionRecreateTest extends BaseNonCon } @Test - public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind() { - inTransaction( session -> { + public void testLoadAndCommitTransactionDoesNotDeleteCollectionFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity entity = new MyEntity(); entity.setId( 1 ); entity.setElements( Arrays.asList( "one", "two", "four" ) ); session.persist( entity ); } ); - inTransaction( session -> - session.find( MyEntity.class, 1 ) + scope.inTransaction( session -> + session.find( MyEntity.class, 1 ) ); - inTransaction( session -> { + scope.inTransaction( session -> { MyEntity entity = session.find( MyEntity.class, 1 ); assertThat( entity.getElements() ) .containsExactlyInAnyOrder( "one", "two", "four" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/deletetransient/DeleteTransientEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/deletetransient/DeleteTransientEntityTest.java index 347ad26004..d45d0fa5c5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/deletetransient/DeleteTransientEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/deletetransient/DeleteTransientEntityTest.java @@ -13,39 +13,37 @@ import org.hibernate.orm.test.deletetransient.Note; import org.hibernate.orm.test.deletetransient.Person; import org.hibernate.orm.test.deletetransient.Suite; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Steve Ebersole */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/deletetransient/Person.hbm.xml" + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { +public class DeleteTransientEntityTest { - @Override - protected String getBaseForMappings() { - return "org/hibernate/orm/test/"; - } - - @Override - public String[] getMappings() { - return new String[] { "deletetransient/Person.hbm.xml" }; - } - - @Override - protected boolean isCleanupTestDataRequired() { - return true; + @AfterEach + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "from java.lang.Object", Object.class ).list().forEach( session::remove ) ); } @Test - public void testTransientEntityDeletionNoCascades() { - inTransaction( + public void testTransientEntityDeletionNoCascades(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.remove( new Address() ); } @@ -53,8 +51,8 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } @Test - public void testTransientEntityDeletionCascadingToTransientAssociation() { - inTransaction( + public void testTransientEntityDeletionCascadingToTransientAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person p = new Person(); p.getAddresses().add( new Address() ); @@ -64,8 +62,8 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } @Test - public void testTransientEntityDeleteCascadingToCircularity() { - inTransaction( + public void testTransientEntityDeleteCascadingToCircularity(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person p1 = new Person(); Person p2 = new Person(); @@ -77,16 +75,16 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } @Test - public void testTransientEntityDeletionCascadingToDetachedAssociation() { + public void testTransientEntityDeletionCascadingToDetachedAssociation(SessionFactoryScope scope) { Address address = new Address(); - inTransaction( + scope.inTransaction( session -> { address.setInfo( "123 Main St." ); session.persist( address ); } ); - inTransaction( + scope.inTransaction( session -> { Person p = new Person(); p.getAddresses().add( address ); @@ -94,18 +92,18 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Long count = (Long) session.createQuery( "select count(*) from Address" ).list().get( 0 ); - assertEquals( "delete not cascaded properly across transient entity", 0, count.longValue() ); + assertEquals( 0, count.longValue(), "delete not cascaded properly across transient entity" ); } ); } @Test - public void testTransientEntityDeletionCascadingToPersistentAssociation() { - Long id = fromTransaction( + public void testTransientEntityDeletionCascadingToPersistentAssociation(SessionFactoryScope scope) { + Long id = scope.fromTransaction( session -> { Address address = new Address(); address.setInfo( "123 Main St." ); @@ -114,7 +112,7 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Address address = session.get( Address.class, id ); Person p = new Person(); @@ -123,20 +121,20 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Long count = (Long) session.createQuery( "select count(*) from Address" ).list().get( 0 ); - assertEquals( "delete not cascaded properly across transient entity", 0, count.longValue() ); + assertEquals( 0, count.longValue(), "delete not cascaded properly across transient entity" ); } ); } @Test @SuppressWarnings({ "unchecked" }) - public void testCascadeAllFromClearedPersistentAssnToTransientEntity() { + public void testCascadeAllFromClearedPersistentAssnToTransientEntity(SessionFactoryScope scope) { final Person p = new Person(); Address address = new Address(); - inTransaction( + scope.inTransaction( session -> { address.setInfo( "123 Main St." ); p.getAddresses().add( address ); @@ -144,7 +142,7 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Suite suite = new Suite(); address.getSuites().add( suite ); @@ -153,10 +151,10 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.get( p.getClass(), p.getId() ); - assertEquals( "persistent collection not cleared", 0, person.getAddresses().size() ); + assertEquals( 0, person.getAddresses().size(), "persistent collection not cleared" ); Long count = (Long) session.createQuery( "select count(*) from Address" ).list().get( 0 ); assertEquals( 1, count.longValue() ); count = (Long) session.createQuery( "select count(*) from Suite" ).list().get( 0 ); @@ -167,19 +165,19 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings({ "unchecked" }) - public void testCascadeAllDeleteOrphanFromClearedPersistentAssnToTransientEntity() { + public void testCascadeAllDeleteOrphanFromClearedPersistentAssnToTransientEntity(SessionFactoryScope scope) { Address address = new Address(); address.setInfo( "123 Main St." ); Suite suite = new Suite(); address.getSuites().add( suite ); - inTransaction( + scope.inTransaction( session -> { session.save( address ); } ); - inTransaction( + scope.inTransaction( session -> { Note note = new Note(); note.setDescription( "a description" ); @@ -190,13 +188,13 @@ public class DeleteTransientEntityTest extends BaseCoreFunctionalTestCase { ); - inTransaction( + scope.inTransaction( session -> { Long count = (Long) session.createQuery( "select count(*) from Suite" ).list().get( 0 ); assertEquals( - "all-delete-orphan not cascaded properly to cleared persistent collection entities", 0, - count.longValue() + count.longValue(), + "all-delete-orphan not cascaded properly to cleared persistent collection entities" ); count = (Long) session.createQuery( "select count(*) from Note" ).list().get( 0 ); assertEquals( 0, count.longValue() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/DetachedGetIdentifierTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/DetachedGetIdentifierTest.java index 158f58eefe..96c5c5b624 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/DetachedGetIdentifierTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/DetachedGetIdentifierTest.java @@ -1,45 +1,48 @@ package org.hibernate.orm.test.bytecode.enhancement.detached; -import org.hibernate.SessionFactory; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.junit.Assert.assertNotNull; +import org.hibernate.SessionFactory; + +import org.junit.jupiter.api.Test; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-11426" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DetachedGetIdentifierTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{SimpleEntity.class}; - } +@JiraKey( "HHH-11426" ) +@DomainModel( + annotatedClasses = { + DetachedGetIdentifierTest.SimpleEntity.class + } +) +@org.hibernate.testing.orm.junit.SessionFactory +@BytecodeEnhanced +public class DetachedGetIdentifierTest { @Test - public void test() { + public void test(SessionFactoryScope scope) { SimpleEntity[] entities = new SimpleEntity[2]; entities[0] = new SimpleEntity(); entities[0].name = "test"; - TransactionUtil.doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { entities[1] = em.merge( entities[0] ); assertNotNull( em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( entities[1] ) ); } ); // Call as detached entity - try ( SessionFactory sessionFactory = sessionFactory() ) { + try ( SessionFactory sessionFactory = scope.getSessionFactory() ) { assertNotNull( sessionFactory.getPersistenceUnitUtil().getIdentifier( entities[1] ) ); } } @@ -48,7 +51,7 @@ public class DetachedGetIdentifierTest extends BaseCoreFunctionalTestCase { @Entity(name = "SimpleEntity") @Table( name = "SIMPLE_ENTITY" ) - private static class SimpleEntity { + static class SimpleEntity { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/RemoveUninitializedLazyCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/RemoveUninitializedLazyCollectionTest.java index 2407748329..2ac752f418 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/RemoveUninitializedLazyCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/detached/RemoveUninitializedLazyCollectionTest.java @@ -12,14 +12,14 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -34,27 +34,25 @@ import jakarta.persistence.OneToMany; * @author Christian Beikov */ @TestForIssue(jiraKey = "HHH-14387") -@RunWith( BytecodeEnhancerRunner.class ) +@DomainModel( + annotatedClasses = { + RemoveUninitializedLazyCollectionTest.Parent.class, + RemoveUninitializedLazyCollectionTest.Child1.class, + RemoveUninitializedLazyCollectionTest.Child2.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true, inlineDirtyChecking = true, biDirectionalAssociationManagement = true ) -public class RemoveUninitializedLazyCollectionTest extends BaseCoreFunctionalTestCase { +public class RemoveUninitializedLazyCollectionTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ - Parent.class, - Child1.class, - Child2.class - }; - } - - @After - public void tearDown() { - TransactionUtil.doInJPA( - this::sessionFactory, + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Child1" ).executeUpdate(); session.createQuery( "delete from Child2" ).executeUpdate(); @@ -63,11 +61,10 @@ public class RemoveUninitializedLazyCollectionTest extends BaseCoreFunctionalTes ); } - @Before - public void setup() { + @BeforeEach + public void setup(SessionFactoryScope scope) { Parent parent = new Parent( 1L, "test" ); - TransactionUtil.doInJPA( - this::sessionFactory, + scope.inTransaction( entityManager -> { entityManager.persist( parent ); entityManager.persist( new Child2( 1L, "child2", parent ) ); @@ -76,8 +73,8 @@ public class RemoveUninitializedLazyCollectionTest extends BaseCoreFunctionalTes } @Test - public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() { - EntityManager em = sessionFactory().createEntityManager(); + public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef(SessionFactoryScope scope) { + EntityManager em = scope.getSessionFactory().createEntityManager(); try { // Lazily initialize the child1 collection List child1 = em.find( Parent.class, 1L ).getChild1(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupFalseTest.java index e9559a096b..1253fc9716 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupFalseTest.java @@ -6,34 +6,32 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; +import jakarta.persistence.CollectionTable; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; @@ -46,35 +44,23 @@ import jakarta.persistence.Table; * * @author Christian Beikov */ -@TestForIssue( jiraKey = "HHH-14348" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DirtyTrackingCollectionInDefaultFetchGroupFalseTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-14348" ) +@DomainModel( + annotatedClasses = { + DirtyTrackingCollectionInDefaultFetchGroupFalseTest.StringsEntity.class + } +) +@SessionFactory( + // We want to test with this setting set to false explicitly, + // because another test already takes care of the default. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class DirtyTrackingCollectionInDefaultFetchGroupFalseTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } - - @Before - public void prepare() { - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) ); @@ -83,8 +69,8 @@ public class DirtyTrackingCollectionInDefaultFetchGroupFalseTest extends BaseCor } @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); entityManager.flush(); BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) ( (PersistentAttributeInterceptable) entity ) @@ -98,13 +84,14 @@ public class DirtyTrackingCollectionInDefaultFetchGroupFalseTest extends BaseCor // --- // @Entity - @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + @Table(name = "STRINGS_ENTITY") + static class StringsEntity { @Id Long id; @ElementCollection + @CollectionTable(name = "STRINGS_ENTITY_SOME", joinColumns = @JoinColumn(name = "SOME_ID")) List someStrings; @ManyToOne(fetch = FetchType.LAZY) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupTest.java index 4ad7938fca..b4aae20c1d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionInDefaultFetchGroupTest.java @@ -11,53 +11,48 @@ import java.util.Arrays; import java.util.List; import java.util.Set; import jakarta.persistence.CascadeType; +import jakarta.persistence.CollectionTable; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.SessionFactoryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.jupiter.api.Assertions; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Christian Beikov */ -@TestForIssue( jiraKey = "HHH-14348" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey( "HHH-14348" ) +@DomainModel( + annotatedClasses = { + DirtyTrackingCollectionInDefaultFetchGroupTest.StringsEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class DirtyTrackingCollectionInDefaultFetchGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @Before - public void prepare() { - assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) ); @@ -66,8 +61,8 @@ public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseNonConfi } @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); entityManager.flush(); @@ -88,12 +83,13 @@ public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseNonConfi @Entity @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + static class StringsEntity { @Id Long id; @ElementCollection + @CollectionTable(name = "STRINGS_ENTITY_SOME", joinColumns = @JoinColumn(name = "SOME_ID")) List someStrings; @ManyToOne(fetch = FetchType.LAZY) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionNotInDefaultFetchGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionNotInDefaultFetchGroupTest.java index 5f5e57709e..438922dff6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionNotInDefaultFetchGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionNotInDefaultFetchGroupTest.java @@ -11,53 +11,50 @@ import java.util.Arrays; import java.util.List; import java.util.Set; -import org.hibernate.boot.SessionFactoryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.jupiter.api.Assertions; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; +import jakarta.persistence.CollectionTable; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Christian Beikov */ -@TestForIssue( jiraKey = "HHH-14348" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DirtyTrackingCollectionNotInDefaultFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey( "HHH-14348" ) +@DomainModel( + annotatedClasses = { + DirtyTrackingCollectionNotInDefaultFetchGroupTest.StringsEntity.class + } +) +@SessionFactory( + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class DirtyTrackingCollectionNotInDefaultFetchGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( false ); - } - - @Before - public void prepare() { - assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + assertFalse( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) ); @@ -66,8 +63,8 @@ public class DirtyTrackingCollectionNotInDefaultFetchGroupTest extends BaseNonCo } @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); entityManager.flush(); BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) ( (PersistentAttributeInterceptable) entity ) @@ -82,12 +79,13 @@ public class DirtyTrackingCollectionNotInDefaultFetchGroupTest extends BaseNonCo @Entity @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + static class StringsEntity { @Id Long id; @ElementCollection + @CollectionTable(name = "STRINGS_ENTITY_SOME", joinColumns = @JoinColumn(name = "SOME_ID")) List someStrings; @ManyToOne(fetch = FetchType.LAZY) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionTest.java index 23cb7a8cb5..39084aa9cd 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingCollectionTest.java @@ -6,12 +6,13 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; @@ -21,24 +22,24 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-11293" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DirtyTrackingCollectionTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-11293" ) +@DomainModel( + annotatedClasses = { + DirtyTrackingCollectionTest.StringsEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class DirtyTrackingCollectionTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } - - @Before - public void prepare() { - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) ); @@ -47,25 +48,25 @@ public class DirtyTrackingCollectionTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); entity.someStrings.clear(); } ); - doInJPA( this::sessionFactory, entityManager -> { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); assertEquals( 0, entity.someStrings.size() ); entity.someStrings.add( "d" ); } ); - doInJPA( this::sessionFactory, entityManager -> { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); assertEquals( 1, entity.someStrings.size() ); entity.someStrings = new ArrayList<>(); } ); - doInJPA( this::sessionFactory, entityManager -> { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); assertEquals( 0, entity.someStrings.size() ); } ); @@ -75,7 +76,7 @@ public class DirtyTrackingCollectionTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + static class StringsEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateAndInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateAndInheritanceTest.java index 51919ebb5c..086db2b1f2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateAndInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateAndInheritanceTest.java @@ -4,12 +4,13 @@ import java.util.List; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.DiscriminatorColumn; @@ -23,26 +24,29 @@ import jakarta.persistence.Table; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + DirtyTrackingDynamicUpdateAndInheritanceTest.SuperEntity.class, + DirtyTrackingDynamicUpdateAndInheritanceTest.ChildEntity.class, + DirtyTrackingDynamicUpdateAndInheritanceTest.AbstractVersion.class, + DirtyTrackingDynamicUpdateAndInheritanceTest.FileVersion.class + } +) +@SessionFactory( + // We want to test with this setting set to false explicitly, + // because another test already takes care of the default. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) -public class DirtyTrackingDynamicUpdateAndInheritanceTest extends BaseCoreFunctionalTestCase { +public class DirtyTrackingDynamicUpdateAndInheritanceTest { public static final int ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - SuperEntity.class, - ChildEntity.class, - AbstractVersion.class, - FileVersion.class - }; - } - @Test @JiraKey("HHH-16688") - public void testDynamicUpdateWithInheritance() { - inTransaction( + public void testDynamicUpdateWithInheritance(SessionFactoryScope scope) { + scope.inTransaction( session -> { ChildEntity entity = new ChildEntity( ID ); entity.setaSuper( "aSuper before" ); @@ -58,7 +62,7 @@ public class DirtyTrackingDynamicUpdateAndInheritanceTest extends BaseCoreFuncti String aChildNewValue = "aChild after"; String bChildNewValue = "bChild after"; - inTransaction( + scope.inTransaction( session -> { ChildEntity entity = session.find( ChildEntity.class, ID ); entity.setaSuper( aSuperNewValue ); @@ -69,7 +73,7 @@ public class DirtyTrackingDynamicUpdateAndInheritanceTest extends BaseCoreFuncti } ); - inTransaction( + scope.inTransaction( session -> { ChildEntity entity = session.find( ChildEntity.class, ID ); assertThat( entity.getaSuper() ).isEqualTo( aSuperNewValue ); @@ -82,9 +86,8 @@ public class DirtyTrackingDynamicUpdateAndInheritanceTest extends BaseCoreFuncti @Test @JiraKey("HHH-16379") - public void testWithDynamicUpdate() { - - inTransaction( + public void testWithDynamicUpdate(SessionFactoryScope scope) { + scope.inTransaction( session -> { FileVersion version = new FileVersion(); version.setId( "1" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateTest.java index 1b524aae07..62cd598d96 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingDynamicUpdateTest.java @@ -1,33 +1,35 @@ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + DirtyTrackingDynamicUpdateTest.TestEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(inlineDirtyChecking = true) @JiraKey("HHH-16688") -public class DirtyTrackingDynamicUpdateTest extends BaseCoreFunctionalTestCase { +public class DirtyTrackingDynamicUpdateTest { public static final int ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { TestEntity.class }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { TestEntity testEntity = new TestEntity( ID ); @@ -41,14 +43,14 @@ public class DirtyTrackingDynamicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void testDynamicUpdate() { + public void testDynamicUpdate(SessionFactoryScope scope) { String aSuperNewValue = "aSuper after"; String bSuperNewValue = "bSuper after"; String aChildNewValue = "aChild after"; String bChildNewValue = "bChild after"; - inTransaction( + scope.inTransaction( session -> { TestEntity entity = session.find( TestEntity.class, ID ); entity.setaSuper( aSuperNewValue ); @@ -59,7 +61,7 @@ public class DirtyTrackingDynamicUpdateTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { TestEntity entity = session.find( TestEntity.class, ID ); assertThat( entity.getaSuper() ).isEqualTo( aSuperNewValue ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingEmbeddableTest.java index 9d1d9926a2..496356e08a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingEmbeddableTest.java @@ -6,11 +6,10 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -19,7 +18,7 @@ import jakarta.persistence.Id; @JiraKey( "HHH-16774" ) @JiraKey( "HHH-16952" ) -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class DirtyTrackingEmbeddableTest { @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceTest.java index c0345d001f..0ea3495059 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceTest.java @@ -6,13 +6,11 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.Jira; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -24,7 +22,7 @@ import jakarta.persistence.MappedSuperclass; * @author Yoann Rodière * @author Marco Belladelli */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced @EnhancementOptions( inlineDirtyChecking = true ) @Jira( "https://hibernate.atlassian.net/browse/HHH-16459" ) public class DirtyTrackingInheritanceTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceWithGenericsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceWithGenericsTest.java index be2824478e..b8f93d9d9f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceWithGenericsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingInheritanceWithGenericsTest.java @@ -6,12 +6,11 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.orm.junit.Jira; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -23,7 +22,7 @@ import jakarta.persistence.MappedSuperclass; * @author Yoann Rodière * @author Marco Belladelli */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced @EnhancementOptions( inlineDirtyChecking = true ) @Jira( "https://hibernate.atlassian.net/browse/HHH-16459" ) public class DirtyTrackingInheritanceWithGenericsTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNonUpdateableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNonUpdateableTest.java index d1a6039a30..c4e6173a96 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNonUpdateableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNonUpdateableTest.java @@ -6,11 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -20,23 +21,22 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Version; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-12051" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class DirtyTrackingNonUpdateableTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Thing.class}; - } +@JiraKey( "HHH-12051" ) +@DomainModel( + annotatedClasses = { + DirtyTrackingNonUpdateableTest.Thing.class + } +) +@SessionFactory +@BytecodeEnhanced +public class DirtyTrackingNonUpdateableTest { @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { Thing thing = new Thing(); entityManager.persist( thing ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNotInDefaultFetchGroupPersistTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNotInDefaultFetchGroupPersistTest.java index 57cb36e4d4..8280cfeb83 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNotInDefaultFetchGroupPersistTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingNotInDefaultFetchGroupPersistTest.java @@ -11,15 +11,13 @@ import java.util.Collections; import java.util.Date; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import jakarta.persistence.Basic; import jakarta.persistence.CascadeType; @@ -36,40 +34,37 @@ import jakarta.persistence.OrderColumn; import jakarta.persistence.Temporal; import jakarta.persistence.TemporalType; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; /** * @author Christian Beikov */ -@TestForIssue(jiraKey = "HHH-14360") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14360") +@DomainModel( + annotatedClasses = { + DirtyTrackingNotInDefaultFetchGroupPersistTest.HotherEntity.class, + DirtyTrackingNotInDefaultFetchGroupPersistTest.Hentity.class + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced @RequiresDialectFeature(DialectChecks.SupportsIdentityColumns.class) -public class DirtyTrackingNotInDefaultFetchGroupPersistTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { HotherEntity.class, Hentity.class }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( false ); - } +public class DirtyTrackingNotInDefaultFetchGroupPersistTest { @Test - public void test() { - assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + public void test(SessionFactoryScope scope) { + assertFalse( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); Hentity hentity = new Hentity(); HotherEntity hotherEntity = new HotherEntity(); hentity.setLineItems( new ArrayList<>( Collections.singletonList( hotherEntity ) ) ); hentity.setNextRevUNs( new ArrayList<>( Collections.singletonList( "something" ) ) ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { session.persist( hentity ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { hentity.bumpNumber(); session.saveOrUpdate( hentity ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingPersistTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingPersistTest.java index f3e0681e7a..04a2ae1ef8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingPersistTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingPersistTest.java @@ -25,50 +25,46 @@ import jakarta.persistence.OrderColumn; import jakarta.persistence.Temporal; import jakarta.persistence.TemporalType; -import org.hibernate.boot.SessionFactoryBuilder; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; /** * @author Christian Beikov */ -@TestForIssue(jiraKey = "HHH-14360") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14360") +@DomainModel( + annotatedClasses = { + DirtyTrackingPersistTest.HotherEntity.class, DirtyTrackingPersistTest.Hentity.class + } +) +@SessionFactory +@BytecodeEnhanced @RequiresDialectFeature(DialectChecks.SupportsIdentityColumns.class) -public class DirtyTrackingPersistTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { HotherEntity.class, Hentity.class }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } +public class DirtyTrackingPersistTest { @Test - public void test() { - assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + public void test(SessionFactoryScope scope) { + assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); Hentity hentity = new Hentity(); HotherEntity hotherEntity = new HotherEntity(); hentity.setLineItems( new ArrayList<>( Collections.singletonList( hotherEntity ) ) ); hentity.setNextRevUNs( new ArrayList<>( Collections.singletonList( "something" ) ) ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { session.persist( hentity ); } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { hentity.bumpNumber(); session.saveOrUpdate( hentity ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingTest.java index 37d67da0d8..20a761a4ae 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingTest.java @@ -6,10 +6,9 @@ */ package org.hibernate.orm.test.bytecode.enhancement.dirty; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -24,7 +23,7 @@ import java.util.Set; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@BytecodeEnhanced public class DirtyTrackingTest { @Test diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DynamicUpdateTest.java index da0ede6f82..3c563873e1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DynamicUpdateTest.java @@ -5,12 +5,13 @@ import java.time.LocalDate; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -20,23 +21,22 @@ import jakarta.persistence.OneToOne; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -public class DynamicUpdateTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + DynamicUpdateTest.Payment.class, + DynamicUpdateTest.StuffToPay.class + } +) +@SessionFactory +@BytecodeEnhanced +public class DynamicUpdateTest { public static final Long STUFF_TO_PAY_ID = 1l; public static final Long PAYMENT_ID = 2l; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Payment.class, - StuffToPay.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Payment payment = new Payment(PAYMENT_ID); payment.setFee( new BigDecimal( 42 ) ); @@ -51,8 +51,8 @@ public class DynamicUpdateTest extends BaseCoreFunctionalTestCase { @Test @JiraKey("HHH-16577") - public void testSetAttribute() { - inTransaction( + public void testSetAttribute(SessionFactoryScope scope) { + scope.inTransaction( session -> { StuffToPay stuffToPay = session.find( StuffToPay.class, STUFF_TO_PAY_ID ); stuffToPay.confirmPayment(); @@ -61,7 +61,7 @@ public class DynamicUpdateTest extends BaseCoreFunctionalTestCase { assertThat( payment.getPaidAt() ).isNotNull(); } ); - inTransaction( + scope.inTransaction( session -> { Payment payment = session.find( Payment.class, PAYMENT_ID ); assertThat(payment).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/eviction/EvictionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/eviction/EvictionTest.java index 200759ef8e..cd052c4503 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/eviction/EvictionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/eviction/EvictionTest.java @@ -7,11 +7,13 @@ package org.hibernate.orm.test.bytecode.enhancement.eviction; import org.hibernate.engine.spi.ManagedEntity; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -20,27 +22,27 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class EvictionTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + EvictionTest.Parent.class + } +) +@SessionFactory +@BytecodeEnhanced +public class EvictionTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class}; - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create a Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent p = new Parent(); p.name = "PARENT"; s.persist( p ); @@ -48,8 +50,8 @@ public class EvictionTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { // Delete the Parent Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) @@ -91,7 +93,7 @@ public class EvictionTest extends BaseCoreFunctionalTestCase { @Entity( name = "Parent" ) @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/flush/CollectionFlushAfterQueryTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/flush/CollectionFlushAfterQueryTest.java index d0249bbd8c..bffeb7c81a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/flush/CollectionFlushAfterQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/flush/CollectionFlushAfterQueryTest.java @@ -4,37 +4,37 @@ import java.util.HashSet; import java.util.Optional; import java.util.Set; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16337") -public class CollectionFlushAfterQueryTest extends BaseCoreFunctionalTestCase { - - private static final Long MY_ENTITY_ID = 1l; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { +@DomainModel( + annotatedClasses = { CollectionFlushAfterQueryTest.MyEntity.class, CollectionFlushAfterQueryTest.MyOtherEntity.class, CollectionFlushAfterQueryTest.MyAnotherEntity.class - }; - } + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16337") +public class CollectionFlushAfterQueryTest { + + private static final Long MY_ENTITY_ID = 1l; @Test - public void testAutoFlush() { - inTransaction( + public void testAutoFlush(SessionFactoryScope scope) { + scope.inTransaction( session -> { MyEntity myEntity = new MyEntity( MY_ENTITY_ID, "my entity" ); MyOtherEntity otherEntity = new MyOtherEntity( 2l, "my other entity" ); @@ -44,7 +44,7 @@ public class CollectionFlushAfterQueryTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { MyEntity myEntity = session.find( MyEntity.class, MY_ENTITY_ID ); @@ -57,7 +57,7 @@ public class CollectionFlushAfterQueryTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { MyEntity myEntity = session.find( MyEntity.class, MY_ENTITY_ID ); Set redirectUris = myEntity.getOtherEntities(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/join/HHH3949Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/join/HHH3949Test.java index c659f81335..aa766f16de 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/join/HHH3949Test.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/join/HHH3949Test.java @@ -12,14 +12,14 @@ import org.hibernate.Session; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -33,29 +33,32 @@ import jakarta.persistence.criteria.JoinType; import static org.hibernate.Hibernate.isInitialized; import static org.hibernate.Hibernate.isPropertyInitialized; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue( jiraKey = "HHH-3949" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class HHH3949Test extends BaseCoreFunctionalTestCase { +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Person.class, Vehicle.class}; - } +@JiraKey( "HHH-3949" ) +@DomainModel( + annotatedClasses = { + HHH3949Test.Person.class, HHH3949Test.Vehicle.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + // see HHH-3949 for further details ^^^^^ + } +) +@SessionFactory +@BytecodeEnhanced +public class HHH3949Test { - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - // see HHH-3949 for further details ^^^^^ - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { // it is important that the data associations remain as follows: // * Johnny <-> Volkswagen Golf @@ -87,19 +90,19 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { } @Test - public void test1() { - performQueryAndVerifyPersonResults( "from Person p left join fetch p.vehicle" ); + public void test1(SessionFactoryScope scope) { + performQueryAndVerifyPersonResults( scope, "from Person p left join fetch p.vehicle" ); } @Test - public void test2() { - performQueryAndVerifyVehicleResults( "from Vehicle v left join fetch v.driver" ); + public void test2(SessionFactoryScope scope) { + performQueryAndVerifyVehicleResults( scope, "from Vehicle v left join fetch v.driver" ); } @Test @SuppressWarnings( "unchecked" ) - public void test3() { - doInHibernate( this::sessionFactory, s -> { + public void test3(SessionFactoryScope scope) { + scope.inTransaction( s -> { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); criteria.from( Person.class ).fetch( "vehicle", JoinType.LEFT ); @@ -116,10 +119,10 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( "unchecked" ) - public void test4() { + public void test4(SessionFactoryScope scope) { List vehicles; - try ( Session s = openSession() ) { + try ( Session s = scope.getSessionFactory().openSession() ) { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Vehicle.class ); criteria.from( Vehicle.class ).fetch( "driver", JoinType.LEFT ); @@ -138,9 +141,9 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { // --- // @SuppressWarnings( "unchecked" ) - private void performQueryAndVerifyPersonResults(String query) { + private void performQueryAndVerifyPersonResults(SessionFactoryScope scope, String query) { List persons; - try ( Session s = openSession() ) { + try ( Session s = scope.getSessionFactory().openSession() ) { persons = (List) s.createQuery( query ).list(); } for ( Person person : persons ) { @@ -159,9 +162,9 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { } @SuppressWarnings( "unchecked" ) - private void performQueryAndVerifyVehicleResults(String query) { + private void performQueryAndVerifyVehicleResults(SessionFactoryScope scope, String query) { List vehicles; - try ( Session s = openSession() ) { + try ( Session s = scope.getSessionFactory().openSession() ) { vehicles = (List) s.createQuery( query ).list(); } for ( Vehicle vehicle : vehicles ) { @@ -189,7 +192,7 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { @Entity( name = "Person" ) @Table( name = "PERSON" ) - private static class Person { + static class Person { @Id @GeneratedValue @@ -219,7 +222,7 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { @Entity( name = "Vehicle" ) @Table( name = "VEHICLE" ) - private static class Vehicle { + static class Vehicle { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/BidirectionalLazyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/BidirectionalLazyTest.java index 36eff7fc90..1a4a861d10 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/BidirectionalLazyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/BidirectionalLazyTest.java @@ -23,28 +23,27 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.UnloadedClass; import org.hibernate.bytecode.enhance.spi.UnloadedField; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.sameInstance; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests removing non-owning side of the bidirectional association, @@ -52,31 +51,32 @@ import static org.junit.Assert.assertTrue; * * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-13241") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-13241") +@DomainModel( + annotatedClasses = { + BidirectionalLazyTest.Employer.class, BidirectionalLazyTest.Employee.class, BidirectionalLazyTest.Unrelated.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking BidirectionalLazyTest.NoDirtyCheckEnhancementContext.class // supports laziness; does not support dirty-checking }) -public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { +public class BidirectionalLazyTest { // NOTE : tests in this class seem redundant because they assert things that happened // in previous versions that have been fixed - public Class[] getAnnotatedClasses() { - return new Class[] { Employer.class, Employee.class, Unrelated.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - } - @Test - public void testRemoveWithDeletedAssociatedEntity() { + public void testRemoveWithDeletedAssociatedEntity(SessionFactoryScope scope) { - inTransaction( + scope.inTransaction( (session) -> { Employer employer = new Employer( "RedHat" ); @@ -90,7 +90,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { Employer employer = session.get( Employer.class, "RedHat" ); @@ -109,7 +109,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { assertNull( session.find( Employer.class, "RedHat" ) ); assertTrue( session.createQuery( "from Employee e", Employee.class ).getResultList().isEmpty() ); @@ -118,9 +118,9 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } @Test - public void testRemoveWithNonAssociatedRemovedEntity() { + public void testRemoveWithNonAssociatedRemovedEntity(SessionFactoryScope scope) { - inTransaction( + scope.inTransaction( (session) -> { Employer employer = new Employer( "RedHat" ); session.persist( employer ); @@ -131,7 +131,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { // Delete an entity that is not associated with Employee session.remove( session.get( Unrelated.class, 1 ) ); @@ -145,7 +145,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { assertNull( session.find( Unrelated.class, 1 ) ); assertNull( session.find( Employee.class, "Jack" ) ); @@ -155,9 +155,9 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } @Test - public void testRemoveWithNoRemovedEntities() { + public void testRemoveWithNoRemovedEntities(SessionFactoryScope scope) { - inTransaction( + scope.inTransaction( (session) -> { Employer employer = new Employer( "RedHat" ); session.persist( employer ); @@ -167,7 +167,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { // Don't delete any entities before deleting the Employee final Employee employee = session.get( Employee.class, "Jack" ); @@ -178,13 +178,13 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { Employer employer = session.get( Employer.class, "RedHat" ); verifyBaseState( employer ); - assertThat( employee.getEmployer(), sameInstance( employer ) ); + assertThat( employee.getEmployer() ).isInstanceOf( employer.getClass() ); checkEntityEntryState( session, employee, employer, false ); } ); - inTransaction( + scope.inTransaction( (session) -> { assertNull( session.find( Employee.class, "Jack" ) ); session.remove( session.find( Employer.class, "RedHat" ) ); @@ -193,9 +193,9 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } @Test - public void testRemoveEntityWithNullLazyManyToOne() { + public void testRemoveEntityWithNullLazyManyToOne(SessionFactoryScope scope) { - inTransaction( + scope.inTransaction( (session) -> { Employer employer = new Employer( "RedHat" ); session.persist( employer ); @@ -204,7 +204,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( (session) -> { Employee employee = session.get( Employee.class, "Jack" ); verifyBaseState( employee ); @@ -224,8 +224,8 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { * deleting the Employer linked to the loaded Employee */ @Test - public void testRemoveEntityWithLinkedLazyManyToOne() { - inTransaction( + public void testRemoveEntityWithLinkedLazyManyToOne(SessionFactoryScope scope) { + scope.inTransaction( session -> { Employer employer = new Employer( "RedHat" ); session.persist( employer ); @@ -235,7 +235,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Employee employee = session.get( Employee.class, "Jack" ); verifyBaseState( employee ); @@ -244,7 +244,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { Employer employer = session.get( Employer.class, "RedHat" ); verifyBaseState( employer ); - assertThat( employee.getEmployer(), sameInstance( employer ) ); + assertThat( employee.getEmployer() ).isInstanceOf( employer.getClass() ); session.remove( employer ); session.remove( employee ); @@ -265,7 +265,7 @@ public class BidirectionalLazyTest extends BaseCoreFunctionalTestCase { final Employer employer = employee.getEmployer(); if ( employer != null ) { assertFalse( Hibernate.isInitialized( employer ) ); - assertThat(employer, not( instanceOf( HibernateProxy.class ) ) ); + assertThat( employer ).isNotInstanceOf( HibernateProxy.class ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationAndDynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationAndDynamicUpdateTest.java index d3a6238eaa..de3451ef0c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationAndDynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationAndDynamicUpdateTest.java @@ -5,14 +5,15 @@ import java.util.List; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -22,25 +23,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; @JiraKey("HHH-17049") -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ConstructorInitializationAndDynamicUpdateTest.Person.class, + ConstructorInitializationAndDynamicUpdateTest.LoginAccount.class, + ConstructorInitializationAndDynamicUpdateTest.AccountPreferences.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunctionalTestCase { +public class ConstructorInitializationAndDynamicUpdateTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - LoginAccount.class, - AccountPreferences.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1l, "Henry" ); LoginAccount loginAccount = new LoginAccount(); @@ -50,7 +50,7 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -67,9 +67,9 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Person" ).executeUpdate(); session.createMutationQuery( "delete from LoginAccount" ).executeUpdate(); @@ -79,15 +79,15 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } @Test - public void findTest() { - inTransaction( + public void findTest(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -105,15 +105,15 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } @Test - public void getReferenceTest() { - inTransaction( + public void getReferenceTest(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -131,8 +131,8 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } @Test - public void findTest2() { - inTransaction( + public void findTest2(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -142,7 +142,7 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); @@ -167,8 +167,8 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } @Test - public void getReferenceTest2() { - inTransaction( + public void getReferenceTest2(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -178,7 +178,7 @@ public class ConstructorInitializationAndDynamicUpdateTest extends BaseCoreFunct } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationTest.java index 422d20c29b..7c56dcef3f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ConstructorInitializationTest.java @@ -5,14 +5,15 @@ import java.util.List; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.NoDirtyCheckEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -22,25 +23,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; @JiraKey("HHH-17049") -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ConstructorInitializationTest.Person.class, + ConstructorInitializationTest.LoginAccount.class, + ConstructorInitializationTest.AccountPreferences.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { +public class ConstructorInitializationTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - LoginAccount.class, - AccountPreferences.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1l, "Henry" ); LoginAccount loginAccount = new LoginAccount(); @@ -50,7 +50,7 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -67,9 +67,9 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { ); } - @After - public void tearDown(){ - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( session-> { session.createMutationQuery( "delete from Person" ).executeUpdate(); session.createMutationQuery( "delete from LoginAccount" ).executeUpdate(); @@ -79,15 +79,15 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } @Test - public void findTest() { - inTransaction( + public void findTest(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -105,15 +105,15 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } @Test - public void getReferenceTest() { - inTransaction( + public void getReferenceTest(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - inTransaction( + scope.inTransaction( session -> { List accounts = session.createQuery( "select la from LoginAccount la", @@ -131,8 +131,8 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } @Test - public void findTest2() { - inTransaction( + public void findTest2(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -142,7 +142,7 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); @@ -167,8 +167,8 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } @Test - public void getReferenceTest2() { - inTransaction( + public void getReferenceTest2(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = session.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -178,7 +178,7 @@ public class ConstructorInitializationTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest1.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest1.java index 6bfe89e5a6..890c8edcf2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest1.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest1.java @@ -10,12 +10,14 @@ import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -26,22 +28,21 @@ import jakarta.persistence.Table; import java.util.HashSet; import java.util.Set; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - -@TestForIssue( jiraKey = "HHH-10708" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class UnexpectedDeleteTest1 extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10708" ) +@DomainModel( + annotatedClasses = { + UnexpectedDeleteTest1.Foo.class, UnexpectedDeleteTest1.Bar.class + } +) +@SessionFactory +@BytecodeEnhanced +public class UnexpectedDeleteTest1 { private long fooId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Foo.class, Bar.class}; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Bar bar1 = new Bar(); Bar bar2 = new Bar(); Foo foo = new Foo(); @@ -55,8 +56,8 @@ public class UnexpectedDeleteTest1 extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Foo foo = s.get( Foo.class, fooId ); // accessing the collection results in an exception @@ -68,7 +69,7 @@ public class UnexpectedDeleteTest1 extends BaseCoreFunctionalTestCase { @Entity(name = "Bar") @Table( name = "BAR" ) - private static class Bar { + static class Bar { @Id @GeneratedValue @@ -81,7 +82,7 @@ public class UnexpectedDeleteTest1 extends BaseCoreFunctionalTestCase { @Entity(name = "Foo") @Table( name = "FOO" ) - private static class Foo { + static class Foo { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest2.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest2.java index b0712a6a38..fa8121e4b3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest2.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest2.java @@ -6,13 +6,15 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.HHH_10708; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -23,22 +25,21 @@ import jakarta.persistence.Table; import java.util.HashSet; import java.util.Set; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - -@TestForIssue( jiraKey = "HHH-10708" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class UnexpectedDeleteTest2 extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10708" ) +@DomainModel( + annotatedClasses = { + UnexpectedDeleteTest2.Foo.class, UnexpectedDeleteTest2.Bar.class + } +) +@SessionFactory +@BytecodeEnhanced +public class UnexpectedDeleteTest2 { private Bar myBar; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Foo.class, Bar.class}; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Bar bar = new Bar(); Foo foo1 = new Foo(); Foo foo2 = new Foo(); @@ -54,17 +55,17 @@ public class UnexpectedDeleteTest2 extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.refresh( myBar ); - Assert.assertFalse( myBar.foos.isEmpty() ); + assertFalse( myBar.foos.isEmpty() ); // The issue is that currently, for some unknown reason, foos are deleted on flush } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Bar bar = s.get( Bar.class, myBar.id ); - Assert.assertFalse( bar.foos.isEmpty() ); + assertFalse( bar.foos.isEmpty() ); } ); } @@ -72,7 +73,7 @@ public class UnexpectedDeleteTest2 extends BaseCoreFunctionalTestCase { @Entity(name = "Bar") @Table( name = "BAR" ) - private static class Bar { + static class Bar { @Id @GeneratedValue @@ -84,7 +85,7 @@ public class UnexpectedDeleteTest2 extends BaseCoreFunctionalTestCase { @Entity(name = "Foo") @Table( name = "FOO" ) - private static class Foo { + static class Foo { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest3.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest3.java index 0e437e1776..fbe0bbfc7e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest3.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/HHH_10708/UnexpectedDeleteTest3.java @@ -6,13 +6,13 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.HHH_10708; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; @@ -25,20 +25,21 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertEquals; -@TestForIssue( jiraKey = "HHH-10708" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class UnexpectedDeleteTest3 extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-10708") +@DomainModel( + annotatedClasses = { + UnexpectedDeleteTest3.Parent.class, UnexpectedDeleteTest3.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +public class UnexpectedDeleteTest3 { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Child child = new Child(); child.setId( 2L ); s.save( child ); @@ -53,8 +54,8 @@ public class UnexpectedDeleteTest3 extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.get( Parent.class, 1L ); Child child = new Child(); @@ -67,9 +68,9 @@ public class UnexpectedDeleteTest3 extends BaseCoreFunctionalTestCase { s.save( parent ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent application = s.get( Parent.class, 1L ); - Assert.assertEquals( "Loaded Children collection has unexpected size", 2, application.getChildren().size() ); + assertEquals( 2, application.getChildren().size(), "Loaded Children collection has unexpected size" ); } ); } @@ -77,7 +78,7 @@ public class UnexpectedDeleteTest3 extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { Long id; @@ -94,7 +95,7 @@ public class UnexpectedDeleteTest3 extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { Long id; Set names; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/IdInUninitializedProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/IdInUninitializedProxyTest.java index 682cac757a..1b7e996263 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/IdInUninitializedProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/IdInUninitializedProxyTest.java @@ -11,34 +11,34 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import org.hibernate.Hibernate; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-14571") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14571") +@DomainModel( + annotatedClasses = { + IdInUninitializedProxyTest.AnEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, extendedEnhancement = true) -public class IdInUninitializedProxyTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { AnEntity.class }; - } +public class IdInUninitializedProxyTest { @Test - public void testIdIsAlwaysConsideredInitialized() { - inTransaction( session -> { + public void testIdIsAlwaysConsideredInitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { final AnEntity e = session.byId( AnEntity.class ).getReference( 1 ); assertFalse( Hibernate.isInitialized( e ) ); // This is the gist of the problem @@ -52,9 +52,9 @@ public class IdInUninitializedProxyTest extends BaseNonConfigCoreFunctionalTestC } ); } - @Before - public void prepareTestData() { - inTransaction( session -> { + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { AnEntity anEntity = new AnEntity(); anEntity.id = 1; anEntity.name = "George"; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationAndDynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationAndDynamicUpdateTest.java index 5c26365e2e..ccc3bd180e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationAndDynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationAndDynamicUpdateTest.java @@ -4,15 +4,16 @@ import java.util.List; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -22,26 +23,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.assertj.core.api.Assertions.assertThat; @JiraKey("HHH-17049") -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JpaConstructorInitializationAndDynamicUpdateTest.Person.class, + JpaConstructorInitializationAndDynamicUpdateTest.LoginAccount.class, + JpaConstructorInitializationAndDynamicUpdateTest.AccountPreferences.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntityManagerFunctionalTestCase { +public class JpaConstructorInitializationAndDynamicUpdateTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - LoginAccount.class, - AccountPreferences.class - }; - } - - @Before - public void setUp() { - doInJPA( this::entityManagerFactory, em -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( em -> { Person person = new Person( 1l, "Henry" ); LoginAccount loginAccount = new LoginAccount(); loginAccount.setOwner( person ); @@ -50,7 +49,7 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -66,9 +65,9 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity ); } - @After - public void tearDown() { - doInJPA( this::entityManagerFactory, em -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.createQuery( "delete from Person" ).executeUpdate(); em.createQuery( "delete from LoginAccount" ).executeUpdate(); em.createQuery( "delete from AccountPreferences" ).executeUpdate(); @@ -77,15 +76,15 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } @Test - public void findTest() { - doInJPA( this::entityManagerFactory, em -> { + public void findTest(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.find( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -102,15 +101,15 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } @Test - public void getReferenceTest() { - doInJPA( this::entityManagerFactory, em -> { + public void getReferenceTest(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -127,8 +126,8 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } @Test - public void findTest2() { - doInJPA( this::entityManagerFactory, em -> { + public void findTest2(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.find( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -138,7 +137,7 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { Person person = em.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); @@ -162,8 +161,8 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } @Test - public void getReferenceTest2() { - doInJPA( this::entityManagerFactory, em -> { + public void getReferenceTest2(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -173,7 +172,7 @@ public class JpaConstructorInitializationAndDynamicUpdateTest extends BaseEntity } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { Person person = em.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationTest.java index e94674195d..b3b0187a97 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/JpaConstructorInitializationTest.java @@ -3,15 +3,16 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import java.util.List; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -21,26 +22,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.assertj.core.api.Assertions.assertThat; @JiraKey("HHH-17049") -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JpaConstructorInitializationTest.Person.class, + JpaConstructorInitializationTest.LoginAccount.class, + JpaConstructorInitializationTest.AccountPreferences.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class JpaConstructorInitializationTest extends BaseEntityManagerFunctionalTestCase { +public class JpaConstructorInitializationTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - LoginAccount.class, - AccountPreferences.class - }; - } - - @Before - public void setUp() { - doInJPA( this::entityManagerFactory, em -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( em -> { Person person = new Person( 1l, "Henry" ); LoginAccount loginAccount = new LoginAccount(); loginAccount.setOwner( person ); @@ -49,7 +48,7 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -65,9 +64,9 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona ); } - @After - public void tearDown() { - doInJPA( this::entityManagerFactory, em -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.createQuery( "delete from Person" ).executeUpdate(); em.createQuery( "delete from LoginAccount" ).executeUpdate(); em.createQuery( "delete from AccountPreferences" ).executeUpdate(); @@ -76,15 +75,15 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } @Test - public void findTest() { - doInJPA( this::entityManagerFactory, em -> { + public void findTest(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.find( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -101,15 +100,15 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } @Test - public void getReferenceTest() { - doInJPA( this::entityManagerFactory, em -> { + public void getReferenceTest(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { List accounts = em.createQuery( "select la from LoginAccount la", LoginAccount.class @@ -126,8 +125,8 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } @Test - public void findTest2() { - doInJPA( this::entityManagerFactory, em -> { + public void findTest2(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.find( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -137,7 +136,7 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { Person person = em.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); @@ -161,8 +160,8 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } @Test - public void getReferenceTest2() { - doInJPA( this::entityManagerFactory, em -> { + public void getReferenceTest2(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.clear(); Person person = em.getReference( Person.class, 1L ); person.setFirstName( "Liza" ); @@ -172,7 +171,7 @@ public class JpaConstructorInitializationTest extends BaseEntityManagerFunctiona } ); - doInJPA( this::entityManagerFactory, em -> { + scope.inTransaction( em -> { Person person = em.find( Person.class, 1L ); assertThat( person.getFirstName() ).isEqualTo( "Liza" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyAbstractManyToOneNoProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyAbstractManyToOneNoProxyTest.java index 8fa049c592..a0bfa93d41 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyAbstractManyToOneNoProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyAbstractManyToOneNoProxyTest.java @@ -1,15 +1,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.annotations.Proxy; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.DiscriminatorColumn; import jakarta.persistence.DiscriminatorValue; @@ -22,38 +22,31 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyAbstractManyToOneNoProxyTest.User.class, + LazyAbstractManyToOneNoProxyTest.UserGroup.class, + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-16794") -public class LazyAbstractManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { +public class LazyAbstractManyToOneNoProxyTest { private static final String USER_1_NAME = "Andrea"; private static final String USER_2_NAME = "Fab"; private static final String USER_GROUP_1_NAME = "group1"; private static final String USER_GROUP_2_NAME = "group2"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - UserGroup.class, - }; + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { UserGroup group1 = new UserGroup( 1l, USER_GROUP_1_NAME ); UserGroup group2 = new UserGroup( 2l, USER_GROUP_2_NAME ); @@ -70,10 +63,10 @@ public class LazyAbstractManyToOneNoProxyTest extends BaseCoreFunctionalTestCase } @Test - public void testSelect() { - inTransaction( + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( session -> { - SQLStatementInspector statementInspector = statementInspector(); + SQLStatementInspector statementInspector = statementInspector( scope ); statementInspector.clear(); User user = session.getReference( User.class, 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldMergeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldMergeTest.java index a4d8e71cea..524a91da7a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldMergeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldMergeTest.java @@ -17,43 +17,34 @@ import jakarta.persistence.Lob; import jakarta.persistence.OneToOne; import jakarta.persistence.Table; -import org.hibernate.Hibernate; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.tuple.NonIdentifierAttribute; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; /** * @author Vlad Mihalcea */ -@TestForIssue( jiraKey = "HHH-11117") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyBasicFieldMergeTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Company.class, - Manager.class, - }; - } +@JiraKey("HHH-11117") +@DomainModel( + annotatedClasses = { + LazyBasicFieldMergeTest.Company.class, + LazyBasicFieldMergeTest.Manager.class, + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyBasicFieldMergeTest { @Test - public void test() { - doInHibernate( this::sessionFactory, session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Manager manager = new Manager(); manager.setName("John Doe"); manager.setResume(new byte[] {1, 2, 3}); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldNotInitializedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldNotInitializedTest.java index 03b949174f..64947b42ed 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldNotInitializedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyBasicFieldNotInitializedTest.java @@ -8,19 +8,21 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.Hibernate; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.FetchTiming; import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.BasicValuedModelPart; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -30,33 +32,34 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-9937") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyBasicFieldNotInitializedTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-9937") +@DomainModel( + annotatedClasses = { + LazyBasicFieldNotInitializedTest.TestEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyBasicFieldNotInitializedTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{TestEntity.class}; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity entity = new TestEntity(); entity.description = "desc"; s.persist( entity ); @@ -65,18 +68,18 @@ public class LazyBasicFieldNotInitializedTest extends BaseCoreFunctionalTestCase } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity entity = s.get( TestEntity.class, entityId ); - Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) ); + assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) ); - EntityPersister entityPersister = sessionFactory().getRuntimeMetamodels() + EntityPersister entityPersister = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( TestEntity.class ); boolean[] propertyLaziness = entityPersister.getPropertyLaziness(); assertEquals( 1, propertyLaziness.length ); - assertTrue( propertyLaziness[0] ); + Assertions.assertTrue( propertyLaziness[0] ); // Make sure NonIdentifierAttribute#isLazy is consistent (HHH-10551) final AttributeMapping theBytesAttr = entityPersister.findAttributeMapping( "description" ); @@ -89,7 +92,7 @@ public class LazyBasicFieldNotInitializedTest extends BaseCoreFunctionalTestCase @Entity(name = "TestEntity") @Table( name = "TEST_ENTITY" ) - private static class TestEntity { + static class TestEntity { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDeletedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDeletedTest.java index 2485067066..edfd29db42 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDeletedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDeletedTest.java @@ -7,14 +7,16 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -29,32 +31,34 @@ import jakarta.persistence.Table; import java.util.HashSet; import java.util.Set; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-11576" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-11576") +@DomainModel( + annotatedClasses = { + LazyCollectionDeletedTest.Post.class, + LazyCollectionDeletedTest.Tag.class, + LazyCollectionDeletedTest.AdditionalDetails.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyCollectionDeletedTest { private Long postId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Post.class, Tag.class, AdditionalDetails.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Post post = new Post(); Tag tag1 = new Tag( "tag1" ); @@ -75,8 +79,8 @@ public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Query query = s.createQuery( "from AdditionalDetails where id=" + postId ); AdditionalDetails additionalDetails = (AdditionalDetails) query.getSingleResult(); additionalDetails.details = "New data"; @@ -85,11 +89,11 @@ public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { // additionalDetails.post.tags get deleted on commit } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Query query = s.createQuery( "from Post where id=" + postId ); Post retrievedPost = (Post) query.getSingleResult(); - assertFalse( "No tags found", retrievedPost.tags.isEmpty() ); + assertFalse( retrievedPost.tags.isEmpty(), "No tags found" ); retrievedPost.tags.forEach( tag -> System.out.println( "Found tag: " + tag ) ); } ); } @@ -98,7 +102,7 @@ public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { @Entity( name = "Tag" ) @Table( name = "TAG" ) - private static class Tag { + static class Tag { @Id @GeneratedValue @@ -116,7 +120,7 @@ public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { @Entity( name = "Post" ) @Table( name = "POST" ) - private static class Post { + static class Post { @Id @GeneratedValue @@ -131,7 +135,7 @@ public class LazyCollectionDeletedTest extends BaseCoreFunctionalTestCase { @Entity( name = "AdditionalDetails" ) @Table( name = "ADDITIONAL_DETAILS" ) - private static class AdditionalDetails { + static class AdditionalDetails { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachTest.java index 6fdc37ac4b..9733d3bc86 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachTest.java @@ -12,7 +12,6 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.Hibernate.isInitialized; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertFalse; import java.util.ArrayList; @@ -29,28 +28,32 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "HHH-12260") -@RunWith(BytecodeEnhancerRunner.class) -public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-12260") +@DomainModel( + annotatedClasses = { + LazyCollectionDetachTest.Parent.class, LazyCollectionDetachTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyCollectionDetachTest { private static final int CHILDREN_SIZE = 10; private Long parentID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ Parent.class, Child.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); parent.setChildren( new ArrayList<>() ); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { @@ -63,9 +66,14 @@ public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { } ); } + @AfterEach + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createQuery( "from java.lang.Object", Object.class ).list().forEach( session::remove ) ); + } + @Test - public void testDetach() { - doInHibernate( this::sessionFactory, s -> { + public void testDetach(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); @@ -80,8 +88,8 @@ public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { } @Test - public void testDetachProxy() { - doInHibernate( this::sessionFactory, s -> { + public void testDetachProxy(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.getReference( Parent.class, parentID ); checkDirtyTracking( parent ); @@ -93,8 +101,8 @@ public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh() { - doInHibernate( this::sessionFactory, s -> { + public void testRefresh(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); @@ -111,7 +119,7 @@ public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { @Entity(name = "Parent") @Table(name = "PARENT") - private static class Parent { + static class Parent { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -127,7 +135,7 @@ public class LazyCollectionDetachTest extends BaseCoreFunctionalTestCase { @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.java index fc7ae7a71c..87a036197f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.java @@ -12,24 +12,20 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.Hibernate.isPropertyInitialized; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertFalse; import java.util.ArrayList; import java.util.List; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -47,38 +43,23 @@ import jakarta.persistence.Table; *

* Kept here for historical reasons. */ -@TestForIssue(jiraKey = "HHH-12260") -@RunWith(BytecodeEnhancerRunner.class) -public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-12260") +@DomainModel( + annotatedClasses = { + LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.Parent.class, + LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest.Child.class + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced +public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest { private static final int CHILDREN_SIZE = 10; private Long parentID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ Parent.class, Child.class }; - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); parent.setChildren( new ArrayList<>() ); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { @@ -92,8 +73,8 @@ public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest exte } @Test - public void testDetach() { - doInHibernate( this::sessionFactory, s -> { + public void testDetach(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); @@ -108,8 +89,8 @@ public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest exte } @Test - public void testDetachProxy() { - doInHibernate( this::sessionFactory, s -> { + public void testDetachProxy(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.getReference( Parent.class, parentID ); checkDirtyTracking( parent ); @@ -121,8 +102,8 @@ public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest exte } @Test - public void testRefresh() { - doInHibernate( this::sessionFactory, s -> { + public void testRefresh(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); @@ -139,7 +120,7 @@ public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest exte @Entity(name = "Parent") @Table(name = "PARENT") - private static class Parent { + static class Parent { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -155,7 +136,7 @@ public class LazyCollectionDetachWithCollectionInDefaultFetchGroupFalseTest exte @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionHandlingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionHandlingTest.java index a2037a7914..685b9ea721 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionHandlingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionHandlingTest.java @@ -6,8 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - import java.util.LinkedHashSet; import java.util.Set; @@ -18,28 +16,26 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToMany; import jakarta.persistence.MappedSuperclass; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "") -@RunWith(BytecodeEnhancerRunner.class) -public class LazyCollectionHandlingTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyCollectionHandlingTest.JafSid.class, LazyCollectionHandlingTest.UserGroup.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyCollectionHandlingTest { private Integer id; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - JafSid.class, UserGroup.class - }; - } - @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { JafSid sid = new JafSid(); s.save( sid ); @@ -49,7 +45,7 @@ public class LazyCollectionHandlingTest extends BaseCoreFunctionalTestCase { this.id = sid.getId(); }); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.get( JafSid.class, this.id ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionLoadingTest.java index 04eeec043e..7d5af023f7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyCollectionLoadingTest.java @@ -9,14 +9,16 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -39,9 +41,8 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.Hibernate.isInitialized; import static org.hibernate.Hibernate.isPropertyInitialized; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Simple test for lazy collection handling in the new bytecode support. @@ -52,27 +53,28 @@ import static org.junit.Assert.assertTrue; * * @author Steve Ebersole */ -@TestForIssue( jiraKey = "HHH-10055" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10055" ) +@DomainModel( + annotatedClasses = { + LazyCollectionLoadingTest.Parent.class, LazyCollectionLoadingTest.Child.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyCollectionLoadingTest { private static final int CHILDREN_SIZE = 10; private Long parentID; private Parent parent; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); parent.setChildren( new ArrayList<>() ); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { @@ -86,8 +88,8 @@ public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { } @Test - public void testTransaction() { - doInHibernate( this::sessionFactory, s -> { + public void testTransaction(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.load( Parent.class, parentID ); assertThat( parent, notNullValue() ); assertThat( parent, not( instanceOf( HibernateProxy.class ) ) ); @@ -109,9 +111,9 @@ public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue( jiraKey = "HHH-14620" ) - public void testTransaction_noProxy() { - doInHibernate( this::sessionFactory, s -> { + @JiraKey( "HHH-14620" ) + public void testTransaction_noProxy(SessionFactoryScope scope) { + scope.inTransaction( s -> { // find will not return a proxy, which is exactly what we want here. Parent parent = s.find( Parent.class, parentID ); assertThat( parent, notNullValue() ); @@ -136,8 +138,8 @@ public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { } @Test - public void testNoTransaction() { - doInHibernate( this::sessionFactory, s -> { + public void testNoTransaction(SessionFactoryScope scope) { + scope.inTransaction( s -> { parent = s.load( Parent.class, parentID ); assertThat( parent, notNullValue() ); assertThat( parent, not( instanceOf( HibernateProxy.class ) ) ); @@ -161,7 +163,7 @@ public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -177,7 +179,7 @@ public class LazyCollectionLoadingTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyInitializationWithoutInlineDirtyTrackingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyInitializationWithoutInlineDirtyTrackingTest.java index 4a94c34938..7c45453663 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyInitializationWithoutInlineDirtyTrackingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyInitializationWithoutInlineDirtyTrackingTest.java @@ -6,8 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; - import jakarta.persistence.Basic; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -17,30 +15,33 @@ import jakarta.persistence.Lob; import jakarta.persistence.Table; import org.hibernate.bytecode.enhance.spi.UnloadedClass; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; + import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; /** * @author Guillaume Smet */ -@TestForIssue(jiraKey = "HHH-12633") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-12633") +@DomainModel( + annotatedClasses = { + LazyInitializationWithoutInlineDirtyTrackingTest.File.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext( {EnhancerTestContext.class, LazyInitializationWithoutInlineDirtyTrackingTest.NoInlineDirtyTrackingContext.class} ) -public class LazyInitializationWithoutInlineDirtyTrackingTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ File.class }; - } +public class LazyInitializationWithoutInlineDirtyTrackingTest { @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { File file = new File(); file.setId( 1L ); file.setName( "file" ); @@ -49,7 +50,7 @@ public class LazyInitializationWithoutInlineDirtyTrackingTest extends BaseCoreFu s.persist( file ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { File file = s.find( File.class, 1L ); file.setBytes( new byte[]{ 1 } ); s.persist( file ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingAndInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingAndInheritanceTest.java index 0bf43430c7..0f48029531 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingAndInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingAndInheritanceTest.java @@ -7,18 +7,20 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import org.hibernate.Hibernate; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -27,26 +29,29 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-15090") -public class LazyLoadingAndInheritanceTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyLoadingAndInheritanceTest.Containing.class, + LazyLoadingAndInheritanceTest.Contained.class, + LazyLoadingAndInheritanceTest.ContainedExtended.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-15090") +public class LazyLoadingAndInheritanceTest { private Long containingID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Containing.class, Contained.class, ContainedExtended.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Containing containing = new Containing(); ContainedExtended contained = new ContainedExtended( "George" ); containing.contained = contained; @@ -57,8 +62,8 @@ public class LazyLoadingAndInheritanceTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Containing containing = s.load( Containing.class, containingID ); Contained contained = containing.contained; assertThat( contained ).isNotNull(); @@ -68,7 +73,7 @@ public class LazyLoadingAndInheritanceTest extends BaseCoreFunctionalTestCase { } @Entity(name = "Containing") - private static class Containing { + static class Containing { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -79,7 +84,7 @@ public class LazyLoadingAndInheritanceTest extends BaseCoreFunctionalTestCase { } @Entity(name = "Contained") - private static class Contained { + static class Contained { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -96,7 +101,7 @@ public class LazyLoadingAndInheritanceTest extends BaseCoreFunctionalTestCase { } @Entity(name = "ContainedExtended") - private static class ContainedExtended extends Contained { + static class ContainedExtended extends Contained { ContainedExtended() { } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingByEnhancerSetterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingByEnhancerSetterTest.java index c44ae28cc8..c490d8068c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingByEnhancerSetterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingByEnhancerSetterTest.java @@ -6,16 +6,17 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; @@ -28,7 +29,7 @@ import jakarta.persistence.Table; import java.util.HashMap; import java.util.Map; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * This tests issues HHH-11624. The fix is also for HHH-10747 (and HHH-11476) and is a change on the enhanced setter. @@ -36,31 +37,27 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10747" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyLoadingByEnhancerSetterTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10747" ) +@DomainModel( + annotatedClasses = { + LazyLoadingByEnhancerSetterTest.ItemField.class, LazyLoadingByEnhancerSetterTest.ItemProperty.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyLoadingByEnhancerSetterTest { private Item item, mergedItem; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ItemField.class, ItemProperty.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( Environment.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - - } - @Test - public void testField() { - doInHibernate( this::sessionFactory, s -> { + public void testField(SessionFactoryScope scope) { + scope.inTransaction( s -> { ItemField input = new ItemField(); input.name = "F"; input.parameters = new HashMap<>(); @@ -69,22 +66,22 @@ public class LazyLoadingByEnhancerSetterTest extends BaseCoreFunctionalTestCase s.persist( input ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // A parameters map is created with the class and is being compared to the persistent map (by the generated code) -- it shouldn't item = s.find( ItemField.class, "F" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { mergedItem = (Item) s.merge( item ); } ); - Assert.assertEquals( 2, mergedItem.getParameters().size() ); + assertEquals( 2, mergedItem.getParameters().size() ); } @Test @FailureExpected( jiraKey = "HHH-10747" ) - public void testProperty() { - doInHibernate( this::sessionFactory, s -> { + public void testProperty(SessionFactoryScope scope) { + scope.inTransaction( s -> { ItemProperty input = new ItemProperty(); input.setName( "P" ); Map parameters = new HashMap<>(); @@ -94,16 +91,16 @@ public class LazyLoadingByEnhancerSetterTest extends BaseCoreFunctionalTestCase s.persist( input ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // A parameters map is created with the class and is being compared to the persistent map (by the generated code) -- it shouldn't item = s.find( ItemProperty.class, "P" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { mergedItem = (Item) s.merge( item ); } ); - Assert.assertEquals( 2, mergedItem.getParameters().size() ); + assertEquals( 2, mergedItem.getParameters().size() ); } // --- // @@ -114,7 +111,7 @@ public class LazyLoadingByEnhancerSetterTest extends BaseCoreFunctionalTestCase @Entity @Table( name = "ITEM_F" ) - private static class ItemField implements Item { + static class ItemField implements Item { @Id @Column( nullable = false ) @@ -134,7 +131,7 @@ public class LazyLoadingByEnhancerSetterTest extends BaseCoreFunctionalTestCase @Entity @Table( name = "ITEM_P" ) - private static class ItemProperty implements Item { + static class ItemProperty implements Item { private String aName; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingIntegrationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingIntegrationTest.java index b4f23740ff..34d909b1c5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingIntegrationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingIntegrationTest.java @@ -9,15 +9,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.metamodel.CollectionClassification; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -31,34 +31,34 @@ import jakarta.persistence.Table; import java.util.ArrayList; import java.util.List; -import static org.hibernate.cfg.AvailableSettings.DEFAULT_LIST_SEMANTICS; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyLoadingIntegrationTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyLoadingIntegrationTest.Parent.class, LazyLoadingIntegrationTest.Child.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.DEFAULT_LIST_SEMANTICS, value = "BAG" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyLoadingIntegrationTest { private static final int CHILDREN_SIZE = 10; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - configuration.setProperty( DEFAULT_LIST_SEMANTICS, CollectionClassification.BAG ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { Child child = new Child(); @@ -72,8 +72,8 @@ public class LazyLoadingIntegrationTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); checkDirtyTracking( loadedChild ); @@ -89,7 +89,7 @@ public class LazyLoadingIntegrationTest extends BaseCoreFunctionalTestCase { loadedChildren.remove( loadedChild ); loadedParent.setChildren( loadedChildren ); - Assert.assertNull( loadedChild.parent ); + assertNull( loadedChild.parent ); } ); } @@ -97,7 +97,7 @@ public class LazyLoadingIntegrationTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -113,7 +113,7 @@ public class LazyLoadingIntegrationTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingTest.java index 01fe0e4ab8..a99b85b1b3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyLoadingTest.java @@ -24,49 +24,52 @@ import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertThat; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyLoadingTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyLoadingTest.Parent.class, LazyLoadingTest.Child.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyLoadingTest { private static final int CHILDREN_SIZE = 10; private Long parentID; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { Child child = new Child( "Child #" + i ); @@ -81,8 +84,8 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild, not( instanceOf( HibernateProxy.class ) ) ); assertThat( loadedChild, instanceOf( PersistentAttributeInterceptable.class ) ); @@ -120,7 +123,7 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -139,7 +142,7 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyManyToOneNoProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyManyToOneNoProxyTest.java index de72941eee..cfefc2575b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyManyToOneNoProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyManyToOneNoProxyTest.java @@ -1,15 +1,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.annotations.Proxy; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -18,38 +18,32 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyManyToOneNoProxyTest.User.class, + LazyManyToOneNoProxyTest.UserGroup.class, + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-16794") -public class LazyManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { +public class LazyManyToOneNoProxyTest { private static final String USER_1_NAME = "Andrea"; private static final String USER_2_NAME = "Fab"; private static final String USER_GROUP_1_NAME = "group1"; private static final String USER_GROUP_2_NAME = "group2"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - UserGroup.class, - }; + + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { UserGroup group1 = new UserGroup( 1l, USER_GROUP_1_NAME ); UserGroup group2 = new UserGroup( 2l, USER_GROUP_2_NAME ); @@ -66,10 +60,10 @@ public class LazyManyToOneNoProxyTest extends BaseCoreFunctionalTestCase { } @Test - public void testSelect() { - inTransaction( + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( session -> { - SQLStatementInspector statementInspector = statementInspector(); + SQLStatementInspector statementInspector = statementInspector( scope ); statementInspector.clear(); User user = session.getReference( User.class, 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java index d46849d87d..d64f2d9613 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToManyWithEqualsImplementationTest.java @@ -8,13 +8,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -28,24 +30,22 @@ import java.util.HashSet; import java.util.Objects; import java.util.Set; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -@TestForIssue(jiraKey = "HHH-13380") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyOneToManyWithEqualsImplementationTest - extends BaseEntityManagerFunctionalTestCase { +@JiraKey("HHH-13380") +@DomainModel( + annotatedClasses = { + LazyOneToManyWithEqualsImplementationTest.Person.class, LazyOneToManyWithEqualsImplementationTest.Course.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyOneToManyWithEqualsImplementationTest { private Long personId; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class, Course.class }; - } - - @Before - public void setUp() { - doInJPA( this::entityManagerFactory, entityManager -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { Person p = new Person(); entityManager.persist(p); personId = p.getId(); @@ -62,12 +62,12 @@ public class LazyOneToManyWithEqualsImplementationTest @Test - public void testRetrievalOfOneToMany() { - doInJPA(this::entityManagerFactory, entityManager -> { + public void testRetrievalOfOneToMany(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { Person p = entityManager.find( Person.class, personId ); Set courses = p.getCourses(); - assertEquals( courses.size(), 2 ); + Assertions.assertEquals( courses.size(), 2 ); }); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTest.java index 01c6a49952..b9ee377b03 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyOnEnhancedEntityTest.java @@ -13,14 +13,15 @@ import org.hibernate.event.spi.EventType; import org.hibernate.event.spi.LoadEvent; import org.hibernate.event.spi.LoadEventListener; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -30,26 +31,25 @@ import jakarta.persistence.Id; import jakarta.persistence.OneToOne; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; - /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10922" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-10922" ) +@DomainModel( + annotatedClasses = { + LazyProxyOnEnhancedEntityTest.Parent.class, LazyProxyOnEnhancedEntityTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext( {EnhancerTestContext.class, LazyProxyOnEnhancedEntityTest.NoLazyLoadingContext.class} ) -public class LazyProxyOnEnhancedEntityTest extends BaseCoreFunctionalTestCase { +public class LazyProxyOnEnhancedEntityTest { private Long parentID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Before - public void prepare() { - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { Child c = new Child(); em.persist( c ); @@ -61,11 +61,11 @@ public class LazyProxyOnEnhancedEntityTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService( EventListenerRegistry.class ); + public void test(SessionFactoryScope scope) { + EventListenerRegistry registry = scope.getSessionFactory().getServiceRegistry().getService( EventListenerRegistry.class ); registry.prependListeners( EventType.LOAD, new ImmediateLoadTrap() ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { em.find( Parent.class, parentID ); @@ -87,7 +87,7 @@ public class LazyProxyOnEnhancedEntityTest extends BaseCoreFunctionalTestCase { @Entity(name = "Parent") @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -112,7 +112,7 @@ public class LazyProxyOnEnhancedEntityTest extends BaseCoreFunctionalTestCase { @Entity(name = "Child") @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyWithCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyWithCollectionTest.java index df0995a97e..3e205bd644 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyWithCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyProxyWithCollectionTest.java @@ -9,12 +9,13 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import java.util.HashSet; import java.util.Set; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.Jira; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -27,25 +28,26 @@ import jakarta.persistence.Table; import jakarta.persistence.Version; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; /** * @author Christian Beikov */ @Jira( "https://hibernate.atlassian.net/browse/HHH-14619" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyProxyWithCollectionTest.Parent.class, + LazyProxyWithCollectionTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyProxyWithCollectionTest { private Long childId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class, Child.class}; - } - - @Before - public void prepare() { - doInJPA( this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { Child c = new Child(); em.persist( c ); childId = c.getId(); @@ -53,8 +55,8 @@ public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { } @Test - public void testReference() { - doInJPA( this::sessionFactory, em -> { + public void testReference(SessionFactoryScope scope) { + scope.inTransaction( em -> { Child child = em.getReference( Child.class, childId ); Parent parent = new Parent(); parent.child = child; @@ -65,8 +67,8 @@ public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { } @Test - public void testLazyCollection() { - doInJPA( this::sessionFactory, em -> { + public void testLazyCollection(SessionFactoryScope scope) { + scope.inTransaction( em -> { Child child = em.find( Child.class, childId ); Parent parent = new Parent(); parent.child = child; @@ -79,22 +81,18 @@ public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { @Test @Jira( "https://hibernate.atlassian.net/browse/HHH-17750" ) - public void testMerge() { - final Child child = doInJPA( this::sessionFactory, em -> { - return em.find( Child.class, childId ); - } ); + public void testMerge(SessionFactoryScope scope) { + final Child child = scope.fromTransaction( em -> em.find( Child.class, childId ) ); - final Parent parent = doInJPA( this::sessionFactory, em -> { + final Parent parent = scope.fromTransaction( em -> { Parent p = new Parent(); p.setChild( child ); return em.merge( p ); } ); - doInJPA( this::sessionFactory, em -> { - em.merge( parent ); - } ); + scope.inTransaction( em -> em.merge( parent ) ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { assertThat( em.find( Parent.class, parent.getId() ).getChild().getId() ).isEqualTo( child.getId() ); } ); } @@ -103,7 +101,7 @@ public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -127,7 +125,7 @@ public class LazyProxyWithCollectionTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyToOneJoinOnNonPrimaryKeyColumnTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyToOneJoinOnNonPrimaryKeyColumnTest.java index f05d8706ec..b4ce6fe923 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyToOneJoinOnNonPrimaryKeyColumnTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyToOneJoinOnNonPrimaryKeyColumnTest.java @@ -8,14 +8,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import static org.assertj.core.api.Assertions.assertThat; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -24,19 +25,20 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyToOneJoinOnNonPrimaryKeyColumnTest.EntityA.class, LazyToOneJoinOnNonPrimaryKeyColumnTest.EntityB.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) @JiraKey("HHH-17075") -public class LazyToOneJoinOnNonPrimaryKeyColumnTest extends BaseNonConfigCoreFunctionalTestCase { +public class LazyToOneJoinOnNonPrimaryKeyColumnTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1 ); entityA.setMyUniqueKey( "someValue" ); s.persist( entityA ); @@ -49,17 +51,17 @@ public class LazyToOneJoinOnNonPrimaryKeyColumnTest extends BaseNonConfigCoreFun } ); } - @After - public void tearDown() { - inTransaction( s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createMutationQuery( "delete entityb" ).executeUpdate(); s.createMutationQuery( "delete entitya" ).executeUpdate(); } ); } @Test - public void testLazyLoading() { - inTransaction( s -> { + public void testLazyLoading(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityB entityB = s.get( EntityB.class, 2 ); // Trigger lazy loading assertThat( entityB.getMyAssociation() ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultiPathCascadeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultiPathCascadeTest.java index 007447b4ee..3766062d6a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultiPathCascadeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultiPathCascadeTest.java @@ -12,37 +12,38 @@ import org.hibernate.orm.test.cascade.G; import org.hibernate.orm.test.cascade.H; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Ovidiu Feodorov * @author Gail Badner */ -@RunWith(BytecodeEnhancerRunner.class) -@CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { - - @Override - protected String[] getOrmXmlFiles() { - return new String[] { +@DomainModel( + xmlMappings = { "org/hibernate/orm/test/cascade/MultiPathCascade.hbm.xml" - }; - } + } +) +@SessionFactory +@BytecodeEnhanced +@CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) +public class MultiPathCascadeTest { - @After - public void cleanupTest() { - inTransaction( + + @AfterEach + public void cleanupTest(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from A" ); session.createQuery( "delete from G" ); @@ -52,12 +53,12 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { } @Test - public void testMultiPathMergeModifiedDetached() { + public void testMultiPathMergeModifiedDetached(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -65,21 +66,21 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - inTransaction( + scope.inTransaction( session -> session.merge( a ) ); - verifyModifications( a.getId() ); + verifyModifications( scope, a.getId() ); } @Test - public void testMultiPathMergeModifiedDetachedIntoProxy() { + public void testMultiPathMergeModifiedDetachedIntoProxy(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -87,25 +88,25 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - inTransaction( + scope.inTransaction( session -> { A aLoaded = session.load( A.class, new Long( a.getId() ) ); - assertTrue( aLoaded instanceof HibernateProxy ); + assertInstanceOf( HibernateProxy.class, aLoaded ); assertSame( aLoaded, session.merge( a ) ); } ); - verifyModifications( a.getId() ); + verifyModifications( scope, a.getId() ); } @Test - public void testMultiPathUpdateModifiedDetached() { + public void testMultiPathUpdateModifiedDetached(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -113,27 +114,27 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - inTransaction( + scope.inTransaction( session -> session.update( a ) ); - verifyModifications( a.getId() ); + verifyModifications( scope, a.getId() ); } @Test - public void testMultiPathGetAndModify() { + public void testMultiPathGetAndModify(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); - inTransaction( + scope.inTransaction( session -> { A result = session.get( A.class, new Long( a.getId() ) ); modifyEntity( result ); @@ -141,17 +142,17 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { ); // retrieve the previously saved instance from the database, and update it - verifyModifications( a.getId() ); + verifyModifications( scope, a.getId() ); } @Test - public void testMultiPathMergeNonCascadedTransientEntityInCollection() { + public void testMultiPathMergeNonCascadedTransientEntityInCollection(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -159,12 +160,12 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - A merged = fromTransaction( + A merged = scope.fromTransaction( session -> (A) session.merge( a ) ); - verifyModifications( merged.getId() ); + verifyModifications( scope, merged.getId() ); // add a new (transient) G to collection in h // there is no cascade from H to the collection, so this should fail when merged @@ -177,7 +178,7 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { gNew.getHs().add( h ); h.getGs().add( gNew ); - inSession( + scope.inSession( session -> { session.beginTransaction(); try { @@ -197,12 +198,12 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { } @Test - public void testMultiPathMergeNonCascadedTransientEntityInOneToOne() { + public void testMultiPathMergeNonCascadedTransientEntityInOneToOne(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -210,12 +211,12 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - A merged = fromTransaction( + A merged = scope.fromTransaction( session -> (A) session.merge( a ) ); - verifyModifications( merged.getId() ); + verifyModifications( scope, merged.getId() ); // change the one-to-one association from g to be a new (transient) A // there is no cascade from G to A, so this should fail when merged @@ -226,7 +227,7 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { g.setA( aNew ); aNew.setG( g ); - inSession( + scope.inSession( session -> { session.beginTransaction(); try { @@ -246,13 +247,13 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { } @Test - public void testMultiPathMergeNonCascadedTransientEntityInManyToOne() { + public void testMultiPathMergeNonCascadedTransientEntityInManyToOne(SessionFactoryScope scope) { // persist a simple A in the database A a = new A(); a.setData( "Anna" ); - inTransaction( + scope.inTransaction( session -> session.save( a ) ); @@ -260,12 +261,12 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { // modify detached entity modifyEntity( a ); - A merged = fromTransaction( + A merged = scope.fromTransaction( session -> (A) session.merge( a ) ); - verifyModifications( a.getId() ); + verifyModifications( scope, a.getId() ); // change the many-to-one association from h to be a new (transient) A // there is no cascade from H to A, so this should fail when merged @@ -276,7 +277,7 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { aNew.setData( "Alice" ); aNew.addH( h ); - inSession( + scope.inSession( session -> { session.beginTransaction(); try { @@ -315,8 +316,8 @@ public class MultiPathCascadeTest extends BaseCoreFunctionalTestCase { h.getGs().add( g ); } - private void verifyModifications(long aId) { - inTransaction( + private void verifyModifications(SessionFactoryScope scope, long aId) { + scope.inTransaction( session -> { // retrieve the A object and check it A a = session.get( A.class, new Long( aId ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFtechModeSelectTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFetchModeSelectTest.java similarity index 53% rename from hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFtechModeSelectTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFetchModeSelectTest.java index 420cf86933..63670fa7b0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFtechModeSelectTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupFetchModeSelectTest.java @@ -12,16 +12,15 @@ import java.util.List; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; @@ -29,39 +28,33 @@ import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Christian Beikov */ -@RunWith(BytecodeEnhancerRunner.class) -public class MultipleBagsInLazyFetchGroupFtechModeSelectTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + MultipleBagsInLazyFetchGroupFetchModeSelectTest.StringsEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MultipleBagsInLazyFetchGroupFetchModeSelectTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { StringsEntity.class }; - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @After - public void tearDown() { - doInJPA( this::sessionFactory, em -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( em -> { em.createQuery( "delete from StringsEntity" ).executeUpdate(); } ); } - @Before - public void prepare() { - assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + @BeforeEach + public void prepare(SessionFactoryScope scope) { + assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.text = "abc"; @@ -72,9 +65,9 @@ public class MultipleBagsInLazyFetchGroupFtechModeSelectTest extends BaseNonConf } @Test - public void testGetReference() { - Assertions.assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, entityManager -> { + public void testGetReference(SessionFactoryScope scope) { + Assertions.assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.getReference( StringsEntity.class, 1L ); assertEquals( 3, entity.someStrings.size() ); assertEquals( 4, entity.someStrings2.size() ); @@ -82,9 +75,9 @@ public class MultipleBagsInLazyFetchGroupFtechModeSelectTest extends BaseNonConf } @Test - public void testFind() { - Assertions.assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, entityManager -> { + public void testFind(SessionFactoryScope scope) { + Assertions.assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.find( StringsEntity.class, 1L ); assertEquals( 3, entity.someStrings.size() ); assertEquals( 4, entity.someStrings2.size() ); @@ -96,7 +89,7 @@ public class MultipleBagsInLazyFetchGroupFtechModeSelectTest extends BaseNonConf @Entity(name = "StringsEntity") @Table(name = "STRINGS_ENTITY") - private static class StringsEntity { + static class StringsEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupTest.java index eb04b314f9..800b8e5c09 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsInLazyFetchGroupTest.java @@ -15,41 +15,34 @@ import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Assertions; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Christian Beikov */ -@RunWith( BytecodeEnhancerRunner.class ) -public class MultipleBagsInLazyFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + MultipleBagsInLazyFetchGroupTest.StringsEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MultipleBagsInLazyFetchGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } + @BeforeEach + public void prepare(SessionFactoryScope scope) { + assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @Before - public void prepare() { - assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.text = "abc"; @@ -60,9 +53,9 @@ public class MultipleBagsInLazyFetchGroupTest extends BaseNonConfigCoreFunctiona } @Test - public void test() { - Assertions.assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + Assertions.assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.getReference( StringsEntity.class, 1L ); assertEquals( 3, entity.someStrings.size() ); assertEquals( 3, entity.someStrings2.size() ); @@ -73,7 +66,7 @@ public class MultipleBagsInLazyFetchGroupTest extends BaseNonConfigCoreFunctiona @Entity @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + static class StringsEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsNotInLazyFetchGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsNotInLazyFetchGroupTest.java index d3c4be136b..0622783b25 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsNotInLazyFetchGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/MultipleBagsNotInLazyFetchGroupTest.java @@ -10,13 +10,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.hibernate.boot.SessionFactoryBuilder; - -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; @@ -24,32 +23,26 @@ import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Christian Beikov */ -@RunWith( BytecodeEnhancerRunner.class ) -public class MultipleBagsNotInLazyFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + MultipleBagsNotInLazyFetchGroupTest.StringsEntity.class + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced +public class MultipleBagsNotInLazyFetchGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{StringsEntity.class}; - } + @BeforeEach + public void prepare(SessionFactoryScope scope) { + assertFalse( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyCollectionsInDefaultFetchGroup( false ); - } - - @Before - public void prepare() { - assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); - - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { StringsEntity entity = new StringsEntity(); entity.id = 1L; entity.text = "abc"; @@ -60,8 +53,8 @@ public class MultipleBagsNotInLazyFetchGroupTest extends BaseNonConfigCoreFuncti } @Test - public void test() { - doInJPA( this::sessionFactory, entityManager -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { StringsEntity entity = entityManager.getReference( StringsEntity.class, 1L ); assertEquals( 3, entity.someStrings.size() ); assertEquals( 3, entity.someStrings2.size() ); @@ -71,8 +64,8 @@ public class MultipleBagsNotInLazyFetchGroupTest extends BaseNonConfigCoreFuncti // --- // @Entity - @Table( name = "STRINGS_ENTITY" ) - private static class StringsEntity { + @Table(name = "STRINGS_ENTITY") + static class StringsEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/NaturalIdInUninitializedAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/NaturalIdInUninitializedAssociationTest.java index 3721313517..e2da4ee978 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/NaturalIdInUninitializedAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/NaturalIdInUninitializedAssociationTest.java @@ -16,35 +16,51 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.annotations.NaturalId; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13607" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-13607" ) +@DomainModel( + annotatedClasses = { + NaturalIdInUninitializedAssociationTest.AnEntity.class, + NaturalIdInUninitializedAssociationTest.EntityMutableNaturalId.class, + NaturalIdInUninitializedAssociationTest.EntityImmutableNaturalId.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true, extendedEnhancement = true ) -public class NaturalIdInUninitializedAssociationTest extends BaseNonConfigCoreFunctionalTestCase { +public class NaturalIdInUninitializedAssociationTest { @Test - public void testLoad() { - inTransaction( + public void testLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { final AnEntity e = session.byId( AnEntity.class ).load(3 ); assertTrue( Hibernate.isInitialized( e ) ); @@ -61,8 +77,8 @@ public class NaturalIdInUninitializedAssociationTest extends BaseNonConfigCoreFu } @Test - public void testGetReference() { - inTransaction( + public void testGetReference(SessionFactoryScope scope) { + scope.inTransaction( session -> { final AnEntity e = session.byId( AnEntity.class ).getReference( 3 ); assertFalse( Hibernate.isInitialized( e ) ); @@ -79,7 +95,7 @@ public class NaturalIdInUninitializedAssociationTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { final AnEntity e = session.get( AnEntity.class, 3 ); assertEquals( "mutable name", e.entityMutableNaturalId.name ); @@ -87,31 +103,9 @@ public class NaturalIdInUninitializedAssociationTest extends BaseNonConfigCoreFu ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( AnEntity.class ); - sources.addAnnotatedClass( EntityMutableNaturalId.class ); - sources.addAnnotatedClass( EntityImmutableNaturalId.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityMutableNaturalId entityMutableNaturalId = new EntityMutableNaturalId( 1, "mutable name" ); EntityImmutableNaturalId entityImmutableNaturalId = new EntityImmutableNaturalId( 2, "immutable name" ); @@ -124,9 +118,9 @@ public class NaturalIdInUninitializedAssociationTest extends BaseNonConfigCoreFu ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.delete( session.get( AnEntity.class, 3 ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ReferenceLoadedEnhancedEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ReferenceLoadedEnhancedEntityTest.java index f47ee952d1..35fefdbcbb 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ReferenceLoadedEnhancedEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/ReferenceLoadedEnhancedEntityTest.java @@ -8,23 +8,25 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy; import org.hibernate.ObjectNotFoundException; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.Test; /** * Verifies that an object loaded via getReference and with @@ -34,23 +36,24 @@ import static org.junit.jupiter.api.Assertions.assertThrows; * doesn't exist in the database and a property is being read * forcing initialization. */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ReferenceLoadedEnhancedEntityTest.Country.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10" ), + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-16669") -public class ReferenceLoadedEnhancedEntityTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Country.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, 10 ); - } +public class ReferenceLoadedEnhancedEntityTest { @Test - public void referenceLoadAlwaysWorks() { - doInHibernate( this::sessionFactory, s -> { + public void referenceLoadAlwaysWorks(SessionFactoryScope scope) { + scope.inTransaction( s -> { //Materialize a reference to an object which doesn't exist final Country entity = s.getReference( Country.class, 1L ); //should be fine.. @@ -58,59 +61,59 @@ public class ReferenceLoadedEnhancedEntityTest extends BaseCoreFunctionalTestCas } @Test - public void referenceNotExisting() { + public void referenceNotExisting(SessionFactoryScope scope) { Exception exception = assertThrows( ObjectNotFoundException.class, - () -> doInHibernate( this::sessionFactory, s -> { + () -> scope.inTransaction( s -> { //Materialize a reference to an object which doesn't exist final Country entity = s.getReference( Country.class, 1L ); //This should fail: final String name = entity.getName(); //Ensure we failed at the previous line: - Assert.fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); + fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); } ) ); assertNotNull( exception ); } @Test - public void referenceNotExisting2() { + public void referenceNotExisting2(SessionFactoryScope scope) { Exception exception = assertThrows( ObjectNotFoundException.class, - () -> doInHibernate( this::sessionFactory, s -> { + () -> scope.inTransaction( s -> { //Materialize a reference to an object which doesn't exist final Country entity = s.getReference( Country.class, 1L ); final Country entity2 = s.getReference( Country.class, 2L ); //This should fail: final String name = entity.getName(); //Ensure we failed at the previous line: - Assert.fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); + fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); } ) ); assertNotNull( exception ); } @Test - public void referenceNotExistingFieldAccess() { + public void referenceNotExistingFieldAccess(SessionFactoryScope scope) { Exception exception = assertThrows( ObjectNotFoundException.class, - () -> doInHibernate( this::sessionFactory, s -> { + () -> scope.inTransaction( s -> { //Materialize a reference to an object which doesn't exist final Country entity = s.getReference( Country.class, 1L ); //This should fail: final String name = entity.name; //Ensure we failed at the previous line: - Assert.fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); + fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); } ) ); assertNotNull( exception ); } @Test - public void referenceNotExistingFieldAccess2() { + public void referenceNotExistingFieldAccess2(SessionFactoryScope scope) { Exception exception = assertThrows( ObjectNotFoundException.class, - () -> doInHibernate( this::sessionFactory, s -> { + () -> scope.inTransaction( s -> { //Materialize a reference to an object which doesn't exist final Country entity = s.getReference( Country.class, 1L ); final Country entity2 = s.getReference( Country.class, 2L ); //This should fail: final String name = entity.name; //Ensure we failed at the previous line: - Assert.fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); + fail( "Should have thrown an ObjectNotFoundException exception before reaching this point" ); } ) ); assertNotNull( exception ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/StatelessQueryScrollingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/StatelessQueryScrollingTest.java index 362b1e64e1..c6927f6850 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/StatelessQueryScrollingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/StatelessQueryScrollingTest.java @@ -21,31 +21,40 @@ import org.hibernate.Hibernate; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.StatelessSession; -import org.hibernate.boot.MetadataSources; -import org.hibernate.dialect.DB2Dialect; -import org.hibernate.dialect.DerbyDialect; import org.hibernate.query.Query; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith(BytecodeEnhancerRunner.class) -public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + StatelessQueryScrollingTest.Task.class, + StatelessQueryScrollingTest.User.class, + StatelessQueryScrollingTest.Resource.class, + StatelessQueryScrollingTest.Product.class, + StatelessQueryScrollingTest.Producer.class, + StatelessQueryScrollingTest.Vendor.class + } +) +@SessionFactory +@BytecodeEnhanced +public class StatelessQueryScrollingTest { @Test - public void testDynamicFetchScMapsIdProxyBidirectionalTestroll() { + public void testDynamicFetchScMapsIdProxyBidirectionalTestroll(SessionFactoryScope scope) { ScrollableResults scrollableResults = null; - final StatelessSession statelessSession = sessionFactory().openStatelessSession(); + final StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession(); try { final Query query = statelessSession.createQuery( "from Task t join fetch t.resource join fetch t.user" ); scrollableResults = query.scroll( ScrollMode.FORWARD_ONLY ); @@ -66,9 +75,9 @@ public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTest } @Test - public void testDynamicFetchCollectionScroll() { + public void testDynamicFetchCollectionScroll(SessionFactoryScope scope) { ScrollableResults scrollableResults = null; - StatelessSession statelessSession = sessionFactory().openStatelessSession(); + StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession(); statelessSession.beginTransaction(); try { @@ -96,9 +105,9 @@ public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTest } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { Date now = new Date(); User me = new User( "me" ); @@ -123,7 +132,7 @@ public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTest } ); - inTransaction( + scope.inTransaction( session -> { Producer p1 = new Producer( 1, "Acme" ); Producer p2 = new Producer( 2, "ABC" ); @@ -148,9 +157,9 @@ public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTest ); } - @After - public void deleteTestData() { - inTransaction( + @AfterEach + public void deleteTestData(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete Task" ).executeUpdate(); s.createQuery( "delete Resource" ).executeUpdate(); @@ -163,18 +172,6 @@ public class StatelessQueryScrollingTest extends BaseNonConfigCoreFunctionalTest ); } - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Task.class ); - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Resource.class ); - sources.addAnnotatedClass( Product.class ); - sources.addAnnotatedClass( Producer.class ); - sources.addAnnotatedClass( Vendor.class ); - } - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Collection fetch scrolling diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/backref/BackrefCompositeMapKeyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/backref/BackrefCompositeMapKeyTest.java index 733681a178..3ffc704851 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/backref/BackrefCompositeMapKeyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/backref/BackrefCompositeMapKeyTest.java @@ -16,15 +16,16 @@ import org.hibernate.orm.test.collection.backref.map.compkey.MapKey; import org.hibernate.orm.test.collection.backref.map.compkey.Part; import org.hibernate.orm.test.collection.backref.map.compkey.Product; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -33,20 +34,19 @@ import static org.junit.Assert.assertTrue; * * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class ) -@CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) -public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { - - @Override - protected String[] getOrmXmlFiles() { - return new String[] { +@DomainModel( + xmlMappings = { "org/hibernate/orm/test/collection/backref/map/compkey/Mappings.hbm.xml" - }; - } + } +) +@SessionFactory +@BytecodeEnhanced +@CustomEnhancementContext({ NoDirtyCheckingContext.class, DirtyCheckEnhancementContext.class }) +public class BackrefCompositeMapKeyTest { @Test - public void testOrphanDeleteOnDelete() { - inTransaction( + public void testOrphanDeleteOnDelete(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product( "Widget" ); Part part = new Part( "Widge", "part if a Widget" ); @@ -63,18 +63,18 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { - assertNull( "Orphan 'Widge' was not deleted", session.get( Part.class, "Widge" ) ); - assertNull( "Orphan 'Get' was not deleted", session.get( Part.class, "Get" ) ); - assertNull( "Orphan 'Widget' was not deleted", session.get( Product.class, "Widget" ) ); + assertNull( session.get( Part.class, "Widge" ), "Orphan 'Widge' was not deleted" ); + assertNull( session.get( Part.class, "Get" ), "Orphan 'Get' was not deleted" ); + assertNull( session.get( Product.class, "Widget" ), "Orphan 'Widget' was not deleted" ); } ); } @Test - public void testOrphanDeleteAfterPersist() { - inTransaction( + public void testOrphanDeleteAfterPersist(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product( "Widget" ); Part part = new Part( "Widge", "part if a Widget" ); @@ -88,15 +88,15 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> session.delete( session.get( Product.class, "Widget" ) ) ); } @Test - public void testOrphanDeleteAfterPersistAndFlush() { - inTransaction( + public void testOrphanDeleteAfterPersistAndFlush(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product( "Widget" ); Part part = new Part( "Widge", "part if a Widget" ); @@ -111,7 +111,7 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -122,10 +122,10 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } @Test - public void testOrphanDeleteAfterLock() { + public void testOrphanDeleteAfterLock(SessionFactoryScope scope) { Product prod = new Product( "Widget" ); MapKey mapKey = new MapKey( "Top" ); - inTransaction( + scope.inTransaction( session -> { Part part = new Part( "Widge", "part if a Widget" ); prod.getParts().put( mapKey, part ); @@ -136,14 +136,14 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { ); - inTransaction( + scope.inTransaction( session -> { session.lock( prod, LockMode.READ ); prod.getParts().remove( mapKey ); } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -153,10 +153,10 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } @Test - public void testOrphanDeleteOnSaveOrUpdate() { + public void testOrphanDeleteOnSaveOrUpdate(SessionFactoryScope scope) { Product prod = new Product( "Widget" ); MapKey mapKey = new MapKey( "Top" ); - inTransaction( + scope.inTransaction( session -> { Part part = new Part( "Widge", "part if a Widget" ); prod.getParts().put( mapKey, part ); @@ -168,12 +168,12 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { prod.getParts().remove( mapKey ); - inTransaction( + scope.inTransaction( session -> session.saveOrUpdate( prod ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -183,10 +183,10 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } @Test - public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() { + public void testOrphanDeleteOnSaveOrUpdateAfterSerialization(SessionFactoryScope scope) { Product prod = new Product( "Widget" ); MapKey mapKey = new MapKey( "Top" ); - inTransaction( + scope.inTransaction( session -> { Part part = new Part( "Widge", "part if a Widget" ); prod.getParts().put( mapKey, part ); @@ -200,12 +200,12 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { Product cloned = (Product) SerializationHelper.clone( prod ); - inTransaction( + scope.inTransaction( session -> session.saveOrUpdate( cloned ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -215,9 +215,9 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } @Test - public void testOrphanDelete() { + public void testOrphanDelete(SessionFactoryScope scope) { MapKey mapKey = new MapKey( "Top" ); - inTransaction( + scope.inTransaction( session -> { Product prod = new Product( "Widget" ); Part part = new Part( "Widge", "part if a Widget" ); @@ -229,11 +229,11 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { ); - SessionFactoryImplementor sessionFactory = sessionFactory(); + SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); sessionFactory.getCache().evictEntityData( Product.class ); sessionFactory.getCache().evictEntityData( Part.class ); - inTransaction( + scope.inTransaction( session -> { Product prod = session.get( Product.class, "Widget" ); assertTrue( Hibernate.isInitialized( prod.getParts() ) ); @@ -246,7 +246,7 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { sessionFactory.getCache().evictEntityData( Product.class ); sessionFactory.getCache().evictEntityData( Part.class ); - inTransaction( + scope.inTransaction( session -> { Product prod = session.get( Product.class, "Widget" ); assertTrue( Hibernate.isInitialized( prod.getParts() ) ); @@ -258,10 +258,10 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { } @Test - public void testOrphanDeleteOnMerge() { + public void testOrphanDeleteOnMerge(SessionFactoryScope scope) { Product prod = new Product( "Widget" ); MapKey mapKey = new MapKey( "Top" ); - inTransaction( + scope.inTransaction( session -> { Part part = new Part( "Widge", "part if a Widget" ); prod.getParts().put( mapKey, part ); @@ -274,12 +274,12 @@ public class BackrefCompositeMapKeyTest extends BaseCoreFunctionalTestCase { prod.getParts().remove( mapKey ); - inTransaction( + scope.inTransaction( session -> session.merge( prod ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/EagerAndLazyBasicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/EagerAndLazyBasicUpdateTest.java index 3110fe8a44..8b45c138d5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/EagerAndLazyBasicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/EagerAndLazyBasicUpdateTest.java @@ -6,22 +6,21 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic; -import org.hibernate.cfg.Configuration; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -30,38 +29,34 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -@RunWith(BytecodeEnhancerRunner.class) -@CustomEnhancementContext( {EnhancerTestContext.class, NoDirtyCheckingContext.class} ) -@TestForIssue(jiraKey = { "HHH-15634", "HHH-16049" }) -public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + EagerAndLazyBasicUpdateTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +@CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class }) +@JiraKey("HHH-15634") +@JiraKey("HHH-16049") +public class EagerAndLazyBasicUpdateTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - private void initNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); s.persist( entity ); entityId = entity.getId(); } ); } - private void initNonNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNonNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.setEagerProperty( "eager_initial" ); entity.setLazyProperty1( "lazy1_initial" ); @@ -71,32 +66,32 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } ); } - @Before - public void clearStatementInspector() { - statementInspector().clear(); + @BeforeEach + public void clearStatementInspector(SessionFactoryScope scope) { + statementInspector( scope ).clear(); } @Test - public void updateOneLazyProperty_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneLazyProperty_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateOneLazyProperty_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneLazyProperty_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -106,13 +101,13 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneLazyProperty_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneLazyProperty_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -122,26 +117,26 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneLazyProperty_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneLazyProperty_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); } ); // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateOneLazyProperty_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneLazyProperty_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); @@ -151,25 +146,25 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerProperty_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerProperty_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( null ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateOneEagerProperty_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerProperty_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update", entity.getEagerProperty() ); @@ -179,13 +174,13 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerProperty_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerProperty_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update", entity.getEagerProperty() ); @@ -195,25 +190,25 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerProperty_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerProperty_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_initial" ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateOneEagerProperty_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerProperty_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getEagerProperty() ); @@ -223,9 +218,9 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerPropertyAndOneLazyProperty_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerPropertyAndOneLazyProperty_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( null ); entity.setLazyProperty1( null ); @@ -233,18 +228,18 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerPropertyAndOneLazyProperty_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update" ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update", entity.getEagerProperty() ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -254,14 +249,14 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update" ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update", entity.getEagerProperty() ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -271,27 +266,27 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( entity.getEagerProperty() ); entity.setLazyProperty1( entity.getLazyProperty1() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateOneEagerPropertyAndOneLazyProperty_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( null ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getEagerProperty() ); assertNull( entity.getLazyProperty1() ); @@ -301,9 +296,9 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); @@ -311,18 +306,18 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateAllLazyProperties_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); entity.setLazyProperty2( "lazy2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); assertEquals( "lazy2_update", entity.getLazyProperty2() ); @@ -332,14 +327,14 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); entity.setLazyProperty2( "lazy2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); assertEquals( "lazy2_update", entity.getLazyProperty2() ); @@ -349,27 +344,27 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( entity.getLazyProperty1() ); entity.setLazyProperty2( entity.getLazyProperty2() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateAllLazyProperties_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); @@ -380,7 +375,7 @@ public class EagerAndLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicFieldAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicFieldAccessTest.java index ff03959b4d..9332e8246c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicFieldAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicFieldAccessTest.java @@ -7,15 +7,16 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Access; import jakarta.persistence.AccessType; @@ -28,34 +29,35 @@ import jakarta.persistence.Table; import static org.hibernate.Hibernate.isPropertyInitialized; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyBasicFieldAccessTest.LazyEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyBasicFieldAccessTest { private LazyEntity entity; private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{LazyEntity.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.description = "desc"; s.persist( entity ); @@ -64,20 +66,20 @@ public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { } @Test - public void testAttachedUpdate() { - doInHibernate( this::sessionFactory, s -> { + public void testAttachedUpdate(SessionFactoryScope scope) { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); checkDirtyTracking( entity ); assertEquals( "desc", entity.description ); assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); entity.description = "desc1"; checkDirtyTracking( entity, "description" ); @@ -86,26 +88,26 @@ public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc1", entity.description ); } ); } @Test - @TestForIssue(jiraKey = "HHH-11882") - public void testDetachedUpdate() { - doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-11882") + public void testDetachedUpdate(SessionFactoryScope scope) { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); checkDirtyTracking( entity ); assertEquals( "desc", entity.description ); assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity.description = "desc1"; s.update( entity ); @@ -115,12 +117,12 @@ public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc1", entity.description ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity.description = "desc2"; LazyEntity mergedEntity = (LazyEntity) s.merge( entity ); @@ -131,7 +133,7 @@ public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( mergedEntity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc2", entity.description ); } ); @@ -142,7 +144,7 @@ public class LazyBasicFieldAccessTest extends BaseCoreFunctionalTestCase { @Entity @Access( AccessType.FIELD ) @Table( name = "LAZY_PROPERTY_ENTITY" ) - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicPropertyAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicPropertyAccessTest.java index c6871b3330..75772f7d90 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicPropertyAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/LazyBasicPropertyAccessTest.java @@ -7,15 +7,16 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -26,33 +27,34 @@ import jakarta.persistence.Table; import static org.hibernate.Hibernate.isPropertyInitialized; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyBasicPropertyAccessTest.LazyEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false"), + @Setting(name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyBasicPropertyAccessTest { private LazyEntity entity; private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{LazyEntity.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.setDescription( "desc" ); s.persist( entity ); @@ -61,20 +63,20 @@ public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { } @Test - public void testAttachedUpdate() { - doInHibernate( this::sessionFactory, s -> { + public void testAttachedUpdate(SessionFactoryScope scope) { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); checkDirtyTracking( entity ); assertEquals( "desc", entity.getDescription() ); assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); entity.setDescription( "desc1" ); checkDirtyTracking( entity, "description" ); @@ -83,26 +85,26 @@ public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc1", entity.getDescription() ); } ); } @Test - @TestForIssue(jiraKey = "HHH-11882") - public void testDetachedUpdate() { - doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-11882") + public void testDetachedUpdate(SessionFactoryScope scope) { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); - Assert.assertFalse( isPropertyInitialized( entity, "description" ) ); + assertFalse( isPropertyInitialized( entity, "description" ) ); checkDirtyTracking( entity ); assertEquals( "desc", entity.getDescription() ); assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity.setDescription( "desc1" ); s.update( entity ); @@ -112,12 +114,12 @@ public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( entity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc1", entity.getDescription() ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity.setDescription( "desc2" ); LazyEntity mergedEntity = (LazyEntity) s.merge( entity ); @@ -128,7 +130,7 @@ public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( mergedEntity, "description" ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { entity = s.get( LazyEntity.class, entityId ); assertEquals( "desc2", entity.getDescription() ); } ); @@ -138,7 +140,7 @@ public class LazyBasicPropertyAccessTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "LAZY_FIELD_ENTITY" ) - private static class LazyEntity { + static class LazyEntity { Long id; String description; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyEagerBasicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyEagerBasicUpdateTest.java index cdb74b02ff..457754e2c4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyEagerBasicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyEagerBasicUpdateTest.java @@ -6,22 +6,21 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -import org.hibernate.cfg.Configuration; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -35,38 +34,33 @@ import jakarta.persistence.Table; * This is mostly for comparison with {@link EagerAndLazyBasicUpdateTest}/{@link OnlyLazyBasicUpdateTest}, * because the mere presence of lazy properties in one entity may affect the behavior of eager properties, too. */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OnlyEagerBasicUpdateTest.EagerEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class }) -@TestForIssue(jiraKey = "HHH-16049") -public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-16049") +public class OnlyEagerBasicUpdateTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EagerEntity.class }; + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - private void initNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { EagerEntity entity = new EagerEntity(); s.persist( entity ); entityId = entity.getId(); } ); } - private void initNonNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNonNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { EagerEntity entity = new EagerEntity(); entity.setEagerProperty1( "eager1_initial" ); entity.setEagerProperty2( "eager2_initial" ); @@ -75,31 +69,31 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } ); } - @Before - public void clearStatementInspector() { - statementInspector().clear(); + @BeforeEach + public void clearStatementInspector(SessionFactoryScope scope) { + statementInspector( scope ).clear(); } @Test - public void updateSomeEagerProperty_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeEagerProperty_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( null ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateSomeEagerProperty_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeEagerProperty_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( "eager1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertEquals( "eager1_update", entity.getEagerProperty1() ); @@ -108,13 +102,13 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateSomeEagerProperty_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeEagerProperty_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( "eager1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertEquals( "eager1_update", entity.getEagerProperty1() ); @@ -123,25 +117,25 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateSomeEagerProperty_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeEagerProperty_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( entity.getEagerProperty1() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateSomeEagerProperty_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeEagerProperty_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertNull( entity.getEagerProperty1() ); @@ -150,27 +144,27 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllEagerProperties_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllEagerProperties_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( null ); entity.setEagerProperty2( null ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateAllEagerProperties_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllEagerProperties_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( "eager1_update" ); entity.setEagerProperty2( "eager2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertEquals( "eager1_update", entity.getEagerProperty1() ); assertEquals( "eager2_update", entity.getEagerProperty2() ); @@ -178,14 +172,14 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllEagerProperties_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllEagerProperties_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( "eager1_update" ); entity.setEagerProperty2( "eager2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertEquals( "eager1_update", entity.getEagerProperty1() ); assertEquals( "eager2_update", entity.getEagerProperty2() ); @@ -193,27 +187,27 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllEagerProperties_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllEagerProperties_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( entity.getEagerProperty1() ); entity.setEagerProperty2( entity.getEagerProperty2() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateAllEagerProperties_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllEagerProperties_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); entity.setEagerProperty1( null ); entity.setEagerProperty2( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { EagerEntity entity = s.get( EagerEntity.class, entityId ); assertNull( entity.getEagerProperty1() ); assertNull( entity.getEagerProperty2() ); @@ -222,7 +216,7 @@ public class OnlyEagerBasicUpdateTest extends BaseCoreFunctionalTestCase { @Entity @Table(name = "LAZY_ENTITY") - private static class EagerEntity { + static class EagerEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyLazyBasicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyLazyBasicUpdateTest.java index c6b148492d..6aa12b9c41 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyLazyBasicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/basic/OnlyLazyBasicUpdateTest.java @@ -6,18 +6,18 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.basic; -import org.hibernate.cfg.Configuration; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -26,42 +26,37 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OnlyLazyBasicUpdateTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class }) -@TestForIssue(jiraKey = { "HHH-15634", "HHH-16049" }) -public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-15634") +@JiraKey("HHH-16049") +public class OnlyLazyBasicUpdateTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - private void initNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); s.persist( entity ); entityId = entity.getId(); } ); } - private void initNonNull() { - doInHibernate( this::sessionFactory, s -> { + private void initNonNull(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.setLazyProperty1( "lazy1_initial" ); entity.setLazyProperty2( "lazy2_initial" ); @@ -70,32 +65,32 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } ); } - @Before - public void clearStatementInspector() { - statementInspector().clear(); + @BeforeEach + public void clearStatementInspector(SessionFactoryScope scope) { + statementInspector( scope ).clear(); } @Test - public void updateSomeLazyProperty_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeLazyProperty_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateSomeLazyProperty_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeLazyProperty_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -104,13 +99,13 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateSomeLazyProperty_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeLazyProperty_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); @@ -119,25 +114,25 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateSomeLazyProperty_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeLazyProperty_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( entity.getLazyProperty1() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateSomeLazyProperty_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateSomeLazyProperty_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); @@ -146,9 +141,9 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nullToNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nullToNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); @@ -156,18 +151,18 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { // When a lazy property is modified Hibernate does not perform any select // but during flush an update is performed - statementInspector().assertUpdate(); + statementInspector( scope ).assertUpdate(); } @Test - public void updateAllLazyProperties_nullToNonNull() { - initNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nullToNonNull(SessionFactoryScope scope) { + initNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); entity.setLazyProperty2( "lazy2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); assertEquals( "lazy2_update", entity.getLazyProperty2() ); @@ -175,14 +170,14 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nonNullToNonNull_differentValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNonNull_differentValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "lazy1_update" ); entity.setLazyProperty2( "lazy2_update" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "lazy1_update", entity.getLazyProperty1() ); assertEquals( "lazy2_update", entity.getLazyProperty2() ); @@ -190,27 +185,27 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { } @Test - public void updateAllLazyProperties_nonNullToNonNull_sameValues() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNonNull_sameValues(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( entity.getLazyProperty1() ); entity.setLazyProperty2( entity.getLazyProperty2() ); } ); // We should not update entities when property values did not change - statementInspector().assertNoUpdate(); + statementInspector( scope ).assertNoUpdate(); } @Test - public void updateAllLazyProperties_nonNullToNull() { - initNonNull(); - doInHibernate( this::sessionFactory, s -> { + public void updateAllLazyProperties_nonNullToNull(SessionFactoryScope scope) { + initNonNull( scope ); + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); @@ -219,7 +214,7 @@ public class OnlyLazyBasicUpdateTest extends BaseCoreFunctionalTestCase { @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/LazyInCacheTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/LazyInCacheTest.java index ba02f7b319..11f7c07b50 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/LazyInCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/LazyInCacheTest.java @@ -14,15 +14,16 @@ import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Type; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.usertype.UserTypeLegacyBridge; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -33,35 +34,37 @@ import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyInCacheTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + LazyInCacheTest.Order.class, LazyInCacheTest.Product.class, LazyInCacheTest.Tag.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyInCacheTest { private Long orderId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Order.class, Product.class, Tag.class}; - } - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { Order order = new Order(); Product product = new Product(); order.products.add( product ); order.data = "some data".getBytes( Charset.defaultCharset() ); - doInJPA( this::sessionFactory, em -> { + scope.inTransaction( em -> { em.persist( product ); em.persist( order ); } ); @@ -70,10 +73,10 @@ public class LazyInCacheTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInJPA( this::sessionFactory, em -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( em -> { Order order = em.find( Order.class, orderId ); - Assert.assertEquals( 1, order.products.size() ); + assertEquals( 1, order.products.size() ); } ); } @@ -82,7 +85,7 @@ public class LazyInCacheTest extends BaseCoreFunctionalTestCase { @Entity(name = "Order") @Table( name = "ORDER_TABLE" ) @Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE ) - private static class Order { + static class Order { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -102,7 +105,7 @@ public class LazyInCacheTest extends BaseCoreFunctionalTestCase { @Entity(name = "Product") @Table( name = "PRODUCT" ) - private static class Product { + static class Product { @Id @GeneratedValue( strategy = GenerationType.AUTO ) @@ -113,7 +116,7 @@ public class LazyInCacheTest extends BaseCoreFunctionalTestCase { @Entity(name = "Tag") @Table( name = "TAG" ) - private static class Tag { + static class Tag { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedAssociationsInCacheTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedAssociationsInCacheTest.java index 71caa0b2b7..b63314046e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedAssociationsInCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedAssociationsInCacheTest.java @@ -17,21 +17,17 @@ import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.CacheRegionStatistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.not; @@ -39,41 +35,32 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) -public class UninitializedAssociationsInCacheTest extends BaseCoreFunctionalTestCase { +import org.junit.Assert; +import org.junit.jupiter.api.Test; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Employee.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, true); - configuration.setProperty(AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, false); - configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, true); - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // This test only makes sense if association properties *can* be uninitialized. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } +@DomainModel( + annotatedClasses = { + UninitializedAssociationsInCacheTest.Employee.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory( + // This test only makes sense if association properties *can* be uninitialized. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class UninitializedAssociationsInCacheTest { @Test - @TestForIssue( jiraKey = "HHH-11766") - public void attributeLoadingFromCache() { - inTransaction( + @JiraKey("HHH-11766") + public void attributeLoadingFromCache(SessionFactoryScope scope) { + scope.inTransaction( (s) -> { Employee boss = new Employee( 1, "boss" ); Employee teamleader = new Employee( 2, "leader" ); @@ -90,11 +77,11 @@ public class UninitializedAssociationsInCacheTest extends BaseCoreFunctionalTest } ); - sessionFactory().getCache().evictAll(); - sessionFactory().getStatistics().clear(); - CacheRegionStatistics regionStatistics = sessionFactory().getStatistics().getCacheRegionStatistics( "Employee" ); + scope.getSessionFactory().getCache().evictAll(); + scope.getSessionFactory().getStatistics().clear(); + CacheRegionStatistics regionStatistics = scope.getSessionFactory().getStatistics().getCacheRegionStatistics( "Employee" ); - inTransaction( + scope.inTransaction( (s) -> { final Employee boss = s.find( Employee.class, 1 ); Assert.assertEquals( "boss", boss.regularString ); @@ -124,7 +111,7 @@ public class UninitializedAssociationsInCacheTest extends BaseCoreFunctionalTest assertEquals( 3, regionStatistics.getMissCount() ); assertEquals( 3, regionStatistics.getPutCount() ); - inTransaction( + scope.inTransaction( (s) -> { final Employee boss = s.find( Employee.class, 1 ); final Employee leader = s.find( Employee.class, 2 ); @@ -164,7 +151,7 @@ public class UninitializedAssociationsInCacheTest extends BaseCoreFunctionalTest @Table(name = "EMPLOYEE_TABLE") @Cacheable @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "Employee") - private static class Employee { + static class Employee { @Id Integer id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedLazyBasicCacheTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedLazyBasicCacheTest.java index ca759ff258..e48b26533e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedLazyBasicCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/cache/UninitializedLazyBasicCacheTest.java @@ -18,45 +18,46 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.stat.CacheRegionStatistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Aaron Schmischke * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class UninitializedLazyBasicCacheTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + UninitializedLazyBasicCacheTest.Person.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class UninitializedLazyBasicCacheTest { private Long personId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Person.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, true ); - } - - @Before - public void prepare() { - this.personId = doInHibernate( - this::sessionFactory, s -> { - + @BeforeEach + public void prepare(SessionFactoryScope scope) { + this.personId = scope.fromTransaction( s -> { final Person person = new Person(); person.setLazyAttribute( "does_not_matter" ); s.persist( person ); @@ -66,26 +67,24 @@ public class UninitializedLazyBasicCacheTest extends BaseCoreFunctionalTestCase } @Test - @TestForIssue( jiraKey = "HHH-11766") - public void test() { + @JiraKey("HHH-11766") + public void test(SessionFactoryScope scope) { - sessionFactory().getStatistics().clear(); - sessionFactory().getCache().evictAll(); + scope.getSessionFactory().getStatistics().clear(); + scope.getSessionFactory().getCache().evictAll(); - doInHibernate( - this::sessionFactory, s -> { + scope.inTransaction( s -> { final Person person = s.get( Person.class, personId ); assertFalse( Hibernate.isPropertyInitialized( person, "lazyAttribute" ) ); } ); - CacheRegionStatistics regionStatistics = sessionFactory().getStatistics().getCacheRegionStatistics( "Person" ); + CacheRegionStatistics regionStatistics = scope.getSessionFactory().getStatistics().getCacheRegionStatistics( "Person" ); assertEquals( 0, regionStatistics.getHitCount() ); assertEquals( 1, regionStatistics.getMissCount() ); assertEquals( 1, regionStatistics.getPutCount() ); - doInHibernate( - this::sessionFactory, s -> { + scope.inTransaction( s -> { final Person person = s.get( Person.class, personId ); assertFalse( Hibernate.isPropertyInitialized( person, "lazyAttribute" ) ); person.getLazyAttribute(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/fetch/EnhancedFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/fetch/EnhancedFetchTest.java index 36098ec9a7..ecd6a5cc20 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/fetch/EnhancedFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/fetch/EnhancedFetchTest.java @@ -12,51 +12,51 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.collection.spi.PersistentSet; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gavin King */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + EnhancedFetchTest.School.class, EnhancedFetchTest.Student.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class EnhancedFetchTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - sfb.applyCollectionsInDefaultFetchGroup( true ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( School.class ); - sources.addAnnotatedClass( Student.class ); - } +public class EnhancedFetchTest { @Test - public void testFetch() { - inStatelessTransaction( + public void testFetch(SessionFactoryScope scope) { + scope.inStatelessTransaction( session -> { School school = new School( "BHS" ); Student student = new Student( "gavin" ); @@ -66,9 +66,9 @@ public class EnhancedFetchTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final Student student = session.get( Student.class, "gavin" ); assertFalse( Hibernate.isInitialized( student.getSchool() ) ); @@ -83,9 +83,9 @@ public class EnhancedFetchTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final School school = session.get( School.class, "BHS" ); assertFalse( Hibernate.isInitialized( school.getStudents() ) ); @@ -99,9 +99,9 @@ public class EnhancedFetchTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Pupil" ).executeUpdate(); session.createQuery( "delete from School" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BasicAttributesLazyGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BasicAttributesLazyGroupTest.java index 27f04f42c2..678c068042 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BasicAttributesLazyGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BasicAttributesLazyGroupTest.java @@ -5,14 +5,19 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyGroup; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Column; @@ -25,27 +30,28 @@ import jakarta.persistence.Id; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-14874") -public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-14874") +@DomainModel( + annotatedClasses = { + BasicAttributesLazyGroupTest.Review.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class BasicAttributesLazyGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Review.class }; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Review review = new Review(); review.setComment( "My first review" ); @@ -55,9 +61,16 @@ public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { ); } + @AfterAll + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> session.createMutationQuery( "delete Review" ).executeUpdate() + ); + } + @Test - public void testLoad() { - inTransaction( session -> { + public void testLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Review review = session.load( Review.class, 1L ); assertFalse( Hibernate.isPropertyInitialized( review, "rating" ) ); @@ -71,8 +84,8 @@ public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { } @Test - public void testLoad2() { - inTransaction( session -> { + public void testLoad2(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Review review = session.load( Review.class, 1L ); assertFalse( Hibernate.isPropertyInitialized( review, "rating" ) ); @@ -86,8 +99,8 @@ public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { } @Test - public void testLoad3() { - inTransaction( session -> { + public void testLoad3(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Review review = session.load( Review.class, 1L ); assertThat( review.getComment(), is( "My first review" ) ); @@ -96,8 +109,8 @@ public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { } @Test - public void testLoad4() { - inTransaction( session -> { + public void testLoad4(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Review review = session.load( Review.class, 1L ); assertThat( review.getRating(), is( Rating.ONE ) ); assertThat( review.getComment(), is( "My first review" ) ); @@ -105,8 +118,8 @@ public class BasicAttributesLazyGroupTest extends BaseCoreFunctionalTestCase { } @Test - public void testHql() { - inTransaction( session -> { + public void testHql(SessionFactoryScope scope) { + scope.inTransaction( session -> { final List reviews = session.createQuery( "select r from Review r" ).list(); assertThat( reviews.size(), is( 1 ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsInEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsInEmbeddableTest.java index 2f594af1b7..f381069246 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsInEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsInEmbeddableTest.java @@ -26,14 +26,16 @@ import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.UnloadedClass; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; + import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; /** * Tests removing non-owning side of the bidirectional association, @@ -43,25 +45,24 @@ import org.junit.runner.RunWith; * * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-13241") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-13241") +@DomainModel( + annotatedClasses = { + BidirectionalLazyGroupsInEmbeddableTest.Employer.class, BidirectionalLazyGroupsInEmbeddableTest.Employee.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, BidirectionalLazyGroupsInEmbeddableTest.NoDirtyCheckEnhancementContext.class }) -public class BidirectionalLazyGroupsInEmbeddableTest extends BaseCoreFunctionalTestCase { - - public Class[] getAnnotatedClasses() { - return new Class[] { Employer.class, Employee.class }; - } +public class BidirectionalLazyGroupsInEmbeddableTest { @Test - @Ignore("Test is failing with ByteBuddy if the mappings are moved to the fields.") - public void test() { - - doInHibernate( - this::sessionFactory, session -> { - + @Disabled("Test is failing with ByteBuddy if the mappings are moved to the fields.") + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Employer employer = new Employer( "RedHat" ); session.persist( employer ); employer.addEmployee( new Employee( "Jack" ) ); @@ -73,8 +74,7 @@ public class BidirectionalLazyGroupsInEmbeddableTest extends BaseCoreFunctionalT } ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employer employer = session.createQuery( "from Employer e", Employer.class ).getSingleResult(); session.remove( employer ); for ( Employee employee : employer.getEmployees() ) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsTest.java index 9edaf1a233..98f719c1da 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/BidirectionalLazyGroupsTest.java @@ -23,19 +23,19 @@ import org.hibernate.annotations.LazyToOneOption; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.UnloadedClass; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hamcrest.CoreMatchers; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; /** * Tests removing non-owning side of the bidirectional association, @@ -43,22 +43,24 @@ import static org.junit.Assert.assertTrue; * * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-13241") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-13241") +@DomainModel( + annotatedClasses = { + BidirectionalLazyGroupsTest.Employer.class, + BidirectionalLazyGroupsTest.Employee.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, BidirectionalLazyGroupsTest.NoDirtyCheckEnhancementContext.class }) -public class BidirectionalLazyGroupsTest extends BaseCoreFunctionalTestCase { - - public Class[] getAnnotatedClasses() { - return new Class[] { Employer.class, Employee.class }; - } +public class BidirectionalLazyGroupsTest { @Test - public void testRemoveCollectionOwnerNoCascade() { - inTransaction( - (session) -> { + public void testRemoveCollectionOwnerNoCascade(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Employer employer = new Employer( "RedHat" ); session.persist( employer ); employer.addEmployee( new Employee( "Jack" ) ); @@ -70,21 +72,19 @@ public class BidirectionalLazyGroupsTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( - (session) -> { + scope.inTransaction( session -> { final Employer employer = session.createQuery( "from Employer e", Employer.class ).getSingleResult(); session.remove( employer ); for ( Employee employee : employer.getEmployees() ) { assertTrue( Hibernate.isPropertyInitialized( employee, "employer") ); - assertThat( employee.getEmployer(), CoreMatchers.sameInstance( employer ) ); + assertThat( employee.getEmployer() ).isSameAs( employer ); session.remove( employee ); } } ); - inTransaction( - (session) -> { + scope.inTransaction( session -> { assertNull( session.find( Employer.class, "RedHat" ) ); assertNull( session.createQuery( "from Employee e", Employee.class ).uniqueResult() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java index 42e22ff886..a71ad6000a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupMappedByTest.java @@ -1,42 +1,42 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.hibernate.stat.SessionStatistics; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; /** * Testing OneToOne LazyToOne association * * @author Jan-Oliver Lustig, Sebastian Viefhaus */ -@TestForIssue(jiraKey = "HHH-11986") -@RunWith(BytecodeEnhancerRunner.class) -public class LazyGroupMappedByTest extends BaseCoreFunctionalTestCase { - - public Class[] getAnnotatedClasses() { - return new Class[] { LGMB_From.class, LGMB_To.class }; - } +@JiraKey("HHH-11986") +@DomainModel( + annotatedClasses = { + LGMB_From.class, LGMB_To.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyGroupMappedByTest { @Test - @TestForIssue(jiraKey = "HHH-11986") - public void test() { - Long fromId = createEntities(); + @JiraKey("HHH-11986") + public void test(SessionFactoryScope scope) { + Long fromId = createEntities( scope ); - Statistics stats = sessionFactory().getStatistics(); + Statistics stats = scope.getSessionFactory().getStatistics(); stats.setStatisticsEnabled( true ); stats.clear(); - doInHibernate( - this::sessionFactory, session -> { - + scope.inTransaction( session -> { SessionStatistics sessionStats = session.getStatistics(); // Should be loaded lazy. @@ -65,9 +65,8 @@ public class LazyGroupMappedByTest extends BaseCoreFunctionalTestCase { * * @return ID der Quell-Entität */ - public Long createEntities() { - return doInHibernate( - this::sessionFactory, session -> { + public Long createEntities(SessionFactoryScope scope) { + return scope.fromTransaction( session -> { session.createQuery( "delete from LGMB_To" ).executeUpdate(); session.createQuery( "delete from LGMB_From" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupTest.java index 0618297178..5932c223d9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/LazyGroupTest.java @@ -24,49 +24,47 @@ import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * @author Steve Ebersole */ -@TestForIssue( jiraKey = "HHH-11155" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyGroupTest extends BaseCoreFunctionalTestCase { - private SQLStatementInterceptor sqlInterceptor; +@JiraKey( "HHH-11155" ) +@DomainModel( + annotatedClasses = { + LazyGroupTest.Child.class, LazyGroupTest.Parent.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyGroupTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Child.class, Parent.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - sqlInterceptor = new SQLStatementInterceptor( configuration ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Child c1 = new Child( "steve", "Hibernater" ); Child c2 = new Child( "sally", "Joe Mama" ); @@ -91,17 +89,20 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue( jiraKey = "HHH-10267" ) - public void testAccess() { - sqlInterceptor.clear(); - - inTransaction( + @JiraKey( "HHH-10267" ) + public void testAccess(SessionFactoryScope scope) { + scope.inTransaction( (s) -> { + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getSessionFactory() + .getSessionFactoryOptions() + .getStatementInspector(); + statementInspector.clear(); + final Child c1 = s.createQuery( "from Child c where c.name = :name", Child.class ) .setParameter( "name", "steve" ) .uniqueResult(); - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + statementInspector.assertExecutedCount( 1 ); assertTrue( Hibernate.isPropertyInitialized( c1, "name" ) ); @@ -109,50 +110,50 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "parent" ) ); - assertThat( c1.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getParent() ) ); // alternateParent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "alternateParent" ) ); - assertThat( c1.getAlternateParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getAlternateParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getAlternateParent() ) ); // Now lets access nickName which ought to initialize nickName c1.getNickName(); - assertThat( sqlInterceptor.getQueryCount(), is( 2 ) ); + statementInspector.assertExecutedCount( 2 ); assertTrue( Hibernate.isPropertyInitialized( c1, "nickName" ) ); // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "parent" ) ); - assertThat( c1.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getParent() ) ); // alternateParent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "alternateParent" ) ); - assertThat( c1.getAlternateParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getAlternateParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getAlternateParent() ) ); - - - sqlInterceptor.clear(); } ); } @Test - @TestForIssue( jiraKey = "HHH-11155" ) - public void testUpdate() { + @JiraKey( "HHH-11155" ) + public void testUpdate(SessionFactoryScope scope) { Parent p1New = new Parent(); p1New.nombre = "p1New"; - inTransaction( + scope.inTransaction( (s) -> { - sqlInterceptor.clear(); + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getSessionFactory() + .getSessionFactoryOptions() + .getStatementInspector(); + statementInspector.clear(); final Child c1 = s.createQuery( "from Child c where c.name = :name", Child.class ) .setParameter( "name", "steve" ) .uniqueResult(); - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + statementInspector.assertExecutedCount( 1 ); assertTrue( Hibernate.isPropertyInitialized( c1, "name" ) ); @@ -160,18 +161,18 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "parent" ) ); - assertThat( c1.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getParent() ) ); // alternateParent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "alternateParent" ) ); - assertThat( c1.getAlternateParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getAlternateParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getAlternateParent() ) ); // Now lets update nickName c1.nickName = "new nickName"; - assertThat( sqlInterceptor.getQueryCount(), is( 1 ) ); + statementInspector.assertExecutedCount( 1 ); assertTrue( Hibernate.isPropertyInitialized( c1, "name" ) ); @@ -179,12 +180,12 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { // parent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "parent" ) ); - assertThat( c1.getParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getParent() ) ); // alternateParent should be an uninitialized enhanced-proxy assertTrue( Hibernate.isPropertyInitialized( c1, "alternateParent" ) ); - assertThat( c1.getAlternateParent(), not( instanceOf( HibernateProxy.class ) ) ); + assertThat( c1.getAlternateParent() ).isNotInstanceOf( HibernateProxy.class ); assertFalse( Hibernate.isInitialized( c1.getAlternateParent() ) ); // Now update c1.parent @@ -195,21 +196,21 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { ); // verify updates - inTransaction( + scope.inTransaction( (s) -> { final Child c1 = s.createQuery( "from Child c where c.name = :name", Child.class ) .setParameter( "name", "steve" ) .uniqueResult(); - assertThat( c1.getNickName(), is( "new nickName" ) ); - assertThat( c1.parent.nombre, is( "p1New" ) ); + assertThat( c1.getNickName() ).isEqualTo( "new nickName" ); + assertThat( c1.parent.nombre ).isEqualTo( "p1New" ); } ); } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete Child" ).executeUpdate(); s.createQuery( "delete Parent" ).executeUpdate(); } ); @@ -221,7 +222,7 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { @Entity( name = "Parent" ) @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id @GeneratedValue( strategy = GenerationType.AUTO ) Long id; @@ -244,7 +245,7 @@ public class LazyGroupTest extends BaseCoreFunctionalTestCase { @Entity( name = "Child" ) @Table( name = "CHILD" ) - private static class Child { + static class Child { @Id @GeneratedValue( strategy = GenerationType.AUTO ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateTest.java index b204745cf5..f30785ca54 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateTest.java @@ -6,20 +6,20 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import org.hibernate.annotations.LazyGroup; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -28,20 +28,21 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MultiLazyBasicInLazyGroupUpdateTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext( { EnhancerTestContext.class, NoDirtyCheckingContext.class} ) -public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestCase { +public class MultiLazyBasicInLazyGroupUpdateTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); s.persist( entity ); entityId = entity.getId(); @@ -49,13 +50,13 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } @Test - public void updateOneLazyProperty() { + public void updateOneLazyProperty(SessionFactoryScope scope) { // null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update1" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update1", entity.getLazyProperty1() ); assertNull(entity.getLazyProperty2()); @@ -63,11 +64,11 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } ); // non-null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update2" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update2", entity.getLazyProperty1() ); assertNull(entity.getLazyProperty2()); @@ -76,14 +77,14 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } @Test - public void updateOneEagerPropertyAndOneLazyProperty() { + public void updateOneEagerPropertyAndOneLazyProperty(SessionFactoryScope scope) { // null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update1" ); entity.setLazyProperty1( "update1" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update1", entity.getEagerProperty() ); assertEquals( "update1", entity.getLazyProperty1() ); @@ -91,12 +92,12 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } ); // non-null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( "eager_update2" ); entity.setLazyProperty1( "update2" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "eager_update2", entity.getEagerProperty() ); assertEquals( "update2", entity.getLazyProperty1() ); @@ -105,14 +106,14 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } @Test - public void updateAllLazyProperties() { + public void updateAllLazyProperties(SessionFactoryScope scope) { // null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update1" ); entity.setLazyProperty2( "update2_1" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update1", entity.getLazyProperty1() ); assertEquals( "update2_1", entity.getLazyProperty2() ); @@ -120,12 +121,12 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC } ); // non-null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update2" ); entity.setLazyProperty2( "update2_2" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update2", entity.getLazyProperty1() ); assertEquals( "update2_2", entity.getLazyProperty2() ); @@ -135,7 +136,7 @@ public class MultiLazyBasicInLazyGroupUpdateTest extends BaseCoreFunctionalTestC @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateToNullTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateToNullTest.java index a78d177c6d..af0ec542fa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateToNullTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/MultiLazyBasicInLazyGroupUpdateToNullTest.java @@ -9,13 +9,14 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; import org.hibernate.annotations.LazyGroup; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -24,24 +25,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MultiLazyBasicInLazyGroupUpdateToNullTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext( { EnhancerTestContext.class, NoDirtyCheckingContext.class} ) -public class MultiLazyBasicInLazyGroupUpdateToNullTest extends BaseCoreFunctionalTestCase { +public class MultiLazyBasicInLazyGroupUpdateToNullTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.setEagerProperty( "eager" ); entity.setLazyProperty1( "update1" ); @@ -52,13 +53,13 @@ public class MultiLazyBasicInLazyGroupUpdateToNullTest extends BaseCoreFunctiona } @Test - public void updateOneLazyProperty() { + public void updateOneLazyProperty(SessionFactoryScope scope) { // non-null -> null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNotNull( entity.getLazyProperty2() ); @@ -67,14 +68,14 @@ public class MultiLazyBasicInLazyGroupUpdateToNullTest extends BaseCoreFunctiona } @Test - public void updateOneEagerPropertyAndOneLazyProperty() { + public void updateOneEagerPropertyAndOneLazyProperty(SessionFactoryScope scope) { // non-null -> null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setEagerProperty( null ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getEagerProperty() ); assertNull( entity.getLazyProperty1() ); @@ -83,14 +84,14 @@ public class MultiLazyBasicInLazyGroupUpdateToNullTest extends BaseCoreFunctiona } @Test - public void updateAllLazyProperties() { + public void updateAllLazyProperties(SessionFactoryScope scope) { // non-null -> null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); @@ -100,7 +101,7 @@ public class MultiLazyBasicInLazyGroupUpdateToNullTest extends BaseCoreFunctiona @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateTest.java index 6ced98f0c4..de942410b6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateTest.java @@ -9,13 +9,14 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; import org.hibernate.annotations.LazyGroup; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -24,24 +25,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OnlyLazyBasicInLazyGroupBasicUpdateTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, NoDirtyCheckingContext.class }) -public class OnlyLazyBasicInLazyGroupBasicUpdateTest extends BaseCoreFunctionalTestCase { +public class OnlyLazyBasicInLazyGroupBasicUpdateTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); s.persist( entity ); entityId = entity.getId(); @@ -49,24 +50,24 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateTest extends BaseCoreFunctionalT } @Test - public void updateOneLazyProperty() { + public void updateOneLazyProperty(SessionFactoryScope scope) { // null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update1" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update1", entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); } ); // non-null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update2" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update2", entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); @@ -74,26 +75,26 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateTest extends BaseCoreFunctionalT } @Test - public void updateAllLazyProperties() { + public void updateAllLazyProperties(SessionFactoryScope scope) { // null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update1" ); entity.setLazyProperty2( "update2_1" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update1", entity.getLazyProperty1() ); assertEquals( "update2_1", entity.getLazyProperty2() ); } ); // non-null -> non-null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( "update2" ); entity.setLazyProperty2( "update2_2" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertEquals( "update2", entity.getLazyProperty1() ); assertEquals( "update2_2", entity.getLazyProperty2() ); @@ -102,7 +103,7 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateTest extends BaseCoreFunctionalT @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateToNullTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateToNullTest.java index a9b257f9c1..593d206df9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateToNullTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/OnlyLazyBasicInLazyGroupBasicUpdateToNullTest.java @@ -9,13 +9,14 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; import org.hibernate.annotations.LazyGroup; import org.hibernate.orm.test.bytecode.enhancement.lazy.NoDirtyCheckingContext; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -24,24 +25,24 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OnlyLazyBasicInLazyGroupBasicUpdateToNullTest.LazyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext( { EnhancerTestContext.class, NoDirtyCheckingContext.class} ) -public class OnlyLazyBasicInLazyGroupBasicUpdateToNullTest extends BaseCoreFunctionalTestCase { +public class OnlyLazyBasicInLazyGroupBasicUpdateToNullTest { private Long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { LazyEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { LazyEntity entity = new LazyEntity(); entity.setLazyProperty1( "update1" ); entity.setLazyProperty2( "update2" ); @@ -51,13 +52,13 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateToNullTest extends BaseCoreFunct } @Test - public void updateOneLazyProperty() { + public void updateOneLazyProperty(SessionFactoryScope scope) { // non-null -> null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNotNull( entity.getLazyProperty2() ); @@ -65,14 +66,14 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateToNullTest extends BaseCoreFunct } @Test - public void updateAllLazyProperties() { + public void updateAllLazyProperties(SessionFactoryScope scope) { // non-null -> null - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); entity.setLazyProperty1( null ); entity.setLazyProperty2( null ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { LazyEntity entity = s.get( LazyEntity.class, entityId ); assertNull( entity.getLazyProperty1() ); assertNull( entity.getLazyProperty2() ); @@ -81,7 +82,7 @@ public class OnlyLazyBasicInLazyGroupBasicUpdateToNullTest extends BaseCoreFunct @Entity @Table(name = "LAZY_ENTITY") - private static class LazyEntity { + static class LazyEntity { @Id @GeneratedValue Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/SimpleLazyGroupUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/SimpleLazyGroupUpdateTest.java index f38da7020b..6804908f87 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/SimpleLazyGroupUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/group/SimpleLazyGroupUpdateTest.java @@ -9,17 +9,19 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.group; import org.hibernate.annotations.LazyGroup; import org.hibernate.bytecode.enhance.spi.UnloadedClass; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -28,42 +30,43 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.getFieldByReflection; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * @author Steve Ebersole */ -@TestForIssue( jiraKey = "HHH-11155, HHH-11506" ) -@RunWith( BytecodeEnhancerRunner.class ) -@CustomEnhancementContext( {EnhancerTestContext.class, SimpleLazyGroupUpdateTest.NoDirtyCheckingContext.class} ) -public class SimpleLazyGroupUpdateTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-11155") +@JiraKey("HHH-11506") +@DomainModel( + annotatedClasses = { + SimpleLazyGroupUpdateTest.TestEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false"), + @Setting(name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true"), + } +) +@SessionFactory +@BytecodeEnhanced +@CustomEnhancementContext({ EnhancerTestContext.class, SimpleLazyGroupUpdateTest.NoDirtyCheckingContext.class }) +public class SimpleLazyGroupUpdateTest { public static final String REALLY_BIG_STRING = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{TestEntity.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.save( new TestEntity( 1L, "entity 1", "blah", REALLY_BIG_STRING ) ); } ); } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity entity = s.get( TestEntity.class, 1L ); assertLoaded( entity, "name" ); assertNotLoaded( entity, "lifeStory" ); @@ -75,7 +78,7 @@ public class SimpleLazyGroupUpdateTest extends BaseCoreFunctionalTestCase { assertNotLoaded( entity, "reallyBigString" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { TestEntity entity = s.get( TestEntity.class, 1L ); assertLoaded( entity, "name" ); @@ -89,18 +92,18 @@ public class SimpleLazyGroupUpdateTest extends BaseCoreFunctionalTestCase { private void assertLoaded(Object owner, String name) { // NOTE we assume null == not-loaded Object fieldByReflection = getFieldByReflection( owner, name ); - assertNotNull( "Expecting field '" + name + "' to be loaded, but it was not", fieldByReflection ); + assertNotNull( fieldByReflection, "Expecting field '" + name + "' to be loaded, but it was not" ); } private void assertNotLoaded(Object owner, String name) { // NOTE we assume null == not-loaded Object fieldByReflection = getFieldByReflection( owner, name ); - assertNull( "Expecting field '" + name + "' to be not loaded, but it was", fieldByReflection ); + assertNull( fieldByReflection, "Expecting field '" + name + "' to be not loaded, but it was" ); } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete TestEntity" ).executeUpdate(); } ); } @@ -109,7 +112,7 @@ public class SimpleLazyGroupUpdateTest extends BaseCoreFunctionalTestCase { @Entity( name = "TestEntity" ) @Table( name = "TEST_ENTITY" ) - private static class TestEntity { + static class TestEntity { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundManyToOneNonUpdatableNonInsertableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundManyToOneNonUpdatableNonInsertableTest.java index 3369b77f5d..eba169e9b0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundManyToOneNonUpdatableNonInsertableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundManyToOneNonUpdatableNonInsertableTest.java @@ -22,37 +22,35 @@ import org.hibernate.annotations.LazyToOneOption; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-12226") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyNotFoundManyToOneNonUpdatableNonInsertableTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-12226") +@DomainModel( + annotatedClasses = { + LazyNotFoundManyToOneNonUpdatableNonInsertableTest.User.class, + LazyNotFoundManyToOneNonUpdatableNonInsertableTest.Lazy.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyNotFoundManyToOneNonUpdatableNonInsertableTest { private static int ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - Lazy.class - }; - } - @Test - public void test() { - doInHibernate( - this::sessionFactory, session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Lazy p = new Lazy(); p.id = ID; User u = new User(); @@ -62,14 +60,9 @@ public class LazyNotFoundManyToOneNonUpdatableNonInsertableTest extends BaseCore } ); - doInHibernate( - this::sessionFactory, session -> { - session.delete( session.get( Lazy.class, ID ) ); - } - ); + scope.inTransaction( session -> session.delete( session.get( Lazy.class, ID ) ) ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { User user = session.find( User.class, ID ); // per UserGuide (and simply correct behavior), `@NotFound` forces EAGER fetching assertThat( Hibernate.isPropertyInitialized( user, "lazy" ) ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneNonUpdatableNonInsertableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneNonUpdatableNonInsertableTest.java index 0fe3bfd928..ff58921538 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneNonUpdatableNonInsertableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneNonUpdatableNonInsertableTest.java @@ -11,10 +11,8 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.notfound; */ import jakarta.persistence.CascadeType; -import jakarta.persistence.ConstraintMode; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; -import jakarta.persistence.ForeignKey; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToOne; @@ -26,35 +24,31 @@ import org.hibernate.annotations.LazyToOneOption; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -@TestForIssue( jiraKey = "HHH-12226") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyNotFoundOneToOneNonUpdatableNonInsertableTest extends BaseCoreFunctionalTestCase { +import org.junit.jupiter.api.Test; + +@JiraKey("HHH-12226") +@DomainModel( + annotatedClasses = { + LazyNotFoundOneToOneNonUpdatableNonInsertableTest.User.class, + LazyNotFoundOneToOneNonUpdatableNonInsertableTest.Lazy.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyNotFoundOneToOneNonUpdatableNonInsertableTest { private static int ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - Lazy.class - }; - } - @Test - public void test() { - doInHibernate( - this::sessionFactory, session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Lazy p = new Lazy(); p.id = ID; User u = new User(); @@ -64,14 +58,9 @@ public class LazyNotFoundOneToOneNonUpdatableNonInsertableTest extends BaseCoreF } ); - doInHibernate( - this::sessionFactory, session -> { - session.delete( session.get( Lazy.class, ID ) ); - } - ); + scope.inTransaction( session -> session.delete( session.get( Lazy.class, ID ) ) ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { User user = session.find( User.class, ID ); assertThat( Hibernate.isPropertyInitialized( user, "lazy" ) ) .describedAs( "Expecting `User#lazy` to be bytecode initialized due to `@NotFound`" ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneTest.java index 16cc478845..15b6dcd0ec 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/notfound/LazyNotFoundOneToOneTest.java @@ -21,49 +21,38 @@ import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.CoreMatchers.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-12226") -@RunWith( BytecodeEnhancerRunner.class ) -public class LazyNotFoundOneToOneTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-12226") +@DomainModel( + annotatedClasses = { + LazyNotFoundOneToOneTest.User.class, + LazyNotFoundOneToOneTest.Lazy.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyNotFoundOneToOneTest { private static int ID = 1; - private SQLStatementInterceptor sqlInterceptor; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - User.class, - Lazy.class - }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure(configuration); - sqlInterceptor = new SQLStatementInterceptor( configuration ); - } @Test - public void test() { - doInHibernate( - this::sessionFactory, session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { Lazy p = new Lazy(); p.id = ID; User u = new User(); @@ -73,22 +62,17 @@ public class LazyNotFoundOneToOneTest extends BaseCoreFunctionalTestCase { } ); - doInHibernate( - this::sessionFactory, session -> { - session.delete( session.get( Lazy.class, ID ) ); - } - ); + scope.inTransaction( session -> session.delete( session.get( Lazy.class, ID ) ) ); - sqlInterceptor.clear(); + scope.inTransaction( session -> { + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getSessionFactory() + .getSessionFactoryOptions().getStatementInspector(); + statementInspector.clear(); - doInHibernate( - this::sessionFactory, session -> { User user = session.find( User.class, ID ); // `@NotFound` forces EAGER join fetching - assertThat( sqlInterceptor.getQueryCount() ). - describedAs( "Expecting 2 queries due to `@NotFound`" ) - .isEqualTo( 2 ); + statementInspector.assertExecutedCount(2); assertThat( Hibernate.isPropertyInitialized( user, "lazy" ) ) .describedAs( "Expecting `User#lazy` to be eagerly fetched due to `@NotFound`" ) .isTrue(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java index f93a16d05d..4fcd2bfca9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BatchFetchProxyTest.java @@ -16,45 +16,59 @@ import jakarta.persistence.ManyToOne; import org.hibernate.Hibernate; import org.hibernate.annotations.BatchSize; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + BatchFetchProxyTest.Employee.class, + BatchFetchProxyTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.SHOW_SQL, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class BatchFetchProxyTest { private static int NUMBER_OF_ENTITIES = 20; @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testBatchAssociationFetch() { - inTransaction( + @JiraKey("HHH-11147") + public void testBatchAssociationFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); List employees = session.createQuery( "from Employee", Employee.class ).getResultList(); @@ -74,19 +88,19 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { } // assert that all 20 Employee and all 20 Employers have been loaded - assertThat( statistics.getEntityLoadCount(), is( 40L ) ); + assertThat( statistics.getEntityLoadCount() ).isEqualTo( 40L ); // but assert that it only took 3 queries (the initial plus the 2 batch fetches) - assertThat( statistics.getPrepareStatementCount(), is( 3L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 3L ); } ); } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testBatchAssociation() { - inTransaction( + @JiraKey("HHH-11147") + public void testBatchAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); List employees = session.createQuery( "from Employee", Employee.class ).getResultList(); @@ -111,11 +125,11 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testBatchEntityLoad() { - inTransaction( + @JiraKey("HHH-11147") + public void testBatchEntityLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); List employers = new ArrayList<>(); @@ -144,11 +158,11 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testBatchEntityLoadThenModify() { - inTransaction( + @JiraKey("HHH-11147") + public void testBatchEntityLoadThenModify(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); List employers = new ArrayList<>(); @@ -175,7 +189,7 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { final Employer employer = session.get( Employer.class, i + 1 ); @@ -185,17 +199,9 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - Employer.class - }; - } - - @Before - public void setUpData() { - inTransaction( + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) { final Employee employee = new Employee(); @@ -211,9 +217,9 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @After - public void cleanupDate() { - inTransaction( + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Employee" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); @@ -221,22 +227,6 @@ public class BatchFetchProxyTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.applySetting( AvailableSettings.SHOW_SQL, true ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "Employee") public static class Employee { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BidirectionalProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BidirectionalProxyTest.java index 5c730cd58c..35106f0857 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BidirectionalProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BidirectionalProxyTest.java @@ -23,36 +23,52 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.annotations.LazyGroup; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.Assert.assertEquals; -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + BidirectionalProxyTest.BEntity.class, + BidirectionalProxyTest.CEntity.class, + BidirectionalProxyTest.AMappedSuperclass.class, + BidirectionalProxyTest.AEntity.class, + BidirectionalProxyTest.AChildEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class BidirectionalProxyTest { @Test - public void testIt() { - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { for (BEntity b : session.createQuery("from BEntity b", BEntity.class).getResultList()) { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AChildEntity a = b.getA(); assertEquals( 0, stats.getPrepareStatementCount() ); @@ -78,10 +94,10 @@ public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { for (BEntity b : session.createQuery("from BEntity b", BEntity.class).getResultList()) { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AChildEntity a = b.getA(); assertEquals( "this is a string", a.getStringField() ); @@ -91,10 +107,10 @@ public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { for (CEntity c : session.createQuery("from CEntity c", CEntity.class).getResultList()) { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity a = c.getA(); assertEquals( 0, stats.getPrepareStatementCount() ); @@ -110,10 +126,10 @@ public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { for (CEntity c : session.createQuery("from CEntity c", CEntity.class).getResultList()) { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity a = c.getA(); assertEquals( 2, a.getVersion() ); @@ -123,34 +139,9 @@ public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( BEntity.class ); - sources.addAnnotatedClass( CEntity.class ); - sources.addAnnotatedClass( AMappedSuperclass.class ); - sources.addAnnotatedClass( AEntity.class ); - sources.addAnnotatedClass( AChildEntity.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { AChildEntity a = new AChildEntity("a"); BEntity b = new BEntity("b"); @@ -167,9 +158,9 @@ public class BidirectionalProxyTest extends BaseNonConfigCoreFunctionalTestCase ); } - @After - public void clearTestData(){ - inTransaction( + @AfterEach + public void clearTestData(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createQuery( "delete from BEntity" ).executeUpdate(); session.createQuery( "delete from CEntity" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BytecodeEnhancedLazyLoadingOnDeletedEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BytecodeEnhancedLazyLoadingOnDeletedEntityTest.java index b1862471b6..d1a6250d81 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BytecodeEnhancedLazyLoadingOnDeletedEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/BytecodeEnhancedLazyLoadingOnDeletedEntityTest.java @@ -8,7 +8,7 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hibernate.cfg.AvailableSettings.DEFAULT_LIST_SEMANTICS; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.BytecodeEnhancedLazyLoadingOnDeletedEntityTest.*; import java.util.ArrayList; import java.util.List; @@ -20,65 +20,48 @@ import jakarta.persistence.ManyToMany; import jakarta.persistence.Table; import org.hibernate.LazyInitializationException; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; -import org.hibernate.cfg.Configuration; -import org.hibernate.metamodel.CollectionClassification; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "HHH-14811") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14811") +@DomainModel( + annotatedClasses = { + AssociationOwner.class, AssociationNonOwner.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = DEFAULT_LIST_SEMANTICS, value = "BAG" ), + } +) +@SessionFactory( + // This test only makes sense if association properties *can* be uninitialized. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest - extends BaseCoreFunctionalTestCase { +public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { AssociationOwner.class, AssociationNonOwner.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - configuration.setProperty( DEFAULT_LIST_SEMANTICS, CollectionClassification.BAG ); - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // This test only makes sense if association properties *can* be uninitialized. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } - - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from AOwner" ).executeUpdate(); s.createQuery( "delete from ANonOwner" ).executeUpdate(); } ); } @Test - public void accessUnloadedLazyAssociationOnDeletedOwner() { - inTransaction( s -> { + public void accessUnloadedLazyAssociationOnDeletedOwner(SessionFactoryScope scope) { + scope.inTransaction( s -> { AssociationOwner owner = new AssociationOwner(); owner.setId( 1 ); for ( int i = 0; i < 2; i++ ) { @@ -90,7 +73,7 @@ public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest } s.persist( owner ); } ); - assertThatThrownBy( () -> inTransaction( session -> { + assertThatThrownBy( () -> scope.inTransaction( session -> { AssociationOwner owner = session.load( AssociationOwner.class, 1 ); session.delete( owner ); session.flush(); @@ -102,13 +85,13 @@ public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest } @Test - public void accessUnloadedLazyAssociationOnDeletedNonOwner() { - inTransaction( s -> { + public void accessUnloadedLazyAssociationOnDeletedNonOwner(SessionFactoryScope scope) { + scope.inTransaction( s -> { AssociationNonOwner nonOwner = new AssociationNonOwner(); nonOwner.setId( 1 ); s.persist( nonOwner ); } ); - assertThatThrownBy( () -> inTransaction( session -> { + assertThatThrownBy( () -> scope.inTransaction( session -> { AssociationNonOwner nonOwner = session.load( AssociationNonOwner.class, 1 ); session.delete( nonOwner ); session.flush(); @@ -121,7 +104,7 @@ public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest @Entity(name = "AOwner") @Table - private static class AssociationOwner { + static class AssociationOwner { @Id Integer id; @@ -149,7 +132,7 @@ public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest @Entity(name = "ANonOwner") @Table - private static class AssociationNonOwner { + static class AssociationNonOwner { @Id Integer id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceProxyTest.java index b439bdfe2b..700814b417 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceProxyTest.java @@ -14,39 +14,56 @@ import jakarta.persistence.InheritanceType; import jakarta.persistence.MappedSuperclass; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + DeepInheritanceProxyTest.AMappedSuperclass.class, + DeepInheritanceProxyTest.AEntity.class, + DeepInheritanceProxyTest.AAEntity.class, + DeepInheritanceProxyTest.AAAEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class DeepInheritanceProxyTest { @Test - public void testRootGetValueToInitialize() { - inTransaction( + public void testRootGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -63,9 +80,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -84,10 +101,10 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testRootSetValueToInitialize() { - inTransaction( + public void testRootSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -104,9 +121,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -123,9 +140,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.get( AEntity.class, "AEntity" ); @@ -142,10 +159,10 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testMiddleGetValueToInitialize() { - inTransaction( + public void testMiddleGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -164,9 +181,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -185,9 +202,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -208,10 +225,10 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testMiddleSetValueToInitialize() { - inTransaction( + public void testMiddleSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -230,9 +247,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -251,9 +268,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -272,9 +289,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.get( AAEntity.class, "AAEntity" ); @@ -293,10 +310,10 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testLeafGetValueToInitialize() { - inTransaction( + public void testLeafGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -317,9 +334,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -340,9 +357,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -365,10 +382,10 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testLeafSetValueToInitialize() { - inTransaction( + public void testLeafSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -389,9 +406,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -413,9 +430,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -436,9 +453,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -459,9 +476,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.get( AAAEntity.class, "AAAEntity" ); @@ -481,33 +498,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( AMappedSuperclass.class ); - sources.addAnnotatedClass( AEntity.class ); - sources.addAnnotatedClass( AAEntity.class ); - sources.addAnnotatedClass( AAAEntity.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { AEntity aEntity = new AEntity( "AEntity" ); aEntity.setFieldInAMappedSuperclass( (short) 2 ); @@ -530,9 +523,9 @@ public class DeepInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCas ); } - @After - public void clearTestData(){ - inTransaction( + @AfterEach + public void clearTestData(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createQuery( "delete from AEntity" ).executeUpdate(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceWithNonEntitiesProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceWithNonEntitiesProxyTest.java index 6cd61b495d..6d95ddaaf1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceWithNonEntitiesProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/DeepInheritanceWithNonEntitiesProxyTest.java @@ -14,40 +14,57 @@ import jakarta.persistence.InheritanceType; import jakarta.persistence.MappedSuperclass; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + DeepInheritanceWithNonEntitiesProxyTest.AMappedSuperclass.class, + DeepInheritanceWithNonEntitiesProxyTest.AEntity.class, + DeepInheritanceWithNonEntitiesProxyTest.AAEntity.class, + DeepInheritanceWithNonEntitiesProxyTest.AAAEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class DeepInheritanceWithNonEntitiesProxyTest { @Test - public void testRootGetValueToInitialize() { - inTransaction( + public void testRootGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -68,9 +85,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -93,10 +110,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testRootGetValueInNonEntity() { - inTransaction( + public void testRootGetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -118,9 +135,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -144,10 +161,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testRootSetValueToInitialize() { - inTransaction( + public void testRootSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -168,9 +185,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -191,9 +208,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.get( AEntity.class, "AEntity" ); @@ -212,10 +229,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testRootSetValueInNonEntity() { - inTransaction( + public void testRootSetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -243,9 +260,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.getReference( AEntity.class, "AEntity" ); @@ -273,9 +290,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AEntity aEntity = session.get( AEntity.class, "AEntity" ); @@ -295,10 +312,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testMiddleGetValueToInitialize() { - inTransaction( + public void testMiddleGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -322,9 +339,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -348,9 +365,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -376,10 +393,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testMiddleGetValueInNonEntity() { - inTransaction( + public void testMiddleGetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -404,9 +421,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -431,9 +448,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -460,10 +477,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testMiddleSetValueToInitialize() { - inTransaction( + public void testMiddleSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -487,9 +504,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -513,9 +530,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -539,9 +556,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.get( AAEntity.class, "AAEntity" ); @@ -563,10 +580,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testMiddleSetValueInNonEntity() { - inTransaction( + public void testMiddleSetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -599,9 +616,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -634,9 +651,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.getReference( AAEntity.class, "AAEntity" ); @@ -669,9 +686,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAEntity aaEntity = session.get( AAEntity.class, "AAEntity" ); @@ -694,10 +711,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testLeafGetValueToInitialize() { - inTransaction( + public void testLeafGetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -725,9 +742,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -755,9 +772,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -787,10 +804,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testLeafGetValueInNonEntity() { - inTransaction( + public void testLeafGetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -824,9 +841,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -859,9 +876,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -894,9 +911,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -931,10 +948,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testLeafSetValueToInitialize() { - inTransaction( + public void testLeafSetValueToInitialize(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -962,9 +979,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -993,9 +1010,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1023,9 +1040,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1053,9 +1070,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.get( AAAEntity.class, "AAAEntity" ); @@ -1080,10 +1097,10 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } @Test - public void testLeafSetValueInNonEntity() { - inTransaction( + public void testLeafSetValueInNonEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1124,9 +1141,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1168,9 +1185,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1213,9 +1230,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.getReference( AAAEntity.class, "AAAEntity" ); @@ -1258,9 +1275,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu } ); - inTransaction( + scope.inTransaction( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); AAAEntity aaaEntity = session.get( AAAEntity.class, "AAAEntity" ); @@ -1285,33 +1302,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( AMappedSuperclass.class ); - sources.addAnnotatedClass( AEntity.class ); - sources.addAnnotatedClass( AAEntity.class ); - sources.addAnnotatedClass( AAAEntity.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { AEntity aEntity = new AEntity( "AEntity" ); aEntity.setFieldInAMappedSuperclass( (short) 2 ); @@ -1343,9 +1336,9 @@ public class DeepInheritanceWithNonEntitiesProxyTest extends BaseNonConfigCoreFu ); } - @After - public void clearTestData(){ - inTransaction( + @AfterEach + public void clearTestData(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createQuery( "delete from AEntity" ).executeUpdate(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EagerOneToOneMappedByInDoubleEmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EagerOneToOneMappedByInDoubleEmbeddedTest.java index d4525091c7..83b90e7815 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EagerOneToOneMappedByInDoubleEmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EagerOneToOneMappedByInDoubleEmbeddedTest.java @@ -2,37 +2,38 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import java.io.Serializable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + EagerOneToOneMappedByInDoubleEmbeddedTest.EntityA.class, EagerOneToOneMappedByInDoubleEmbeddedTest.EntityB.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-15967") -public class EagerOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-15967") +public class EagerOneToOneMappedByInDoubleEmbeddedTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1 ); EntityB entityB = new EntityB( 2 ); @@ -49,9 +50,9 @@ public class EagerOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCore } ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from EntityB" ).executeUpdate(); session.createQuery( "delete from EntityA" ).executeUpdate(); @@ -60,8 +61,8 @@ public class EagerOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCore } @Test - public void testGetEntityA() { - inTransaction( + public void testGetEntityA(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityA entityA = session.get( EntityA.class, 1 ); assertThat( entityA ).isNotNull(); @@ -74,8 +75,8 @@ public class EagerOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCore } @Test - public void testGetReferenceEntityA() { - inTransaction( + public void testGetReferenceEntityA(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityA entityA = session.getReference( EntityA.class, 1 ); assertThat( entityA ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EntitySharedInCollectionAndToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EntitySharedInCollectionAndToOneTest.java index cfba317196..707f29db31 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EntitySharedInCollectionAndToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/EntitySharedInCollectionAndToOneTest.java @@ -20,20 +20,18 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyGroup; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -41,13 +39,29 @@ import static org.hamcrest.MatcherAssert.assertThat; /** * @author Steve Ebersole */ -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + EntitySharedInCollectionAndToOneTest.CodeTableItem.class, + EntitySharedInCollectionAndToOneTest.CodeTable.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class EntitySharedInCollectionAndToOneTest extends BaseNonConfigCoreFunctionalTestCase { +public class EntitySharedInCollectionAndToOneTest { + @Test - public void testIt() { - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { int passes = 0; for ( CodeTable codeTable : session.createQuery( "from CodeTable ct where ct.id = 2", CodeTable.class ).list() ) { @@ -61,9 +75,9 @@ public class EntitySharedInCollectionAndToOneTest extends BaseNonConfigCoreFunct ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final CodeTable codeTable1 = new CodeTable( 1, 1 ); final CodeTableItem item1 = new CodeTableItem( 1, 1, "first" ); @@ -113,27 +127,6 @@ public class EntitySharedInCollectionAndToOneTest extends BaseNonConfigCoreFunct // ); // } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( CodeTableItem.class ); - sources.addAnnotatedClass( CodeTable.class ); - } - @MappedSuperclass public static class BaseEntity { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java index 280e4ab2b2..9d73e82c99 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/FetchGraphTest.java @@ -13,30 +13,26 @@ import java.util.List; import java.util.Set; import org.hibernate.Hibernate; -import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.annotations.LazyGroup; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.dialect.DB2Dialect; -import org.hibernate.dialect.DerbyDialect; -import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.proxy.HibernateProxy; import org.hibernate.query.Query; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Column; @@ -60,28 +56,56 @@ import static org.junit.Assert.assertTrue; /** * @author Steve Ebersole */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + FetchGraphTest.AEntity.class, + FetchGraphTest.BEntity.class, + FetchGraphTest.CEntity.class, + FetchGraphTest.DEntity.class, + FetchGraphTest.EEntity.class, + FetchGraphTest.GEntity.class, + Activity.class, + Instruction.class, + WebApplication.class, + SpecializedKey.class, + MoreSpecializedKey.class, + RoleEntity.class, + AbstractKey.class, + GenericKey.class, + SpecializedEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { +public class FetchGraphTest { @Test - public void testLoadNonOwningOneToOne() { + public void testLoadNonOwningOneToOne(SessionFactoryScope scope) { // Test loading D and accessing E // E is the owner of the FK, not D. When `D#e` is accessed we // need to actually load E because its table has the FK value, not // D's table - final StatisticsImplementor stats = sessionFactory().getStatistics(); + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ) .getBytecodeEnhancementMetadata() .isEnhancedForLazyLoading(); - inSession( + scope.inSession( session -> { final DEntity entityD = session.load( DEntity.class, 1L ); assertThat( stats.getPrepareStatementCount(), is( 0L ) ); @@ -103,19 +127,19 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testLoadOwningOneToOne() { + public void testLoadOwningOneToOne(SessionFactoryScope scope) { // switch it around - final StatisticsImplementor stats = sessionFactory().getStatistics(); + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ) .getBytecodeEnhancementMetadata() .isEnhancedForLazyLoading(); - inSession( + scope.inSession( session -> { final EEntity entityE = session.load( EEntity.class, 17L ); assertThat( stats.getPrepareStatementCount(), is( 0L ) ); @@ -131,9 +155,9 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void basicTypeLazyGroup() { + public void basicTypeLazyGroup(SessionFactoryScope scope) { - inSession( + scope.inSession( session -> { final String qry = "select d from D d"; @@ -150,7 +174,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inSession( + scope.inSession( session -> { final DEntity dEntity = session.get( DEntity.class, 1L ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); @@ -168,7 +192,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inSession( + scope.inSession( session -> { final DEntity dEntity = session.get( DEntity.class, 1L ); assertFalse( Hibernate.isPropertyInitialized( dEntity, "blob" ) ); @@ -185,7 +209,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inSession( + scope.inSession( session -> { final String qry = "select e from E e join fetch e.d"; @@ -217,17 +241,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testFetchingScroll() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testFetchingScroll(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ) .getBytecodeEnhancementMetadata() .isEnhancedForLazyLoading(); - inStatelessSession( + scope.inStatelessSession( session -> { final String qry = "select e from E e join fetch e.d"; @@ -243,7 +267,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inStatelessSession( + scope.inStatelessSession( session -> { final String qry = "select d from D d " + "join fetch d.a " + @@ -265,7 +289,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } ); - inStatelessSession( + scope.inStatelessSession( session -> { final String qry = "select g from G g join fetch g.dEntities"; @@ -280,17 +304,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testFetchingScroll2() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testFetchingScroll2(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ) .getBytecodeEnhancementMetadata() .isEnhancedForLazyLoading(); - inStatelessSession( + scope.inStatelessSession( session -> { final String qry = "select d, d.d from D d " + "join fetch d.a " + @@ -320,15 +344,15 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testLazyAssociationSameAsNonLazyInPC() { + public void testLazyAssociationSameAsNonLazyInPC(SessionFactoryScope scope) { - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ) .getBytecodeEnhancementMetadata() .isEnhancedForLazyLoading(); - inSession( + scope.inSession( session -> { final AEntity entityA = session.get( AEntity.class, 1L ); @@ -345,16 +369,16 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testRandomAccess() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testRandomAccess(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final EntityPersister persister = sessionFactory().getRuntimeMetamodels() + final EntityPersister persister = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( DEntity.class ); assert persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading(); - inSession( + scope.inSession( session -> { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Load a D @@ -456,11 +480,11 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testNullManyToOneHql() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testNullManyToOneHql(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( session -> { final String qry = "select e from Activity e"; final List activities = session.createQuery( qry, Activity.class ).list(); @@ -493,17 +517,17 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testAbstractClassAssociation() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testAbstractClassAssociation(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - assert sessionFactory().getRuntimeMetamodels() + assert scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( RoleEntity.class ) .getInstrumentationMetadata() .isEnhancedForLazyLoading(); - inTransaction( + scope.inTransaction( session -> { final String qry = "select e from RoleEntity e"; final List keyRoleEntities = session.createQuery( qry, RoleEntity.class ).list(); @@ -534,11 +558,11 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testNonAbstractAssociationWithSubclassValue() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testNonAbstractAssociationWithSubclassValue(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( session -> { final String qry = "select e from RoleEntity e"; final List keyRoleEntities = session.createQuery( qry, RoleEntity.class ).list(); @@ -562,8 +586,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testQueryAndDeleteDEntity() { - inTransaction( + public void testQueryAndDeleteDEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { List result = session.createQuery( "select d from D d ", @@ -584,8 +608,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testLoadAndDeleteDEntity() { - inTransaction( + public void testLoadAndDeleteDEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { DEntity entity = session.load( DEntity.class, 1L ); session.delete( entity ); @@ -600,8 +624,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testGetAndDeleteDEntity() { - inTransaction( + public void testGetAndDeleteDEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { DEntity entity = session.get( DEntity.class, 1L ); session.delete( entity ); @@ -616,8 +640,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testGetAndDeleteEEntity() { - inTransaction( + public void testGetAndDeleteEEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { EEntity entity = session.get( EEntity.class, 17L ); session.delete( entity ); @@ -627,8 +651,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testLoadAndDeleteEEntity() { - inTransaction( + public void testLoadAndDeleteEEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { EEntity entity = session.load( EEntity.class, 17L ); session.delete( entity ); @@ -638,8 +662,8 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testQueryAndDeleteEEntity() { - inTransaction( + public void testQueryAndDeleteEEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { List result = session.createQuery( "select e from E e", @@ -653,45 +677,9 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( AEntity.class ); - sources.addAnnotatedClass( BEntity.class ); - sources.addAnnotatedClass( CEntity.class ); - sources.addAnnotatedClass( DEntity.class ); - sources.addAnnotatedClass( EEntity.class ); - sources.addAnnotatedClass( GEntity.class ); - - sources.addAnnotatedClass( Activity.class ); - sources.addAnnotatedClass( Instruction.class ); - sources.addAnnotatedClass( WebApplication.class ); - - sources.addAnnotatedClass( SpecializedKey.class ); - sources.addAnnotatedClass( MoreSpecializedKey.class ); - sources.addAnnotatedClass( RoleEntity.class ); - sources.addAnnotatedClass( AbstractKey.class ); - sources.addAnnotatedClass( GenericKey.class ); - sources.addAnnotatedClass( SpecializedEntity.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { DEntity d = new DEntity(); d.setD( "bla" ); @@ -801,9 +789,9 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from E" ).executeUpdate(); session.createQuery( "delete from D" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/IdClassEntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/IdClassEntityGraphTest.java index 2ee74377b2..06925fc10d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/IdClassEntityGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/IdClassEntityGraphTest.java @@ -14,17 +14,17 @@ import java.util.List; import java.util.Objects; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; import org.hibernate.jpa.SpecHints; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Column; @@ -43,21 +43,21 @@ import jakarta.persistence.Table; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-15607") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-15607") +@DomainModel( + annotatedClasses = { + IdClassEntityGraphTest.Parent.class, + IdClassEntityGraphTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class IdClassEntityGraphTest extends BaseNonConfigCoreFunctionalTestCase { +public class IdClassEntityGraphTest { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Parent.class ); - sources.addAnnotatedClass( Child.class ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = new Parent( 1l, "abc" ); Child child1 = new Child( parent, LocalDateTime.of( 2002, Month.APRIL, 12, 12, 12 ) ); @@ -69,9 +69,9 @@ public class IdClassEntityGraphTest extends BaseNonConfigCoreFunctionalTestCase ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Child" ).executeUpdate(); session.createQuery( "delete from Parent" ).executeUpdate(); @@ -80,8 +80,8 @@ public class IdClassEntityGraphTest extends BaseNonConfigCoreFunctionalTestCase } @Test - public void testFetchBasicAttributeAndOneToMany() { - inTransaction( + public void testFetchBasicAttributeAndOneToMany(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = session.createQuery( "SELECT p FROM Parent p WHERE p.id = :id", Parent.class ) .setParameter( "id", 1L ) @@ -98,8 +98,8 @@ public class IdClassEntityGraphTest extends BaseNonConfigCoreFunctionalTestCase } @Test - public void testFetchBasicAttributeOnly() { - inTransaction( + public void testFetchBasicAttributeOnly(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = session.createQuery( "SELECT p FROM Parent p WHERE p.id = :id", Parent.class ) .setParameter( "id", 1L ) @@ -116,8 +116,8 @@ public class IdClassEntityGraphTest extends BaseNonConfigCoreFunctionalTestCase } @Test - public void testFetchOneToMany() { - inTransaction( + public void testFetchOneToMany(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = session.createQuery( "SELECT p FROM Parent p WHERE p.id = :id", Parent.class ) .setParameter( "id", 1L ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyCollectionDeletedAllowProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyCollectionDeletedAllowProxyTest.java index 420f3a12c0..63fd896e76 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyCollectionDeletedAllowProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyCollectionDeletedAllowProxyTest.java @@ -20,51 +20,54 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Query; import jakarta.persistence.Table; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + LazyCollectionDeletedAllowProxyTest.Post.class, + LazyCollectionDeletedAllowProxyTest.Tag.class, + LazyCollectionDeletedAllowProxyTest.AdditionalDetails.class, + LazyCollectionDeletedAllowProxyTest.Label.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class LazyCollectionDeletedAllowProxyTest { private Long postId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Post.class, Tag.class, AdditionalDetails.class, Label.class }; - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); - } - @Test - public void updatingAnAttributeDoesNotDeleteLazyCollectionsTest() { - doInHibernate( this::sessionFactory, s -> { + public void updatingAnAttributeDoesNotDeleteLazyCollectionsTest(SessionFactoryScope scope) { + scope.inTransaction( s -> { Query query = s.createQuery( "from AdditionalDetails where id = :id" ); query.setParameter( "id", postId ); AdditionalDetails additionalDetails = (AdditionalDetails) query.getSingleResult(); @@ -72,16 +75,16 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti s.persist( additionalDetails ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Query query = s.createQuery( "from Post where id = :id" ); query.setParameter( "id", postId ); Post retrievedPost = (Post) query.getSingleResult(); - assertFalse( "No tags found", retrievedPost.getTags().isEmpty() ); + assertFalse( retrievedPost.getTags().isEmpty(), "No tags found" ); retrievedPost.getTags().forEach( tag -> assertNotNull( tag ) ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Query query = s.createQuery( "from AdditionalDetails where id = :id" ); query.setParameter( "id", postId ); AdditionalDetails additionalDetails = (AdditionalDetails) query.getSingleResult(); @@ -91,24 +94,24 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti post.setMessage( "new message" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Query query = s.createQuery( "from Post where id = :id" ); query.setParameter( "id", postId ); Post retrievedPost = (Post) query.getSingleResult(); assertEquals( "new message", retrievedPost.getMessage() ); - assertFalse( "No tags found", retrievedPost.getTags().isEmpty() ); + assertFalse( retrievedPost.getTags().isEmpty(), "No tags found" ); retrievedPost.getTags().forEach( tag -> { assertNotNull( tag ); - assertFalse( "No Labels found", tag.getLabels().isEmpty() ); + assertFalse( tag.getLabels().isEmpty(), "No Labels found" ); } ); } ); } - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Post post = new Post(); Tag tag1 = new Tag( "tag1" ); @@ -134,9 +137,9 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti } ); } - @After - public void cleanData() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanData(SessionFactoryScope scope) { + scope.inTransaction( s -> { Query query = s.createQuery( "from Post" ); List posts = query.getResultList(); posts.forEach( post -> { @@ -158,7 +161,7 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti @Entity(name = "Tag") @Table(name = "TAG") - private static class Tag { + static class Tag { @Id @GeneratedValue @@ -215,7 +218,7 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti @Entity(name = "Post") @Table(name = "POST") - private static class Post { + static class Post { @Id @GeneratedValue @@ -256,7 +259,7 @@ public class LazyCollectionDeletedAllowProxyTest extends BaseNonConfigCoreFuncti @Entity(name = "AdditionalDetails") @Table(name = "ADDITIONAL_DETAILS") - private static class AdditionalDetails { + static class AdditionalDetails { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceAllowProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceAllowProxyTest.java index e1abeda65e..197dc08830 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceAllowProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceAllowProxyTest.java @@ -10,39 +10,63 @@ import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Steve Ebersole */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + Customer.class, + ForeignCustomer.class, + DomesticCustomer.class, + Payment.class, + CreditCardPayment.class, + DebitCardPayment.class, + Address.class, + Order.class, + OrderSupplemental.class, + OrderSupplemental2.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class LazyGroupWithInheritanceAllowProxyTest { + @Test - public void baseline() { - inTransaction( + public void baseline(SessionFactoryScope scope) { + scope.inTransaction( session -> { final List orders = session.createQuery( "select o from Order o", Order.class ).list(); for ( Order order : orders ) { @@ -57,8 +81,8 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun } @Test - public void testMergingUninitializedProxy() { - inTransaction( + public void testMergingUninitializedProxy(SessionFactoryScope scope) { + scope.inTransaction( session -> { final List orders = session.createQuery( "select o from Order o", Order.class ).list(); for ( Order order : orders ) { @@ -74,13 +98,13 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun @Test - public void queryEntityWithAssociationToAbstract() { - final Statistics stats = sessionFactory().getStatistics(); + public void queryEntityWithAssociationToAbstract(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final AtomicInteger expectedQueryCount = new AtomicInteger( 0 ); - inTransaction( + scope.inTransaction( session -> { final List orders = session.createQuery( "select o from Order o", Order.class ).list(); @@ -126,17 +150,17 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun } /** - * Same test as {@link #queryEntityWithAssociationToAbstract()}, but using runtime + * Same test as {@link #queryEntityWithAssociationToAbstract(SessionFactoryScope)}, but using runtime * fetching to issues just a single select */ @Test - public void queryEntityWithAssociationToAbstractRuntimeFetch() { - final Statistics stats = sessionFactory().getStatistics(); + public void queryEntityWithAssociationToAbstractRuntimeFetch(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final AtomicInteger expectedQueryCount = new AtomicInteger( 0 ); - inTransaction( + scope.inTransaction( session -> { final String qry = "select o from Order o join fetch o.customer c join fetch o.payments join fetch o.supplemental join fetch o.supplemental2"; @@ -187,19 +211,9 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.USE_QUERY_CACHE, "false" ); - } - - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Address austin = new Address( 1, "Austin" ); final Address london = new Address( 2, "London" ); @@ -251,9 +265,9 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from CreditCardPayment" ).executeUpdate(); session.createQuery( "delete from DebitCardPayment" ).executeUpdate(); @@ -271,24 +285,4 @@ public class LazyGroupWithInheritanceAllowProxyTest extends BaseNonConfigCoreFun } ); } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( ForeignCustomer.class ); - sources.addAnnotatedClass( DomesticCustomer.class ); - - sources.addAnnotatedClass( Payment.class ); - sources.addAnnotatedClass( CreditCardPayment.class ); - sources.addAnnotatedClass( DebitCardPayment.class ); - - sources.addAnnotatedClass( Address.class ); - - sources.addAnnotatedClass( Order.class ); - sources.addAnnotatedClass( OrderSupplemental.class ); - sources.addAnnotatedClass( OrderSupplemental2.class ); - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceTest.java index 2936c9dd5b..cec1a92323 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyGroupWithInheritanceTest.java @@ -11,42 +11,66 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Steve Ebersole */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + Customer.class, + ForeignCustomer.class, + DomesticCustomer.class, + Payment.class, + CreditCardPayment.class, + DebitCardPayment.class, + Address.class, + Order.class, + OrderSupplemental.class, + OrderSupplemental2.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTestCase { +public class LazyGroupWithInheritanceTest { + @Test - public void loadEntityWithAssociationToAbstract() { - final Statistics stats = sessionFactory().getStatistics(); + public void loadEntityWithAssociationToAbstract(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( (session) -> { final Order loaded = session.byId( Order.class ).load( 1 ); assert Hibernate.isPropertyInitialized( loaded, "customer" ); @@ -61,13 +85,13 @@ public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTes } @Test - public void queryEntityWithAssociationToAbstract() { - final Statistics stats = sessionFactory().getStatistics(); + public void queryEntityWithAssociationToAbstract(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final AtomicInteger expectedQueryCount = new AtomicInteger( 0 ); - inTransaction( + scope.inTransaction( session -> { final List orders = session.createQuery( "select o from Order o", Order.class ).list(); @@ -121,17 +145,17 @@ public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTes } /** - * Same test as {@link #queryEntityWithAssociationToAbstract()}, but using runtime + * Same test as {@link #queryEntityWithAssociationToAbstract(SessionFactoryScope)}, but using runtime * fetching to issues just a single select */ @Test - public void queryEntityWithAssociationToAbstractRuntimeFetch() { - final Statistics stats = sessionFactory().getStatistics(); + public void queryEntityWithAssociationToAbstractRuntimeFetch(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final AtomicInteger expectedQueryCount = new AtomicInteger( 0 ); - inTransaction( + scope.inTransaction( session -> { final String qry = "select o from Order o join fetch o.customer c join fetch o.payments join fetch o.supplemental join fetch o.supplemental2"; @@ -182,19 +206,9 @@ public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTes ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.USE_QUERY_CACHE, "false" ); - } - - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Address austin = new Address( 1, "Austin" ); final Address london = new Address( 2, "London" ); @@ -246,9 +260,9 @@ public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTes ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from CreditCardPayment" ).executeUpdate(); session.createQuery( "delete from DebitCardPayment" ).executeUpdate(); @@ -266,25 +280,4 @@ public class LazyGroupWithInheritanceTest extends BaseNonConfigCoreFunctionalTes } ); } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( ForeignCustomer.class ); - sources.addAnnotatedClass( DomesticCustomer.class ); - - sources.addAnnotatedClass( Payment.class ); - sources.addAnnotatedClass( CreditCardPayment.class ); - sources.addAnnotatedClass( DebitCardPayment.class ); - - sources.addAnnotatedClass( Address.class ); - - sources.addAnnotatedClass( Order.class ); - sources.addAnnotatedClass( OrderSupplemental.class ); - sources.addAnnotatedClass( OrderSupplemental2.class ); - } - - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInDoubleEmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInDoubleEmbeddedTest.java index ccb6d5c7b9..2e70ba9056 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInDoubleEmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInDoubleEmbeddedTest.java @@ -2,14 +2,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import java.io.Serializable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -18,21 +19,22 @@ import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyOneToOneMappedByInDoubleEmbeddedTest.EntityA.class, LazyOneToOneMappedByInDoubleEmbeddedTest.EntityB.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-15967") -public class LazyOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-15967") +public class LazyOneToOneMappedByInDoubleEmbeddedTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1 ); EntityB entityB = new EntityB( 2 ); @@ -49,9 +51,9 @@ public class LazyOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreF } ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from EntityB" ).executeUpdate(); session.createQuery( "delete from EntityA" ).executeUpdate(); @@ -60,8 +62,8 @@ public class LazyOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreF } @Test - public void testGetEntityA() { - inTransaction( + public void testGetEntityA(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityA entityA = session.get( EntityA.class, 1 ); assertThat( entityA ).isNotNull(); @@ -74,8 +76,8 @@ public class LazyOneToOneMappedByInDoubleEmbeddedTest extends BaseNonConfigCoreF } @Test - public void testGetReferenceEntityA() { - inTransaction( + public void testGetReferenceEntityA(SessionFactoryScope scope) { + scope.inTransaction( session -> { EntityA entityA = session.getReference( EntityA.class, 1 ); assertThat( entityA ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInEmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInEmbeddedTest.java index 49626d2492..3ca6bce570 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInEmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByInEmbeddedTest.java @@ -8,14 +8,15 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import java.io.Serializable; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -26,19 +27,20 @@ import jakarta.persistence.OneToOne; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyOneToOneMappedByInEmbeddedTest.EntityA.class, LazyOneToOneMappedByInEmbeddedTest.EntityB.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-15606") -public class LazyOneToOneMappedByInEmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-15606") +public class LazyOneToOneMappedByInEmbeddedTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1 ); EntityB entityB = new EntityB( 2 ); entityA.getEmbedded().setEntityB( entityB ); @@ -48,17 +50,17 @@ public class LazyOneToOneMappedByInEmbeddedTest extends BaseNonConfigCoreFunctio } ); } - @After - public void tearDown() { - inTransaction( s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createMutationQuery( "delete entityb" ).executeUpdate(); s.createMutationQuery( "delete entitya" ).executeUpdate(); } ); } @Test - public void testGet() { - inTransaction( s -> { + public void testGet(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = s.get( EntityA.class, "1" ); assertThat( entityA ).isNotNull(); @@ -72,8 +74,8 @@ public class LazyOneToOneMappedByInEmbeddedTest extends BaseNonConfigCoreFunctio } @Test - public void testGetReference() { - inTransaction( s -> { + public void testGetReference(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = s.getReference( EntityA.class, "1" ); assertThat( entityA ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByTest.java index 47b7e0f2b6..f27509da11 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMappedByTest.java @@ -6,20 +6,21 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; -import java.util.Map; - import org.hibernate.Hibernate; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -29,24 +30,25 @@ import jakarta.persistence.OneToOne; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyOneToOneMappedByTest.EntityA.class, LazyOneToOneMappedByTest.EntityB.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-15606") -public class LazyOneToOneMappedByTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-15606") +public class LazyOneToOneMappedByTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Override - protected void addSettings(Map settings) { - settings.put( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1, "A" ); EntityB entityB = new EntityB( 2 ); entityA.setEntityB( entityB ); @@ -56,19 +58,19 @@ public class LazyOneToOneMappedByTest extends BaseNonConfigCoreFunctionalTestCas } ); } - @After - public void tearDown() { - inTransaction( s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createMutationQuery( "delete entityb" ).executeUpdate(); s.createMutationQuery( "delete entitya" ).executeUpdate(); } ); } @Test - public void testGet() { - final Statistics stats = sessionFactory().getStatistics(); + public void testGet(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( s -> { + scope.inTransaction( s -> { EntityA entityA = s.get( EntityA.class, "1" ); assertThat( stats.getPrepareStatementCount() ).isEqualTo( 1 ); assertThat( entityA ).isNotNull(); @@ -86,10 +88,10 @@ public class LazyOneToOneMappedByTest extends BaseNonConfigCoreFunctionalTestCas } @Test - public void testGetReference() { - final Statistics stats = sessionFactory().getStatistics(); + public void testGetReference(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( s -> { + scope.inTransaction( s -> { EntityA entityA = s.getReference( EntityA.class, "1" ); assertThat( stats.getPrepareStatementCount() ).isEqualTo( 0 ); assertThat( entityA ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMultiAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMultiAssociationTest.java index ea5291cc2f..c1225c188b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMultiAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyOneToOneMultiAssociationTest.java @@ -11,49 +11,51 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import org.hibernate.Hibernate; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyOneToOneMultiAssociationTest.EntityA.class, LazyOneToOneMultiAssociationTest.EntityB.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -@TestForIssue(jiraKey = "HHH-16108") -public class LazyOneToOneMultiAssociationTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-16108") +public class LazyOneToOneMultiAssociationTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { EntityA.class, EntityB.class }; - } - - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = new EntityA( 1 ); s.persist( entityA ); } ); } - @After - public void tearDown() { - inTransaction( s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createMutationQuery( "delete entityb" ).executeUpdate(); s.createMutationQuery( "delete entitya" ).executeUpdate(); } ); } @Test - public void testPersist() { - inTransaction( s -> { + public void testPersist(SessionFactoryScope scope) { + scope.inTransaction( s -> { EntityA entityA = s.get( EntityA.class, 1 ); EntityB entityB = new EntityB( 2 ); entityA.setMappedAssociation1( entityB ); @@ -64,7 +66,7 @@ public class LazyOneToOneMultiAssociationTest extends BaseNonConfigCoreFunctiona // at org.hibernate.persister.entity.mutation.UpdateCoordinatorStandard.processSet(UpdateCoordinatorStandard.java:665) } ); - inTransaction( s -> { + scope.inTransaction( s -> { EntityA entityA = s.get( EntityA.class, 1 ); assertThat( entityA ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyProxyBytecodeEnhancementCollectionInitializationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyProxyBytecodeEnhancementCollectionInitializationTest.java index ca0f26e5d8..0070b0007c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyProxyBytecodeEnhancementCollectionInitializationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyProxyBytecodeEnhancementCollectionInitializationTest.java @@ -15,12 +15,13 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -36,26 +37,26 @@ import jakarta.persistence.Table; *

* See this comment. */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyProxyBytecodeEnhancementCollectionInitializationTest.Parent.class, + LazyProxyBytecodeEnhancementCollectionInitializationTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LazyProxyBytecodeEnhancementCollectionInitializationTest - extends BaseCoreFunctionalTestCase { +public class LazyProxyBytecodeEnhancementCollectionInitializationTest { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class }; - } - - @Before - public void checkSettings() { + @BeforeEach + public void checkSettings(SessionFactoryScope scope) { // We want to test this configuration exactly - assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); + assertTrue( scope.getSessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() ); } - @Before - public void prepare() { - inTransaction( s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); parent.setId( 1 ); for ( int i = 0; i < 2; i++ ) { @@ -70,8 +71,8 @@ public class LazyProxyBytecodeEnhancementCollectionInitializationTest } @Test - public void collectionInitializationOnLazyProxy() { - inTransaction( s -> { + public void collectionInitializationOnLazyProxy(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = s.getReference( Parent.class, 1 ); assertThat( Hibernate.isPropertyInitialized( parent, "children") ).isFalse(); assertThat( s.unwrap( SessionImplementor.class ).getPersistenceContext().getCollectionEntries() ) @@ -92,7 +93,7 @@ public class LazyProxyBytecodeEnhancementCollectionInitializationTest @Entity(name = "Parent") @Table - private static class Parent { + static class Parent { @Id Integer id; @@ -119,7 +120,7 @@ public class LazyProxyBytecodeEnhancementCollectionInitializationTest @Entity(name = "Child") @Table - private static class Child { + static class Child { @Id Integer id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.java index b2e64740ec..685d0b3967 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.java @@ -7,17 +7,19 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -27,38 +29,38 @@ import jakarta.persistence.InheritanceType; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) -public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.Animal.class, + LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.Primate.class, + LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.Human.class, + LazyToOnesNoProxyFactoryWithSubclassesStatefulTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - sources.addAnnotatedClass( Primate.class ); - sources.addAnnotatedClass( Human.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } - @Test - public void testNewEnhancedProxyAssociation() { - inTransaction( + public void testNewEnhancedProxyAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -69,9 +71,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC } ); - inSession( + scope.inSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) ); @@ -83,8 +85,8 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC } @Test - public void testExistingInitializedAssociationLeafSubclass() { - inTransaction( + public void testExistingInitializedAssociationLeafSubclass(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -96,10 +98,10 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC } ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inSession( + scope.inSession( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -145,8 +147,8 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC } @Test - public void testExistingEnhancedProxyAssociationLeafSubclassOnly() { - inTransaction( + public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -157,9 +159,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC } ); - inSession( + scope.inSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertNull( otherEntity.animal ); @@ -176,9 +178,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatefulTest extends BaseNonC ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Human" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java index 479966d629..03947c2b82 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.java @@ -7,17 +7,19 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -27,39 +29,38 @@ import jakarta.persistence.InheritanceType; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) -public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - sources.addAnnotatedClass( Primate.class ); - sources.addAnnotatedClass( Human.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.Animal.class, + LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.Primate.class, + LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.Human.class, + LazyToOnesNoProxyFactoryWithSubclassesStatelessTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest { @Test - public void testNewEnhancedProxyAssociation() { - inStatelessTransaction( + public void testNewEnhancedProxyAssociation(SessionFactoryScope scope) { + scope.inStatelessTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -70,9 +71,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) ); @@ -84,8 +85,8 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon } @Test - public void testExistingInitializedAssociationLeafSubclass() { - inStatelessSession( + public void testExistingInitializedAssociationLeafSubclass(SessionFactoryScope scope) { + scope.inStatelessSession( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -97,10 +98,10 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon } ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inStatelessSession( + scope.inStatelessSession( session -> { final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); @@ -145,8 +146,8 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon } @Test - public void testExistingEnhancedProxyAssociationLeafSubclassOnly() { - inStatelessSession( + public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryScope scope) { + scope.inStatelessSession( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -157,9 +158,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = (OtherEntity) session.get( OtherEntity.class, "test1" ); @@ -177,9 +178,9 @@ public class LazyToOnesNoProxyFactoryWithSubclassesStatelessTest extends BaseNon ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Human" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyMergeWithSubclassesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyMergeWithSubclassesTest.java index f1970715bc..2f0b6e03cd 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyMergeWithSubclassesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyMergeWithSubclassesTest.java @@ -16,64 +16,60 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + LazyToOnesProxyMergeWithSubclassesTest.Animal.class, + LazyToOnesProxyMergeWithSubclassesTest.Primate.class, + LazyToOnesProxyMergeWithSubclassesTest.Human.class, + LazyToOnesProxyMergeWithSubclassesTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - sources.addAnnotatedClass( Primate.class ); - sources.addAnnotatedClass( Human.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +public class LazyToOnesProxyMergeWithSubclassesTest { @Test - public void mergeUpdatedHibernateProxy() { + public void mergeUpdatedHibernateProxy(SessionFactoryScope scope) { - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity withInitializedHibernateProxy = doInHibernate( - this::sessionFactory, + final OtherEntity withInitializedHibernateProxy = scope.fromTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -93,15 +89,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 2, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); stats.clear(); // merge updated HibernateProxy to updated HibernateProxy withInitializedHibernateProxy.getHuman().setAge( 2 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -129,15 +124,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 2 ); + checkAgeInNewSession( scope, 2 ); stats.clear(); // merge updated HibernateProxy to updated enhanced entity withInitializedHibernateProxy.getHuman().setAge( 4 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -166,15 +160,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 4 ); + checkAgeInNewSession( scope, 4 ); stats.clear(); // merge updated HibernateProxy to uninitialized HibernateProxy withInitializedHibernateProxy.getHuman().setAge( 6 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -203,15 +196,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 6 ); + checkAgeInNewSession( scope, 6 ); stats.clear(); // merge updated HibernateProxy To uninitialized enhanced proxy withInitializedHibernateProxy.getHuman().setAge( 7 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -234,19 +226,18 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 7 ); + checkAgeInNewSession( scope, 7 ); } @Test - public void mergeUpdatedEnhancedProxy() { + public void mergeUpdatedEnhancedProxy(SessionFactoryScope scope) { - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity withInitializedEnhancedProxy = doInHibernate( - this::sessionFactory, + final OtherEntity withInitializedEnhancedProxy = scope.fromTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); @@ -272,15 +263,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 2, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); stats.clear(); // merge updated enhanced proxy to updated HibernateProxy withInitializedEnhancedProxy.getHuman().setAge( 2 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -309,15 +299,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 2 ); + checkAgeInNewSession( scope, 2 ); stats.clear(); // merge updated enhanced proxy to updated enhanced proxy withInitializedEnhancedProxy.getHuman().setAge( 4 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -344,15 +333,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 4 ); + checkAgeInNewSession( scope, 4 ); stats.clear(); // merge updated enhanced proxy to uninitialized HibernateProxy withInitializedEnhancedProxy.getHuman().setAge( 6 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -374,15 +362,14 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 6 ); + checkAgeInNewSession( scope, 6 ); stats.clear(); // merge updated enhanced proxy to uninitialized enhanced proxy withInitializedEnhancedProxy.getHuman().setAge( 7 ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -405,19 +392,18 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 7 ); + checkAgeInNewSession( scope, 7 ); } @Test - public void mergeUninitializedHibernateProxy() { + public void mergeUninitializedHibernateProxy(SessionFactoryScope scope) { - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity withUninitializedHibernateProxy = doInHibernate( - this::sessionFactory, + final OtherEntity withUninitializedHibernateProxy = scope.fromTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -434,13 +420,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); stats.clear(); // merge uninitialized HibernateProxy to updated HibernateProxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -470,13 +455,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 3 ); + checkAgeInNewSession( scope, 3 ); stats.clear(); // merge uninitialized HibernateProxy to updated enhanced proxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -503,13 +487,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); stats.clear(); // merge uninitialized HibernateProxy to uninitialized HibernateProxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -528,13 +511,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); stats.clear(); // merge uninitialized HibernateProxy to uninitialized enhanced proxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -555,19 +537,18 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); } @Test - public void testmergeUninitializedEnhancedProxy() { + public void testmergeUninitializedEnhancedProxy(SessionFactoryScope scope) { - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - final OtherEntity withUninitializedEnhancedProxy = doInHibernate( - this::sessionFactory, + final OtherEntity withUninitializedEnhancedProxy = scope.fromTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); @@ -588,13 +569,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 1 ); + checkAgeInNewSession( scope, 1 ); stats.clear(); // merge uninitialized enhanced proxy to updated HibernateProxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -623,13 +603,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 3 ); + checkAgeInNewSession( scope, 3 ); stats.clear(); // merge uninitialized enhanced proxy to updated enhanced entity - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -656,13 +635,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 3, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); stats.clear(); // merge uninitialized enhanced proxy to uninitialized HibernateProxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertEquals( 1, stats.getPrepareStatementCount() ); @@ -681,13 +659,12 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); stats.clear(); // merge uninitialized enhanced proxy to uninitialized enhanced proxy - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { final Human human = session.getReference( Human.class, "A Human" ); assertFalse( Hibernate.isInitialized( human ) ); @@ -708,24 +685,21 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun assertEquals( 1, stats.getPrepareStatementCount() ); stats.clear(); - checkAgeInNewSession( 5 ); + checkAgeInNewSession( scope, 5 ); } - private void checkAgeInNewSession(int expectedAge) { - - doInHibernate( - this::sessionFactory, + private void checkAgeInNewSession(SessionFactoryScope scope, int expectedAge) { + scope.inTransaction( session -> { final Human human = session.get( Human.class, "A Human" ); assertEquals( expectedAge, human.getAge() ); } ); - } - @Before - public void setupData() { - inTransaction( + @BeforeEach + public void setupData(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -740,9 +714,9 @@ public class LazyToOnesProxyMergeWithSubclassesTest extends BaseNonConfigCoreFun ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Human" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesStatelessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesStatelessTest.java index 5eddaabf9d..859f7f9670 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesStatelessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesStatelessTest.java @@ -15,50 +15,51 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ @TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) -public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - sources.addAnnotatedClass( Primate.class ); - sources.addAnnotatedClass( Human.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +@DomainModel( + annotatedClasses = { + LazyToOnesProxyWithSubclassesStatelessTest.Animal.class, + LazyToOnesProxyWithSubclassesStatelessTest.Primate.class, + LazyToOnesProxyWithSubclassesStatelessTest.Human.class, + LazyToOnesProxyWithSubclassesStatelessTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyToOnesProxyWithSubclassesStatelessTest { @Test - public void testNewHibernateProxyAssociation() { - inStatelessTransaction( + public void testNewHibernateProxyAssociation(SessionFactoryScope scope) { + scope.inStatelessTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -68,9 +69,9 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); @@ -83,8 +84,8 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } @Test - public void testNewEnhancedProxyAssociation() { - inStatelessTransaction( + public void testNewEnhancedProxyAssociation(SessionFactoryScope scope) { + scope.inStatelessTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -95,9 +96,9 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "human" ) ); @@ -110,8 +111,8 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } @Test - public void testExistingProxyAssociation() { - inStatelessTransaction( + public void testExistingProxyAssociation(SessionFactoryScope scope) { + scope.inStatelessTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -122,9 +123,9 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); @@ -139,8 +140,8 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } @Test - public void testExistingHibernateProxyAssociationLeafSubclass() { - inStatelessSession( + public void testExistingHibernateProxyAssociationLeafSubclass(SessionFactoryScope scope) { + scope.inStatelessSession( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -152,10 +153,10 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inStatelessSession( + scope.inStatelessSession( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -176,8 +177,8 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } @Test - public void testExistingEnhancedProxyAssociationLeafSubclassOnly() { - inStatelessSession( + public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryScope scope) { + scope.inStatelessSession( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -187,9 +188,9 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor } ); - inStatelessSession( + scope.inStatelessSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -203,9 +204,9 @@ public class LazyToOnesProxyWithSubclassesStatelessTest extends BaseNonConfigCor ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Human" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java index c440caf227..f9809b0f16 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LazyToOnesProxyWithSubclassesTest.java @@ -15,55 +15,51 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) -public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - sources.addAnnotatedClass( Primate.class ); - sources.addAnnotatedClass( Human.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + LazyToOnesProxyWithSubclassesTest.Animal.class, + LazyToOnesProxyWithSubclassesTest.Primate.class, + LazyToOnesProxyWithSubclassesTest.Human.class, + LazyToOnesProxyWithSubclassesTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class LazyToOnesProxyWithSubclassesTest { @Test - public void testNewProxyAssociation() { - inTransaction( + public void testNewProxyAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -73,9 +69,9 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } ); - inSession( + scope.inSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); @@ -92,8 +88,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } @Test - public void testGetInitializeAssociations() { - inTransaction( + public void testGetInitializeAssociations(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -104,9 +100,9 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } ); - inSession( + scope.inSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); @@ -122,8 +118,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } @Test - public void testExistingProxyAssociation() { - inTransaction( + public void testExistingProxyAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -134,9 +130,9 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } ); - inSession( + scope.inSession( session -> { - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); assertTrue( Hibernate.isPropertyInitialized( otherEntity, "animal" ) ); @@ -151,8 +147,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } @Test - public void testExistingHibernateProxyAssociationLeafSubclass() { - inTransaction( + public void testExistingHibernateProxyAssociationLeafSubclass(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -164,10 +160,10 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } ); - final Statistics stats = sessionFactory().getStatistics(); + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inSession( + scope.inSession( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -203,7 +199,7 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction assertEquals( 2, stats.getPrepareStatementCount() ); stats.clear(); - inSession( + scope.inSession( session -> { final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -233,8 +229,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } @Test - public void testExistingEnhancedProxyAssociationLeafSubclassOnly() { - inTransaction( + public void testExistingEnhancedProxyAssociationLeafSubclassOnly(SessionFactoryScope scope) { + scope.inTransaction( session -> { Human human = new Human( "A Human" ); OtherEntity otherEntity = new OtherEntity( "test1" ); @@ -244,9 +240,8 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction } ); - doInHibernate( - this::sessionFactory, session -> { - final Statistics stats = sessionFactory().getStatistics(); + scope.fromTransaction( session -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final OtherEntity otherEntity = session.get( OtherEntity.class, "test1" ); @@ -276,9 +271,9 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Human" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingEntityTest.java index 437674f19d..adbd5d6b56 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingEntityTest.java @@ -21,46 +21,57 @@ import jakarta.persistence.ManyToOne; import org.hibernate.Hibernate; import org.hibernate.ObjectNotFoundException; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + LoadANonExistingEntityTest.Employee.class, + LoadANonExistingEntityTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LoadANonExistingEntityTest extends BaseNonConfigCoreFunctionalTestCase { +public class LoadANonExistingEntityTest { private static int NUMBER_OF_ENTITIES = 20; @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testInitilaizeNonExistingEntity() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void testInitilaizeNonExistingEntity(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employer nonExisting = session.load( Employer.class, -1 ); assertEquals( 0, statistics.getPrepareStatementCount() ); assertFalse( Hibernate.isInitialized( nonExisting ) ); @@ -77,12 +88,11 @@ public class LoadANonExistingEntityTest extends BaseNonConfigCoreFunctionalTestC } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testSetFieldNonExistingEntity() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void testSetFieldNonExistingEntity(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employer nonExisting = session.load( Employer.class, -1 ); assertEquals( 0, statistics.getPrepareStatementCount() ); assertFalse( Hibernate.isInitialized( nonExisting ) ); @@ -99,12 +109,11 @@ public class LoadANonExistingEntityTest extends BaseNonConfigCoreFunctionalTestC } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testGetFieldNonExistingEntity() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void testGetFieldNonExistingEntity(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employer nonExisting = session.load( Employer.class, -1 ); assertEquals( 0, statistics.getPrepareStatementCount() ); assertFalse( Hibernate.isInitialized( nonExisting ) ); @@ -120,18 +129,9 @@ public class LoadANonExistingEntityTest extends BaseNonConfigCoreFunctionalTestC ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - Employer.class - }; - } - - @Before - public void setUpData() { - doInHibernate( - this::sessionFactory, session -> { + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { final Employee employee = new Employee(); employee.id = i + 1; @@ -146,31 +146,15 @@ public class LoadANonExistingEntityTest extends BaseNonConfigCoreFunctionalTestC ); } - @After - public void cleanupDate() { - doInHibernate( - this::sessionFactory, session -> { + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Employee" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); } ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "Employee") public static class Employee { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundBatchEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundBatchEntityTest.java index eaed1580d4..a0c8d381b0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundBatchEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundBatchEntityTest.java @@ -28,44 +28,58 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Andrea Boriero * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + LoadANonExistingNotFoundBatchEntityTest.Employee.class, + LoadANonExistingNotFoundBatchEntityTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFunctionalTestCase { +public class LoadANonExistingNotFoundBatchEntityTest { private static final int NUMBER_OF_ENTITIES = 20; @Test - @TestForIssue(jiraKey = "HHH-11147") - public void loadEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void loadEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { List employees = new ArrayList<>( NUMBER_OF_ENTITIES ); for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) { employees.add( session.load( Employee.class, i + 1 ) ); @@ -81,12 +95,12 @@ public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFu } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void getEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void getEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); assertNull( employee.employer ); @@ -98,12 +112,12 @@ public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFu } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void updateNotFoundAssociationWithNew() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void updateNotFoundAssociationWithNew(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); Employer employer = new Employer(); @@ -113,7 +127,7 @@ public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFu } } ); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); assertTrue( Hibernate.isInitialized( employee.employer ) ); @@ -123,18 +137,9 @@ public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFu } ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - Employer.class - }; - } - - @Before - public void setUpData() { - doInHibernate( - this::sessionFactory, session -> { + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { final Employee employee = new Employee(); employee.id = i + 1; @@ -144,40 +149,22 @@ public class LoadANonExistingNotFoundBatchEntityTest extends BaseNonConfigCoreFu } ); - - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { // Add "not found" associations session.createNativeQuery( "update Employee set employer_id = id" ).executeUpdate(); } ); } - @After - public void cleanupDate() { - doInHibernate( - this::sessionFactory, session -> { + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Employee" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); } ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "Employee") public static class Employee { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundEntityTest.java index d8eb51ae98..bebee952fb 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundEntityTest.java @@ -25,42 +25,56 @@ import jakarta.persistence.ManyToOne; import org.hibernate.Hibernate; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Andrea Boriero * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + LoadANonExistingNotFoundEntityTest.Employee.class, + LoadANonExistingNotFoundEntityTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctionalTestCase { +public class LoadANonExistingNotFoundEntityTest { @Test - @TestForIssue(jiraKey = "HHH-11147") - public void loadEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void loadEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employee employee = session.load( Employee.class, 1 ); Hibernate.initialize( employee ); assertNull( employee.employer ); @@ -72,13 +86,12 @@ public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctio } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void getEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void getEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employee employee = session.get( Employee.class, 1 ); assertNull( employee.employer ); } @@ -89,13 +102,12 @@ public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctio } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void updateNotFoundAssociationWithNew() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void updateNotFoundAssociationWithNew(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employee employee = session.get( Employee.class, 1 ); Employer employer = new Employer(); employer.id = 2 * employee.id; @@ -104,8 +116,7 @@ public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctio } ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { Employee employee = session.get( Employee.class, 1 ); assertTrue( Hibernate.isInitialized( employee.employer ) ); assertEquals( employee.id * 2, employee.employer.id ); @@ -114,18 +125,9 @@ public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctio ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - Employer.class - }; - } - - @Before - public void setUpData() { - doInHibernate( - this::sessionFactory, session -> { + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Employee employee = new Employee(); employee.id = 1; employee.name = "Employee #" + employee.id; @@ -134,39 +136,22 @@ public class LoadANonExistingNotFoundEntityTest extends BaseNonConfigCoreFunctio ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { // Add "not found" associations session.createNativeQuery( "update Employee set employer_id = 0 ").executeUpdate(); } ); } - @After - public void cleanupDate() { - doInHibernate( - this::sessionFactory, session -> { + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Employee" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); } ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "Employee") public static class Employee { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundLazyBatchEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundLazyBatchEntityTest.java index cc27b00bc0..5cba645840 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundLazyBatchEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/LoadANonExistingNotFoundLazyBatchEntityTest.java @@ -20,19 +20,20 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.ConstraintMode; @@ -43,29 +44,43 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Andrea Boriero * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + LoadANonExistingNotFoundLazyBatchEntityTest.Employee.class, + LoadANonExistingNotFoundLazyBatchEntityTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCoreFunctionalTestCase { +public class LoadANonExistingNotFoundLazyBatchEntityTest { private static final int NUMBER_OF_ENTITIES = 20; @Test - @TestForIssue(jiraKey = "HHH-11147") - public void loadEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void loadEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { List employees = new ArrayList<>( NUMBER_OF_ENTITIES ); for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) { employees.add( session.load( Employee.class, i + 1 ) ); @@ -84,12 +99,12 @@ public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCo } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void getEntityWithNotFoundAssociation() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void getEntityWithNotFoundAssociation(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0 ; i < NUMBER_OF_ENTITIES ; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); assertNull( employee.employer ); @@ -104,12 +119,12 @@ public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCo } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void updateNotFoundAssociationWithNew() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @JiraKey("HHH-11147") + public void updateNotFoundAssociationWithNew(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); Employer employer = new Employer(); @@ -119,7 +134,7 @@ public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCo } } ); - inTransaction( (session) -> { + scope.inTransaction( (session) -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { Employee employee = session.get( Employee.class, i + 1 ); assertTrue( Hibernate.isInitialized( employee.employer ) ); @@ -129,18 +144,9 @@ public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCo } ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - Employer.class - }; - } - - @Before - public void setUpData() { - doInHibernate( - this::sessionFactory, session -> { + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { for ( int i = 0; i < NUMBER_OF_ENTITIES; i++ ) { final Employee employee = new Employee(); employee.id = i + 1; @@ -151,39 +157,22 @@ public class LoadANonExistingNotFoundLazyBatchEntityTest extends BaseNonConfigCo ); - doInHibernate( - this::sessionFactory, session -> { + scope.inTransaction( session -> { // Add "not found" associations session.createNativeQuery( "update Employee set employer_id = id" ).executeUpdate(); } ); } - @After - public void cleanupDate() { - doInHibernate( - this::sessionFactory, session -> { + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Employee" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); } ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "Employee") public static class Employee { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MappedSuperclassWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MappedSuperclassWithEmbeddableTest.java index 16e2cd2d54..60ee9be95e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MappedSuperclassWithEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MappedSuperclassWithEmbeddableTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.Serializable; import java.util.Objects; import jakarta.persistence.Column; @@ -15,62 +17,59 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.hamcrest.core.IsNull.notNullValue; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertThat; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MappedSuperclassWithEmbeddableTest.TestEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true) -public class MappedSuperclassWithEmbeddableTest extends BaseNonConfigCoreFunctionalTestCase { +public class MappedSuperclassWithEmbeddableTest { - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { TestEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity testEntity = new TestEntity( "2", "test" ); s.persist( testEntity ); } ); } - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from TestEntity" ).executeUpdate(); } ); } @Test - public void testIt() { - doInHibernate( this::sessionFactory, s -> { + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( s -> { TestEntity testEntity = s.get( TestEntity.class, "2" ); - assertThat( testEntity, notNullValue() ); + assertThat( testEntity ).isNotNull(); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyBidirectionalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyBidirectionalTest.java index ed1c4e2673..61e8297981 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyBidirectionalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyBidirectionalTest.java @@ -14,41 +14,56 @@ import jakarta.persistence.MapsId; import jakarta.persistence.OneToOne; import org.hibernate.Hibernate; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ @JiraKey("HHH-13814") -@RunWith( BytecodeEnhancerRunner.class ) +@DomainModel( + annotatedClasses = { + MapsIdProxyBidirectionalTest.EmployerInfo.class, + MapsIdProxyBidirectionalTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class MapsIdProxyBidirectionalTest extends BaseNonConfigCoreFunctionalTestCase { +public class MapsIdProxyBidirectionalTest { @Test - public void testAssociation() { - inTransaction( + public void testAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); EmployerInfo employerInfo = session.get( EmployerInfo.class, 1 ); @@ -60,17 +75,17 @@ public class MapsIdProxyBidirectionalTest extends BaseNonConfigCoreFunctionalTes Hibernate.initialize( employer ); assertEquals( "Employer #" + employer.id, employer.name ); - assertThat( statistics.getEntityLoadCount(), is( 2L ) ); - assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( statistics.getEntityLoadCount() ).isEqualTo( 2L ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 2L ); } ); } @Test - public void testMappedByAssociation() { - inTransaction( + public void testMappedByAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); Employer employer = session.get( Employer.class, 1 ); @@ -83,24 +98,16 @@ public class MapsIdProxyBidirectionalTest extends BaseNonConfigCoreFunctionalTes assertTrue( Hibernate.isPropertyInitialized( employerInfo, "employer" ) ); assertSame( employer, employerInfo.employer ); - assertThat( statistics.getEntityLoadCount(), is( 2L ) ); - assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( statistics.getEntityLoadCount() ).isEqualTo( 2L ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 2L ); } ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EmployerInfo.class, - Employer.class - }; - } - - @Before - public void setUpData() { - inTransaction( + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Employer employer = new Employer(); employer.id = 1; @@ -113,9 +120,9 @@ public class MapsIdProxyBidirectionalTest extends BaseNonConfigCoreFunctionalTes ); } - @After - public void cleanupDate() { - inTransaction( + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from EmployerInfo" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); @@ -123,22 +130,6 @@ public class MapsIdProxyBidirectionalTest extends BaseNonConfigCoreFunctionalTes ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.applySetting( AvailableSettings.SHOW_SQL, true ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "EmployerInfo") public static class EmployerInfo { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyUnidirectionalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyUnidirectionalTest.java index d5292dbbd8..f255ea83dd 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyUnidirectionalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MapsIdProxyUnidirectionalTest.java @@ -13,43 +13,56 @@ import jakarta.persistence.MapsId; import jakarta.persistence.OneToOne; import org.hibernate.Hibernate; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-13814") -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey("HHH-13814") +@DomainModel( + annotatedClasses = { + MapsIdProxyUnidirectionalTest.EmployerInfo.class, + MapsIdProxyUnidirectionalTest.Employer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class MapsIdProxyUnidirectionalTest extends BaseNonConfigCoreFunctionalTestCase { +public class MapsIdProxyUnidirectionalTest { @Test - @TestForIssue(jiraKey = "HHH-13814") - public void testBatchAssociation() { - inTransaction( + @JiraKey("HHH-13814") + public void testBatchAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { - final Statistics statistics = sessionFactory().getStatistics(); + final Statistics statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); EmployerInfo employerInfo = session.get( EmployerInfo.class, 1 ); @@ -59,23 +72,15 @@ public class MapsIdProxyUnidirectionalTest extends BaseNonConfigCoreFunctionalTe assertFalse( Hibernate.isInitialized( employerInfo.employer ) ); Hibernate.initialize( employerInfo.employer ); - assertThat( statistics.getEntityLoadCount(), is( 2L ) ); - assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( statistics.getEntityLoadCount() ).isEqualTo( 2L ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 2L ); } ); } - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - EmployerInfo.class, - Employer.class - }; - } - - @Before - public void setUpData() { - inTransaction( + @BeforeEach + public void setUpData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final Employer employer = new Employer(); employer.id =1; @@ -88,9 +93,9 @@ public class MapsIdProxyUnidirectionalTest extends BaseNonConfigCoreFunctionalTe ); } - @After - public void cleanupDate() { - inTransaction( + @AfterEach + public void cleanupDate(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from EmployerInfo" ).executeUpdate(); session.createQuery( "delete from Employer" ).executeUpdate(); @@ -98,22 +103,6 @@ public class MapsIdProxyUnidirectionalTest extends BaseNonConfigCoreFunctionalTe ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.applySetting( AvailableSettings.SHOW_SQL, true ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - @Entity(name = "EmployerInfo") public static class EmployerInfo { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeDetachedToProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeDetachedToProxyTest.java index ca2579cfef..37d5a328f2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeDetachedToProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeDetachedToProxyTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; - import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -14,42 +13,54 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import org.hibernate.Hibernate; -import org.hibernate.annotations.LazyToOne; -import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + MergeDetachedToProxyTest.AEntity.class, + MergeDetachedToProxyTest.BEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class MergeDetachedToProxyTest { @Test - public void testMergeInitializedDetachedOntoProxy() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + public void testMergeInitializedDetachedOntoProxy(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); - final AEntity aEntityDetached = fromTransaction( + final AEntity aEntityDetached = scope.fromTransaction( session -> { AEntity aEntity = session.get( AEntity.class, 1 ); assertIsEnhancedProxy( aEntity.bEntity ); @@ -59,16 +70,16 @@ public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCas ); statistics.clear(); - assertThat( statistics.getPrepareStatementCount(), is( 0L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 0L ); - inSession( + scope.inSession( session -> { BEntity bEntity = session.getReference( BEntity.class, 2 ); assertIsEnhancedProxy( bEntity ); - assertThat( statistics.getPrepareStatementCount(), is( 0L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 0L ); AEntity aEntityMerged = (AEntity) session.merge( aEntityDetached ); - assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 1L ); assertSame( bEntity, aEntityMerged.bEntity ); assertEquals( "a description", aEntityDetached.bEntity.description ); @@ -76,14 +87,14 @@ public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 1L ); } @Test - public void testMergeUpdatedDetachedOntoProxy() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + public void testMergeUpdatedDetachedOntoProxy(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); - final AEntity aEntityDetached = fromTransaction( + final AEntity aEntityDetached = scope.fromTransaction( session -> { AEntity aEntity = session.get( AEntity.class, 1 ); assertIsEnhancedProxy( aEntity.bEntity ); @@ -95,16 +106,16 @@ public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCas aEntityDetached.bEntity.description = "new description"; statistics.clear(); - assertThat( statistics.getPrepareStatementCount(), is( 0L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 0L ); - inSession( + scope.inSession( session -> { BEntity bEntity = session.getReference( BEntity.class, 2 ); assertIsEnhancedProxy( bEntity ); - assertThat( statistics.getPrepareStatementCount(), is( 0L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 0L ); AEntity aEntityMerged = (AEntity) session.merge( aEntityDetached ); - assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 1L ); assertSame( bEntity, aEntityMerged.bEntity ); assertEquals( "new description", aEntityDetached.bEntity.description ); @@ -112,34 +123,12 @@ public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCas } ); - assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 1L ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( AEntity.class ); - sources.addAnnotatedClass( BEntity.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { final AEntity aEntity = new AEntity(); aEntity.id = 1; @@ -152,9 +141,9 @@ public class MergeDetachedToProxyTest extends BaseNonConfigCoreFunctionalTestCas ); } - @After - public void clearTestData(){ - inTransaction( + @AfterEach + public void clearTestData(SessionFactoryScope scope){ + scope.inTransaction( session -> { session.createQuery( "delete from AEntity" ).executeUpdate(); session.createQuery( "delete from BEntity" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeProxyTest.java index 298723961d..610bc94dec 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/MergeProxyTest.java @@ -7,23 +7,23 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.hamcrest.CoreMatchers; @@ -32,22 +32,36 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole * @author Gail BadnerMergeProxyTest */ -@TestForIssue( jiraKey = "HHH-11147" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-11147" ) +@DomainModel( + annotatedClasses = { + Activity.class, Instruction.class, WebApplication.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class MergeProxyTest { @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testMergeDetachUninitializedProxy() { - final Activity activity = fromTransaction( + @JiraKey("HHH-11147") + public void testMergeDetachUninitializedProxy(SessionFactoryScope scope) { + final Activity activity = scope.fromTransaction( session -> session.get( Activity.class, 0 ) ); @@ -61,14 +75,14 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { activity.setNbr( "2" ); - final Activity mergedActivity = fromTransaction( + final Activity mergedActivity = scope.fromTransaction( session -> (Activity) session.merge( activity ) ); assertTrue( Hibernate.isInitialized( mergedActivity ) ); assertThat( activity, not( CoreMatchers.sameInstance( mergedActivity ) ) ); - inTransaction( + scope.inTransaction( session -> { final Instruction persistentInstruction = session.get( Instruction.class, 0 ); assertThat( persistentInstruction.getSummary(), is( "Instruction #0" ) ); @@ -77,9 +91,9 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testMergeDetachInitializedProxy() { - final Activity activityProxy = fromTransaction( + @JiraKey("HHH-11147") + public void testMergeDetachInitializedProxy(SessionFactoryScope scope) { + final Activity activityProxy = scope.fromTransaction( session -> { final Activity activity = session.load( Activity.class, 0 ); assertFalse( Hibernate.isInitialized( activity) ); @@ -106,14 +120,14 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { activityProxy.setNbr( "2" ); - final Activity mergedActivity = fromTransaction( + final Activity mergedActivity = scope.fromTransaction( session -> (Activity) session.merge( activityProxy ) ); assertTrue( Hibernate.isInitialized( mergedActivity ) ); assertThat( activityProxy, not( CoreMatchers.sameInstance( mergedActivity ) ) ); - inTransaction( + scope.inTransaction( session -> { final Instruction instruction = session.get( Instruction.class, 0 ); assertThat( instruction.getSummary(), is( "Instruction #0" ) ); @@ -122,9 +136,9 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - @TestForIssue(jiraKey = "HHH-11147") - public void testMergeDetachInitializedByAccessProxy() { - final Activity activityProxy = fromTransaction( + @JiraKey("HHH-11147") + public void testMergeDetachInitializedByAccessProxy(SessionFactoryScope scope) { + final Activity activityProxy = scope.fromTransaction( session -> { final Activity activity = session.load( Activity.class, 0 ); assertFalse( Hibernate.isInitialized( activity) ); @@ -151,14 +165,14 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { activityProxy.setNbr( "2" ); - final Activity mergedActivity = fromTransaction( + final Activity mergedActivity = scope.fromTransaction( session -> (Activity) session.merge( activityProxy ) ); assertTrue( Hibernate.isInitialized( mergedActivity ) ); assertThat( activityProxy, not( CoreMatchers.sameInstance( mergedActivity ) ) ); - inTransaction( + scope.inTransaction( session -> { final Instruction instruction = session.get( Instruction.class, 0 ); assertThat( instruction.getSummary(), is( "Instruction #0" ) ); @@ -166,31 +180,9 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Activity.class ); - sources.addAnnotatedClass( Instruction.class ); - sources.addAnnotatedClass( WebApplication.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { // create a slew of Activity objects, some with Instruction reference // some without. @@ -216,9 +208,9 @@ public class MergeProxyTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Activity" ).executeUpdate(); session.createQuery( "delete from Instruction" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/NaturalIdInUninitializedProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/NaturalIdInUninitializedProxyTest.java index 5b0bc8e77d..82f2fbb87d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/NaturalIdInUninitializedProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/NaturalIdInUninitializedProxyTest.java @@ -11,41 +11,56 @@ import jakarta.persistence.Id; import org.hibernate.Hibernate; import org.hibernate.annotations.NaturalId; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13607" ) -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey( "HHH-13607" ) +@DomainModel( + annotatedClasses = { + NaturalIdInUninitializedProxyTest.EntityMutableNaturalId.class, + NaturalIdInUninitializedProxyTest.EntityImmutableNaturalId.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class NaturalIdInUninitializedProxyTest extends BaseNonConfigCoreFunctionalTestCase { +public class NaturalIdInUninitializedProxyTest { @Test - public void testImmutableNaturalId() { - inTransaction( + public void testImmutableNaturalId(SessionFactoryScope scope) { + scope.inTransaction( session -> { final EntityImmutableNaturalId e = session.getReference( EntityImmutableNaturalId.class, 1 ); assertFalse( Hibernate.isInitialized( e ) ); } ); - inTransaction( + scope.inTransaction( session -> { final EntityImmutableNaturalId e = session.get( EntityImmutableNaturalId.class, 1 ); assertEquals( "name", e.name ); @@ -54,15 +69,15 @@ public class NaturalIdInUninitializedProxyTest extends BaseNonConfigCoreFunction } @Test - public void testMutableNaturalId() { - inTransaction( + public void testMutableNaturalId(SessionFactoryScope scope) { + scope.inTransaction( session -> { final EntityMutableNaturalId e = session.getReference( EntityMutableNaturalId.class, 1 ); assertFalse( Hibernate.isInitialized( e ) ); } ); - inTransaction( + scope.inTransaction( session -> { final EntityMutableNaturalId e = session.get( EntityMutableNaturalId.class, 1 ); assertEquals( "name", e.name ); @@ -70,30 +85,9 @@ public class NaturalIdInUninitializedProxyTest extends BaseNonConfigCoreFunction ); } - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( EntityMutableNaturalId.class ); - sources.addAnnotatedClass( EntityImmutableNaturalId.class ); - } - - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.persist( new EntityMutableNaturalId( 1, "name" ) ); session.persist( new EntityImmutableNaturalId( 1, "name" ) ); @@ -101,9 +95,9 @@ public class NaturalIdInUninitializedProxyTest extends BaseNonConfigCoreFunction ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from EntityMutableNaturalId" ).executeUpdate(); session.createQuery( "delete from EntityImmutableNaturalId" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest.java index e49ef39c43..ed6154133b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest.java @@ -13,56 +13,51 @@ import jakarta.persistence.spi.LoadState; import org.hibernate.Hibernate; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.jpa.internal.util.PersistenceUtilHelper; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest.Animal.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true,inlineDirtyChecking = true) -public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase { +public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest { private final PersistenceUtilHelper.MetadataCache metadataCache = new PersistenceUtilHelper.MetadataCache(); - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - } - @Test - public void testInitializeWithGetter() { - inTransaction( + public void testInitializeWithGetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -73,7 +68,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -86,7 +81,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -99,8 +94,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } @Test - public void testInitializeWithSetter() { - inTransaction( + public void testInitializeWithSetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -111,7 +106,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -124,7 +119,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -136,8 +131,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } @Test - public void testMergeUpdatedOntoUninitialized() { - inTransaction( + public void testMergeUpdatedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -147,9 +142,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -161,7 +154,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -174,7 +167,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -185,8 +178,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } @Test - public void testMergeUpdatedOntoUpdated() { - inTransaction( + public void testMergeUpdatedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -196,9 +189,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -210,7 +201,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -222,7 +213,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -233,8 +224,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } @Test - public void testMergeUninitializedOntoUninitialized() { - inTransaction( + public void testMergeUninitializedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -244,9 +235,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); assertEquals( LoadState.NOT_LOADED , PersistenceUtilHelper.isLoadedWithoutReference( animal, "age", metadataCache ) ); @@ -255,7 +244,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -267,7 +256,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -278,8 +267,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } @Test - public void testMergeUninitializedOntoUpdated() { - inTransaction( + public void testMergeUninitializedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -289,9 +278,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); assertEquals( LoadState.NOT_LOADED , PersistenceUtilHelper.isLoadedWithoutReference( animal, "age", metadataCache ) ); @@ -300,7 +287,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -313,7 +300,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -323,9 +310,9 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingDynamicUpdateTest extend ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Animal" ).executeUpdate(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingTest.java index 9253dbae5c..ffd4b08518 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateInlineDirtyTrackingTest.java @@ -11,47 +11,48 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + ProxyInitializeAndUpdateInlineDirtyTrackingTest.Animal.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true,inlineDirtyChecking = true) -public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - } +public class ProxyInitializeAndUpdateInlineDirtyTrackingTest { @Test - public void testInitializeWithGetter() { - inTransaction( + public void testInitializeWithGetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -62,7 +63,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -73,7 +74,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -85,8 +86,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } @Test - public void testInitializeWithSetter() { - inTransaction( + public void testInitializeWithSetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -97,7 +98,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -107,7 +108,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -119,8 +120,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } @Test - public void testMergeUpdatedOntoUninitialized() { - inTransaction( + public void testMergeUpdatedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -130,9 +131,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -144,7 +143,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -155,7 +154,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -166,8 +165,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } @Test - public void testMergeUpdatedOntoUpdated() { - inTransaction( + public void testMergeUpdatedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -177,9 +176,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -191,7 +188,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -203,7 +200,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -214,8 +211,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } @Test - public void testMergeUninitializedOntoUninitialized() { - inTransaction( + public void testMergeUninitializedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -225,16 +222,14 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); return animal; } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -243,7 +238,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -254,8 +249,8 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } @Test - public void testMergeUninitializedOntoUpdated() { - inTransaction( + public void testMergeUninitializedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -265,16 +260,14 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); return animal; } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -287,7 +280,7 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -297,9 +290,9 @@ public class ProxyInitializeAndUpdateInlineDirtyTrackingTest extends BaseNonConf ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Animal" ).executeUpdate(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateTest.java index 16015ccfa8..5e1e9c8487 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/ProxyInitializeAndUpdateTest.java @@ -11,48 +11,49 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * @author Gail Badner */ -@TestForIssue( jiraKey = "HHH-13640" ) -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey( "HHH-13640" ) +@DomainModel( + annotatedClasses = { + ProxyInitializeAndUpdateTest.Animal.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Animal.class ); - } +public class ProxyInitializeAndUpdateTest { @Test - public void testInitializeWithGetter() { - inTransaction( + public void testInitializeWithGetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -63,7 +64,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -74,7 +75,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -86,8 +87,8 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } @Test - public void testInitializeWithSetter() { - inTransaction( + public void testInitializeWithSetter(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -98,7 +99,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -108,7 +109,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inSession( + scope.inSession( session -> { Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -120,8 +121,8 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } @Test - public void testMergeUpdatedOntoUninitialized() { - inTransaction( + public void testMergeUpdatedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -131,9 +132,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -145,7 +144,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -157,7 +156,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -168,8 +167,8 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } @Test - public void testMergeUpdatedOntoUpdated() { - inTransaction( + public void testMergeUpdatedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -179,9 +178,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - final Animal animalInitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalInitialized = scope.fromTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); assertEquals( "female", animal.getSex() ); @@ -193,7 +190,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes animalInitialized.setAge( 4 ); animalInitialized.setSex( "other" ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -206,7 +203,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -217,8 +214,8 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } @Test - public void testMergeUninitializedOntoUninitialized() { - inTransaction( + public void testMergeUninitializedOntoUninitialized(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -228,16 +225,14 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); return animal; } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -247,7 +242,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -258,8 +253,8 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } @Test - public void testMergeUninitializedOntoUpdated() { - inTransaction( + public void testMergeUninitializedOntoUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { Animal animal = new Animal(); animal.name = "animal"; @@ -269,16 +264,14 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - final Animal animalUninitialized = doInHibernate( - this::sessionFactory, - session -> { + final Animal animalUninitialized = scope.fromTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); return animal; } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.load( Animal.class, "animal" ); assertFalse( Hibernate.isInitialized( animal ) ); @@ -293,7 +286,7 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes } ); - inTransaction( + scope.inTransaction( session -> { final Animal animal = session.get( Animal.class, "animal" ); assertTrue( Hibernate.isInitialized( animal ) ); @@ -303,9 +296,9 @@ public class ProxyInitializeAndUpdateTest extends BaseNonConfigCoreFunctionalTes ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Animal" ).executeUpdate(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyEagerManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyEagerManyToOneTest.java index fa555ba49d..4be998f7e4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyEagerManyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyEagerManyToOneTest.java @@ -15,17 +15,16 @@ import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.StatelessSession; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.query.Query; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -40,34 +39,37 @@ import jakarta.persistence.Table; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) -public class QueryScrollingWithInheritanceProxyEagerManyToOneTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( EmployeeParent.class ); - sources.addAnnotatedClass( Employee.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +@DomainModel( + annotatedClasses = { + QueryScrollingWithInheritanceProxyEagerManyToOneTest.EmployeeParent.class, + QueryScrollingWithInheritanceProxyEagerManyToOneTest.Employee.class, + QueryScrollingWithInheritanceProxyEagerManyToOneTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class QueryScrollingWithInheritanceProxyEagerManyToOneTest { @Test - public void testScrollableWithStatelessSession() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testScrollableWithStatelessSession(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); ScrollableResults scrollableResults = null; - final StatelessSession statelessSession = sessionFactory().openStatelessSession(); + final StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession(); try { statelessSession.beginTransaction(); @@ -116,11 +118,11 @@ public class QueryScrollingWithInheritanceProxyEagerManyToOneTest extends BaseNo } @Test - public void testScrollableWithSession() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testScrollableWithSession(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); ScrollableResults scrollableResults = null; - final Session session = sessionFactory().openSession(); + final Session session = scope.getSessionFactory().openSession(); try { session.beginTransaction(); @@ -168,9 +170,9 @@ public class QueryScrollingWithInheritanceProxyEagerManyToOneTest extends BaseNo } } - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { Employee e1 = new Employee( "ENG1" ); Employee e2 = new Employee( "ENG2" ); @@ -192,9 +194,9 @@ public class QueryScrollingWithInheritanceProxyEagerManyToOneTest extends BaseNo ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Employee" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyTest.java index ddd5e2437a..e5d1eda6c2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/QueryScrollingWithInheritanceProxyTest.java @@ -15,17 +15,19 @@ import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.StatelessSession; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.query.Query; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -43,31 +45,30 @@ import static org.hamcrest.MatcherAssert.assertThat; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) -public class QueryScrollingWithInheritanceProxyTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( EmployeeParent.class ); - sources.addAnnotatedClass( Employee.class ); - sources.addAnnotatedClass( OtherEntity.class ); - } +@DomainModel( + annotatedClasses = { + QueryScrollingWithInheritanceProxyTest.EmployeeParent.class, + QueryScrollingWithInheritanceProxyTest.Employee.class, + QueryScrollingWithInheritanceProxyTest.OtherEntity.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class QueryScrollingWithInheritanceProxyTest { @Test - public void testScrollableWithStatelessSession() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testScrollableWithStatelessSession(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); ScrollableResults scrollableResults = null; - final StatelessSession statelessSession = sessionFactory().openStatelessSession(); + final StatelessSession statelessSession = scope.getSessionFactory().openStatelessSession(); try { statelessSession.beginTransaction(); @@ -116,11 +117,11 @@ public class QueryScrollingWithInheritanceProxyTest extends BaseNonConfigCoreFun } @Test - public void testScrollableWithSession() { - final StatisticsImplementor stats = sessionFactory().getStatistics(); + public void testScrollableWithSession(SessionFactoryScope scope) { + final StatisticsImplementor stats = scope.getSessionFactory().getStatistics(); stats.clear(); ScrollableResults scrollableResults = null; - final Session session = sessionFactory().openSession(); + final Session session = scope.getSessionFactory().openSession(); try { session.beginTransaction(); @@ -168,9 +169,9 @@ public class QueryScrollingWithInheritanceProxyTest extends BaseNonConfigCoreFun } } - @Before - public void prepareTestData() { - inTransaction( + @BeforeEach + public void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { Employee e1 = new Employee( "ENG1" ); Employee e2 = new Employee( "ENG2" ); @@ -192,9 +193,9 @@ public class QueryScrollingWithInheritanceProxyTest extends BaseNonConfigCoreFun ); } - @After - public void cleanUpTestData() { - inTransaction( + @AfterEach + public void cleanUpTestData(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from OtherEntity" ).executeUpdate(); session.createQuery( "delete from Employee" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SetIdentifierOnAEnhancedProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SetIdentifierOnAEnhancedProxyTest.java index c9a68e3bbb..e30abf7394 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SetIdentifierOnAEnhancedProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SetIdentifierOnAEnhancedProxyTest.java @@ -28,95 +28,93 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.PersistenceException; import jakarta.persistence.Table; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.hamcrest.MatcherAssert; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.core.Is.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + SetIdentifierOnAEnhancedProxyTest.Parent.class, + SetIdentifierOnAEnhancedProxyTest.Child.class, + SetIdentifierOnAEnhancedProxyTest.Person.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunctionalTestCase { - - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } +public class SetIdentifierOnAEnhancedProxyTest { private static final int CHILDREN_SIZE = 10; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class, Person.class }; - } - @Test - public void setIdTest() { - final Statistics stats = sessionFactory().getStatistics(); + public void setIdTest(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( session -> { stats.clear(); Child loadedChild = session.load( Child.class, lastChildID ); final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) loadedChild; final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); + assertThat( interceptor ).isInstanceOf( EnhancementAsProxyLazinessInterceptor.class ); loadedChild.setId( lastChildID ); assertEquals( 0, stats.getPrepareStatementCount() ); - assertThat( loadedChild.getId(), is( lastChildID ) ); + assertThat( loadedChild.getId() ).isEqualTo( lastChildID ); assertEquals( 0, stats.getPrepareStatementCount() ); } ); - inTransaction( + scope.inTransaction( session -> { Child loadedChild = session.load( Child.class, lastChildID ); - assertThat( loadedChild, is( notNullValue() ) ); + assertThat( loadedChild ).isNotNull(); } ); } @Test - public void setIdClassTest(){ - final Statistics stats = sessionFactory().getStatistics(); + public void setIdClassTest(SessionFactoryScope scope){ + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( session -> { stats.clear(); ModelId id = new ModelId(); @@ -126,7 +124,7 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) parent; final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); + assertThat( interceptor ).isInstanceOf( EnhancementAsProxyLazinessInterceptor.class ); assertEquals( 0, stats.getPrepareStatementCount() ); @@ -135,27 +133,28 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction assertEquals( 0, stats.getPrepareStatementCount() ); - assertThat( parent.getId1(), is( 1L ) ); - assertThat( parent.getId2(), is( 2L ) ); + assertThat( parent.getId1() ).isEqualTo( 1L ); + assertThat( parent.getId2() ).isEqualTo( 2L ); assertEquals( 0, stats.getPrepareStatementCount() ); } ); - inTransaction( + scope.inTransaction( session -> { Child loadedChild = session.load( Child.class, lastChildID ); - assertThat( loadedChild, is( notNullValue() ) ); + assertThat( loadedChild ).isNotNull(); } ); } - @Test(expected = PersistenceException.class) - public void updateIdClassTest(){ - final Statistics stats = sessionFactory().getStatistics(); + @Test + public void updateIdClassTest(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + assertThatThrownBy( + () -> scope.inTransaction( session -> { stats.clear(); ModelId id = new ModelId(); @@ -165,47 +164,47 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) parent; final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); + assertThat( interceptor ).isInstanceOf( EnhancementAsProxyLazinessInterceptor.class ); // should trigger an exception parent.setId1( 3L ); - } - ); + } ) + ).isInstanceOf( PersistenceException.class ); - inTransaction( - session -> { - Child loadedChild = session.load( Child.class, lastChildID ); - assertThat( loadedChild, is( notNullValue() ) ); - } - ); +// scope.inTransaction( +// session -> { +// Child loadedChild = session.load( Child.class, lastChildID ); +// assertThat( loadedChild, is( notNullValue() ) ); +// } +// ); } - @Test(expected = PersistenceException.class) - public void updateIdTest() { - final Statistics stats = sessionFactory().getStatistics(); + @Test + public void updateIdTest(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Long updatedId = lastChildID + 1; + assertThatThrownBy( + () -> scope.inTransaction( + session -> { + stats.clear(); + Child loadedChild = session.load( Child.class, lastChildID ); - inTransaction( - session -> { - stats.clear(); - Child loadedChild = session.load( Child.class, lastChildID ); - - final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) loadedChild; - final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); - MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); - - // should trigger an exception - loadedChild.setId( updatedId ); - } - ); + final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) loadedChild; + final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor(); + assertThat( interceptor ).isInstanceOf( EnhancementAsProxyLazinessInterceptor.class ); + // should trigger an exception + loadedChild.setId( updatedId ); + } + ) + ).isInstanceOf( PersistenceException.class ); } - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); parent.setId1( 1L ); parent.setId2( 2L ); @@ -229,9 +228,9 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction } ); } - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from Child" ).executeUpdate(); s.createQuery( "delete from Parent" ).executeUpdate(); } ); @@ -262,7 +261,7 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction @Entity(name = "Parent") @Table(name = "PARENT") @IdClass( ModelId.class ) - private static class Parent { + static class Parent { String name; @@ -309,7 +308,7 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction @Entity(name = "Person") @Table(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @@ -327,7 +326,7 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SharingReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SharingReferenceTest.java index 3a7ebf86b7..8fa13df247 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SharingReferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SharingReferenceTest.java @@ -17,19 +17,20 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToOne; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.NoDirtyCheckEnhancementContext; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; @@ -37,36 +38,30 @@ import static org.junit.Assert.assertSame; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + SharingReferenceTest.Ceo.class, + SharingReferenceTest.Manager.class, + SharingReferenceTest.Supervisor.class, + SharingReferenceTest.Worker.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.FORMAT_SQL, value = "false"), + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false"), + @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "false"), + @Setting(name = AvailableSettings.GENERATE_STATISTICS, value = "true"), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -public class SharingReferenceTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } +public class SharingReferenceTest { - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Ceo.class ); - sources.addAnnotatedClass( Manager.class ); - sources.addAnnotatedClass( Supervisor.class ); - sources.addAnnotatedClass( Worker.class ); - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Ceo ceo = new Ceo(); ceo.setName( "Jill" ); @@ -98,8 +93,8 @@ public class SharingReferenceTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testFind() { - inTransaction( + public void testFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { Ceo ceo = session.find( Ceo.class, 1L ); assertEquals( "Jill", ceo.getName() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoading.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoading.java index 7b7c7442fe..7b0c0df215 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoading.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoading.java @@ -19,7 +19,6 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.EntityEntry; @@ -28,21 +27,23 @@ import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.hamcrest.MatcherAssert; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.core.Is.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -50,31 +51,33 @@ import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + SimpleUpdateTestWithLazyLoading.Parent.class, + SimpleUpdateTestWithLazyLoading.Child.class, + SimpleUpdateTestWithLazyLoading.Person.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } +public class SimpleUpdateTestWithLazyLoading { private static final int CHILDREN_SIZE = 10; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class, Person.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { Child child = new Child(); @@ -95,26 +98,26 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional } - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from Child" ).executeUpdate(); s.createQuery( "delete from Parent" ).executeUpdate(); } ); } @Test - public void updateSimpleField() { - final Statistics stats = sessionFactory().getStatistics(); + public void updateSimpleField(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final String updatedName = "Barrabas_"; - final EntityPersister childPersister = sessionFactory().getRuntimeMetamodels() + final EntityPersister childPersister = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Child.class.getName() ); - inTransaction( + scope.inTransaction( session -> { stats.clear(); Child loadedChild = session.load( Child.class, lastChildID ); @@ -147,7 +150,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { Child loadedChild = session.load( Child.class, lastChildID ); assertThat( loadedChild.getName(), is( updatedName ) ); @@ -159,11 +162,11 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional } @Test - public void testUpdateAssociation() { + public void testUpdateAssociation(SessionFactoryScope scope) { String updatedName = "Barrabas_"; String parentName = "Yodit"; - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -180,7 +183,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional s.save( parent ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( Hibernate.isInitialized( loadedChild ), is( false ) ); assertThat( loadedChild.getName(), is( updatedName ) ); @@ -192,9 +195,9 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional } @Test - public void testUpdateCollection() { - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + public void testUpdateCollection(SessionFactoryScope scope) { + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -209,7 +212,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional s.persist( relative ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild.getRelatives().size(), is( 2 ) ); } ); @@ -217,7 +220,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional @Entity(name = "Parent") @Table(name = "PARENT") - private static class Parent { + static class Parent { String name; @@ -243,7 +246,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional @Entity(name = "Person") @Table(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @@ -261,7 +264,7 @@ public class SimpleUpdateTestWithLazyLoading extends BaseNonConfigCoreFunctional @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.java index 6a44d39db3..48ccac52f0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.java @@ -18,58 +18,61 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.hamcrest.MatcherAssert; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.core.Is.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.Parent.class, + SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.Child.class, + SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking.Person.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true, inlineDirtyChecking = true ) -public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } +public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking { private static final int CHILDREN_SIZE = 10; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class, Person.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { Child child = new Child(); @@ -90,20 +93,20 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN } - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from Child" ).executeUpdate(); s.createQuery( "delete from Parent" ).executeUpdate(); } ); } @Test - public void updateSimpleField() { - final Statistics stats = sessionFactory().getStatistics(); + public void updateSimpleField(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); String updatedName = "Barrabas_"; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -120,18 +123,18 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN // the UPDATE assertEquals( 2, stats.getPrepareStatementCount() ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild.getName(), is( updatedName ) ); } ); } @Test - public void testUpdateAssociation() { + public void testUpdateAssociation(SessionFactoryScope scope) { String updatedName = "Barrabas_"; String parentName = "Yodit"; - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -148,7 +151,7 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN s.save( parent ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild.getName(), is( updatedName ) ); assertThat( loadedChild.getParent().getName(), is( parentName ) ); @@ -156,9 +159,9 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN } @Test - public void testUpdateCollection() { - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + public void testUpdateCollection(SessionFactoryScope scope) { + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -173,7 +176,7 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN s.persist( relative ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild.getRelatives().size(), is( 2 ) ); } ); @@ -181,7 +184,7 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN @Entity(name = "Parent") @Table(name = "PARENT") - private static class Parent { + static class Parent { String name; @@ -207,7 +210,7 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN @Entity(name = "Person") @Table(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @@ -225,7 +228,7 @@ public class SimpleUpdateTestWithLazyLoadingAndInlineDirtyTracking extends BaseN @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.java index 8de68fd7b2..caa9c1a398 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.java @@ -9,7 +9,9 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.core.Is.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.Child; +import static org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.Parent; +import static org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest.Person; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -17,10 +19,6 @@ import java.util.ArrayList; import java.util.List; import org.hibernate.Hibernate; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; @@ -30,14 +28,17 @@ import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -58,44 +59,31 @@ import org.hamcrest.MatcherAssert; * * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-11147") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-11147") +@DomainModel( + annotatedClasses = { + Parent.class, Child.class, Person.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory(applyCollectionsInDefaultFetchGroup = false) +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true) -public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); - ssrb.applySetting( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - ssrb.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } +public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTest { private static final int CHILDREN_SIZE = 10; private Long lastChildID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class, Person.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent parent = new Parent(); for ( int i = 0; i < CHILDREN_SIZE; i++ ) { Child child = new Child(); @@ -116,28 +104,28 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe } - @After - public void tearDown() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.createQuery( "delete from Child" ).executeUpdate(); s.createQuery( "delete from Parent" ).executeUpdate(); } ); } @Test - public void updateSimpleField() { - final Statistics stats = sessionFactory().getStatistics(); + public void updateSimpleField(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); final String updatedName = "Barrabas_"; - final EntityPersister childPersister = sessionFactory().getRuntimeMetamodels() + final EntityPersister childPersister = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Child.class.getName() ); final int relativesAttributeIndex = childPersister.getEntityMetamodel().getPropertyIndex( "relatives" ); - inTransaction( + scope.inTransaction( session -> { stats.clear(); Child loadedChild = session.load( Child.class, lastChildID ); @@ -177,7 +165,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe } ); - inTransaction( + scope.inTransaction( session -> { Child loadedChild = session.load( Child.class, lastChildID ); assertThat( loadedChild.getName(), is( updatedName ) ); @@ -193,11 +181,11 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe } @Test - public void testUpdateAssociation() { + public void testUpdateAssociation(SessionFactoryScope scope) { String updatedName = "Barrabas_"; String parentName = "Yodit"; - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -214,7 +202,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe s.save( parent ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( Hibernate.isInitialized( loadedChild ), is( false ) ); assertThat( loadedChild.getName(), is( updatedName ) ); @@ -226,9 +214,9 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe } @Test - public void testUpdateCollection() { - doInHibernate( this::sessionFactory, s -> { - final Statistics stats = sessionFactory().getStatistics(); + public void testUpdateCollection(SessionFactoryScope scope) { + scope.inTransaction( s -> { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); Child loadedChild = s.load( Child.class, lastChildID ); @@ -243,7 +231,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe s.persist( relative ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Child loadedChild = s.load( Child.class, lastChildID ); assertThat( loadedChild.getRelatives().size(), is( 2 ) ); } ); @@ -251,7 +239,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe @Entity(name = "Parent") @Table(name = "PARENT") - private static class Parent { + static class Parent { String name; @@ -277,7 +265,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe @Entity(name = "Person") @Table(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id; @@ -295,7 +283,7 @@ public class SimpleUpdateWithLazyLoadingWithCollectionInDefaultFetchGroupFalseTe @Entity(name = "Child") @Table(name = "CHILD") - private static class Child { + static class Child { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/AbstractBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/AbstractBatchingTest.java index d6f3e0d03b..99df626fd2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/AbstractBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/AbstractBatchingTest.java @@ -15,71 +15,67 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToOne; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator; + import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.NoDirtyCheckEnhancementContext; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + AbstractBatchingTest.ParentEntity.class, AbstractBatchingTest.ChildEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class }) -public abstract class AbstractBatchingTest extends BaseNonConfigCoreFunctionalTestCase { +public abstract class AbstractBatchingTest { protected String childName = SafeRandomUUIDGenerator.safeRandomUUIDAsString(); protected Long parentId; - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - sources.addAnnotatedClass( ParentEntity.class ); - sources.addAnnotatedClass( ChildEntity.class ); - } - @Test - public void testLoadParent() { - StatisticsImplementor statistics = sessionFactory().getStatistics(); + public void testLoadParent(SessionFactoryScope scope) { + StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( + scope.inTransaction( session -> { ParentEntity parentEntity = session.find( ParentEntity.class, parentId ); - assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 1L ); ChildEntity childEntity = parentEntity.getChildEntity(); assertFalse( Hibernate.isPropertyInitialized( childEntity, "name" ) ); assertEquals( childName, childEntity.getName() ); - assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 2L ); } ); } - @Before - public void setUp() { + @BeforeEach + public void setUp(SessionFactoryScope scope) { ParentEntity parent = new ParentEntity(); - inTransaction( + scope.inTransaction( session -> { ChildEntity childEntity = new ChildEntity(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/DynamicBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/DynamicBatchingTest.java index 21e0f27a34..f89d558a10 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/DynamicBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/DynamicBatchingTest.java @@ -6,22 +6,23 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.batch; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.loader.BatchFetchStyle; -import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.Setting; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-14108") +@JiraKey("HHH-14108") +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.BATCH_FETCH_STYLE, value = "DYNAMIC"), + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) public class DynamicBatchingTest extends AbstractBatchingTest { - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.BATCH_FETCH_STYLE, BatchFetchStyle.DYNAMIC.toString() ); - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/PaddedBatchingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/PaddedBatchingTest.java index c5c79f30cc..b6c9ec24d1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/PaddedBatchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/batch/PaddedBatchingTest.java @@ -6,21 +6,22 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.batch; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.loader.BatchFetchStyle; -import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.Setting; /** * @author Andrea Boriero */ -@TestForIssue(jiraKey = "HHH-14108") +@JiraKey("HHH-14108") +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.BATCH_FETCH_STYLE, value = "PADDED"), + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) public class PaddedBatchingTest extends AbstractBatchingTest { - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.BATCH_FETCH_STYLE, BatchFetchStyle.PADDED.toString() ); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/crosspackage/CrossPackageMappedSuperclassWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/crosspackage/CrossPackageMappedSuperclassWithEmbeddableTest.java index f037f57bf2..2e5b72e00e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/crosspackage/CrossPackageMappedSuperclassWithEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/crosspackage/CrossPackageMappedSuperclassWithEmbeddableTest.java @@ -11,27 +11,29 @@ import static org.assertj.core.api.Assertions.assertThat; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.crosspackage.base.EmbeddableType; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.crosspackage.derived.TestEntity; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + TestEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true) -public class CrossPackageMappedSuperclassWithEmbeddableTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { TestEntity.class }; - } +public class CrossPackageMappedSuperclassWithEmbeddableTest { @Test - @TestForIssue(jiraKey = "HHH-15141") - public void testIt() { + @JiraKey("HHH-15141") + public void testIt(SessionFactoryScope scope) { // Just a smoke test; the original failure happened during bytecode enhancement. - Long id = fromTransaction( s -> { + Long id = scope.fromTransaction( s -> { TestEntity testEntity = new TestEntity(); EmbeddableType embedded = new EmbeddableType(); embedded.setField( "someValue" ); @@ -39,7 +41,7 @@ public class CrossPackageMappedSuperclassWithEmbeddableTest extends BaseNonConfi s.persist( testEntity ); return testEntity.getId(); } ); - inTransaction( s -> { + scope.inTransaction( s -> { TestEntity testEntity = s.find( TestEntity.class, id ); assertThat( testEntity.getEmbeddedField().getField() ).isEqualTo( "someValue" ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/DirtyCheckPrivateUnMappedCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/DirtyCheckPrivateUnMappedCollectionTest.java index d830058951..ec413f53c4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/DirtyCheckPrivateUnMappedCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/DirtyCheckPrivateUnMappedCollectionTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -18,55 +20,52 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + DirtyCheckPrivateUnMappedCollectionTest.Measurement.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class }) @RequiresDialectFeature(DialectChecks.SupportsIdentityColumns.class) -public class DirtyCheckPrivateUnMappedCollectionTest extends BaseNonConfigCoreFunctionalTestCase { +public class DirtyCheckPrivateUnMappedCollectionTest { - boolean skipTest; - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } - else { - sources.addAnnotatedClass( Measurement.class ); - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } @Test - public void testIt() { - if ( skipTest ) { - return; - } - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { Tag tag = new Tag(); tag.setName( "tag1" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/EntityWithMutableAttributesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/EntityWithMutableAttributesTest.java index b34d5a46a5..42355d8970 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/EntityWithMutableAttributesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/EntityWithMutableAttributesTest.java @@ -16,58 +16,56 @@ import jakarta.persistence.Temporal; import jakarta.persistence.TemporalType; import jakarta.validation.constraints.NotNull; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeFalse; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + EntityWithMutableAttributesTest.User.class, + EntityWithMutableAttributesTest.Role.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class, DirtyCheckEnhancementContext.class }) -public class EntityWithMutableAttributesTest extends BaseNonConfigCoreFunctionalTestCase { +public class EntityWithMutableAttributesTest { - boolean skipTest; - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } - else { - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Role.class ); - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - @Before - public void setUp() { - if ( skipTest ) { - return; - } - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = new User(); user.setId( 1 ); @@ -88,12 +86,9 @@ public class EntityWithMutableAttributesTest extends BaseNonConfigCoreFunctional ); } - @After - public void tearDown() { - if ( skipTest ) { - return; - } - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from User" ).executeUpdate(); session.createQuery( "delete from Role" ).executeUpdate(); @@ -102,11 +97,8 @@ public class EntityWithMutableAttributesTest extends BaseNonConfigCoreFunctional } @Test - public void testLoad() { - if ( skipTest ) { - return; - } - inTransaction( + public void testLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = session.load( User.class, 1 ); assertThat( @@ -122,18 +114,15 @@ public class EntityWithMutableAttributesTest extends BaseNonConfigCoreFunctional } @Test - public void testMutableAttributeIsUpdated() { - if ( skipTest ) { - return; - } - inTransaction( + public void testMutableAttributeIsUpdated(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = session.load( User.class, 1 ); user.getDate().setTime( 0 ); } ); - inTransaction( + scope.inTransaction( session -> { User user = session.getReference( User.class, 1 ); assertThat( user.getDate().getTime(), is( 0L ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadAndUpdateEntitiesWithCollectionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadAndUpdateEntitiesWithCollectionsTest.java index cdeff802eb..b9e0cfe999 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadAndUpdateEntitiesWithCollectionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadAndUpdateEntitiesWithCollectionsTest.java @@ -13,8 +13,6 @@ import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Root; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; @@ -22,54 +20,54 @@ import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeFalse; -@TestForIssue(jiraKey = "HHH14424") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH14424") +@DomainModel( + annotatedClasses = { + SamplingOrder.class, + Customer.class, + User.class, + Role.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) @RequiresDialectFeature(DialectChecks.SupportsIdentityColumns.class) -public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreFunctionalTestCase { +public class LoadAndUpdateEntitiesWithCollectionsTest { - boolean skipTest; - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } - else { - sources.addAnnotatedClass( SamplingOrder.class ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Role.class ); - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - @Before - public void setUp() { - if ( skipTest ) { - return; - } - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = new User(); user.setEmail( "foo@bar.com" ); @@ -95,12 +93,9 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF ); } - @After - public void tearDown() { - if ( skipTest ) { - return; - } - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from SamplingOrder" ).executeUpdate(); session.createQuery( "delete from Customer" ).executeUpdate(); @@ -111,11 +106,8 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } @Test - public void testLoad() { - if ( skipTest ) { - return; - } - inTransaction( + public void testLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { CriteriaBuilder cb = session.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery( SamplingOrder.class ); @@ -127,7 +119,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); @@ -137,11 +129,8 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } @Test - public void testAddUserRoles() { - if ( skipTest ) { - return; - } - inTransaction( + public void testAddUserRoles(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrder( session ); User user = samplingOrder.getCustomer().getUser(); @@ -152,7 +141,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); @@ -161,7 +150,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrder( session ); User user = samplingOrder.getCustomer().getUser(); @@ -172,7 +161,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); @@ -181,7 +170,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { User user = session .createQuery( @@ -196,7 +185,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session .createQuery( @@ -214,11 +203,8 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } @Test - public void testDeleteUserRoles() { - if ( skipTest ) { - return; - } - inTransaction( + public void testDeleteUserRoles(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrder( session ); User user = samplingOrder.getCustomer().getUser(); @@ -226,7 +212,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); @@ -237,11 +223,8 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } @Test - public void testModifyUserMail() { - if ( skipTest ) { - return; - } - inTransaction( + public void testModifyUserMail(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrder( session ); User user = samplingOrder.getCustomer().getUser(); @@ -249,7 +232,7 @@ public class LoadAndUpdateEntitiesWithCollectionsTest extends BaseNonConfigCoreF } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadUninitializedCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadUninitializedCollectionTest.java index 387fda91f0..615228d63c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadUninitializedCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadUninitializedCollectionTest.java @@ -9,7 +9,6 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecki import java.util.ArrayList; import java.util.List; -import java.util.Map; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -22,52 +21,44 @@ import jakarta.persistence.Table; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assumptions.assumeFalse; -@TestForIssue(jiraKey = "HHH-14549") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14549") +@DomainModel( + annotatedClasses = { + LoadUninitializedCollectionTest.Bank.class, + LoadUninitializedCollectionTest.BankAccount.class, + LoadUninitializedCollectionTest.BankDepartment.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -public class LoadUninitializedCollectionTest extends BaseEntityManagerFunctionalTestCase { +public class LoadUninitializedCollectionTest { - boolean skipTest; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Bank.class, - BankAccount.class, - BankDepartment.class - }; - } - - @Override - protected void addMappings(Map settings) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - - @Before - public void setUp() { - if ( skipTest ) { - return; - } - doInJPA( - this::entityManagerFactory, entityManager -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { Bank bank = new Bank( 1L, "International" ); BankAccount bankAccount = new BankAccount( 1L, bank, "1234567890" ); BankDepartment bankDepartmentA = new BankDepartment( 1L, "A" ); @@ -88,11 +79,8 @@ public class LoadUninitializedCollectionTest extends BaseEntityManagerFunctional } @Test - public void testLoadAfterNativeQueryExecution() { - if ( skipTest ) { - return; - } - doInJPA( this::entityManagerFactory, entityManager -> { + public void testLoadAfterNativeQueryExecution(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { BankAccount account = entityManager.find( BankAccount.class, 1L ); Query nativeQuery = entityManager.createNativeQuery( "SELECT ID FROM BANK" ); @@ -107,11 +95,8 @@ public class LoadUninitializedCollectionTest extends BaseEntityManagerFunctional } @Test - public void testLoadAfterFlush() { - if ( skipTest ) { - return; - } - doInJPA( this::entityManagerFactory, entityManager -> { + public void testLoadAfterFlush(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { BankAccount account = entityManager.find( BankAccount.class, 1L ); entityManager.flush(); @@ -124,12 +109,9 @@ public class LoadUninitializedCollectionTest extends BaseEntityManagerFunctional ); } - @After - public void tearDown() { - if ( skipTest ) { - return; - } - doInJPA( this::entityManagerFactory, entityManager -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { Bank bank = entityManager.find( Bank.class, 1L ); bank.getDepartments().forEach( department -> entityManager.remove( department ) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.java index 389345542a..bdcf83ace0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.java @@ -2,7 +2,6 @@ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecki import java.util.ArrayList; import java.util.List; -import java.util.Map; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -11,58 +10,56 @@ import jakarta.persistence.MappedSuperclass; import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; -import org.hibernate.FlushMode; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assumptions.assumeFalse; -@TestForIssue( jiraKey = "HHH-14549") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-14549") +@DomainModel( + annotatedClasses = { + LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.Customer.class, + LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.ProductOrder.class, + LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.Product.class, + LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest.City.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FLUSH_MODE, value = "ALWAYS" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -public class LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest - extends BaseEntityManagerFunctionalTestCase { +public class LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest { - boolean skipTest; - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Customer.class, - ProductOrder.class, - Product.class, - City.class - }; - } - - @Override - protected void addMappings(Map settings) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - settings.put( AvailableSettings.FLUSH_MODE, FlushMode.ALWAYS ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - @Before - public void setUp() { - if ( skipTest ) { - return; - } - doInJPA( - this::entityManagerFactory, entityManager -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { ProductOrder order = new ProductOrder(); order.setOrderNumber( "12345" ); @@ -83,12 +80,8 @@ public class LoadingLazyCollectionAfterQueryExecutionWithFlushModeAlwaysTest } @Test - public void reproducer_Case1() { - if ( skipTest ) { - return; - } - doInJPA( - this::entityManagerFactory, entityManager -> { + public void reproducer_Case1(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { List customers = entityManager.createQuery( "select c from Customer c" ).getResultList(); assertEquals( 1, customers.size() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOnePropertyAccessByFieldTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOnePropertyAccessByFieldTest.java index 5fbc7e0448..9931909726 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOnePropertyAccessByFieldTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOnePropertyAccessByFieldTest.java @@ -34,20 +34,20 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import org.hibernate.annotations.NaturalId; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; @@ -56,43 +56,37 @@ import static org.hamcrest.MatcherAssert.assertThat; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ManyToOnePropertyAccessByFieldTest.User.class, + ManyToOnePropertyAccessByFieldTest.Office.class, + ManyToOnePropertyAccessByFieldTest.Client.class, + ManyToOnePropertyAccessByFieldTest.Request.class, + ManyToOnePropertyAccessByFieldTest.InternalRequest.class, + ManyToOnePropertyAccessByFieldTest.Phone.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -@TestForIssue(jiraKey = "HHH-13705") -public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Office.class ); - sources.addAnnotatedClass( Client.class ); - sources.addAnnotatedClass( Request.class ); - sources.addAnnotatedClass( InternalRequest.class ); - sources.addAnnotatedClass( Phone.class ); - } +@JiraKey("HHH-13705") +public class ManyToOnePropertyAccessByFieldTest { private Long userId; private Long targetUserId; private Long officeId; - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Log log = new Log(); log.setCreationDate( OffsetDateTime.now() ); @@ -127,9 +121,9 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Request" ).executeUpdate(); session.createQuery( "delete from User" ).executeUpdate(); @@ -141,12 +135,12 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } @Test - public void testPersist() { - final Statistics stats = sessionFactory().getStatistics(); + public void testPersist(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); InternalRequest internalRequest = new InternalRequest( 1L ); - inTransaction( + scope.inTransaction( session -> { User user = session.find( User.class, userId ); internalRequest.setUser( user ); @@ -165,18 +159,18 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } @Test - public void testDelete() { + public void testDelete(SessionFactoryScope scope) { Set officePhones = new HashSet<>(); officePhones.add( new Phone( 1L, "landline", "028-234-9876" ) ); officePhones.add( new Phone( 2L, "mobile", "072-122-9876" ) ); Office office = buildOffice( "second office", "Fab", officePhones ); - inTransaction( + scope.inTransaction( session -> { session.save( office ); } ); - inTransaction( + scope.inTransaction( session -> { Office result = session.find( Office.class, office.id ); session.delete( result ); @@ -184,7 +178,7 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio ); - inTransaction( + scope.inTransaction( session -> { List offices = session.createQuery( "from Office" ).list(); assertThat( offices.size(), is( 1 ) ); @@ -197,9 +191,9 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } @Test - public void testUpdate() { + public void testUpdate(SessionFactoryScope scope) { InternalRequest internalRequest = new InternalRequest( 1L ); - inTransaction( + scope.inTransaction( session -> { User user = session.find( User.class, userId ); internalRequest.setUser( user ); @@ -212,7 +206,7 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } ); - inTransaction( + scope.inTransaction( session -> { InternalRequest result = session.find( InternalRequest.class, internalRequest.getId() ); assertThat( result.getTargetUser().getId(), is( targetUserId ) ); @@ -221,7 +215,7 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } ); - inTransaction( + scope.inTransaction( session -> { InternalRequest result = session.find( InternalRequest.class, internalRequest.getId() ); assertThat( result.getTargetUser().getId(), is( targetUserId ) ); @@ -233,7 +227,7 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } ); - inTransaction( + scope.inTransaction( session -> { InternalRequest result = session.find( InternalRequest.class, internalRequest.getId() ); assertThat( result.getTargetUser().getId(), is( userId ) ); @@ -257,7 +251,7 @@ public class ManyToOnePropertyAccessByFieldTest extends BaseNonConfigCoreFunctio } ); - inTransaction( + scope.inTransaction( session -> { InternalRequest result = session.find( InternalRequest.class, internalRequest.getId() ); assertThat( result.getTargetUser().getId(), is( userId ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOneWithEmbeddedAndNotOptionalFieldTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOneWithEmbeddedAndNotOptionalFieldTest.java index e87bf89943..91cb99c7a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOneWithEmbeddedAndNotOptionalFieldTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/ManyToOneWithEmbeddedAndNotOptionalFieldTest.java @@ -18,19 +18,19 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -38,39 +38,32 @@ import static org.hamcrest.MatcherAssert.assertThat; /** * @author Andrea Boriero */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ManyToOneWithEmbeddedAndNotOptionalFieldTest.Client.class, + ManyToOneWithEmbeddedAndNotOptionalFieldTest.User.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.FORMAT_SQL, value = "false" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ DirtyCheckEnhancementContext.class, NoDirtyCheckEnhancementContext.class }) -@TestForIssue(jiraKey = "HHH-13705") -public class ManyToOneWithEmbeddedAndNotOptionalFieldTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.FORMAT_SQL, "false" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - super.configureSessionFactoryBuilder( sfb ); - sfb.applyStatisticsSupport( true ); - sfb.applySecondLevelCacheSupport( false ); - sfb.applyQueryCacheSupport( false ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Client.class ); - sources.addAnnotatedClass( User.class ); - } +@JiraKey("HHH-13705") +public class ManyToOneWithEmbeddedAndNotOptionalFieldTest { private Long userId; - @Before - public void setUp() { + @BeforeEach + public void setUp(SessionFactoryScope scope) { User user = new User(); - inTransaction( + scope.inTransaction( session -> { Log log = new Log(); @@ -95,10 +88,10 @@ public class ManyToOneWithEmbeddedAndNotOptionalFieldTest extends BaseNonConfigC @Test - public void load() { - final Statistics stats = sessionFactory().getStatistics(); + public void load(SessionFactoryScope scope) { + final Statistics stats = scope.getSessionFactory().getStatistics(); stats.clear(); - inTransaction( + scope.inTransaction( session -> { session.find( User.class, userId ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/SimpleDynamicUpdateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/SimpleDynamicUpdateTest.java index 8fd765e34d..d2bd953d01 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/SimpleDynamicUpdateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/SimpleDynamicUpdateTest.java @@ -14,59 +14,58 @@ import jakarta.persistence.Table; import jakarta.validation.constraints.NotNull; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.PersistentAttributeInterceptable; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.junit.jupiter.api.Assumptions.assumeFalse; -@RunWith(BytecodeEnhancerRunner.class) +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@DomainModel( + annotatedClasses = { + SimpleDynamicUpdateTest.User.class, + SimpleDynamicUpdateTest.Role.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100"), + @Setting(name = AvailableSettings.GENERATE_STATISTICS, value = "true"), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class, DirtyCheckEnhancementContext.class }) -public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase { +public class SimpleDynamicUpdateTest { - boolean skipTest; - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { + @BeforeAll + static void beforeAll() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } - else { - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Role.class ); - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - @Before - public void setUp() { - if ( skipTest ) { - return; - } - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = new User(); user.setId( 1 ); @@ -90,11 +89,8 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } @Test - public void testIt() { - if ( skipTest ) { - return; - } - inTransaction( + public void testIt(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = session.getReference( User.class, 1 ); assertThat( @@ -108,14 +104,14 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); entity.setName( "abc" ); } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); assertThat( entity.getName(), is( "abc" ) ); @@ -123,14 +119,14 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); entity.setRole( null ); } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); assertThat( entity.getName(), is( "abc" ) ); @@ -139,14 +135,14 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); entity.setName( null ); } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); assertThat( entity.getName(), is( nullValue() ) ); @@ -154,14 +150,14 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); entity.setAddress( null ); } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); assertThat( entity.getName(), is( nullValue() ) ); @@ -171,7 +167,7 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); Role role = new Role(); @@ -182,7 +178,7 @@ public class SimpleDynamicUpdateTest extends BaseNonConfigCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { User entity = session.getReference( User.class, 1 ); assertThat( entity.getName(), is( nullValue() ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/dynamicupdate/DynamicUpdateAndCollectionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/dynamicupdate/DynamicUpdateAndCollectionsTest.java index 2c105926c3..25f3749225 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/dynamicupdate/DynamicUpdateAndCollectionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/proxy/inlinedirtychecking/dynamicupdate/DynamicUpdateAndCollectionsTest.java @@ -6,6 +6,9 @@ */ package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.dynamicupdate; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + import java.util.HashSet; import java.util.List; import jakarta.persistence.TypedQuery; @@ -13,8 +16,6 @@ import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Root; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; @@ -22,57 +23,54 @@ import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.DirtyCheckEnhancementContext; import org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.inlinedirtychecking.NoDirtyCheckEnhancementContext; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "HHH14424") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH14424") +@DomainModel( + annotatedClasses = { + SamplingOrder.class, + Customer.class, + User.class, + Role.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "100" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class, DirtyCheckEnhancementContext.class }) @RequiresDialectFeature(DialectChecks.SupportsIdentityColumns.class) -public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctionalTestCase { +public class DynamicUpdateAndCollectionsTest { - boolean skipTest; - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void applyMetadataSources(MetadataSources sources) { + @BeforeAll + protected void applyMetadataSources() { String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); - if ( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { - // skip the test if the bytecode provider is Javassist - skipTest = true; - } - else { - sources.addAnnotatedClass( SamplingOrder.class ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( Role.class ); - } + assumeFalse( byteCodeProvider != null && !BytecodeProviderInitiator.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( + byteCodeProvider ) ); } - @Before - public void setUp() { - if ( skipTest ) { - return; - } - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user = new User(); user.setEmail( "foo@bar.com" ); @@ -97,12 +95,9 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional ); } - @After - public void tearDown() { - if ( skipTest ) { - return; - } - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from SamplingOrder" ).executeUpdate(); session.createQuery( "delete from Customer" ).executeUpdate(); @@ -113,11 +108,8 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } @Test - public void testLoad() { - if ( skipTest ) { - return; - } - inTransaction( + public void testLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { CriteriaBuilder cb = session.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery( SamplingOrder.class ); @@ -129,21 +121,18 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getEmail(), is( "foo@bar.com" ) ); + assertThat( user.getEmail() ).isEqualTo( "foo@bar.com" ); } ); } @Test - public void testRemoveCustomers() { - if ( skipTest ) { - return; - } - Long samplingOrderId = fromTransaction( + public void testRemoveCustomers(SessionFactoryScope scope) { + Long samplingOrderId = scope.fromTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrderFetchCustomer( session ); samplingOrder.setCustomer( null ); @@ -151,20 +140,17 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { SamplingOrder samplingOrder = session.get( SamplingOrder.class, samplingOrderId ); - assertThat( samplingOrder.getCustomer(), is( nullValue() ) ); + assertThat( samplingOrder.getCustomer() ).isNull(); } ); } @Test - public void testAddUserRoles() { - if ( skipTest ) { - return; - } - inTransaction( + public void testAddUserRoles(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrderFetchCustomer( session ); User user = samplingOrder.getCustomer().getUser(); @@ -175,16 +161,16 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getEmail(), is( "foo@bar.com" ) ); - assertThat( user.getRoles().size(), is( 2 ) ); + assertThat( user.getEmail() ).isEqualTo( "foo@bar.com" ); + assertThat( user.getRoles() ).hasSize( 2 ); } ); - inTransaction( + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrderFetchCustomer( session ); User user = samplingOrder.getCustomer().getUser(); @@ -194,16 +180,16 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getEmail(), is( "foo@bar.com" ) ); - assertThat( user.getRoles().size(), is( 3 ) ); + assertThat( user.getEmail() ).isEqualTo( "foo@bar.com" ); + assertThat( user.getRoles() ).hasSize( 3 ); } ); - inTransaction( + scope.inTransaction( session -> { User user = session.createQuery( "from User", User.class ).list().get( 0 ); Role role = new Role(); @@ -212,23 +198,20 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getEmail(), is( "foo@bar.com" ) ); - assertThat( user.getRoles().size(), is( 4 ) ); + assertThat( user.getEmail() ).isEqualTo( "foo@bar.com" ); + assertThat( user.getRoles() ).hasSize( 4 ); } ); } @Test - public void testDeleteUserRoles() { - if ( skipTest ) { - return; - } - inTransaction( + public void testDeleteUserRoles(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrderFetchCustomer( session ); User user = samplingOrder.getCustomer().getUser(); @@ -236,21 +219,18 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getRoles().size(), is( 0 ) ); + assertThat( user.getRoles() ).isEmpty(); } ); } @Test - public void testModifyUserMail() { - if ( skipTest ) { - return; - } - inTransaction( + public void testModifyUserMail(SessionFactoryScope scope) { + scope.inTransaction( session -> { SamplingOrder samplingOrder = getSamplingOrderFetchCustomer( session ); User user = samplingOrder.getCustomer().getUser(); @@ -258,12 +238,12 @@ public class DynamicUpdateAndCollectionsTest extends BaseNonConfigCoreFunctional } ); - inTransaction( + scope.inTransaction( session -> { List users = session.createQuery( "from User u", User.class ).list(); User user = users.get( 0 ); - assertThat( user.getEmail(), is( "bar@foo.com" ) ); - assertThat( user.getRoles().size(), is( 1 ) ); + assertThat( user.getEmail() ).isEqualTo( "bar@foo.com" ); + assertThat( user.getRoles() ).hasSize( 1 ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java index 38efc75bdf..f970cc8953 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java @@ -15,16 +15,18 @@ import org.hibernate.annotations.Formula; import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.entry.StandardCacheEntryImpl; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.graph.RootGraph; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Cacheable; @@ -39,41 +41,41 @@ import jakarta.persistence.criteria.CriteriaQuery; import static org.hibernate.Hibernate.isPropertyInitialized; import static org.hibernate.jpa.SpecHints.HINT_SPEC_FETCH_GRAPH; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve EbersolenPropertyRefTest */ -@RunWith( BytecodeEnhancerRunner.class ) -public class InitFromCacheTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + InitFromCacheTest.Document.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class InitFromCacheTest { private EntityPersister persister; private Long documentID; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Document.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); - configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, true ); - } - - @Before - public void prepare() { - persister = sessionFactory().getRuntimeMetamodels() + @BeforeEach + public void prepare(SessionFactoryScope scope) { + persister = scope.getSessionFactory().getRuntimeMetamodels() .getMappingMetamodel() .getEntityDescriptor( Document.class ); assertTrue( persister.hasCache() ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Document document = new Document( "HiA", "Hibernate book", "Hibernate is...." ); s.persist( document ); documentID = document.id; @@ -81,11 +83,8 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { } @Test - public void execute() { - - doInHibernate( - this::sessionFactory, - s -> { + public void execute(SessionFactoryScope scope) { + scope.inTransaction( s -> { final RootGraph entityGraph = s.createEntityGraph( Document.class ); entityGraph.addAttributeNodes( "text", "summary" ); final Document document = s.createQuery( "from Document", Document.class ) @@ -98,7 +97,7 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { final Object cacheKey = entityDataAccess.generateCacheKey( document.id, persister, - sessionFactory(), + scope.getSessionFactory(), null ); final Object cachedItem = entityDataAccess.get( (SharedSessionContractImplementor) s, cacheKey ); @@ -107,9 +106,9 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { } ); - sessionFactory().getStatistics().clear(); + scope.getSessionFactory().getStatistics().clear(); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Document.class ); criteria.from( Document.class ); @@ -122,9 +121,9 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { assertTrue( isPropertyInitialized( d, "summary" ) ); } ); - assertEquals( 2, sessionFactory().getStatistics().getPrepareStatementCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getPrepareStatementCount() ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Document d = s.get( Document.class, documentID ); assertFalse( isPropertyInitialized( d, "text" ) ); assertFalse( isPropertyInitialized( d, "summary" ) ); @@ -137,7 +136,7 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { @Table( name = "DOCUMENT" ) @Cacheable @Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "non-lazy", region = "foo" ) - private static class Document { + static class Document { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/locking/OptimisticLockTypeDirtyWithLazyOneToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/locking/OptimisticLockTypeDirtyWithLazyOneToOneTest.java index 799df51b47..47a41d4903 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/locking/OptimisticLockTypeDirtyWithLazyOneToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/locking/OptimisticLockTypeDirtyWithLazyOneToOneTest.java @@ -11,15 +11,15 @@ import java.util.List; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.OptimisticLockType; import org.hibernate.annotations.OptimisticLocking; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -30,31 +30,24 @@ import jakarta.persistence.OneToOne; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) @JiraKey("HHH-16839") -public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + OptimisticLockTypeDirtyWithLazyOneToOneTest.Address.class, + OptimisticLockTypeDirtyWithLazyOneToOneTest.Person.class + } +) +@SessionFactory +@BytecodeEnhanced +public class OptimisticLockTypeDirtyWithLazyOneToOneTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Address.class, - Person.class - }; + SQLStatementInspector statementInspector(SessionFactoryScope scope) { + return (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions().getStatementInspector(); } - @Override - protected void afterConfigurationBuilt(Configuration configuration) { - super.afterConfigurationBuilt( configuration ); - configuration.setStatementInspector( new SQLStatementInspector() ); - } - - SQLStatementInspector statementInspector() { - return (SQLStatementInspector) sessionFactory().getSessionFactoryOptions().getStatementInspector(); - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Person" ).executeUpdate(); session.createMutationQuery( "delete from Address" ).executeUpdate(); @@ -63,20 +56,20 @@ public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctio } @Test - public void testUpdate() { - inTransaction( + public void testUpdate(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1L, "Name", new Address( 10L, "Street" ) ); session.persist( person ); } ); - inTransaction( + scope.inTransaction( session -> { Person person = session.find( Person.class, 1L ); person.getAddress().setStreet( "new Street" ); } ); - inTransaction( + scope.inTransaction( session -> { Address address = session.find( Address.class, 10L ); assertThat( address.getStreet() ).isEqualTo( "new Street" ); @@ -85,18 +78,18 @@ public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctio } @Test - public void testUpdate2() { - inTransaction( + public void testUpdate2(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1L, "Name", new Address( 10L, null ) ); session.persist( person ); } ); - SQLStatementInspector statementInspector = statementInspector(); + SQLStatementInspector statementInspector = statementInspector(scope); statementInspector.clear(); - inTransaction( + scope.inTransaction( session -> { Address address = session.find( Address.class, 10L ); address.setStreet( "new Street" ); @@ -110,7 +103,7 @@ public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctio String updateQuery = sqlQueries.get( 1 ); updateQuery.contains( "where id=? and street is null" ); - inTransaction( + scope.inTransaction( session -> { Address address = session.find( Address.class, 10L ); assertThat( address.getStreet() ).isEqualTo( "new Street" ); @@ -119,17 +112,17 @@ public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctio } @Test - public void testUpdate3() { - inTransaction( + public void testUpdate3(SessionFactoryScope scope) { + scope.inTransaction( session -> { Person person = new Person( 1L, "Name", new Address( 10L, null ) ); session.persist( person ); } ); - SQLStatementInspector statementInspector = statementInspector(); + SQLStatementInspector statementInspector = statementInspector(scope); statementInspector.clear(); - inTransaction( + scope.inTransaction( session -> { Address address = session.find( Address.class, 10L ); address.setStreet( "new Street" ); @@ -143,7 +136,7 @@ public class OptimisticLockTypeDirtyWithLazyOneToOneTest extends BaseCoreFunctio String updateQuery = sqlQueries.get( 1 ); updateQuery.contains( "where id=? and street=?" ); - inTransaction( + scope.inTransaction( session -> { Address address = session.find( Address.class, 10L ); assertThat( address.getStreet() ).isEqualTo( "new Street" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeMergeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeMergeTest.java index d408e01d40..8a8fe12529 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeMergeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeMergeTest.java @@ -6,12 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.merge; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.CollectionTable; @@ -28,23 +28,24 @@ import java.util.Arrays; import java.util.List; import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.checkDirtyTracking; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) -public class CompositeMergeTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + CompositeMergeTest.ParentEntity.class, CompositeMergeTest.Address.class, CompositeMergeTest.Country.class + } +) +@SessionFactory +@BytecodeEnhanced +public class CompositeMergeTest { private long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ParentEntity.class, Address.class, Country.class}; - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { ParentEntity parent = new ParentEntity(); parent.description = "desc"; parent.address = new Address(); @@ -55,7 +56,7 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { parent.lazyField = new byte[100]; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.persist( parent ); } ); @@ -64,10 +65,10 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { + public void test(SessionFactoryScope scope) { ParentEntity[] parent = new ParentEntity[3]; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { parent[0] = s.get( ParentEntity.class, entityId ); } ); @@ -77,7 +78,7 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { checkDirtyTracking( parent[0], "address.country" ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { parent[1] = (ParentEntity) s.merge( parent[0] ); checkDirtyTracking( parent[0], "address.country" ); checkDirtyTracking( parent[1], "address.country" ); @@ -90,14 +91,14 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { checkDirtyTracking( parent[1], "address.country" ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.saveOrUpdate( parent[1] ); checkDirtyTracking( parent[1], "address.country" ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { parent[2] = s.get( ParentEntity.class, entityId ); - Assert.assertEquals( "Honduras", parent[2].address.country.name ); + assertEquals( "Honduras", parent[2].address.country.name ); } ); } @@ -105,7 +106,7 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { @Entity(name = "Parent") @Table( name = "PARENT_ENTITY" ) - private static class ParentEntity { + static class ParentEntity { @Id @GeneratedValue @@ -122,7 +123,7 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { @Embeddable @Table( name = "ADDRESS" ) - private static class Address { + static class Address { String street; @@ -132,7 +133,7 @@ public class CompositeMergeTest extends BaseCoreFunctionalTestCase { @Embeddable @Table( name = "COUNTRY" ) - private static class Country { + static class Country { String name; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeNullTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeNullTest.java index 41493f7a39..319e95f68b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeNullTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/CompositeNullTest.java @@ -6,14 +6,12 @@ */ package org.hibernate.orm.test.bytecode.enhancement.merge; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -22,28 +20,33 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Table; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * @author Luis Barreiro */ -@RunWith( BytecodeEnhancerRunner.class ) +@DomainModel( + annotatedClasses = { + CompositeNullTest.ParentEntity.class, CompositeNullTest.Address.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true) -public class CompositeNullTest extends BaseCoreFunctionalTestCase { +public class CompositeNullTest { private long entityId; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ParentEntity.class, Address.class}; - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { ParentEntity parent = new ParentEntity(); parent.description = "Test"; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.persist( parent ); } ); @@ -51,11 +54,11 @@ public class CompositeNullTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue( jiraKey = "HHH-15730") - public void testNullComposite() { - doInHibernate( this::sessionFactory, s -> { + @JiraKey("HHH-15730") + public void testNullComposite(SessionFactoryScope scope) { + scope.inTransaction( s -> { ParentEntity parentEntity = s.find( ParentEntity.class, entityId ); - Assert.assertNull( parentEntity.address ); + assertNull( parentEntity.address ); } ); } @@ -63,7 +66,7 @@ public class CompositeNullTest extends BaseCoreFunctionalTestCase { @Entity(name = "Parent") @Table( name = "PARENT_ENTITY" ) - private static class ParentEntity { + static class ParentEntity { @Id @GeneratedValue @@ -77,7 +80,7 @@ public class CompositeNullTest extends BaseCoreFunctionalTestCase { @Embeddable @Table( name = "ADDRESS" ) - private static class Address { + static class Address { String street; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedCascadedCollectionInEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedCascadedCollectionInEmbeddableTest.java index cd08dda6ca..8c738f9267 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedCascadedCollectionInEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedCascadedCollectionInEmbeddableTest.java @@ -17,29 +17,36 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToMany; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.hibernate.orm.test.bytecode.enhancement.merge.MergeDetachedCascadedCollectionInEmbeddableTest.Grouping; +import static org.hibernate.orm.test.bytecode.enhancement.merge.MergeDetachedCascadedCollectionInEmbeddableTest.Heading; +import static org.hibernate.orm.test.bytecode.enhancement.merge.MergeDetachedCascadedCollectionInEmbeddableTest.Thing; import static org.junit.Assert.assertNotSame; +import org.junit.jupiter.api.Test; + + /** * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-12592") -@RunWith(BytecodeEnhancerRunner.class) -public class MergeDetachedCascadedCollectionInEmbeddableTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Heading.class, Grouping.class, Thing.class }; - } +@JiraKey("HHH-12592") +@DomainModel( + annotatedClasses = { + Heading.class, Grouping.class, Thing.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MergeDetachedCascadedCollectionInEmbeddableTest { @Test - public void testMergeDetached() { - final Heading heading = doInHibernate( this::sessionFactory, session -> { + public void testMergeDetached(SessionFactoryScope scope) { + final Heading heading = scope.fromSession( session -> { Heading entity = new Heading(); entity.name = "new"; entity.setGrouping( new Grouping() ); @@ -48,7 +55,7 @@ public class MergeDetachedCascadedCollectionInEmbeddableTest extends BaseCoreFun return entity; } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { heading.name = "updated"; Heading headingMerged = (Heading) session.merge( heading ); assertNotSame( heading, headingMerged ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedNonCascadedCollectionInEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedNonCascadedCollectionInEmbeddableTest.java index 76d229a391..e7cea27725 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedNonCascadedCollectionInEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeDetachedNonCascadedCollectionInEmbeddableTest.java @@ -16,29 +16,33 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToMany; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.hibernate.orm.test.bytecode.enhancement.merge.MergeDetachedNonCascadedCollectionInEmbeddableTest.*; import static org.junit.Assert.assertNotSame; +import org.junit.jupiter.api.Test; + /** * @author Gail Badner */ -@TestForIssue(jiraKey = "HHH-12637") -@RunWith(BytecodeEnhancerRunner.class) -public class MergeDetachedNonCascadedCollectionInEmbeddableTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Heading.class, Grouping.class, Thing.class }; - } +@JiraKey("HHH-12637") +@DomainModel( + annotatedClasses = { + Heading.class, Grouping.class, Thing.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MergeDetachedNonCascadedCollectionInEmbeddableTest { @Test - public void testMergeDetached() { - final Heading heading = doInHibernate( this::sessionFactory, session -> { + public void testMergeDetached(SessionFactoryScope scope) { + final Heading heading = scope.fromTransaction( session -> { Heading entity = new Heading(); entity.name = "new"; entity.setGrouping( new Grouping() ); @@ -49,7 +53,7 @@ public class MergeDetachedNonCascadedCollectionInEmbeddableTest extends BaseCore return entity; } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { heading.name = "updated"; Heading headingMerged = (Heading) session.merge( heading ); assertNotSame( heading, headingMerged ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedDetachedOrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedDetachedOrphanRemovalTest.java index eae8f99bca..fc18345591 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedDetachedOrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedDetachedOrphanRemovalTest.java @@ -6,47 +6,51 @@ */ package org.hibernate.orm.test.bytecode.enhancement.merge; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; + +import static org.junit.jupiter.api.Assertions.assertNotSame; + +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotSame; /** * @author Chris Cranford */ -@TestForIssue(jiraKey = "HHH-12592") -@RunWith(BytecodeEnhancerRunner.class) -public class MergeEnhancedDetachedOrphanRemovalTest extends BaseCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Leaf.class, Root.class }; - } +@JiraKey("HHH-12592") +@DomainModel( + annotatedClasses = { + Leaf.class, Root.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MergeEnhancedDetachedOrphanRemovalTest { @Test - public void testMergeDetachedOrphanRemoval() { - final Root entity = doInHibernate( this::sessionFactory, session -> { + public void testMergeDetachedOrphanRemoval(SessionFactoryScope scope) { + final Root entity = scope.fromTransaction( session -> { Root root = new Root(); root.setName( "new" ); session.save( root ); return root; } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { entity.setName( "updated" ); - Root entityMerged = (Root) session.merge( entity ); + Root entityMerged = session.merge( entity ); assertNotSame( entity, entityMerged ); assertNotSame( entity.getLeaves(), entityMerged.getLeaves() ); } ); } @Test - public void testMergeDetachedNonEmptyCollection() { - final Root entity = doInHibernate( this::sessionFactory, session -> { + public void testMergeDetachedNonEmptyCollection(SessionFactoryScope scope) { + final Root entity = scope.fromTransaction( session -> { Root root = new Root(); root.setName( "new" ); Leaf leaf = new Leaf(); @@ -56,9 +60,9 @@ public class MergeEnhancedDetachedOrphanRemovalTest extends BaseCoreFunctionalTe return root; } ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { entity.setName( "updated" ); - Root entityMerged = (Root) session.merge( entity ); + Root entityMerged = session.merge( entity ); assertNotSame( entity, entityMerged ); assertNotSame( entity.getLeaves(), entityMerged.getLeaves() ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedEntityTest.java index e0177f0549..da51afd97a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeEnhancedEntityTest.java @@ -6,13 +6,14 @@ */ package org.hibernate.orm.test.bytecode.enhancement.merge; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -26,32 +27,35 @@ import java.util.ArrayList; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-11459" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-11459" ) +@DomainModel( + annotatedClasses = { + MergeEnhancedEntityTest.Person.class, + MergeEnhancedEntityTest.PersonAddress.class, + MergeEnhancedEntityTest.NullablePerson.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MergeEnhancedEntityTest { private Person person; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Person.class, PersonAddress.class, NullablePerson.class}; - } - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { person = new Person( 1L, "Sam" ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.persist( person ); } ); } @Test - public void testMerge() { - doInHibernate( this::sessionFactory, s -> { + public void testMerge(SessionFactoryScope scope) { + scope.inTransaction( s -> { Person entity = s.find( Person.class, 1L ); entity.name = "John"; try { @@ -63,8 +67,8 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh() { - doInHibernate( this::sessionFactory, s -> { + public void testRefresh(SessionFactoryScope scope) { + scope.inTransaction( s -> { Person entity = s.find( Person.class, 1L ); entity.name = "John"; @@ -78,47 +82,47 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { } ); } - @Test - public void testMergeWithNullValues() { - doInHibernate( this::sessionFactory, em -> { - NullablePerson nullablePerson = new NullablePerson( 1L, "Sam", 100 ); - em.persist( nullablePerson ); - } ); - doInHibernate( this::sessionFactory, em -> { - NullablePerson updated = em.find( NullablePerson.class, 1L ); - assertThat( updated.name ).isEqualTo( "Sam" ); - assertThat( updated.number ).isEqualTo( 100 ); - } ); + @Test + public void testMergeWithNullValues(SessionFactoryScope scope) { + scope.inTransaction( em -> { + NullablePerson nullablePerson = new NullablePerson( 1L, "Sam", 100 ); + em.persist( nullablePerson ); + } ); + scope.inTransaction( em -> { + NullablePerson updated = em.find( NullablePerson.class, 1L ); + assertThat( updated.name ).isEqualTo( "Sam" ); + assertThat( updated.number ).isEqualTo( 100 ); + } ); - // only some properties are null - doInHibernate( this::sessionFactory, em -> { - NullablePerson nullablePerson = new NullablePerson( 1L, "Joe", null ); - em.merge( nullablePerson ); - } ); - doInHibernate( this::sessionFactory, em -> { - NullablePerson updated = em.find( NullablePerson.class, 1L ); - assertThat( updated.name ).isEqualTo( "Joe" ); - assertThat( updated.number ).isNull(); - } ); + // only some properties are null + scope.inTransaction( em -> { + NullablePerson nullablePerson = new NullablePerson( 1L, "Joe", null ); + em.merge( nullablePerson ); + } ); + scope.inTransaction( em -> { + NullablePerson updated = em.find( NullablePerson.class, 1L ); + assertThat( updated.name ).isEqualTo( "Joe" ); + assertThat( updated.number ).isNull(); + } ); - // all properties are null: - doInHibernate( this::sessionFactory, em -> { - NullablePerson nullablePerson = new NullablePerson( 1L, null, null ); - em.merge( nullablePerson ); - } ); - doInHibernate( this::sessionFactory, em -> { - NullablePerson updated = em.find( NullablePerson.class, 1L ); - assertThat( updated.name ).isNull(); - assertThat( updated.number ).isNull(); - } ); - } + // all properties are null: + scope.inTransaction( em -> { + NullablePerson nullablePerson = new NullablePerson( 1L, null, null ); + em.merge( nullablePerson ); + } ); + scope.inTransaction( em -> { + NullablePerson updated = em.find( NullablePerson.class, 1L ); + assertThat( updated.name ).isNull(); + assertThat( updated.number ).isNull(); + } ); + } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.delete( person ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.createQuery( "delete from NullablePerson" ); } ); } @@ -127,7 +131,7 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PERSON" ) - private static class Person { + static class Person { @Id Long id; @@ -149,7 +153,7 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PERSON_ADDRESS" ) - private static class PersonAddress { + static class PersonAddress { @Id Long id; @@ -160,7 +164,7 @@ public class MergeEnhancedEntityTest extends BaseCoreFunctionalTestCase { @Entity(name = "NullablePerson") @Table(name = "NULLABLE_PERSON") - private static class NullablePerson { + static class NullablePerson { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeUnsavedEntitiesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeUnsavedEntitiesTest.java index f7b742e383..f8bba25be6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeUnsavedEntitiesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeUnsavedEntitiesTest.java @@ -3,12 +3,11 @@ package org.hibernate.orm.test.bytecode.enhancement.merge; import java.util.ArrayList; import java.util.List; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -18,25 +17,27 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import static jakarta.persistence.CascadeType.MERGE; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16322") -public class MergeUnsavedEntitiesTest extends BaseCoreFunctionalTestCase { +import org.junit.jupiter.api.Test; + + +@DomainModel( + annotatedClasses = { + MergeUnsavedEntitiesTest.Parent.class, + MergeUnsavedEntitiesTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16322") +public class MergeUnsavedEntitiesTest { public static final String CHILD_NAME = "first child"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - @Test - public void testMerge() { - inTransaction( + public void testMerge(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = new Parent( 1l, 2l ); parent = session.merge( parent ); @@ -47,7 +48,7 @@ public class MergeUnsavedEntitiesTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Parent parent = session.find( Parent.class, 1l ); assertThat( parent.getChildren().size() ).isEqualTo( 1 ); @@ -56,14 +57,14 @@ public class MergeUnsavedEntitiesTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { Parent parent = session.find( Parent.class, 1l ); session.merge( parent ); } ); - inTransaction( + scope.inTransaction( session -> { Parent parent = session.find( Parent.class, 1l ); assertThat( parent.getChildren().size() ).isEqualTo( 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeWithReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeWithReferenceTest.java index 950d3c2dd1..e20f21d0cf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeWithReferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeWithReferenceTest.java @@ -3,12 +3,12 @@ package org.hibernate.orm.test.bytecode.enhancement.merge; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -22,20 +22,19 @@ import org.assertj.core.api.Assertions; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -public class MergeWithReferenceTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + MergeWithReferenceTest.Foo.class, + MergeWithReferenceTest.Bar.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MergeWithReferenceTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Foo.class, - Bar.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Foo" ).executeUpdate(); session.createMutationQuery( "delete from Bar" ).executeUpdate(); @@ -44,13 +43,13 @@ public class MergeWithReferenceTest extends BaseCoreFunctionalTestCase { } @Test - public void testMergeReference() { + public void testMergeReference(SessionFactoryScope scope) { Bar bar = new Bar( "unique3" ); - inTransaction( session -> { + scope.inTransaction( session -> { session.persist( bar ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { Bar reference = session.getReference( Bar.class, bar.getId() ); Foo merged = session.merge( new Foo( reference ) ); Assertions.assertThat( merged.getBar().getKey() ).isEqualTo( bar.getKey() ); @@ -58,19 +57,19 @@ public class MergeWithReferenceTest extends BaseCoreFunctionalTestCase { } @Test - public void testMergeReference2() { + public void testMergeReference2(SessionFactoryScope scope) { Bar bar = new Bar( "unique3" ); - inTransaction( session -> { + scope.inTransaction( session -> { session.persist( bar ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { Bar reference = session.getReference( Bar.class, bar.getId() ); reference.setName( "Davide" ); session.merge( reference ); } ); - inTransaction( + scope.inTransaction( session -> { Bar reference = session.getReference( Bar.class, bar.getId() ); assertThat( reference.getName() ).isEqualTo( "Davide" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/mutable/MutableTypeEnhancementTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/mutable/MutableTypeEnhancementTestCase.java index 618720fc04..15062c279b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/mutable/MutableTypeEnhancementTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/mutable/MutableTypeEnhancementTestCase.java @@ -7,27 +7,30 @@ package org.hibernate.orm.test.bytecode.enhancement.mutable; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.Date; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; -@RunWith(BytecodeEnhancerRunner.class) -public class MutableTypeEnhancementTestCase extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { TestEntity.class }; - } +@DomainModel( + annotatedClasses = { + TestEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MutableTypeEnhancementTestCase { @Test - @TestForIssue(jiraKey = "HHH-14329") - public void testMutateMutableTypeObject() throws Exception { - inTransaction( entityManager -> { + @JiraKey("HHH-14329") + public void testMutateMutableTypeObject(SessionFactoryScope scope) throws Exception { + scope.inTransaction( entityManager -> { TestEntity e = new TestEntity(); e.setId( 1L ); e.setDate( new Date() ); @@ -35,16 +38,16 @@ public class MutableTypeEnhancementTestCase extends BaseCoreFunctionalTestCase { entityManager.persist( e ); } ); - inTransaction( entityManager -> { + scope.inTransaction( entityManager -> { TestEntity e = entityManager.find( TestEntity.class, 1L ); e.getDate().setTime( 0 ); e.getTexts().put( "a", "def" ); } ); - inTransaction( entityManager -> { + scope.inTransaction( entityManager -> { TestEntity e = entityManager.find( TestEntity.class, 1L ); - Assert.assertEquals( 0L, e.getDate().getTime() ); - Assert.assertEquals( "def", e.getTexts().get( "a" ) ); + assertEquals( 0L, e.getDate().getTime() ); + assertEquals( "def", e.getTexts().get( "a" ) ); } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java index 938a7e72b0..52380a52a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java @@ -1,14 +1,14 @@ package org.hibernate.orm.test.bytecode.enhancement.naturalid; -import org.hibernate.Hibernate; import org.hibernate.annotations.NaturalId; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -16,23 +16,22 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToOne; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + QueryWithProxyAsParametersTest.Parent.class, + QueryWithProxyAsParametersTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey( "HHH-17881" ) -public class QueryWithProxyAsParametersTest extends BaseCoreFunctionalTestCase { +public class QueryWithProxyAsParametersTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - Parent.class, - Child.class - }; - } - - @Before - public void setUp(){ - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope){ + scope.inTransaction( session -> { Child child = new Child(1l, "abc", "Andrea"); Parent parent = new Parent(2l, "Lionello", child); @@ -43,8 +42,8 @@ public class QueryWithProxyAsParametersTest extends BaseCoreFunctionalTestCase { } @Test - public void testQuery(){ - inTransaction( + public void testQuery(SessionFactoryScope scope){ + scope.inTransaction( session -> { Child child = session.getReference( Child.class, 1l ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadTest.java index e40a185318..f48dcb9a49 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadTest.java @@ -9,15 +9,14 @@ package org.hibernate.orm.test.bytecode.enhancement.ondemandload; import org.hibernate.annotations.GenericGenerator; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -31,39 +30,42 @@ import jakarta.persistence.Table; import jakarta.persistence.Version; import java.math.BigDecimal; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static org.hibernate.Hibernate.isInitialized; import static org.hibernate.Hibernate.isPropertyInitialized; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10055" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10055" ) +@DomainModel( + annotatedClasses = { + OnDemandLoadTest.Store.class, OnDemandLoadTest.Inventory.class, OnDemandLoadTest.Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class OnDemandLoadTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Store.class, Inventory.class, Product.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, true ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Store store = new Store( 1L ).setName( "Acme Super Outlet" ); s.persist( store ); @@ -75,41 +77,41 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { } @Test - public void testClosedSession() { - sessionFactory().getStatistics().clear(); + public void testClosedSession(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); Store[] store = new Store[1]; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // first load the store, making sure it is not initialized store[0] = s.load( Store.class, 1L ); assertNotNull( store[0] ); assertFalse( isPropertyInitialized( store[0], "inventories" ) ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 0, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); store[0].getInventories(); assertTrue( isPropertyInitialized( store[0], "inventories" ) ); - assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } @Test - public void testClearedSession() { - sessionFactory().getStatistics().clear(); + public void testClearedSession(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // first load the store, making sure collection is not initialized Store store = s.get( Store.class, 1L ); assertNotNull( store ); assertFalse( isInitialized( store.getInventories() ) ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 0, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // then clear session and try to initialize collection s.clear(); @@ -119,15 +121,15 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { assertTrue( isInitialized( store.getInventories() ) ); // the extra Session is the temp Sessions needed to perform the collection init (since it's lazy) - assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // clear Session again. The collection should still be recognized as initialized from above s.clear(); assertNotNull( store ); assertTrue( isInitialized( store.getInventories() ) ); - assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // lets clear the Session again and this time reload the Store s.clear(); @@ -137,27 +139,27 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { // collection should be back to uninitialized since we have a new entity instance assertFalse( isInitialized( store.getInventories() ) ); - assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); store.getInventories().size(); assertTrue( isInitialized( store.getInventories() ) ); // the extra Session is the temp Sessions needed to perform the collection init (since it's lazy) - assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 3, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // clear Session again. The collection should still be recognized as initialized from above s.clear(); assertNotNull( store ); assertTrue( isInitialized( store.getInventories() ) ); - assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 3, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } ); } - @After - public void cleanup() throws Exception { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) throws Exception { + scope.inTransaction( s -> { Store store = s.find( Store.class, 1L ); s.delete( store ); @@ -170,7 +172,7 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "STORE" ) - private static class Store { + static class Store { @Id Long id; @@ -207,7 +209,7 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "INVENTORY" ) - private static class Inventory { + static class Inventory { @Id @GeneratedValue @@ -257,7 +259,7 @@ public class OnDemandLoadTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PRODUCT" ) - private static class Product { + static class Product { @Id String id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest.java index 79603f7f3d..83d28810e6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/ondemandload/OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest.java @@ -8,11 +8,11 @@ package org.hibernate.orm.test.bytecode.enhancement.ondemandload; import static org.hibernate.Hibernate.isPropertyInitialized; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.hibernate.orm.test.bytecode.enhancement.ondemandload.OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; import java.util.ArrayList; @@ -20,20 +20,18 @@ import java.util.Collections; import java.util.List; import org.hibernate.annotations.GenericGenerator; -import org.hibernate.boot.internal.SessionFactoryBuilderImpl; -import org.hibernate.boot.internal.SessionFactoryOptionsBuilder; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.SessionFactoryBuilderService; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -54,42 +52,30 @@ import jakarta.persistence.Version; * * @author Luis Barreiro */ -@TestForIssue( jiraKey = "HHH-10055" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-10055" ) +@DomainModel( + annotatedClasses = { + Store.class, Inventory.class, Product.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false" ), + @Setting( name = AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, value = "true" ), + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory( + // We want to test with this setting set to false explicitly, + // because another test already takes care of the default. + applyCollectionsInDefaultFetchGroup = false +) +@BytecodeEnhanced +public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Store.class, Inventory.class, Product.class}; - } - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, false ); - configuration.setProperty( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, true ); - configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, true ); - } - - @Override - protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) { - serviceRegistryBuilder.addService( - SessionFactoryBuilderService.class, - (SessionFactoryBuilderService) (metadata, bootstrapContext) -> { - SessionFactoryOptionsBuilder optionsBuilder = new SessionFactoryOptionsBuilder( - metadata.getMetadataBuildingOptions().getServiceRegistry(), - bootstrapContext - ); - // We want to test with this setting set to false explicitly, - // because another test already takes care of the default. - optionsBuilder.enableCollectionInDefaultFetchGroup( false ); - return new SessionFactoryBuilderImpl( metadata, optionsBuilder, bootstrapContext ); - } - ); - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { Store store = new Store( 1L ).setName( "Acme Super Outlet" ); s.persist( store ); @@ -101,41 +87,41 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base } @Test - public void testClosedSession() { - sessionFactory().getStatistics().clear(); + public void testClosedSession(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); Store[] store = new Store[1]; - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // first load the store, making sure it is not initialized store[0] = s.load( Store.class, 1L ); assertNotNull( store[0] ); assertFalse( isPropertyInitialized( store[0], "inventories" ) ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 0, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); store[0].getInventories(); assertTrue( isPropertyInitialized( store[0], "inventories" ) ); - assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } @Test - public void testClearedSession() { - sessionFactory().getStatistics().clear(); + public void testClearedSession(SessionFactoryScope scope) { + scope.getSessionFactory().getStatistics().clear(); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { // first load the store, making sure collection is not initialized Store store = s.get( Store.class, 1L ); assertNotNull( store ); assertFalse( isPropertyInitialized( store, "inventories" ) ); - assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 1, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 0, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // then clear session and try to initialize collection s.clear(); @@ -146,15 +132,15 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base // the extra Sessions are the temp Sessions needed to perform the init: // first the entity, then the collection (since it's lazy) - assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 3, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // clear Session again. The collection should still be recognized as initialized from above s.clear(); assertNotNull( store ); assertTrue( isPropertyInitialized( store, "inventories" ) ); - assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 3, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // lets clear the Session again and this time reload the Store s.clear(); @@ -164,28 +150,28 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base // collection should be back to uninitialized since we have a new entity instance assertFalse( isPropertyInitialized( store, "inventories" ) ); - assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 3, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 2, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); store.getInventories().size(); assertTrue( isPropertyInitialized( store, "inventories" ) ); // the extra Sessions are the temp Sessions needed to perform the init: // first the entity, then the collection (since it's lazy) - assertEquals( 5, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 4, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 5, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 4, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); // clear Session again. The collection should still be recognized as initialized from above s.clear(); assertNotNull( store ); assertTrue( isPropertyInitialized( store, "inventories" ) ); - assertEquals( 5, sessionFactory().getStatistics().getSessionOpenCount() ); - assertEquals( 4, sessionFactory().getStatistics().getSessionCloseCount() ); + assertEquals( 5, scope.getSessionFactory().getStatistics().getSessionOpenCount() ); + assertEquals( 4, scope.getSessionFactory().getStatistics().getSessionCloseCount() ); } ); } - @After - public void cleanup() throws Exception { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) throws Exception { + scope.inTransaction( s -> { Store store = s.find( Store.class, 1L ); s.delete( store ); @@ -198,7 +184,7 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base @Entity @Table( name = "STORE" ) - private static class Store { + static class Store { @Id Long id; @@ -235,7 +221,7 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base @Entity @Table( name = "INVENTORY" ) - private static class Inventory { + static class Inventory { @Id @GeneratedValue @@ -285,7 +271,7 @@ public class OnDemandLoadWithCollectionInDefaultFetchGroupFalseTest extends Base @Entity @Table( name = "PRODUCT" ) - private static class Product { + static class Product { @Id String id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoad2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoad2Test.java index 033c34f457..a1b45cfe99 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoad2Test.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoad2Test.java @@ -6,14 +6,15 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -23,31 +24,30 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + EagerOneToManyPersistAndLoad2Test.Parent.class, + EagerOneToManyPersistAndLoad2Test.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { +public class EagerOneToManyPersistAndLoad2Test { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -56,15 +56,15 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -87,15 +87,15 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -117,8 +117,8 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -128,7 +128,7 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -150,8 +150,8 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -161,7 +161,7 @@ public class EagerOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCas } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); List parents = session.createQuery( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoadTest.java index c0c31f5243..1b0a276ea0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerOneToManyPersistAndLoadTest.java @@ -6,14 +6,15 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -23,31 +24,30 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + EagerOneToManyPersistAndLoadTest.Parent.class, + EagerOneToManyPersistAndLoadTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { +public class EagerOneToManyPersistAndLoadTest { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -56,8 +56,8 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistLoad() { - inTransaction( + public void testEmptyCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); @@ -76,15 +76,15 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -108,15 +108,15 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -140,8 +140,8 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistLoad() { - inTransaction( + public void testCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -161,8 +161,8 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -172,7 +172,7 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -195,8 +195,8 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -206,7 +206,7 @@ public class EagerOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoad2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoad2Test.java index c85ebeee1f..1ee6b30ffa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoad2Test.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoad2Test.java @@ -8,14 +8,15 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -25,31 +26,30 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + EagerSubSelectOneToManyPersistAndLoad2Test.Parent.class, + EagerSubSelectOneToManyPersistAndLoad2Test.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { +public class EagerSubSelectOneToManyPersistAndLoad2Test { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -58,15 +58,15 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -89,15 +89,15 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -119,8 +119,8 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -130,7 +130,7 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -152,8 +152,8 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -163,7 +163,7 @@ public class EagerSubSelectOneToManyPersistAndLoad2Test extends BaseCoreFunction } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); List parents = session.createQuery( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoadTest.java index bc2bc7aedf..a9a254dbde 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/EagerSubSelectOneToManyPersistAndLoadTest.java @@ -8,14 +8,15 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -25,31 +26,30 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + EagerSubSelectOneToManyPersistAndLoadTest.Parent.class, + EagerSubSelectOneToManyPersistAndLoadTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { +public class EagerSubSelectOneToManyPersistAndLoadTest { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -58,8 +58,8 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testEmptyCollectionPersistLoad() { - inTransaction( + public void testEmptyCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); @@ -78,15 +78,15 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -110,15 +110,15 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -142,8 +142,8 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testCollectionPersistLoad() { - inTransaction( + public void testCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -163,8 +163,8 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -174,7 +174,7 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -197,8 +197,8 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -208,7 +208,7 @@ public class EagerSubSelectOneToManyPersistAndLoadTest extends BaseCoreFunctiona } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoad2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoad2Test.java index 08f62f59b2..890d62b5c7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoad2Test.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoad2Test.java @@ -6,14 +6,15 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -22,32 +23,31 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + LazyOneToManyPersistAndLoad2Test.Parent.class, + LazyOneToManyPersistAndLoad2Test.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { +public class LazyOneToManyPersistAndLoad2Test { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -56,15 +56,15 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -86,15 +86,15 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -116,8 +116,8 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -127,7 +127,7 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -149,8 +149,8 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -160,7 +160,7 @@ public class LazyOneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoadTest.java index 5ad3375ddf..bda31d38ef 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/LazyOneToManyPersistAndLoadTest.java @@ -6,14 +6,15 @@ import java.util.List; import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -22,32 +23,31 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + LazyOneToManyPersistAndLoadTest.Parent.class, + LazyOneToManyPersistAndLoadTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking NoDirtyCheckEnhancementContext.class, // supports laziness; does not support dirty-checking, DefaultEnhancementContext.class }) -public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { +public class LazyOneToManyPersistAndLoadTest { public static final String CHILD_NAME = "Luigi"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from Child" ).executeUpdate(); session.createMutationQuery( "delete from Parent" ).executeUpdate(); @@ -56,8 +56,8 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistLoad() { - inTransaction( + public void testEmptyCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); @@ -76,15 +76,15 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); @@ -108,15 +108,15 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); @@ -138,8 +138,8 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistLoad() { - inTransaction( + public void testCollectionPersistLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -158,8 +158,8 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -169,7 +169,7 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); assertFalse( Hibernate.isInitialized( p.getChildren() ) ); @@ -192,8 +192,8 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); Child c = new Child( CHILD_NAME ); @@ -203,7 +203,7 @@ public class LazyOneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List children = p.getChildren(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoad2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoad2Test.java index 53b1c018cf..5efdb36451 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoad2Test.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoad2Test.java @@ -8,14 +8,15 @@ import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -25,16 +26,23 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinTable; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + OneToManyPersistAndLoad2Test.Parent.class, + OneToManyPersistAndLoad2Test.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking DefaultEnhancementContext.class }) -public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { +public class OneToManyPersistAndLoad2Test { public static final String CHILD_NAME = "Luigi"; public static final String CHILD_NAME_2 = "Fab1"; @@ -48,17 +56,9 @@ public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { public static final String CHILD_NAME_8 = "Fab7"; public static final String CHILD_NAME_9 = "Fab8"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = session.get( Parent.class, 1l ); session.delete( parent ); @@ -67,15 +67,15 @@ public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -104,15 +104,15 @@ public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -141,14 +141,14 @@ public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { populateParentWithChildren( session ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); @@ -177,14 +177,14 @@ public class OneToManyPersistAndLoad2Test extends BaseCoreFunctionalTestCase { } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { populateParentWithChildren( session ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.getReference( Parent.class, 1l ); List parents = session.createQuery( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoadTest.java index f10661ff83..e03f688bec 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OneToManyPersistAndLoadTest.java @@ -8,14 +8,15 @@ import org.hibernate.Hibernate; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -25,16 +26,23 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinTable; import jakarta.persistence.OneToMany; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-16334") -@RunWith(BytecodeEnhancerRunner.class) +@JiraKey("HHH-16334") +@DomainModel( + annotatedClasses = { + OneToManyPersistAndLoadTest.Parent.class, + OneToManyPersistAndLoadTest.Child.class + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking DefaultEnhancementContext.class }) -public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { +public class OneToManyPersistAndLoadTest { public static final String CHILD_NAME = "Luigi"; public static final String CHILD_NAME_2 = "Fab1"; @@ -48,17 +56,9 @@ public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { public static final String CHILD_NAME_8 = "Fab7"; public static final String CHILD_NAME_9 = "Fab8"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Parent.class, - Child.class - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent parent = session.get( Parent.class, 1l ); session.delete( parent ); @@ -67,15 +67,15 @@ public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { } @Test - public void testEmptyCollectionPersistQueryJoinFetch() { - inTransaction( + public void testEmptyCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); @@ -104,15 +104,15 @@ public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { } @Test - public void testEmptyCollectionPersistQuery() { - inTransaction( + public void testEmptyCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { Parent p = new Parent( 1l ); session.persist( p ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); @@ -141,14 +141,14 @@ public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { } @Test - public void testCollectionPersistQueryJoinFetch() { - inTransaction( + public void testCollectionPersistQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { populateParentWithChildren( session ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); @@ -177,14 +177,14 @@ public class OneToManyPersistAndLoadTest extends BaseCoreFunctionalTestCase { } @Test - public void testCollectionPersistQuery() { - inTransaction( + public void testCollectionPersistQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { populateParentWithChildren( session ); } ); - inTransaction( + scope.inTransaction( session -> { Parent p = session.get( Parent.class, 1l ); List parents = session.createQuery( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanRemovalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanRemovalTest.java index 79356ce1d9..2c6b3dab09 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanRemovalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanRemovalTest.java @@ -6,12 +6,13 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.LazyToOne; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -25,26 +26,24 @@ import static jakarta.persistence.CascadeType.ALL; import static jakarta.persistence.FetchType.LAZY; import static org.hibernate.annotations.FetchMode.SELECT; import static org.hibernate.annotations.LazyToOneOption.NO_PROXY; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNull; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OrphanRemovalTest.Entity1.class, + OrphanRemovalTest.Entity2.class + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-16756") -public class OrphanRemovalTest extends BaseCoreFunctionalTestCase { +public class OrphanRemovalTest { static final int ENTITY_ID = 1; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Entity1.class, - Entity2.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { Entity1 e1 = new Entity1( ENTITY_ID ); Entity2 e2 = new Entity2(); @@ -61,8 +60,8 @@ public class OrphanRemovalTest extends BaseCoreFunctionalTestCase { @Test - public void testRemovingChild() { - inTransaction( + public void testRemovingChild(SessionFactoryScope scope) { + scope.inTransaction( session -> { Entity1 e1 = session.byId( Entity1.class ).load( ENTITY_ID ); e1.setChild( null ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanTest.java index b95026664e..383a8d71e8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/orphan/OrphanTest.java @@ -13,14 +13,15 @@ import org.hibernate.internal.util.SerializationHelper; import org.hibernate.orm.test.orphan.Part; import org.hibernate.orm.test.orphan.Product; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -29,23 +30,22 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Gavin King */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/orphan/Product.hbm.xml" + } +) +@SessionFactory +@BytecodeEnhanced @CustomEnhancementContext({ EnhancerTestContext.class, // supports laziness and dirty-checking DefaultEnhancementContext.class }) -public class OrphanTest extends BaseCoreFunctionalTestCase { +public class OrphanTest { - @Override - protected String[] getOrmXmlFiles() { - return new String[]{ - "org/hibernate/orm/test/orphan/Product.hbm.xml" - }; - } - - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createQuery( "delete from Part" ).executeUpdate(); session.createQuery( "delete from Product" ).executeUpdate(); @@ -55,8 +55,8 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteOnDelete() { - inTransaction( + public void testOrphanDeleteOnDelete(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product(); prod.setName( "Widget" ); @@ -78,7 +78,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNull( session.get( Part.class, "Get" ) ); @@ -89,8 +89,8 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteAfterPersist() { - inTransaction( + public void testOrphanDeleteAfterPersist(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product(); prod.setName( "Widget" ); @@ -108,7 +108,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -119,8 +119,8 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteAfterPersistAndFlush() { - inTransaction( + public void testOrphanDeleteAfterPersistAndFlush(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product(); prod.setName( "Widget" ); @@ -139,7 +139,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -150,10 +150,10 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteAfterLock() { + public void testOrphanDeleteAfterLock(SessionFactoryScope scope) { Product prod = new Product(); Part part = new Part(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); part.setName( "Widge" ); @@ -168,14 +168,14 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { ); - inTransaction( + scope.inTransaction( session -> { session.lock( prod, LockMode.READ ); prod.getParts().remove( part ); } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -186,10 +186,10 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteOnSaveOrUpdate() { + public void testOrphanDeleteOnSaveOrUpdate(SessionFactoryScope scope) { Product prod = new Product(); Part part = new Part(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); part.setName( "Widge" ); @@ -205,12 +205,12 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { prod.getParts().remove( part ); - inTransaction( + scope.inTransaction( session -> session.saveOrUpdate( prod ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -221,10 +221,10 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() { + public void testOrphanDeleteOnSaveOrUpdateAfterSerialization(SessionFactoryScope scope) { Product prod = new Product(); Part part = new Part(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); part.setName( "Widge" ); @@ -242,12 +242,12 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { Product cloned = (Product) SerializationHelper.clone( prod ); - inTransaction( + scope.inTransaction( session -> session.saveOrUpdate( cloned ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -258,8 +258,8 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDelete() { - inTransaction( + public void testOrphanDelete(SessionFactoryScope scope) { + scope.inTransaction( session -> { Product prod = new Product(); prod.setName( "Widget" ); @@ -275,11 +275,11 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - sessionFactory().getCache().evictEntityData( Product.class ); - sessionFactory().getCache().evictEntityData( Part.class ); + scope.getSessionFactory().getCache().evictEntityData( Product.class ); + scope.getSessionFactory().getCache().evictEntityData( Part.class ); - inTransaction( + scope.inTransaction( session -> { Product prod = session.get( Product.class, "Widget" ); assertTrue( Hibernate.isInitialized( prod.getParts() ) ); @@ -288,7 +288,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -299,11 +299,11 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteOnMerge() { + public void testOrphanDeleteOnMerge(SessionFactoryScope scope) { Product prod = new Product(); Part part = new Part(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); part.setName( "Widge" ); @@ -319,12 +319,12 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { prod.getParts().remove( part ); - inTransaction( + scope.inTransaction( session -> session.merge( prod ) ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); assertNotNull( session.get( Part.class, "Get" ) ); @@ -335,10 +335,10 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - public void testOrphanDeleteOnMergeRemoveElementMerge() { + public void testOrphanDeleteOnMergeRemoveElementMerge(SessionFactoryScope scope) { Product prod = new Product(); Part part = new Part(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); part.setName( "Widge" ); @@ -348,7 +348,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { session.merge( prod ); prod.getParts().remove( part ); @@ -356,7 +356,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNull( session.get( Part.class, "Widge" ) ); session.delete( session.get( Product.class, "Widget" ) ); @@ -366,10 +366,10 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings("unchecked") - @TestForIssue(jiraKey = "HHH-9171") - public void testOrphanDeleteOnAddElementMergeRemoveElementMerge() { + @JiraKey("HHH-9171") + public void testOrphanDeleteOnAddElementMergeRemoveElementMerge(SessionFactoryScope scope) { Product prod = new Product(); - inTransaction( + scope.inTransaction( session -> { prod.setName( "Widget" ); session.persist( prod ); @@ -381,7 +381,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { part.setDescription( "part if a Widget" ); prod.getParts().add( part ); - inTransaction( + scope.inTransaction( session -> { session.merge( prod ); // In Section 2.9, Entity Relationships, the JPA 2.1 spec says: @@ -394,7 +394,7 @@ public class OrphanTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { assertNotNull( session.get( Part.class, "Widge" ) ); session.delete( session.get( Product.class, "Widget" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/otherentityentrycontext/OtherEntityEntryContextTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/otherentityentrycontext/OtherEntityEntryContextTest.java index f9febc2c72..c1742897b5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/otherentityentrycontext/OtherEntityEntryContextTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/otherentityentrycontext/OtherEntityEntryContextTest.java @@ -8,48 +8,50 @@ package org.hibernate.orm.test.bytecode.enhancement.otherentityentrycontext; import org.hibernate.HibernateException; import org.hibernate.engine.spi.ManagedEntity; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * This task tests ManagedEntity objects that are already associated with a different PersistenceContext. * * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class OtherEntityEntryContextTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + OtherEntityEntryContextTest.Parent.class + } +) +@SessionFactory +@BytecodeEnhanced +public class OtherEntityEntryContextTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{Parent.class}; - } - - @Before - public void prepare() { + @BeforeEach + public void prepare(SessionFactoryScope scope) { // Create a Parent - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { s.persist( new Parent( 1L, "first" ) ); } ); } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { Parent p = s.get( Parent.class, 1L ); assertTrue( ManagedEntity.class.isInstance( p ) ); p.name = "second"; @@ -57,7 +59,7 @@ public class OtherEntityEntryContextTest extends BaseCoreFunctionalTestCase { assertTrue( s.contains( p ) ); // open another session and evict p from the new session - doInHibernate( this::sessionFactory, session2 -> { + scope.inTransaction( session2 -> { // s2 should contains no entities assertFalse( session2.contains( p ) ); @@ -66,7 +68,7 @@ public class OtherEntityEntryContextTest extends BaseCoreFunctionalTestCase { session2.evict( p ); assertFalse( session2.contains( p ) ); - assertNull( ( (SharedSessionContractImplementor) session2 ).getPersistenceContext().getEntry( p ) ); + assertNull( session2.getPersistenceContext().getEntry( p ) ); try { session2.update( p ); @@ -78,7 +80,7 @@ public class OtherEntityEntryContextTest extends BaseCoreFunctionalTestCase { } ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { Parent p = s.get( Parent.class, 1L ); p.name = "third"; @@ -97,7 +99,7 @@ public class OtherEntityEntryContextTest extends BaseCoreFunctionalTestCase { @Entity @Table( name = "PARENT" ) - private static class Parent { + static class Parent { @Id Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/pk/EmbeddedPKTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/pk/EmbeddedPKTest.java index 7382f5bb79..2e3052d0f3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/pk/EmbeddedPKTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/pk/EmbeddedPKTest.java @@ -6,12 +6,11 @@ */ package org.hibernate.orm.test.bytecode.enhancement.pk; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.testing.transaction.TransactionUtil; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -26,17 +25,18 @@ import java.util.Calendar; /** * @author Gail Badner */ -@RunWith( BytecodeEnhancerRunner.class ) -public class EmbeddedPKTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{WorkOrder.class, WorkOrderPK.class}; - } +@DomainModel( + annotatedClasses = { + EmbeddedPKTest.WorkOrder.class, EmbeddedPKTest.WorkOrderPK.class + } +) +@SessionFactory +@BytecodeEnhanced +public class EmbeddedPKTest { @Test - public void test() { - TransactionUtil.doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { s.persist( new WorkOrder() ); } ); } @@ -46,7 +46,7 @@ public class EmbeddedPKTest extends BaseCoreFunctionalTestCase { @Entity @IdClass( WorkOrderPK.class ) @Table( name = "WORK_ORDER" ) - private static class WorkOrder { + static class WorkOrder { @Id long id; @@ -184,7 +184,7 @@ public class EmbeddedPKTest extends BaseCoreFunctionalTestCase { } } - private static class WorkOrderPK implements Serializable { + static class WorkOrderPK implements Serializable { long id; long location; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/MergeAndRefreshTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/MergeAndRefreshTest.java index 32b5a3692c..319032ffe0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/MergeAndRefreshTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/MergeAndRefreshTest.java @@ -5,11 +5,13 @@ import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; + +import org.junit.jupiter.api.Test; import jakarta.persistence.Cacheable; import jakarta.persistence.CascadeType; @@ -20,22 +22,21 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.OneToOne; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + MergeAndRefreshTest.Phase.class, + MergeAndRefreshTest.PhaseDescription.class + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-17668") -public class MergeAndRefreshTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Phase.class, - PhaseDescription.class - }; - } +public class MergeAndRefreshTest { @Test - public void testRefresh() { + public void testRefresh(SessionFactoryScope scope) { Long phaseId = 1L; - inTransaction( + scope.inTransaction( session -> { PhaseDescription description = new PhaseDescription("phase 1"); Phase phase = new Phase( phaseId, description ); @@ -43,13 +44,9 @@ public class MergeAndRefreshTest extends BaseCoreFunctionalTestCase { } ); - Phase phase = fromTransaction( - session -> { - return session.find( Phase.class, phaseId ); - } - ); + Phase phase = scope.fromTransaction( session -> session.find( Phase.class, phaseId ) ); - inTransaction( + scope.inTransaction( session -> { Phase merged = session.merge( phase ); session.refresh( merged ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/RefreshTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/RefreshTest.java index c67437a740..dd48f76d32 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/RefreshTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/refresh/RefreshTest.java @@ -6,13 +6,13 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.Set; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @@ -27,24 +27,23 @@ import jakarta.persistence.Table; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-16423") -public class RefreshTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + RefreshTest.RealmEntity.class, + RefreshTest.RealmAttributeEntity.class, + RefreshTest.ComponentEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced +@JiraKey("HHH-16423") +public class RefreshTest { private static final String REALM_ID = "id"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - RealmEntity.class, - RealmAttributeEntity.class, - ComponentEntity.class, - }; - } - - @After - public void trearDown() { - inTransaction( + @AfterEach + public void trearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { RealmEntity find = session.find( RealmEntity.class, "id" ); if(find != null) { @@ -55,8 +54,8 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh() { - inTransaction( + public void testRefresh(SessionFactoryScope scope) { + scope.inTransaction( session -> { RealmEntity realm = new RealmEntity(); realm.setId( REALM_ID ); @@ -65,7 +64,7 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { RealmEntity realm = session.find( RealmEntity.class, REALM_ID ); @@ -79,8 +78,8 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh2() { - inTransaction( + public void testRefresh2(SessionFactoryScope scope) { + scope.inTransaction( session -> { RealmEntity realm = new RealmEntity(); realm.setId( "id" ); @@ -100,7 +99,7 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { RealmEntity realm = session.find( RealmEntity.class, REALM_ID ); @@ -115,8 +114,8 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh3() { - inTransaction( + public void testRefresh3(SessionFactoryScope scope) { + scope.inTransaction( session -> { RealmEntity realm = new RealmEntity(); realm.setId( "id" ); @@ -134,7 +133,7 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { RealmEntity realm = session.find( RealmEntity.class, REALM_ID ); @@ -149,8 +148,8 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } @Test - public void testRefresh4() { - inTransaction( + public void testRefresh4(SessionFactoryScope scope) { + scope.inTransaction( session -> { RealmEntity realm = new RealmEntity(); realm.setId( "id" ); @@ -178,7 +177,7 @@ public class RefreshTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( session -> { RealmEntity realm = session.find( RealmEntity.class, REALM_ID ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/secondarytables/SecondaryTableDynamicUpateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/secondarytables/SecondaryTableDynamicUpateTest.java index 0ef090965e..65f1567974 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/secondarytables/SecondaryTableDynamicUpateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/secondarytables/SecondaryTableDynamicUpateTest.java @@ -2,12 +2,13 @@ package org.hibernate.orm.test.bytecode.enhancement.secondarytables; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -18,24 +19,23 @@ import jakarta.persistence.SecondaryTable; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @JiraKey("HHH-17587") -@RunWith(BytecodeEnhancerRunner.class) -public class SecondaryTableDynamicUpateTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + SecondaryTableDynamicUpateTest.TestEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class SecondaryTableDynamicUpateTest { private static final Long ENTITY_ID = 123l; private static final String COL_VALUE = "col"; private static final String COL1_VALUE = "col1"; private static final String COL2_VALUE = "col2"; - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - TestEntity.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { TestEntity testEntity = new TestEntity( ENTITY_ID, COL_VALUE, COL1_VALUE, COL2_VALUE ); entityManager.persist( testEntity ); @@ -44,8 +44,8 @@ public class SecondaryTableDynamicUpateTest extends BaseCoreFunctionalTestCase { } @Test - public void testSetSecondaryTableColumnToNull() { - inTransaction( + public void testSetSecondaryTableColumnToNull(SessionFactoryScope scope) { + scope.inTransaction( entityManager -> { TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); assertThat( testEntity.getTestCol() ).isEqualTo( COL_VALUE ); @@ -55,7 +55,7 @@ public class SecondaryTableDynamicUpateTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( entityManager -> { TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); assertThat( testEntity ).isNotNull(); @@ -66,7 +66,7 @@ public class SecondaryTableDynamicUpateTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( entityManager -> { TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); assertThat( testEntity ).isNotNull(); @@ -78,7 +78,7 @@ public class SecondaryTableDynamicUpateTest extends BaseCoreFunctionalTestCase { } ); - inTransaction( + scope.inTransaction( entityManager -> { TestEntity testEntity = entityManager.find( TestEntity.class, ENTITY_ID ); assertThat( testEntity ).isNotNull(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/superclass/MappedSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/superclass/MappedSuperclassTest.java index ca18452d23..0d9fa6108b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/superclass/MappedSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/superclass/MappedSuperclassTest.java @@ -1,17 +1,17 @@ package org.hibernate.orm.test.bytecode.enhancement.superclass; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import java.time.LocalDateTime; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -19,19 +19,20 @@ import jakarta.persistence.Id; import jakarta.persistence.MappedSuperclass; @JiraKey("HHH-17418") -@RunWith(BytecodeEnhancerRunner.class) -public class MappedSuperclassTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + MappedSuperclassTest.MyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class MappedSuperclassTest { private static final LocalDateTime TEST_DATE_UPDATED_VALUE = LocalDateTime.of( 2023, 11, 10, 0, 0 ); private static final long TEST_ID = 1L; - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { MyEntity.class }; - } - - @Before - public void prepare() { - doInHibernate( this::sessionFactory, s -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( s -> { MyEntity testEntity = new MyEntity(); testEntity.id = TEST_ID; s.persist( testEntity ); @@ -39,16 +40,16 @@ public class MappedSuperclassTest extends BaseCoreFunctionalTestCase { } @Test - public void test() { - doInHibernate( this::sessionFactory, s -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( s -> { MyEntity testEntity = s.get( MyEntity.class, TEST_ID ); assertThat( testEntity.value() ).isEqualTo( TEST_DATE_UPDATED_VALUE ); } ); } - @After - public void cleanup() { - doInHibernate( this::sessionFactory, s -> { + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( s -> { MyEntity testEntity = s.get( MyEntity.class, TEST_ID ); s.remove( testEntity ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/update/JoinedInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/update/JoinedInheritanceTest.java index 5485d34346..5da3bacb3e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/update/JoinedInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/update/JoinedInheritanceTest.java @@ -2,15 +2,14 @@ package org.hibernate.orm.test.bytecode.enhancement.update; import java.util.List; -import org.hibernate.annotations.Formula; - -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Column; @@ -27,20 +26,19 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JoinedInheritanceTest.Plane.class, JoinedInheritanceTest.A320.class + } +) +@SessionFactory +@BytecodeEnhanced @JiraKey("HHH-17632") -public class JoinedInheritanceTest extends BaseNonConfigCoreFunctionalTestCase { +public class JoinedInheritanceTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Plane.class, A320.class - }; - } - - @Before - public void setUp() { - inTransaction( + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { A320 a320 = new A320( 1l, "Airbus A320", 101, true, "1.0" ); session.persist( a320 ); @@ -48,9 +46,9 @@ public class JoinedInheritanceTest extends BaseNonConfigCoreFunctionalTestCase { ); } - @After - public void tearDown() { - inTransaction( + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from A320" ).executeUpdate(); } @@ -58,8 +56,8 @@ public class JoinedInheritanceTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testUpdateField() { - inTransaction( + public void testUpdateField(SessionFactoryScope scope) { + scope.inTransaction( session -> { A320 referenceById = session.getReference( A320.class, 1L ); @@ -80,8 +78,8 @@ public class JoinedInheritanceTest extends BaseNonConfigCoreFunctionalTestCase { } @Test - public void testUpdateTwoFields() { - inTransaction( + public void testUpdateTwoFields(SessionFactoryScope scope) { + scope.inTransaction( session -> { A320 referenceById = session.getReference( A320.class, 1L ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/version/VersionedEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/version/VersionedEntityTest.java index 6c9c379eec..9a9fb69c6a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/version/VersionedEntityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/version/VersionedEntityTest.java @@ -1,12 +1,14 @@ package org.hibernate.orm.test.bytecode.enhancement.version; import org.hibernate.Hibernate; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.Serializable; import java.util.HashSet; @@ -25,31 +27,31 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Version; import static org.hibernate.Hibernate.isInitialized; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -@TestForIssue(jiraKey = "HHH-15134") -@RunWith(BytecodeEnhancerRunner.class) -public class VersionedEntityTest extends BaseCoreFunctionalTestCase { +@JiraKey("HHH-15134") +@DomainModel( + annotatedClasses = { + VersionedEntityTest.FooEntity.class, VersionedEntityTest.BarEntity.class, VersionedEntityTest.BazEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class VersionedEntityTest { private final Long parentID = 1L; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{ FooEntity.class, BarEntity.class, BazEntity.class }; - } - - @Before - public void prepare() { - doInJPA(this::sessionFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { final FooEntity entity = FooEntity.of( parentID, "foo" ); em.persist( entity ); - }); + } ); } @Test - public void testUpdateVersionedEntity() { - doInJPA(this::sessionFactory, em -> { + public void testUpdateVersionedEntity(SessionFactoryScope scope) { + scope.inTransaction( em -> { final FooEntity entity = em.getReference( FooEntity.class, parentID ); assertFalse( isInitialized( entity ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cache/EnhancedProxyCacheTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cache/EnhancedProxyCacheTest.java index ff61caee9d..565ecc4f39 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cache/EnhancedProxyCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/cache/EnhancedProxyCacheTest.java @@ -10,21 +10,24 @@ import java.util.concurrent.atomic.AtomicLong; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cache.spi.CacheImplementor; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.stat.Statistics; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; /** * Tests to verify that even when dealing with Enhanced Proxies, we will still attempt @@ -32,29 +35,29 @@ import static org.junit.Assert.assertTrue; * * @author Sanne Grinovero */ -@TestForIssue( jiraKey = "HHH-14004" ) -@RunWith(BytecodeEnhancerRunner.class) -public class EnhancedProxyCacheTest extends BaseCoreFunctionalTestCase { +@JiraKey( "HHH-14004" ) +@DomainModel( + annotatedClasses = { + Country.class, Continent.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class EnhancedProxyCacheTest { private static final AtomicLong countryId = new AtomicLong(); - @Override - protected void configure(Configuration cfg) { - super.configure( cfg ); - cfg.setProperty( Environment.GENERATE_STATISTICS, true ); - cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, true ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Country.class, Continent.class }; - } - @Test - public void testPreferenceFor2LCOverUninitializedProxy() throws Exception { - final Statistics stats = sessionFactory().getStatistics(); - storeTestData(); - clearAllCaches(); + public void testPreferenceFor2LCOverUninitializedProxy(SessionFactoryScope scope) throws Exception { + final Statistics stats = scope.getSessionFactory().getStatistics(); + storeTestData( scope ); + clearAllCaches( scope ); stats.clear(); assertTrue( stats.isStatisticsEnabled() ); assertEquals( 0, stats.getEntityFetchCount() ); @@ -62,7 +65,7 @@ public class EnhancedProxyCacheTest extends BaseCoreFunctionalTestCase { // First we load the Country once, then trigger initialization of the related Continent proxy. // 2LC is empty, so stats should show that these objects are being loaded from the DB. - inSession( s -> { + scope.inSession( s -> { Country nl = s.get( Country.class, countryId.get() ); assertNotNull( nl ); @@ -95,7 +98,7 @@ public class EnhancedProxyCacheTest extends BaseCoreFunctionalTestCase { //and we should see no needs to hit the DB. //Also, since all data is readily available we won't need to make //all attributes lazy. - inSession( s -> { + scope.inSession( s -> { assertEquals( 0, stats.getSecondLevelCacheHitCount() ); assertEquals( 0, stats.getSecondLevelCacheMissCount() ); @@ -132,15 +135,15 @@ public class EnhancedProxyCacheTest extends BaseCoreFunctionalTestCase { } - private void clearAllCaches() { - final CacheImplementor cache = sessionFactory().getCache(); + private void clearAllCaches(SessionFactoryScope scope) { + final CacheImplementor cache = scope.getSessionFactory().getCache(); for (String name : cache.getCacheRegionNames() ) { cache.getRegion( name ).clear(); } } - private void storeTestData() { - inTransaction( s -> { + private void storeTestData(SessionFactoryScope scope) { + scope.inTransaction( s -> { Continent continent = new Continent(); continent.setCode( "EU" ); continent.setName( "Europe" ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hbm/query/HbmNamedQueryConfigurationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hbm/query/HbmNamedQueryConfigurationTest.java index 9fe8dc692d..055d4eed07 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hbm/query/HbmNamedQueryConfigurationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hbm/query/HbmNamedQueryConfigurationTest.java @@ -8,40 +8,44 @@ package org.hibernate.orm.test.hbm.query; import java.util.Map; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; import org.hibernate.query.named.NamedObjectRepository; import org.hibernate.query.sqm.spi.NamedSqmQueryMemento; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) -@EnhancementOptions(inlineDirtyChecking = true, lazyLoading = true, extendedEnhancement = true) -public class HbmNamedQueryConfigurationTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected String[] getMappings() { - return new String[]{ +import org.junit.jupiter.api.Test; + +@DomainModel( + xmlMappings = { "org/hibernate/orm/test/hbm/query/HbmOverridesAnnotation.orm.xml", "org/hibernate/orm/test/hbm/query/HbmOverridesAnnotation.hbm.xml" - }; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - protected void addConfigOptions(Map options) { - options.put( "hibernate.enable_specj_proprietary_syntax", "true" ); - options.put( "hibernate.transform_hbm_xml.enabled", "true" ); - } + } +) +@ServiceRegistry( + settings = { + @Setting( name ="hibernate.enable_specj_proprietary_syntax", value = "true"), + @Setting( name ="hibernate.transform_hbm_xml.enabled", value = "true"), + } +) +@SessionFactory +@BytecodeEnhanced +@EnhancementOptions(inlineDirtyChecking = true, lazyLoading = true, extendedEnhancement = true) +public class HbmNamedQueryConfigurationTest { @Test - @TestForIssue( jiraKey = { "HHH-15619", "HHH-15620"} ) - public void testHbmOverride() { - NamedObjectRepository namedObjectRepository = entityManagerFactory() + @JiraKey("HHH-15619") + @JiraKey("HHH-15620") + public void testHbmOverride(SessionFactoryScope scope) { + NamedObjectRepository namedObjectRepository = scope.getSessionFactory() .getQueryEngine() .getNamedObjectRepository(); NamedSqmQueryMemento sqmQueryMemento = namedObjectRepository.getSqmQueryMemento( Bar.FIND_ALL ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinfetch/enhanced/JoinFetchWithEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinfetch/enhanced/JoinFetchWithEnhancementTest.java index f087a55dbd..0e49a1919b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/joinfetch/enhanced/JoinFetchWithEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinfetch/enhanced/JoinFetchWithEnhancementTest.java @@ -6,13 +6,11 @@ */ package org.hibernate.orm.test.joinfetch.enhanced; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.Map; import java.util.Set; import jakarta.persistence.Access; @@ -28,32 +26,27 @@ import org.hibernate.Hibernate; import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "HHH-12298") -@RunWith(BytecodeEnhancerRunner.class) -public class JoinFetchWithEnhancementTest extends BaseEntityManagerFunctionalTestCase { +@JiraKey("HHH-12298") +@DomainModel( + annotatedClasses = { + JoinFetchWithEnhancementTest.Employee.class, JoinFetchWithEnhancementTest.OtherEntity.class + } +) +@SessionFactory +@BytecodeEnhanced +public class JoinFetchWithEnhancementTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ Employee.class, OtherEntity.class }; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - protected void addConfigOptions(Map options) { - options.put( AvailableSettings.CLASSLOADERS, getClass().getClassLoader() ); - } - - @Before - public void prepare() { - doInJPA( this::entityManagerFactory, em -> { + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( em -> { Employee e = new Employee( "John Smith" ); OtherEntity other = new OtherEntity( "test" ); e.getOtherEntities().add( other ); @@ -64,8 +57,8 @@ public class JoinFetchWithEnhancementTest extends BaseEntityManagerFunctionalTes } @Test - public void testJoinFetchWithEnhancement() { - Employee myEmployee = doInJPA( this::entityManagerFactory, em -> { + public void testJoinFetchWithEnhancement(SessionFactoryScope scope) { + Employee myEmployee = scope.fromTransaction( em -> { Employee localEmployee = em.createQuery( "from Employee e left join fetch e.otherEntities", Employee.class ) .getResultList().get( 0 ); assertTrue( Hibernate.isPropertyInitialized( localEmployee, "otherEntities" ) ); @@ -76,7 +69,7 @@ public class JoinFetchWithEnhancementTest extends BaseEntityManagerFunctionalTes } @Entity(name = "Employee") - private static class Employee { + static class Employee { @Id private String name; @@ -118,7 +111,7 @@ public class JoinFetchWithEnhancementTest extends BaseEntityManagerFunctionalTes } @Entity(name = "OtherEntity") - private static class OtherEntity { + static class OtherEntity { @Id private String id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/PrivateConstructorEnhancerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/PrivateConstructorEnhancerTest.java index 6123b53da5..9bffc15957 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/PrivateConstructorEnhancerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/callbacks/PrivateConstructorEnhancerTest.java @@ -14,45 +14,52 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; -import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -@RunWith(BytecodeEnhancerRunner.class) -public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + PrivateConstructorEnhancerTest.Person.class, + PrivateConstructorEnhancerTest.Country.class + } +) +@SessionFactory +@BytecodeEnhanced +public class PrivateConstructorEnhancerTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - Country.class - }; - } + private Country country; + private Person person; - private Country country = new Country( "Romania" ); - private Person person = new Person( "Vlad Mihalcea", country ); - - @Override - protected void afterSessionFactoryBuilt(SessionFactoryImplementor sessionFactory) { - doInHibernate( this::sessionFactory, session -> { + @BeforeEach + protected void setup(SessionFactoryScope scope) { + scope.inTransaction( session -> { + country = new Country( "Romania" ); + person = new Person( "Vlad Mihalcea", country ); session.persist( country ); - session.persist( person ); } ); } - @Test - public void testFindEntity() { + @AfterEach + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.remove( country ); + session.remove( person ); + } ); + } - doInHibernate( this::sessionFactory, session -> { + @Test + public void testFindEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { try { Country country = session.find( Country.class, this.country.id ); @@ -66,9 +73,8 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Test - public void testGetReferenceEntity() { - - doInHibernate( this::sessionFactory, session -> { + public void testGetReferenceEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { try { Country country = session.getReference( Country.class, this.country.id ); @@ -82,9 +88,8 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Test - public void testLoadProxyAssociation() { - - doInHibernate( this::sessionFactory, session -> { + public void testLoadProxyAssociation(SessionFactoryScope scope) { + scope.inTransaction( session -> { try { Person person = session.find( Person.class, this.person.id ); @@ -98,9 +103,8 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Test - public void testListEntity() { - - doInHibernate( this::sessionFactory, session -> { + public void testListEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { try { List persons = session.createQuery( "select p from Person p" ).getResultList(); assertTrue( persons.stream().anyMatch( p -> p.getCountry().getName().equals( "Romania" ) ) ); @@ -114,9 +118,8 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Test - public void testListJoinFetchEntity() { - - doInHibernate( this::sessionFactory, session -> { + public void testListJoinFetchEntity(SessionFactoryScope scope) { + scope.inTransaction( session -> { try { List persons = session.createQuery( "select p from Person p join fetch p.country" ) .getResultList(); @@ -131,7 +134,7 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Entity(name = "Person") - private static class Person { + static class Person { @Id @GeneratedValue @@ -164,7 +167,7 @@ public class PrivateConstructorEnhancerTest extends BaseNonConfigCoreFunctionalT } @Entity(name = "Country") - private static class Country { + static class Country { @Id @GeneratedValue diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/enhancement/TestLazyPropertyOnPreUpdate.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/enhancement/TestLazyPropertyOnPreUpdate.java index e1662f0aa7..f5ac9246de 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/enhancement/TestLazyPropertyOnPreUpdate.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/enhancement/TestLazyPropertyOnPreUpdate.java @@ -6,16 +6,21 @@ */ package org.hibernate.orm.test.jpa.enhancement; +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.hibernate.Hibernate; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.transaction.TransactionUtil.JPATransactionVoidFunction; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -25,170 +30,165 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.PreUpdate; import jakarta.persistence.Table; + import java.util.Arrays; -import java.util.Map; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -@TestForIssue( jiraKey = "HHH-7573" ) -@RunWith( BytecodeEnhancerRunner.class ) -public class TestLazyPropertyOnPreUpdate extends BaseEntityManagerFunctionalTestCase { +@JiraKey("HHH-7573") +@DomainModel( + annotatedClasses = { + TestLazyPropertyOnPreUpdate.EntityWithLazyProperty.class + } +) +@SessionFactory +@BytecodeEnhanced +public class TestLazyPropertyOnPreUpdate { - private EntityWithLazyProperty entity; + private EntityWithLazyProperty entity; - @Override - public Class[] getAnnotatedClasses() { - return new Class[]{EntityWithLazyProperty.class}; - } - @Override - protected void addConfigOptions(Map options) { - options.put( AvailableSettings.CLASSLOADERS, getClass().getClassLoader() ); - } + @BeforeEach + public void prepare(SessionFactoryScope scope) throws Exception { + EntityPersister ep = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( + EntityWithLazyProperty.class.getName() ); + assertTrue( ep.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ); - @Before - public void prepare() throws Exception { - EntityPersister ep = entityManagerFactory().getMappingMetamodel().getEntityDescriptor( EntityWithLazyProperty.class.getName() ); - assertTrue( ep.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ); + byte[] testArray = new byte[] { 0x2A }; - byte[] testArray = new byte[]{0x2A}; + scope.inTransaction( em -> { + //persist the test entity.d + entity = new EntityWithLazyProperty(); + entity.setSomeField( "TEST" ); + entity.setLazyData( testArray ); + em.persist( entity ); + } ); - doInJPA( this::entityManagerFactory, em -> { - //persist the test entity.d - entity = new EntityWithLazyProperty(); - entity.setSomeField( "TEST" ); - entity.setLazyData( testArray ); - em.persist( entity ); - } ); + checkLazyField( scope, entity, testArray ); + } - checkLazyField( entity, testArray ); - } + /** + * Set a non lazy field, therefore the lazyData field will be LazyPropertyInitializer.UNFETCHED_PROPERTY + * for both state and newState so the field should not change. This should no longer cause a ClassCastException. + */ + @Test + public void testNoUpdate(SessionFactoryScope scope) { + byte[] testArray = new byte[] { 0x2A }; - /** - * Set a non lazy field, therefore the lazyData field will be LazyPropertyInitializer.UNFETCHED_PROPERTY - * for both state and newState so the field should not change. This should no longer cause a ClassCastException. - */ - @Test - public void testNoUpdate() { - byte[] testArray = new byte[]{0x2A}; + doInJPA( scope::getSessionFactory, new JPATransactionVoidFunction() { + @Override + public void accept(EntityManager em) { + entity = em.find( EntityWithLazyProperty.class, entity.id ); + entity.setSomeField( "TEST1" ); + assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + } - doInJPA( this::entityManagerFactory, new JPATransactionVoidFunction() { - @Override - public void accept(EntityManager em) { - entity = em.find( EntityWithLazyProperty.class, entity.id ); - entity.setSomeField( "TEST1" ); - assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - } + @Override + public void afterTransactionCompletion() { + assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + } + } ); - @Override - public void afterTransactionCompletion() { - assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - } - } ); + checkLazyField( scope, entity, testArray ); + } - checkLazyField( entity, testArray ); - } + /** + * Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the + * PreUpdate annotated callback method. So state == LazyPropertyInitializer.UNFETCHED_PROPERTY and + * newState == EntityWithLazyProperty.PRE_UPDATE_VALUE. This should no longer cause a ClassCastException. + */ + @Test + public void testPreUpdate(SessionFactoryScope scope) { + doInJPA( scope::getSessionFactory, new JPATransactionVoidFunction() { + @Override + public void accept(EntityManager em) { + entity = em.find( EntityWithLazyProperty.class, entity.id ); + entity.setUpdateLazyFieldInPreUpdate( true ); + entity.setSomeField( "TEST2" ); + assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + } - /** - * Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the - * PreUpdate annotated callback method. So state == LazyPropertyInitializer.UNFETCHED_PROPERTY and - * newState == EntityWithLazyProperty.PRE_UPDATE_VALUE. This should no longer cause a ClassCastException. - */ - @Test - public void testPreUpdate() { - doInJPA( this::entityManagerFactory, new JPATransactionVoidFunction() { - @Override - public void accept(EntityManager em) { - entity = em.find( EntityWithLazyProperty.class, entity.id ); - entity.setUpdateLazyFieldInPreUpdate( true ); - entity.setSomeField( "TEST2" ); - assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - } + @Override + public void afterTransactionCompletion() { + assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + } + } ); - @Override - public void afterTransactionCompletion() { - assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - } - } ); + checkLazyField( scope, entity, EntityWithLazyProperty.PRE_UPDATE_VALUE ); + } - checkLazyField( entity, EntityWithLazyProperty.PRE_UPDATE_VALUE ); - } + /** + * Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the + * PreUpdate annotated callback method and also set the lazyData field directly to testArray1. When we reload we + * should get EntityWithLazyProperty.PRE_UPDATE_VALUE. + */ + @Test + public void testPreUpdateOverride(SessionFactoryScope scope) { + byte[] testArray = new byte[] { 0x2A }; - /** - * Set the updateLazyFieldInPreUpdate flag so that the lazy field is updated from within the - * PreUpdate annotated callback method and also set the lazyData field directly to testArray1. When we reload we - * should get EntityWithLazyProperty.PRE_UPDATE_VALUE. - */ - @Test - public void testPreUpdateOverride() { - byte[] testArray = new byte[]{0x2A}; + scope.inTransaction( em -> { + entity = em.find( EntityWithLazyProperty.class, entity.id ); + entity.setUpdateLazyFieldInPreUpdate( true ); + assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + entity.setLazyData( testArray ); + assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); + entity.setSomeField( "TEST3" ); + } ); - doInJPA( this::entityManagerFactory, em -> { - entity = em.find( EntityWithLazyProperty.class, entity.id ); - entity.setUpdateLazyFieldInPreUpdate( true ); - assertFalse( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - entity.setLazyData( testArray ); - assertTrue( Hibernate.isPropertyInitialized( entity, "lazyData" ) ); - entity.setSomeField( "TEST3" ); - } ); + checkLazyField( scope, entity, EntityWithLazyProperty.PRE_UPDATE_VALUE ); + } - checkLazyField( entity, EntityWithLazyProperty.PRE_UPDATE_VALUE ); - } + private void checkLazyField(SessionFactoryScope scope, EntityWithLazyProperty entity, byte[] expected) { + // reload the entity and check the lazy value matches what we expect. + scope.inTransaction( em -> { + EntityWithLazyProperty testEntity = em.find( EntityWithLazyProperty.class, entity.id ); + assertFalse( Hibernate.isPropertyInitialized( testEntity, "lazyData" ) ); + assertTrue( Arrays.equals( expected, testEntity.lazyData ) ); + assertTrue( Hibernate.isPropertyInitialized( testEntity, "lazyData" ) ); + } ); + } - private void checkLazyField(EntityWithLazyProperty entity, byte[] expected) { - // reload the entity and check the lazy value matches what we expect. - doInJPA( this::entityManagerFactory, em -> { - EntityWithLazyProperty testEntity = em.find( EntityWithLazyProperty.class, entity.id ); - assertFalse( Hibernate.isPropertyInitialized( testEntity, "lazyData" ) ); - assertTrue( Arrays.equals( expected, testEntity.lazyData ) ); - assertTrue( Hibernate.isPropertyInitialized( testEntity, "lazyData" ) ); - } ); - } + // --- // - // --- // + /** + * Test entity with a lazy property which requires build time instrumentation. + * + * @author Martin Ball + */ + @Entity + @Table(name = "ENTITY_WITH_LAZY_PROPERTY") + static class EntityWithLazyProperty { - /** - * Test entity with a lazy property which requires build time instrumentation. - * - * @author Martin Ball - */ - @Entity - @Table( name = "ENTITY_WITH_LAZY_PROPERTY" ) - private static class EntityWithLazyProperty { + public static final byte[] PRE_UPDATE_VALUE = new byte[] { 0x2A, 0x2A, 0x2A, 0x2A }; - public static final byte[] PRE_UPDATE_VALUE = new byte[]{0x2A, 0x2A, 0x2A, 0x2A}; + @Id + @GeneratedValue + private Long id; - @Id - @GeneratedValue - private Long id; + @Basic(fetch = FetchType.LAZY) + private byte[] lazyData; - @Basic( fetch = FetchType.LAZY ) - private byte[] lazyData; + private String someField; - private String someField; + private boolean updateLazyFieldInPreUpdate; - private boolean updateLazyFieldInPreUpdate; + public void setLazyData(byte[] lazyData) { + this.lazyData = lazyData; + } - public void setLazyData(byte[] lazyData) { - this.lazyData = lazyData; - } + public void setSomeField(String someField) { + this.someField = someField; + } - public void setSomeField(String someField) { - this.someField = someField; - } + public void setUpdateLazyFieldInPreUpdate(boolean updateLazyFieldInPreUpdate) { + this.updateLazyFieldInPreUpdate = updateLazyFieldInPreUpdate; + } - public void setUpdateLazyFieldInPreUpdate(boolean updateLazyFieldInPreUpdate) { - this.updateLazyFieldInPreUpdate = updateLazyFieldInPreUpdate; - } - - @PreUpdate - public void onPreUpdate() { - //Allow the update of the lazy field from within the pre update to check that this does not break things. - if ( updateLazyFieldInPreUpdate ) { - this.lazyData = PRE_UPDATE_VALUE; - } - } - } + @PreUpdate + public void onPreUpdate() { + //Allow the update of the lazy field from within the pre update to check that this does not break things. + if ( updateLazyFieldInPreUpdate ) { + this.lazyData = PRE_UPDATE_VALUE; + } + } + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/locking/LockExistingBytecodeProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/locking/LockExistingBytecodeProxyTest.java index 1e0b01112a..42e45b78e2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/locking/LockExistingBytecodeProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/locking/LockExistingBytecodeProxyTest.java @@ -2,13 +2,14 @@ package org.hibernate.orm.test.locking; import org.hibernate.Hibernate; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.Jira; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -22,20 +23,20 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -@RunWith( BytecodeEnhancerRunner.class ) +@DomainModel( + annotatedClasses = { + LockExistingBytecodeProxyTest.MainEntity.class, + LockExistingBytecodeProxyTest.ReferencedEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced @Jira( "https://hibernate.atlassian.net/browse/HHH-17828" ) -public class LockExistingBytecodeProxyTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - MainEntity.class, - ReferencedEntity.class, - }; - } +public class LockExistingBytecodeProxyTest { @Test - public void testFindAndLockAfterFind() { - inTransaction( session -> { + public void testFindAndLockAfterFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { final MainEntity main = session.find( MainEntity.class, 1L ); assertFalse( Hibernate.isInitialized( main.referenced ) ); final ReferencedEntity lazyEntity = session.find( ReferencedEntity.class, 1L, LockModeType.PESSIMISTIC_WRITE ); @@ -46,8 +47,8 @@ public class LockExistingBytecodeProxyTest extends BaseNonConfigCoreFunctionalTe } @Test - public void testLockAfterFind() { - inTransaction( session -> { + public void testLockAfterFind(SessionFactoryScope scope) { + scope.inTransaction( session -> { final MainEntity main = session.find( MainEntity.class, 1L ); assertFalse( Hibernate.isInitialized( main.referenced ) ); session.lock( main.referenced, LockModeType.PESSIMISTIC_FORCE_INCREMENT ); @@ -56,18 +57,18 @@ public class LockExistingBytecodeProxyTest extends BaseNonConfigCoreFunctionalTe } ); } - @Before - public void setUp() { - inTransaction( session -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { final ReferencedEntity e1 = new ReferencedEntity( 1L, "referenced" ); session.persist( e1 ); session.persist( new MainEntity( 1L, e1 ) ); } ); } - @After - public void tearDown() { - inTransaction( session -> { + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { session.createMutationQuery( "delete from MainEntity" ).executeUpdate(); session.createMutationQuery( "delete from ReferencedEntity" ).executeUpdate(); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java index e183817035..406df35301 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java @@ -6,12 +6,17 @@ */ package org.hibernate.orm.test.mapping.inheritance; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.orm.test.mapping.inheritance.BaseIdEntityByteCodeTest.BaseEntity; +import static org.hibernate.orm.test.mapping.inheritance.BaseIdEntityByteCodeTest.ContainingEntity; + import org.hibernate.annotations.LazyGroup; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Embeddable; @@ -20,19 +25,18 @@ import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(BytecodeEnhancerRunner.class) -public class BaseIdEntityByteCodeTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { BaseEntity.class, ContainingEntity.class }; - } +@DomainModel( + annotatedClasses = { + BaseEntity.class, ContainingEntity.class, + } +) +@SessionFactory +@BytecodeEnhanced(runNotEnhancedAsWell = true, testEnhancedClasses = BaseEntity.class) +public class BaseIdEntityByteCodeTest { @Test - public void test() { - inTransaction( session -> { + void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { ContainingEntity entity1 = new ContainingEntity(); entity1.id = 1; entity1.baseText = "initialValue"; @@ -43,13 +47,14 @@ public class BaseIdEntityByteCodeTest extends BaseCoreFunctionalTestCase { session.persist( entity1 ); } ); - inTransaction( session -> { + scope.inTransaction( session -> { ContainingEntity entity = session.load( ContainingEntity.class, 1 ); ContainedEmbeddable containedEmbeddable = entity.getContainedEmbeddable(); assertThat( containedEmbeddable.getText() ).isEqualTo( "initialValue" ); } ); } + @Entity(name = "base") public static class BaseEntity { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedLazyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedLazyToOneTest.java index 7f309a4474..67bc8ad43a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedLazyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedLazyToOneTest.java @@ -6,40 +6,44 @@ */ package org.hibernate.orm.test.mapping.lazytoone; +import static org.assertj.core.api.Assertions.assertThat; + import org.hibernate.Hibernate; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Same as {@link LazyToOneTest} except here we have bytecode-enhanced entities - * via {@link BytecodeEnhancerRunner} + * via {@link BytecodeEnhanced} */ -@RunWith( BytecodeEnhancerRunner.class ) -public class InstrumentedLazyToOneTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + Airport.class, Flight.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced(runNotEnhancedAsWell = true) +public class InstrumentedLazyToOneTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Airport.class, Flight.class }; - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void prepareTest() throws Exception { - inTransaction( + @BeforeEach + protected void prepareTest(SessionFactoryScope scope) throws Exception { + scope.inTransaction( (session) -> { final Airport austin = new Airport( 1, "AUS" ); final Airport baltimore = new Airport( 2, "BWI" ); @@ -56,38 +60,38 @@ public class InstrumentedLazyToOneTest extends BaseNonConfigCoreFunctionalTestCa ); } - @Override - protected void cleanupTestData() throws Exception { - inTransaction( + @AfterEach + protected void cleanupTestData(SessionFactoryScope scope) throws Exception { + scope.inTransaction( (session) -> { - session.createQuery( "delete Flight" ).executeUpdate(); - session.createQuery( "delete Airport" ).executeUpdate(); + session.createMutationQuery( "delete Flight" ).executeUpdate(); + session.createMutationQuery( "delete Airport" ).executeUpdate(); } ); } @Test - @FailureExpected( jiraKey = "HHH-13658", message = "Flight#origination is not treated as lazy. Not sure why exactly" ) - public void testEnhancedButProxyNotAllowed() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + @FailureExpected( jiraKey = "HHH-13658", reason = "Flight#origination is not treated as lazy. Not sure why exactly" ) + public void testEnhancedButProxyNotAllowed(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( + scope.inTransaction( (session) -> { final Flight flight1 = session.byId( Flight.class ).load( 1 ); // unlike the other 2 tests we should get 2 db queries here - assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( statistics.getPrepareStatementCount() ).isEqualTo( 2L ); - assertThat( Hibernate.isInitialized( flight1 ), is( true ) ); + assertThat( Hibernate.isInitialized( flight1 ) ).isTrue(); - assertThat( Hibernate.isPropertyInitialized( flight1, "origination" ), is( true ) ); + assertThat( Hibernate.isPropertyInitialized( flight1, "origination" ) ).isTrue(); // this should be a non-enhanced proxy - assertThat( Hibernate.isInitialized( flight1.getOrigination() ), is( false ) ); + assertThat( Hibernate.isInitialized( flight1.getOrigination() ) ).isFalse(); - assertThat( Hibernate.isPropertyInitialized( flight1, "destination" ), is( false ) ); + assertThat( Hibernate.isPropertyInitialized( flight1, "destination" ) ).isFalse(); // the NO_PROXY here should trigger an EAGER load - assertThat( Hibernate.isInitialized( flight1.getDestination() ), is( false ) ); + assertThat( Hibernate.isInitialized( flight1.getDestination() ) ).isFalse(); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedProxyLazyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedProxyLazyToOneTest.java index ff9c9bb20a..eb874f441f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedProxyLazyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/InstrumentedProxyLazyToOneTest.java @@ -7,16 +7,20 @@ package org.hibernate.orm.test.mapping.lazytoone; import org.hibernate.Hibernate; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; @@ -26,22 +30,23 @@ import static org.hamcrest.MatcherAssert.assertThat; /** * Same as {@link InstrumentedLazyToOneTest} except here we enable bytecode-enhanced proxies */ -@RunWith( BytecodeEnhancerRunner.class ) -public class InstrumentedProxyLazyToOneTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + Airport.class, Flight.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class InstrumentedProxyLazyToOneTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Airport.class, Flight.class }; - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - @Override - protected void prepareTest() throws Exception { - inTransaction( + @BeforeEach + protected void prepareTest(SessionFactoryScope scope) throws Exception { + scope.inTransaction( (session) -> { final Airport austin = new Airport( 1, "AUS" ); final Airport baltimore = new Airport( 2, "BWI" ); @@ -58,22 +63,22 @@ public class InstrumentedProxyLazyToOneTest extends BaseNonConfigCoreFunctionalT ); } - @Override - protected void cleanupTestData() throws Exception { - inTransaction( + @AfterEach + protected void cleanupTestData(SessionFactoryScope scope) throws Exception { + scope.inTransaction( (session) -> { - session.createQuery( "delete Flight" ).executeUpdate(); - session.createQuery( "delete Airport" ).executeUpdate(); + session.createMutationQuery( "delete Flight" ).executeUpdate(); + session.createMutationQuery( "delete Airport" ).executeUpdate(); } ); } @Test - public void testEnhancedWithProxy() { - final StatisticsImplementor statistics = sessionFactory().getStatistics(); + public void testEnhancedWithProxy(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); statistics.clear(); - inTransaction( + scope.inTransaction( (session) -> { final Flight flight1 = session.byId( Flight.class ).load( 1 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/JoinFetchedManyToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/JoinFetchedManyToOneAllowProxyTests.java index 9f37c76421..2c5d0888bf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/JoinFetchedManyToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/JoinFetchedManyToOneAllowProxyTests.java @@ -14,25 +14,18 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.Fetch; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.BeforeClassOnce; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -43,38 +36,36 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.FetchMode.JOIN; import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * Test for lazy uni-directional to-one (with JOIN fetching) when enhanced proxies are allowed */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JoinFetchedManyToOneAllowProxyTests.Customer.class, + JoinFetchedManyToOneAllowProxyTests.Order.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class JoinFetchedManyToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( Order.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class JoinFetchedManyToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + SQLStatementInspector sqlStatementInterceptor = (SQLStatementInspector) scope.getStatementInspector(); + final EntityPersister orderDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata(); assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + scope.inTransaction( (session) -> { final Order order = session.byId( Order.class ).getReference( 1 ); @@ -111,9 +102,10 @@ public class JoinFetchedManyToOneAllowProxyTests extends BaseNonConfigCoreFuncti } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - Order order = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SQLStatementInspector sqlStatementInterceptor = (SQLStatementInspector) scope.getStatementInspector(); + Order order = scope.fromTransaction( (session) -> { final Order result = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) @@ -131,9 +123,10 @@ public class JoinFetchedManyToOneAllowProxyTests extends BaseNonConfigCoreFuncti assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + SQLStatementInspector sqlStatementInterceptor = (SQLStatementInspector) scope.getStatementInspector(); + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -144,9 +137,9 @@ public class JoinFetchedManyToOneAllowProxyTests extends BaseNonConfigCoreFuncti sqlStatementInterceptor.clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/LanyProxylessManyToOneTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/LanyProxylessManyToOneTests.java index e10521bdf3..1cf4d00184 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/LanyProxylessManyToOneTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/LanyProxylessManyToOneTests.java @@ -11,22 +11,18 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import java.math.BigDecimal; @@ -40,31 +36,28 @@ import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * Baseline test for uni-directional to-one, using an explicit @LazyToOne(NO_PROXY) * * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LanyProxylessManyToOneTests.Customer.class, LanyProxylessManyToOneTests.Order.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; +public class LanyProxylessManyToOneTests { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( Order.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - - @Test public void testLazyManyToOne() { - inTransaction( + @Test + public void testLazyManyToOne(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Order order = session.byId(Order.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( order, "customer"), is(false) ); @@ -80,7 +73,7 @@ public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTest assertThat( Hibernate.isInitialized(customer), is(true) ); } ); - inTransaction( + scope.inTransaction( (session) -> { final Order order = session.byId(Order.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( order, "customer"), is(false) ); @@ -99,34 +92,37 @@ public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTest } @Test - public void testOwnerIsProxy() { - final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getSessionFactory() + .getSessionFactoryOptions() + .getStatementInspector(); + final EntityPersister orderDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata(); assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + scope.inTransaction( (session) -> { final Order order = session.byId( Order.class ).getReference( 1 ); // we should have just the uninitialized proxy of the owner - and // therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db order.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( initialInterceptor, sameInstance( orderEnhancementMetadata.extractLazyInterceptor( order ) ) ); // this should trigger loading the entity's base state order.getAmount(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -135,32 +131,36 @@ public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTest // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = order.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - Order order = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getSessionFactory() + .getSessionFactoryOptions() + .getStatementInspector(); + + Order order = scope.fromTransaction( (session) -> { final Order result = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -170,12 +170,12 @@ public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTest // The "join fetch" should have already initialized the associated entity. Customer customer = order.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -183,12 +183,13 @@ public class LanyProxylessManyToOneTests extends BaseNonConfigCoreFunctionalTest session.persist( order ); } ); - sqlStatementInterceptor.clear(); + ( (SQLStatementInspector) scope.getSessionFactory().getSessionFactoryOptions() + .getStatementInspector() ).clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneAllowProxyTests.java index bbd9e0941a..22f00f01f9 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneAllowProxyTests.java @@ -13,25 +13,19 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -39,58 +33,57 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Test for lazy uni-directional to-one (with SELECT fetching) when enhanced proxies are allowed */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ManyToOneAllowProxyTests.Customer.class, + ManyToOneAllowProxyTests.Order.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class ManyToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( Order.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class ManyToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister orderDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata(); assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final Order order = session.byId( Order.class ).getReference( 1 ); // we should have just the uninitialized proxy of the owner - and // therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db order.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( initialInterceptor, sameInstance( orderEnhancementMetadata.extractLazyInterceptor( order ) ) ); // this should trigger loading the entity's base state order.getAmount(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -99,32 +92,33 @@ public class ManyToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCas // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = order.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - Order order = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + Order order = scope.fromTransaction( (session) -> { final Order result = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -134,12 +128,13 @@ public class ManyToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCas // The "join fetch" should have already initialized the associated entity. Customer customer = order.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -147,12 +142,12 @@ public class ManyToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCas session.persist( order ); } ); - sqlStatementInterceptor.clear(); + statementInspector.clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneExplicitOptionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneExplicitOptionTests.java index a31517048d..078551a4e2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneExplicitOptionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/ManyToOneExplicitOptionTests.java @@ -14,23 +14,19 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -43,31 +39,28 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.LazyToOneOption.NO_PROXY; import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + /** * Baseline test for uni-directional to-one, using an explicit @LazyToOne(NO_PROXY) * * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + ManyToOneExplicitOptionTests.Customer.class, ManyToOneExplicitOptionTests.Order.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; +public class ManyToOneExplicitOptionTests { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( Order.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - - @Test public void testLazyManyToOne() { - inTransaction( + @Test + public void testLazyManyToOne(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Order order = session.byId(Order.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( order, "customer"), is(false) ); @@ -83,7 +76,7 @@ public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTes assertThat( Hibernate.isInitialized(customer), is(true) ); } ); - inTransaction( + scope.inTransaction( (session) -> { final Order order = session.byId(Order.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( order, "customer"), is(false) ); @@ -102,34 +95,36 @@ public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTes } @Test - public void testOwnerIsProxy() { - final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister orderDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Order.class ); final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata(); assertThat( orderEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final Order order = session.byId( Order.class ).getReference( 1 ); // we should have just the uninitialized proxy of the owner - and // therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db order.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( initialInterceptor, sameInstance( orderEnhancementMetadata.extractLazyInterceptor( order ) ) ); // this should trigger loading the entity's base state order.getAmount(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = orderEnhancementMetadata.extractLazyInterceptor( order ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -138,32 +133,32 @@ public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTes // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = order.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - Order order = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + Order order = scope.fromTransaction( (session) -> { final Order result = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -173,12 +168,12 @@ public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTes // The "join fetch" should have already initialized the associated entity. Customer customer = order.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -186,12 +181,12 @@ public class ManyToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTes session.persist( order ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/collectioninitializer/InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/collectioninitializer/InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest.java index c02fd7abf4..97544fdab5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/collectioninitializer/InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/collectioninitializer/InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest.java @@ -7,19 +7,20 @@ package org.hibernate.orm.test.mapping.lazytoone.collectioninitializer; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.hibernate.loader.BatchFetchStyle.PADDED; /** * Test lazy-to-one initialization within a collection initialization, @@ -38,31 +39,29 @@ import static org.hibernate.loader.BatchFetchStyle.PADDED; * (once for UserAuthorization1, and once for UserAuthorization2) * */ -@RunWith(BytecodeEnhancerRunner.class) -@TestForIssue(jiraKey = "HHH-14730") -public class InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest - extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14730") +@DomainModel( + annotatedClasses = { + User.class, + UserAuthorization.class, + Company.class, + CostCenter.class, + Offer.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.BATCH_FETCH_STYLE, value = "PADDED" ), + @Setting( name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "10" ), + } +) +@SessionFactory +@BytecodeEnhanced +public class InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( User.class ); - sources.addAnnotatedClass( UserAuthorization.class ); - sources.addAnnotatedClass( Company.class ); - sources.addAnnotatedClass( CostCenter.class ); - sources.addAnnotatedClass( Offer.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - ssrb.applySetting( AvailableSettings.BATCH_FETCH_STYLE, PADDED ); - ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, 10 ); - } - - @Override - protected void afterSessionFactoryBuilt(SessionFactoryImplementor sessionFactory) { - inTransaction( session -> { + @BeforeEach + void afterSessionFactoryBuilt(SessionFactoryScope scope) { + scope.inTransaction( session -> { User user0 = new User(); user0.setId( 0L ); session.persist( user0 ); @@ -127,9 +126,20 @@ public class InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest } ); } + @AfterEach + void tearDown(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createMutationQuery( "delete from UserAuthorization" ).executeUpdate(); + session.createMutationQuery( "delete from Offer" ).executeUpdate(); + session.createMutationQuery( "delete from CostCenter" ).executeUpdate(); + session.createMutationQuery( "delete from User" ).executeUpdate(); + session.createMutationQuery( "delete from Company" ).executeUpdate(); + } ); + } + @Test - public void testOneReference() { - inTransaction( (session) -> { + public void testOneReference(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { // Add a lazy proxy of the cost center to the persistence context // through the lazy to-one association from the offer. Offer offer = session.find( Offer.class, 6L ); @@ -148,8 +158,8 @@ public class InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest } @Test - public void testTwoReferences() { - inTransaction( (session) -> { + public void testTwoReferences(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { // Add a lazy proxy of the cost center to the persistence context // through the lazy to-one association from the offer. Offer offer = session.find( Offer.class, 6L ); @@ -168,8 +178,8 @@ public class InitLazyToOneWithinPaddedCollectionInitializationAllowProxyTest } @Test - public void testThreeReferences() { - inTransaction( (session) -> { + public void testThreeReferences(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { // Add a lazy proxy of the cost center to the persistence context // through the lazy to-one association from the offer. Offer offer = session.find( Offer.class, 6L ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneAllowProxyTests.java index c27ddf7b6c..ebdebfbe7c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneAllowProxyTests.java @@ -12,24 +12,22 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -37,40 +35,34 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + InverseToOneAllowProxyTests.Customer.class, InverseToOneAllowProxyTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class InverseToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { // Get a reference to the SupplementalInfo we created @@ -78,14 +70,14 @@ public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTest final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // 1) we should have just the uninitialized SupplementalInfo enhanced proxy - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // (2) Access the SupplementalInfo's id value - should trigger no SQL supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( initialInterceptor, sameInstance( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ) ) ); // 3) Access SupplementalInfo's `something` state @@ -93,7 +85,7 @@ public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTest // NOTE: `customer` is not part of this lazy group because we do not know the // Customer PK from this side supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -101,27 +93,27 @@ public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTest // 4) Access SupplementalInfo's `customer` state // - should trigger load from Customer table, by FK final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -131,12 +123,12 @@ public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTest // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -144,12 +136,12 @@ public class InverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTest session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Customer" ).executeUpdate(); session.createQuery( "delete SupplementalInfo" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneExplicitOptionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneExplicitOptionTests.java index 828eb154d1..46331bd4d8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneExplicitOptionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/InverseToOneExplicitOptionTests.java @@ -13,23 +13,21 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -41,35 +39,29 @@ import static org.junit.Assert.assertTrue; /** * Baseline test for inverse (mappedBy) to-one, using an explicit @LazyToOne(NO_PROXY) */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + InverseToOneExplicitOptionTests.Customer.class, InverseToOneExplicitOptionTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class InverseToOneExplicitOptionTests { @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { // 1) Get a reference to the SupplementalInfo we created @@ -87,7 +79,7 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // we should have just the uninitialized SupplementalInfo proxy // - therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), @@ -96,7 +88,7 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // access the id - should do nothing with db supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), instanceOf( EnhancementAsProxyLazinessInterceptor.class ) @@ -104,7 +96,7 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // this should trigger loading the entity's base state supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), instanceOf( LazyAttributeLoadingInterceptor.class ) @@ -115,7 +107,7 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // // here we access customer which triggers a load from customer table final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), @@ -124,23 +116,23 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 2 ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -150,12 +142,12 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -163,12 +155,12 @@ public class InverseToOneExplicitOptionTests extends BaseNonConfigCoreFunctional session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Customer" ).executeUpdate(); session.createQuery( "delete SupplementalInfo" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/JoinFetchedInverseToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/JoinFetchedInverseToOneAllowProxyTests.java index 923bbb4a62..79a966b05e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/JoinFetchedInverseToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/mappedby/JoinFetchedInverseToOneAllowProxyTests.java @@ -13,24 +13,22 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.Fetch; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -39,40 +37,34 @@ import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.FetchMode.JOIN; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JoinFetchedInverseToOneAllowProxyTests.Customer.class, JoinFetchedInverseToOneAllowProxyTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class JoinFetchedInverseToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class JoinFetchedInverseToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { // Get a reference to the SupplementalInfo we created @@ -80,45 +72,45 @@ public class JoinFetchedInverseToOneAllowProxyTests extends BaseNonConfigCoreFun final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // 1) we should have just the uninitialized SupplementalInfo enhanced proxy - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // 2) Access the SupplementalInfo's id value - should trigger no SQL supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( initialInterceptor, sameInstance( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ) ) ); // 3) Access SupplementalInfo's `something` state // - should trigger loading the "base group" state // - customer should be join fetched as part of loading this base state supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); // 4) Access SupplementalInfo's `customer` state final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); customer.getId(); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( statementInspector.getSqlQueries().size(), is( 1 ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -128,12 +120,12 @@ public class JoinFetchedInverseToOneAllowProxyTests extends BaseNonConfigCoreFun // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -141,12 +133,12 @@ public class JoinFetchedInverseToOneAllowProxyTests extends BaseNonConfigCoreFun session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Customer" ).executeUpdate(); session.createQuery( "delete SupplementalInfo" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/JoinFetchedOneToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/JoinFetchedOneToOneAllowProxyTests.java index fd6a45b8ef..7f8f4fc667 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/JoinFetchedOneToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/JoinFetchedOneToOneAllowProxyTests.java @@ -13,96 +13,88 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.Fetch; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.FetchMode.JOIN; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JoinFetchedOneToOneAllowProxyTests.Customer.class, JoinFetchedOneToOneAllowProxyTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class JoinFetchedOneToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class JoinFetchedOneToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // we should have just the uninitialized SupplementalInfo proxy // - therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); // access the id - should do nothing with db supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); // this should trigger loading the entity's base state which should include join fetching Customer supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); // just as above, accessing id should trigger no loads customer.getId(); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -112,12 +104,12 @@ public class JoinFetchedOneToOneAllowProxyTests extends BaseNonConfigCoreFunctio // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -125,12 +117,12 @@ public class JoinFetchedOneToOneAllowProxyTests extends BaseNonConfigCoreFunctio session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete SupplementalInfo" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/LazyProxylessOneToOneTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/LazyProxylessOneToOneTests.java index f38f39230b..22a6e20ad2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/LazyProxylessOneToOneTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/LazyProxylessOneToOneTests.java @@ -11,22 +11,21 @@ import jakarta.persistence.Id; import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -36,31 +35,24 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Same as OneToOneExplicitOptionTests but using @Proxyless */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + LazyProxylessOneToOneTests.Customer.class, LazyProxylessOneToOneTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; +public class LazyProxylessOneToOneTests { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - - @Test public void testLazyOneToOne() { - inTransaction( + @Test + public void testLazyOneToOne(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( supplementalInfo, "customer"), is(false) ); @@ -76,7 +68,7 @@ public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestC assertThat( Hibernate.isInitialized(customer), is(true) ); } ); - inTransaction( + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( supplementalInfo, "customer"), is(false) ); @@ -95,34 +87,36 @@ public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestC } @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // we should have just the uninitialized SupplementalInfo proxy // - therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), sameInstance( initialInterceptor ) ); // this should trigger loading the entity's base state supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -131,32 +125,32 @@ public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestC // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -166,12 +160,12 @@ public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestC // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -179,12 +173,12 @@ public class LazyProxylessOneToOneTests extends BaseNonConfigCoreFunctionalTestC session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete SupplementalInfo" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneAllowProxyTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneAllowProxyTests.java index 1148c0419b..70fb732e86 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneAllowProxyTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneAllowProxyTests.java @@ -12,24 +12,22 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -37,58 +35,52 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OneToOneAllowProxyTests.Customer.class, OneToOneAllowProxyTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class OneToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } +public class OneToOneAllowProxyTests { @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // we should have just the uninitialized SupplementalInfo proxy // - therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), sameInstance( initialInterceptor ) ); // this should trigger loading the entity's base state supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -97,32 +89,32 @@ public class OneToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -132,12 +124,12 @@ public class OneToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -145,12 +137,12 @@ public class OneToOneAllowProxyTests extends BaseNonConfigCoreFunctionalTestCase session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete SupplementalInfo" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneExplicitOptionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneExplicitOptionTests.java index 1dc4de40e4..3cd613a8b6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneExplicitOptionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/onetoone/OneToOneExplicitOptionTests.java @@ -13,23 +13,22 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; @@ -40,31 +39,24 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.LazyToOneOption.NO_PROXY; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Baseline test for uni-directional one-to-one, using an explicit @LazyToOne(NO_PROXY) and allowing enhanced proxies */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + OneToOneExplicitOptionTests.Customer.class, OneToOneExplicitOptionTests.SupplementalInfo.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTestCase { - private SQLStatementInterceptor sqlStatementInterceptor; +public class OneToOneExplicitOptionTests { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( SupplementalInfo.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - - @Test public void testLazyOneToOne() { - inTransaction( + @Test + public void testLazyOneToOne(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( supplementalInfo, "customer"), is(false) ); @@ -80,7 +72,7 @@ public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTest assertThat( Hibernate.isInitialized(customer), is(true) ); } ); - inTransaction( + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1); assertThat( Hibernate.isPropertyInitialized( supplementalInfo, "customer"), is(false) ); @@ -99,34 +91,36 @@ public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTest } @Test - public void testOwnerIsProxy() { - final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); + public void testOwnerIsProxy(SessionFactoryScope scope) { + final EntityPersister supplementalInfoDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( SupplementalInfo.class ); final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata(); assertThat( supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); + final EntityPersister customerDescriptor = scope.getSessionFactory().getMappingMetamodel().getEntityDescriptor( Customer.class ); final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata(); assertThat( customerEnhancementMetadata.isEnhancedForLazyLoading(), is( true ) ); - inTransaction( + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + + scope.inTransaction( (session) -> { final SupplementalInfo supplementalInfo = session.byId( SupplementalInfo.class ).getReference( 1 ); // we should have just the uninitialized SupplementalInfo proxy // - therefore no SQL statements should have been executed - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // access the id - should do nothing with db supplementalInfo.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 0 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 0 ) ); assertThat( supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ), sameInstance( initialInterceptor ) ); // this should trigger loading the entity's base state supplementalInfo.getSomething(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor( supplementalInfo ); assertThat( initialInterceptor, not( sameInstance( interceptor ) ) ); assertThat( interceptor, instanceOf( LazyAttributeLoadingInterceptor.class ) ); @@ -135,32 +129,32 @@ public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTest // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy final Customer customer = supplementalInfo.getCustomer(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor( customer ); assertThat( initialCustomerInterceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) ); // just as above, accessing id should trigger no loads customer.getId(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 1 ) ); assertThat( initialCustomerInterceptor, sameInstance( customerEnhancementMetadata.extractLazyInterceptor( customer ) ) ); customer.getName(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 2 ) ); + assertThat( sqlStatementInspector.getSqlQueries().size(), is( 2 ) ); assertThat( customerEnhancementMetadata.extractLazyInterceptor( customer ), instanceOf( LazyAttributeLoadingInterceptor.class ) ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - SupplementalInfo info = fromTransaction( (session) -> { + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + SupplementalInfo info = scope.fromTransaction( (session) -> { final SupplementalInfo result = session.createQuery( "select s from SupplementalInfo s join fetch s.customer", SupplementalInfo.class ) .uniqueResult(); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); return result; } ); @@ -170,12 +164,12 @@ public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTest // The "join fetch" should have already initialized the associated entity. Customer customer = info.getCustomer(); assertTrue( Hibernate.isInitialized( customer ) ); - assertThat( sqlStatementInterceptor.getSqlQueries().size(), is( 1 ) ); + assertThat( scope.getCollectingStatementInspector().getSqlQueries().size(), is( 1 ) ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Customer customer = new Customer( 1, "Acme Brick" ); session.persist( customer ); @@ -183,12 +177,12 @@ public class OneToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTest session.persist( supplementalInfo ); } ); - sqlStatementInterceptor.clear(); + scope.getCollectingStatementInspector().clear(); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete SupplementalInfo" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/JoinFetchedPolymorphicToOneTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/JoinFetchedPolymorphicToOneTests.java index ec4b9616e5..174591da3a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/JoinFetchedPolymorphicToOneTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/JoinFetchedPolymorphicToOneTests.java @@ -16,67 +16,74 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.Fetch; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hibernate.annotations.FetchMode.JOIN; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + JoinFetchedPolymorphicToOneTests.Order.class, + JoinFetchedPolymorphicToOneTests.Customer.class, + JoinFetchedPolymorphicToOneTests.ForeignCustomer.class, + JoinFetchedPolymorphicToOneTests.DomesticCustomer.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class JoinFetchedPolymorphicToOneTests extends BaseNonConfigCoreFunctionalTestCase { +public class JoinFetchedPolymorphicToOneTests { + @Test - public void testInheritedToOneLaziness() { - inTransaction( + public void testInheritedToOneLaziness(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { - sqlStatementInterceptor.clear(); + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + sqlStatementInspector.clear(); final Order order = session.byId( Order.class ).getReference( 1 ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( "Order # " + order.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( " - amount : " + order.getAmount() ); // triggers load of base fetch state - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); final Customer customer = order.getCustomer(); // customer is part of base fetch state - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); assertTrue( Hibernate.isInitialized( customer ) ); System.out.println( " - customer : " + customer.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); customer.getName(); // customer base fetch state should also have been loaded above - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - inTransaction( + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); @@ -88,9 +95,9 @@ public class JoinFetchedPolymorphicToOneTests extends BaseNonConfigCoreFunctiona ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final DomesticCustomer customer = new DomesticCustomer( 1, "them", "123" ); session.persist( customer ); @@ -100,9 +107,9 @@ public class JoinFetchedPolymorphicToOneTests extends BaseNonConfigCoreFunctiona ); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); @@ -110,23 +117,6 @@ public class JoinFetchedPolymorphicToOneTests extends BaseNonConfigCoreFunctiona ); } - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Order.class ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( ForeignCustomer.class ); - sources.addAnnotatedClass( DomesticCustomer.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - @Entity( name = "Order" ) @Table( name = "`order`" ) public static class Order { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneExplicitOptionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneExplicitOptionTests.java index 120c068de3..6ecc1ce4f1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneExplicitOptionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneExplicitOptionTests.java @@ -17,39 +17,46 @@ import jakarta.persistence.Table; import org.hibernate.Hibernate; import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + PolymorphicToOneExplicitOptionTests.Order.class, + PolymorphicToOneExplicitOptionTests.Customer.class, + PolymorphicToOneExplicitOptionTests.ForeignCustomer.class, + PolymorphicToOneExplicitOptionTests.DomesticCustomer.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions( lazyLoading = true ) -public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFunctionalTestCase { +public class PolymorphicToOneExplicitOptionTests { @Test - public void testInheritedToOneLaziness() { - inTransaction( + public void testInheritedToOneLaziness(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { - sqlStatementInterceptor.clear(); + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + sqlStatementInspector.clear(); // NOTE : this test shows an edge case that does not work the way it // should. Because we have a polymorphic to-one, we will have to @@ -66,21 +73,21 @@ public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFuncti // is a different discussion final Order order = session.byId( Order.class ).getReference( 1 ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( "Order # " + order.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( " - amount : " + order.getAmount() ); // triggers load of base fetch state - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); final Customer customer = order.getCustomer(); // this *should* be 2 - the customer should get loaded //int expectedCount = 2; // but it is 1 because we get back a HibernateProxy int expectedCount = 1; - assertThat( sqlStatementInterceptor.getQueryCount(), is( expectedCount ) ); + sqlStatementInspector.assertExecutedCount( expectedCount ); // should be true... //assertTrue( Hibernate.isInitialized( customer ) ); // but is false @@ -91,21 +98,21 @@ public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFuncti assertThat( customer, instanceOf( HibernateProxy.class ) ); System.out.println( " - customer : " + customer.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( expectedCount ) ); + sqlStatementInspector.assertExecutedCount( expectedCount ); customer.getName(); // this should not trigger SQL because the customer ought to already be initialized // but again that is not the case expectedCount++; - assertThat( sqlStatementInterceptor.getQueryCount(), is( expectedCount ) ); + sqlStatementInspector.assertExecutedCount( expectedCount ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - inTransaction( + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); @@ -117,9 +124,9 @@ public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFuncti ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final DomesticCustomer customer = new DomesticCustomer( 1, "them", "123" ); session.persist( customer ); @@ -129,9 +136,9 @@ public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFuncti ); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); @@ -139,23 +146,6 @@ public class PolymorphicToOneExplicitOptionTests extends BaseNonConfigCoreFuncti ); } - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Order.class ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( ForeignCustomer.class ); - sources.addAnnotatedClass( DomesticCustomer.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - @Entity( name = "Order" ) @Table( name = "`order`" ) public static class Order { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneImplicitOptionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneImplicitOptionTests.java index 9082b2556a..372222a038 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneImplicitOptionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/lazytoone/polymorphic/PolymorphicToOneImplicitOptionTests.java @@ -15,71 +15,79 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import org.hibernate.Hibernate; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.proxy.HibernateProxy; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static jakarta.persistence.FetchType.LAZY; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Steve Ebersole */ -@RunWith( BytecodeEnhancerRunner.class) -@EnhancementOptions( lazyLoading = true ) -public class PolymorphicToOneImplicitOptionTests extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + PolymorphicToOneImplicitOptionTests.Order.class, + PolymorphicToOneImplicitOptionTests.Customer.class, + PolymorphicToOneImplicitOptionTests.ForeignCustomer.class, + PolymorphicToOneImplicitOptionTests.DomesticCustomer.class + } +) +@SessionFactory +@BytecodeEnhanced +@EnhancementOptions(lazyLoading = true) +public class PolymorphicToOneImplicitOptionTests { @Test - public void testInheritedToOneLaziness() { - inTransaction( + public void testInheritedToOneLaziness(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { - sqlStatementInterceptor.clear(); + SQLStatementInspector sqlStatementInspector = scope.getCollectingStatementInspector(); + sqlStatementInspector.clear(); final Order order = session.byId( Order.class ).getReference( 1 ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( "Order # " + order.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 0 ) ); + sqlStatementInspector.assertExecutedCount( 0 ); System.out.println( " - amount : " + order.getAmount() ); // triggers load of base fetch state - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); final Customer customer = order.getCustomer(); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); // customer is part of base fetch state assertFalse( Hibernate.isInitialized( customer ) ); assertThat( customer, instanceOf( HibernateProxy.class ) ); System.out.println( " - customer : " + customer.getId() ); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 1 ) ); + sqlStatementInspector.assertExecutedCount( 1 ); customer.getName(); - assertThat( sqlStatementInterceptor.getQueryCount(), is( 2 ) ); + sqlStatementInspector.assertExecutedCount( 2 ); } ); } @Test - @TestForIssue(jiraKey = "HHH-14659") - public void testQueryJoinFetch() { - inTransaction( + @JiraKey("HHH-14659") + public void testQueryJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { - final Order order = session.createQuery( "select o from Order o join fetch o.customer", Order.class ) + final Order order = session.createQuery( + "select o from Order o join fetch o.customer", Order.class ) .uniqueResult(); assertTrue( Hibernate.isPropertyInitialized( order, "customer" ) ); @@ -89,9 +97,9 @@ public class PolymorphicToOneImplicitOptionTests extends BaseNonConfigCoreFuncti ); } - @Before - public void createTestData() { - inTransaction( + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { final DomesticCustomer customer = new DomesticCustomer( 1, "them", "123" ); session.persist( customer ); @@ -101,9 +109,9 @@ public class PolymorphicToOneImplicitOptionTests extends BaseNonConfigCoreFuncti ); } - @After - public void dropTestData() { - inTransaction( + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { session.createQuery( "delete Order" ).executeUpdate(); session.createQuery( "delete Customer" ).executeUpdate(); @@ -111,30 +119,13 @@ public class PolymorphicToOneImplicitOptionTests extends BaseNonConfigCoreFuncti ); } - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Order.class ); - sources.addAnnotatedClass( Customer.class ); - sources.addAnnotatedClass( ForeignCustomer.class ); - sources.addAnnotatedClass( DomesticCustomer.class ); - } - - @Override - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { - super.configureStandardServiceRegistryBuilder( ssrb ); - sqlStatementInterceptor = new SQLStatementInterceptor( ssrb ); - } - - @Entity( name = "Order" ) - @Table( name = "`order`" ) + @Entity(name = "Order") + @Table(name = "`order`") public static class Order { @Id private Integer id; private BigDecimal amount; - @ManyToOne( fetch = LAZY, optional = false ) + @ManyToOne(fetch = LAZY, optional = false) private Customer customer; public Order() { @@ -171,8 +162,8 @@ public class PolymorphicToOneImplicitOptionTests extends BaseNonConfigCoreFuncti } } - @Entity( name = "Customer" ) - @Inheritance( strategy = InheritanceType.TABLE_PER_CLASS ) + @Entity(name = "Customer") + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public static abstract class Customer { @Id private Integer id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/property/FieldMappingWithGetterAndIsTest2.java b/hibernate-core/src/test/java/org/hibernate/orm/test/property/FieldMappingWithGetterAndIsTest2.java index 3b25a63cc9..2ade8949f0 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/property/FieldMappingWithGetterAndIsTest2.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/property/FieldMappingWithGetterAndIsTest2.java @@ -6,13 +6,12 @@ */ package org.hibernate.orm.test.property; -import org.hibernate.boot.MetadataSources; - -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Basic; import jakarta.persistence.Entity; @@ -22,18 +21,19 @@ import jakarta.persistence.Table; /** * @author Steve Ebersole */ -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + FieldMappingWithGetterAndIsTest2.Tester.class + } +) +@SessionFactory +@BytecodeEnhanced(testEnhancedClasses = FieldMappingWithGetterAndIsTest2.Tester.class) @EnhancementOptions( inlineDirtyChecking = true, lazyLoading = true ) -public class FieldMappingWithGetterAndIsTest2 extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected void applyMetadataSources(MetadataSources sources) { - super.applyMetadataSources( sources ); - sources.addAnnotatedClass( Tester.class ); - } +public class FieldMappingWithGetterAndIsTest2 { @Test - public void testResolution() { - sessionFactory(); + public void testResolution(SessionFactoryScope scope) { + scope.getSessionFactory(); } @Entity(name="Tester") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java index d48b34eb99..609de6b05a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java @@ -18,27 +18,26 @@ import org.hibernate.cfg.Environment; import org.hibernate.service.ServiceRegistry; import org.hibernate.testing.ServiceRegistryBuilder; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.util.ServiceRegistryUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; /** * * @author Christian Beikov */ -@TestForIssue(jiraKey = "HHH-14460") -@RunWith( BytecodeEnhancerRunner.class ) +@JiraKey("HHH-14460") +@BytecodeEnhanced public class MissingSetterWithEnhancementTest { private ServiceRegistry serviceRegistry; - @Before + @BeforeEach public void setUp() { final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); builder.applyClassLoader( getClass().getClassLoader() ); @@ -47,7 +46,7 @@ public class MissingSetterWithEnhancementTest { .build(); } - @After + @AfterEach public void tearDown() { if ( serviceRegistry != null ) { ServiceRegistryBuilder.destroy( serviceRegistry ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/serialization/CacheKeyEmbeddedIdEnanchedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/serialization/CacheKeyEmbeddedIdEnanchedTest.java index f933c51933..3d8031c757 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/serialization/CacheKeyEmbeddedIdEnanchedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/serialization/CacheKeyEmbeddedIdEnanchedTest.java @@ -5,64 +5,68 @@ import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.util.Map; import org.hibernate.Session; import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.internal.SimpleCacheKeysFactory; import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cfg.Environment; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.orm.test.serialization.entity.BuildRecord; import org.hibernate.orm.test.serialization.entity.BuildRecordId; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.cache.CachingRegionFactory; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(BytecodeEnhancerRunner.class) +@DomainModel( + annotatedClasses = { + BuildRecord.class + } +) +@ServiceRegistry( + settings = { + @Setting( name = Environment.USE_SECOND_LEVEL_CACHE, value = "true"), + @Setting( name = Environment.CACHE_REGION_FACTORY, value = "org.hibernate.testing.cache.CachingRegionFactory" ), + @Setting( name = Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, value = "transactional"), + @Setting( name = "javax.persistence.sharedCache.mode", value = "ALL"), + @Setting( name = Environment.CACHE_KEYS_FACTORY, value = "org.hibernate.cache.internal.DefaultCacheKeysFactory" ), + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true) -@TestForIssue( jiraKey = "HHH-14843") -public class CacheKeyEmbeddedIdEnanchedTest extends BaseNonConfigCoreFunctionalTestCase { +@JiraKey("HHH-14843") +public class CacheKeyEmbeddedIdEnanchedTest { - @Override - protected void addSettings(Map settings) { - settings.put( Environment.USE_SECOND_LEVEL_CACHE, "true" ); - settings.put( Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); - settings.put( Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "transactional" ); - settings.put( "javax.persistence.sharedCache.mode", "ALL" ); - settings.put( Environment.CACHE_KEYS_FACTORY, DefaultCacheKeysFactory.INSTANCE.getClass().getName() ); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { BuildRecord.class }; + @Test + public void testDefaultCacheKeysFactorySerialization(SessionFactoryScope scope) throws Exception { + testId( scope, DefaultCacheKeysFactory.INSTANCE, BuildRecord.class.getName(), new BuildRecordId( 2l ) ); } @Test - public void testDefaultCacheKeysFactorySerialization() throws Exception { - testId( DefaultCacheKeysFactory.INSTANCE, BuildRecord.class.getName(), new BuildRecordId( 2l ) ); + public void testSimpleCacheKeysFactorySerialization(SessionFactoryScope scope) throws Exception { + testId( scope, SimpleCacheKeysFactory.INSTANCE, BuildRecord.class.getName(), new BuildRecordId( 2l ) ); } - @Test - public void testSimpleCacheKeysFactorySerialization() throws Exception { - testId( SimpleCacheKeysFactory.INSTANCE, BuildRecord.class.getName(), new BuildRecordId( 2l ) ); - } - - private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception { - final EntityPersister persister = sessionFactory().getMetamodel().entityPersister( entityName ); + private void testId(SessionFactoryScope scope, CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception { + SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); + final EntityPersister persister = sessionFactory.getMetamodel().entityPersister( entityName ); final Object key = cacheKeysFactory.createEntityKey( id, persister, - sessionFactory(), + sessionFactory, null ); @@ -84,7 +88,7 @@ public class CacheKeyEmbeddedIdEnanchedTest extends BaseNonConfigCoreFunctionalT } else { // DefaultCacheKeysFactory#getEntityId will return a disassembled version - try (Session session = sessionFactory().openSession()) { + try (Session session = sessionFactory.openSession()) { idClone = persister.getIdentifierType().assemble( (Serializable) cacheKeysFactory.getEntityId( keyClone ), (SharedSessionContractImplementor) session, @@ -96,7 +100,7 @@ public class CacheKeyEmbeddedIdEnanchedTest extends BaseNonConfigCoreFunctionalT assertEquals( id.hashCode(), idClone.hashCode() ); assertEquals( id, idClone ); assertEquals( idClone, id ); - assertTrue( persister.getIdentifierType().isEqual( id, idClone, sessionFactory() ) ); - assertTrue( persister.getIdentifierType().isEqual( idClone, id, sessionFactory() ) ); + assertTrue( persister.getIdentifierType().isEqual( id, idClone, sessionFactory ) ); + assertTrue( persister.getIdentifierType().isEqual( idClone, id, sessionFactory ) ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java index 8650d59b0d..f5d3b09b44 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/LobUnfetchedPropertyTest.java @@ -6,10 +6,10 @@ */ package org.hibernate.orm.test.type; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.sql.Blob; @@ -31,30 +31,30 @@ import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.dialect.SybaseDialect; -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.testing.orm.junit.Jira; -import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SkipForDialect; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; -@TestForIssue(jiraKey = "HHH-12555") -@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class) -@RunWith(BytecodeEnhancerRunner.class) -public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ FileBlob.class, FileClob.class, FileNClob.class }; - } +@JiraKey("HHH-12555") +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsExpectedLobUsagePattern.class) +@DomainModel( + annotatedClasses = { + LobUnfetchedPropertyTest.FileBlob.class, LobUnfetchedPropertyTest.FileClob.class, LobUnfetchedPropertyTest.FileNClob.class + } +) +@SessionFactory +@BytecodeEnhanced +public class LobUnfetchedPropertyTest { @Test - public void testBlob() throws SQLException { - final int id = doInHibernate( this::sessionFactory, s -> { + public void testBlob(SessionFactoryScope scope) throws SQLException { + final int id = scope.fromTransaction( s -> { FileBlob file = new FileBlob(); file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) ); // merge transient entity @@ -62,7 +62,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { return file.getId(); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { FileBlob file = s.get( FileBlob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "blob" ) ); Blob blob = file.getBlob(); @@ -78,8 +78,8 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { } @Test - public void testClob() { - final int id = doInHibernate( this::sessionFactory, s -> { + public void testClob(SessionFactoryScope scope) throws SQLException { + final int id = scope.fromTransaction( s -> { FileClob file = new FileClob(); file.setClob( s.getLobHelper().createClob( "TEST CASE" ) ); // merge transient entity @@ -87,14 +87,14 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { return file.getId(); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { FileClob file = s.get( FileClob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) ); Clob clob = file.getClob(); try { final char[] chars = new char[(int) file.getClob().length()]; clob.getCharacterStream().read( chars ); - assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) ); + assertArrayEquals( "TEST CASE".toCharArray(), chars ); } catch (SQLException ex ) { fail( "could not determine Lob length" ); @@ -106,12 +106,12 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { } @Test - @RequiresDialectFeature(DialectChecks.SupportsNClob.class) + @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsNClob.class) @SkipForDialect( dialectClass = SybaseDialect.class, matchSubTypes = true, reason = "jConnect does not support Connection#createNClob which is ultimately used by LobHelper#createNClob" ) - public void testNClob() { - final int id = doInHibernate( this::sessionFactory, s -> { + public void testNClob(SessionFactoryScope scope) { + final int id = scope.fromTransaction( s -> { FileNClob file = new FileNClob(); file.setClob( s.getLobHelper().createNClob( "TEST CASE" ) ); // merge transient entity @@ -119,7 +119,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { return file.getId(); }); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { FileNClob file = s.get( FileNClob.class, id ); assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) ); NClob nClob = file.getClob(); @@ -127,7 +127,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase { try { final char[] chars = new char[(int) file.getClob().length()]; nClob.getCharacterStream().read( chars ); - assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) ); + assertArrayEquals( "TEST CASE".toCharArray(), chars ); } catch (SQLException ex ) { fail( "could not determine Lob length" ); diff --git a/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordAsEmbeddableEnhancementTest.java b/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordAsEmbeddableEnhancementTest.java index 336917de05..f748a12d1a 100644 --- a/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordAsEmbeddableEnhancementTest.java +++ b/hibernate-core/src/test/java17/org/hibernate/orm/test/records/RecordAsEmbeddableEnhancementTest.java @@ -10,12 +10,10 @@ package org.hibernate.orm.test.records; import org.hibernate.engine.spi.ManagedComposite; import org.hibernate.engine.spi.ManagedEntity; -import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -29,30 +27,29 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @JiraKey( "HHH-15072" ) -@RunWith( BytecodeEnhancerRunner.class ) +@DomainModel( + annotatedClasses = { + RecordAsEmbeddableEnhancementTest.MyEntity.class + } +) +@SessionFactory +@BytecodeEnhanced @EnhancementOptions(lazyLoading = true, extendedEnhancement = true, inlineDirtyChecking = true) -public class RecordAsEmbeddableEnhancementTest extends BaseCoreFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { MyEntity.class }; - } +public class RecordAsEmbeddableEnhancementTest { @Test - public void test() { + public void test(SessionFactoryScope scope) { // Ensure entity is enhanced, but not the record class assertTrue( ManagedEntity.class.isAssignableFrom( MyEntity.class ) ); assertFalse( ManagedComposite.class.isAssignableFrom( MyRecord.class ) ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { session.persist( new MyEntity( 1L, new MyRecord( "test", "abc" ) ) ); } ); - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { MyEntity myEntity = session.get( MyEntity.class, 1L ); assertNotNull( myEntity ); diff --git a/hibernate-testing/hibernate-testing.gradle b/hibernate-testing/hibernate-testing.gradle index 7c9d1f04f1..10d9bb6d51 100644 --- a/hibernate-testing/hibernate-testing.gradle +++ b/hibernate-testing/hibernate-testing.gradle @@ -43,6 +43,8 @@ dependencies { implementation libs.hcann implementation libs.jandex implementation testLibs.wildFlyTxnClient + implementation testLibs.junit5Engine + implementation testLibs.junit5Launcher annotationProcessor project( ':hibernate-jpamodelgen' ) } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhanced.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhanced.java new file mode 100644 index 0000000000..d5ce2703c8 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhanced.java @@ -0,0 +1,29 @@ +/* + * 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.bytecode.enhancement.extension; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(BytecodeEnhancementExtension.class) +public @interface BytecodeEnhanced { + /** + * If set to true, the test will be executed with and without bytecode enhancement within the same execution. + */ + boolean runNotEnhancedAsWell() default false; + + /** + * Entity classes will be checked whether they were enhanced or not depending on the context the test is executed in. + * Enhancement check simply verifies that the class has any methods starting with {@code $$_hibernate_} + */ + Class[] testEnhancedClasses() default {}; +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementExtension.java new file mode 100644 index 0000000000..d71ba8e4c1 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementExtension.java @@ -0,0 +1,31 @@ +/* + * 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.bytecode.enhancement.extension; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestInstanceFactoryContext; +import org.junit.jupiter.api.extension.TestInstancePreConstructCallback; +import org.junit.jupiter.api.extension.TestInstancePreDestroyCallback; + +public class BytecodeEnhancementExtension implements TestInstancePreConstructCallback, TestInstancePreDestroyCallback { + + private ClassLoader originalClassLoader; + + @Override + public void preConstructTestInstance(TestInstanceFactoryContext testInstanceFactoryContext, + ExtensionContext extensionContext) { + originalClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader( testInstanceFactoryContext.getTestClass().getClassLoader() ); + } + + @Override + public void preDestroyTestInstance(ExtensionContext extensionContext) { + Thread.currentThread().setContextClassLoader( originalClassLoader ); + } + + +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementPostDiscoveryFilter.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementPostDiscoveryFilter.java new file mode 100644 index 0000000000..abb4b96626 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/BytecodeEnhancementPostDiscoveryFilter.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.bytecode.enhancement.extension; + + +import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated; + +import org.hibernate.testing.bytecode.enhancement.extension.engine.BytecodeEnhancedEngineDescriptor; +import org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor; +import org.junit.platform.engine.FilterResult; +import org.junit.platform.engine.TestDescriptor; + +public class BytecodeEnhancementPostDiscoveryFilter implements org.junit.platform.launcher.PostDiscoveryFilter { + @Override + public FilterResult apply(TestDescriptor testDescriptor) { + if ( testDescriptor instanceof ClassBasedTestDescriptor ) { + ClassBasedTestDescriptor descriptor = (ClassBasedTestDescriptor) testDescriptor; + + TestDescriptor root = testDescriptor; + while ( !root.isRoot() ) { + root = root.getParent().get(); + } + + boolean isEnhanced = isAnnotated( descriptor.getTestClass(), BytecodeEnhanced.class ); + if ( root instanceof BytecodeEnhancedEngineDescriptor ) { + if ( !isEnhanced ) { + return FilterResult.excluded( "Not bytecode enhanced." ); + } + } + else { + if ( isEnhanced ) { + testDescriptor.removeFromHierarchy(); + return FilterResult.excluded( "Not bytecode enhanced." ); + } + } + } + return FilterResult.included( "Ok." ); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedClassUtils.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedClassUtils.java new file mode 100644 index 0000000000..78cda58e45 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedClassUtils.java @@ -0,0 +1,243 @@ +/* + * 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.bytecode.enhancement.extension.engine; + +import static org.hibernate.bytecode.internal.BytecodeProviderInitiator.buildDefaultBytecodeProvider; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; + +import org.hibernate.bytecode.enhance.spi.EnhancementContext; +import org.hibernate.bytecode.enhance.spi.Enhancer; +import org.hibernate.bytecode.enhance.spi.UnloadedClass; +import org.hibernate.bytecode.enhance.spi.UnloadedField; + +import org.hibernate.testing.bytecode.enhancement.ClassEnhancementSelector; +import org.hibernate.testing.bytecode.enhancement.ClassEnhancementSelectors; +import org.hibernate.testing.bytecode.enhancement.ClassSelector; +import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; +import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; +import org.hibernate.testing.bytecode.enhancement.EnhancementSelector; +import org.hibernate.testing.bytecode.enhancement.EnhancerTestContext; +import org.hibernate.testing.bytecode.enhancement.ImplEnhancementSelector; +import org.hibernate.testing.bytecode.enhancement.ImplEnhancementSelectors; +import org.hibernate.testing.bytecode.enhancement.PackageEnhancementSelector; +import org.hibernate.testing.bytecode.enhancement.PackageEnhancementSelectors; +import org.hibernate.testing.bytecode.enhancement.PackageSelector; + +final class BytecodeEnhancedClassUtils { + private BytecodeEnhancedClassUtils() { + } + + static Map> enhanceTestClass(Class klass) throws ClassNotFoundException { + String packageName = klass.getPackage().getName(); + Map> classes = new LinkedHashMap<>(); + + try { + if ( klass.isAnnotationPresent( EnhancementOptions.class ) + || klass.isAnnotationPresent( ClassEnhancementSelector.class ) + || klass.isAnnotationPresent( ClassEnhancementSelectors.class ) + || klass.isAnnotationPresent( PackageEnhancementSelector.class ) + || klass.isAnnotationPresent( PackageEnhancementSelectors.class ) + || klass.isAnnotationPresent( ImplEnhancementSelector.class ) + || klass.isAnnotationPresent( ImplEnhancementSelectors.class ) ) { + classes.put( "-", buildEnhancerClassLoader( klass ).loadClass( klass.getName() ) ); + } + else if ( klass.isAnnotationPresent( CustomEnhancementContext.class ) ) { + for ( Class contextClass : klass.getAnnotation( CustomEnhancementContext.class ) + .value() ) { + EnhancementContext enhancementContextInstance = contextClass.getConstructor().newInstance(); + classes.put( contextClass.getSimpleName(), + getEnhancerClassLoader( enhancementContextInstance, packageName ).loadClass( klass.getName() ) ); + } + } + else { + classes.put( "-", getEnhancerClassLoader( new EnhancerTestContext(), packageName ).loadClass( klass.getName() ) ); + } + } + catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { + // This is unlikely, but if happens throw runtime exception to fail the test + throw new RuntimeException( e ); + } + return classes; + } + + // --- // + + + private static ClassLoader buildEnhancerClassLoader(Class klass) { + final EnhancementOptions options = klass.getAnnotation( EnhancementOptions.class ); + final EnhancementContext enhancerContext; + if ( options == null ) { + enhancerContext = new EnhancerTestContext(); + } + else { + enhancerContext = new EnhancerTestContext() { + @Override + public boolean doBiDirectionalAssociationManagement(UnloadedField field) { + return options.biDirectionalAssociationManagement() && super.doBiDirectionalAssociationManagement( field ); + } + + @Override + public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) { + return options.inlineDirtyChecking() && super.doDirtyCheckingInline( classDescriptor ); + } + + @Override + public boolean doExtendedEnhancement(UnloadedClass classDescriptor) { + return options.extendedEnhancement() && super.doExtendedEnhancement( classDescriptor ); + } + + @Override + public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) { + return options.lazyLoading() && super.hasLazyLoadableAttributes( classDescriptor ); + } + + @Override + public boolean isLazyLoadable(UnloadedField field) { + return options.lazyLoading() && super.isLazyLoadable( field ); + } + }; + } + + final List selectors = new ArrayList<>(); + selectors.add( new PackageSelector( klass.getPackage().getName() ) ); + applySelectors( + klass, + ClassEnhancementSelector.class, + ClassEnhancementSelectors.class, + selectorAnnotation -> selectors.add( new ClassSelector( selectorAnnotation.value().getName() ) ) + ); + applySelectors( + klass, + PackageEnhancementSelector.class, + PackageEnhancementSelectors.class, + selectorAnnotation -> selectors.add( new PackageSelector( selectorAnnotation.value() ) ) + ); + applySelectors( + klass, + ImplEnhancementSelector.class, + ImplEnhancementSelectors.class, + selectorAnnotation -> { + try { + selectors.add( selectorAnnotation.impl().getDeclaredConstructor().newInstance() ); + } + catch (RuntimeException re) { + throw re; + } + catch (Exception e) { + throw new RuntimeException( e ); + } + } + ); + + return buildEnhancerClassLoader( enhancerContext, selectors ); + } + + private static void applySelectors( + Class klass, + Class selectorAnnotationType, + Class selectorsAnnotationType, + Consumer action) { + final A selectorAnnotation = klass.getAnnotation( selectorAnnotationType ); + final Annotation selectorsAnnotation = klass.getAnnotation( selectorsAnnotationType ); + + if ( selectorAnnotation != null ) { + action.accept( selectorAnnotation ); + } + else if ( selectorsAnnotation != null ) { + try { + final Method valuesMethod = selectorsAnnotationType.getDeclaredMethods()[0]; + @SuppressWarnings("unchecked") + final A[] selectorAnnotations = (A[]) valuesMethod.invoke( selectorsAnnotation ); + for ( A groupedSelectorAnnotation : selectorAnnotations ) { + action.accept( groupedSelectorAnnotation ); + } + + } + catch (Exception e) { + throw new RuntimeException( e ); + } + } + } + + private static ClassLoader buildEnhancerClassLoader( + EnhancementContext enhancerContext, + List selectors) { + return new EnhancingClassLoader( + buildDefaultBytecodeProvider().getEnhancer( enhancerContext ), + selectors + ); + } + + private static class EnhancingClassLoader extends ClassLoader { + private final Enhancer enhancer; + private final List selectors; + + public EnhancingClassLoader(Enhancer enhancer, List selectors) { + this.enhancer = enhancer; + this.selectors = selectors; + } + + @Override + public Class loadClass(String name) throws ClassNotFoundException { + for ( EnhancementSelector selector : selectors ) { + if ( selector.select( name ) ) { + final Class c = findLoadedClass( name ); + if ( c != null ) { + return c; + } + + try ( InputStream is = getResourceAsStream( name.replace( '.', '/' ) + ".class" ) ) { + if ( is == null ) { + throw new ClassNotFoundException( name + " not found" ); + } + + byte[] original = new byte[is.available()]; + try ( BufferedInputStream bis = new BufferedInputStream( is ) ) { + bis.read( original ); + } + + byte[] enhanced = enhancer.enhance( name, original ); + if ( enhanced == null ) { + return defineClass( name, original, 0, original.length ); + } + Path f = Files.createTempDirectory( "" ).getParent() + .resolve( name.replace( ".", File.separator ) + ".class" ); + Files.createDirectories( f.getParent() ); + try ( OutputStream out = Files.newOutputStream( f ) ) { + out.write( enhanced ); + } + return defineClass( name, enhanced, 0, enhanced.length ); + } + catch (Exception t) { + throw new ClassNotFoundException( name + " not found", t ); + } + } + } + + return getParent().loadClass( name ); + } + } + + private static ClassLoader getEnhancerClassLoader(EnhancementContext context, String packageName) { + return buildEnhancerClassLoader( context, Collections.singletonList( new PackageSelector( packageName ) ) ); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedEngineDescriptor.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedEngineDescriptor.java new file mode 100644 index 0000000000..e8e279614c --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedEngineDescriptor.java @@ -0,0 +1,17 @@ +/* + * 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.bytecode.enhancement.extension.engine; + +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor; +import org.junit.platform.engine.UniqueId; + +public class BytecodeEnhancedEngineDescriptor extends JupiterEngineDescriptor { + public BytecodeEnhancedEngineDescriptor(UniqueId uniqueId, JupiterConfiguration configuration) { + super( uniqueId, configuration ); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedTestEngine.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedTestEngine.java new file mode 100644 index 0000000000..7be2328b18 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedTestEngine.java @@ -0,0 +1,437 @@ +/* + * 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.bytecode.enhancement.extension.engine; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.testing.bytecode.enhancement.extension.engine.BytecodeEnhancedClassUtils.enhanceTestClass; +import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.ClassOrderer; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.io.CleanupMode; +import org.junit.jupiter.api.io.TempDirFactory; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.junit.jupiter.engine.config.CachingJupiterConfiguration; +import org.junit.jupiter.engine.config.DefaultJupiterConfiguration; +import org.junit.jupiter.engine.config.JupiterConfiguration; +import org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor; +import org.junit.jupiter.engine.descriptor.ClassTestDescriptor; +import org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor; +import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor; +import org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor; +import org.junit.jupiter.engine.discovery.DiscoverySelectorResolver; +import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; +import org.junit.platform.engine.EngineDiscoveryRequest; +import org.junit.platform.engine.ExecutionRequest; +import org.junit.platform.engine.TestDescriptor; +import org.junit.platform.engine.UniqueId; +import org.junit.platform.engine.support.hierarchical.EngineExecutionContext; +import org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine; +import org.junit.platform.engine.support.hierarchical.ThrowableCollector; + +public class BytecodeEnhancedTestEngine extends HierarchicalTestEngine { + + @Override + public String getId() { + return "bytecode-enhanced-engine"; + } + + @Override + public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) { + JupiterConfiguration configuration = new CachingJupiterConfiguration( + new DefaultJupiterConfiguration( discoveryRequest.getConfigurationParameters() ) ); + JupiterEngineDescriptor engineDescriptor = new BytecodeEnhancedEngineDescriptor( uniqueId, configuration ); + new DiscoverySelectorResolver().resolveSelectors( discoveryRequest, engineDescriptor ); + + for ( TestDescriptor testDescriptor : new HashSet<>( engineDescriptor.getChildren() ) ) { + if ( testDescriptor instanceof ClassBasedTestDescriptor ) { + try { + ClassBasedTestDescriptor descriptor = (ClassBasedTestDescriptor) testDescriptor; + // if the test class is annotated with @BytecodeEnhanced + // we replace the descriptor with the new one that will point to an enhanced test class, + // this also means that we need to add all the child descriptors back as well... + // Then on the extension side we set the classloader that contains the enhanced test class + // and set it back to the original once the test class is destroyed. + Optional bytecodeEnhanced = findAnnotation( + descriptor.getTestClass(), BytecodeEnhanced.class ); + if ( bytecodeEnhanced.isPresent() ) { + TestDescriptor parent = descriptor.getParent().orElseThrow( IllegalStateException::new ); + Class klass = descriptor.getTestClass(); + + JupiterConfiguration jc = ( (JupiterEngineDescriptor) parent ).getConfiguration(); + + String[] testEnhancedClasses = Arrays.stream( bytecodeEnhanced.get().testEnhancedClasses() ) + .map( Class::getName ).toArray( String[]::new ); + + // NOTE: get children before potentially removing from hierarchy, since after that there will be none. + Set children = new HashSet<>( descriptor.getChildren() ); + if ( !bytecodeEnhanced.get().runNotEnhancedAsWell() ) { + descriptor.removeFromHierarchy(); + } + + Map> classes = enhanceTestClass( klass ); + if ( classes.size() == 1 ) { + replaceWithEnhanced( classes.values().iterator().next(), descriptor, jc, children, parent, testEnhancedClasses ); + } + else { + for ( Map.Entry> entry : classes.entrySet() ) { + replaceWithEnhanced( + entry.getValue(), descriptor, jc, children, parent, testEnhancedClasses, entry.getKey() ); + } + } + + addEnhancementCheck( false, testEnhancedClasses, descriptor, jc ); + } + else { + testDescriptor.removeFromHierarchy(); + } + } + catch (ClassNotFoundException | NoSuchMethodException e) { + throw new RuntimeException( e ); + } + } + } + + return engineDescriptor; + } + + private void addEnhancementCheck(boolean enhance, String[] testEnhancedClasses, + ClassBasedTestDescriptor descriptor, JupiterConfiguration jc) { + if ( testEnhancedClasses.length > 0 ) { + descriptor.addChild( new EnhancementWorkedCheckMethodTestDescriptor( + UniqueId.forEngine( getId() ) + .append( + ClassTestDescriptor.SEGMENT_TYPE, + descriptor.getTestClass().getName() + ), + descriptor.getTestClass(), + jc, + enhance, + testEnhancedClasses + ) ); + } + } + + private void replaceWithEnhanced(Class enhanced, ClassBasedTestDescriptor descriptor, JupiterConfiguration jc, + Set children, TestDescriptor parent, String[] testEnhancedClasses) + throws NoSuchMethodException { + replaceWithEnhanced( enhanced, descriptor, jc, children, parent, testEnhancedClasses, null ); + } + + private void replaceWithEnhanced(Class enhanced, ClassBasedTestDescriptor descriptor, JupiterConfiguration jc, + Set children, TestDescriptor parent, String[] testEnhancedClasses, + Object enhancementContextId) + throws NoSuchMethodException { + DelegatingJupiterConfiguration configuration = new DelegatingJupiterConfiguration( jc, enhancementContextId ); + + ClassTestDescriptor updated = new ClassTestDescriptor( + convertUniqueId( descriptor.getUniqueId(), enhancementContextId ), + enhanced, + configuration + ); + + for ( TestDescriptor child : children ) { + // this needs more cases for parameterized tests, test templates and so on ... + // for now it'll only work with simple @Test tests + if ( child instanceof TestMethodTestDescriptor ) { + Method testMethod = ( (TestMethodTestDescriptor) child ).getTestMethod(); + updated.addChild( + new TestMethodTestDescriptor( + convertUniqueId( child.getUniqueId(), enhancementContextId ), + updated.getTestClass(), + findMethodReplacement( updated, testMethod ), + configuration + ) + ); + + } + if ( child instanceof TestTemplateTestDescriptor ) { + Method testMethod = ( (TestTemplateTestDescriptor) child ).getTestMethod(); + updated.addChild( new TestTemplateTestDescriptor( + convertUniqueId( child.getUniqueId(), enhancementContextId ), + updated.getTestClass(), + findMethodReplacement( updated, testMethod ), + configuration + ) ); + } + } + addEnhancementCheck( true, testEnhancedClasses, updated, configuration ); + parent.addChild( updated ); + } + + private UniqueId convertUniqueId(UniqueId id, Object enhancementContextId) { + UniqueId uniqueId = UniqueId.forEngine( getId() ) + .append( "Enhanced", enhancementContextId == null ? "true" : Objects.toString( enhancementContextId ) ); + + List segments = id.getSegments(); + for ( int i = 1; i < segments.size(); i++ ) { + UniqueId.Segment segment = segments.get( i ); + uniqueId = uniqueId.append( segment ); + } + return uniqueId; + } + + private Method findMethodReplacement(ClassTestDescriptor updated, Method testMethod) throws NoSuchMethodException { + String name = testMethod.getDeclaringClass().getName(); + + Class testClass = updated.getTestClass(); + while ( !testClass.getName().equals( name ) ) { + testClass = testClass.getSuperclass(); + if ( Object.class.equals( testClass ) ) { + throw new IllegalStateException( "Wasn't able to find a test method " + testMethod ); + } + } + return testClass.getDeclaredMethod( + testMethod.getName(), + testMethod.getParameterTypes() + ); + } + + @Override + protected JupiterEngineExecutionContext createExecutionContext(ExecutionRequest request) { + return new JupiterEngineExecutionContext( + request.getEngineExecutionListener(), + this.getJupiterConfiguration( request ) + ); + } + + private JupiterConfiguration getJupiterConfiguration(ExecutionRequest request) { + JupiterEngineDescriptor engineDescriptor = (JupiterEngineDescriptor) request.getRootTestDescriptor(); + return engineDescriptor.getConfiguration(); + } + + public Optional getGroupId() { + return Optional.of( "org.junit.jupiter" ); + } + + public Optional getArtifactId() { + return Optional.of( "junit-jupiter-engine" ); + } + + public static class Context implements EngineExecutionContext { + private final ExecutionRequest request; + + public Context(ExecutionRequest request) { + this.request = request; + } + } + + private static class DelegatingJupiterConfiguration implements JupiterConfiguration { + private final JupiterConfiguration configuration; + private final DelegatingDisplayNameGenerator displayNameGenerator; + + private DelegatingJupiterConfiguration(JupiterConfiguration configuration, Object id) { + this.configuration = configuration; + displayNameGenerator = new DelegatingDisplayNameGenerator( + configuration.getDefaultDisplayNameGenerator(), + id + ); + } + + @Override + public Optional getRawConfigurationParameter(String s) { + return configuration.getRawConfigurationParameter( s ); + } + + @Override + public Optional getRawConfigurationParameter(String s, Function function) { + return configuration.getRawConfigurationParameter( s, function ); + } + + @Override + public boolean isParallelExecutionEnabled() { + return configuration.isParallelExecutionEnabled(); + } + + @Override + public boolean isExtensionAutoDetectionEnabled() { + return configuration.isExtensionAutoDetectionEnabled(); + } + + @Override + public ExecutionMode getDefaultExecutionMode() { + return configuration.getDefaultExecutionMode(); + } + + @Override + public ExecutionMode getDefaultClassesExecutionMode() { + return configuration.getDefaultClassesExecutionMode(); + } + + @Override + public TestInstance.Lifecycle getDefaultTestInstanceLifecycle() { + return configuration.getDefaultTestInstanceLifecycle(); + } + + @Override + public Predicate getExecutionConditionFilter() { + return configuration.getExecutionConditionFilter(); + } + + @Override + public DisplayNameGenerator getDefaultDisplayNameGenerator() { + return displayNameGenerator; + } + + @Override + public Optional getDefaultTestMethodOrderer() { + return configuration.getDefaultTestMethodOrderer(); + } + + @Override + public Optional getDefaultTestClassOrderer() { + return configuration.getDefaultTestClassOrderer(); + } + + @Override + public CleanupMode getDefaultTempDirCleanupMode() { + return configuration.getDefaultTempDirCleanupMode(); + } + + @Override + public Supplier getDefaultTempDirFactorySupplier() { + return configuration.getDefaultTempDirFactorySupplier(); + } + } + + private static class DelegatingDisplayNameGenerator implements DisplayNameGenerator { + + private final DisplayNameGenerator delegate; + private final Object id; + + private DelegatingDisplayNameGenerator(DisplayNameGenerator delegate, Object id) { + this.delegate = delegate; + this.id = id; + } + + @Override + public String generateDisplayNameForClass(Class aClass) { + return prefix() + delegate.generateDisplayNameForClass( aClass ); + } + + private String prefix() { + return "Enhanced" + ( id == null ? "" : "[" + id + "]" ) + ":"; + } + + @Override + public String generateDisplayNameForNestedClass(Class aClass) { + return prefix() + delegate.generateDisplayNameForNestedClass( aClass ); + } + + @Override + public String generateDisplayNameForMethod(Class aClass, Method method) { + return prefix() + delegate.generateDisplayNameForMethod( aClass, method ); + } + } + + private static class EnhancementWorkedCheckMethodTestDescriptor extends TestMethodTestDescriptor { + + private final boolean enhanced; + private final String[] classes; + + public EnhancementWorkedCheckMethodTestDescriptor(UniqueId uniqueId, Class testClass, + JupiterConfiguration configuration, + boolean enhanced, String[] classes) { + super( + prepareId( uniqueId, testMethod( enhanced ) ), + testClass, testMethod( enhanced ), + configuration + ); + this.enhanced = enhanced; + this.classes = classes; + } + + private static Method testMethod(boolean enhanced) { + return enhanced ? METHOD_ENHANCED : METHOD_NOT_ENHANCED; + } + + @Override + public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, + DynamicTestExecutor dynamicTestExecutor) { + ExtensionContext extensionContext = context.getExtensionContext(); + ThrowableCollector throwableCollector = context.getThrowableCollector(); + + throwableCollector.execute( () -> { + Object instance = extensionContext.getRequiredTestInstance(); + for ( String className : classes ) { + assertEnhancementWorked( className, enhanced, instance ); + } + } ); + + return context; + } + + private static final Method METHOD_ENHANCED; + private static final Method METHOD_NOT_ENHANCED; + + static { + try { + METHOD_ENHANCED = EnhancementWorkedCheckMethodTestDescriptor.class.getDeclaredMethod( + "assertEntityClassesWereEnhanced" ); + METHOD_NOT_ENHANCED = EnhancementWorkedCheckMethodTestDescriptor.class.getDeclaredMethod( + "assertEntityClassesWereNotEnhanced" ); + } + catch (NoSuchMethodException e) { + throw new RuntimeException( e ); + } + } + + private static void assertEntityClassesWereEnhanced() { + // just for JUint to display the name + } + + private static void assertEntityClassesWereNotEnhanced() { + // just for JUint to display the name + } + + private static void assertEnhancementWorked(String className, boolean enhanced, Object testClassInstance) { + try { + Class loaded = testClassInstance.getClass().getClassLoader().loadClass( className ); + if ( enhanced ) { + assertThat( loaded.getDeclaredMethods() ) + .extracting( Method::getName ) + .anyMatch( name -> name.startsWith( "$$_hibernate_" ) ); + } + else { + assertThat( loaded.getDeclaredMethods() ) + .extracting( Method::getName ) + .noneMatch( name -> name.startsWith( "$$_hibernate_" ) ); + } + } + + catch (ClassNotFoundException e) { + Assertions.fail( e.getMessage() ); + } + } + + private static UniqueId prepareId(UniqueId uniqueId, Method method) { + return uniqueId.append( + TestMethodTestDescriptor.SEGMENT_TYPE, + method.getName() + ); + } + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectFeatureChecks.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectFeatureChecks.java index 320899ba6b..3f75a18ed3 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectFeatureChecks.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/DialectFeatureChecks.java @@ -691,4 +691,11 @@ abstract public class DialectFeatureChecks { return dialect.supportsCaseInsensitiveLike(); } } + + public static class SupportsNClob implements DialectFeatureCheck { + @Override + public boolean apply(Dialect dialect) { + return dialect.getNationalizationSupport() == NationalizationSupport.EXPLICIT; + } + } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryExtension.java index 3ea34e4a60..e56cb92587 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryExtension.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/ServiceRegistryExtension.java @@ -334,6 +334,7 @@ public class ServiceRegistryExtension private StandardServiceRegistry createRegistry() { BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder().enableAutoClose(); + bsrb.applyClassLoader( Thread.currentThread().getContextClassLoader() ); ssrProducer.prepareBootstrapRegistryBuilder(bsrb); final org.hibernate.boot.registry.BootstrapServiceRegistry bsr = bsrProducer.produceServiceRegistry( bsrb ); 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 65e2a0cfc8..76b9e3a584 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 @@ -56,4 +56,6 @@ public @interface SessionFactory { * @see SQLStatementInspector */ boolean useCollectingStatementInspector() default false; + + boolean applyCollectionsInDefaultFetchGroup() default true; } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java index 3867dc8477..629e51a198 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java @@ -147,6 +147,7 @@ public class SessionFactoryExtension else if ( ! explicitInspectorClass.equals( StatementInspector.class ) ) { sessionFactoryBuilder.applyStatementInspector( explicitInspectorClass.getConstructor().newInstance() ); } + sessionFactoryBuilder.applyCollectionsInDefaultFetchGroup( sessionFactoryConfig.applyCollectionsInDefaultFetchGroup() ); final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) sessionFactoryBuilder.build(); diff --git a/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.engine.TestEngine b/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.engine.TestEngine new file mode 100644 index 0000000000..e54cf08aec --- /dev/null +++ b/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.engine.TestEngine @@ -0,0 +1 @@ +org.hibernate.testing.bytecode.enhancement.extension.engine.BytecodeEnhancedTestEngine diff --git a/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter b/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter new file mode 100644 index 0000000000..3d0758381c --- /dev/null +++ b/hibernate-testing/src/main/resources/META-INF/services/org.junit.platform.launcher.PostDiscoveryFilter @@ -0,0 +1 @@ +org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhancementPostDiscoveryFilter diff --git a/settings.gradle b/settings.gradle index 2d3fb14b04..d17a35ccf2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -171,6 +171,7 @@ dependencyResolutionManagement { testLibs { def junit5Version = version "junit5", "5.9.2" def junit4Version = version "junit4", "4.13.2" + def junit5LauncherVersion = version "junit5Launcher", "1.10.2" def assertjVersion = version "assertj", "3.22.0" def bytemanVersion = version "byteman", "4.0.20" @@ -191,6 +192,7 @@ dependencyResolutionManagement { library( "junit5Engine", "org.junit.jupiter", "junit-jupiter-engine" ).versionRef( junit5Version ) library( "junit5Params", "org.junit.jupiter", "junit-jupiter-params" ).versionRef( junit5Version ) library( "junit4Engine", "org.junit.vintage", "junit-vintage-engine" ).versionRef( junit5Version ) + library( "junit5Launcher", "org.junit.platform", "junit-platform-launcher" ).versionRef( junit5LauncherVersion ) library( "junit4", "junit", "junit" ).versionRef( junit4Version ) library( "assertjCore", "org.assertj", "assertj-core" ).versionRef( assertjVersion )