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.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -313,27 +314,29 @@ public abstract class ConnectionManagementTestCase extends BaseNonConfigCoreFunc
s.createQuery( "from Silly" ).list(); s.createQuery( "from Silly" ).list();
fail( "allowed to create query on closed session" ); fail( "allowed to create query on closed session" );
} }
catch( Throwable ignore ) { catch (AssertionError testFailed) {
} throw testFailed;
try {
s.getTransaction();
fail( "allowed to access transaction on closed session" );
} }
catch (Throwable ignore) { catch (Throwable ignore) {
} }
try { // 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 );
// 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(); s.close();
fail( "allowed to close already closed session" );
}
catch( Throwable ignore ) {
}
try { try {
s.isDirty(); s.isDirty();
fail( "allowed to check dirtiness of closed session" ); fail( "allowed to check dirtiness of closed session" );
} }
catch (AssertionError testFailed) {
throw testFailed;
}
catch (Throwable ignore) { catch (Throwable ignore) {
} }
} }

View File

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

View File

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

View File

@ -160,22 +160,20 @@ public class MutableNaturalIdTest {
.load(); .load();
assertNotNull( loaded ); assertNotNull( loaded );
} }
catch( HibernateException expected ) {
session.getTransaction().markRollbackOnly();
}
catch (Throwable t) { catch (Throwable t) {
try { try {
session.getTransaction().markRollbackOnly(); session.getTransaction().markRollbackOnly();
} }
catch (Throwable ignore) { catch (Throwable ignore) {
// ignore
} }
fail(); if ( t instanceof AssertionError ) {
throw (AssertionError) t;
} }
} }
); } );
} }
@Test @Test
public void testNonexistentNaturalIdCache(SessionFactoryScope scope) { public void testNonexistentNaturalIdCache(SessionFactoryScope scope) {
final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();

View File

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

View File

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