HHH-11957 : Fix test case to work pre-5.2

(cherry picked from commit 57e63081b7)
This commit is contained in:
Gail Badner 2017-09-01 15:57:03 -07:00
parent a983c9c37b
commit 09a278a1b9
1 changed files with 52 additions and 41 deletions

View File

@ -11,6 +11,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.dialect.DB297Dialect; import org.hibernate.dialect.DB297Dialect;
@ -25,7 +26,6 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -57,20 +57,24 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase {
AnEntity anEntity = new AnEntity(); AnEntity anEntity = new AnEntity();
anEntity.description = "A very long, boring description."; anEntity.description = "A very long, boring description.";
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.persist( anEntity ); session.persist( anEntity );
} }
); session.getTransaction().commit();
session.close();
} }
@After @After
public void cleanup() { public void cleanup() {
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
{
session.createQuery( "delete from AnEntity" ).executeUpdate(); session.createQuery( "delete from AnEntity" ).executeUpdate();
} }
); session.getTransaction().commit();
session.close();
} }
@Test @Test
@ -79,15 +83,16 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase {
mostRecentStatementInspector.clear(); mostRecentStatementInspector.clear();
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
String value = session.createQuery( {
"select substring( e.description, 21, 11, octets ) from AnEntity e", String value = (String) session.createQuery(
String.class "select substring( e.description, 21, 11, octets ) from AnEntity e"
).uniqueResult(); ).uniqueResult();
assertEquals( "description", value ); assertEquals( "description", value );
} }
); session.getTransaction().commit();
session.close();
assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substring(" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substring(" ) );
assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "octets" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "octets" ) );
@ -99,15 +104,17 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase {
mostRecentStatementInspector.clear(); mostRecentStatementInspector.clear();
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
String value = session.createQuery( {
"select substring( e.description, 21, 11 ) from AnEntity e", String value = (String) session.createQuery(
String.class "select substring( e.description, 21, 11 ) from AnEntity e"
).uniqueResult(); ).uniqueResult();
assertEquals( "description", value ); assertEquals( "description", value );
} }
); session.getTransaction().commit();
session.close();
assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) );
} }
@ -117,20 +124,22 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase {
mostRecentStatementInspector.clear(); mostRecentStatementInspector.clear();
Session session = openSession();
session.beginTransaction();
try { try {
doInHibernate( String value = (String) session.createQuery(
this::sessionFactory, session -> { "select substr( e.description, 21, 11, octets ) from AnEntity e"
String value = session.createQuery(
"select substr( e.description, 21, 11, octets ) from AnEntity e",
String.class
).uniqueResult(); ).uniqueResult();
assertEquals( "description", value ); 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) { catch (SQLGrammarException expected) {
assertTrue( SQLGrammarException.class.isInstance( expected.getCause() ) ); // expected
}
finally {
session.getTransaction().rollback();
session.close();
} }
assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) );
@ -143,15 +152,17 @@ public class DB297SubStringFunctionsTest extends BaseCoreFunctionalTestCase {
mostRecentStatementInspector.clear(); mostRecentStatementInspector.clear();
doInHibernate( Session session = openSession();
this::sessionFactory, session -> { session.beginTransaction();
String value = session.createQuery( {
"select substr( e.description, 21, 11 ) from AnEntity e", String value = (String) session.createQuery(
String.class "select substr( e.description, 21, 11 ) from AnEntity e"
).uniqueResult(); ).uniqueResult();
assertEquals( "description", value ); assertEquals( "description", value );
} }
); session.getTransaction().commit();
session.close();
assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) ); assertTrue( mostRecentStatementInspector.mostRecentSql.contains( "substr(" ) );
} }