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 junit.framework.TestCase;
20  
21  /**
22   *
23   * @author peter at realityforge.org Peter Donald
24   */
25  public class LogEnabledTest extends TestCase {
26      public void testGetLogger() throws Exception {
27          MockLogEnabled logEnabled = new MockLogEnabled();
28          MockLogger logger = new MockLogger("base");
29          logEnabled.enableLogging(logger);
30          assertEquals("logger", logger, logEnabled.getLogger());
31      }
32  
33      public void testSetupLoggerOnLogEnabled() throws Exception {
34          MockLogEnabled logEnabled = new MockLogEnabled();
35          MockLogEnabled childLogEnabled = new MockLogEnabled();
36          MockLogger logger = new MockLogger("base");
37          logEnabled.enableLogging(logger);
38          logEnabled.setupLogger(childLogEnabled);
39          assertEquals("logEnabled.logger", logger, logEnabled.getLogger());
40          assertEquals("childLogEnabled.logger", logger, childLogEnabled.getLogger());
41      }
42  
43      public void testSetupLoggerOnNonLogEnabled() throws Exception {
44          MockLogEnabled logEnabled = new MockLogEnabled();
45          MockLogger logger = new MockLogger("base");
46          logEnabled.enableLogging(logger);
47          logEnabled.setupLogger(new Object());
48      }
49  
50      public void testSetupLoggerWithNameOnLogEnabled() throws Exception {
51          MockLogEnabled logEnabled = new MockLogEnabled();
52          MockLogEnabled childLogEnabled = new MockLogEnabled();
53          MockLogger logger = new MockLogger("base");
54          logEnabled.enableLogging(logger);
55          logEnabled.setupLogger(childLogEnabled, "child");
56          assertEquals("logEnabled.logger", logger, logEnabled.getLogger());
57          assertEquals("childLogEnabled.logger.name", "base.child", ((MockLogger) childLogEnabled.getLogger()).getName());
58      }
59  
60      public void testSetupLoggerWithNullName() throws Exception {
61          MockLogEnabled logEnabled = new MockLogEnabled();
62          MockLogEnabled childLogEnabled = new MockLogEnabled();
63          MockLogger logger = new MockLogger("base");
64          logEnabled.enableLogging(logger);
65          try {
66              logEnabled.setupLogger(childLogEnabled, (String) null);
67          } catch (IllegalStateException npe) {
68              return;
69          }
70          fail("Expected to fail setting up child logger with null name");
71      }
72  }