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.boot.registry.classloading.internal.TcclLookupPrecedence;
import org.hibernate.testing.TestForIssue; 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.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -25,6 +28,18 @@ import static org.junit.Assert.assertTrue;
* @author Cédric Tabin * @author Cédric Tabin
*/ */
public class ClassLoaderServiceImplTest { public class ClassLoaderServiceImplTest {
protected ClassLoader originalClassLoader;
@BeforeEach
public void setUp(){
originalClassLoader = Thread.currentThread().getContextClassLoader();
}
@AfterEach
public void tearDown(){
Thread.currentThread().setContextClassLoader( originalClassLoader );
}
@Test @Test
public void testNullTCCL() { public void testNullTCCL() {
Thread.currentThread().setContextClassLoader( null ); 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.defaultpar.Version;
import org.hibernate.jpa.test.pack.explodedpar.Carpet; import org.hibernate.jpa.test.pack.explodedpar.Carpet;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue; 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.assertEquals;
import static org.junit.Assert.assertNotNull; 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.jpa.test.pack.various.Seat;
import org.hibernate.stat.Statistics; import org.hibernate.stat.Statistics;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.orm.junit.FailureExpected;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
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.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 * 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") @SuppressWarnings("unchecked")
public class PackagedEntityManagerTest extends PackagingTestCase { public class PackagedEntityManagerTest extends PackagingTestCase {
private EntityManagerFactory emf; private EntityManagerFactory emf;
@After @AfterEach
public void tearDown(){ public void tearDown(){
if(emf != null && emf.isOpen()) { if(emf != null && emf.isOpen()) {
emf.close(); emf.close();
@ -160,20 +161,20 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
as.setVersion( v ); as.setVersion( v );
em.persist( as ); em.persist( as );
em.flush(); em.flush();
assertEquals( "Failure in default listeners", 1, IncrementListener.getIncrement() ); assertEquals( 1, IncrementListener.getIncrement(), "Failure in default listeners" );
assertEquals( "Failure in XML overriden listeners", 1, OtherIncrementListener.getIncrement() ); assertEquals( 1, OtherIncrementListener.getIncrement(), "Failure in XML overriden listeners" );
Mouse mouse = new Mouse(); Mouse mouse = new Mouse();
mouse.setName( "mickey" ); mouse.setName( "mickey" );
em.persist( mouse ); em.persist( mouse );
em.flush(); em.flush();
assertEquals( "Failure in @ExcludeDefaultListeners", 1, IncrementListener.getIncrement() ); assertEquals( 1, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" );
assertEquals( 1, OtherIncrementListener.getIncrement() ); assertEquals( 1, OtherIncrementListener.getIncrement() );
Money money = new Money(); Money money = new Money();
em.persist( money ); em.persist( money );
em.flush(); em.flush();
assertEquals( "Failure in @ExcludeDefaultListeners", 2, IncrementListener.getIncrement() ); assertEquals( 2, IncrementListener.getIncrement(), "Failure in @ExcludeDefaultListeners" );
assertEquals( 1, OtherIncrementListener.getIncrement() ); assertEquals( 1, OtherIncrementListener.getIncrement() );
em.getTransaction().rollback(); em.getTransaction().rollback();
@ -324,10 +325,10 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
.getServiceRegistry() .getServiceRegistry()
.getService( EventListenerRegistry.class ); .getService( EventListenerRegistry.class );
assertEquals( assertEquals(
"Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work",
listenerRegistry.getEventListenerGroup( EventType.PRE_INSERT ).count(), 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(); em.close();
emf.close(); emf.close();
@ -384,7 +385,7 @@ public class PackagedEntityManagerTest extends PackagingTestCase {
} }
@Test @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 { public void testConfiguration() throws Exception {
File testPackage = buildExplicitPar(); File testPackage = buildExplicitPar();
addPackageToClasspath( testPackage ); 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.Airplane;
import org.hibernate.jpa.test.pack.various.Seat; import org.hibernate.jpa.test.pack.various.Seat;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.junit.After;
import org.junit.Before; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.jboss.shrinkwrap.api.ArchivePath; import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths; import org.jboss.shrinkwrap.api.ArchivePaths;
@ -58,12 +59,16 @@ import static org.junit.Assert.fail;
* @author Hardy Ferentschik * @author Hardy Ferentschik
* @author Brett Meyer * @author Brett Meyer
*/ */
public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase { public abstract class PackagingTestCase extends BaseSessionFactoryFunctionalTest {
protected static ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); protected static ClassLoader originalClassLoader;
private static Thread thread;
protected static ClassLoader bundleClassLoader; protected static ClassLoader bundleClassLoader;
protected static File packageTargetDir; protected static File packageTargetDir;
static { static {
thread = Thread.currentThread();
originalClassLoader = thread.getContextClassLoader();
// get a URL reference to something we now is part of the classpath (us) // get a URL reference to something we now is part of the classpath (us)
URL myUrl = originalClassLoader.getResource( URL myUrl = originalClassLoader.getResource(
PackagingTestCase.class.getName().replace( '.', '/' ) + ".class" PackagingTestCase.class.getName().replace( '.', '/' ) + ".class"
@ -106,36 +111,36 @@ public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase {
packageTargetDir.mkdirs(); packageTargetDir.mkdirs();
} }
@Before @BeforeEach
public void prepareTCCL() { public void prepareTCCL() {
// add the bundle class loader in order for ShrinkWrap to build the test package // add the bundle class loader in order for ShrinkWrap to build the test package
Thread.currentThread().setContextClassLoader( bundleClassLoader ); thread.setContextClassLoader( bundleClassLoader );
} }
@After @AfterEach
public void resetTCCL() throws Exception { public void resetTCCL() {
// reset the classloader // reset the classloader
Thread.currentThread().setContextClassLoader( originalClassLoader ); thread.setContextClassLoader( originalClassLoader );
} }
protected void addPackageToClasspath(File... files) throws MalformedURLException { protected void addPackageToClasspath(File... files) throws MalformedURLException {
List<URL> urlList = new ArrayList<URL>(); List<URL> urlList = new ArrayList<>();
for ( File file : files ) { for ( File file : files ) {
urlList.add( file.toURL() ); urlList.add( file.toURL() );
} }
URLClassLoader classLoader = new URLClassLoader( URLClassLoader classLoader = new URLClassLoader(
urlList.toArray( new URL[urlList.size()] ), originalClassLoader urlList.toArray( new URL[urlList.size()] ), originalClassLoader
); );
Thread.currentThread().setContextClassLoader( classLoader ); thread.setContextClassLoader( classLoader );
} }
protected void addPackageToClasspath(URL... urls) throws MalformedURLException { protected void addPackageToClasspath(URL... urls) throws MalformedURLException {
List<URL> urlList = new ArrayList<URL>(); List<URL> urlList = new ArrayList<>();
urlList.addAll( Arrays.asList( urls ) ); urlList.addAll( Arrays.asList( urls ) );
URLClassLoader classLoader = new URLClassLoader( URLClassLoader classLoader = new URLClassLoader(
urlList.toArray( new URL[urlList.size()] ), originalClassLoader urlList.toArray( new URL[urlList.size()] ), originalClassLoader
); );
Thread.currentThread().setContextClassLoader( classLoader ); thread.setContextClassLoader( classLoader );
} }
protected File buildDefaultPar() { protected File buildDefaultPar() {
@ -433,9 +438,9 @@ public abstract class PackagingTestCase extends BaseCoreFunctionalTestCase {
JavaArchive archive = ShrinkWrap.create( JavaArchive.class, fileName ); JavaArchive archive = ShrinkWrap.create( JavaArchive.class, fileName );
archive.addAsResource( includeFile ); archive.addAsResource( includeFile );
File testPackage = new File( packageTargetDir, fileName ); File testPackage = new File( packageTargetDir, fileName );
archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir ); archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage; 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.ApplicationServer;
import org.hibernate.jpa.test.pack.defaultpar.Version; 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 * @author Emmanuel Bernard

View File

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