HHH-11559 - Fix tests catching exceptions without re-throwing them

This commit is contained in:
Andrea Boriero 2017-03-08 15:07:05 +00:00
parent 17b8d2e294
commit 1ced091409
12 changed files with 193 additions and 356 deletions

View File

@ -9,8 +9,8 @@ package org.hibernate.jpa.test.criteria;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.Parameter; import javax.persistence.Parameter;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path; import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
@ -22,6 +22,7 @@ import org.junit.Test;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.transaction.TransactionUtil;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -122,32 +123,20 @@ public class ParameterTest extends BaseEntityManagerFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-10870") @TestForIssue(jiraKey = "HHH-10870")
public void testParameterInParameterList2() { public void testParameterInParameterList2() {
EntityManager em = getOrCreateEntityManager(); TransactionUtil.doInJPA( this::entityManagerFactory, em -> {
try { final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
em.getTransaction().begin(); final CriteriaQuery<MultiTypedBasicAttributesEntity> criteria = criteriaBuilder
final CriteriaQuery<MultiTypedBasicAttributesEntity> query = em.getCriteriaBuilder()
.createQuery( MultiTypedBasicAttributesEntity.class ); .createQuery( MultiTypedBasicAttributesEntity.class );
final Root<MultiTypedBasicAttributesEntity> root = query.from( MultiTypedBasicAttributesEntity.class ); final ParameterExpression<Iterable> parameter = criteriaBuilder.parameter( Iterable.class );
root.get( "id" );
final ParameterExpression<Iterable> parameter = em.getCriteriaBuilder().parameter( Iterable.class );
root.in( new Expression[] {parameter} );
query.select( root );
final TypedQuery<MultiTypedBasicAttributesEntity> query1 = em.createQuery( query ); final Root<MultiTypedBasicAttributesEntity> root = criteria.from( MultiTypedBasicAttributesEntity.class );
criteria.select( root ).where( root.get( "id" ).in( parameter ) );
final TypedQuery<MultiTypedBasicAttributesEntity> query1 = em.createQuery( criteria );
query1.setParameter( parameter, Arrays.asList( 1L, 2L, 3L ) ); query1.setParameter( parameter, Arrays.asList( 1L, 2L, 3L ) );
query1.getResultList(); query1.getResultList();
} );
em.getTransaction().commit();
}
catch (Exception e) {
if ( em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
}
finally {
em.close();
}
} }
@Override @Override

View File

@ -86,12 +86,12 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase {
Investor inv = (Investor) sess.get( Investor.class, 2L ); Investor inv = (Investor) sess.get( Investor.class, 2L );
assertEquals( new BigDecimal( "100" ), inv.getInvestments().get( 0 ).getAmount().getAmount() ); assertEquals( new BigDecimal( "100" ), inv.getInvestments().get( 0 ).getAmount().getAmount() );
sess.close();
}catch (Exception e){ }catch (Exception e){
sess.getTransaction().rollback(); sess.getTransaction().rollback();
throw e;
} }
finally { finally {
sess.close();
sf.close(); sf.close();
} }
} }

View File

