From 102a1a31aa7e1a64caf9612688b7cd38a140069a Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Mon, 16 Sep 2024 11:51:14 +0200 Subject: [PATCH 1/3] Use a random `${test:logging.path}` The `@TempLoggingDir` JUnit 5 extensions create a totally deterministic directory to hold the log files for tests. Since the directory is deleted only if all the tests succeed, this causes a problem with test reruns. --- .../test/junit/TempLoggingDirectory.java | 44 ++++++++++++++----- .../test/junit/TempLoggingDirectoryTest.java | 6 ++- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TempLoggingDirectory.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TempLoggingDirectory.java index d222f6dc173..ef97ff2378e 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TempLoggingDirectory.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TempLoggingDirectory.java @@ -51,7 +51,8 @@ public void beforeAll(ExtensionContext context) throws Exception { Path loggingPath = null; for (final Field field : fields) { if (loggingPath != null) { - StatusLogger.getLogger().warn("Multiple fields with @TempLoggingDir annotation are not supported."); + StatusLogger.getLogger() + .warn("Multiple static fields with @TempLoggingDir annotation are not supported."); } else { final CleanupMode cleanup = determineCleanupMode(field); loggingPath = createLoggingPath(context, cleanup).getPath(); @@ -63,8 +64,8 @@ public void beforeAll(ExtensionContext context) throws Exception { @Override public void beforeEach(ExtensionContext context) throws Exception { - // JUnit 5 does not set an error on the parent context if one of the children - // fail. We record the list of children. + // JUnit 5 does not set an error on the parent context if one of the children fails. + // We record the list of children. context.getParent().ifPresent(c -> { final PathHolder holder = ExtensionContextAnchor.getAttribute(PathHolder.class, PathHolder.class, c); if (holder != null) { @@ -78,7 +79,8 @@ public void beforeEach(ExtensionContext context) throws Exception { final Object instance = context.getRequiredTestInstance(); for (final Field field : fields) { if (loggingPath != null) { - StatusLogger.getLogger().warn("Multiple fields with @TempLoggingDir annotation are not supported."); + StatusLogger.getLogger() + .warn("Multiple instance fields with @TempLoggingDir annotation are not supported."); } else { final CleanupMode cleanup = determineCleanupMode(field); loggingPath = createLoggingPath(context, cleanup).getPath(); @@ -102,7 +104,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte throws ParameterResolutionException { final TempLoggingDir annotation = parameterContext.findAnnotation(TempLoggingDir.class).get(); - // Get or create temporary directory + // Get or create a temporary directory PathHolder holder = ExtensionContextAnchor.getAttribute(PathHolder.class, PathHolder.class, extensionContext); if (holder == null || !extensionContext.equals(holder.getMainContext())) { final CleanupMode mode = determineCleanupMode(annotation); @@ -111,14 +113,9 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte return holder.getPath(); } - private PathHolder createLoggingPath(final ExtensionContext context, final CleanupMode cleanup) { + private PathHolder createLoggingPath(ExtensionContext context, CleanupMode cleanup) { final TestProperties props = TestPropertySource.createProperties(context); - // Create temporary directory - final String baseDir = System.getProperty("basedir"); - final Path basePath = (baseDir != null ? Paths.get(baseDir, "target") : Paths.get(".")).resolve("logs"); - final Class clazz = context.getRequiredTestClass(); - final String dir = clazz.getName().replaceAll("[.$]", File.separatorChar == '\\' ? "\\\\" : File.separator); - final Path perClassPath = basePath.resolve(dir); + final Path perClassPath = determinePerClassPath(context); // Per test subfolder final Path loggingPath = context.getTestMethod() .map(m -> perClassPath.resolve(m.getName())) @@ -135,6 +132,29 @@ private PathHolder createLoggingPath(final ExtensionContext context, final Clean return holder; } + private Path determinePerClassPath(ExtensionContext context) { + // Check if the parent context already created a folder + PathHolder holder = ExtensionContextAnchor.getAttribute(PathHolder.class, PathHolder.class, context); + if (holder == null) { + try { + // Create temporary per-class directory + final String baseDir = System.getProperty("basedir"); + final Path basePath = (baseDir != null ? Paths.get(baseDir, "target") : Paths.get(".")).resolve("logs"); + final Class clazz = context.getRequiredTestClass(); + final Package pkg = clazz.getPackage(); + final String dir = + pkg.getName().replaceAll("[.$]", File.separatorChar == '\\' ? "\\\\" : File.separator); + // Create a temporary directory that uses the simple class name as prefix + Path packagePath = basePath.resolve(dir); + Files.createDirectories(packagePath); + return Files.createTempDirectory(packagePath, clazz.getSimpleName()); + } catch (final IOException e) { + throw new ExtensionContextException("Failed to create temporary directory.", e); + } + } + return holder.getPath(); + } + private CleanupMode determineCleanupMode(final TempLoggingDir annotation) { final CleanupMode mode = annotation.cleanup(); // TODO: use JupiterConfiguration diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/test/junit/TempLoggingDirectoryTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/test/junit/TempLoggingDirectoryTest.java index 846a044e798..0a1dd2f15f3 100644 --- a/log4j-api-test/src/test/java/org/apache/logging/log4j/test/junit/TempLoggingDirectoryTest.java +++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/test/junit/TempLoggingDirectoryTest.java @@ -20,13 +20,14 @@ import java.nio.file.Path; import java.nio.file.Paths; +import java.util.regex.Pattern; import org.apache.logging.log4j.test.TestProperties; import org.junit.jupiter.api.Test; @UsingTestProperties public class TempLoggingDirectoryTest { - private static final Path PER_CLASS_PATH = Paths.get("TempLoggingDirectoryTest"); + private static final Pattern PER_CLASS_PATH = Pattern.compile("TempLoggingDirectoryTest\\d+"); private static final Path PER_TEST_PATH = Paths.get("testInjectedFields"); @TempLoggingDir @@ -37,7 +38,8 @@ public class TempLoggingDirectoryTest { @Test void testInjectedFields(final @TempLoggingDir Path parameterLoggingPath, final TestProperties props) { - assertThat(staticLoggingPath).exists().endsWith(PER_CLASS_PATH); + assertThat(staticLoggingPath).exists(); + assertThat(staticLoggingPath.getFileName().toString()).matches(PER_CLASS_PATH); assertThat(instanceLoggingPath).exists().startsWith(staticLoggingPath).endsWith(PER_TEST_PATH); assertThat(parameterLoggingPath).isEqualTo(instanceLoggingPath); assertThat(props.getProperty(TestProperties.LOGGING_PATH)).isEqualTo(instanceLoggingPath.toString()); From 4a9c2b26e95c55e34833c5862ea5db0e477d6765 Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Tue, 1 Oct 2024 11:17:29 +0200 Subject: [PATCH 2/3] Remove ancient test fixtures and dependencies This removes the test dependency on `oro:oro` and the `VelocityTest`, which uses Apache Velocity 1.7 for no clear reason. --- log4j-1.2-api/pom.xml | 34 +++--- .../java/org/apache/log4j/VelocityTest.java | 60 ---------- .../log4j/util/AbsoluteDateAndTimeFilter.java | 35 ------ .../apache/log4j/util/AbsoluteTimeFilter.java | 34 ------ .../java/org/apache/log4j/util/Compare.java | 106 +----------------- .../org/apache/log4j/util/ControlFilter.java | 10 +- .../util/EnhancedJunitTestRunnerFilter.java | 64 ----------- .../log4j/util/EnhancedLineNumberFilter.java | 41 ------- .../java/org/apache/log4j/util/Filter.java | 15 +-- .../org/apache/log4j/util/ISO8601Filter.java | 11 +- .../log4j/util/JunitTestRunnerFilter.java | 25 ++--- .../apache/log4j/util/LineNumberFilter.java | 15 ++- .../apache/log4j/util/RelativeTimeFilter.java | 35 ------ .../apache/log4j/util/SunReflectFilter.java | 18 ++- .../org/apache/log4j/util/Transformer.java | 18 +-- .../log4j/util/XMLLineAttributeFilter.java | 35 ------ ...TimestampFilter.java => package-info.java} | 16 +-- log4j-1.2-api/src/test/resources/hello.vm | 6 - 18 files changed, 58 insertions(+), 520 deletions(-) delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/VelocityTest.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteDateAndTimeFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteTimeFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedJunitTestRunnerFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedLineNumberFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/RelativeTimeFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLLineAttributeFilter.java rename log4j-1.2-api/src/test/java/org/apache/log4j/util/{XMLTimestampFilter.java => package-info.java} (68%) delete mode 100644 log4j-1.2-api/src/test/resources/hello.vm diff --git a/log4j-1.2-api/pom.xml b/log4j-1.2-api/pom.xml index 097921ad56d..090439a2153 100644 --- a/log4j-1.2-api/pom.xml +++ b/log4j-1.2-api/pom.xml @@ -51,85 +51,91 @@ + javax.jms javax.jms-api provided - true - + + + org.jspecify + jspecify + test + + org.apache.logging.log4j log4j-api + org.apache.logging.log4j log4j-core true + org.apache.logging.log4j log4j-api-test test + org.apache.logging.log4j log4j-core-test test + org.awaitility awaitility test + commons-io commons-io test + org.apache.commons commons-lang3 test + com.fasterxml.jackson.dataformat jackson-dataformat-xml test + org.junit.jupiter junit-jupiter-engine test + org.junit.jupiter junit-jupiter-params test - + org.junit.vintage junit-vintage-engine test + org.mockito mockito-core test - - - oro - oro - test - - - org.apache.velocity - velocity - test - + diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/VelocityTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/VelocityTest.java deleted file mode 100644 index d46e49cc3de..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/VelocityTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j; - -import java.io.StringWriter; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.config.Configurator; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; -import org.apache.velocity.app.Velocity; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Note that this test must clean up after itself or it may cause other tests to fail. - */ -public class VelocityTest { - - private static LoggerContext context; - - @BeforeClass - public static void setupClass() { - context = LoggerContext.getContext(false); - } - - @AfterClass - public static void tearDownClass() { - Configurator.shutdown(context); - StatusLogger.getLogger().reset(); - } - - @Test - public void testVelocity() { - Velocity.init(); - final VelocityContext vContext = new VelocityContext(); - vContext.put("name", "Velocity"); - - final Template template = Velocity.getTemplate("target/test-classes/hello.vm"); - - final StringWriter sw = new StringWriter(); - - template.merge(vContext, sw); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteDateAndTimeFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteDateAndTimeFilter.java deleted file mode 100644 index 538e7228b7c..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteDateAndTimeFilter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.apache.oro.text.perl.Perl5Util; - -public class AbsoluteDateAndTimeFilter implements Filter { - - Perl5Util util = new Perl5Util(); - - @Override - public String filter(final String in) { - final String pat = "/" + Filter.ABSOLUTE_DATE_AND_TIME_PAT + "/"; - - if (util.match(pat, in)) { - return util.substitute("s/" + Filter.ABSOLUTE_DATE_AND_TIME_PAT + "//", in); - } else { - return in; - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteTimeFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteTimeFilter.java deleted file mode 100644 index d9c173668e5..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/AbsoluteTimeFilter.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.apache.oro.text.perl.Perl5Util; - -public class AbsoluteTimeFilter implements Filter { - - Perl5Util util = new Perl5Util(); - - @Override - public String filter(final String in) { - final String pat = "/" + Filter.ABSOLUTE_TIME_PAT + "/"; - - if (util.match(pat, in)) { - return util.substitute("s/" + Filter.ABSOLUTE_TIME_PAT + "//", in); - } - return in; - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java index 36af9f5e295..e540ba53c66 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java @@ -17,66 +17,12 @@ package org.apache.log4j.util; import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; public class Compare { - static final int B1_NULL = -1; - static final int B2_NULL = -2; - - public static boolean compare(final Class testClass, final String file1, final String file2) throws IOException { - try (final BufferedReader in1 = new BufferedReader(new FileReader(file1)); - final BufferedReader in2 = new BufferedReader(new InputStreamReader(open(testClass, file2)))) { - return compare(testClass, file1, file2, in1, in2); - } - } - - public static boolean compare( - final Class testClass, - final String file1, - final String file2, - final BufferedReader in1, - final BufferedReader in2) - throws IOException { - - String s1; - int lineCounter = 0; - - while ((s1 = in1.readLine()) != null) { - lineCounter++; - - final String s2 = in2.readLine(); - - if (!s1.equals(s2)) { - System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter); - System.out.println("One reads: [" + s1 + "]."); - System.out.println("Other reads:[" + s2 + "]."); - outputFile(testClass, file1); - outputFile(testClass, file2); - - return false; - } - } - - // the second file is longer - if (in2.read() != -1) { - System.out.println("File [" + file2 + "] longer than file [" + file1 + "]."); - outputFile(testClass, file1); - outputFile(testClass, file2); - - return false; - } - - return true; - } - - public static boolean compare(final String file1, final String file2) throws FileNotFoundException, IOException { + public static boolean compare(final String file1, final String file2) throws IOException { try (final BufferedReader in1 = new BufferedReader(new FileReader(file1)); final BufferedReader in2 = new BufferedReader(new FileReader(file2))) { @@ -102,54 +48,4 @@ public static boolean compare(final String file1, final String file2) throws Fil return true; } } - - private static final InputStream open(final Class testClass, final String fileName) throws IOException { - String resourceName = fileName; - if (fileName.startsWith("witness/")) { - resourceName = fileName.substring(fileName.lastIndexOf('/') + 1); - } - InputStream is = testClass.getResourceAsStream(resourceName); - if (is == null) { - final File file = new File(fileName); - if (file.exists()) { - is = new FileInputStream(file); - } else { - throw new FileNotFoundException("Resource " + resourceName + " not found"); - } - } - return is; - } - - /** - * - * Prints file on the console. - * - */ - private static void outputFile(final Class testClass, final String file) throws IOException { - try (final InputStream is = open(testClass, file); - final BufferedReader in1 = new BufferedReader(new InputStreamReader(is))) { - - String s1; - int lineCounter = 0; - System.out.println("--------------------------------"); - System.out.println("Contents of " + file + ":"); - - while ((s1 = in1.readLine()) != null) { - lineCounter++; - System.out.print(lineCounter); - - if (lineCounter < 10) { - System.out.print(" : "); - } else if (lineCounter < 100) { - System.out.print(" : "); - } else if (lineCounter < 1000) { - System.out.print(" : "); - } else { - System.out.print(": "); - } - - System.out.println(s1); - } - } - } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java index a2f872a1ae8..ad1c83e4794 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java @@ -17,12 +17,9 @@ package org.apache.log4j.util; import java.util.Arrays; -import org.apache.oro.text.perl.Perl5Util; public class ControlFilter implements Filter { - Perl5Util util = new Perl5Util(); - String[] allowedPatterns; public ControlFilter(final String[] allowedPatterns) { @@ -31,11 +28,8 @@ public ControlFilter(final String[] allowedPatterns) { @Override public String filter(final String in) throws UnexpectedFormatException { - final int len = allowedPatterns.length; - for (int i = 0; i < len; i++) { - // System.out.println("["+allowedPatterns[i]+"]"); - if (util.match("/" + allowedPatterns[i] + "/", in)) { - // System.out.println("["+in+"] matched ["+allowedPatterns[i]); + for (String allowedPattern : allowedPatterns) { + if (in.matches(allowedPattern)) { return in; } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedJunitTestRunnerFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedJunitTestRunnerFilter.java deleted file mode 100644 index 7b44b1c0f9d..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedJunitTestRunnerFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.apache.oro.text.perl.Perl5Util; - -public class EnhancedJunitTestRunnerFilter implements Filter { - private static final String[] PATTERNS = { - "at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner", - "at org.apache.tools.ant", - "at junit.textui.TestRunner", - "at com.intellij.rt.execution.junit", - "at java.lang.reflect.Method.invoke", - "at org.apache.maven.", - "at org.codehaus.", - "at org.junit.internal.runners.", - "at junit.framework.JUnit4TestAdapter" - }; - - private final Perl5Util util = new Perl5Util(); - - public EnhancedJunitTestRunnerFilter() {} - - /** - * Filter out stack trace lines coming from the various JUnit TestRunners. - */ - @Override - public String filter(final String in) { - if (in == null) { - return null; - } - - // - // restore the one instance of Method.invoke that we actually want - // - if (in.indexOf("at junit.framework.TestCase.runTest") != -1) { - return "\tat java.lang.reflect.Method.invoke(X)\n\t" + in.trim(); - } - - for (final String element : PATTERNS) { - if (in.indexOf(element) != -1) { - return null; - } - } - if (util.match("/\\sat /", in)) { - return "\t" + in.trim(); - } - return in; - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedLineNumberFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedLineNumberFilter.java deleted file mode 100644 index fa8132adf0f..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/EnhancedLineNumberFilter.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.util.regex.Pattern; - -public class EnhancedLineNumberFilter implements Filter { - private final Pattern linePattern; - private final Pattern nativePattern; - - public EnhancedLineNumberFilter() { - linePattern = Pattern.compile("\\(.*:\\d{1,4}\\)"); - nativePattern = Pattern.compile("\\(Native Method\\)"); - } - - @Override - public String filter(final String in) { - - if (linePattern.matcher(in).find()) { - return linePattern.matcher(in).replaceAll("(X)"); - } else if (nativePattern.matcher(in).find()) { - return nativePattern.matcher(in).replaceAll("(X)"); - } else { - return in; - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java index f3ebdcea693..8c6fab310c2 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java @@ -16,19 +16,12 @@ */ package org.apache.log4j.util; -public interface Filter { - - final String BASIC_PAT = "\\[main\\] (FATAL|ERROR|WARN|INFO|DEBUG)"; - final String ISO8601_PAT = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}"; +import org.jspecify.annotations.Nullable; - // 06 avr. 2002 18:36:32,036 - // 18 fevr. 2002 20:05:36,222 - public static final String ABSOLUTE_DATE_AND_TIME_PAT = "^\\d{1,2} .{2,6}\\.? 2\\d{3} \\d{2}:\\d{2}:\\d{2},\\d{3}"; - - // 18:54:19,201 - public static final String ABSOLUTE_TIME_PAT = "^\\d{2}:\\d{2}:\\d{2},\\d{3}"; +public interface Filter { - public static final String RELATIVE_TIME_PAT = "^\\d{1,10}"; + String ISO8601_PAT = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}"; + @Nullable String filter(String in) throws UnexpectedFormatException; } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java index 023337e9af5..0ee54d4b05e 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java @@ -16,19 +16,10 @@ */ package org.apache.log4j.util; -import org.apache.oro.text.perl.Perl5Util; - public class ISO8601Filter implements Filter { - Perl5Util util = new Perl5Util(); - @Override public String filter(final String in) { - final String pat = "/" + ISO8601_PAT + "/"; - - if (util.match(pat, in)) { - return util.substitute("s/" + ISO8601_PAT + "//", in); - } - return in; + return in.replaceFirst(ISO8601_PAT, ""); } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java index bd18f8893ae..6a77d457e7d 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java @@ -16,35 +16,32 @@ */ package org.apache.log4j.util; -import org.apache.oro.text.perl.Perl5Util; +import java.util.regex.Pattern; +import org.jspecify.annotations.Nullable; public class JunitTestRunnerFilter implements Filter { - Perl5Util util = new Perl5Util(); /** * Filter out stack trace lines coming from the various JUnit TestRunners. */ @Override - public String filter(final String in) { - if (in == null) { - return null; - } + public @Nullable String filter(final String in) { - if (util.match("/at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner/", in)) { + if (in.contains("at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner")) { return null; - } else if (util.match("/at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner/", in)) { + } else if (in.contains("at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner")) { return null; - } else if (util.match("/at com.intellij/", in)) { + } else if (in.contains("at com.intellij")) { return null; - } else if (in.indexOf("at junit.") >= 0 && in.indexOf("ui.TestRunner") >= 0) { + } else if (in.contains("at junit.") && in.contains("ui.TestRunner")) { return null; - } else if (in.indexOf("org.apache.maven") >= 0) { + } else if (in.contains("org.apache.maven")) { return null; - } else if (in.indexOf("junit.internal") >= 0) { + } else if (in.contains("junit.internal")) { return null; - } else if (in.indexOf("JUnit4TestAdapter") >= 0) { + } else if (in.contains("JUnit4TestAdapter")) { return null; - } else if (util.match("/\\sat /", in)) { + } else if (Pattern.compile("\\sat ").matcher(in).find()) { return "\t" + in.trim(); } else { return in; diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java index 3148d927eb9..d3bba3f9947 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java @@ -16,20 +16,19 @@ */ package org.apache.log4j.util; -import org.apache.oro.text.perl.Perl5Util; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class LineNumberFilter implements Filter { - Perl5Util util = new Perl5Util(); + private static final Pattern LINE_NUMBER_PATTERN = Pattern.compile(":\\d+\\)"); @Override public String filter(final String in) { - if (util.match("/\\(.*:\\d{1,4}\\)/", in)) { - return util.substitute("s/:\\d{1,4}\\)/:XXX)/", in); + Matcher matcher = LINE_NUMBER_PATTERN.matcher(in); + if (matcher.find()) { + return matcher.replaceFirst(":XXX"); } - if (in.indexOf(", Compiled Code") >= 0) { - return util.substitute("s/, Compiled Code/:XXX/", in); - } - return in; + return in.replaceFirst(", Compiled Code", ":XXX"); } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/RelativeTimeFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/RelativeTimeFilter.java deleted file mode 100644 index 3f7c251478a..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/RelativeTimeFilter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.apache.oro.text.perl.Perl5Util; - -public class RelativeTimeFilter implements Filter { - - Perl5Util util = new Perl5Util(); - - @Override - public String filter(final String in) { - final String pat = "/" + Filter.RELATIVE_TIME_PAT + "/"; - - if (util.match(pat, in)) { - // System.out.println("Removing relative time from line ["+in+"]"); - return util.substitute("s/" + Filter.RELATIVE_TIME_PAT + "//", in); - } - return in; - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java index 07866f0294c..c54755bcb98 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java @@ -16,27 +16,23 @@ */ package org.apache.log4j.util; -import org.apache.oro.text.perl.Perl5Util; +import org.jspecify.annotations.Nullable; /** * The sun.reflect.* and java.lang.reflect.* lines are not present in all JDKs. */ public class SunReflectFilter implements Filter { - Perl5Util util = new Perl5Util(); @Override - public String filter(final String in) { - if ((in == null) || util.match("/at sun.reflect/", in) || (in.indexOf("at java.lang.reflect.") >= 0)) { + public @Nullable String filter(final String in) { + if (in.contains("at sun.reflect") || in.contains("at java.lang.reflect")) { return null; } - if (in.indexOf("Compiled Code") >= 0) { - if (in.indexOf("junit.framework.TestSuite") >= 0) { - return util.substitute("s/Compiled Code/TestSuite.java:XXX/", in); + if (in.contains("Compiled Code")) { + if (in.contains("junit.framework.TestSuite")) { + return in.replaceFirst("Compiled Code", "TestSuite.java:XXX"); } } - if (util.match("/\\(Method.java:.*\\)/", in)) { - return util.substitute("s/\\(Method.java:.*\\)/(Native Method)/", in); - } - return in; + return in.replaceFirst("\\(Method.java:.*\\)", "(Native Method)"); } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java index d28d4003eb0..3c6e4983440 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java @@ -24,20 +24,6 @@ public class Transformer { - public static void transform(final String in, final String out, final Filter filter) - throws IOException, UnexpectedFormatException { - - String line; - final BufferedReader input = new BufferedReader(new FileReader(in)); - final PrintStream output = new PrintStream(new FileOutputStream(out)); - - // Initialization of input and output omitted - while ((line = input.readLine()) != null) { - line = filter.filter(line); - output.println(line); - } - } - public static void transform(final String in, final String out, final Filter[] filters) throws IOException, UnexpectedFormatException { @@ -49,7 +35,9 @@ public static void transform(final String in, final String out, final Filter[] f while ((line = input.readLine()) != null) { // apply all filters for (final Filter filter : filters) { - line = filter.filter(line); + if (line != null) { + line = filter.filter(line); + } } if (line != null) { output.println(line); diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLLineAttributeFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLLineAttributeFilter.java deleted file mode 100644 index 86727f436c8..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLLineAttributeFilter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.apache.oro.text.perl.Perl5Util; - -public class XMLLineAttributeFilter implements Filter { - - Perl5Util util = new Perl5Util(); - - @Override - public String filter(final String in) { - if (util.match("/line=\"\\d{1,3}\"/", in)) { - return util.substitute("s/line=\"\\d{1,3}\"/line=\"X\"/", in); - } else if (util.match("/line=\"?\"/", in)) { - return util.substitute("s/line=\"?\"/line=\"X\"/", in); - } else { - return in; - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLTimestampFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java similarity index 68% rename from log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLTimestampFilter.java rename to log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java index 6a0fb531c5a..ed3ee99b4e0 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/XMLTimestampFilter.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java @@ -14,19 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@NullMarked package org.apache.log4j.util; -import org.apache.oro.text.perl.Perl5Util; - -public class XMLTimestampFilter implements Filter { - - Perl5Util util = new Perl5Util(); - - @Override - public String filter(final String in) { - if (util.match("/timestamp=\"\\d{10,13}\"/", in)) { - return util.substitute("s/timestamp=\"\\d{10,13}\"/timestamp=\"XXX\"/", in); - } - return in; - } -} +import org.jspecify.annotations.NullMarked; diff --git a/log4j-1.2-api/src/test/resources/hello.vm b/log4j-1.2-api/src/test/resources/hello.vm deleted file mode 100644 index 5ce97550285..00000000000 --- a/log4j-1.2-api/src/test/resources/hello.vm +++ /dev/null @@ -1,6 +0,0 @@ - - - #set( $foo = "Velocity" ) -Hello $foo World! - - \ No newline at end of file From 2cc3b16794214fffe0a3a68bc2aa9faefeeeeb52 Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Mon, 7 Oct 2024 14:15:47 +0200 Subject: [PATCH 3/3] Remove disabled `log4j-1.2-api` tests Since `log4j-1.2-api` is a legacy component, reactivating disabled tests is not worth the effort. Therefore, we remove all the disabled tests and the test fixtures they used. --- .../apache/log4j/xml/XmlConfiguration.java | 2 - .../java/org/apache/log4j/util/Compare.java | 51 --- .../org/apache/log4j/util/ControlFilter.java | 39 -- .../java/org/apache/log4j/util/Filter.java | 27 -- .../org/apache/log4j/util/ISO8601Filter.java | 25 -- .../log4j/util/JunitTestRunnerFilter.java | 50 --- .../apache/log4j/util/LineNumberFilter.java | 34 -- .../apache/log4j/util/SunReflectFilter.java | 38 -- .../org/apache/log4j/util/Transformer.java | 47 --- .../log4j/util/UnexpectedFormatException.java | 26 -- .../org/apache/log4j/util/package-info.java | 20 - .../org/apache/log4j/xml/DOMTestCase.java | 383 ++---------------- .../test/resources/DOMTestCase/DOMTest4.xml | 44 -- .../resources/DOMTestCase/DOMTest4_A1.xml | 26 -- .../resources/DOMTestCase/DOMTest4_A2.xml | 24 -- .../DOMTestCase/categoryfactory1.xml | 35 -- .../DOMTestCase/categoryfactory2.xml | 38 -- .../resources/DOMTestCase/loggerfactory1.xml | 35 -- ...enderer1.xml => testThrowableRenderer.xml} | 8 +- 19 files changed, 34 insertions(+), 918 deletions(-) delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/UnexpectedFormatException.java delete mode 100644 log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4.xml delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A1.xml delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A2.xml delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory1.xml delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory2.xml delete mode 100644 log4j-1.2-api/src/test/resources/DOMTestCase/loggerfactory1.xml rename log4j-1.2-api/src/test/resources/DOMTestCase/{throwableRenderer1.xml => testThrowableRenderer.xml} (86%) diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java index 0aeaa776750..dd2a0312b06 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java @@ -90,7 +90,6 @@ public class XmlConfiguration extends Log4j1Configuration { private static final String INTERNAL_DEBUG_ATTR = "debug"; private static final String THRESHOLD_ATTR = "threshold"; private static final String EMPTY_STR = ""; - private static final Class[] ONE_STRING_PARAM = new Class[] {String.class}; private static final String dbfKey = "javax.xml.parsers.DocumentBuilderFactory"; private static final String THROWABLE_RENDERER_TAG = "throwableRenderer"; @@ -504,7 +503,6 @@ private void parseErrorHandler(Element element, Appender appender) { /** * Used internally to parse a filter element. * @param filterElement The Filter Element. - * @return The Filter. */ public void addFilter(final AtomicReference ref, final Element filterElement) { final Filter value = parseFilters(filterElement); diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java deleted file mode 100644 index e540ba53c66..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Compare.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - -public class Compare { - - public static boolean compare(final String file1, final String file2) throws IOException { - try (final BufferedReader in1 = new BufferedReader(new FileReader(file1)); - final BufferedReader in2 = new BufferedReader(new FileReader(file2))) { - - String s1; - int lineCounter = 0; - while ((s1 = in1.readLine()) != null) { - lineCounter++; - final String s2 = in2.readLine(); - if (!s1.equals(s2)) { - System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter); - System.out.println("One reads: [" + s1 + "]."); - System.out.println("Other reads:[" + s2 + "]."); - return false; - } - } - - // the second file is longer - if (in2.read() != -1) { - System.out.println("File [" + file2 + "] longer than file [" + file1 + "]."); - return false; - } - - return true; - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java deleted file mode 100644 index ad1c83e4794..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ControlFilter.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.util.Arrays; - -public class ControlFilter implements Filter { - - String[] allowedPatterns; - - public ControlFilter(final String[] allowedPatterns) { - this.allowedPatterns = allowedPatterns; - } - - @Override - public String filter(final String in) throws UnexpectedFormatException { - for (String allowedPattern : allowedPatterns) { - if (in.matches(allowedPattern)) { - return in; - } - } - - throw new UnexpectedFormatException("[" + in + "] allowedPatterns = " + Arrays.toString(allowedPatterns)); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java deleted file mode 100644 index 8c6fab310c2..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Filter.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.jspecify.annotations.Nullable; - -public interface Filter { - - String ISO8601_PAT = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}"; - - @Nullable - String filter(String in) throws UnexpectedFormatException; -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java deleted file mode 100644 index 0ee54d4b05e..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/ISO8601Filter.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -public class ISO8601Filter implements Filter { - - @Override - public String filter(final String in) { - return in.replaceFirst(ISO8601_PAT, ""); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java deleted file mode 100644 index 6a77d457e7d..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/JunitTestRunnerFilter.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.util.regex.Pattern; -import org.jspecify.annotations.Nullable; - -public class JunitTestRunnerFilter implements Filter { - - /** - * Filter out stack trace lines coming from the various JUnit TestRunners. - */ - @Override - public @Nullable String filter(final String in) { - - if (in.contains("at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner")) { - return null; - } else if (in.contains("at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner")) { - return null; - } else if (in.contains("at com.intellij")) { - return null; - } else if (in.contains("at junit.") && in.contains("ui.TestRunner")) { - return null; - } else if (in.contains("org.apache.maven")) { - return null; - } else if (in.contains("junit.internal")) { - return null; - } else if (in.contains("JUnit4TestAdapter")) { - return null; - } else if (Pattern.compile("\\sat ").matcher(in).find()) { - return "\t" + in.trim(); - } else { - return in; - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java deleted file mode 100644 index d3bba3f9947..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/LineNumberFilter.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class LineNumberFilter implements Filter { - - private static final Pattern LINE_NUMBER_PATTERN = Pattern.compile(":\\d+\\)"); - - @Override - public String filter(final String in) { - Matcher matcher = LINE_NUMBER_PATTERN.matcher(in); - if (matcher.find()) { - return matcher.replaceFirst(":XXX"); - } - return in.replaceFirst(", Compiled Code", ":XXX"); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java deleted file mode 100644 index c54755bcb98..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/SunReflectFilter.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import org.jspecify.annotations.Nullable; - -/** - * The sun.reflect.* and java.lang.reflect.* lines are not present in all JDKs. - */ -public class SunReflectFilter implements Filter { - - @Override - public @Nullable String filter(final String in) { - if (in.contains("at sun.reflect") || in.contains("at java.lang.reflect")) { - return null; - } - if (in.contains("Compiled Code")) { - if (in.contains("junit.framework.TestSuite")) { - return in.replaceFirst("Compiled Code", "TestSuite.java:XXX"); - } - } - return in.replaceFirst("\\(Method.java:.*\\)", "(Native Method)"); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java deleted file mode 100644 index 3c6e4983440..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/Transformer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -import java.io.BufferedReader; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintStream; - -public class Transformer { - - public static void transform(final String in, final String out, final Filter[] filters) - throws IOException, UnexpectedFormatException { - - String line; - final BufferedReader input = new BufferedReader(new FileReader(in)); - final PrintStream output = new PrintStream(new FileOutputStream(out, false)); - - // Initialization of input and output omitted - while ((line = input.readLine()) != null) { - // apply all filters - for (final Filter filter : filters) { - if (line != null) { - line = filter.filter(line); - } - } - if (line != null) { - output.println(line); - } - } - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/UnexpectedFormatException.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/UnexpectedFormatException.java deleted file mode 100644 index e770fb1d32a..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/UnexpectedFormatException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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. - */ -package org.apache.log4j.util; - -public class UnexpectedFormatException extends Exception { - - private static final long serialVersionUID = 1787725660780924147L; - - public UnexpectedFormatException(final String msg) { - super(msg); - } -} diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java b/log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java deleted file mode 100644 index ed3ee99b4e0..00000000000 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/util/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ -@NullMarked -package org.apache.log4j.util; - -import org.jspecify.annotations.NullMarked; diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/xml/DOMTestCase.java b/log4j-1.2-api/src/test/java/org/apache/log4j/xml/DOMTestCase.java index 256be945600..fb63057df19 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/xml/DOMTestCase.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/xml/DOMTestCase.java @@ -16,221 +16,42 @@ */ package org.apache.log4j.xml; +import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.log4j.Appender; -import org.apache.log4j.FileAppender; import org.apache.log4j.Level; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.VectorAppender; import org.apache.log4j.bridge.AppenderWrapper; -import org.apache.log4j.spi.ErrorHandler; -import org.apache.log4j.spi.LoggerFactory; -import org.apache.log4j.spi.LoggingEvent; -import org.apache.log4j.spi.OptionHandler; -import org.apache.log4j.spi.ThrowableRenderer; -import org.apache.log4j.spi.ThrowableRendererSupport; -import org.apache.log4j.util.Compare; -import org.apache.log4j.util.ControlFilter; -import org.apache.log4j.util.Filter; -import org.apache.log4j.util.ISO8601Filter; -import org.apache.log4j.util.JunitTestRunnerFilter; -import org.apache.log4j.util.LineNumberFilter; -import org.apache.log4j.util.SunReflectFilter; -import org.apache.log4j.util.Transformer; +import org.apache.logging.log4j.test.ListStatusListener; import org.apache.logging.log4j.test.junit.SetTestProperty; +import org.apache.logging.log4j.test.junit.UsingStatusListener; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @SetTestProperty(key = "log4j1.compatibility", value = "true") class DOMTestCase { - /** - * CustomErrorHandler for testCategoryFactory2. - */ - public static class CustomErrorHandler implements ErrorHandler { - public CustomErrorHandler() {} - - public void activateOptions() {} - - @Override - public void error(final String message) {} - - @Override - public void error(final String message, final Exception e, final int errorCode) {} - - @Override - public void error(final String message, final Exception e, final int errorCode, final LoggingEvent event) {} - - @Override - public void setAppender(final Appender appender) {} - - @Override - public void setBackupAppender(final Appender appender) {} - - @Override - public void setLogger(final Logger logger) {} - } - - /** - * CustomLogger implementation for testCategoryFactory1 and 2. - */ - private static class CustomLogger extends Logger { - /** - * Creates new instance. - * - * @param name logger name. - */ - public CustomLogger(final String name) { - super(name); - } - } - - /** - * Creates new instances of CustomLogger. - */ - public static class CustomLoggerFactory implements LoggerFactory { - - /** - * Additivity, expected to be set false in configuration file. - */ - private boolean additivity; - - /** - * Create new instance of factory. - */ - public CustomLoggerFactory() { - additivity = true; - } - - /** - * Create new logger. - * - * @param name logger name. - * @return new logger. - */ - @Override - public Logger makeNewLoggerInstance(final String name) { - final Logger logger = new CustomLogger(name); - assertFalse(additivity); - return logger; - } - - /** - * Set additivity. - * - * @param newVal new value of additivity. - */ - public void setAdditivity(final boolean newVal) { - additivity = newVal; - } - } - - /** - * Mock ThrowableRenderer for testThrowableRenderer. See bug 45721. - */ - public static class MockThrowableRenderer implements ThrowableRenderer, OptionHandler { - private boolean activated = false; - private boolean showVersion = true; - - public MockThrowableRenderer() {} - - @Override - public void activateOptions() { - activated = true; - } - - @Override - public String[] doRender(final Throwable t) { - return new String[0]; - } - - public boolean getShowVersion() { - return showVersion; - } - - public boolean isActivated() { - return activated; - } - - public void setShowVersion(final boolean v) { - showVersion = v; - } - } - - static String TEMP_A1 = "target/output/temp.A1"; - static String TEMP_A2 = "target/output/temp.A2"; - static String FILTERED_A1 = "target/output/filtered.A1"; - static String FILTERED_A2 = "target/output/filtered.A2"; - static String EXCEPTION1 = "java.lang.Exception: Just testing"; - static String EXCEPTION2 = "\\s*at .*\\(.*\\)"; - static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)"; - static String EXCEPTION4 = "\\s*at .*\\(.*Compiled Code\\)"; - static String EXCEPTION5 = "\\s*at .*\\(.*libgcj.*\\)"; - static String TEST1_1A_PAT = "(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) \\w*\\.\\w* - Message \\d"; - static String TEST1_1B_PAT = "(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) root - Message \\d"; - static String TEST1_2_PAT = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3} " - + "\\[main]\\ (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d"; - - private static final boolean Log4j1ActualAppender = false; - - private static final boolean AssumeThrowableRendererSupport = false; - Logger root; Logger logger; - void common() { - final String oldThreadName = Thread.currentThread().getName(); - Thread.currentThread().setName("main"); - - int i = -1; - - logger.trace("Message " + ++i); - root.trace("Message " + i); - - logger.debug("Message " + ++i); - root.debug("Message " + i); - - logger.info("Message " + ++i); - root.info("Message " + i); - - logger.warn("Message " + ++i); - root.warn("Message " + i); - - logger.error("Message " + ++i); - root.error("Message " + i); - - logger.log(Level.FATAL, "Message " + ++i); - root.log(Level.FATAL, "Message " + i); - - final Exception e = new Exception("Just testing"); - logger.debug("Message " + ++i, e); - root.debug("Message " + i, e); - - logger.error("Message " + ++i, e); - root.error("Message " + i, e); - - Thread.currentThread().setName(oldThreadName); - } - @BeforeEach public void setUp() { root = Logger.getRootLogger(); @@ -242,105 +63,6 @@ public void tearDown() { root.getLoggerRepository().resetConfiguration(); } - @Test - @Disabled - public void test1() throws Exception { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/DOMTestCase1.xml")); - common(); - - final ControlFilter cf1 = new ControlFilter( - new String[] {TEST1_1A_PAT, TEST1_1B_PAT, EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5}); - - final ControlFilter cf2 = new ControlFilter( - new String[] {TEST1_2_PAT, EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5}); - - Transformer.transform(TEMP_A1, FILTERED_A1, new Filter[] { - cf1, new LineNumberFilter(), new SunReflectFilter(), new JunitTestRunnerFilter() - }); - - Transformer.transform(TEMP_A2, FILTERED_A2, new Filter[] { - cf2, new LineNumberFilter(), new ISO8601Filter(), new SunReflectFilter(), new JunitTestRunnerFilter() - }); - - assertTrue(Compare.compare(FILTERED_A1, "witness/dom.A1.1")); - assertTrue(Compare.compare(FILTERED_A2, "witness/dom.A2.1")); - } - - /** - * Tests processing of external entities in XML file. - */ - @Test - @Disabled - public void test4() throws Exception { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/DOMTest4.xml")); - common(); - - final ControlFilter cf1 = new ControlFilter( - new String[] {TEST1_1A_PAT, TEST1_1B_PAT, EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5}); - - final ControlFilter cf2 = new ControlFilter( - new String[] {TEST1_2_PAT, EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5}); - - Transformer.transform(TEMP_A1 + ".4", FILTERED_A1 + ".4", new Filter[] { - cf1, new LineNumberFilter(), new SunReflectFilter(), new JunitTestRunnerFilter() - }); - - Transformer.transform(TEMP_A2 + ".4", FILTERED_A2 + ".4", new Filter[] { - cf2, new LineNumberFilter(), new ISO8601Filter(), new SunReflectFilter(), new JunitTestRunnerFilter() - }); - - assertTrue(Compare.compare(FILTERED_A1 + ".4", "witness/dom.A1.4")); - assertTrue(Compare.compare(FILTERED_A2 + ".4", "witness/dom.A2.4")); - } - - /** - * Tests that loggers mentioned in logger elements use the specified categoryFactory. See bug 33708. - */ - @Test - @Disabled - public void testCategoryFactory1() { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/categoryfactory1.xml")); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory1.2"); - assertFalse(logger2.toString(), logger2 instanceof CustomLogger); - // - // logger explicitly mentioned in configuration, - // should be a CustomLogger - final Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory1.1"); - assertTrue(logger1.toString(), logger1 instanceof CustomLogger); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2Bis = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory1.2"); - assertFalse(logger2Bis.toString(), logger2Bis instanceof CustomLogger); - } - - /** - * Tests that loggers mentioned in logger-ref elements use the specified categoryFactory. See bug 33708. - */ - @Test - @Disabled - public void testCategoryFactory2() { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/categoryfactory2.xml")); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory2.2"); - assertFalse(logger2.toString(), logger2 instanceof CustomLogger); - // - // logger explicitly mentioned in configuration, - // should be a CustomLogger - final Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory2.1"); - assertTrue(logger1.toString(), logger1 instanceof CustomLogger); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2Bis = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory2.2"); - assertFalse(logger2Bis.toString(), logger2Bis instanceof CustomLogger); - } - /** * Test checks that configureAndWatch does initial configuration, see bug 33502. * @@ -348,7 +70,7 @@ public void testCategoryFactory2() { */ @Test void testConfigureAndWatch() throws Exception { - final URL url = DOMTestCase.class.getResource("/DOMTestCase/DOMTestCase1.xml"); + final URL url = requireNonNull(DOMTestCase.class.getResource("/DOMTestCase/DOMTestCase1.xml")); DOMConfigurator.configureAndWatch(Paths.get(url.toURI()).toString()); assertNotNull(Logger.getRootLogger().getAppender("A1")); } @@ -360,13 +82,13 @@ void testConfigureAndWatch() throws Exception { */ @Test void testJarURL() throws Exception { - final URL url = DOMTestCase.class.getResource("/DOMTestCase/defaultInit.xml"); + final URL url = requireNonNull(DOMTestCase.class.getResource("/DOMTestCase/defaultInit.xml")); final File input = Paths.get(url.toURI()).toFile(); System.out.println(input.getAbsolutePath()); final File configJar = new File("target/output/xml.jar"); final File dir = new File("target/output"); - dir.mkdirs(); - try (final InputStream inputStream = new FileInputStream(input); + Files.createDirectories(dir.toPath()); + try (final InputStream inputStream = Files.newInputStream(input.toPath()); final FileOutputStream out = new FileOutputStream(configJar); final ZipOutputStream zos = new ZipOutputStream(out)) { zos.putNextEntry(new ZipEntry("log4j.xml")); @@ -377,80 +99,39 @@ void testJarURL() throws Exception { } zos.closeEntry(); } - final URL urlInJar = new URL("jar:" + configJar.toURL() + "!/log4j.xml"); + final URL urlInJar = new URL("jar:" + configJar.toURI() + "!/log4j.xml"); DOMConfigurator.configure(urlInJar); assertTrue(configJar.delete()); assertFalse(configJar.exists()); } - /** - * Tests that loggers mentioned in logger elements use the specified loggerFactory. See bug 33708. - */ - @Test - @Disabled("TODO") - public void testLoggerFactory1() { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/loggerfactory1.xml")); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testLoggerFactory1.2"); - assertNotNull(logger2); - assertFalse(logger2.toString(), logger2 instanceof CustomLogger); - // - // logger explicitly mentioned in configuration, - // should be a CustomLogger - final Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testLoggerFactory1.1"); - assertNotNull(logger1); - assertTrue(logger1.toString(), logger1 instanceof CustomLogger); - // - // logger not explicitly mentioned in configuration, - // should use default factory - final Logger logger2Bis = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testLoggerFactory1.2"); - assertNotNull(logger2Bis); - assertFalse(logger2Bis.toString(), logger2Bis instanceof CustomLogger); - } - /** * This test checks that the subst method of an extending class is checked when evaluating parameters. See bug 43325. * */ @Test public void testOverrideSubst() { - final DOMConfigurator configurator = new DOMConfigurator() { - private String subst(final String value) { - return "target/output/temp.A1".equals(value) ? "target/output/subst-test.A1" : value; - } - }; + final DOMConfigurator configurator = new DOMConfigurator(); configurator.doConfigure( DOMTestCase.class.getResource("/DOMTestCase/DOMTestCase1.xml"), LogManager.getLoggerRepository()); final String name = "A1"; final Appender appender = Logger.getRootLogger().getAppender(name); - assertNotNull(name, appender); - if (Log4j1ActualAppender) { - final FileAppender a1 = (FileAppender) appender; - assertNotNull(name, a1); - final String file = a1.getFile(); - assertEquals("target/output/subst-test.A1", file); - } else { - final AppenderWrapper wrapper = (AppenderWrapper) appender; - assertNotNull(name, wrapper); - final org.apache.logging.log4j.core.appender.FileAppender a1 = - (org.apache.logging.log4j.core.appender.FileAppender) wrapper.getAppender(); - assertNotNull(wrapper.toString(), a1); - final String file = a1.getFileName(); - assertNotNull(a1.toString(), file); - // TODO Support this or not? - // assertEquals("target/output/subst-test.A1", file); - } + assertNotNull(appender, name); + final AppenderWrapper wrapper = (AppenderWrapper) appender; + assertNotNull(wrapper, name); + final org.apache.logging.log4j.core.appender.FileAppender a1 = + (org.apache.logging.log4j.core.appender.FileAppender) wrapper.getAppender(); + assertNotNull(a1, wrapper.toString()); + final String file = a1.getFileName(); + assertNotNull(file, a1.toString()); } /** * Tests that reset="true" on log4j:configuration element resets repository before configuration. * - * @throws Exception thrown on error. */ @Test - public void testReset() throws Exception { + public void testReset() { final VectorAppender appender = new VectorAppender(); appender.setName("V1"); Logger.getRootLogger().addAppender(appender); @@ -462,21 +143,17 @@ public void testReset() throws Exception { * Test of log4j.throwableRenderer support. See bug 45721. */ @Test - public void testThrowableRenderer1() { - DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/throwableRenderer1.xml")); - final ThrowableRendererSupport repo = (ThrowableRendererSupport) LogManager.getLoggerRepository(); - final MockThrowableRenderer renderer = (MockThrowableRenderer) repo.getThrowableRenderer(); - LogManager.resetConfiguration(); - if (AssumeThrowableRendererSupport) { - assertNotNull(renderer); - assertTrue(renderer.isActivated()); - assertFalse(renderer.getShowVersion()); - } + @UsingStatusListener + void testThrowableRenderer(ListStatusListener listener) { + DOMConfigurator.configure(DOMTestCase.class.getResource("/DOMTestCase/testThrowableRenderer.xml")); + assertThat(listener.findStatusData(org.apache.logging.log4j.Level.WARN)) + .anySatisfy(status -> assertThat(status.getMessage().getFormattedMessage()) + .contains("Log4j 1 throwable renderers are not supported")); } @Test @SetTestProperty(key = "log4j1.compatibility", value = "false") - void when_compatibility_disabled_configurator_is_no_op() throws IOException { + void when_compatibility_disabled_configurator_is_no_op() { final Logger rootLogger = Logger.getRootLogger(); final Logger logger = Logger.getLogger("org.apache.log4j.xml"); assertThat(logger.getLevel()).isNull(); diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4.xml deleted file mode 100644 index 5cfa8d3a328..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - -]> - - &a1; - &a2; - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A1.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A1.xml deleted file mode 100644 index f24dda87654..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A1.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A2.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A2.xml deleted file mode 100644 index 00cdd55b766..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/DOMTest4_A2.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory1.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory1.xml deleted file mode 100644 index 24812860d47..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory1.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory2.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory2.xml deleted file mode 100644 index eccea215c58..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/categoryfactory2.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/loggerfactory1.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/loggerfactory1.xml deleted file mode 100644 index 031f714d232..00000000000 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/loggerfactory1.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-1.2-api/src/test/resources/DOMTestCase/throwableRenderer1.xml b/log4j-1.2-api/src/test/resources/DOMTestCase/testThrowableRenderer.xml similarity index 86% rename from log4j-1.2-api/src/test/resources/DOMTestCase/throwableRenderer1.xml rename to log4j-1.2-api/src/test/resources/DOMTestCase/testThrowableRenderer.xml index a8af90d6558..50813462379 100644 --- a/log4j-1.2-api/src/test/resources/DOMTestCase/throwableRenderer1.xml +++ b/log4j-1.2-api/src/test/resources/DOMTestCase/testThrowableRenderer.xml @@ -17,8 +17,8 @@ --> - - - - + + + +