KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > test > OutputTargetTestCase


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

17 package org.apache.log.output.test;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import junit.framework.TestCase;
25 import org.apache.log.Hierarchy;
26 import org.apache.log.LogTarget;
27 import org.apache.log.Logger;
28 import org.apache.log.Priority;
29 import org.apache.log.format.RawFormatter;
30 import org.apache.log.output.AbstractOutputTarget;
31 import org.apache.log.output.MemoryTarget;
32 import org.apache.log.output.io.FileTarget;
33 import org.apache.log.output.io.SafeFileTarget;
34 import org.apache.log.output.io.StreamTarget;
35 import org.apache.log.output.io.WriterTarget;
36
37 /**
38  * Test suite for the formatters.
39  * TODO: Incorporate testing for ContextStack and ContextMap
40  *
41  * @author Peter Donald
42  */

43 public final class OutputTargetTestCase
44     extends TestCase
45 {
46     private static String JavaDoc M1 = "meep meep!";
47     private static String JavaDoc M2 = "marina";
48     private static String JavaDoc M3 = "spin and then fall down";
49
50     private static String JavaDoc HEAD = "";
51     private static String JavaDoc TAIL = "";
52
53     private static String JavaDoc R1 = M1;
54     private static String JavaDoc R2 = M2;
55     private static String JavaDoc R3 = M3;
56
57     private static String JavaDoc OUTPUT = HEAD + R1 + R2 + R3 + TAIL;
58
59     private static RawFormatter FORMATTER = new RawFormatter();
60
61     private final File JavaDoc m_logFile;
62
63     public OutputTargetTestCase( final String JavaDoc name )
64         throws IOException JavaDoc
65     {
66         super( name );
67
68         m_logFile = ( new File JavaDoc( "test/log/logfile.txt" ) ).getCanonicalFile();
69     }
70
71     private String JavaDoc getResult( final ByteArrayOutputStream JavaDoc output )
72     {
73         final String JavaDoc result = output.toString();
74         output.reset();
75         return result;
76     }
77
78     public void testStreamTarget()
79         throws Exception JavaDoc
80     {
81         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
82         final StreamTarget target = new StreamTarget( output, FORMATTER );
83         doStreamTest( output, target );
84         /*
85           final ExtendedPatternFormatter formatter =
86           new ExtendedPatternFormatter( "%{method} from %{thread}\n" );
87           final StreamTarget target2 = new StreamTarget( System.out, formatter );
88           final Logger logger = getNewLogger( target2 );
89           logger.debug( M1 );
90         */

91     }
92
93     public void testWriterTarget()
94         throws Exception JavaDoc
95     {
96         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
97         final OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc( output );
98         final WriterTarget target = new WriterTarget( writer, FORMATTER );
99         doStreamTest( output, target );
100     }
101
102     public void testFileTarget()
103         throws Exception JavaDoc
104     {
105         final FileTarget target = new FileTarget( m_logFile, false, FORMATTER );
106
107         final Logger logger = getNewLogger( target );
108         logger.debug( M1 );
109         logger.debug( M2 );
110         logger.debug( M3 );
111         target.close();
112
113         final String JavaDoc data = getFileContents( m_logFile );
114         assertEquals( "Targets file output", OUTPUT, data );
115         assertTrue( "Deleting logfile", m_logFile.delete() );
116
117         logger.debug( M1 );
118         assertTrue( "Write after close()", !m_logFile.exists() );
119     }
120
121     public void testSafeFileTarget()
122         throws Exception JavaDoc
123     {
124         final SafeFileTarget target = new SafeFileTarget( m_logFile, false, FORMATTER );
125
126         final Logger logger = getNewLogger( target );
127         logger.debug( M1 );
128
129         final String JavaDoc data1 = getFileContents( m_logFile );
130         assertEquals( "Targets file output", HEAD + R1, data1 );
131         assertTrue( "Deleting logfile", m_logFile.delete() );
132
133         logger.debug( M2 );
134         logger.debug( M3 );
135         target.close();
136
137         final String JavaDoc data2 = getFileContents( m_logFile );
138         assertEquals( "Targets file output", R2 + R3 + TAIL, data2 );
139         assertTrue( "Deleting logfile", m_logFile.delete() );
140
141         logger.debug( M1 );
142         assertTrue( "Write after close()", !m_logFile.exists() );
143     }
144
145     /**
146      * Test that MemoryTarget triggers on buffer size hit.
147      *
148      * @exception Exception if an error occurs
149      */

150     public void testMemoryTarget1()
151         throws Exception JavaDoc
152     {
153         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
154         final StreamTarget other = new StreamTarget( output, FORMATTER );
155         final MemoryTarget target = new MemoryTarget( other, 1, Priority.FATAL_ERROR );
156         doStreamOutputTest( output, target );
157     }
158
159     /**
160      * Test that MemoryTarget triggers on priority correctly.
161      *
162      * @exception Exception if an error occurs
163      */

164     public void testMemoryTarget2()
165         throws Exception JavaDoc
166     {
167         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
168         final StreamTarget other = new StreamTarget( output, FORMATTER );
169         final MemoryTarget target = new MemoryTarget( other, 10, Priority.DEBUG );
170         doStreamOutputTest( output, target );
171     }
172
173     /**
174      * Test that MemoryTarget triggers on priority correctly.
175      *
176      * @exception Exception if an error occurs
177      */

178     public void testMemoryTarget3()
179         throws Exception JavaDoc
180     {
181         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
182         final StreamTarget other = new StreamTarget( output, FORMATTER );
183         final MemoryTarget target = new MemoryTarget( other, 10, Priority.FATAL_ERROR );
184
185         final Logger logger = getNewLogger( target );
186
187         //Head output should not be pushed yet
188
final String JavaDoc head = getResult( output );
189         assertEquals( "Targets Head output", "", head );
190
191         //Not pushed yet
192
logger.debug( M1 );
193         final String JavaDoc result1 = getResult( output );
194         assertEquals( "Targets R1 debug output", "", result1 );
195
196         target.push();
197         final String JavaDoc resultPP = getResult( output );
198         assertEquals( "Targets HEAD+R1 debug output", HEAD + R1, resultPP );
199
200         logger.debug( M2 );
201         final String JavaDoc result2 = getResult( output );
202
203         logger.debug( M3 );
204         final String JavaDoc result3 = getResult( output );
205
206         //fatal error triggers a push
207
logger.fatalError( M3 );
208         final String JavaDoc result4 = getResult( output );
209
210         assertEquals( "Targets R2 debug output", "", result2 );
211         assertEquals( "Targets R3 debug output", "", result3 );
212         assertEquals( "Targets R3 debug output", R2 + R3 + R3, result4 );
213     }
214
215     private Logger getNewLogger( final LogTarget target )
216     {
217         final Hierarchy hierarchy = new Hierarchy();
218         final Logger logger = hierarchy.getLoggerFor( "myCategory" );
219         logger.setLogTargets( new LogTarget[]{target} );
220         return logger;
221     }
222
223     private String JavaDoc getFileContents( final File JavaDoc file )
224         throws IOException JavaDoc
225     {
226         final FileInputStream JavaDoc input = new FileInputStream JavaDoc( file );
227         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
228
229         int ch = input.read();
230         while( -1 != ch )
231         {
232             sb.append( (char)ch );
233             ch = input.read();
234         }
235
236         input.close();
237
238         return sb.toString();
239     }
240
241     private Logger doStreamOutputTest( final ByteArrayOutputStream JavaDoc output,
242                                        final LogTarget target )
243     {
244         final Logger logger = getNewLogger( target );
245
246         final String JavaDoc head = getResult( output );
247
248         logger.debug( M1 );
249         final String JavaDoc result1 = getResult( output );
250
251         logger.debug( M2 );
252         final String JavaDoc result2 = getResult( output );
253
254         logger.debug( M3 );
255         final String JavaDoc result3 = getResult( output );
256
257         assertEquals( "Targets Head output", HEAD, head );
258         assertEquals( "Targets R1 debug output", R1, result1 );
259         assertEquals( "Targets R2 debug output", R2, result2 );
260         assertEquals( "Targets R3 debug output", R3, result3 );
261
262         return logger;
263     }
264
265     private void doStreamTest( final ByteArrayOutputStream JavaDoc output,
266                                final AbstractOutputTarget target )
267     {
268         final Logger logger = doStreamOutputTest( output, target );
269
270         target.close();
271         final String JavaDoc tail = getResult( output );
272         assertEquals( "Targets Tail output", TAIL, tail );
273
274         logger.debug( M1 );
275         final String JavaDoc noresult = getResult( output );
276         //final String errorResult = getErrorResult( errorOutput );
277

278         assertEquals( "Write after close()", "", noresult );
279         //assertEquals( "Epecting error", "", errorResult );
280
}
281 }
282
Popular Tags