@ -22,6 +22,7 @@ import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; 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.FloatLeg.RateIndex;
import org.hibernate.test.annotations.embedded.Leg.Frequency; import org.hibernate.test.annotations.embedded.Leg.Frequency;
import org.hibernate.test.util.SchemaUtil; import org.hibernate.test.util.SchemaUtil;
@ -40,9 +41,7 @@ import static org.junit.Assert.assertTrue;
public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase { public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
@Test @Test
public void testSimple() throws Exception { public void testSimple() throws Exception {
Session s; Person person = new Person();
Transaction tx;
Person p = new Person();
Address a = new Address(); Address a = new Address();
Country c = new Country(); Country c = new Country();
Country bornCountry = new Country(); Country bornCountry = new Country();
@ -54,28 +53,16 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
a.address1 = "colorado street"; a.address1 = "colorado street";
a.city = "Springfield"; a.city = "Springfield";
a.country = c; a.country = c;
p.address = a; person.address = a;
p.bornIn = bornCountry; person.bornIn = bornCountry;
p.name = "Homer"; person.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();
}
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session ->{
tx = s.beginTransaction(); session.persist( person );
try { } );
p = (Person) s.get( Person.class, p.id );
TransactionUtil.doInHibernate( this::sessionFactory, session ->{
Person p = session.get( Person.class, person.id );
assertNotNull( p ); assertNotNull( p );
assertNotNull( p.address ); assertNotNull( p.address );
assertEquals( "Springfield", p.address.city ); assertEquals( "Springfield", p.address.city );
@ -83,24 +70,13 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "DM", p.address.country.getIso2() );
assertNotNull( p.bornIn ); assertNotNull( p.bornIn );
assertEquals( "US", p.bornIn.getIso2() ); assertEquals( "US", p.bornIn.getIso2() );
tx.commit(); } );
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8172") @TestForIssue(jiraKey = "HHH-8172")
public void testQueryWithEmbeddedIsNull() throws Exception { public void testQueryWithEmbeddedIsNull() throws Exception {
Session s; Person person = new Person();
Transaction tx;
Person p = new Person();
Address a = new Address(); Address a = new Address();
Country c = new Country(); Country c = new Country();
Country bornCountry = new Country(); Country bornCountry = new Country();
@ -112,44 +88,22 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
a.address1 = "colorado street"; a.address1 = "colorado street";
a.city = "Springfield"; a.city = "Springfield";
a.country = c; a.country = c;
p.address = a; person.address = a;
p.bornIn = bornCountry; person.bornIn = bornCountry;
p.name = "Homer"; person.name = "Homer";
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); session.persist( person );
try { } );
s.persist( p );
tx.commit();
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); Person p = (Person) session.createQuery( "from Person p where p.bornIn is null" ).uniqueResult();
try {
p = (Person) s.createQuery( "from Person p where p.bornIn is null" ).uniqueResult();
assertNotNull( p ); assertNotNull( p );
assertNotNull( p.address ); assertNotNull( p.address );
assertEquals( "Springfield", p.address.city ); assertEquals( "Springfield", p.address.city );
assertNotNull( p.address.country ); assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "DM", p.address.country.getIso2() );
assertNull( p.bornIn ); assertNull( p.bornIn );
tx.commit(); } );
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
} }
@Test @Test
@ -158,7 +112,7 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
public void testQueryWithEmbeddedParameterAllNull() throws Exception { public void testQueryWithEmbeddedParameterAllNull() throws Exception {
Session s; Session s;
Transaction tx; Transaction tx;
Person p = new Person(); Person person = new Person();
Address a = new Address(); Address a = new Address();
Country c = new Country(); Country c = new Country();
Country bornCountry = new Country(); Country bornCountry = new Country();
@ -168,29 +122,17 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
a.address1 = "colorado street"; a.address1 = "colorado street";
a.city = "Springfield"; a.city = "Springfield";
a.country = c; a.country = c;
p.address = a; person.address = a;
p.bornIn = bornCountry; person.bornIn = bornCountry;
p.name = "Homer"; person.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();
}
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); session.persist( person );
try { } );
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" )
.setParameter( "b", p.bornIn ) TransactionUtil.doInHibernate( this::sessionFactory, session -> {
Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" )
.setParameter( "b", person.bornIn )
.uniqueResult(); .uniqueResult();
assertNotNull( p ); assertNotNull( p );
assertNotNull( p.address ); assertNotNull( p.address );
@ -198,25 +140,14 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
assertNotNull( p.address.country ); assertNotNull( p.address.country );
assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "DM", p.address.country.getIso2() );
assertNull( p.bornIn ); assertNull( p.bornIn );
tx.commit(); } );
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8172") @TestForIssue(jiraKey = "HHH-8172")
@FailureExpected(jiraKey = "HHH-8172") @FailureExpected(jiraKey = "HHH-8172")
public void testQueryWithEmbeddedParameterOneNull() throws Exception { public void testQueryWithEmbeddedParameterOneNull() throws Exception {
Session s; Person person = new Person();
Transaction tx;
Person p = new Person();
Address a = new Address(); Address a = new Address();
Country c = new Country(); Country c = new Country();
Country bornCountry = new Country(); Country bornCountry = new Country();
@ -228,29 +159,17 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
a.address1 = "colorado street"; a.address1 = "colorado street";
a.city = "Springfield"; a.city = "Springfield";
a.country = c; a.country = c;
p.address = a; person.address = a;
p.bornIn = bornCountry; person.bornIn = bornCountry;
p.name = "Homer"; person.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();
}
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); session.persist( person );
try { } );
p = (Person) s.createQuery( "from Person p where p.bornIn = :b" )
.setParameter( "b", p.bornIn ) TransactionUtil.doInHibernate( this::sessionFactory, session -> {
Person p = (Person) session.createQuery( "from Person p where p.bornIn = :b" )
.setParameter( "b", person.bornIn )
.uniqueResult(); .uniqueResult();
assertNotNull( p ); assertNotNull( p );
assertNotNull( p.address ); assertNotNull( p.address );
@ -259,24 +178,13 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "DM", p.address.country.getIso2() );
assertEquals( "US", p.bornIn.getIso2() ); assertEquals( "US", p.bornIn.getIso2() );
assertNull( p.bornIn.getName() ); assertNull( p.bornIn.getName() );
tx.commit(); } );
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-8172") @TestForIssue(jiraKey = "HHH-8172")
public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception { public void testQueryWithEmbeddedWithNullUsingSubAttributes() throws Exception {
Session s; Person person = new Person();
Transaction tx;
Person p = new Person();
Address a = new Address(); Address a = new Address();
Country c = new Country(); Country c = new Country();
Country bornCountry = new Country(); Country bornCountry = new Country();
@ -288,32 +196,19 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
a.address1 = "colorado street"; a.address1 = "colorado street";
a.city = "Springfield"; a.city = "Springfield";
a.country = c; a.country = c;
p.address = a; person.address = a;
p.bornIn = bornCountry; person.bornIn = bornCountry;
p.name = "Homer"; person.name = "Homer";
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); session.persist( person );
try { } );
s.persist( p );
tx.commit();
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
tx = s.beginTransaction(); Person p = (Person) session.createQuery( "from Person p " +
try {
p = (Person) s.createQuery( "from Person p " +
"where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " + "where ( p.bornIn.iso2 is null or p.bornIn.iso2 = :i ) and " +
"( p.bornIn.name is null or p.bornIn.name = :n )" "( p.bornIn.name is null or p.bornIn.name = :n )"
).setParameter( "i", p.bornIn.getIso2() ) ).setParameter( "i", person.bornIn.getIso2() )
.setParameter( "n", p.bornIn.getName() ) .setParameter( "n", person.bornIn.getName() )
.uniqueResult(); .uniqueResult();
assertNotNull( p ); assertNotNull( p );
assertNotNull( p.address ); assertNotNull( p.address );
@ -322,16 +217,7 @@ public class EmbeddedTest extends BaseNonConfigCoreFunctionalTestCase {
assertEquals( "DM", p.address.country.getIso2() ); assertEquals( "DM", p.address.country.getIso2() );
assertEquals( "US", p.bornIn.getIso2() ); assertEquals( "US", p.bornIn.getIso2() );
assertNull( p.bornIn.getName() ); assertNull( p.bornIn.getName() );
tx.commit(); } );
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
}
finally {
s.close();
}
} }
@Test @Test

