1616 */
1717package org .apache .logging .log4j .core .config ;
1818
19+ import static java .util .Objects .requireNonNull ;
1920import static org .junit .jupiter .api .Assertions .assertEquals ;
2021import static org .junit .jupiter .api .Assertions .assertNotNull ;
2122import static org .junit .jupiter .api .Assertions .assertNull ;
3233import java .net .URL ;
3334import java .nio .file .Files ;
3435import java .nio .file .Path ;
35- import java .nio .file .Paths ;
3636import java .util .jar .Attributes ;
3737import java .util .jar .JarEntry ;
3838import java .util .jar .JarOutputStream ;
3939import java .util .jar .Manifest ;
40+ import org .apache .commons .io .IOUtils ;
4041import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
4142import org .junit .jupiter .api .Test ;
43+ import org .junit .jupiter .api .io .TempDir ;
4244
4345public class ConfigurationSourceTest {
44- private static final Path JAR_FILE = Paths .get ("target" , "test-classes" , "jarfile.jar" );
45- private static final Path CONFIG_FILE = Paths .get ("target" , "test-classes" , "log4j2-console.xml" );
46- private static final byte [] buffer = new byte [1024 ];
46+ /**
47+ * The path inside the jar created by {@link #prepareJarConfigURL} containing the configuration.
48+ */
49+ public static final String PATH_IN_JAR = "/config/console.xml" ;
50+
51+ private static final String CONFIG_FILE = "/config/ConfigurationSourceTest.xml" ;
52+
53+ @ TempDir
54+ private Path tempDir ;
4755
4856 @ Test
49- public void testJira_LOG4J2_2770_byteArray () throws Exception {
57+ void testJira_LOG4J2_2770_byteArray () throws Exception {
5058 final ConfigurationSource configurationSource =
5159 new ConfigurationSource (new ByteArrayInputStream (new byte [] {'a' , 'b' }));
5260 assertNotNull (configurationSource .resetInputStream ());
5361 }
5462
5563 /**
5664 * Checks if the usage of 'jar:' URLs does not increase the file descriptor
57- * count and the jar file can be deleted.
58- *
59- * @throws Exception
65+ * count, and the jar file can be deleted.
6066 */
6167 @ Test
62- public void testNoJarFileLeak () throws Exception {
63- final URL jarConfigURL = prepareJarConfigURL ();
68+ void testNoJarFileLeak () throws Exception {
69+ final Path jarFile = prepareJarConfigURL (tempDir );
70+ final URL jarConfigURL = new URL ("jar:" + jarFile .toUri ().toURL () + "!" + PATH_IN_JAR );
6471 final long expected = getOpenFileDescriptorCount ();
6572 UrlConnectionFactory .createConnection (jarConfigURL ).getInputStream ().close ();
6673 // This can only fail on UNIX
6774 assertEquals (expected , getOpenFileDescriptorCount ());
6875 // This can only fail on Windows
6976 try {
70- Files .delete (JAR_FILE );
77+ Files .delete (jarFile );
7178 } catch (IOException e ) {
7279 fail (e );
7380 }
7481 }
7582
7683 @ Test
7784 public void testLoadConfigurationSourceFromJarFile () throws Exception {
78- final URL jarConfigURL = prepareJarConfigURL ();
85+ final Path jarFile = prepareJarConfigURL (tempDir );
86+ final URL jarConfigURL = new URL ("jar:" + jarFile .toUri ().toURL () + "!" + PATH_IN_JAR );
7987 final long expectedFdCount = getOpenFileDescriptorCount ();
8088 ConfigurationSource configSource = ConfigurationSource .fromUri (jarConfigURL .toURI ());
8189 assertNotNull (configSource );
@@ -90,7 +98,7 @@ public void testLoadConfigurationSourceFromJarFile() throws Exception {
9098 assertEquals (expectedFdCount , getOpenFileDescriptorCount ());
9199 // This can only fail on Windows
92100 try {
93- Files .delete (JAR_FILE );
101+ Files .delete (jarFile );
94102 } catch (IOException e ) {
95103 fail (e );
96104 }
@@ -104,22 +112,18 @@ private long getOpenFileDescriptorCount() {
104112 return 0L ;
105113 }
106114
107- public static URL prepareJarConfigURL () throws IOException {
108- if (!Files .exists (JAR_FILE )) {
109- final Manifest manifest = new Manifest ();
110- manifest .getMainAttributes ().put (Attributes .Name .MANIFEST_VERSION , "1.0" );
111- try (final OutputStream os = Files .newOutputStream (JAR_FILE );
112- final JarOutputStream jar = new JarOutputStream (os , manifest );
113- final InputStream config = Files .newInputStream (CONFIG_FILE )) {
114- final JarEntry jarEntry = new JarEntry ("config/console.xml" );
115- jar .putNextEntry (jarEntry );
116- int len ;
117- while ((len = config .read (buffer )) != -1 ) {
118- jar .write (buffer , 0 , len );
119- }
120- jar .closeEntry ();
121- }
115+ public static Path prepareJarConfigURL (Path dir ) throws IOException {
116+ Path jarFile = dir .resolve ("jarFile.jar" );
117+ final Manifest manifest = new Manifest ();
118+ manifest .getMainAttributes ().put (Attributes .Name .MANIFEST_VERSION , "1.0" );
119+ try (final OutputStream os = Files .newOutputStream (jarFile );
120+ final JarOutputStream jar = new JarOutputStream (os , manifest );
121+ final InputStream config =
122+ requireNonNull (ConfigurationSourceTest .class .getResourceAsStream (CONFIG_FILE ))) {
123+ final JarEntry jarEntry = new JarEntry ("config/console.xml" );
124+ jar .putNextEntry (jarEntry );
125+ IOUtils .copy (config , os );
122126 }
123- return new URL ( "jar:" + JAR_FILE . toUri (). toURL () + "!/config/console.xml" ) ;
127+ return jarFile ;
124128 }
125129}
0 commit comments