From 19f78bbd3d1c824f5781f0c7fdf57a5858fb6117 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sun, 15 Apr 2018 18:56:06 +0100 Subject: [PATCH] HHH-12487 - Add test for issue --- ...ntityManagerWithActiveTransactionTest.java | 1 + ...nagerWithJpaTransactionComplianceTest.java | 129 ++++++++++++++++++ ...erWithoutJpaTransactionComplianceTest.java | 121 ++++++++++++++++ .../test/transaction/GetTransactionTest.java | 42 ++++++ .../JtaGetTransactionThrowsExceptionTest.java | 56 ++++++++ 5 files changed, 349 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithJpaTransactionComplianceTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithoutJpaTransactionComplianceTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/GetTransactionTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaGetTransactionThrowsExceptionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java index b06398fbf7..bd81641e84 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java @@ -50,6 +50,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag super.addConfigOptions( options ); TestingJtaBootstrap.prepare( options ); options.put( AvailableSettings.TRANSACTION_TYPE, "JTA" ); + options.put( org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithJpaTransactionComplianceTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithJpaTransactionComplianceTest.java new file mode 100644 index 0000000000..46b037f4ee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithJpaTransactionComplianceTest.java @@ -0,0 +1,129 @@ +/* + * 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.jpa.test.transaction; + +import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import org.hamcrest.CoreMatchers; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +public class ClosedEntityManagerWithJpaTransactionComplianceTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected void addConfigOptions(Map options) { + options.put( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); + } + + @Test + public void shouldReturnNotNullValueTest() { + EntityManager em = createEntityManager(); + em.close(); + assertThat( em.getTransaction(), CoreMatchers.notNullValue() ); + } + + @Test + public void testCallTransactionIsActive() { + EntityManager em = createEntityManager(); + em.close(); + assertFalse( em.getTransaction().isActive() ); + } + + @Test + public void testCallTransactionIsActive2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + assertFalse( transaction.isActive() ); + } + + @Test(expected = IllegalStateException.class) + public void testCallCommit() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().commit(); + } + + @Test(expected = IllegalStateException.class) + public void testCallCommit2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.commit(); + } + + @Test(expected = IllegalStateException.class) + public void testCallBegin() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().begin(); + } + + @Test(expected = IllegalStateException.class) + public void testCallBegin2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.begin(); + } + + @Test(expected = IllegalStateException.class) + public void testCallSetRollBackOnly() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().setRollbackOnly(); + } + + @Test(expected = IllegalStateException.class) + public void testCallSetRollBackOnly2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.setRollbackOnly(); + } + + @Test(expected = IllegalStateException.class) + public void testCallRollBack() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().rollback(); + } + + @Test(expected = IllegalStateException.class) + public void testCallRollBack2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.rollback(); + } + + @Test(expected = IllegalStateException.class) + public void testCallGetRollBackOnly() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().getRollbackOnly(); + } + + @Test(expected = IllegalStateException.class) + public void testCallGetRollBackOnly2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.getRollbackOnly(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithoutJpaTransactionComplianceTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithoutJpaTransactionComplianceTest.java new file mode 100644 index 0000000000..1bbd018011 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/ClosedEntityManagerWithoutJpaTransactionComplianceTest.java @@ -0,0 +1,121 @@ +/* + * 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.jpa.test.transaction; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import org.hamcrest.CoreMatchers; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +public class ClosedEntityManagerWithoutJpaTransactionComplianceTest extends BaseEntityManagerFunctionalTestCase { + @Test + public void shouldReturnNotNullValueTest() { + EntityManager em = createEntityManager(); + em.close(); + assertThat( em.getTransaction(), CoreMatchers.notNullValue() ); + } + + @Test + public void testCallTransactionIsActive() { + EntityManager em = createEntityManager(); + em.close(); + assertFalse( em.getTransaction().isActive() ); + } + + @Test + public void testCallTransactionIsActive2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + assertFalse( transaction.isActive() ); + } + + @Test(expected = IllegalStateException.class) + public void testCallCommit() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().commit(); + } + + @Test(expected = IllegalStateException.class) + public void testCallCommit2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.commit(); + } + + @Test(expected = IllegalStateException.class) + public void testCallBegin() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().begin(); + } + + @Test(expected = IllegalStateException.class) + public void testCallBegin2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.begin(); + } + + @Test + public void testCallSetRollBackOnly() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().setRollbackOnly(); + } + + @Test + public void testCallSetRollBackOnly2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.setRollbackOnly(); + } + + @Test + public void testCallRollBack() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction().rollback(); + } + + @Test + public void testCallRollBack2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + transaction.rollback(); + } + + @Test + public void testCallGetRollBackOnly() { + EntityManager em = createEntityManager(); + em.close(); + assertFalse( em.getTransaction().getRollbackOnly() ); + } + + @Test + public void testCallGetRollBackOnly2() { + EntityManager em = createEntityManager(); + EntityTransaction transaction = em.getTransaction(); + em.close(); + assertFalse( transaction.getRollbackOnly() ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/GetTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/GetTransactionTest.java new file mode 100644 index 0000000000..ef3fdb60fe --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/GetTransactionTest.java @@ -0,0 +1,42 @@ +/* + * 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.jpa.test.transaction; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * @author Andrea Boriero + */ +public class GetTransactionTest extends BaseEntityManagerFunctionalTestCase { + + @Test + public void testMultipleCallsReturnTheSameTransaction() { + EntityManager em = createEntityManager(); + EntityTransaction t = em.getTransaction(); + assertSame( t, em.getTransaction() ); + assertFalse( t.isActive() ); + t.begin(); + assertSame( t, em.getTransaction() ); + assertTrue( t.isActive() ); + t.commit(); + assertSame( t, em.getTransaction() ); + assertFalse( t.isActive() ); + em.close(); + assertSame( t, em.getTransaction() ); + assertFalse( t.isActive() ); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaGetTransactionThrowsExceptionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaGetTransactionThrowsExceptionTest.java new file mode 100644 index 0000000000..003a69beac --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaGetTransactionThrowsExceptionTest.java @@ -0,0 +1,56 @@ +/* + * 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.jpa.test.transaction; + +import java.util.Map; + +import javax.persistence.EntityManager; + +import org.hibernate.jpa.AvailableSettings; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jta.TestingJtaBootstrap; +import org.junit.Test; + +import static org.junit.Assert.fail; + +/** + * @author Andrea Boriero + */ +public class JtaGetTransactionThrowsExceptionTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected void addConfigOptions(Map options) { + super.addConfigOptions( options ); + TestingJtaBootstrap.prepare( options ); + options.put( AvailableSettings.TRANSACTION_TYPE, "JTA" ); + options.put( org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); + } + + @Test(expected = IllegalStateException.class) + @TestForIssue( jiraKey = "HHH-12487") + public void onCloseEntityManagerTest() { + EntityManager em = createEntityManager(); + em.close(); + em.getTransaction(); + fail( "Calling getTransaction on a JTA entity manager should throw an IllegalStateException" ); + } + + @Test(expected = IllegalStateException.class) + @TestForIssue(jiraKey = "HHH-12487") + public void onOpenEntityManagerTest() { + EntityManager em = createEntityManager(); + try { + em.getTransaction(); + fail( "Calling getTransaction on a JTA entity manager should throw an IllegalStateException" ); + } + finally { + em.close(); + } + } +} +