From a7f242af033b4b7027ab34ddab0f7e9055d69294 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 26 Mar 2021 11:10:37 +0100 Subject: [PATCH] HHH-14503 - Migration of tests from jpa/test to orm/test/jpa --- .../ForeignGeneratorResourceLocalTest.java | 1 + ...eDiscardPersistenceContextOnCloseTest.java | 12 ++--- .../hibernate/orm/test/jpa/ops/FindTest.java | 9 +++- .../batch/JtaWithFailingBatchTest.java | 48 +++++++++---------- .../batch/JtaWithStatementsBatchTest.java | 33 +++++++++---- 5 files changed, 58 insertions(+), 45 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/foreign/ForeignGeneratorResourceLocalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/foreign/ForeignGeneratorResourceLocalTest.java index 6f18d4f9af..b2019a585f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/foreign/ForeignGeneratorResourceLocalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/foreign/ForeignGeneratorResourceLocalTest.java @@ -138,6 +138,7 @@ public class ForeignGeneratorResourceLocalTest { EntityManager entityManager = null; EntityTransaction txn = null; + try { entityManager = scope.getEntityManagerFactory().createEntityManager(); txn = entityManager.getTransaction(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EnableDiscardPersistenceContextOnCloseTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EnableDiscardPersistenceContextOnCloseTest.java index 9c86319033..8051076917 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EnableDiscardPersistenceContextOnCloseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ejb3configuration/EnableDiscardPersistenceContextOnCloseTest.java @@ -7,7 +7,6 @@ package org.hibernate.orm.test.jpa.ejb3configuration; import java.util.Map; -import javax.persistence.EntityManager; import org.hibernate.jpa.AvailableSettings; import org.hibernate.jpa.test.Wallet; @@ -18,7 +17,6 @@ import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Setting; import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -32,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.fail; @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) @Jpa( annotatedClasses = { Wallet.class }, - integrationSettings = { @Setting( name = AvailableSettings.DISCARD_PC_ON_CLOSE, value = "true") }, + integrationSettings = { @Setting(name = AvailableSettings.DISCARD_PC_ON_CLOSE, value = "true") }, nonStringValueSettingProviders = { PreparedStatementSpyConnectionProviderSettingValueProvider.class } ) public class EnableDiscardPersistenceContextOnCloseTest { @@ -59,6 +57,8 @@ public class EnableDiscardPersistenceContextOnCloseTest { entityManager.close(); assertEquals( 0, connectionProvider.getAcquiredConnections().size() ); assertTrue( entityManager.getTransaction().isActive() ); + } + finally { try { entityManager.getTransaction().rollback(); fail( "Should throw IllegalStateException because the Connection is already closed!" ); @@ -66,12 +66,6 @@ public class EnableDiscardPersistenceContextOnCloseTest { catch (IllegalStateException expected) { } } - catch (Exception e) { - if ( entityManager.getTransaction().isActive() ) { - entityManager.getTransaction().rollback(); - } - throw e; - } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java index 6d186add28..548a9cf817 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java @@ -47,18 +47,23 @@ public class FindTest { } @Test - @TestForIssue( jiraKey = "HHH-9856" ) + @TestForIssue(jiraKey = "HHH-9856") public void testNonEntity(EntityManagerFactoryScope scope) { scope.inEntityManager( entityManager -> { - entityManager.getTransaction().begin(); try { + entityManager.getTransaction().begin(); entityManager.find( String.class, 1 ); Assertions.fail( "Expecting a failure" ); } catch (IllegalArgumentException ignore) { // expected } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java index ae3fb49b04..a5ce50d6a7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java @@ -49,9 +49,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse; AbstractJtaBatchTest.Comment.class, AbstractJtaBatchTest.EventLog.class }, - integrationSettings = { @Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA"), + integrationSettings = { + @Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA"), @Setting(name = AvailableSettings.JPA_TRANSACTION_COMPLIANCE, value = "true"), - @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50") }, + @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50") + }, nonStringValueSettingProviders = { AbstractJtaBatchTest.JtaPlatformNonStringValueSettingProvider.class, AbstractJtaBatchTest.ConnectionNonStringValueSettingProvider.class, @@ -86,39 +88,35 @@ public class JtaWithFailingBatchTest extends AbstractJtaBatchTest { Comment comment = new Comment(); comment.setMessage( "Bar" ); + em.persist( comment ); + transactionManager.commit(); + } + catch (Exception expected) { + //expected try { - em.persist( comment ); - transactionManager.commit(); - } - catch (Exception expected) { - //expected switch ( transactionManager.getStatus() ) { case Status.STATUS_ACTIVE: case Status.STATUS_MARKED_ROLLBACK: transactionManager.rollback(); } } - - assertThat( - "AbstractBatchImpl#releaseStatements() has not been callled", - testBatch.calledReleaseStatements, - is( true ) - ); - assertAllStatementsAreClosed( testBatch.createdStatements ); - assertStatementsListIsCleared(); - } - catch (Exception e) { - try { - if (transactionManager.getStatus() == Status.STATUS_ACTIVE) { - transactionManager.rollback(); - } - } - catch (Exception e2) { - // Ignore + catch (Exception e) { + //ignore e } } - assertFalse( triggerable.wasTriggered(), "HHH000352: Unable to release batch statement... has been thrown" ); + assertThat( + "AbstractBatchImpl#releaseStatements() has not been callled", + testBatch.calledReleaseStatements, + is( true ) + ); + assertAllStatementsAreClosed( testBatch.createdStatements ); + assertStatementsListIsCleared(); + + assertFalse( + triggerable.wasTriggered(), + "HHH000352: Unable to release batch statement... has been thrown" + ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java index 776b6e9ccf..e3e63177d7 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java @@ -10,7 +10,9 @@ import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; import javax.persistence.FlushModeType; +import javax.transaction.NotSupportedException; import javax.transaction.Status; +import javax.transaction.SystemException; import javax.transaction.TransactionManager; import org.hibernate.cfg.AvailableSettings; @@ -93,12 +95,13 @@ public class JtaWithStatementsBatchTest extends AbstractJtaBatchTest { } catch (Exception e) { try { - if ( transactionManager.getStatus() == Status.STATUS_ACTIVE ) { - transactionManager.rollback(); + switch ( transactionManager.getStatus() ) { + case Status.STATUS_ACTIVE: + case Status.STATUS_MARKED_ROLLBACK: + transactionManager.rollback(); } - } - catch (Exception e2) { - // Ignore + }catch (Exception e2){ + //ignore e } } @@ -123,12 +126,24 @@ public class JtaWithStatementsBatchTest extends AbstractJtaBatchTest { } catch (Exception e) { try { - if ( transactionManager.getStatus() == Status.STATUS_ACTIVE ) { - transactionManager.rollback(); + switch ( transactionManager.getStatus() ) { + case Status.STATUS_ACTIVE: + case Status.STATUS_MARKED_ROLLBACK: + transactionManager.rollback(); } + }catch (Exception e2){ + //ignore e } - catch (Exception e2) { - // Ignore + } + finally { + try { + switch ( transactionManager.getStatus() ) { + case Status.STATUS_ACTIVE: + case Status.STATUS_MARKED_ROLLBACK: + transactionManager.rollback(); + } + }catch (Exception e){ + //ignore e } } }