KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > Log4jWebConfigurerTests


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.web.util;
18
19 import java.io.FileNotFoundException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.servlet.ServletContextEvent JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
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 /**
36  * @author Juergen Hoeller
37  * @since 21.02.2005
38  */

39 public class Log4jWebConfigurerTests extends TestCase {
40
41     public void testInitLoggingWithClasspath() throws FileNotFoundException JavaDoc {
42         doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", false);
43     }
44
45     public void testInitLoggingWithRelativeFilePath() throws FileNotFoundException JavaDoc {
46         doTestInitLogging("test/org/springframework/util/testlog4j.properties", false);
47     }
48
49     public void testInitLoggingWithAbsoluteFilePath() throws FileNotFoundException JavaDoc {
50         URL JavaDoc url = Log4jConfigurerTests.class.getResource("testlog4j.properties");
51         doTestInitLogging(url.toString(), false);
52     }
53
54     public void testInitLoggingWithClasspathAndRefreshInterval() throws FileNotFoundException JavaDoc {
55         doTestInitLogging("classpath:org/springframework/util/testlog4j.properties", true);
56     }
57
58     public void testInitLoggingWithRelativeFilePathAndRefreshInterval() throws FileNotFoundException JavaDoc {
59         doTestInitLogging("test/org/springframework/util/testlog4j.properties", true);
60     }
61
62     /* only works on Windows
63     public void testInitLoggingWithAbsoluteFilePathAndRefreshInterval() throws FileNotFoundException {
64         URL url = Log4jConfigurerTests.class.getResource("testlog4j.properties");
65         doTestInitLogging(url.getFile(), true);
66     }
67     */

68
69     public void testInitLoggingWithFileUrlAndRefreshInterval() throws FileNotFoundException JavaDoc {
70         URL JavaDoc url = Log4jConfigurerTests.class.getResource("testlog4j.properties");
71         doTestInitLogging(url.toString(), true);
72     }
73
74     private void doTestInitLogging(String JavaDoc 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 JavaDoc(sc));
113
114         try {
115             doTestLogOutput();
116         }
117         finally {
118             listener.contextDestroyed(new ServletContextEvent JavaDoc(sc));
119         }
120         assertTrue(MockLog4jAppender.closeCalled);
121     }
122
123     public void testLog4jConfigServlet() throws ServletException JavaDoc {
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