KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.avalon.framework.logger.Logger;
25 import org.apache.avalon.framework.logger.LoggerAwareOutputStream;
26
27 /**
28  * Test case for LoggerAwareOutputStream class.
29  *
30  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
31  * @since Dec 4, 2004 6:53:48 PM
32  */

33 public class LoggerAwareOutputStreamTestCase extends TestCase
34 {
35     /**
36      * Tests the logger aware output stream by sending a test string to the
37      * output stream, and checking whether the logger logged the same string.
38      *
39      * @throws Exception if an error occurs
40      */

41     public void testLoggerAwareOutputStream() throws Exception JavaDoc
42     {
43         final TestLogger logger = new TestLogger();
44         final String JavaDoc testString = "a test string";
45         
46         final OutputStream JavaDoc os =
47             new LoggerAwareOutputStream( logger ) {
48                 protected void logMessage( String JavaDoc message )
49                 {
50                     m_logger.debug( message );
51                 }
52             };
53
54         final OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(os);
55         writer.write(testString, 0, testString.length());
56         writer.close();
57         final String JavaDoc message = logger.getDebugMessage();
58
59         // check that string printed to the output stream is the same in the log
60
assertEquals("Logged message doesn't equal source string", testString, message);
61
62     }
63
64     /**
65      * Based on NullLogger.
66      */

67     private class TestLogger implements Logger
68     {
69         /**
70          * Creates a new <code>NullLogger</code>.
71          */

72         public TestLogger()
73         {
74         }
75
76         /**
77          * Stores debug message inside of this test logger.
78          *
79          * @param message ignored
80          */

81         public void debug( String JavaDoc message )
82         {
83             this.message = message;
84         }
85         
86         private String JavaDoc message;
87         
88         /**
89          * Get the saved message from the last call to debug
90          * @return
91          */

92         public String JavaDoc getDebugMessage()
93         {
94             return message;
95         }
96
97         /**
98          * No-op.
99          *
100          * @param message ignored
101          * @param throwable ignored
102          */

103         public void debug( String JavaDoc message, Throwable JavaDoc throwable )
104         {
105         }
106
107         /**
108          * Always returns true
109          *
110          * @return <code>true</code>
111          */

112         public boolean isDebugEnabled()
113         {
114             return true;
115         }
116
117         /**
118          * No-op.
119          *
120          * @param message ignored
121          */

122         public void info( String JavaDoc message )
123         {
124         }
125
126         /**
127          * No-op.
128          *
129          * @param message ignored
130          * @param throwable ignored
131          */

132         public void info( String JavaDoc message, Throwable JavaDoc throwable )
133         {
134         }
135
136         /**
137          * No-op.
138          *
139          * @return <code>false</code>
140          */

141         public boolean isInfoEnabled()
142         {
143             return false;
144         }
145
146         /**
147          * No-op.
148          *
149          * @param message ignored
150          */

151         public void warn( String JavaDoc message )
152         {
153         }
154
155         /**
156          * No-op.
157          *
158          * @param message ignored
159          * @param throwable ignored
160          */

161         public void warn( String JavaDoc message, Throwable JavaDoc throwable )
162         {
163         }
164
165         /**
166          * No-op.
167          *
168          * @return <code>false</code>
169          */

170         public boolean isWarnEnabled()
171         {
172             return false;
173         }
174
175         /**
176          * No-op.
177          *
178          * @param message ignored
179          */

180         public void error( String JavaDoc message )
181         {
182         }
183
184         /**
185          * No-op.
186          *
187          * @param message ignored
188          * @param throwable ignored
189          */

190         public void error( String JavaDoc message, Throwable JavaDoc throwable )
191         {
192         }
193
194         /**
195          * No-op.
196          *
197          * @return <code>false</code>
198          */

199         public boolean isErrorEnabled()
200         {
201             return false;
202         }
203
204         /**
205          * No-op.
206          *
207          * @param message ignored
208          */

209         public void fatalError( String JavaDoc message )
210         {
211         }
212
213         /**
214          * No-op.
215          *
216          * @param message ignored
217          * @param throwable ignored
218          */

219         public void fatalError( String JavaDoc message, Throwable JavaDoc throwable )
220         {
221         }
222
223         /**
224          * No-op.
225          *
226          * @return <code>false</code>
227          */

228         public boolean isFatalErrorEnabled()
229         {
230             return false;
231         }
232
233         /**
234          * Returns this <code>NullLogger</code>.
235          *
236          * @param name ignored
237          * @return this <code>NullLogger</code>
238          */

239         public Logger getChildLogger( String JavaDoc name )
240         {
241             return this;
242         }
243     }
244 }
245
Popular Tags