Skip to content

Commit a863232

Browse files
committed
Port log4j-jcl changes from 2.x
1 parent 4bd7e19 commit a863232

File tree

5 files changed

+87
-55
lines changed

5 files changed

+87
-55
lines changed

log4j-jcl/pom.xml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@
2323
<version>${revision}</version>
2424
<relativePath>../log4j-parent</relativePath>
2525
</parent>
26-
2726
<artifactId>log4j-jcl</artifactId>
2827
<packaging>jar</packaging>
2928
<name>Apache Log4j Commons Logging Bridge</name>
3029
<description>The Apache Log4j Commons Logging Adapter</description>
3130
<properties>
32-
<log4jParentDir>${basedir}/..</log4jParentDir>
3331
<bnd-extra-module-options>
34-
<!-- Use module name returned by `jar -d` -->
35-
commons.logging;substitute="commons-logging"
32+
<!-- Filebase module names: MUST be static -->
33+
commons.logging;substitute="commons-logging";transitive=false;static=true
3634
</bnd-extra-module-options>
3735
</properties>
3836
<dependencies>
@@ -60,15 +58,14 @@
6058
<scope>test</scope>
6159
</dependency>
6260
<dependency>
63-
<groupId>org.junit.jupiter</groupId>
64-
<artifactId>junit-jupiter-engine</artifactId>
61+
<groupId>org.assertj</groupId>
62+
<artifactId>assertj-core</artifactId>
6563
<scope>test</scope>
6664
</dependency>
6765
<dependency>
68-
<groupId>org.junit.vintage</groupId>
69-
<artifactId>junit-vintage-engine</artifactId>
66+
<groupId>org.junit.jupiter</groupId>
67+
<artifactId>junit-jupiter-api</artifactId>
7068
<scope>test</scope>
7169
</dependency>
7270
</dependencies>
73-
7471
</project>

log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,47 @@
1616
*/
1717
package org.apache.logging.log4j.jcl;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import java.util.List;
2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
24+
import org.apache.logging.log4j.core.LoggerContext;
2425
import org.apache.logging.log4j.core.test.appender.ListAppender;
25-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
26-
import org.junit.ClassRule;
27-
import org.junit.Test;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.apache.logging.log4j.test.junit.SetTestProperty;
28+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
29+
import org.junit.jupiter.api.Test;
2830

