HHH-14503 - Migration of tests from jpa/test to orm/test/jpa

This commit is contained in:
Andrea Boriero 2021-03-26 11:10:37 +01:00
parent e2225d8814
commit a7f242af03
5 changed files with 58 additions and 45 deletions

View File

@ -138,6 +138,7 @@ public class ForeignGeneratorResourceLocalTest {
EntityManager entityManager = null; EntityManager entityManager = null;
EntityTransaction txn = null; EntityTransaction txn = null;
try { try {
entityManager = scope.getEntityManagerFactory().createEntityManager(); entityManager = scope.getEntityManagerFactory().createEntityManager();
txn = entityManager.getTransaction(); txn = entityManager.getTransaction();

View File

@ -7,7 +7,6 @@
package org.hibernate.orm.test.jpa.ejb3configuration; package org.hibernate.orm.test.jpa.ejb3configuration;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManager;
import org.hibernate.jpa.AvailableSettings; import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.test.Wallet; 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.Jpa;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -59,6 +57,8 @@ public class EnableDiscardPersistenceContextOnCloseTest {
entityManager.close(); entityManager.close();
assertEquals( 0, connectionProvider.getAcquiredConnections().size() ); assertEquals( 0, connectionProvider.getAcquiredConnections().size() );
assertTrue( entityManager.getTransaction().isActive() ); assertTrue( entityManager.getTransaction().isActive() );
}
finally {
try { try {
entityManager.getTransaction().rollback(); entityManager.getTransaction().rollback();
fail( "Should throw IllegalStateException because the Connection is already closed!" ); fail( "Should throw IllegalStateException because the Connection is already closed!" );
@ -66,12 +66,6 @@ public class EnableDiscardPersistenceContextOnCloseTest {
catch (IllegalStateException expected) { catch (IllegalStateException expected) {
} }
} }
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
} }
); );

View File

@ -51,14 +51,19 @@ public class FindTest {
public void testNonEntity(EntityManagerFactoryScope scope) { public void testNonEntity(EntityManagerFactoryScope scope) {
scope.inEntityManager( scope.inEntityManager(
entityManager -> { entityManager -> {
entityManager.getTransaction().begin();
try { try {
entityManager.getTransaction().begin();
entityManager.find( String.class, 1 ); entityManager.find( String.class, 1 );
Assertions.fail( "Expecting a failure" ); Assertions.fail( "Expecting a failure" );
} }
catch (IllegalArgumentException ignore) { catch (IllegalArgumentException ignore) {
// expected // expected
} }
finally {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
}
} }
); );
} }

View File

@ -49,9 +49,11 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
AbstractJtaBatchTest.Comment.class, AbstractJtaBatchTest.Comment.class,
AbstractJtaBatchTest.EventLog.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.JPA_TRANSACTION_COMPLIANCE, value = "true"),
@Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50") }, @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50")
},
nonStringValueSettingProviders = { nonStringValueSettingProviders = {
AbstractJtaBatchTest.JtaPlatformNonStringValueSettingProvider.class, AbstractJtaBatchTest.JtaPlatformNonStringValueSettingProvider.class,
AbstractJtaBatchTest.ConnectionNonStringValueSettingProvider.class, AbstractJtaBatchTest.ConnectionNonStringValueSettingProvider.class,
@ -86,18 +88,22 @@ public class JtaWithFailingBatchTest extends AbstractJtaBatchTest {
Comment comment = new Comment(); Comment comment = new Comment();
comment.setMessage( "Bar" ); comment.setMessage( "Bar" );
try {
em.persist( comment ); em.persist( comment );
transactionManager.commit(); transactionManager.commit();
} }
catch (Exception expected) { catch (Exception expected) {
//expected //expected
try {
switch ( transactionManager.getStatus() ) { switch ( transactionManager.getStatus() ) {
case Status.STATUS_ACTIVE: case Status.STATUS_ACTIVE:
case Status.STATUS_MARKED_ROLLBACK: case Status.STATUS_MARKED_ROLLBACK:
transactionManager.rollback(); transactionManager.rollback();
} }
} }
catch (Exception e) {
//ignore e
}
}
assertThat( assertThat(
"AbstractBatchImpl#releaseStatements() has not been callled", "AbstractBatchImpl#releaseStatements() has not been callled",
@ -106,19 +112,11 @@ public class JtaWithFailingBatchTest extends AbstractJtaBatchTest {
); );
assertAllStatementsAreClosed( testBatch.createdStatements ); assertAllStatementsAreClosed( testBatch.createdStatements );
assertStatementsListIsCleared(); assertStatementsListIsCleared();
}
catch (Exception e) {
try {
if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
transactionManager.rollback();
}
}
catch (Exception e2) {
// Ignore
}
}
assertFalse( triggerable.wasTriggered(), "HHH000352: Unable to release batch statement... has been thrown" ); assertFalse(
triggerable.wasTriggered(),
"HHH000352: Unable to release batch statement... has been thrown"
);
} }
); );
} }

View File

@ -10,7 +10,9 @@ import java.sql.PreparedStatement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
import javax.transaction.NotSupportedException;
import javax.transaction.Status; import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
@ -93,12 +95,13 @@ public class JtaWithStatementsBatchTest extends AbstractJtaBatchTest {
} }
catch (Exception e) { catch (Exception e) {
try { try {
if ( transactionManager.getStatus() == Status.STATUS_ACTIVE ) { switch ( transactionManager.getStatus() ) {
case Status.STATUS_ACTIVE:
case Status.STATUS_MARKED_ROLLBACK:
transactionManager.rollback(); transactionManager.rollback();
} }
} }catch (Exception e2){
catch (Exception e2) { //ignore e
// Ignore
} }
} }
@ -123,12 +126,24 @@ public class JtaWithStatementsBatchTest extends AbstractJtaBatchTest {
} }
catch (Exception e) { catch (Exception e) {
try { try {
if ( transactionManager.getStatus() == Status.STATUS_ACTIVE ) { switch ( transactionManager.getStatus() ) {
case Status.STATUS_ACTIVE:
case Status.STATUS_MARKED_ROLLBACK:
transactionManager.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
} }
} }
} }