KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > logger > test > CommonsLoggerTestCase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.framework.logger.test;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.PrintStream JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.avalon.framework.logger.CommonsLogger;
24 import org.apache.avalon.framework.logger.Logger;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.logging.impl.SimpleLog;
28
29 import org.jmock.Mock;
30 import org.jmock.MockObjectTestCase;
31
32 /**
33  * Test case for CommonsLogger class.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version $Id: CommonsLoggerTestCase.java 506231 2007-02-12 02:36:54Z crossley $
37  * @since 4.3
38  */

39 public class CommonsLoggerTestCase extends MockObjectTestCase
40 {
41
42     private Mock mockLog;
43     private Exception JavaDoc exception;
44
45     protected void setUp() throws Exception JavaDoc
46     {
47         super.setUp();
48         mockLog = mock(Log.class);
49         exception = new Exception JavaDoc("JUnit");
50     }
51
52     /**
53      * Test debug level.
54      */

55     public void testDebug()
56     {
57         final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit");
58
59         mockLog.expects(once()).method("isDebugEnabled").will(returnValue(true));
60         mockLog.expects(once()).method("debug").with(eq("JUnit"));
61         mockLog.expects(once()).method("debug").with(eq("JUnit"), same(exception));
62
63         if(logger.isDebugEnabled())
64         {
65             logger.debug("JUnit");
66             logger.debug("JUnit", exception);
67         }
68     }
69
70     /**
71      * Test info level.
72      */

73     public void testInfo()
74     {
75         final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit");
76
77         mockLog.expects(once()).method("isInfoEnabled").will(returnValue(true));
78         mockLog.expects(once()).method("info").with(eq("JUnit"));
79         mockLog.expects(once()).method("info").with(eq("JUnit"), same(exception));
80
81         if(logger.isInfoEnabled())
82         {
83             logger.info("JUnit");
84             logger.info("JUnit", exception);
85         }
86     }
87
88     /**
89      * Test warn level.
90      */

91     public void testWarn()
92     {
93         final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit");
94
95         mockLog.expects(once()).method("isWarnEnabled").will(returnValue(true));
96         mockLog.expects(once()).method("warn").with(eq("JUnit"));
97         mockLog.expects(once()).method("warn").with(eq("JUnit"), same(exception));
98
99         if(logger.isWarnEnabled())
100         {
101             logger.warn("JUnit");
102             logger.warn("JUnit", exception);
103         }
104     }
105
106     /**
107      * Test error level.
108      */

109     public void testError()
110     {
111         final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit");
112
113         mockLog.expects(once()).method("isErrorEnabled").will(returnValue(true));
114         mockLog.expects(once()).method("error").with(eq("JUnit"));
115         mockLog.expects(once()).method("error").with(eq("JUnit"), same(exception));
116
117         if(logger.isErrorEnabled())
118         {
119             logger.error("JUnit");
120             logger.error("JUnit", exception);
121         }
122     }
123
124     /**
125      * Test fatal error level.
126      */

127     public void testFatalError()
128     {
129         final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit");
130
131         mockLog.expects(once()).method("isFatalEnabled").will(returnValue(true));
132         mockLog.expects(once()).method("fatal").with(eq("JUnit"));
133         mockLog.expects(once()).method("fatal").with(eq("JUnit"), same(exception));
134
135         if(logger.isFatalErrorEnabled())
136         {
137             logger.fatalError("JUnit");
138             logger.fatalError("JUnit", exception);
139         }
140     }
141
142     /**
143      * Test creation of a child logger. Nees <tt>simplelog.properties</tt> as resource.
144      */

145     public void testChildLogger()
146     {
147         final Properties JavaDoc systemProperties = System.getProperties();
148         final PrintStream JavaDoc err = System.err;
149         try
150         {
151             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
152             final PrintStream JavaDoc stream = new PrintStream JavaDoc(buffer, true);
153             System.setProperty(Log.class.getName(), SimpleLog.class.getName());
154             LogFactory.releaseAll();
155             System.setErr(stream);
156             final Logger logger = new CommonsLogger(LogFactory.getLog("JUnit"), "JUnit");
157             final Logger child = logger.getChildLogger("test");
158             child.fatalError("foo");
159             assertEquals("[FATAL] JUnit.test - foo", buffer.toString().trim());
160         }
161         finally
162         {
163             System.setProperties(systemProperties);
164             System.setErr(err);
165         }
166
167     }
168 }
169
170
Popular Tags