You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/06/14 20:09:16 UTC
[logging-log4j2] branch 2.x updated: Create test jar file programmatically
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 2916104f0b Create test jar file programmatically
2916104f0b is described below
commit 2916104f0babb9a0d2166a2e6263128e0d611624
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Wed Jun 14 22:04:14 2023 +0200
Create test jar file programmatically
Creating the JAR file for the `ConfigurationSourceTest` programmatically
removes some security warnings.
---
log4j-core-test/src/main/resources/jarfile.jar | Bin 1122 -> 0 bytes
.../src/main/resources/jarfile/config/console.xml | 27 ----------------
.../log4j/core/config/ConfigurationSourceTest.java | 35 ++++++++++++++++-----
3 files changed, 28 insertions(+), 34 deletions(-)
diff --git a/log4j-core-test/src/main/resources/jarfile.jar b/log4j-core-test/src/main/resources/jarfile.jar
deleted file mode 100644
index bf34b8c468..0000000000
Binary files a/log4j-core-test/src/main/resources/jarfile.jar and /dev/null differ
diff --git a/log4j-core-test/src/main/resources/jarfile/config/console.xml b/log4j-core-test/src/main/resources/jarfile/config/console.xml
deleted file mode 100644
index ff2ba891a1..0000000000
--- a/log4j-core-test/src/main/resources/jarfile/config/console.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- ~ Licensed to the Apache Software Foundation (ASF) under one or more
- ~ contributor license agreements. See the NOTICE file distributed with
- ~ this work for additional information regarding copyright ownership.
- ~ The ASF licenses this file to you under the Apache License, Version 2.0
- ~ (the "License"); you may not use this file except in compliance with
- ~ the License. You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
-<Configuration status="WARN">
- <Appenders>
- <Console name="Console" />
- </Appenders>
- <Loggers>
- <Root level="TRACE">
- <AppenderRef ref="Console" />
- </Root>
- </Loggers>
-</Configuration>
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
index 91b3a5714c..bb47da565a 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationSourceTest.java
@@ -18,12 +18,18 @@ package org.apache.logging.log4j.core.config;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
import com.sun.management.UnixOperatingSystemMXBean;
import org.apache.logging.log4j.core.net.UrlConnectionFactory;
@@ -36,8 +42,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ConfigurationSourceTest {
- private static final Path JAR_FILE = Paths.get("target", "classes", "jarfile.jar");
- private static final Path TEST_JARFILE = Paths.get("target", "classes", "jarfile-copy.jar");
+ private static final Path JAR_FILE = Paths.get("target", "test-classes", "jarfile.jar");
+ private static final Path CONFIG_FILE = Paths.get("target","test-classes", "log4j2-console.xml");
+ private static final byte[] buffer = new byte[1024];
@Test
public void testJira_LOG4J2_2770_byteArray() throws Exception {
@@ -60,7 +67,7 @@ public class ConfigurationSourceTest {
assertEquals(expected, getOpenFileDescriptorCount());
// This can only fail on Windows
try {
- Files.delete(TEST_JARFILE);
+ Files.delete(JAR_FILE);
} catch (IOException e) {
fail(e);
}
@@ -71,6 +78,7 @@ public class ConfigurationSourceTest {
final URL jarConfigURL = prepareJarConfigURL();
final long expectedFdCount = getOpenFileDescriptorCount();
ConfigurationSource configSource = ConfigurationSource.fromUri(jarConfigURL.toURI());
+ assertNotNull(configSource);
assertEquals(jarConfigURL.toString(), configSource.getLocation());
assertNull(configSource.getFile());
assertTrue(configSource.getLastModified() > 0);
@@ -82,7 +90,7 @@ public class ConfigurationSourceTest {
assertEquals(expectedFdCount, getOpenFileDescriptorCount());
// This can only fail on Windows
try {
- Files.delete(TEST_JARFILE);
+ Files.delete(JAR_FILE);
} catch (IOException e) {
fail(e);
}
@@ -97,9 +105,22 @@ public class ConfigurationSourceTest {
}
private static URL prepareJarConfigURL() throws IOException {
- if(!Files.exists(TEST_JARFILE))
- Files.copy(JAR_FILE, TEST_JARFILE);
- return new URL("jar:" + TEST_JARFILE.toUri().toURL() + "!/config/console.xml");
+ if (!Files.exists(JAR_FILE)) {
+ final Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+ try (final OutputStream os = Files.newOutputStream(JAR_FILE);
+ final JarOutputStream jar = new JarOutputStream(os, manifest);
+ final InputStream config = Files.newInputStream(CONFIG_FILE)) {
+ final JarEntry jarEntry = new JarEntry("config/console.xml");
+ jar.putNextEntry(jarEntry);
+ int len;
+ while ((len = config.read(buffer)) != -1) {
+ jar.write(buffer, 0, len);
+ }
+ jar.closeEntry();
+ }
+ }
+ return new URL("jar:" + JAR_FILE.toUri().toURL() + "!/config/console.xml");
}
}