1 16 17 package org.springframework.util; 18 19 import java.io.FileNotFoundException ; 20 import java.net.URL ; 21 22 import junit.framework.TestCase; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 30 public class Log4jConfigurerTests extends TestCase { 31 32 public void testInitLoggingWithClasspath() throws FileNotFoundException { 33 doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", false); 34 } 35 36 public void testInitLoggingWithRelativeFilePath() throws FileNotFoundException { 37 doTestInitLogging("test/org/springframework/util/testlog4j.properties", false); 38 } 39 40 public void testInitLoggingWithAbsoluteFilePath() throws FileNotFoundException { 41 URL url = getClass().getResource("testlog4j.properties"); 42 doTestInitLogging(url.toString(), false); 43 } 44 45 public void testInitLoggingWithClasspathAndRefreshInterval() throws FileNotFoundException { 46 doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", true); 47 } 48 49 public void testInitLoggingWithRelativeFilePathAndRefreshInterval() throws FileNotFoundException { 50 doTestInitLogging("test/org/springframework/util/testlog4j.properties", true); 51 } 52 53 59 60 public void testInitLoggingWithFileUrlAndRefreshInterval() throws FileNotFoundException { 61 URL url = getClass().getResource("testlog4j.properties"); 62 doTestInitLogging(url.toString(), true); 63 } 64 65 private void doTestInitLogging(String location, boolean refreshInterval) throws FileNotFoundException { 66 if (refreshInterval) { 67 Log4jConfigurer.initLogging(location, 10); 68 } 69 else { 70 Log4jConfigurer.initLogging(location); 71 } 72 73 Log log = LogFactory.getLog(this.getClass()); 74 log.debug("debug"); 75 log.info("info"); 76 log.warn("warn"); 77 log.error("error"); 78 log.fatal("fatal"); 79 80 assertTrue(MockLog4jAppender.loggingStrings.contains("debug")); 81 assertTrue(MockLog4jAppender.loggingStrings.contains("info")); 82 assertTrue(MockLog4jAppender.loggingStrings.contains("warn")); 83 assertTrue(MockLog4jAppender.loggingStrings.contains("error")); 84 assertTrue(MockLog4jAppender.loggingStrings.contains("fatal")); 85 86 Log4jConfigurer.shutdownLogging(); 87 assertTrue(MockLog4jAppender.closeCalled); 88 } 89 90 public void testInitLoggingWithRefreshIntervalAndFileNotFound() throws FileNotFoundException { 91 try { 92 Log4jConfigurer.initLogging("test/org/springframework/util/bla.properties", 10); 93 fail("Exception should have been thrown, file does not exist!"); 94 } 95 catch (FileNotFoundException ex) { 96 } 98 } 99 100 } 101 | Popular Tags |