16
16
*/
17
17
package org .apache .logging .log4j .core .config ;
18
18
19
+ import static java .util .Objects .requireNonNull ;
19
20
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
21
import static org .junit .jupiter .api .Assertions .assertNotNull ;
21
22
import static org .junit .jupiter .api .Assertions .assertNull ;
32
33
import java .net .URL ;
33
34
import java .nio .file .Files ;
34
35
import java .nio .file .Path ;
35
- import java .nio .file .Paths ;
36
36
import java .util .jar .Attributes ;
37
37
import java .util .jar .JarEntry ;
38
38
import java .util .jar .JarOutputStream ;
39
39
import java .util .jar .Manifest ;
40
+ import org .apache .commons .io .IOUtils ;
40
41
import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
41
42
import org .junit .jupiter .api .Test ;
43
+ import org .junit .jupiter .api .io .TempDir ;
42
44
43
45
public 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 ;
47
55
48
56
@ Test
49
- public void testJira_LOG4J2_2770_byteArray () throws Exception {
57
+ void testJira_LOG4J2_2770_byteArray () throws Exception {
50
58
final ConfigurationSource configurationSource =
51
59
new ConfigurationSource (new ByteArrayInputStream (new byte [] {'a' , 'b' }));
52
60
assertNotNull (configurationSource .resetInputStream ());
53
61
}
54
62
55
63
/**
56
64
* 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.
60
66
*/
61
67
@ 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 );
64
71
final long expected = getOpenFileDescriptorCount ();
65
72
UrlConnectionFactory .createConnection (jarConfigURL ).getInputStream ().close ();
66
73
// This can only fail on UNIX
67
74
assertEquals (expected , getOpenFileDescriptorCount ());
68
75
// This can only fail on Windows
69
76
try {
70
- Files .delete (JAR_FILE );
77
+ Files .delete (jarFile );
71
78
} catch (IOException e ) {
72
79
fail (e );
73
80
}
74
81
}
75
82
76
83
@ Test
77
84
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 );
79
87
final long expectedFdCount = getOpenFileDescriptorCount ();
80
88
ConfigurationSource configSource = ConfigurationSource .fromUri (jarConfigURL .toURI ());
81
89
assertNotNull (configSource );
@@ -90,7 +98,7 @@ public void testLoadConfigurationSourceFromJarFile() throws Exception {
90
98
assertEquals (expectedFdCount , getOpenFileDescriptorCount ());
91
99
// This can only fail on Windows
92
100
try {
93
- Files .delete (JAR_FILE );
101
+ Files .delete (jarFile );
94
102
} catch (IOException e ) {
95
103
fail (e );
96
104
}
@@ -104,22 +112,18 @@ private long getOpenFileDescriptorCount() {
104
112
return 0L ;
105
113
}
106
114
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 );
122
126
}
123
- return new URL ( "jar:" + JAR_FILE . toUri (). toURL () + "!/config/console.xml" ) ;
127
+ return jarFile ;
124
128
}
125
129
}
0 commit comments