1 16 17 package org.springframework.web.util; 18 19 import java.io.FileNotFoundException ; 20 import java.net.URL ; 21 22 import javax.servlet.ServletContextEvent ; 23 import javax.servlet.ServletException ; 24 25 import junit.framework.TestCase; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.core.io.FileSystemResourceLoader; 30 import org.springframework.mock.web.MockServletConfig; 31 import org.springframework.mock.web.MockServletContext; 32 import org.springframework.util.Log4jConfigurerTests; 33 import org.springframework.util.MockLog4jAppender; 34 35 39 public class Log4jWebConfigurerTests extends TestCase { 40 41 public void testInitLoggingWithClasspath() throws FileNotFoundException { 42 doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", false); 43 } 44 45 public void testInitLoggingWithRelativeFilePath() throws FileNotFoundException { 46 doTestInitLogging("test/org/springframework/util/testlog4j.properties", false); 47 } 48 49 public void testInitLoggingWithAbsoluteFilePath() throws FileNotFoundException { 50 URL url = Log4jConfigurerTests.class.getResource("testlog4j.properties"); 51 doTestInitLogging(url.toString(), false); 52 } 53 54 public void testInitLoggingWithClasspathAndRefreshInterval() throws FileNotFoundException { 55 doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", true); 56 } 57 58 public void testInitLoggingWithRelativeFilePathAndRefreshInterval() throws FileNotFoundException { 59 doTestInitLogging("test/org/springframework/util/testlog4j.properties", true); 60 } 61 62 68 69 public void testInitLoggingWithFileUrlAndRefreshInterval() throws FileNotFoundException { 70 URL url = Log4jConfigurerTests.class.getResource("testlog4j.properties"); 71 doTestInitLogging(url.toString(), true); 72 } 73 74 private void doTestInitLogging(String location, boolean refreshInterval) { 75 MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader()); 76 sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, location); 77 if (refreshInterval) { 78 sc.addInitParameter(Log4jWebConfigurer.REFRESH_INTERVAL_PARAM, "10"); 79 } 80 Log4jWebConfigurer.initLogging(sc); 81 82 try { 83 doTestLogOutput(); 84 } 85 finally { 86 Log4jWebConfigurer.shutdownLogging(sc); 87 } 88 assertTrue(MockLog4jAppender.closeCalled); 89 } 90 91 private void doTestLogOutput() { 92 Log log = LogFactory.getLog(this.getClass()); 93 log.debug("debug"); 94 log.info("info"); 95 log.warn("warn"); 96 log.error("error"); 97 log.fatal("fatal"); 98 99 assertTrue(MockLog4jAppender.loggingStrings.contains("debug")); 100 assertTrue(MockLog4jAppender.loggingStrings.contains("info")); 101 assertTrue(MockLog4jAppender.loggingStrings.contains("warn")); 102 assertTrue(MockLog4jAppender.loggingStrings.contains("error")); 103 assertTrue(MockLog4jAppender.loggingStrings.contains("fatal")); 104 } 105 106 public void testLog4jConfigListener() { 107 Log4jConfigListener listener = new Log4jConfigListener(); 108 109 MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader()); 110 sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, 111 "test/org/springframework/util/testlog4j.properties"); 112 listener.contextInitialized(new ServletContextEvent (sc)); 113 114 try { 115 doTestLogOutput(); 116 } 117 finally { 118 listener.contextDestroyed(new ServletContextEvent (sc)); 119 } 120 assertTrue(MockLog4jAppender.closeCalled); 121 } 122 123 public void testLog4jConfigServlet() throws ServletException { 124 Log4jConfigServlet servlet = new Log4jConfigServlet(); 125 126 MockServletContext sc = new MockServletContext("", new FileSystemResourceLoader()); 127 sc.addInitParameter(Log4jWebConfigurer.CONFIG_LOCATION_PARAM, 128 "test/org/springframework/util/testlog4j.properties"); 129 servlet.init(new MockServletConfig(sc)); 130 131 try { 132 doTestLogOutput(); 133 } 134 finally { 135 servlet.destroy(); 136 } 137 assertTrue(MockLog4jAppender.closeCalled); 138 } 139 140 } 141 | Popular Tags |