diff --git a/hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/InterceptorTransactionTest.java similarity index 99% rename from hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/tm/InterceptorTransactionTest.java index 985a566232..945d098ecf 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/tm/InterceptorTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/InterceptorTransactionTest.java @@ -4,7 +4,7 @@ * 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; +package org.hibernate.orm.test.tm; import java.util.Arrays; import java.util.Map; diff --git a/hibernate-core/src/test/java/org/hibernate/test/tm/JtaAfterCompletionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaAfterCompletionTest.java similarity index 81% rename from hibernate-core/src/test/java/org/hibernate/test/tm/JtaAfterCompletionTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaAfterCompletionTest.java index 8145a6df1d..98b34410cd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/tm/JtaAfterCompletionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaAfterCompletionTest.java @@ -4,9 +4,7 @@ * 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.Map; +package org.hibernate.orm.test.tm; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -19,36 +17,38 @@ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.action.spi.AfterTransactionCompletionProcess; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.ActionQueue; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.junit.Test; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; + +import static org.hibernate.testing.orm.junit.ExtraAssertions.assertTyping; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Chris Cranford */ -public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase { +public class JtaAfterCompletionTest extends BaseSessionFactoryFunctionalTest { + @Override protected Class[] getAnnotatedClasses() { return new Class[] { SimpleEntity.class }; } @Override - protected void addSettings(Map settings) { - super.addSettings( settings ); - TestingJtaBootstrap.prepare( settings ); - settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); - settings.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); + protected void applySettings(StandardServiceRegistryBuilder builder) { + TestingJtaBootstrap.prepare( builder.getSettings() ); + builder.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); + builder.applySetting( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); } @Test @@ -63,7 +63,7 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase Session session = null; try { - session = openSession(); + session = sessionFactory().openSession(); SimpleEntity entity = new SimpleEntity( "Hello World" ); session.save( entity ); @@ -78,7 +78,7 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase TestingJtaPlatformImpl.transactionManager().commit(); } - catch ( Exception e ) { + catch (Exception e) { // This is expected assertTyping( RollbackException.class, e ); } @@ -88,7 +88,7 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase session.close(); } } - catch ( HibernateException e ) { + catch (HibernateException e) { // This is expected assertEquals( "Transaction was rolled back in a different thread!", e.getMessage() ); } @@ -104,10 +104,11 @@ public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase try { // Wait for the transaction to be rolled back by the Reaper thread. final Transaction transaction = TestingJtaPlatformImpl.transactionManager().getTransaction(); - while ( transaction.getStatus() != Status.STATUS_ROLLEDBACK ) + while ( transaction.getStatus() != Status.STATUS_ROLLEDBACK ) { Thread.sleep( 10 ); + } } - catch ( Exception e ) { + catch (Exception e) { // we aren't concerned with this. } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaBeforeCompletionFailureTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaBeforeCompletionFailureTest.java new file mode 100644 index 0000000000..c969150ad6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tm/JtaBeforeCompletionFailureTest.java @@ -0,0 +1,124 @@ +/* + * 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.orm.test.tm; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.transaction.RollbackException; +import javax.transaction.Status; +import javax.transaction.TransactionManager; + +import org.hibernate.JDBCException; +import org.hibernate.Session; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.orm.test.resource.transaction.jta.JtaPlatformStandardTestingImpl; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jta.TestingJtaBootstrap; +import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Steve Ebersole + */ +public class JtaBeforeCompletionFailureTest extends BaseSessionFactoryFunctionalTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { SimpleEntity.class }; + } + + @Override + protected void applySettings(StandardServiceRegistryBuilder builder) { + TestingJtaBootstrap.prepare( builder.getSettings() ); + builder.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); + } + + @BeforeEach + public void setUp() { + inTransaction( + session -> + session.save( newEntity( 1 ) ) + ); + + } + + @AfterEach + public void tearDown() { + inTransaction( + session -> + session.createQuery( "delete SimpleEntity" ).executeUpdate() + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-9888") + public void testUniqueConstraintViolationDuringManagedFlush() throws Exception { + final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager(); + assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() ); + + // begin the transaction ("CMT" style) + tm.begin(); + + try (Session session = sessionFactory().openSession()) { + + session.save( newEntity( 2 ) ); + + // complete the transaction ("CMT" style) - this leads to the managed flush + // which should lead to the UK violation + try { + tm.commit(); + fail( "Expecting a failure from JTA commit" ); + } + catch (RollbackException expected) { + boolean violationExceptionFound = false; + Throwable cause = expected; + while ( cause != null ) { + if ( cause instanceof JDBCException ) { + violationExceptionFound = true; + break; + } + cause = cause.getCause(); + } + + if ( !violationExceptionFound ) { + fail( "Did not find JDBCException in JTA RollbackException chain" ); + } + } + } + } + + private SimpleEntity newEntity(int id) { + // since "key" is reused, should violate the UK + return new SimpleEntity( id, "key", "name" ); + } + + @Entity(name = "SimpleEntity") + public static class SimpleEntity { + @Id + public Integer id; + @Column(unique = true, name = "entity_key") + public String key; + public String name; + + public SimpleEntity() { + } + + public SimpleEntity(Integer id, String key, String name) { + this.id = id; + this.key = key; + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/tm/JtaBeforeCompletionFailureTest.java b/hibernate-core/src/test/java/org/hibernate/test/tm/JtaBeforeCompletionFailureTest.java deleted file mode 100644 index cc957056b8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/tm/JtaBeforeCompletionFailureTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.Map; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.transaction.RollbackException; -import javax.transaction.Status; -import javax.transaction.TransactionManager; - -import org.hibernate.JDBCException; -import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionImplementor; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.jta.TestingJtaBootstrap; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.orm.test.resource.transaction.jta.JtaPlatformStandardTestingImpl; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * @author Steve Ebersole - */ -public class JtaBeforeCompletionFailureTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { SimpleEntity.class }; - } - - @Override - protected void addSettings(Map settings) { - super.addSettings( settings ); - TestingJtaBootstrap.prepare( settings ); - settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" ); - } - - @Test - @TestForIssue( jiraKey = "HHH-9888" ) - public void testUniqueConstraintViolationDuringManagedFlush() throws Exception { - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // set up test data - - Session session = openSession(); - session.getTransaction().begin(); - session.save( newEntity( 1 ) ); - session.getTransaction().commit(); - session.close(); - - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // do the test - - final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager(); - assertEquals( Status.STATUS_NO_TRANSACTION, tm.getStatus() ); - - // begin the transaction ("CMT" style) - tm.begin(); - - session = openSession(); - - session.save( newEntity( 2 ) ); - - // complete the transaction ("CMT" style) - this leads to the managed flush - // which should lead to the UK violation - try { - tm.commit(); - fail( "Expecting a failure from JTA commit" ); - } - catch (RollbackException expected) { - log.info( "Test encountered expected JTA RollbackException; looking for nested JDBCException", expected ); - boolean violationExceptionFound = false; - Throwable cause = expected; - while ( cause != null ) { - if ( cause instanceof JDBCException ) { - log.info( "Found JDBCException, assuming related to UK violation", cause ); - violationExceptionFound = true; - break; - } - cause = cause.getCause(); - } - - if ( !violationExceptionFound ) { - fail( "Did not find JDBCException in JTA RollbackException chain" ); - } - } - finally { - if ( !( (SessionImplementor) session ).isClosed() ) { - session.close(); - } - } - - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // clean up test data - - session = openSession(); - session.getTransaction().begin(); - session.createQuery( "delete SimpleEntity" ).executeUpdate(); - session.getTransaction().commit(); - session.close(); - } - - private SimpleEntity newEntity(int id) { - // since "key" is reused, should violate the UK - return new SimpleEntity( id, "key", "name" ); - } - - @Entity(name = "SimpleEntity") - public static class SimpleEntity { - @Id - public Integer id; - @Column(unique = true, name = "entity_key") - public String key; - public String name; - - public SimpleEntity() { - } - - public SimpleEntity(Integer id, String key, String name) { - this.id = id; - this.key = key; - this.name = name; - } - } -}