View File

@ -6,9 +6,11 @@
*/ */
package org.hibernate.test.annotations.filter.secondarytable; package org.hibernate.test.annotations.filter.secondarytable;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -21,35 +23,43 @@ public class SecondaryTableTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected void prepareTest() throws Exception { protected void prepareTest() throws Exception {
Session s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, s -> {
s.beginTransaction(); insertUser( s, "q@s.com", 21, false, "a1", "b" );
try { insertUser( s, "r@s.com", 22, false, "a2", "b" );
insertUser( "q@s.com", 21, false, "a1", "b" ); insertUser( s, "s@s.com", 23, true, "a3", "b" );
insertUser( "r@s.com", 22, false, "a2", "b" ); insertUser( s, "t@s.com", 24, false, "a4", "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();
}
} }
@Test @Test
public void testFilter(){ public void testFilter() {
Assert.assertEquals(Long.valueOf(4), session.createQuery("select count(u) from User u").uniqueResult()); try (Session session = openSession()) {
session.enableFilter("ageFilter").setParameter("age", 24); Assert.assertEquals(
Assert.assertEquals(Long.valueOf(2), session.createQuery("select count(u) from User u").uniqueResult()); 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 user = new User();
user.setEmailAddress(emailAddress); user.setEmailAddress( emailAddress );
user.setAge(age); user.setAge( age );
user.setLockedOut(lockedOut); user.setLockedOut( lockedOut );
user.setUsername(username); user.setUsername( username );
user.setPassword(password); user.setPassword( password );
session.persist(user); session.persist( user );
} }
} }

View File

@ -41,6 +41,7 @@ public class EmptyCompositesDirtynessTest extends BaseCoreFunctionalTestCase {
@TestForIssue(jiraKey = "HHH-7610") @TestForIssue(jiraKey = "HHH-7610")
public void testCompositesEmpty() { public void testCompositesEmpty() {
Session s = openSession(); Session s = openSession();
try {
s.getTransaction().begin(); s.getTransaction().begin();
ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
@ -60,4 +61,8 @@ public class EmptyCompositesDirtynessTest extends BaseCoreFunctionalTestCase {
s.getTransaction().rollback(); s.getTransaction().rollback();
} }
finally {
s.close();
}
}
} }

View File

@ -42,6 +42,7 @@ public class EmptyInitializedCompositesDirtynessTest extends BaseCoreFunctionalT
@TestForIssue(jiraKey = "HHH-7610") @TestForIssue(jiraKey = "HHH-7610")
public void testInitializedCompositesEmpty() { public void testInitializedCompositesEmpty() {
Session s = openSession(); Session s = openSession();
try {
s.getTransaction().begin(); s.getTransaction().begin();
ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
@ -58,4 +59,8 @@ public class EmptyInitializedCompositesDirtynessTest extends BaseCoreFunctionalT
s.getTransaction().rollback(); s.getTransaction().rollback();
} }
finally {
s.close();
}
}
} }

