Skip to content

Commit

Permalink
Introduce Nashorn integration test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ppkarwasz committed Dec 27, 2024
1 parent d9a98d6 commit 76041b2
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 0 deletions.
186 changes: 186 additions & 0 deletions log4j-nashorn-test/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
30 changes: 30 additions & 0 deletions log4j-nashorn-test/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
38 changes: 38 additions & 0 deletions log4j-nashorn-test/src/test/resources/ScriptFilterTest.xml
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 76041b2

Please sign in to comment.