From 57e63081b783156b9ffe1235e0fadfc19258f13b Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 1 Sep 2017 15:57:03 -0700 Subject: [PATCH] HHH-11957 : Fix test case to work pre-5.2 --- .../test/hql/DB297SubStringFunctionsTest.java | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/DB297SubStringFunctionsTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/DB297SubStringFunctionsTest.java index 74086582c0..6a83bf8f18 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/DB297SubStringFunctionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/DB297SubStringFunctionsTest.java @@ -11,6 +11,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.PersistenceException; +import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.DB297Dialect; @@ -25,7 +26,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -57,20 +57,24 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase { AnEntity anEntity = new AnEntity(); anEntity.description = "A very long, boring description."; - doInHibernate( - this::sessionFactory, session -> { - session.persist( anEntity ); - } - ); + Session session = openSession(); + session.beginTransaction(); + { + session.persist( anEntity ); + } + session.getTransaction().commit(); + session.close(); } @After public void cleanup() { - doInHibernate( - this::sessionFactory, session -> { + Session session = openSession(); + session.beginTransaction(); + { session.createQuery( "delete from AnEntity" ).executeUpdate(); - } - ); + } + session.getTransaction().commit(); + session.close(); } @Test @@ -79,15 +83,16 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase { mostRecentStatementInspector.clear(); - doInHibernate( - this::sessionFactory, session -> { - String value = session.createQuery( - "select substring( e.description, 21, 11, octets ) from AnEntity e", - String.class + Session session = openSession(); + session.beginTransaction(); + { + String value = (String) session.createQuery( + "select substring( e.description, 21, 11, octets ) from AnEntity e" ).uniqueResult(); assertEquals( "description", value ); - } - ); + } + session.getTransaction().commit(); + session.close(); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substring(" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "octets" ) ); @@ -99,15 +104,17 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase { mostRecentStatementInspector.clear(); - doInHibernate( - this::sessionFactory, session -> { - String value = session.createQuery( - "select substring( e.description, 21, 11 ) from AnEntity e", - String.class + Session session = openSession(); + session.beginTransaction(); + { + String value = (String) session.createQuery( + "select substring( e.description, 21, 11 ) from AnEntity e" ).uniqueResult(); assertEquals( "description", value ); - } - ); + } + session.getTransaction().commit(); + session.close(); + assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); } @@ -117,20 +124,22 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase { mostRecentStatementInspector.clear(); + Session session = openSession(); + session.beginTransaction(); + try { - doInHibernate( - this::sessionFactory, session -> { - String value = session.createQuery( - "select substr( e.description, 21, 11, octets ) from AnEntity e", - String.class + String value = (String) session.createQuery( + "select substr( e.description, 21, 11, octets ) from AnEntity e" ).uniqueResult(); assertEquals( "description", value ); - } - ); - fail( "Should have failed because substr cannot be used with string units." ); + fail( "Should have failed because substr cannot be used with string units." ); } - catch (PersistenceException expected) { - assertTrue( SQLGrammarException.class.isInstance( expected.getCause() ) ); + catch (SQLGrammarException expected) { + // expected + } + finally { + session.getTransaction().rollback(); + session.close(); } assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); @@ -143,15 +152,17 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase { mostRecentStatementInspector.clear(); - doInHibernate( - this::sessionFactory, session -> { - String value = session.createQuery( - "select substr( e.description, 21, 11 ) from AnEntity e", - String.class + Session session = openSession(); + session.beginTransaction(); + { + String value = (String) session.createQuery( + "select substr( e.description, 21, 11 ) from AnEntity e" ).uniqueResult(); assertEquals( "description", value ); - } - ); + } + session.getTransaction().commit(); + session.close(); + assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); }