View File

@ -41,6 +41,7 @@ public class EmptyInitializedCompositesTest extends BaseCoreFunctionalTestCase {
@TestForIssue(jiraKey = "HHH-7610") @TestForIssue(jiraKey = "HHH-7610")
public void testCompositesEmpty() { public void testCompositesEmpty() {
Session s = openSession(); Session s = openSession();
try {
s.getTransaction().begin(); s.getTransaction().begin();
ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner(); ComponentEmptyEmbeddedOwner owner = new ComponentEmptyEmbeddedOwner();
@ -60,4 +61,8 @@ public class EmptyInitializedCompositesTest extends BaseCoreFunctionalTestCase {
s.getTransaction().rollback(); s.getTransaction().rollback();
} }
finally {
s.close();
}
}
} }

View File

@ -6,9 +6,9 @@
*/ */
package org.hibernate.test.discriminator.joined; package org.hibernate.test.discriminator.joined;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -21,30 +21,20 @@ public class JoinedSubclassInheritanceTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected String[] getMappings() { protected String[] getMappings() {
return new String[] { "discriminator/joined/JoinedSubclassInheritance.hbm.xml" }; return new String[] {"discriminator/joined/JoinedSubclassInheritance.hbm.xml"};
} }
@Test @Test
public void testConfiguredDiscriminatorValue() { public void testConfiguredDiscriminatorValue() {
Session session = openSession(); final ChildEntity childEntity = new ChildEntity( 1, "Child" );
try { TransactionUtil.doInHibernate( this::sessionFactory, session -> {
ChildEntity ce = new ChildEntity( 1, "Child" ); session.save( childEntity );
session.getTransaction().begin(); } );
session.save( ce );
session.getTransaction().commit();
session.clear();
ce = (ChildEntity) session.find( ChildEntity.class, 1 ); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
ChildEntity ce = session.find( ChildEntity.class, 1 );
assertEquals( "ce", ce.getType() ); assertEquals( "ce", ce.getType() );
} } );
catch ( Exception e ) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
}
finally {
session.close();
}
} }
} }

