HHH-15982 Refactor tests to use StatementInspector

This commit is contained in:
Marco Belladelli 2023-01-09 12:13:41 +01:00 committed by Christian Beikov
parent bf60dd9d2a
commit e26f4d25ab
4 changed files with 44 additions and 107 deletions

View File

@ -8,10 +8,8 @@ package org.hibernate.orm.test.mapping.converted.converter.onetoone;
import java.io.Serializable; import java.io.Serializable;
import java.util.UUID; 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.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
@ -36,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@SessionFactory @SessionFactory(useCollectingStatementInspector = true)
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
BidirectionalOneToOneWithConverterEagerTest.FooEntity.class, BidirectionalOneToOneWithConverterEagerTest.FooEntity.class,
BidirectionalOneToOneWithConverterEagerTest.BarEntity.class, BidirectionalOneToOneWithConverterEagerTest.BarEntity.class,
@ -72,24 +70,18 @@ public class BidirectionalOneToOneWithConverterEagerTest {
@Test @Test
public void testBidirectionalFetch(SessionFactoryScope scope) { public void testBidirectionalFetch(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inTransaction( session -> { scope.inTransaction( session -> {
FooEntity foo = session.find( FooEntity.class, 1L ); FooEntity foo = session.find( FooEntity.class, 1L );
statementInspector.assertExecutedCount( 1 );
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
BarEntity bar = foo.getBar(); BarEntity bar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( 0.5, bar.getaDouble() ); assertEquals( 0.5, bar.getaDouble() );
FooEntity associatedFoo = bar.getFoo(); FooEntity associatedFoo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( "foo_name", associatedFoo.getName() );
assertEquals( foo, associatedFoo ); assertEquals( foo, associatedFoo );
@ -99,24 +91,18 @@ public class BidirectionalOneToOneWithConverterEagerTest {
@Test @Test
public void testBidirectionalFetchInverse(SessionFactoryScope scope) { public void testBidirectionalFetchInverse(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inTransaction( session -> { scope.inTransaction( session -> {
BarEntity bar = session.find( BarEntity.class, 1L ); BarEntity bar = session.find( BarEntity.class, 1L );
statementInspector.assertExecutedCount( 1 );
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
FooEntity foo = bar.getFoo(); FooEntity foo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( "foo_name", foo.getName() ); assertEquals( "foo_name", foo.getName() );
BarEntity associatedBar = foo.getBar(); BarEntity associatedBar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( 0.5, associatedBar.getaDouble() );
assertEquals( bar, associatedBar ); assertEquals( bar, associatedBar );

View File

@ -8,10 +8,8 @@ package org.hibernate.orm.test.mapping.converted.converter.onetoone;
import java.io.Serializable; import java.io.Serializable;
import java.util.UUID; 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.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
@ -36,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@SessionFactory @SessionFactory(useCollectingStatementInspector = true)
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
BidirectionalOneToOneWithConverterLazyTest.FooEntity.class, BidirectionalOneToOneWithConverterLazyTest.FooEntity.class,
BidirectionalOneToOneWithConverterLazyTest.BarEntity.class, BidirectionalOneToOneWithConverterLazyTest.BarEntity.class,
@ -72,24 +70,17 @@ public class BidirectionalOneToOneWithConverterLazyTest {
@Test @Test
public void testBidirectionalFetch(SessionFactoryScope scope) { public void testBidirectionalFetch(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
scope.inTransaction( session -> { scope.inTransaction( session -> {
FooEntity foo = session.find( FooEntity.class, 1L ); FooEntity foo = session.find( FooEntity.class, 1L );
statementInspector.clear();
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
BarEntity bar = foo.getBar(); BarEntity bar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( 0.5, bar.getaDouble() ); assertEquals( 0.5, bar.getaDouble() );
FooEntity associatedFoo = bar.getFoo(); FooEntity associatedFoo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( "foo_name", associatedFoo.getName() );
assertEquals( foo, associatedFoo ); assertEquals( foo, associatedFoo );
@ -99,24 +90,17 @@ public class BidirectionalOneToOneWithConverterLazyTest {
@Test @Test
public void testBidirectionalFetchInverse(SessionFactoryScope scope) { public void testBidirectionalFetchInverse(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
scope.inTransaction( session -> { scope.inTransaction( session -> {
BarEntity bar = session.find( BarEntity.class, 1L ); BarEntity bar = session.find( BarEntity.class, 1L );
statementInspector.clear();
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
FooEntity foo = bar.getFoo(); FooEntity foo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( "foo_name", foo.getName() ); assertEquals( "foo_name", foo.getName() );
BarEntity associatedBar = foo.getBar(); BarEntity associatedBar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( 0.5, associatedBar.getaDouble() );
assertEquals( bar, associatedBar ); assertEquals( bar, associatedBar );

View File

@ -6,10 +6,7 @@
*/ */
package org.hibernate.orm.test.onetoone.bidirectional; package org.hibernate.orm.test.onetoone.bidirectional;
import java.util.concurrent.atomic.AtomicInteger; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -31,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@SessionFactory @SessionFactory(useCollectingStatementInspector = true)
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
BidirectionalOneToOneEagerFKTest.FooEntity.class, BidirectionalOneToOneEagerFKTest.FooEntity.class,
BidirectionalOneToOneEagerFKTest.BarEntity.class BidirectionalOneToOneEagerFKTest.BarEntity.class
@ -66,24 +63,18 @@ public class BidirectionalOneToOneEagerFKTest {
@Test @Test
public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) { public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inTransaction( session -> { 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 ); FooEntity foo = session.find( FooEntity.class, 1L );
statementInspector.assertExecutedCount( 1 );
BarEntity bar = foo.getBar(); BarEntity bar = foo.getBar();
assertEquals( 1, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( 0.5, bar.getaDouble() ); assertEquals( 0.5, bar.getaDouble() );
FooEntity associatedFoo = bar.getFoo(); FooEntity associatedFoo = bar.getFoo();
assertEquals( 1, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( "foo_name", associatedFoo.getName() );
assertEquals( foo, associatedFoo ); assertEquals( foo, associatedFoo );
@ -93,25 +84,18 @@ public class BidirectionalOneToOneEagerFKTest {
@Test @Test
public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) { public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inTransaction( session -> { 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 ); BarEntity bar = session.find( BarEntity.class, 1L );
assertEquals( 1, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
FooEntity foo = bar.getFoo(); FooEntity foo = bar.getFoo();
assertEquals( 1, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( "foo_name", foo.getName() ); assertEquals( "foo_name", foo.getName() );
BarEntity associatedBar = foo.getBar(); BarEntity associatedBar = foo.getBar();
assertEquals( 1, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 1 );
assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( 0.5, associatedBar.getaDouble() );
assertEquals( bar, associatedBar ); assertEquals( bar, associatedBar );

View File

@ -6,10 +6,7 @@
*/ */
package org.hibernate.orm.test.onetoone.bidirectional; package org.hibernate.orm.test.onetoone.bidirectional;
import java.util.concurrent.atomic.AtomicInteger; import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.engine.internal.StatisticalLoggingSessionEventListener;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -31,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@SessionFactory @SessionFactory(useCollectingStatementInspector = true)
@DomainModel(annotatedClasses = { @DomainModel(annotatedClasses = {
BidirectionalOneToOneLazyFKTest.FooEntity.class, BidirectionalOneToOneLazyFKTest.FooEntity.class,
BidirectionalOneToOneLazyFKTest.BarEntity.class BidirectionalOneToOneLazyFKTest.BarEntity.class
@ -66,24 +63,17 @@ public class BidirectionalOneToOneLazyFKTest {
@Test @Test
public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) { public void testBidirectionalFetchJoinColumnSide(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
scope.inTransaction( session -> { scope.inTransaction( session -> {
FooEntity foo = session.find( FooEntity.class, 1L ); FooEntity foo = session.find( FooEntity.class, 1L );
statementInspector.clear();
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
BarEntity bar = foo.getBar(); BarEntity bar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( 0.5, bar.getaDouble() ); assertEquals( 0.5, bar.getaDouble() );
FooEntity associatedFoo = bar.getFoo(); FooEntity associatedFoo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( "foo_name", associatedFoo.getName() ); assertEquals( "foo_name", associatedFoo.getName() );
assertEquals( foo, associatedFoo ); assertEquals( foo, associatedFoo );
@ -93,24 +83,17 @@ public class BidirectionalOneToOneLazyFKTest {
@Test @Test
public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) { public void testBidirectionalFetchMappedBySide(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
scope.inTransaction( session -> { scope.inTransaction( session -> {
BarEntity bar = session.find( BarEntity.class, 1L ); BarEntity bar = session.find( BarEntity.class, 1L );
statementInspector.clear();
final AtomicInteger queryExecutionCount = new AtomicInteger();
session.getEventListenerManager().addListener( new StatisticalLoggingSessionEventListener() {
@Override
public void jdbcExecuteStatementStart() {
super.jdbcExecuteStatementStart();
queryExecutionCount.getAndIncrement();
}
} );
FooEntity foo = bar.getFoo(); FooEntity foo = bar.getFoo();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( "foo_name", foo.getName() ); assertEquals( "foo_name", foo.getName() );
BarEntity associatedBar = foo.getBar(); BarEntity associatedBar = foo.getBar();
assertEquals( 0, queryExecutionCount.get() ); statementInspector.assertExecutedCount( 0 );
assertEquals( 0.5, associatedBar.getaDouble() ); assertEquals( 0.5, associatedBar.getaDouble() );
assertEquals( bar, associatedBar ); assertEquals( bar, associatedBar );