KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > test > LoggerListenerTestCase


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.test;
18
19 import junit.framework.TestCase;
20 import org.apache.log.Hierarchy;
21 import org.apache.log.Logger;
22
23 /**
24  * Test suite for logger listener features of Logger.
25  *
26  * @author Peter Donald
27  */

28 public final class LoggerListenerTestCase
29     extends TestCase
30 {
31     public LoggerListenerTestCase( final String JavaDoc name )
32     {
33         super( name );
34     }
35
36     public void testUnicastLoggerListener()
37     {
38         final Hierarchy hierarchy = new Hierarchy();
39         final RecordingLoggerListener listener = new RecordingLoggerListener();
40
41         try
42         {
43             hierarchy.addLoggerListener( listener );
44             hierarchy.addLoggerListener( listener );
45
46             fail( "You should only be able to add one listener." );
47         }
48         catch (UnsupportedOperationException JavaDoc uoe)
49         {
50             // It passed, yay!
51
}
52     }
53
54     public void testRemoveLoggerListener()
55     {
56         final Hierarchy hierarchy = new Hierarchy();
57         final RecordingLoggerListener listener = new RecordingLoggerListener();
58
59         hierarchy.addLoggerListener( listener );
60         hierarchy.removeLoggerListener( listener );
61         hierarchy.addLoggerListener( listener );
62
63         // If no exceptions have been thrown, we are in business!
64
}
65
66     public void testPriorityInheritance()
67         throws Exception JavaDoc
68     {
69         final RecordingLoggerListener listener = new RecordingLoggerListener();
70         final Hierarchy hierarchy = new Hierarchy();
71         hierarchy.addLoggerListener( listener );
72
73         final Logger root = hierarchy.getRootLogger();
74         final Logger l1 = root.getChildLogger( "logger1" );
75         final Logger l2 = root.getChildLogger( "logger2" );
76         final Logger l3 = root.getChildLogger( "logger1.logger3" );
77         final Logger l4 = root.getChildLogger( "logger5.logger4" );
78         final Logger l5 = root.getChildLogger( "logger5" );
79
80         final Logger[] loggers = listener.getLoggers();
81         assertEquals( "Logger Count", 5, loggers.length );
82         assertEquals( "Logger[0]", l1, loggers[ 0 ] );
83         assertEquals( "Logger[1]", l2, loggers[ 1 ] );
84         assertEquals( "Logger[2]", l3, loggers[ 2 ] );
85         assertEquals( "Logger[3]", l5, loggers[ 3 ] );
86         assertEquals( "Logger[4]", l4, loggers[ 4 ] );
87     }
88 }
89
Popular Tags