From da9c6e160d6800a506fc1197db5caffb78382fba Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Tue, 12 Jul 2016 18:07:33 +0300 Subject: [PATCH] HHH-10907 - Fix connection leak problem in hibernate-core tests --- .../jpa/test/cdi/NoCdiAvailableTest.java | 30 ++++++++++++------- .../basic/CollectionAsBasicTest.java | 2 +- .../DeepCollectionElementTest.java | 16 ++++++++-- .../testing/junit4/BaseUnitTestCase.java | 13 ++++---- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/cdi/NoCdiAvailableTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/cdi/NoCdiAvailableTest.java index 452f0b7d33..2808cbc347 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/cdi/NoCdiAvailableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/cdi/NoCdiAvailableTest.java @@ -6,20 +6,14 @@ */ package org.hibernate.jpa.test.cdi; -import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.URL; - -import org.hibernate.HibernateException; -import org.hibernate.bytecode.spi.ByteCodeHelper; - -import org.junit.Rule; -import org.junit.Test; +import javax.persistence.EntityManagerFactory; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.ClassLoadingIsolater; -import org.hibernate.testing.junit4.ExtraAssertions; +import org.junit.Rule; +import org.junit.Test; import static org.junit.Assert.fail; @@ -71,7 +65,15 @@ public class NoCdiAvailableTest extends BaseUnitTestCase { "org.hibernate.jpa.test.cdi.NoCdiAvailableTestDelegate" ); Method mainMethod = delegateClass.getMethod( "passingNoBeanManager" ); - mainMethod.invoke( null ); + EntityManagerFactory entityManagerFactory = null; + try { + entityManagerFactory = (EntityManagerFactory) mainMethod.invoke( null ); + } + finally { + if (entityManagerFactory != null ) { + entityManagerFactory.close(); + } + } } @Test @@ -80,12 +82,18 @@ public class NoCdiAvailableTest extends BaseUnitTestCase { "org.hibernate.jpa.test.cdi.NoCdiAvailableTestDelegate" ); Method mainMethod = delegateClass.getMethod( "passingBeanManager" ); + EntityManagerFactory entityManagerFactory = null; try { - mainMethod.invoke( null ); + entityManagerFactory = (EntityManagerFactory) mainMethod.invoke( null ); fail( "Expecting failure from missing CDI classes" ); } catch (InvocationTargetException expected) { // hard to assert specific exception types due to classloader trickery } + finally { + if (entityManagerFactory != null ) { + entityManagerFactory.close(); + } + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/basic/CollectionAsBasicTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/basic/CollectionAsBasicTest.java index af4d5b786b..2ca09cc35b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/basic/CollectionAsBasicTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/basic/CollectionAsBasicTest.java @@ -37,7 +37,7 @@ public class CollectionAsBasicTest extends BaseUnitTestCase { public void testCollectionAsBasic() { StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); try { - Metadata metadata = new MetadataSources().addAnnotatedClass( Post.class ) + Metadata metadata = new MetadataSources(ssr).addAnnotatedClass( Post.class ) .getMetadataBuilder().applyBasicType( new DelimitedStringsType() ) .build(); PersistentClass postBinding = metadata.getEntityBinding( Post.class.getName() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/DeepCollectionElementTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/DeepCollectionElementTest.java index 0550b600d6..1dc4dabfc6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/DeepCollectionElementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/deepcollectionelements/DeepCollectionElementTest.java @@ -6,24 +6,34 @@ */ package org.hibernate.test.annotations.collectionelement.deepcollectionelements; -import org.junit.Test; - +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.Configuration; + import org.hibernate.testing.FailureExpected; import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; /** * @author Emmanuel Bernard */ @FailureExpected( jiraKey = "HHH-3157" ) public class DeepCollectionElementTest extends BaseUnitTestCase { + @Test public void testInitialization() throws Exception { Configuration configuration = new Configuration(); configuration.addAnnotatedClass( A.class ); configuration.addAnnotatedClass( B.class ); configuration.addAnnotatedClass( C.class ); - configuration.buildSessionFactory( ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() ) ).close(); + StandardServiceRegistryImpl serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( configuration.getProperties() ); + try { + SessionFactory sessionFactory = configuration.buildSessionFactory( serviceRegistry ); + sessionFactory.close(); + } + finally { + serviceRegistry.destroy(); + } } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseUnitTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseUnitTestCase.java index 4c157df7ae..53e53257bc 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseUnitTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseUnitTestCase.java @@ -10,11 +10,10 @@ import javax.transaction.SystemException; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; +import org.hibernate.testing.AfterClassOnce; import org.hibernate.testing.jdbc.leak.ConnectionLeakUtil; import org.hibernate.testing.jta.TestingJtaPlatformImpl; import org.junit.After; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestRule; import org.junit.rules.Timeout; @@ -34,21 +33,21 @@ public abstract class BaseUnitTestCase { private static boolean enableConnectionLeakDetection = Boolean.TRUE.toString() .equals( System.getenv( "HIBERNATE_CONNECTION_LEAK_DETECTION" ) ); - private static ConnectionLeakUtil connectionLeakUtil; + private ConnectionLeakUtil connectionLeakUtil; @Rule public TestRule globalTimeout = new Timeout( 30 * 60 * 1000 ); // no test should run longer than 30 minutes - @BeforeClass - public static void initConnectionLeakUtility() { + public BaseUnitTestCase() { if ( enableConnectionLeakDetection ) { connectionLeakUtil = new ConnectionLeakUtil(); } } - @AfterClass - public static void assertNoLeaks() { + @AfterClassOnce + public void assertNoLeaks() { if ( enableConnectionLeakDetection ) { + log.info( "Assert no leaks!" ); connectionLeakUtil.assertNoLeaks(); } }