View Javadoc
1   package org.codehaus.plexus.logging;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
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.codehaus.plexus.PlexusTestCase;
20  
21  /**
22   * Abtract base class for testing implementations of the {@link LoggerManager}
23   * and {@link Logger} interfaces.
24   *
25   * @author Mark H. Wilkinson
26   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
27   * @version $Revision$
28   */
29  public abstract class AbstractLoggerManagerTest
30      extends PlexusTestCase
31  {
32      protected abstract LoggerManager createLoggerManager()
33          throws Exception;
34  
35      public void testSetThreshold()
36          throws Exception
37      {
38          LoggerManager manager;
39          Logger logger1, logger2;
40  
41          manager = createLoggerManager();
42  
43          manager.setThreshold( Logger.LEVEL_FATAL );
44          logger1 = manager.getLoggerForComponent( "role1", "roleHint1" );
45          assertEquals( Logger.LEVEL_FATAL, logger1.getThreshold() );
46  
47          manager.setThreshold( Logger.LEVEL_DEBUG );
48          logger2 = manager.getLoggerForComponent( "role2", "roleHint2" );
49          assertEquals( Logger.LEVEL_FATAL, logger1.getThreshold() );
50          assertEquals( Logger.LEVEL_DEBUG, logger2.getThreshold() );
51      }
52  
53      /**
54       * There is only one logger instance pr component even if looked up more that once.
55       */
56      public void testActiveLoggerCount()
57          throws Exception
58      {
59          LoggerManager manager;
60          Logger b, c1_1, c1_2, c2;
61  
62          manager = getManager( Logger.LEVEL_FATAL );
63          assertEquals(0, manager.getActiveLoggerCount());
64  
65          b = manager.getLoggerForComponent( "b" );
66          assertNotNull( b );
67          assertEquals(1, manager.getActiveLoggerCount());
68  
69          c1_1 = manager.getLoggerForComponent( "c", "1" );
70          c1_2 = manager.getLoggerForComponent( "c", "1" );
71          assertNotNull( c1_1 );
72          assertNotNull( c1_2 );
73          assertSame( c1_1, c1_2 );
74          assertEquals(2, manager.getActiveLoggerCount());
75  
76          c2 = manager.getLoggerForComponent( "c", "2" );
77          assertNotNull( c2 );
78          assertEquals(3, manager.getActiveLoggerCount());
79  
80          manager.returnComponentLogger( "c", "1" );
81          assertEquals(2, manager.getActiveLoggerCount());
82  
83          manager.returnComponentLogger( "c", "2" );
84          manager.returnComponentLogger( "c", "2" );
85          manager.returnComponentLogger( "c", "1" );
86          assertEquals(1, manager.getActiveLoggerCount());
87  
88          manager.returnComponentLogger( "b" );
89          assertEquals(0, manager.getActiveLoggerCount());
90      }
91  
92      public void testDebugLevelConfiguration()
93          throws Exception
94      {
95          LoggerManager manager = getManager( Logger.LEVEL_DEBUG );
96  
97          Logger logger = extractLogger( manager );
98  
99          checkDebugLevel( logger );
100 
101         logger = extractLogger( manager );
102 
103         checkDebugLevel( logger );
104     }
105 
106     public void testInfoLevelConfiguration()
107         throws Exception
108     {
109         LoggerManager manager = getManager( Logger.LEVEL_INFO );
110 
111         Logger logger = extractLogger( manager );
112 
113         checkInfoLevel( logger );
114 
115         logger = extractLogger( manager );
116 
117         checkInfoLevel( logger );
118     }
119 
120     public void testWarnLevelConfiguration()
121         throws Exception
122     {
123         LoggerManager manager = getManager( Logger.LEVEL_WARN );
124 
125         Logger logger = extractLogger( manager );
126 
127         checkWarnLevel( logger );
128 
129         logger = extractLogger( manager );
130 
131         checkWarnLevel( logger );
132     }
133 
134     public void testErrorLevelConfiguration()
135         throws Exception
136     {
137         LoggerManager manager = getManager( Logger.LEVEL_ERROR );
138 
139         Logger logger = extractLogger( manager );
140 
141         checkErrorLevel( logger );
142 
143         logger = extractLogger( manager );
144 
145         checkErrorLevel( logger );
146     }
147 
148     public void testFatalLevelConfiguration()
149         throws Exception
150     {
151         LoggerManager manager = getManager( Logger.LEVEL_FATAL );
152 
153         Logger logger = extractLogger( manager );
154 
155         checkFatalLevel( logger );
156 
157         logger = extractLogger( manager );
158 
159         checkFatalLevel( logger );
160     }
161 
162     private LoggerManager getManager( int threshold )
163         throws Exception
164     {
165         LoggerManager manager = createLoggerManager();
166 
167         manager.setThreshold( threshold );
168 
169         assertNotNull( manager );
170 
171         return manager;
172     }
173 /*
174     private Logger extractRootLogger( LoggerManager manager )
175     {
176         Logger logger = manager.getRootLogger();
177 
178         assertNotNull( logger );
179 
180         return logger;
181     }
182 */
183     private Logger extractLogger( LoggerManager manager )
184     {
185         Logger logger = manager.getLoggerForComponent( "foo" );
186 
187         assertNotNull( logger );
188         assertEquals( "foo", logger.getName() );
189 
190         return logger;
191     }
192 
193     private void checkDebugLevel( Logger logger )
194     {
195         assertTrue( "debug enabled", logger.isDebugEnabled() );
196         assertTrue( "info enabled", logger.isInfoEnabled() );
197         assertTrue( "warn enabled", logger.isWarnEnabled() );
198         assertTrue( "error enabled", logger.isErrorEnabled() );
199         assertTrue( "fatal enabled", logger.isFatalErrorEnabled() );
200     }
201 
202     private void checkInfoLevel( Logger logger )
203     {
204         assertFalse( "debug disabled", logger.isDebugEnabled() );
205         assertTrue( "info enabled", logger.isInfoEnabled() );
206         assertTrue( "warn enabled", logger.isWarnEnabled() );
207         assertTrue( "error enabled", logger.isErrorEnabled() );
208         assertTrue( "fatal enabled", logger.isFatalErrorEnabled() );
209     }
210 
211     private void checkWarnLevel( Logger logger )
212     {
213         assertFalse( "debug disabled", logger.isDebugEnabled() );
214         assertFalse( "info disabled", logger.isInfoEnabled() );
215         assertTrue( "warn enabled", logger.isWarnEnabled() );
216         assertTrue( "error enabled", logger.isErrorEnabled() );
217         assertTrue( "fatal enabled", logger.isFatalErrorEnabled() );
218     }
219 
220     private void checkErrorLevel( Logger logger )
221     {
222         assertFalse( "debug disabled", logger.isDebugEnabled() );
223         assertFalse( "info disabled", logger.isInfoEnabled() );
224         assertFalse( "warn disabled", logger.isWarnEnabled() );
225         assertTrue( "error enabled", logger.isErrorEnabled() );
226         assertTrue( "fatal enabled", logger.isFatalErrorEnabled() );
227     }
228 
229     private void checkFatalLevel( Logger logger )
230     {
231         assertFalse( "debug disabled", logger.isDebugEnabled() );
232         assertFalse( "info disabled", logger.isInfoEnabled() );
233         assertFalse( "warn disabled", logger.isWarnEnabled() );
234         assertFalse( "error disabled", logger.isErrorEnabled() );
235         assertTrue( "fatal enabled", logger.isFatalErrorEnabled() );
236     }
237 }