[SPR-8090] Fixed broken tests in Log4jWebConfigurerTests.

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4117 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Sam Brannen 2011-03-28 17:20:40 +00:00
parent 00c3ca6c3f
commit 56fa618e12
2 changed files with 51 additions and 43 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,62 +16,70 @@
package org.springframework.web.util; package org.springframework.web.util;
import java.io.FileNotFoundException; import static org.junit.Assert.assertTrue;
import java.net.URL; import java.net.URL;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import junit.framework.TestCase;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.FileSystemResourceLoader; import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
* @since 21.02.2005 * @since 21.02.2005
*/ */
@Ignore public class Log4jWebConfigurerTests {
public class Log4jWebConfigurerTests extends TestCase {
public void testInitLoggingWithClasspath() throws FileNotFoundException { private static final String TESTLOG4J_PROPERTIES = "testlog4j.properties";
doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", false); private static final String CLASSPATH_RESOURCE = "classpath:org/springframework/web/util/testlog4j.properties";
private static final String RELATIVE_PATH = "src/test/resources/org/springframework/web/util/testlog4j.properties";
@Test
public void initLoggingWithClasspathResource() {
initLogging(CLASSPATH_RESOURCE, false);
} }
public void testInitLoggingWithRelativeFilePath() throws FileNotFoundException { @Test
doTestInitLogging("test/org/springframework/util/testlog4j.properties", false); public void initLoggingWithClasspathResourceAndRefreshInterval() {
initLogging(CLASSPATH_RESOURCE, true);
}
@Test
public void initLoggingWithRelativeFilePath() {
initLogging(RELATIVE_PATH, false);
} }
public void testInitLoggingWithAbsoluteFilePath() throws FileNotFoundException { @Test
URL url = Log4jWebConfigurerTests.class.getResource("testlog4j.properties"); public void initLoggingWithRelativeFilePathAndRefreshInterval() {
doTestInitLogging(url.toString(), false); initLogging(RELATIVE_PATH, true);
}
@Test
public void initLoggingWithUrl() {
URL url = Log4jWebConfigurerTests.class.getResource(TESTLOG4J_PROPERTIES);
initLogging(url.toString(), false);
} }
public void testInitLoggingWithClasspathAndRefreshInterval() throws FileNotFoundException { @Test
doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", true); public void initLoggingWithUrlAndRefreshInterval() {
URL url = Log4jWebConfigurerTests.class.getResource(TESTLOG4J_PROPERTIES);
initLogging(url.toString(), true);
} }
public void testInitLoggingWithRelativeFilePathAndRefreshInterval() throws FileNotFoundException { @Ignore("Only works on MS Windows")
doTestInitLogging("test/org/springframework/util/testlog4j.properties", true); @Test
public void initLoggingWithAbsoluteFilePathAndRefreshInterval() {
URL url = Log4jWebConfigurerTests.class.getResource(TESTLOG4J_PROPERTIES);
initLogging(url.getFile(), true);
} }
/* only works on Windows private void initLogging(String location, boolean refreshInterval) {
public void testInitLoggingWithAbsoluteFilePathAndRefreshInterval() throws FileNotFoundException {
URL url = Log4jConfigurerTests.class.getResource("testlog4j.properties");
doTestInitLogging(url.getFile(), true);
}
*/
public void testInitLoggingWithFileUrlAndRefreshInterval() throws FileNotFoundException {
URL url = Log4jWebConfigurerTests.class.getResource("testlog4j.properties");
doTestInitLogging(url.toString(), true);
}
private void doTestInitLogging(String location, boolean refreshInterval) {
MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader()); MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, location); sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, location);
if (refreshInterval) { if (refreshInterval) {
@ -80,15 +88,14 @@ public class Log4jWebConfigurerTests extends TestCase {
Log4jWebConfigurer.initLogging(sc); Log4jWebConfigurer.initLogging(sc);
try { try {
doTestLogOutput(); assertLogOutput();
} } finally {
finally {
Log4jWebConfigurer.shutdownLogging(sc); Log4jWebConfigurer.shutdownLogging(sc);
} }
assertTrue(MockLog4jAppender.closeCalled); assertTrue(MockLog4jAppender.closeCalled);
} }
private void doTestLogOutput() { private void assertLogOutput() {
Log log = LogFactory.getLog(this.getClass()); Log log = LogFactory.getLog(this.getClass());
log.debug("debug"); log.debug("debug");
log.info("info"); log.info("info");
@ -103,18 +110,17 @@ public class Log4jWebConfigurerTests extends TestCase {
assertTrue(MockLog4jAppender.loggingStrings.contains("fatal")); assertTrue(MockLog4jAppender.loggingStrings.contains("fatal"));
} }
@Test
public void testLog4jConfigListener() { public void testLog4jConfigListener() {
Log4jConfigListener listener = new Log4jConfigListener(); Log4jConfigListener listener = new Log4jConfigListener();
MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader()); MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader());
sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, RELATIVE_PATH);
"test/org/springframework/util/testlog4j.properties");
listener.contextInitialized(new ServletContextEvent(sc)); listener.contextInitialized(new ServletContextEvent(sc));
try { try {
doTestLogOutput(); assertLogOutput();
} } finally {
finally {
listener.contextDestroyed(new ServletContextEvent(sc)); listener.contextDestroyed(new ServletContextEvent(sc));
} }
assertTrue(MockLog4jAppender.closeCalled); assertTrue(MockLog4jAppender.closeCalled);

View File

@ -0,0 +1,2 @@
log4j.rootCategory=DEBUG, mock
log4j.appender.mock=org.springframework.web.util.MockLog4jAppender