From e26f4d25ab1ccf5711f7fbd4ad6cd41aa1844e1c Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 9 Jan 2023 12:13:41 +0100 Subject: [PATCH] HHH-15982 Refactor tests to use StatementInspector --- ...ctionalOneToOneWithConverterEagerTest.java | 38 ++++++------------ ...ectionalOneToOneWithConverterLazyTest.java | 36 +++++------------ .../BidirectionalOneToOneEagerFKTest.java | 40 ++++++------------- .../BidirectionalOneToOneLazyFKTest.java | 37 +++++------------ 4 files changed, 44 insertions(+), 107 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterEagerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterEagerTest.java index ba5a87eb4b..0efb7f767b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterEagerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterEagerTest.java @@ -8,10 +8,8 @@ package org.hibernate.orm.test.mapping.converted.converter.onetoone; import java.io.Serializable; import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; - -import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener; +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; @@ -36,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Marco Belladelli */ -@SessionFactory +@SessionFactory(useCollectingStatementInspector = true) @DomainModel(annotatedClasses = { BidirectionalOneToOneWithConverterEagerTest.FooEntity.class, BidirectionalOneToOneWithConverterEagerTest.BarEntity.class, @@ -72,24 +70,18 @@ public class BidirectionalOneToOneWithConverterEagerTest { @Test public void testBidirectionalFetch(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + statementInspector.clear(); scope.inTransaction( session -> { FooEntity foo = session.find( FooEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.assertExecutedCount( 1 ); BarEntity bar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( 0.5, bar.getaDouble() ); FooEntity associatedFoo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( foo, associatedFoo ); @@ -99,24 +91,18 @@ public class BidirectionalOneToOneWithConverterEagerTest { @Test public void testBidirectionalFetchInverse(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + statementInspector.clear(); scope.inTransaction( session -> { BarEntity bar = session.find( BarEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.assertExecutedCount( 1 ); FooEntity foo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( "foo_name", foo.getName() ); BarEntity associatedBar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( bar, associatedBar ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterLazyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterLazyTest.java index bceb520e40..377969e604 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterLazyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/onetoone/BidirectionalOneToOneWithConverterLazyTest.java @@ -8,10 +8,8 @@ package org.hibernate.orm.test.mapping.converted.converter.onetoone; import java.io.Serializable; import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; - -import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener; +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; @@ -36,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Marco Belladelli */ -@SessionFactory +@SessionFactory(useCollectingStatementInspector = true) @DomainModel(annotatedClasses = { BidirectionalOneToOneWithConverterLazyTest.FooEntity.class, BidirectionalOneToOneWithConverterLazyTest.BarEntity.class, @@ -72,24 +70,17 @@ public class BidirectionalOneToOneWithConverterLazyTest { @Test public void testBidirectionalFetch(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); scope.inTransaction( session -> { FooEntity foo = session.find( FooEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.clear(); BarEntity bar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( 0.5, bar.getaDouble() ); FooEntity associatedFoo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( foo, associatedFoo ); @@ -99,24 +90,17 @@ public class BidirectionalOneToOneWithConverterLazyTest { @Test public void testBidirectionalFetchInverse(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); scope.inTransaction( session -> { BarEntity bar = session.find( BarEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.clear(); FooEntity foo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( "foo_name", foo.getName() ); BarEntity associatedBar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( bar, associatedBar ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneEagerFKTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneEagerFKTest.java index 678322bd3d..32566e4161 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneEagerFKTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneEagerFKTest.java @@ -6,10 +6,7 @@ */ package org.hibernate.orm.test.onetoone.bidirectional; -import java.util.concurrent.atomic.AtomicInteger; - -import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener; - +import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -31,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Marco Belladelli */ -@SessionFactory +@SessionFactory(useCollectingStatementInspector = true) @DomainModel(annotatedClasses = { BidirectionalOneToOneEagerFKTest.FooEntity.class, BidirectionalOneToOneEagerFKTest.BarEntity.class @@ -66,24 +63,18 @@ public class BidirectionalOneToOneEagerFKTest { @Test public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + statementInspector.clear(); scope.inTransaction( session -> { - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); - FooEntity foo = session.find( FooEntity.class, 1L ); + statementInspector.assertExecutedCount( 1 ); BarEntity bar = foo.getBar(); - assertEquals( 1, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( 0.5, bar.getaDouble() ); FooEntity associatedFoo = bar.getFoo(); - assertEquals( 1, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( foo, associatedFoo ); @@ -93,25 +84,18 @@ public class BidirectionalOneToOneEagerFKTest { @Test public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); + statementInspector.clear(); scope.inTransaction( session -> { - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); - BarEntity bar = session.find( BarEntity.class, 1L ); - assertEquals( 1, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); FooEntity foo = bar.getFoo(); - assertEquals( 1, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( "foo_name", foo.getName() ); BarEntity associatedBar = foo.getBar(); - assertEquals( 1, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 1 ); assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( bar, associatedBar ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneLazyFKTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneLazyFKTest.java index d207a4f5a0..31f74aa90e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneLazyFKTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/bidirectional/BidirectionalOneToOneLazyFKTest.java @@ -6,10 +6,7 @@ */ package org.hibernate.orm.test.onetoone.bidirectional; -import java.util.concurrent.atomic.AtomicInteger; - -import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener; - +import org.hibernate.testing.jdbc.SQLStatementInspector; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; @@ -31,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Marco Belladelli */ -@SessionFactory +@SessionFactory(useCollectingStatementInspector = true) @DomainModel(annotatedClasses = { BidirectionalOneToOneLazyFKTest.FooEntity.class, BidirectionalOneToOneLazyFKTest.BarEntity.class @@ -66,24 +63,17 @@ public class BidirectionalOneToOneLazyFKTest { @Test public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); scope.inTransaction( session -> { FooEntity foo = session.find( FooEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.clear(); BarEntity bar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( 0.5, bar.getaDouble() ); FooEntity associatedFoo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( foo, associatedFoo ); @@ -93,24 +83,17 @@ public class BidirectionalOneToOneLazyFKTest { @Test public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) { + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); scope.inTransaction( session -> { BarEntity bar = session.find( BarEntity.class, 1L ); - - final AtomicInteger queryExecutionCount = new AtomicInteger(); - session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() { - @Override - public void jdbcExecuteStatementStart() { - super.jdbcExecuteStatementStart(); - queryExecutionCount.getAndIncrement(); - } - } ); + statementInspector.clear(); FooEntity foo = bar.getFoo(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( "foo_name", foo.getName() ); BarEntity associatedBar = foo.getBar(); - assertEquals( 0, queryExecutionCount.get() ); + statementInspector.assertExecutedCount( 0 ); assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( bar, associatedBar );