From 0864c6e71129a1313802bf7dd26293e921562c76 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 22 Mar 2021 17:46:04 +0100 Subject: [PATCH] Fix test failures --- .../ClassLoaderServiceImplTest.java | 17 +++++++- .../bootstrap/scanning/JarVisitorTest.java | 5 ++- .../scanning/PackagedEntityManagerTest.java | 35 +++++++++-------- .../bootstrap/scanning/PackagingTestCase.java | 39 +++++++++++-------- .../test/bootstrap/scanning/ScannerTest.java | 11 +++--- .../DriverManagerRegistrationTest.java | 19 +++++---- 6 files changed, 76 insertions(+), 50 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/registry/classloading/ClassLoaderServiceImplTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/registry/classloading/ClassLoaderServiceImplTest.java index 379bb162d1..3aac806599 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/registry/classloading/ClassLoaderServiceImplTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/registry/classloading/ClassLoaderServiceImplTest.java @@ -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 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/JarVisitorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/JarVisitorTest.java index bd55e16948..0ee602e2b2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/JarVisitorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/JarVisitorTest.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java index a7afdf669d..509ee7badf 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagedEntityManagerTest.java @@ -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 ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagingTestCase.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagingTestCase.java index 8a393142bc..c33883afa2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagingTestCase.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/PackagingTestCase.java @@ -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 urlList = new ArrayList(); + List 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 urlList = new ArrayList(); + List 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; } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java index aadaec8b1d..7d7444bf81 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/connection/DriverManagerRegistrationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/connection/DriverManagerRegistrationTest.java index 6f17128726..c86cad2f89 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/connection/DriverManagerRegistrationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/connection/DriverManagerRegistrationTest.java @@ -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 );