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 2024/12/27 07:13:18 UTC

(logging-log4j-samples) 01/01: Introduce Nashorn integration test

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch feature/nashorn-test
in repository https://gitbox.apache.org/repos/asf/logging-log4j-samples.git

commit 76041b2628baa7bb8bdb56c203e8bfeeb45fc285
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Fri Dec 27 08:13:01 2024 +0100

    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 +++++++++++++++++++++
 log4j-nashorn-test/src/test/java/module-info.java  |  30 ++++
 .../log4j/samples/nashorn/ScriptFilterTest.java    |  53 ++++++
 .../src/test/resources/ScriptFilterTest.xml        |  38 +++++
 pom.xml                                            |   1 +
 5 files changed, 308 insertions(+)

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 @@
+<?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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.logging.log4j.samples</groupId>
+    <artifactId>log4j-samples</artifactId>
+    <version>${revision}</version>
+  </parent>
+
+  <artifactId>log4j-nashorn-test</artifactId>
+  <name>Apache Log4j Samples: Nashorn script</name>
+
+  <properties>
+    <spotbugs.skip>true</spotbugs.skip>
+    <!-- Dependency versions -->
+    <nashorn.version>15.6</nashorn.version>
+  </properties>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-api</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-api-test</artifactId>
+      <scope>test</scope>
+      <exclusions>
+        <exclusion>
+          <groupId>org.hamcrest</groupId>
+          <artifactId>hamcrest</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>junit</groupId>
+          <artifactId>junit</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.junit-pioneer</groupId>
+          <artifactId>junit-pioneer</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.junit.platform</groupId>
+          <artifactId>junit-platform-commons</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven</groupId>
+          <artifactId>maven-core</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven</groupId>
+          <artifactId>maven-model</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.codehaus.plexus</groupId>
+          <artifactId>plexus-utils</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.mockito</groupId>
+          <artifactId>mockito-core</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <!-- Used for the `ListAppender` -->
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-core-test</artifactId>
+      <scope>test</scope>
+      <exclusions>
+        <exclusion>
+          <groupId>org.awaitility</groupId>
+          <artifactId>awaitility</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.hamcrest</groupId>
+          <artifactId>hamcrest</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>com.google.code.java-allocation-instrumenter</groupId>
+          <artifactId>java-allocation-instrumenter</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>junit</groupId>
+          <artifactId>junit</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.junit.platform</groupId>
+          <artifactId>junit-platform-commons</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.springframework</groupId>
+          <artifactId>spring-test</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-jul</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.assertj</groupId>
+      <artifactId>assertj-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.openjdk.nashorn</groupId>
+      <artifactId>nashorn-core</artifactId>
+      <version>${nashorn.version}</version>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
+
+  <build>
+    <plugins>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>default-testCompile</id>
+            <configuration>
+              <useModulePath>true</useModulePath>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>default-test</id>
+            <configuration>
+              <argLine>--add-opens org.apache.logging.log4j.core.test/org.apache.logging.log4j.core.test.appender=org.apache.logging.log4j.core</argLine>
+              <systemPropertyVariables>
+                <java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
+                <log4j2.scriptEnableLanguages>javascript</log4j2.scriptEnableLanguages>
+              </systemPropertyVariables>
+              <useModulePath>true</useModulePath>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
+    </plugins>
+  </build>
+
+</project>
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 @@
+<?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 xmlns="https://logging.apache.org/xml/ns"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="
+                   https://logging.apache.org/xml/ns
+                   https://logging.apache.org/xml/ns/log4j-config-2.xsd">
+    <Appenders>
+        <List name="LIST">
+            <PatternLayout pattern="%msg"/>
+        </List>
+    </Appenders>
+    <ScriptFilter>
+        <Script language="javascript"><![CDATA[
+            marker == null || !marker.isInstanceOf("DENY");
+        ]]></Script>
+    </ScriptFilter>
+    <Loggers>
+        <Root level="ALL">
+            <AppenderRef ref="LIST"/>
+        </Root>
+    </Loggers>
+</Configuration>
diff --git a/pom.xml b/pom.xml
index 04d06ba..b3f5241 100644
--- a/pom.xml
+++ b/pom.xml
@@ -42,6 +42,7 @@
     <module>log4j-samples-flume-remote</module>
     <module>log4j-samples-jlink</module>
     <module>log4j-samples-loggerProperties</module>
+    <module>log4j-nashorn-test</module>
     <module>log4j-samples-parser</module>
     <!-- Disabled, since compilation fails
     <module>log4j-server</module>