KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > test > slf4j > TestOfSlf4jAdapter


1 package org.grlea.log.test.slf4j;
2
3 // $Id: TestOfSlf4jAdapter.java,v 1.2 2006/07/13 12:44:56 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

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

18
19 import org.grlea.log.DebugLevel;
20 import org.grlea.log.SimpleLog;
21 import org.grlea.log.adapters.commons.CommonsLoggingAdapter;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.lang.reflect.InvocationTargetException JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 /**
38  * <p>Tests the public interface of {@link CommonsLoggingAdapter}.</p>
39  *
40  * @author Graham Lea
41  * @version $Revision: 1.2 $
42  */

43 public class
44 TestOfSlf4jAdapter
45 extends TestCase
46 {
47    private ByteArrayOutputStream JavaDoc outputStream;
48
49    public
50    TestOfSlf4jAdapter(String JavaDoc name)
51    {
52       // Standard TestCase constructor. You shouldn't edit this.
53
super(name);
54    }
55
56    static int instanceNumber = 1;
57
58    protected void
59    setUp()
60    throws Exception JavaDoc
61    {
62       outputStream = new ByteArrayOutputStream JavaDoc(512);
63
64       SimpleLog simpleLog = SimpleLog.defaultInstance();
65       Properties JavaDoc simpleLogProperties = getLogProperties(simpleLog);
66       simpleLogProperties.clear();
67       simpleLog.reloadProperties();
68       simpleLog.setWriter(new PrintWriter JavaDoc(outputStream, true));
69    }
70
71    private Properties JavaDoc
72    getLogProperties(SimpleLog simpleLog)
73    throws NoSuchFieldException JavaDoc, IllegalAccessException JavaDoc
74    {
75       Field JavaDoc propertiesField = simpleLog.getClass().getDeclaredField("properties");
76       propertiesField.setAccessible(true);
77       return (Properties JavaDoc) propertiesField.get(simpleLog);
78    }
79
80    protected void
81    tearDown()
82    {
83       outputStream = null;
84    }
85
86    public void
87    testNormalLogging()
88    throws Exception JavaDoc
89    {
90       new SimpleSlf4jLoggingClass().doSomeLogging();
91
92       String JavaDoc[] expectedOutputLineParts =
93       {
94          " |main|SimpleSlf4jLoggingClass|Test of Error",
95          " |main|SimpleSlf4jLoggingClass|Test of Warn",
96          " |main|SimpleSlf4jLoggingClass|Test of Info",
97       };
98
99       checkOutput(expectedOutputLineParts, true);
100    }
101
102    public void
103    testErrorLogging()
104    throws Exception JavaDoc
105    {
106       Properties JavaDoc logProperties = getLogProperties(SimpleLog.defaultInstance());
107       logProperties.setProperty(SimpleSlf4jLoggingClass.class.getName(), "Error");
108       reconfigureLoggers();
109       new SimpleSlf4jLoggingClass().doSomeLogging();
110
111       String JavaDoc[] expectedOutputLineParts =
112       {
113          " |main|SimpleSlf4jLoggingClass|Test of Error",
114       };
115
116       checkOutput(expectedOutputLineParts, true);
117    }
118
119    private void
120    reconfigureLoggers()
121    throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
122    {
123       Method JavaDoc reconfigureMethod = SimpleLog.class.getDeclaredMethod("reconfigureAllLoggers", new Class JavaDoc[0]);
124       reconfigureMethod.setAccessible(true);
125       reconfigureMethod.invoke(SimpleLog.defaultInstance(), new Object JavaDoc[0]);
126    }
127
128    public void
129    testLogginAnError()
130    throws Exception JavaDoc
131    {
132       SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO);
133       new SimpleSlf4jLoggingClass().logAnError();
134
135       String JavaDoc[] expectedOutputLineParts =
136       {
137          " |main|SimpleSlf4jLoggingClass|Test of Exception",
138          "***|main|SimpleSlf4jLoggingClass|java.lang.Throwable: Test Message",
139       };
140
141       checkOutput(expectedOutputLineParts, false);
142    }
143
144    public void
145    testLoggerNameThatIsNotAClassName()
146    throws Exception JavaDoc
147    {
148       String JavaDoc loggerName = "MadeUpLoggerName";
149       SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO);
150       new SimpleSlf4jLoggingClass(loggerName).doSomeLogging();
151
152       String JavaDoc[] expectedOutputLineParts =
153       {
154          " |main|Logger[MadeUpLoggerName]|Test of Error",
155          " |main|Logger[MadeUpLoggerName]|Test of Warn",
156          " |main|Logger[MadeUpLoggerName]|Test of Info",
157       };
158
159       checkOutput(expectedOutputLineParts, true);
160    }
161
162    public void
163    testConfiguringLoggerNameThatIsNotAClassName()
164    throws Exception JavaDoc
165    {
166       String JavaDoc loggerName = "MadeUpLoggerName";
167       getLogProperties(SimpleLog.defaultInstance())
168          .setProperty("org.slf4j.Logger.MadeUpLoggerName", "Error");
169       reconfigureLoggers();
170       new SimpleSlf4jLoggingClass(loggerName).doSomeLogging();
171
172       String JavaDoc[] expectedOutputLineParts =
173       {
174          " |main|Logger[MadeUpLoggerName]|Test of Error",
175       };
176
177       checkOutput(expectedOutputLineParts, true);
178    }
179
180    /**
181     * Returns a test suite that will automatically run all test methods in this
182     * class beginning with "test".
183     */

184    public static TestSuite
185    suite()
186    {
187       return new TestSuite(TestOfSlf4jAdapter.class);
188    }
189
190
191    protected void
192    checkOutput(String JavaDoc[] expectedOutputLineParts, boolean failWhenExtraLinesDetected)
193    throws IOException JavaDoc
194    {
195       System.err.flush();
196
197       byte[] output = outputStream.toByteArray();
198       ByteArrayInputStream JavaDoc byteIn = new ByteArrayInputStream JavaDoc(output);
199       InputStreamReader JavaDoc streamReader = new InputStreamReader JavaDoc(byteIn);
200       BufferedReader JavaDoc in = new BufferedReader JavaDoc(streamReader);
201       String JavaDoc outputLine;
202       int lineNumber = 0;
203       while ((outputLine = in.readLine()) != null)
204       {
205          if (lineNumber >= expectedOutputLineParts.length)
206          {
207             if (failWhenExtraLinesDetected)
208                fail("More output lines than expected.\nExtra line: " + outputLine);
209             else
210                break;
211          }
212
213          String JavaDoc expectedOutputLinePart = expectedOutputLineParts[lineNumber];
214          boolean linePartFound = outputLine.indexOf(expectedOutputLinePart) != -1;
215          assertEquals("'" + expectedOutputLinePart + "' not found in '" + outputLine + "'",
216                       true, linePartFound);
217
218          lineNumber++;
219       }
220
221       assertEquals("output lines", expectedOutputLineParts.length, lineNumber);
222    }
223 }
Popular Tags