HHH-10907 - Fix connection leak problem in hibernate-core tests

This commit is contained in:
Vlad Mihalcea 2016-07-12 18:07:33 +03:00
parent 2246e94ded
commit da9c6e160d
4 changed files with 39 additions and 22 deletions

View File

@ -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();
}
}
}
}

View File

@ -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() );

View File

@ -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();
}
}
}

View File

@ -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();
}
}