From 1ced09140961868ef31a7c5afe266df51f47e5bf Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 8 Mar 2017 15:07:05 +0000 Subject: [PATCH] HHH-11559 - Fix tests catching exceptions without re-throwing them --- .../jpa/test/criteria/ParameterTest.java | 33 +-- .../embeddables/EmbeddableIntegratorTest.java | 4 +- .../annotations/embedded/EmbeddedTest.java | 234 +++++------------- .../secondarytable/SecondaryTableTest.java | 62 +++-- .../empty/EmptyCompositesDirtynessTest.java | 31 ++- ...ptyInitializedCompositesDirtynessTest.java | 27 +- .../empty/EmptyInitializedCompositesTest.java | 31 ++- .../joined/JoinedSubclassInheritanceTest.java | 28 +-- .../org/hibernate/test/legacy/FooBarTest.java | 1 + .../locking/warning/LockNoneWarmingTest.java | 26 +- .../SynonymValidationTest.java | 36 +-- .../schemavalidation/ViewValidationTest.java | 36 +-- 12 files changed, 193 insertions(+), 356 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/ParameterTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/ParameterTest.java index 1b10c45166..2314667918 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/ParameterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/ParameterTest.java @@ -9,8 +9,8 @@ package org.hibernate.jpa.test.criteria; import javax.persistence.EntityManager; import javax.persistence.Parameter; import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Expression; import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Path; import javax.persistence.criteria.Root; @@ -22,6 +22,7 @@ import org.junit.Test; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.transaction.TransactionUtil; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; @@ -122,32 +123,20 @@ public class ParameterTest extends BaseEntityManagerFunctionalTestCase { @Test @TestForIssue(jiraKey = "HHH-10870") public void testParameterInParameterList2() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - final CriteriaQuery query = em.getCriteriaBuilder() + TransactionUtil.doInJPA( this::entityManagerFactory, em -> { + final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + final CriteriaQuery criteria = criteriaBuilder .createQuery( MultiTypedBasicAttributesEntity.class ); - final Root root = query.from( MultiTypedBasicAttributesEntity.class ); - root.get( "id" ); - final ParameterExpression parameter = em.getCriteriaBuilder().parameter( Iterable.class ); - root.in( new Expression[] {parameter} ); - query.select( root ); + final ParameterExpression parameter = criteriaBuilder.parameter( Iterable.class ); - final TypedQuery query1 = em.createQuery( query ); + final Root root = criteria.from( MultiTypedBasicAttributesEntity.class ); + criteria.select( root ).where( root.get( "id" ).in( parameter ) ); + + final TypedQuery query1 = em.createQuery( criteria ); query1.setParameter( parameter, Arrays.asList( 1L, 2L, 3L ) ); query1.getResultList(); - - em.getTransaction().commit(); - } - catch (Exception e) { - if ( em.getTransaction().isActive() ) { - em.getTransaction().rollback(); - } - } - finally { - em.close(); - } + } ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java index 3e265257d3..fb8b8e6cd5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embeddables/EmbeddableIntegratorTest.java @@ -86,12 +86,12 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase { Investor inv = (Investor) sess.get( Investor.class, 2L ); assertEquals( new BigDecimal( "100" ), inv.getInvestments().get( 0 ).getAmount().getAmount() ); - - sess.close(); }catch (Exception e){ sess.getTransaction().rollback(); + throw e; } finally { + sess.close(); sf.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java index 1b59d47c9d..83cdea6f8e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/embedded/EmbeddedTest.java @@ -22,6 +22,7 @@ import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; import org.hibernate.testing.FailureExpected; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.transaction.TransactionUtil; import org.hibernate.test.annotations.embedded.FloatLeg.RateIndex; import org.hibernate.test.annotations.embedded.Leg.Frequency; import org.hibernate.test.util.SchemaUtil; @@ -40,9 +41,7 @@ import static org.junit.Assert.assertTrue; public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { @Test public void testSimple() throws Exception { - Session s; - Transaction tx; - Person p = new Person(); + Person person = new Person(); Address a = new Address(); Country c = new Country(); Country bornCountry = new Country(); @@ -54,28 +53,16 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { a.address1 = "colorado street"; a.city = "Springfield"; a.country = c; - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( p ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - p = (Person) s.get( Person.class, p.id ); + TransactionUtil.doInHibernate( this::sessionFactory, session ->{ + session.persist( person ); + } ); + + TransactionUtil.doInHibernate( this::sessionFactory, session ->{ + Person p = session.get( Person.class, person.id ); assertNotNull( p ); assertNotNull( p.address ); assertEquals( "Springfield", p.address.city ); @@ -83,24 +70,13 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { assertEquals( "DM", p.address.country.getIso2() ); assertNotNull( p.bornIn ); assertEquals( "US", p.bornIn.getIso2() ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + } ); } @Test @TestForIssue(jiraKey = "HHH-8172") public void testQueryWithEmbeddedIsNull() throws Exception { - Session s; - Transaction tx; - Person p = new Person(); + Person person = new Person(); Address a = new Address(); Country c = new Country(); Country bornCountry = new Country(); @@ -112,44 +88,22 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { a.address1 = "colorado street"; a.city = "Springfield"; a.country = c; - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( p ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.persist( person ); + } ); - s = openSession(); - tx = s.beginTransaction(); - try { - p = (Person) s.createQuery( "from Person p where p.bornIn is null" ).uniqueResult(); + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn is null" ).uniqueResult(); assertNotNull( p ); assertNotNull( p.address ); assertEquals( "Springfield", p.address.city ); assertNotNull( p.address.country ); assertEquals( "DM", p.address.country.getIso2() ); assertNull( p.bornIn ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + } ); } @Test @@ -158,7 +112,7 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { public void testQueryWithEmbeddedParameterAllNull() throws Exception { Session s; Transaction tx; - Person p = new Person(); + Person person = new Person(); Address a = new Address(); Country c = new Country(); Country bornCountry = new Country(); @@ -168,29 +122,17 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { a.address1 = "colorado street"; a.city = "Springfield"; a.country = c; - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( p ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ) - .setParameter( "b", p.bornIn ) + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.persist( person ); + } ); + + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) + .setParameter( "b", person.bornIn ) .uniqueResult(); assertNotNull( p ); assertNotNull( p.address ); @@ -198,25 +140,14 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { assertNotNull( p.address.country ); assertEquals( "DM", p.address.country.getIso2() ); assertNull( p.bornIn ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + } ); } @Test @TestForIssue(jiraKey = "HHH-8172") @FailureExpected(jiraKey = "HHH-8172") public void testQueryWithEmbeddedParameterOneNull() throws Exception { - Session s; - Transaction tx; - Person p = new Person(); + Person person = new Person(); Address a = new Address(); Country c = new Country(); Country bornCountry = new Country(); @@ -228,29 +159,17 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { a.address1 = "colorado street"; a.city = "Springfield"; a.country = c; - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( p ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - p = (Person) s.createQuery( "from Person p where p.bornIn = :b" ) - .setParameter( "b", p.bornIn ) + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.persist( person ); + } ); + + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" ) + .setParameter( "b", person.bornIn ) .uniqueResult(); assertNotNull( p ); assertNotNull( p.address ); @@ -259,24 +178,13 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "US", p.bornIn.getIso2() ); assertNull( p.bornIn.getName() ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + } ); } @Test @TestForIssue(jiraKey = "HHH-8172") public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception { - Session s; - Transaction tx; - Person p = new Person(); + Person person = new Person(); Address a = new Address(); Country c = new Country(); Country bornCountry = new Country(); @@ -288,32 +196,19 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { a.address1 = "colorado street"; a.city = "Springfield"; a.country = c; - p.address = a; - p.bornIn = bornCountry; - p.name = "Homer"; - s = openSession(); - tx = s.beginTransaction(); - try { - s.persist( p ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + person.address = a; + person.bornIn = bornCountry; + person.name = "Homer"; + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.persist( person ); + } ); - s = openSession(); - tx = s.beginTransaction(); - try { - p = (Person) s.createQuery( "from Person p " + - "where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " + - "( p.bornIn.name is null or p.bornIn.name = :n )" - ).setParameter( "i", p.bornIn.getIso2() ) - .setParameter( "n", p.bornIn.getName() ) + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + Person p = (Person) session.createQuery( "from Person p " + + "where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " + + "( p.bornIn.name is null or p.bornIn.name = :n )" + ).setParameter( "i", person.bornIn.getIso2() ) + .setParameter( "n", person.bornIn.getName() ) .uniqueResult(); assertNotNull( p ); assertNotNull( p.address ); @@ -322,16 +217,7 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "US", p.bornIn.getIso2() ); assertNull( p.bornIn.getName() ); - tx.commit(); - } - catch (Exception e) { - if ( tx != null && tx.isActive() ) { - tx.rollback(); - } - } - finally { - s.close(); - } + } ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/filter/secondarytable/SecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/filter/secondarytable/SecondaryTableTest.java index 27cb44e5d6..5ff4405d06 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/filter/secondarytable/SecondaryTableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/filter/secondarytable/SecondaryTableTest.java @@ -6,9 +6,11 @@ */ package org.hibernate.test.annotations.filter.secondarytable; + import org.hibernate.Session; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.transaction.TransactionUtil; import org.junit.Assert; import org.junit.Test; @@ -21,35 +23,43 @@ public class SecondaryTableTest extends BaseCoreFunctionalTestCase { @Override protected void prepareTest() throws Exception { - Session s = openSession(); - s.beginTransaction(); - try { - insertUser( "q@s.com", 21, false, "a1", "b" ); - insertUser( "r@s.com", 22, false, "a2", "b" ); - insertUser( "s@s.com", 23, true, "a3", "b" ); - insertUser( "t@s.com", 24, false, "a4", "b" ); - session.flush(); - s.getTransaction().commit(); - }catch (Exception e){ - s.getTransaction().rollback(); + TransactionUtil.doInHibernate( this::sessionFactory, s -> { + insertUser( s, "q@s.com", 21, false, "a1", "b" ); + insertUser( s, "r@s.com", 22, false, "a2", "b" ); + insertUser( s, "s@s.com", 23, true, "a3", "b" ); + insertUser( s, "t@s.com", 24, false, "a4", "b" ); + } ); + } + + @Test + public void testFilter() { + try (Session session = openSession()) { + Assert.assertEquals( + Long.valueOf( 4 ), + session.createQuery( "select count(u) from User u" ).uniqueResult() + ); + session.enableFilter( "ageFilter" ).setParameter( "age", 24 ); + Assert.assertEquals( + Long.valueOf( 2 ), + session.createQuery( "select count(u) from User u" ).uniqueResult() + ); } } - - @Test - public void testFilter(){ - Assert.assertEquals(Long.valueOf(4), session.createQuery("select count(u) from User u").uniqueResult()); - session.enableFilter("ageFilter").setParameter("age", 24); - Assert.assertEquals(Long.valueOf(2), session.createQuery("select count(u) from User u").uniqueResult()); - } - - private void insertUser(String emailAddress, int age, boolean lockedOut, String username, String password){ + + private void insertUser( + Session session, + String emailAddress, + int age, + boolean lockedOut, + String username, + String password) { User user = new User(); - user.setEmailAddress(emailAddress); - user.setAge(age); - user.setLockedOut(lockedOut); - user.setUsername(username); - user.setPassword(password); - session.persist(user); + user.setEmailAddress( emailAddress ); + user.setAge( age ); + user.setLockedOut( lockedOut ); + user.setUsername( username ); + user.setPassword( password ); + session.persist( user ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyCompositesDirtynessTest.java b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyCompositesDirtynessTest.java index 77d2bdf146..82615282be 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyCompositesDirtynessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyCompositesDirtynessTest.java @@ -41,23 +41,28 @@ public class EmptyCompositesDirtynessTest extends BaseCoreFunctionalTestCase { @TestForIssue(jiraKey = "HHH-7610") public void testCompositesEmpty() { Session s = openSession(); - s.getTransaction().begin(); + try { + s.getTransaction().begin(); - ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); - s.persist( owner ); + ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); + s.persist( owner ); - s.flush(); - s.getTransaction().commit(); + s.flush(); + s.getTransaction().commit(); - s.clear(); - s.getTransaction().begin(); - owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); - assertNull( owner.getEmbedded() ); - owner.setEmbedded( new ComponentEmptyEmbedded() ); + s.clear(); + s.getTransaction().begin(); + owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); + assertNull( owner.getEmbedded() ); + owner.setEmbedded( new ComponentEmptyEmbedded() ); - // technically, as all properties are null, update may not be necessary - assertFalse( session.isDirty() ); // must be false to avoid unnecessary updates + // technically, as all properties are null, update may not be necessary + assertFalse( session.isDirty() ); // must be false to avoid unnecessary updates - s.getTransaction().rollback(); + s.getTransaction().rollback(); + } + finally { + s.close(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesDirtynessTest.java b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesDirtynessTest.java index 418430c137..f126d5982c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesDirtynessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesDirtynessTest.java @@ -42,20 +42,25 @@ public class EmptyInitializedCompositesDirtynessTest extends BaseCoreFunctionalT @TestForIssue(jiraKey = "HHH-7610") public void testInitializedCompositesEmpty() { Session s = openSession(); - s.getTransaction().begin(); + try { + s.getTransaction().begin(); - ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); - s.persist( owner ); + ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); + s.persist( owner ); - s.flush(); - s.getTransaction().commit(); + s.flush(); + s.getTransaction().commit(); - s.clear(); - s.getTransaction().begin(); - owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); - assertNotNull( owner.getEmbedded() ); - assertFalse( s.isDirty() ); + s.clear(); + s.getTransaction().begin(); + owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); + assertNotNull( owner.getEmbedded() ); + assertFalse( s.isDirty() ); - s.getTransaction().rollback(); + s.getTransaction().rollback(); + } + finally { + s.close(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesTest.java b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesTest.java index 322835284e..cf2c77a4b0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedCompositesTest.java @@ -41,23 +41,28 @@ public class EmptyInitializedCompositesTest extends BaseCoreFunctionalTestCase { @TestForIssue(jiraKey = "HHH-7610") public void testCompositesEmpty() { Session s = openSession(); - s.getTransaction().begin(); + try { + s.getTransaction().begin(); - ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); - s.persist( owner ); + ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); + s.persist( owner ); - s.flush(); - s.getTransaction().commit(); + s.flush(); + s.getTransaction().commit(); - s.clear(); - s.getTransaction().begin(); - owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); - assertNotNull( owner.getEmbedded() ); - assertFalse( s.isDirty() ); + s.clear(); + s.getTransaction().begin(); + owner = (ComponentEmptyEmbeddedOwner) s.get( ComponentEmptyEmbeddedOwner.class, owner.getId() ); + assertNotNull( owner.getEmbedded() ); + assertFalse( s.isDirty() ); - owner.setEmbedded( null ); - assertFalse( s.isDirty() ); // must be false to avoid unnecessary updates + owner.setEmbedded( null ); + assertFalse( s.isDirty() ); // must be false to avoid unnecessary updates - s.getTransaction().rollback(); + s.getTransaction().rollback(); + } + finally { + s.close(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java index c0b720212b..7478d2ad7f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/discriminator/joined/JoinedSubclassInheritanceTest.java @@ -6,9 +6,9 @@ */ package org.hibernate.test.discriminator.joined; -import org.hibernate.Session; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.transaction.TransactionUtil; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -21,30 +21,20 @@ public class JoinedSubclassInheritanceTest extends BaseCoreFunctionalTestCase { @Override protected String[] getMappings() { - return new String[] { "discriminator/joined/JoinedSubclassInheritance.hbm.xml" }; + return new String[] {"discriminator/joined/JoinedSubclassInheritance.hbm.xml"}; } @Test public void testConfiguredDiscriminatorValue() { - Session session = openSession(); - try { - ChildEntity ce = new ChildEntity( 1, "Child" ); - session.getTransaction().begin(); - session.save( ce ); - session.getTransaction().commit(); - session.clear(); + final ChildEntity childEntity = new ChildEntity( 1, "Child" ); + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.save( childEntity ); + } ); - ce = (ChildEntity) session.find( ChildEntity.class, 1 ); + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + ChildEntity ce = session.find( ChildEntity.class, 1 ); assertEquals( "ce", ce.getType() ); - } - catch ( Exception e ) { - if ( session.getTransaction().isActive() ) { - session.getTransaction().rollback(); - } - } - finally { - session.close(); - } + } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java index 053c27bb42..26f4ad52e6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/legacy/FooBarTest.java @@ -4053,6 +4053,7 @@ public class FooBarTest extends LegacyTestCase { } catch (Exception e) { s.getTransaction().rollback(); + throw e; } finally { s.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/locking/warning/LockNoneWarmingTest.java b/hibernate-core/src/test/java/org/hibernate/test/locking/warning/LockNoneWarmingTest.java index 74574888b9..c3e7492a04 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/locking/warning/LockNoneWarmingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/locking/warning/LockNoneWarmingTest.java @@ -24,7 +24,6 @@ import org.jboss.logging.Logger; import org.hibernate.LockMode; import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.loader.Loader; import org.hibernate.query.Query; @@ -39,6 +38,7 @@ import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.logger.LoggerInspectionRule; import org.hibernate.testing.logger.Triggerable; +import org.hibernate.testing.transaction.TransactionUtil; import static org.junit.Assert.assertFalse; @@ -61,27 +61,17 @@ public class LockNoneWarmingTest extends BaseCoreFunctionalTestCase { } @Before - public void setUp(){ + public void setUp() { buildSessionFactory(); - final Set messagesPrefixes = new HashSet<>( ); + final Set messagesPrefixes = new HashSet<>(); messagesPrefixes.add( "HHH000444" ); messagesPrefixes.add( "HHH000445" ); triggerable = logInspection.watchForLogMessages( messagesPrefixes ); - try (Session s = openSession();) { - Transaction tx = s.beginTransaction(); - try { - Item item = new Item(); - item.name = "ZZZZ"; - s.persist( item ); - - tx.commit(); - } - catch (Exception e) { - if ( tx.isActive() ) { - tx.rollback(); - } - } - } + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + Item item = new Item(); + item.name = "ZZZZ"; + session.persist( item ); + } ); } @After diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/SynonymValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/SynonymValidationTest.java index 4bb971fd38..b1e0644011 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/SynonymValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/SynonymValidationTest.java @@ -12,7 +12,6 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; -import org.hibernate.Session; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @@ -23,6 +22,7 @@ import org.hibernate.tool.schema.JdbcMetadaAccessStrategy; import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.testing.transaction.TransactionUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -47,38 +47,16 @@ public class SynonymValidationTest extends BaseNonConfigCoreFunctionalTestCase { @Before public void setUp() { - Session s = openSession(); - try { - s.getTransaction().begin(); - s.createSQLQuery( "CREATE SYNONYM test_synonym FOR test_entity" ).executeUpdate(); - s.getTransaction().commit(); - } - catch (Exception e) { - if ( s.getTransaction().isActive() ) { - s.getTransaction().rollback(); - } - } - finally { - s.close(); - } + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.createSQLQuery( "CREATE SYNONYM test_synonym FOR test_entity" ).executeUpdate(); + } ); } @After public void tearDown() { - Session s = openSession(); - try { - s.getTransaction().begin(); - s.createSQLQuery( "DROP SYNONYM test_synonym FORCE" ).executeUpdate(); - s.getTransaction().commit(); - } - catch (Exception e) { - if ( s.getTransaction().isActive() ) { - s.getTransaction().rollback(); - } - } - finally { - s.close(); - } + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.createSQLQuery( "DROP SYNONYM test_synonym FORCE" ).executeUpdate(); + }); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/ViewValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/ViewValidationTest.java index a92a68dccd..7fff116b17 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/ViewValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/ViewValidationTest.java @@ -12,7 +12,6 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; -import org.hibernate.Session; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @@ -25,6 +24,7 @@ import org.hibernate.tool.schema.JdbcMetadaAccessStrategy; import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialects; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.transaction.TransactionUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -46,38 +46,16 @@ public class ViewValidationTest extends BaseCoreFunctionalTestCase { @Before public void setUp() { - Session s = openSession(); - try { - s.getTransaction().begin(); - s.createSQLQuery( "CREATE VIEW test_synonym AS SELECT * FROM test_entity" ).executeUpdate(); - s.getTransaction().commit(); - } - catch (Exception e) { - if ( s.getTransaction().isActive() ) { - s.getTransaction().rollback(); - } - } - finally { - s.close(); - } + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.createSQLQuery( "CREATE VIEW test_synonym AS SELECT * FROM test_entity" ).executeUpdate(); + } ); } @After public void tearDown() { - Session s = openSession(); - try { - s.getTransaction().begin(); - s.createSQLQuery( "DROP VIEW test_synonym CASCADE" ).executeUpdate(); - s.getTransaction().commit(); - } - catch (Exception e) { - if ( s.getTransaction().isActive() ) { - s.getTransaction().rollback(); - } - } - finally { - s.close(); - } + TransactionUtil.doInHibernate( this::sessionFactory, session -> { + session.createSQLQuery( "DROP VIEW test_synonym CASCADE" ).executeUpdate(); + } ); } @Test