Make tests run with log4j2
diff --git a/commons-jcs-core/pom.xml b/commons-jcs-core/pom.xml
index 9026c36..e7bc878 100644
--- a/commons-jcs-core/pom.xml
+++ b/commons-jcs-core/pom.xml
@@ -118,11 +118,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${commons.surefire.version}</version>
<configuration>
- <systemPropertyVariables>
- <java.security.manager>true</java.security.manager>
- <java.security.policy>${basedir}/src/test/conf/cache.policy</java.security.policy>
- <java.util.logging.config.file>${basedir}/src/test/test-conf/logging.properties</java.util.logging.config.file>
- </systemPropertyVariables>
<argLine>-Xmx256m</argLine>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
@@ -171,5 +166,58 @@
</plugin>
</plugins>
</build>
-
+
+ <profiles>
+ <profile>
+ <id>testWithJUL</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <systemPropertyVariables>
+ <java.security.manager>true</java.security.manager>
+ <java.security.policy>${basedir}/src/test/conf/cache.policy</java.security.policy>
+ <java.util.logging.config.file>${basedir}/src/test/test-conf/logging.properties</java.util.logging.config.file>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
+ <id>testWithLog4j2</id>
+ <activation>
+ <activeByDefault>false</activeByDefault>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>2.13.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <systemPropertyVariables>
+ <java.security.manager>true</java.security.manager>
+ <java.security.policy>${basedir}/src/test/conf/cache.policy</java.security.policy>
+ <jcs.logSystem>log4j2</jcs.logSystem>
+ <log4j.configurationFile>src/test/test-conf/log4j2-test.xml</log4j.configurationFile>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
</project>
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2Factory.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2Factory.java
index 7c1ab3d..0f56364 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2Factory.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2Factory.java
@@ -1,6 +1,8 @@
package org.apache.commons.jcs.log;
import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.message.MessageFactory;
+import org.apache.logging.log4j.message.MessageFormatMessageFactory;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -24,6 +26,9 @@
*/
public class Log4j2Factory implements LogFactory
{
+ /** Use java.text.MessageFormat for log messages */
+ private MessageFactory messageFactory = new MessageFormatMessageFactory();
+
/**
* Return the name of the Log subsystem managed by this factory
*
@@ -59,7 +64,7 @@
@Override
public Log getLog(final Class<?> clazz)
{
- Logger logger = org.apache.logging.log4j.LogManager.getLogger(clazz);
+ Logger logger = org.apache.logging.log4j.LogManager.getLogger(clazz, messageFactory);
return new Log4j2LogAdapter(logger);
}
@@ -77,7 +82,7 @@
@Override
public Log getLog(final String name)
{
- Logger logger = org.apache.logging.log4j.LogManager.getLogger(name);
+ Logger logger = org.apache.logging.log4j.LogManager.getLogger(name, messageFactory);
return new Log4j2LogAdapter(logger);
}
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2LogAdapter.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2LogAdapter.java
index 66d6644..2f437fe 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2LogAdapter.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/Log4j2LogAdapter.java
@@ -53,24 +53,26 @@
{
switch (paramSuppliers.length)
{
- case 1: logger.log(level, message, paramSuppliers[0]);
+ case 1: logger.log(level, message, paramSuppliers[0].get());
break;
- case 2: logger.log(level, message, paramSuppliers[0],
- paramSuppliers[1]);
+ case 2: logger.log(level, message, paramSuppliers[0].get(),
+ paramSuppliers[1].get());
break;
- case 3: logger.log(level, message, paramSuppliers[0],
- paramSuppliers[1], paramSuppliers[2]);
+ case 3: logger.log(level, message, paramSuppliers[0].get(),
+ paramSuppliers[1].get(), paramSuppliers[2].get());
break;
- case 4: logger.log(level, message, paramSuppliers[0],
- paramSuppliers[1], paramSuppliers[2], paramSuppliers[3]);
+ case 4: logger.log(level, message, paramSuppliers[0].get(),
+ paramSuppliers[1].get(), paramSuppliers[2].get(),
+ paramSuppliers[3].get());
break;
- case 5: logger.log(level, message, paramSuppliers[0],
- paramSuppliers[1], paramSuppliers[2], paramSuppliers[3],
- paramSuppliers[4]);
+ case 5: logger.log(level, message, paramSuppliers[0].get(),
+ paramSuppliers[1].get(), paramSuppliers[2].get(),
+ paramSuppliers[3].get(), paramSuppliers[4].get());
break;
- default: logger.log(level, message, paramSuppliers[0],
- paramSuppliers[1], paramSuppliers[2], paramSuppliers[3],
- paramSuppliers[4], paramSuppliers[5]);
+ default: logger.log(level, message, paramSuppliers[0].get(),
+ paramSuppliers[1].get(), paramSuppliers[2].get(),
+ paramSuppliers[3].get(), paramSuppliers[4].get(),
+ paramSuppliers[5].get());
break;
}
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/LogManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/LogManager.java
index 0ae4b98..4256d12 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/LogManager.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/log/LogManager.java
@@ -22,7 +22,7 @@
/**
* This is a borrowed and stripped-down version of the log4j2 LogManager class.
*
- * The anchor point for the Log4j logging system. The most common usage of this
+ * The anchor point for the JCS logging system. The most common usage of this
* class is to obtain a named {@link Log}.
*/
public class LogManager
@@ -30,7 +30,7 @@
/**
* The name of log subsystem
*/
- private static String logSystem = "jul";
+ private static String logSystem = null;
/**
* The SPI LogFactory
@@ -48,7 +48,10 @@
private static LogFactory createLogFactory()
{
ServiceLoader<LogFactory> factories = ServiceLoader.load(LogFactory.class);
- LogManager.logSystem = System.getProperty("jcs.logSystem", LogManager.logSystem);
+ if (LogManager.logSystem == null)
+ {
+ LogManager.logSystem = System.getProperty("jcs.logSystem", "jul");
+ }
for (LogFactory factory : factories)
{
@@ -100,12 +103,10 @@
* name.
*
* @param clazz
- * The Class whose name should be used as the Log name. If null
- * it will default to the calling class.
+ * The Class whose name should be used as the Log name.
* @return The Log.
* @throws UnsupportedOperationException
- * if {@code clazz} is {@code null} and the calling class cannot
- * be determined.
+ * if {@code clazz} is {@code null}
*/
public static Log getLog(final Class<?> clazz)
{
@@ -116,12 +117,10 @@
* Returns a Log with the specified name.
*
* @param name
- * The logger name. If null the name of the calling class will be
- * used.
+ * The logger name.
* @return The Log.
* @throws UnsupportedOperationException
- * if {@code name} is {@code null} and the calling class cannot
- * be determined.
+ * if {@code name} is {@code null}
*/
public static Log getLog(final String name)
{
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListDumpUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListDumpUnitTest.java
new file mode 100644
index 0000000..644e4c3
--- /dev/null
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListDumpUnitTest.java
@@ -0,0 +1,58 @@
+package org.apache.commons.jcs.utils.struct;
+
+import java.io.StringWriter;
+
+import org.apache.commons.jcs.TestLogConfigurationUtil;
+
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+
+/** Unit tests for the double linked list. */
+public class DoubleLinkedListDumpUnitTest
+ extends TestCase
+{
+ /** verify that the entries are dumped. */
+ public void testDumpEntries_DebugTrue()
+ {
+ // SETUP
+ StringWriter stringWriter = new StringWriter();
+ TestLogConfigurationUtil.configureLogger( stringWriter, DoubleLinkedList.class.getName() );
+
+ DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
+
+ String payload1 = "payload1";
+ DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
+
+ String payload2 = "payload2";
+ DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
+
+ list.addLast( node1 );
+ list.addLast( node2 );
+ list.debugDumpEntries();
+
+ // WO WORK
+ String result = stringWriter.toString();
+
+ // VERIFY
+ assertTrue( "Missing node in log dump", result.indexOf( payload1 ) != -1 );
+ assertTrue( "Missing node in log dump", result.indexOf( payload2 ) != -1 );
+ }
+}
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListUnitTest.java
index a7ca1d8..a215598 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/struct/DoubleLinkedListUnitTest.java
@@ -20,9 +20,6 @@
*/
import junit.framework.TestCase;
-import org.apache.commons.jcs.TestLogConfigurationUtil;
-
-import java.io.StringWriter;
/** Unit tests for the double linked list. */
public class DoubleLinkedListUnitTest
@@ -159,31 +156,4 @@
assertEquals( "Wrong last", node1, list.getLast() );
assertEquals( "Wrong first", node3, list.getFirst() );
}
-
- /** verify that the entries are dumped. */
- public void testDumpEntries_DebugTrue()
- {
- // SETUP
- StringWriter stringWriter = new StringWriter();
- TestLogConfigurationUtil.configureLogger( stringWriter, DoubleLinkedList.class.getName() );
-
- DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
-
- String payload1 = "payload1";
- DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
-
- String payload2 = "payload2";
- DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
-
- list.addLast( node1 );
- list.addLast( node2 );
- list.debugDumpEntries();
-
- // WO WORK
- String result = stringWriter.toString();
-
- // VERIFY
- assertTrue( "Missing node in log dump", result.indexOf( payload1 ) != -1 );
- assertTrue( "Missing node in log dump", result.indexOf( payload2 ) != -1 );
- }
}
diff --git a/commons-jcs-core/src/test/test-conf/log4j2-test.xml b/commons-jcs-core/src/test/test-conf/log4j2-test.xml
new file mode 100644
index 0000000..2fc4fac
--- /dev/null
+++ b/commons-jcs-core/src/test/test-conf/log4j2-test.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<Configuration status="WARN">
+ <Appenders>
+ <File name="jcs" fileName="target/jcs-test.log">
+ <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
+ </File>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.commons.jcs" additivity="false" level="INFO">
+ <AppenderRef ref="jcs"/>
+ </Logger>
+ <Root level="ERROR"><!-- log4j 1.2 has DEBUG -->
+ <AppenderRef ref="jcs" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file