Skip to content

Commit

Permalink
Introduce log4j 2 for Spring's test suite
Browse files Browse the repository at this point in the history
This commit adds a test runtime dependency on log4j 2 for every project
and migrates all log4j.properties files to log4j2-test.xml files.

Issue: SPR-14431
  • Loading branch information
sbrannen committed Jul 5, 2016
1 parent 9a9551b commit 1391248
Show file tree
Hide file tree
Showing 40 changed files with 324 additions and 225 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ configure(allprojects) { project ->
ext.junitVersion = "4.12"
ext.junitJupiterVersion = '5.0.0-SNAPSHOT'
ext.junitPlatformVersion = '1.0.0-SNAPSHOT'
ext.log4JVersion = '2.6.1'
ext.nettyVersion = "4.1.1.Final"
ext.okhttpVersion = "2.7.5"
ext.okhttp3Version = "3.3.1"
Expand Down Expand Up @@ -139,6 +140,9 @@ configure(allprojects) { project ->
}
testCompile("org.hamcrest:hamcrest-all:${hamcrestVersion}")

testRuntime("org.apache.logging.log4j:log4j-core:${log4JVersion}")
testRuntime("org.apache.logging.log4j:log4j-jcl:${log4JVersion}")

sniffer("org.codehaus.mojo:animal-sniffer-ant-tasks:${snifferVersion}")
javaApiSignature("org.codehaus.mojo.signature:java18:1.0@signature")
}
Expand Down
10 changes: 0 additions & 10 deletions spring-aop/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-aop/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.aop" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
9 changes: 0 additions & 9 deletions spring-aspects/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-aspects/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
9 changes: 0 additions & 9 deletions spring-beans/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-beans/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
10 changes: 0 additions & 10 deletions spring-context-support/src/test/resources/log4j.properties

This file was deleted.

16 changes: 16 additions & 0 deletions spring-context-support/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans" level="warn" />
<Logger name="org.springframework.cache" level="warn" />
<Logger name="org.springframework.mail" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class AutowiredConfigurationTests {

Expand Down Expand Up @@ -203,20 +204,23 @@ public void testCustomPropertiesWithClassPathContext() throws IOException {

TestBean testBean = context.getBean("testBean", TestBean.class);
assertThat(testBean.getName(), equalTo("localhost"));
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
assertThat(testBean.getAge(), equalTo(contentLength()));
}

@Test
public void testCustomPropertiesWithGenericContext() throws IOException {
GenericApplicationContext context = new GenericApplicationContext();
// context.setResourceLoader(new FileSystemResourceLoader());
new XmlBeanDefinitionReader(context).loadBeanDefinitions(
new ClassPathResource("AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class));
context.refresh();

TestBean testBean = context.getBean("testBean", TestBean.class);
assertThat(testBean.getName(), equalTo("localhost"));
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
assertThat(testBean.getAge(), equalTo(contentLength()));
}

private int contentLength() throws IOException {
return (int) new ClassPathResource("do_not_delete_me.txt").contentLength();
}


Expand Down Expand Up @@ -477,7 +481,7 @@ public void setHostname(String hostname) {
this.hostname = hostname;
}

@Value("log4j.properties")
@Value("do_not_delete_me.txt")
public void setResource(Resource resource) {
this.resource = resource;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;

import org.springframework.beans.factory.ObjectFactory;
Expand Down Expand Up @@ -61,6 +62,7 @@

/**
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
*/
public class ApplicationContextExpressionTests {
Expand Down Expand Up @@ -326,11 +328,10 @@ public void stringConcatenationWithDebugLogging() {

@Test
public void resourceInjection() throws IOException {
System.setProperty("logfile", "log4j.properties");
try {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class);
System.setProperty("logfile", "do_not_delete_me.txt");
try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) {
ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class);
Resource resource = new ClassPathResource("log4j.properties");
Resource resource = new ClassPathResource("do_not_delete_me.txt");
assertEquals(resource, resourceInjectionBean.resource);
assertEquals(resource.getURL(), resourceInjectionBean.url);
assertEquals(resource.getURI(), resourceInjectionBean.uri);
Expand Down
1 change: 1 addition & 0 deletions spring-context/src/test/resources/do_not_delete_me.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Please do not delete me; otherwise, you'll break some tests.
9 changes: 0 additions & 9 deletions spring-context/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-context/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.core" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
11 changes: 0 additions & 11 deletions spring-core/src/test/resources/log4j.properties

This file was deleted.

21 changes: 21 additions & 0 deletions spring-core/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="build/logs/spring-test.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="org.springframework.core" level="info" />
<Logger name="org.springframework.core.convert" level="warn" />
<Logger name="org.springframework.core.GenericTypeResolver" level="warn" />
<Logger name="org.springframework.util" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
10 changes: 0 additions & 10 deletions spring-expression/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-expression/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.expression" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
10 changes: 0 additions & 10 deletions spring-instrument/src/test/resources/log4j.properties

This file was deleted.

13 changes: 13 additions & 0 deletions spring-instrument/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
16 changes: 0 additions & 16 deletions spring-jdbc/src/test/resources/log4j.properties

This file was deleted.

19 changes: 19 additions & 0 deletions spring-jdbc/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="build/logs/spring-jdbc.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="org.springframework.jdbc" level="warn" />
<Logger name="org.springframework.jdbc.datasource.embedded" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
11 changes: 0 additions & 11 deletions spring-jms/src/test/resources/log4j.properties

This file was deleted.

14 changes: 14 additions & 0 deletions spring-jms/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.jms" level="warn" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
13 changes: 0 additions & 13 deletions spring-messaging/src/test/resources/log4j.properties

This file was deleted.

Loading

0 comments on commit 1391248

Please sign in to comment.