Fix test failures

This commit is contained in:
Andrea Boriero 2021-03-22 17:46:04 +01:00
parent 619a123c8d
commit 0864c6e711
6 changed files with 76 additions and 50 deletions

View File

@ -16,7 +16,10 @@ import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.internal.TcclLookupPrecedence;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -25,6 +28,18 @@ import static org.junit.Assert.assertTrue;
* @author Cédric Tabin
*/
public class ClassLoaderServiceImplTest {
protected ClassLoader originalClassLoader;
@BeforeEach
public void setUp(){
originalClassLoader = Thread.currentThread().getContextClassLoader();
}
@AfterEach
public void tearDown(){
Thread.currentThread().setContextClassLoader( originalClassLoader );
}
@Test
public void testNullTCCL() {
Thread.currentThread().setContextClassLoader( null );

View File

@ -38,9 +38,10 @@ import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.pack.defaultpar.Version;
import org.hibernate.jpa.test.pack.explodedpar.Carpet;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

View File

@ -43,15 +43,16 @@ import org.hibernate.jpa.test.pack.various.Airplane;
import org.hibernate.jpa.test.pack.various.Seat;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.FailureExpected;
import org.junit.After;
import org.junit.Test;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* In this test we verify that it is possible to bootstrap Hibernate/JPA from
@ -65,7 +66,7 @@ import static org.junit.Assert.fail;
@SuppressWarnings("unchecked")
public class PackagedEntityManagerTest extends PackagingTestCase {
private EntityManagerFactory emf;
@After
@AfterEach
public void tearDown(){
if(emf != null && emf.isOpen()) {
emf.close();
@ -160,20 +161,20 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
as.setVersion( v );
em.persist( as );
em.flush();
assertEquals( "Failure in default listeners", 1, IncrementListener.getIncrement() );
assertEquals( "Failure in XML overriden listeners", 1, OtherIncrementListener.getIncrement() );
assertEquals( 1, IncrementListener.getIncrement(), "Failure in default listeners" );
assertEquals( 1, OtherIncrementListener.getIncrement(), "Failure in XML overriden listeners" );
Mouse mouse = new Mouse();
mouse.setName( "mickey" );
em.persist( mouse );
em.flush();
assertEquals( "Failure in @ExcludeDefaultListeners", 1, IncrementListener.getIncrement() );
assertEquals( 1, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" );
assertEquals( 1, OtherIncrementListener.getIncrement() );
Money money = new Money();
em.persist( money );
em.flush();
assertEquals( "Failure in @ExcludeDefaultListeners", 2, IncrementListener.getIncrement() );
assertEquals( 2, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" );
assertEquals( 1, OtherIncrementListener.getIncrement() );
em.getTransaction().rollback();
@ -324,10 +325,10 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
.getServiceRegistry()
.getService( EventListenerRegistry.class );
assertEquals(
"Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work",
listenerRegistry.getEventListenerGroup( EventType.PRE_INSERT ).count(),
listenerRegistry.getEventListenerGroup( EventType.PRE_UPDATE ).count() + 1
);
listenerRegistry.getEventListenerGroup( EventType.PRE_UPDATE ).count() + 1,
"Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work"
);
em.close();
emf.close();
@ -384,7 +385,7 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
}
@Test
@FailureExpected( jiraKey = "none", message = "Problem with caching, specifically managing SoftLocks" )
@FailureExpected( jiraKey = "none", reason = "Problem with caching, specifically managing SoftLocks" )
public void testConfiguration() throws Exception {
File testPackage = buildExplicitPar();
addPackageToClasspath( testPackage );

View File

@ -40,9 +40,10 @@ import org.hibernate.jpa.test.pack.spacepar.Bug;
import org.hibernate.jpa.test.pack.various.Airplane;
import org.hibernate.jpa.test.pack.various.Seat;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
@ -58,12 +59,16 @@ import static org.junit.Assert.fail;
* @author Hardy Ferentschik
* @author Brett Meyer
*/
public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase {
protected static ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
public abstract class PackagingTestCase extends BaseSessionFactoryFunctionalTest {
protected static ClassLoader originalClassLoader;
private static Thread thread;
protected static ClassLoader bundleClassLoader;
protected static File packageTargetDir;
static {
thread = Thread.currentThread();
originalClassLoader = thread.getContextClassLoader();
// get a URL reference to something we now is part of the classpath (us)
URL myUrl = originalClassLoader.getResource(
PackagingTestCase.class.getName().replace( '.', '/' ) + ".class"
@ -106,36 +111,36 @@ public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase {
packageTargetDir.mkdirs();
}
@Before
@BeforeEach
public void prepareTCCL() {
// add the bundle class loader in order for ShrinkWrap to build the test package
Thread.currentThread().setContextClassLoader( bundleClassLoader );
thread.setContextClassLoader( bundleClassLoader );
}
@After
public void resetTCCL() throws Exception {
@AfterEach
public void resetTCCL() {
// reset the classloader
Thread.currentThread().setContextClassLoader( originalClassLoader );
thread.setContextClassLoader( originalClassLoader );
}
protected void addPackageToClasspath(File... files) throws MalformedURLException {
List<URL> urlList = new ArrayList<URL>();
List<URL> urlList = new ArrayList<>();
for ( File file : files ) {
urlList.add( file.toURL() );
}
URLClassLoader classLoader = new URLClassLoader(
urlList.toArray( new URL[urlList.size()] ), originalClassLoader
);
Thread.currentThread().setContextClassLoader( classLoader );
thread.setContextClassLoader( classLoader );
}
protected void addPackageToClasspath(URL... urls) throws MalformedURLException {
List<URL> urlList = new ArrayList<URL>();
List<URL> urlList = new ArrayList<>();
urlList.addAll( Arrays.asList( urls ) );
URLClassLoader classLoader = new URLClassLoader(
urlList.toArray( new URL[urlList.size()] ), originalClassLoader
);
Thread.currentThread().setContextClassLoader( classLoader );
thread.setContextClassLoader( classLoader );
}
protected File buildDefaultPar() {
@ -433,9 +438,9 @@ public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase {
JavaArchive archive = ShrinkWrap.create( JavaArchive.class, fileName );
archive.addAsResource( includeFile );
File testPackage = new File( packageTargetDir, fileName );
archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage;
File testPackage = new File( packageTargetDir, fileName );
archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage;
}
}

View File

@ -28,12 +28,13 @@ import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.test.pack.defaultpar.ApplicationServer;
import org.hibernate.jpa.test.pack.defaultpar.Version;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Emmanuel Bernard

View File

@ -16,12 +16,11 @@ import java.util.Properties;
import java.util.logging.Logger;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
/**
* This test illustrates the problem with calling {@link ClassLoader#loadClass(String)} rather than
@ -30,7 +29,7 @@ import static org.junit.Assert.fail;
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-7272" )
public class DriverManagerRegistrationTest extends BaseUnitTestCase {
public class DriverManagerRegistrationTest {
@Test
public void testDriverRegistrationUsingLoadClassFails() {
@ -73,10 +72,14 @@ public class DriverManagerRegistrationTest extends BaseUnitTestCase {
}
private static ClassLoader determineClassLoader() {
return DriverManagerRegistrationTest.class.getClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if ( classLoader == null ) {
classLoader = DriverManagerRegistrationTest.class.getClassLoader();
}
return classLoader;
}
@AfterClass
@AfterAll
public static void afterwards() {
try {
DriverManager.deregisterDriver( TestDriver1.INSTANCE );