KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > util > test > UtilTestCase


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.util.test;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.PrintStream JavaDoc;
21 import junit.framework.TestCase;
22 import org.apache.log.Hierarchy;
23 import org.apache.log.Logger;
24 import org.apache.log.Priority;
25 import org.apache.log.format.RawFormatter;
26 import org.apache.log.output.io.StreamTarget;
27 import org.apache.log.util.LoggerOutputStream;
28
29 /**
30  * Test suite for utility features of Logger.
31  *
32  * @author Peter Donald
33  */

34 public final class UtilTestCase
35     extends TestCase
36 {
37     private static final String JavaDoc EOL = System.getProperty( "line.separator", "\n" );
38     private static final RawFormatter FORMATTER = new RawFormatter();
39
40     private static final String JavaDoc MSG = "No soup for you!";
41     private static final String JavaDoc RMSG = MSG;
42
43     public UtilTestCase( final String JavaDoc name )
44     {
45         super( name );
46     }
47
48     private String JavaDoc getResult( final ByteArrayOutputStream JavaDoc output )
49     {
50         final String JavaDoc result = output.toString();
51         output.reset();
52         return result;
53     }
54
55     public void testStackIntrospector()
56         throws Exception JavaDoc
57     {
58         /*
59         final String METHOD_RESULT = UtilTestCase.class.getName() + ".";
60         final ByteArrayOutputStream output = new ByteArrayOutputStream();
61         final StreamTarget target = new StreamTarget( output, METHOD_FORMATTER );
62         final Hierarchy hierarchy = new Hierarchy();
63         hierarchy.setDefaultLogTarget( target );
64
65         final Logger logger = hierarchy.getLoggerFor( "myLogger" );
66
67         logger.debug( MSG );
68         final String result = getResult( output );
69         final String expected = METHOD_RESULT + "testStackIntrospector()";
70         assert( "StackIntrospector", result.startsWith( expected ) );
71         //result of StackIntrospector.getCallerMethod( Logger.class );
72         */

73     }
74
75     public void testLoggerOutputStream()
76         throws Exception JavaDoc
77     {
78         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
79         final StreamTarget target = new StreamTarget( output, FORMATTER );
80
81         final Hierarchy hierarchy = new Hierarchy();
82         hierarchy.setDefaultLogTarget( target );
83
84         final Logger logger = hierarchy.getLoggerFor( "myLogger" );
85         final LoggerOutputStream outputStream = new LoggerOutputStream( logger, Priority.DEBUG );
86         final PrintStream JavaDoc printer = new PrintStream JavaDoc( outputStream, true );
87
88         printer.println( MSG );
89         assertEquals( "LoggerOutputStream", RMSG + EOL, getResult( output ) );
90
91         //unbuffered output
92
printer.print( MSG );
93         printer.flush();
94         assertEquals( "LoggerOutputStream", RMSG, getResult( output ) );
95
96         printer.close();
97     }
98 }
99
Popular Tags