From 5bd9a9b1e49cc1998c3351261f330cfe8c3103f7 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 15 Dec 2024 18:53:19 +0100 Subject: [PATCH] Make sure to revert to previous ClassLoader in tests Otherwise we see strange issues if the ClassLoader is closed as part of tests. --- .../logging/jdk/LoggerFactoryTest.java | 49 ++++++++++--------- .../dstadler/commons/svn/SVNCommandsTest.java | 14 ++---- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/test/java/org/dstadler/commons/logging/jdk/LoggerFactoryTest.java b/src/test/java/org/dstadler/commons/logging/jdk/LoggerFactoryTest.java index 8fd74680..a3a1e6f5 100644 --- a/src/test/java/org/dstadler/commons/logging/jdk/LoggerFactoryTest.java +++ b/src/test/java/org/dstadler/commons/logging/jdk/LoggerFactoryTest.java @@ -32,28 +32,33 @@ public void testMake() { @Test public void testInitLogging() throws IOException { - Thread.currentThread().setContextClassLoader(new ClassLoader() { - @Override - public URL getResource(String name) { - return null; - } - }); - - try { - LoggerFactory.initLogging(); - fail("Should throw Exception, seems we found a resource: " + Thread.currentThread().getContextClassLoader().getResource("logging.properties")); - } catch (IOException e) { - // expected if we do not have a logging.properties here - assertNotNull(e); - } - - try (URLClassLoader cl = new URLClassLoader(new URL[] {new File("src/test/resources").toURI().toURL()}, - Thread.currentThread().getContextClassLoader())) { - Thread.currentThread().setContextClassLoader(cl); - - // now it should work - LoggerFactory.initLogging(); - } + ClassLoader prev = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(new ClassLoader() { + @Override + public URL getResource(String name) { + return null; + } + }); + + try { + LoggerFactory.initLogging(); + fail("Should throw Exception, seems we found a resource: " + Thread.currentThread().getContextClassLoader().getResource("logging.properties")); + } catch (IOException e) { + // expected if we do not have a logging.properties here + assertNotNull(e); + } + + try (URLClassLoader cl = new URLClassLoader(new URL[] {new File("src/test/resources").toURI().toURL()}, prev)) { + Thread.currentThread().setContextClassLoader(cl); + + // now it should work + LoggerFactory.initLogging(); + } + } finally { + // restore the previous class loader + Thread.currentThread().setContextClassLoader(prev); + } } // helper method to get coverage of the unused constructor diff --git a/src/test/java/org/dstadler/commons/svn/SVNCommandsTest.java b/src/test/java/org/dstadler/commons/svn/SVNCommandsTest.java index 7620a023..f71682fd 100644 --- a/src/test/java/org/dstadler/commons/svn/SVNCommandsTest.java +++ b/src/test/java/org/dstadler/commons/svn/SVNCommandsTest.java @@ -48,15 +48,11 @@ public class SVNCommandsTest { // use static-block to initialize "static final" members static { - assertNotNull(Thread.currentThread().getContextClassLoader().getResource("logging.properties"), - "Should have logging.properties, but had none with Classloader: " + - Thread.currentThread().getContextClassLoader()); - - try { - LoggerFactory.initLogging(); - } catch (IOException e) { - throw new RuntimeException(e); - } + try { + LoggerFactory.initLogging(); + } catch (IOException e) { + throw new RuntimeException(e); + } repoDir = createLocalSVNRepository();