View File

@ -4053,6 +4053,7 @@ public class FooBarTest extends LegacyTestCase {
} }
catch (Exception e) { catch (Exception e) {
s.getTransaction().rollback(); s.getTransaction().rollback();
throw e;
} }
finally { finally {
s.close(); s.close();

View File

@ -24,7 +24,6 @@ import org.jboss.logging.Logger;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.loader.Loader; import org.hibernate.loader.Loader;
import org.hibernate.query.Query; import org.hibernate.query.Query;
@ -39,6 +38,7 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.logger.LoggerInspectionRule; import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable; import org.hibernate.testing.logger.Triggerable;
import org.hibernate.testing.transaction.TransactionUtil;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -61,27 +61,17 @@ public class LockNoneWarmingTest extends BaseCoreFunctionalTestCase {
} }
@Before @Before
public void setUp(){ public void setUp() {
buildSessionFactory(); buildSessionFactory();
final Set messagesPrefixes = new HashSet<>( ); final Set messagesPrefixes = new HashSet<>();
messagesPrefixes.add( "HHH000444" ); messagesPrefixes.add( "HHH000444" );
messagesPrefixes.add( "HHH000445" ); messagesPrefixes.add( "HHH000445" );
triggerable = logInspection.watchForLogMessages( messagesPrefixes ); triggerable = logInspection.watchForLogMessages( messagesPrefixes );
try (Session s = openSession();) { TransactionUtil.doInHibernate( this::sessionFactory, session -> {
Transaction tx = s.beginTransaction();
try {
Item item = new Item(); Item item = new Item();
item.name = "ZZZZ"; item.name = "ZZZZ";
s.persist( item ); session.persist( item );
} );
tx.commit();
}
catch (Exception e) {
if ( tx.isActive() ) {
tx.rollback();
}
}
}
} }
@After @After

View File

@ -12,7 +12,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 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.RequiresDialect;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -47,38 +47,16 @@ public class SynonymValidationTest extends BaseNonConfigCoreFunctionalTestCase {
@Before @Before
public void setUp() { public void setUp() {
Session s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
try { session.createSQLQuery( "CREATE SYNONYM test_synonym FOR test_entity" ).executeUpdate();
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();
}
} }
@After @After
public void tearDown() { public void tearDown() {
Session s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
try { session.createSQLQuery( "DROP SYNONYM test_synonym FORCE" ).executeUpdate();
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();
}
} }
@Test @Test

View File

@ -12,7 +12,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 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.RequiresDialect;
import org.hibernate.testing.RequiresDialects; import org.hibernate.testing.RequiresDialects;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -46,38 +46,16 @@ public class ViewValidationTest extends BaseCoreFunctionalTestCase {
@Before @Before
public void setUp() { public void setUp() {
Session s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
try { session.createSQLQuery( "CREATE VIEW test_synonym AS SELECT * FROM test_entity" ).executeUpdate();
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();
}
} }
@After @After
public void tearDown() { public void tearDown() {
Session s = openSession(); TransactionUtil.doInHibernate( this::sessionFactory, session -> {
try { session.createSQLQuery( "DROP VIEW test_synonym CASCADE" ).executeUpdate();
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();
}
} }
@Test @Test