From 76041b2628baa7bb8bdb56c203e8bfeeb45fc285 Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Fri, 27 Dec 2024 08:13:01 +0100 Subject: [PATCH] Introduce Nashorn integration test Introduces an integration test that uses: - Log4j Core with a `ScriptFilter`, - Nashorn as scripting engine. Nashorn uses `java.util.logging` to log debug information. - JUL-to-Log4j API bridge to forward Nashorn logging to Log4j Core. The purpose of this test is to check for unintentional initialization loops between Log4j Core and Nashorn, as those reported in apache/logging-log4j2#3119. --- log4j-nashorn-test/pom.xml | 186 ++++++++++++++++++ .../src/test/java/module-info.java | 30 +++ .../samples/nashorn/ScriptFilterTest.java | 53 +++++ .../src/test/resources/ScriptFilterTest.xml | 38 ++++ pom.xml | 1 + 5 files changed, 308 insertions(+) create mode 100644 log4j-nashorn-test/pom.xml create mode 100644 log4j-nashorn-test/src/test/java/module-info.java create mode 100644 log4j-nashorn-test/src/test/java/org/apache/logging/log4j/samples/nashorn/ScriptFilterTest.java create mode 100644 log4j-nashorn-test/src/test/resources/ScriptFilterTest.xml diff --git a/log4j-nashorn-test/pom.xml b/log4j-nashorn-test/pom.xml new file mode 100644 index 0000000..abb03c2 --- /dev/null +++ b/log4j-nashorn-test/pom.xml @@ -0,0 +1,186 @@ + + + + 4.0.0 + + org.apache.logging.log4j.samples + log4j-samples + ${revision} + + + log4j-nashorn-test + Apache Log4j Samples: Nashorn script + + + true + + 15.6 + + + + + + org.apache.logging.log4j + log4j-api + test + + + + org.apache.logging.log4j + log4j-core + test + + + + org.apache.logging.log4j + log4j-api-test + test + + + org.hamcrest + hamcrest + + + junit + junit + + + org.junit-pioneer + junit-pioneer + + + org.junit.platform + junit-platform-commons + + + org.apache.maven + maven-core + + + org.apache.maven + maven-model + + + org.codehaus.plexus + plexus-utils + + + org.mockito + mockito-core + + + + + + + org.apache.logging.log4j + log4j-core-test + test + + + org.awaitility + awaitility + + + org.hamcrest + hamcrest + + + com.google.code.java-allocation-instrumenter + java-allocation-instrumenter + + + junit + junit + + + org.junit.platform + junit-platform-commons + + + org.springframework + spring-test + + + + + + org.apache.logging.log4j + log4j-jul + test + + + + org.assertj + assertj-core + test + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.openjdk.nashorn + nashorn-core + ${nashorn.version} + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-testCompile + + true + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + default-test + + --add-opens org.apache.logging.log4j.core.test/org.apache.logging.log4j.core.test.appender=org.apache.logging.log4j.core + + org.apache.logging.log4j.jul.LogManager + javascript + + true + + + + + + + + + diff --git a/log4j-nashorn-test/src/test/java/module-info.java b/log4j-nashorn-test/src/test/java/module-info.java new file mode 100644 index 0000000..ee669df --- /dev/null +++ b/log4j-nashorn-test/src/test/java/module-info.java @@ -0,0 +1,30 @@ +/* + * 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. + */ +module org.apache.logging.log4j.samples.nashorn { + requires java.base; + // requires java.scripting; + // Log4j modules + requires org.apache.logging.log4j; + requires org.apache.logging.log4j.core; + requires org.apache.logging.log4j.core.test; + // Testing modules + requires org.junit.jupiter.api; + requires org.assertj.core; + // Open to JUnit 5 + opens org.apache.logging.log4j.samples.nashorn to + org.junit.platform.commons; +} diff --git a/log4j-nashorn-test/src/test/java/org/apache/logging/log4j/samples/nashorn/ScriptFilterTest.java b/log4j-nashorn-test/src/test/java/org/apache/logging/log4j/samples/nashorn/ScriptFilterTest.java new file mode 100644 index 0000000..2365797 --- /dev/null +++ b/log4j-nashorn-test/src/test/java/org/apache/logging/log4j/samples/nashorn/ScriptFilterTest.java @@ -0,0 +1,53 @@ +/* + * 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.logging.log4j.samples.nashorn; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.net.URI; +import java.util.Objects; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.test.appender.ListAppender; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.Timeout.ThreadMode; + +class ScriptFilterTest { + + @Test + @Timeout(value = 10, threadMode = ThreadMode.SEPARATE_THREAD) + void workingScriptFilter() throws Exception { + URI configLocation = Objects.requireNonNull(ScriptFilterTest.class.getResource("/ScriptFilterTest.xml")) + .toURI(); + LoggerContext context = + (LoggerContext) LogManager.getContext(ScriptFilterTest.class.getClassLoader(), false, configLocation); + // Get logger and appender + ListAppender appender = context.getConfiguration().getAppender("LIST"); + assertNotNull(appender); + appender.clear(); + Logger logger = context.getLogger(ScriptFilterTest.class); + // Log some events + logger.info("No marker"); + logger.info(MarkerManager.getMarker("AUDIT"), "AUDIT"); + logger.info(MarkerManager.getMarker("DENY"), "DENY"); + assertThat(appender.getMessages()).containsExactly("No marker", "AUDIT"); + } +} diff --git a/log4j-nashorn-test/src/test/resources/ScriptFilterTest.xml b/log4j-nashorn-test/src/test/resources/ScriptFilterTest.xml new file mode 100644 index 0000000..6ac5d7d --- /dev/null +++ b/log4j-nashorn-test/src/test/resources/ScriptFilterTest.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 04d06ba..b3f5241 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ log4j-samples-flume-remote log4j-samples-jlink log4j-samples-loggerProperties + log4j-nashorn-test log4j-samples-parser