From 8aa976ea2d2a126e0fb51b1c1b5e8a2be2d36634 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Tue, 19 Mar 2019 16:58:28 -0700 Subject: [PATCH] HHH-13326 : test cases (cherry picked from commit 705ecec94f6624797b91a4fd4f48db68c8bffdb4) --- ...paOrNativeBootstrapFunctionalTestCase.java | 6 + .../InterceptorNonNullTransactionTest.java | 256 +++++++++++++ .../test/tm/InterceptorTransactionTest.java | 358 ++++++++++++++++++ 3 files changed, 620 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/BaseJpaOrNativeBootstrapFunctionalTestCase.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/BaseJpaOrNativeBootstrapFunctionalTestCase.java index 732cd4c0d6..dad7771844 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/BaseJpaOrNativeBootstrapFunctionalTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/BaseJpaOrNativeBootstrapFunctionalTestCase.java @@ -19,6 +19,7 @@ import javax.persistence.ValidationMode; import javax.persistence.spi.PersistenceUnitTransactionType; import org.hibernate.HibernateException; +import org.hibernate.Interceptor; import org.hibernate.Session; import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; @@ -84,6 +85,11 @@ public abstract class BaseJpaOrNativeBootstrapFunctionalTestCase extends BaseUni return session; } + protected Session openSession(Interceptor interceptor) throws HibernateException { + session = sessionFactory().withOptions().interceptor( interceptor ).openSession(); + return session; + } + protected EntityManager openEntityManager() throws HibernateException { return openSession().unwrap( EntityManager.class ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java new file mode 100644 index 0000000000..4155316005 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java @@ -0,0 +1,256 @@ +/* + * 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 . + */ +package org.hibernate.test.interceptor; + +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 java.util.Arrays; +import java.util.Map; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.EmptyInterceptor; +import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.resource.transaction.spi.TransactionStatus; +import org.hibernate.test.exceptionhandling.BaseJpaOrNativeBootstrapFunctionalTestCase; +import org.hibernate.testing.junit4.CustomParameterized; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(CustomParameterized.class) +public class InterceptorNonNullTransactionTest extends BaseJpaOrNativeBootstrapFunctionalTestCase { + + public enum JpaComplianceTransactionSetting { DEFAULT, TRUE, FALSE } + public enum JtaAllowTransactionAccessSetting { DEFAULT, TRUE, FALSE; }; + + @Parameterized.Parameters(name = "Bootstrap={0}, JpaComplianceTransactionSetting={1}, JtaAllowTransactionAccessSetting={2}") + public static Iterable parameters() { + return Arrays.asList( new Object[][] { + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.DEFAULT, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.DEFAULT, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.FALSE }, + } ); + } + + private final JpaComplianceTransactionSetting jpaComplianceTransactionSetting; + private final JtaAllowTransactionAccessSetting jtaAllowTransactionAccessSetting; + + public InterceptorNonNullTransactionTest( + BootstrapMethod bootstrapMethod, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting, + JtaAllowTransactionAccessSetting jtaAllowTransactionAccessSetting) { + super( bootstrapMethod ); + this.jpaComplianceTransactionSetting = jpaComplianceTransactionSetting; + this.jtaAllowTransactionAccessSetting = jtaAllowTransactionAccessSetting; + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { SimpleEntity.class }; + } + + @Override + protected void configure(Map properties) { + super.configure( properties ); + + switch ( jpaComplianceTransactionSetting ) { + case DEFAULT: + // Keep the default (false) + break; + case TRUE: + properties.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); + break; + case FALSE: + properties.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "false" ); + break; + + } + switch ( jtaAllowTransactionAccessSetting ) { + case DEFAULT: + // Keep the default (true native bootstrap; false if jpa bootstrap) + break; + case TRUE: + properties.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); + break; + case FALSE: + properties.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "false" ); + break; + } + } + + @Test + public void testHibernateTransactionApi() throws Exception { + + final TransactionInterceptor interceptor = new TransactionInterceptor(); + + Session session = sessionFactory().withOptions().interceptor( interceptor ).openSession(); + + session.getTransaction().begin(); + + assertTrue( interceptor.afterTransactionBeginMethodCalled ); + assertTrue( interceptor.afterTransactionBeginAssertionPassed ); + assertFalse( interceptor.beforeTransactionCompletionMethodCalled ); + assertNull( interceptor.beforeTransactionCompletionAssertionPassed ); + assertFalse( interceptor.afterTransactionCompletionMethodCalled ); + assertNull( interceptor.afterTransactionCompletionAssertionPassed ); + + SimpleEntity entity = new SimpleEntity( "Hello World" ); + session.save( entity ); + + interceptor.reset(); + + session.getTransaction().commit(); + + + assertFalse( interceptor.afterTransactionBeginMethodCalled ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + assertTrue( interceptor.beforeTransactionCompletionMethodCalled ); + assertTrue( interceptor.afterTransactionCompletionMethodCalled ); + assertEquals( true, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( true, interceptor.afterTransactionCompletionAssertionPassed ); + + session.close(); + } + + @Test + public void testJtaApiWithSharedTransactionCoordinator() throws Exception { + + final TransactionInterceptor interceptor = new TransactionInterceptor(); + + Session originalSession = openSession( interceptor ); + + Session session = originalSession.sessionWithOptions().interceptor( interceptor ).connection().openSession(); + + interceptor.reset(); + + session.getTransaction().begin(); + + assertTrue( interceptor.afterTransactionBeginMethodCalled ); + assertTrue( interceptor.afterTransactionBeginAssertionPassed ); + assertFalse( interceptor.beforeTransactionCompletionMethodCalled ); + assertNull( interceptor.beforeTransactionCompletionAssertionPassed ); + assertFalse( interceptor.afterTransactionCompletionMethodCalled ); + assertNull( interceptor.afterTransactionCompletionAssertionPassed ); + + SimpleEntity entity = new SimpleEntity( "Hello World" ); + session.save( entity ); + + interceptor.reset(); + + session.getTransaction().commit(); + + assertFalse( interceptor.afterTransactionBeginMethodCalled ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + assertTrue( interceptor.beforeTransactionCompletionMethodCalled ); + assertTrue( interceptor.afterTransactionCompletionMethodCalled ); + assertEquals( true, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( true, interceptor.afterTransactionCompletionAssertionPassed ); + + session.close(); + + originalSession.close(); + } + + @Entity(name = "SimpleEntity") + public static class SimpleEntity { + @Id + @GeneratedValue + private Integer id; + private String name; + + SimpleEntity() { + + } + + SimpleEntity(String name) { + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + private class TransactionInterceptor extends EmptyInterceptor { + private boolean afterTransactionBeginMethodCalled; + private Boolean afterTransactionBeginAssertionPassed; + + + private boolean beforeTransactionCompletionMethodCalled; + private Boolean beforeTransactionCompletionAssertionPassed; + + private boolean afterTransactionCompletionMethodCalled; + private Boolean afterTransactionCompletionAssertionPassed; + + public void reset() { + afterTransactionBeginMethodCalled = false; + afterTransactionBeginAssertionPassed = null; + beforeTransactionCompletionMethodCalled = false; + beforeTransactionCompletionAssertionPassed = null; + afterTransactionCompletionMethodCalled = false; + afterTransactionCompletionAssertionPassed = null; + } + + @Override + public void afterTransactionBegin(org.hibernate.Transaction tx) { + afterTransactionBeginMethodCalled = true; + if ( tx != null ) { + afterTransactionBeginAssertionPassed = false; + assertEquals( TransactionStatus.ACTIVE, tx.getStatus() ); + afterTransactionBeginAssertionPassed = true; + } + } + @Override + public void beforeTransactionCompletion(org.hibernate.Transaction tx) { + beforeTransactionCompletionMethodCalled = true; + if ( tx != null ) { + beforeTransactionCompletionAssertionPassed = false; + assertEquals( TransactionStatus.ACTIVE, tx.getStatus() ); + beforeTransactionCompletionAssertionPassed = true; + } + } + @Override + public void afterTransactionCompletion(org.hibernate.Transaction tx) { + afterTransactionCompletionMethodCalled = true; + if ( tx != null ) { + afterTransactionCompletionAssertionPassed = false; + assertEquals( TransactionStatus.COMMITTED, tx.getStatus() ); + afterTransactionCompletionAssertionPassed = true; + } + } + }; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java new file mode 100644 index 0000000000..d20d110c51 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java @@ -0,0 +1,358 @@ +/* + * 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 . + */ +package org.hibernate.test.tm; + +import java.util.Arrays; +import java.util.Map; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import javax.transaction.Status; + +import org.hibernate.EmptyInterceptor; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.resource.transaction.spi.TransactionStatus; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jta.TestingJtaBootstrap; +import org.hibernate.testing.jta.TestingJtaPlatformImpl; +import org.hibernate.testing.junit4.CustomParameterized; +import org.hibernate.test.exceptionhandling.BaseJpaOrNativeBootstrapFunctionalTestCase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +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; + +@TestForIssue( jiraKey = "HHH-13326") +@RunWith(CustomParameterized.class) +public class InterceptorTransactionTest extends BaseJpaOrNativeBootstrapFunctionalTestCase { + + public enum JpaComplianceTransactionSetting { DEFAULT, TRUE, FALSE } + public enum JtaAllowTransactionAccessSetting { + DEFAULT { + @Override + public boolean allowTransactionAccess( + SessionFactory sessionFactory, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting) { + return !sessionFactory.getSessionFactoryOptions().isJpaBootstrap() || + jpaComplianceTransactionSetting != JpaComplianceTransactionSetting.TRUE; + } + }, + TRUE { + @Override + public boolean allowTransactionAccess( + SessionFactory sessionFactory, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting) { + return true; + } + }, + FALSE { + @Override + public boolean allowTransactionAccess( + SessionFactory sessionFactory, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting) { + // setting is ignored if jpaComplianceTransactionSetting != JpaComplianceTransactionSetting.TRUE + return jpaComplianceTransactionSetting != JpaComplianceTransactionSetting.TRUE; + } + }; + + public abstract boolean allowTransactionAccess( + SessionFactory sessionFactory, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting + ); + }; + + @Parameterized.Parameters(name = "Bootstrap={0}, JpaComplianceTransactionSetting={1}, JtaAllowTransactionAccessSetting={2}") + public static Iterable parameters() { + return Arrays.asList( new Object[][] { + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.DEFAULT, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.JPA, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.DEFAULT, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.TRUE, JtaAllowTransactionAccessSetting.FALSE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.DEFAULT }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.TRUE }, + { BootstrapMethod.NATIVE, JpaComplianceTransactionSetting.FALSE, JtaAllowTransactionAccessSetting.FALSE }, + } ); + } + + private final JpaComplianceTransactionSetting jpaComplianceTransactionSetting; + private final JtaAllowTransactionAccessSetting jtaAllowTransactionAccessSetting; + + public InterceptorTransactionTest( + BootstrapMethod bootstrapMethod, + JpaComplianceTransactionSetting jpaComplianceTransactionSetting, + JtaAllowTransactionAccessSetting jtaAllowTransactionAccessSetting) { + super( bootstrapMethod ); + this.jpaComplianceTransactionSetting = jpaComplianceTransactionSetting; + this.jtaAllowTransactionAccessSetting = jtaAllowTransactionAccessSetting; + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { SimpleEntity.class }; + } + + protected void configure(Map properties) { + super.configure( properties ); + TestingJtaBootstrap.prepare( properties ); + properties.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); + + switch ( jpaComplianceTransactionSetting ) { + case DEFAULT: + // Keep the default (false) + break; + case TRUE: + properties.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); + break; + case FALSE: + properties.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "false" ); + break; + + } + switch ( jtaAllowTransactionAccessSetting ) { + case DEFAULT: + // Keep the default (true native bootstrap; false if jpa bootstrap) + break; + case TRUE: + properties.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); + break; + case FALSE: + properties.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "false" ); + break; + } + } + + @Test + public void testHibernateTransactionApi() throws Exception { + + final TransactionInterceptor interceptor = new TransactionInterceptor(); + + Session session = sessionFactory().withOptions().interceptor( interceptor ).openSession(); + + try { + session.getTransaction().begin(); + if ( !jtaAllowTransactionAccessSetting.allowTransactionAccess( + sessionFactory(), + jpaComplianceTransactionSetting + ) ) { + fail( "IllegalStateException should have been thrown." ); + } + } + catch (IllegalStateException ex) { + if ( TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getStatus() == Status.STATUS_ACTIVE ) { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().setRollbackOnly(); + } + session.close(); + assertEquals( JpaComplianceTransactionSetting.TRUE, jpaComplianceTransactionSetting ); + return; // EARLY RETURN + } + + // Interceptor#afterTransactionBegin is never called when using JTA + assertFalse( interceptor.afterTransactionBeginMethodCalled ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + assertNull( interceptor.beforeTransactionCompletionAssertionPassed ); + assertNull( interceptor.afterTransactionCompletionAssertionPassed ); + + SimpleEntity entity = new SimpleEntity( "Hello World" ); + session.save( entity ); + + interceptor.reset(); + + session.getTransaction().commit(); + + assertTrue( interceptor.beforeTransactionCompletionMethodCalled ); + assertTrue( interceptor.afterTransactionCompletionMethodCalled ); + assertEquals( true, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( true, interceptor.afterTransactionCompletionAssertionPassed ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + + session.close(); + } + + @Test + public void testJtaApi() throws Exception { + + final TransactionInterceptor interceptor = new TransactionInterceptor(); + + Session session = sessionFactory().withOptions().interceptor( interceptor ).openSession(); + + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); + + // Interceptor#afterTransactionBegin is never called when using JTA + assertFalse( interceptor.afterTransactionBeginMethodCalled ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + assertNull( interceptor.beforeTransactionCompletionAssertionPassed ); + assertNull( interceptor.afterTransactionCompletionAssertionPassed ); + + SimpleEntity entity = new SimpleEntity( "Hello World" ); + session.save( entity ); + + interceptor.reset(); + + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); + + assertTrue( interceptor.beforeTransactionCompletionMethodCalled ); + assertTrue( interceptor.afterTransactionCompletionMethodCalled ); + if ( jtaAllowTransactionAccessSetting.allowTransactionAccess( + sessionFactory(), + jpaComplianceTransactionSetting + ) ) { + assertEquals( true, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( true, interceptor.afterTransactionCompletionAssertionPassed ); + } + else { + assertEquals( null, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( null, interceptor.afterTransactionCompletionAssertionPassed ); + } + + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + + session.close(); + } + + @Test + public void testJtaApiWithSharedTransactionCoordinator() throws Exception { + + final TransactionInterceptor interceptor = new TransactionInterceptor(); + + Session originalSession = openSession(); + + Session session = originalSession.sessionWithOptions().connection().interceptor( interceptor ).openSession(); + + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); + + // Interceptor#afterTransactionBegin is never called when using JTA + assertFalse( interceptor.afterTransactionBeginMethodCalled ); + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + assertNull( interceptor.beforeTransactionCompletionAssertionPassed ); + assertNull( interceptor.afterTransactionCompletionAssertionPassed ); + + SimpleEntity entity = new SimpleEntity( "Hello World" ); + session.save( entity ); + + interceptor.reset(); + + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); + + assertTrue( interceptor.beforeTransactionCompletionMethodCalled ); + assertTrue( interceptor.afterTransactionCompletionMethodCalled ); + if ( jtaAllowTransactionAccessSetting.allowTransactionAccess( + sessionFactory(), + jpaComplianceTransactionSetting + ) ) { + assertEquals( true, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( true, interceptor.afterTransactionCompletionAssertionPassed ); + } + else { + assertEquals( null, interceptor.beforeTransactionCompletionAssertionPassed ); + assertEquals( null, interceptor.afterTransactionCompletionAssertionPassed ); + } + + assertNull( interceptor.afterTransactionBeginAssertionPassed ); + + session.close(); + + originalSession.close(); + } + + @Entity(name = "SimpleEntity") + public static class SimpleEntity { + @Id + @GeneratedValue + private Integer id; + private String name; + + SimpleEntity() { + + } + + SimpleEntity(String name) { + this.name = name; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + private class TransactionInterceptor extends EmptyInterceptor { + private boolean afterTransactionBeginMethodCalled; + private Boolean afterTransactionBeginAssertionPassed; + + + private boolean beforeTransactionCompletionMethodCalled; + private Boolean beforeTransactionCompletionAssertionPassed; + + private boolean afterTransactionCompletionMethodCalled; + private Boolean afterTransactionCompletionAssertionPassed; + + public void reset() { + afterTransactionBeginMethodCalled = false; + afterTransactionBeginAssertionPassed = null; + beforeTransactionCompletionMethodCalled = false; + beforeTransactionCompletionAssertionPassed = null; + afterTransactionCompletionMethodCalled = false; + afterTransactionCompletionAssertionPassed = null; + } + + @Override + public void afterTransactionBegin(org.hibernate.Transaction tx) { + afterTransactionBeginMethodCalled = true; + if ( tx != null ) { + afterTransactionBeginAssertionPassed = false; + assertEquals( TransactionStatus.ACTIVE, tx.getStatus() ); + afterTransactionBeginAssertionPassed = true; + } + } + @Override + public void beforeTransactionCompletion(org.hibernate.Transaction tx) { + beforeTransactionCompletionMethodCalled = true; + if ( tx != null ) { + beforeTransactionCompletionAssertionPassed = false; + assertEquals( TransactionStatus.ACTIVE, tx.getStatus() ); + beforeTransactionCompletionAssertionPassed = true; + } + } + @Override + public void afterTransactionCompletion(org.hibernate.Transaction tx) { + afterTransactionCompletionMethodCalled = true; + if ( tx != null ) { + afterTransactionCompletionAssertionPassed = false; + assertEquals( TransactionStatus.COMMITTED, tx.getStatus() ); + afterTransactionCompletionAssertionPassed = true; + } + } + }; +}