From db3346962ac5d9c3cf31946a283d0acba7f19dd5 Mon Sep 17 00:00:00 2001 From: boris-unckel Date: Thu, 29 Jul 2021 15:57:54 +0200 Subject: [PATCH] HHH-14764 Testsuite: AssertionErrors must not be caught Fixes https://hibernate.atlassian.net/browse/HHH-14764 --- .../ConnectionManagementTestCase.java | 31 ++++++++++--------- .../orm/test/jpa/lock/JPALockTest.java | 19 ++++++++++-- .../transaction/TransactionRollbackTest.java | 3 ++ .../mutable/MutableNaturalIdTest.java | 18 +++++------ .../hqlfetchscroll/HQLScrollFetchTest.java | 17 +++++++--- .../test/cache/jcache/HibernateCacheTest.java | 3 ++ 6 files changed, 60 insertions(+), 31 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java index 7caee62810..952fd5b279 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionManagementTestCase.java @@ -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) { } } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/lock/JPALockTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/lock/JPALockTest.java index 6ff351d11d..e85298029f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/lock/JPALockTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/lock/JPALockTest.java @@ -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(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java index 49b94f23c5..dcb3e85608 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java @@ -91,6 +91,9 @@ public class TransactionRollbackTest { if ( entityManager.getTransaction().isActive() ) { entityManager.getTransaction().rollback(); } + if ( e instanceof AssertionError ) { + throw (AssertionError) e; + } } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/mutable/MutableNaturalIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/mutable/MutableNaturalIdTest.java index 85980555e9..b30720696c 100755 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/mutable/MutableNaturalIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/mutable/MutableNaturalIdTest.java @@ -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) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java b/hibernate-core/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java index 98dfe9a1f6..c842e6a92c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java @@ -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(); diff --git a/hibernate-jcache/src/test/java/org/hibernate/test/cache/jcache/HibernateCacheTest.java b/hibernate-jcache/src/test/java/org/hibernate/test/cache/jcache/HibernateCacheTest.java index 80bf768f9f..a71b0db4bf 100644 --- a/hibernate-jcache/src/test/java/org/hibernate/test/cache/jcache/HibernateCacheTest.java +++ b/hibernate-jcache/src/test/java/org/hibernate/test/cache/jcache/HibernateCacheTest.java @@ -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() ) {