HHH-14764 Testsuite: AssertionErrors must not be caught

Fixes https://hibernate.atlassian.net/browse/HHH-14764
This commit is contained in:
boris-unckel 2021-07-29 15:57:54 +02:00 committed by Andrea Boriero
parent ebb91e44c3
commit db3346962a
6 changed files with 60 additions and 31 deletions

View File

@ -13,6 +13,7 @@ import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@ -313,28 +314,30 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
s.createQuery( "from Silly" ).list();
fail( "allowed to create query on closed session" );
}
catch( Throwable ignore ) {
catch (AssertionError testFailed) {
throw testFailed;
}
catch (Throwable ignore) {
}
try {
s.getTransaction();
fail( "allowed to access transaction on closed session" );
}
catch( Throwable ignore ) {
}
// you should be able to access the transaction on a closed EM. that is a change from what we used to do. we changed it
// to better align with JPA.
Transaction tran = s.getTransaction();
assertNotNull( tran );
try {
s.close();
fail( "allowed to close already closed session" );
}
catch( Throwable ignore ) {
}
// Session implements both AutoCloseable and Closeable
// Closable requires an idempotent behaviour, a closed resource must not throw an Exception
// when close is called twice.
s.close();
try {
s.isDirty();
fail( "allowed to check dirtiness of closed session" );
}
catch( Throwable ignore ) {
catch (AssertionError testFailed) {
throw testFailed;
}
catch (Throwable ignore) {
}
}
}

View File

@ -199,12 +199,25 @@ public class JPALockTest extends AbstractJPATest {
t2.commit();
fail( "optimistic lock should have failed" );
}
catch (Throwable ignore) {
catch (Throwable t) {
// expected behavior
t2.rollback();
try {
t2.rollback();
}
catch (Throwable ignore) {
// ignore
}
if ( t instanceof AssertionError ) {
throw (AssertionError) t;
}
}
finally {
s2.close();
try {
s2.close();
}
catch (Throwable ignore) {
// ignore
}
}
s1 = sessionFactory().openSession();

View File

@ -91,6 +91,9 @@ public class TransactionRollbackTest {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
if ( e instanceof AssertionError ) {
throw (AssertionError) e;
}
}
}
);

View File

@ -160,21 +160,19 @@ public class MutableNaturalIdTest {
.load();
assertNotNull( loaded );
}
catch( HibernateException expected ) {
session.getTransaction().markRollbackOnly();
}
catch( Throwable t ) {
catch (Throwable t) {
try {
session.getTransaction().markRollbackOnly();
}
catch ( Throwable ignore ) {
catch (Throwable ignore) {
// ignore
}
if ( t instanceof AssertionError ) {
throw (AssertionError) t;
}
fail();
}
}
);
}
} );
}
@Test
public void testNonexistentNaturalIdCache(SessionFactoryScope scope) {

View File

@ -257,6 +257,8 @@ public class HQLScrollFetchTest extends BaseCoreFunctionalTestCase {
s.close();
}
final String errMsg = "should have failed because data is ordered incorrectly.";
@Test
public void testScrollOrderChildrenDesc() {
Session s = openSession();
@ -271,12 +273,16 @@ public class HQLScrollFetchTest extends BaseCoreFunctionalTestCase {
while ( results.next() ) {
list.add( results.get( ) );
}
try {
assertResultFromAllUsers( list );
fail( "should have failed because data is ordered incorrectly." );
fail( errMsg );
}
catch ( AssertionError ex ) {
// expected
if ( errMsg.equalsIgnoreCase( ex.getMessage() ) ) {
throw ex;
}
// Other AssertionErrors expected
}
finally {
s.close();
@ -295,10 +301,13 @@ public class HQLScrollFetchTest extends BaseCoreFunctionalTestCase {
List results = s.createQuery( QUERY + " order by c.name desc" ).list();
try {
assertResultFromAllUsers( results );
fail( "should have failed because data is ordered incorrectly." );
fail( errMsg );
}
catch ( AssertionError ex ) {
// expected
if ( errMsg.equalsIgnoreCase( ex.getMessage() ) ) {
throw ex;
}
// Other AssertionErrors expected
}
finally {
s.close();

View File

@ -142,6 +142,9 @@ public class HibernateCacheTest extends BaseFunctionalTest {
catch ( Throwable ignore ) {
}
}
if ( expected instanceof AssertionError ) {
throw (AssertionError) expected;
}
}
finally {
if ( s != null && s.isOpen() ) {