diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilCustomProtocolTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilCustomProtocolTest.java index 532b9ef..cb1a248 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilCustomProtocolTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilCustomProtocolTest.java @@ -19,25 +19,27 @@ package org.apache.logging.log4j.core.config.plugins.util; import static org.apache.logging.log4j.core.config.plugins.util.ResolverUtilTest.compileAndCreateClassLoader; import static org.apache.logging.log4j.core.config.plugins.util.ResolverUtilTest.compileJarAndCreateClassLoader; +import static org.apache.logging.log4j.core.config.plugins.util.ResolverUtilTest.WORK_DIR; + import static org.junit.Assert.assertEquals; +import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry.PluginTest; +import org.apache.logging.log4j.junit.CleanFolders; +import org.apache.logging.log4j.junit.URLStreamHandlerFactoryRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; + import java.io.IOException; -import java.lang.reflect.Field; import java.net.Proxy; import java.net.URL; +import java.net.URLClassLoader; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; -import java.util.Hashtable; - -import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry.PluginTest; -import org.apache.logging.log4j.junit.URLStreamHandlerFactoryRule; -import org.junit.AfterClass; -import org.junit.Rule; -import org.junit.Test; /** * Tests the ResolverUtil class for custom protocol like bundleresource, vfs, vfszip. @@ -47,6 +49,9 @@ public class ResolverUtilCustomProtocolTest { @Rule public URLStreamHandlerFactoryRule rule = new URLStreamHandlerFactoryRule(new NoopURLStreamHandlerFactory()); + @Rule + public RuleChain chain = RuleChain.outerRule(new CleanFolders(WORK_DIR)); + static class NoopURLStreamHandlerFactory implements URLStreamHandlerFactory { @Override @@ -182,27 +187,31 @@ public class ResolverUtilCustomProtocolTest { @Test public void testFindInPackageFromVfsDirectoryURL() throws Exception { - ClassLoader cl = compileAndCreateClassLoader("3"); - + try (URLClassLoader cl = compileAndCreateClassLoader("3")) { ResolverUtil resolverUtil = new ResolverUtil(); - resolverUtil.setClassLoader(new SingleURLClassLoader(new URL("vfs:/target/resolverutil3/customplugin3/"), cl)); + resolverUtil + .setClassLoader(new SingleURLClassLoader( + new URL("vfs:/" + WORK_DIR + "/resolverutil3/customplugin3/"), cl)); resolverUtil.findInPackage(new PluginTest(), "customplugin3"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin3.FixedString3Layout"), resolverUtil.getClasses().iterator().next()); + } } @Test public void testFindInPackageFromVfsJarURL() throws Exception { - ClassLoader cl = compileJarAndCreateClassLoader("4"); + try (URLClassLoader cl = compileJarAndCreateClassLoader("4")) { ResolverUtil resolverUtil = new ResolverUtil(); - resolverUtil.setClassLoader( - new SingleURLClassLoader(new URL("vfs:/target/resolverutil4/customplugin4.jar/customplugin4/"), cl)); + resolverUtil + .setClassLoader(new SingleURLClassLoader( + new URL("vfs:/" + WORK_DIR + "/resolverutil4/customplugin4.jar/customplugin4/"), cl)); resolverUtil.findInPackage(new PluginTest(), "customplugin4"); assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); assertEquals("Unexpected class resolved", cl.loadClass("customplugin4.FixedString4Layout"), resolverUtil.getClasses().iterator().next()); + } } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilTest.java index c0f1e3a..bfff544 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/ResolverUtilTest.java @@ -20,6 +20,12 @@ package org.apache.logging.log4j.core.config.plugins.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry.PluginTest; +import org.apache.logging.log4j.junit.CleanFolders; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; + import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -36,13 +42,15 @@ import java.nio.file.StandardCopyOption; import java.util.HashMap; import java.util.Map; -import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry.PluginTest; -import org.junit.Test; - /** * Tests the ResolverUtil class. */ public class ResolverUtilTest { + + static final String WORK_DIR = "target/testpluginsutil"; + + @Rule + public RuleChain chain = RuleChain.outerRule(new CleanFolders(WORK_DIR)); @Test public void testExtractPathFromJarUrl() throws Exception { @@ -141,28 +149,30 @@ public class ResolverUtilTest { @Test public void testFindInPackageFromDirectoryPath() throws Exception { - ClassLoader cl = compileAndCreateClassLoader("1"); - - ResolverUtil resolverUtil = new ResolverUtil(); - resolverUtil.setClassLoader(cl); - resolverUtil.findInPackage(new PluginTest(), "customplugin1"); - assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); - assertEquals("Unexpected class resolved", - cl.loadClass("customplugin1.FixedString1Layout"), - resolverUtil.getClasses().iterator().next()); + try (URLClassLoader cl = compileAndCreateClassLoader("1")) { + + ResolverUtil resolverUtil = new ResolverUtil(); + resolverUtil.setClassLoader(cl); + resolverUtil.findInPackage(new PluginTest(), "customplugin1"); + assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); + assertEquals("Unexpected class resolved", + cl.loadClass("customplugin1.FixedString1Layout"), + resolverUtil.getClasses().iterator().next()); + } } @Test public void testFindInPackageFromJarPath() throws Exception { - ClassLoader cl = compileJarAndCreateClassLoader("2"); + try (URLClassLoader cl = compileJarAndCreateClassLoader("2")) { - ResolverUtil resolverUtil = new ResolverUtil(); - resolverUtil.setClassLoader(cl); - resolverUtil.findInPackage(new PluginTest(), "customplugin2"); - assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); - assertEquals("Unexpected class resolved", - cl.loadClass("customplugin2.FixedString2Layout"), - resolverUtil.getClasses().iterator().next()); + ResolverUtil resolverUtil = new ResolverUtil(); + resolverUtil.setClassLoader(cl); + resolverUtil.findInPackage(new PluginTest(), "customplugin2"); + assertEquals("Class not found in packages", 1, resolverUtil.getClasses().size()); + assertEquals("Unexpected class resolved", + cl.loadClass("customplugin2.FixedString2Layout"), + resolverUtil.getClasses().iterator().next()); + } } static URLClassLoader compileJarAndCreateClassLoader(String suffix) throws IOException, Exception { @@ -181,7 +191,7 @@ public class ResolverUtilTest { static File compile(String suffix) throws IOException { final File orig = new File("target/test-classes/customplugin/FixedStringLayout.java.source"); - final File workDir = new File("target/resolverutil" + suffix); + final File workDir = new File(WORK_DIR, "resolverutil" + suffix); final File f = new File(workDir, "customplugin" + suffix + "/FixedString" + suffix + "Layout.java"); final File parent = f.getParentFile(); if (!parent.exists()) { diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/URLStreamHandlerFactoryRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/URLStreamHandlerFactoryRule.java index ac08bb3..95b4251 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/URLStreamHandlerFactoryRule.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/URLStreamHandlerFactoryRule.java @@ -81,7 +81,7 @@ public class URLStreamHandlerFactoryRule implements TestRule { Assert.assertNotNull("java.net URL does not declare a java.net.URLStreamHandlerFactory field", factoryField); Assert.assertEquals("java.net.URL declares multiple java.net.URLStreamHandlerFactory fields.", 1, - matches); + matches); // FIXME There is a break in the loop so always 0 or 1 URL.setURLStreamHandlerFactory(newURLStreamHandlerFactory); try { base.evaluate();