31+
@UsingStatusListener
32+
@SetTestProperty(key = "log4j2.configurationFile", value = "org/apache/logging/log4j/jcl/CallerInformationTest.xml")
2933
public class CallerInformationTest {
3034

31-
// config from log4j-core test-jar
32-
private static final String CONFIG = "log4j2-calling-class.xml";
33-
34-
@ClassRule
35-
public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
36-
3735
@Test
38-
public void testClassLogger() throws Exception {
39-
final ListAppender app = ctx.getListAppender("Class").clear();
36+
@LoggerContextSource("CallerInformationTest.xml")
37+
public void testClassLogger(final LoggerContext ctx) {
38+
final ListAppender app = ctx.getConfiguration().getAppender("Class");
39+
app.clear();
4040
final Log logger = LogFactory.getLog("ClassLogger");
4141
logger.info("Ignored message contents.");
4242
logger.warn("Verifying the caller class is still correct.");
4343
logger.error("Hopefully nobody breaks me!");
4444
final List<String> messages = app.getMessages();
45-
assertEquals("Incorrect number of messages.", 3, messages.size());
46-
for (final String message : messages) {
47-
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
48-
}
45+
assertThat(messages).hasSize(3).allMatch(c -> getClass().getName().equals(c));
4946
}
5047

5148
@Test
52-
public void testMethodLogger() throws Exception {
53-
final ListAppender app = ctx.getListAppender("Method").clear();
49+
@LoggerContextSource("CallerInformationTest.xml")
50+
public void testMethodLogger(final LoggerContext ctx) {
51+
final ListAppender app = ctx.getConfiguration().getAppender("Method");
52+
app.clear();
5453
final Log logger = LogFactory.getLog("MethodLogger");
5554
logger.info("More messages.");
5655
logger.warn("CATASTROPHE INCOMING!");
5756
logger.error("ZOMBIES!!!");
5857
logger.warn("brains~~~");
5958
logger.info("Itchy. Tasty.");
6059
final List<String> messages = app.getMessages();
61-
assertEquals("Incorrect number of messages.", 5, messages.size());
62-
for (final String message : messages) {
63-
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
64-
}
60+
assertThat(messages).hasSize(5).allMatch("testMethodLogger"::equals);
6561
}
6662
}

log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,45 @@
1616
*/
1717
package org.apache.logging.log4j.jcl;
1818

19-
import static org.hamcrest.Matchers.equalTo;
20-
import static org.hamcrest.Matchers.hasSize;
21-
import static org.junit.Assert.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
2220

2321
import java.util.List;
2422
import org.apache.commons.logging.Log;
2523
import org.apache.commons.logging.LogFactory;
24+
import org.apache.logging.log4j.core.LoggerContext;
2625
import org.apache.logging.log4j.core.test.appender.ListAppender;
27-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
26+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
27+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2828
import org.apache.logging.log4j.util.Strings;
29-
import org.junit.ClassRule;
30-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3130

32-
/**
33-
*
34-
*/
35-
public class LoggerTest {
36-
37-
private static final String CONFIG = "log4j-test1.xml";
31+
@UsingStatusListener
32+
class LoggerTest {
3833

39-
@ClassRule
40-
public static final LoggerContextRule context = new LoggerContextRule(CONFIG);
34+
@Test
35+
void testFactory() {
36+
final LogFactory factory = LogFactory.getFactory();
37+
assertThat(factory).isInstanceOf(LogFactoryImpl.class);
38+
}
4139

4240
@Test
43-
public void testLog() {
41+
@LoggerContextSource("LoggerTest.xml")
42+
void testLog(final LoggerContext loggerContext) {
4443
final Log logger = LogFactory.getLog("LoggerTest");
4544
logger.debug("Test message");
46-
verify("List", "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
45+
verify(loggerContext, "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
4746
logger.debug("Exception: ", new NullPointerException("Test"));
48-
verify("List", "o.a.l.l.j.LoggerTest Exception: MDC{}" + Strings.LINE_SEPARATOR);
47+
verify(loggerContext, "o.a.l.l.j.LoggerTest Exception: MDC{}" + Strings.LINE_SEPARATOR);
4948
logger.info("Info Message");
50-
verify("List", "o.a.l.l.j.LoggerTest Info Message MDC{}" + Strings.LINE_SEPARATOR);
49+
verify(loggerContext, "o.a.l.l.j.LoggerTest Info Message MDC{}" + Strings.LINE_SEPARATOR);
5150
logger.info("Info Message {}");
52-
verify("List", "o.a.l.l.j.LoggerTest Info Message {} MDC{}" + Strings.LINE_SEPARATOR);
51+
verify(loggerContext, "o.a.l.l.j.LoggerTest Info Message {} MDC{}" + Strings.LINE_SEPARATOR);
5352
}
5453

55-
private void verify(final String name, final String expected) {
56-
final ListAppender listApp = context.getListAppender(name);
54+
private static void verify(final LoggerContext loggerContext, final String expected) {
55+
final ListAppender listApp = loggerContext.getConfiguration().getAppender("List");
5756
final List<String> events = listApp.getMessages();
58-
assertThat(events, hasSize(1));
59-
final String actual = events.get(0);
60-
assertThat(actual, equalTo(expected));
57+
assertThat(events).hasSize(1).containsExactly(expected);
6158
listApp.clear();
6259
}
6360
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration name="CallerInformationTest" status="OFF">
19+
<Appenders>
20+
<List name="Class">
21+
<PatternLayout pattern="%class"/>
22+
</List>
23+
<List name="Method">
24+
<PatternLayout pattern="%method"/>
25+
</List>
26+
<List name="Fqcn">
27+
<PatternLayout pattern="%fqcn"/>
28+
</List>
29+
</Appenders>
30+
<Loggers>
31+
<Logger name="ClassLogger" level="info">
32+
<AppenderRef ref="Class"/>
33+
</Logger>
34+
<Logger name="MethodLogger" level="info">
35+
<AppenderRef ref="Method"/>
36+
</Logger>
37+
<Logger name="FqcnLogger" level="info">
38+
<AppenderRef ref="Fqcn"/>
39+
</Logger>
40+
<Root level="off"/>
41+
</Loggers>
42+
</Configuration>

0 commit comments

Comments
 (0)