KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > Log4jConfigurerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.FileNotFoundException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * @author Alef Arendsen
28  * @author Juergen Hoeller
29  */

30 public class Log4jConfigurerTests extends TestCase {
31
32     public void testInitLoggingWithClasspath() throws FileNotFoundException JavaDoc {
33         doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", false);
34     }
35
36     public void testInitLoggingWithRelativeFilePath() throws FileNotFoundException JavaDoc {
37         doTestInitLogging("test/org/springframework/util/testlog4j.properties", false);
38     }
39
40     public void testInitLoggingWithAbsoluteFilePath() throws FileNotFoundException JavaDoc {
41         URL JavaDoc url = getClass().getResource("testlog4j.properties");
42         doTestInitLogging(url.toString(), false);
43     }
44
45     public void testInitLoggingWithClasspathAndRefreshInterval() throws FileNotFoundException JavaDoc {
46         doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", true);
47     }
48
49     public void testInitLoggingWithRelativeFilePathAndRefreshInterval() throws FileNotFoundException JavaDoc {
50         doTestInitLogging("test/org/springframework/util/testlog4j.properties", true);
51     }
52
53     /* only works on Windows
54     public void testInitLoggingWithAbsoluteFilePathAndRefreshInterval() throws FileNotFoundException {
55         URL url = getClass().getResource("testlog4j.properties");
56         doTestInitLogging(url.getFile(), true);
57     }
58     */

59
60     public void testInitLoggingWithFileUrlAndRefreshInterval() throws FileNotFoundException JavaDoc {
61         URL JavaDoc url = getClass().getResource("testlog4j.properties");
62         doTestInitLogging(url.toString(), true);
63     }
64
65     private void doTestInitLogging(String JavaDoc location, boolean refreshInterval) throws FileNotFoundException JavaDoc {
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 JavaDoc {
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 JavaDoc ex) {
96             // OK
97
}
98     }
99
100 }
101
Popular Tags