View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j;
    19
    20  import junit.framework.TestCase;
    21  import org.apache.log4j.spi.LoggingEvent;
    22  import org.apache.log4j.spi.RootLogger;
    23  import org.apache.log4j.spi.LoggerRepository;
    24  import org.apache.log4j.spi.HierarchyEventListener;
    25
    26  import java.util.Enumeration;
    27  import java.util.Locale;
    28  import java.util.ResourceBundle;
    29  import java.util.Vector;
    30
    31  /***
    32     Used for internal unit testing the Logger class.
    33
    34     @author Ceki G&uuml;lc&uuml;
    35
    36  */
    37  public class LoggerTestCase extends TestCase {
    38
    39    Logger logger;
    40    Appender a1;
    41    Appender a2;
    42
    43    ResourceBundle rbUS;
    44    ResourceBundle rbFR;
    45    ResourceBundle rbCH;
    46
    47    // A short message.
    48    static String MSG = "M";
    49
    50
    51    public LoggerTestCase(String name) {
    52      super(name);
    53    }
    54
    55    public
    56    void setUp() {
    57      rbUS = ResourceBundle.getBundle("L7D", new Locale("en", "US"));
    58      assertNotNull(rbUS);
    59
    60      rbFR = ResourceBundle.getBundle("L7D", new Locale("fr", "FR"));
    61      assertNotNull("Got a null resource bundle.", rbFR);
    62
    63      rbCH = ResourceBundle.getBundle("L7D", new Locale("fr", "CH"));
    64      assertNotNull("Got a null resource bundle.", rbCH);
    65
    66    }
    67
    68    public
    69    void tearDown() {
    70      // Regular users should not use the clear method lightly!
    71      //Logger.getDefaultHierarchy().clear();
    72      BasicConfigurator.resetConfiguration();
    73      a1 = null;
    74      a2 = null;
    75    }
    76
    77    /***
    78       Add an appender and see if it can be retrieved.
    79    */
    80    public
    81    void testAppender1() {
    82      logger = Logger.getLogger("test");
    83      a1 = new FileAppender();
    84      a1.setName("testAppender1");
    85      logger.addAppender(a1);
    86
    87      Enumeration enumeration = logger.getAllAppenders();
    88      Appender aHat = (Appender) enumeration.nextElement();
    89      assertEquals(a1, aHat);
    90    }
    91
    92    /***
    93       Add an appender X, Y, remove X and check if Y is the only
    94       remaining appender.
    95    */
    96    public
    97    void testAppender2() {
    98      a1 = new FileAppender();
    99      a1.setName("testAppender2.1");
    100     a2 = new FileAppender();
    101     a2.setName("testAppender2.2");
    102
    103     logger = Logger.getLogger("test");
    104     logger.addAppender(a1);
    105     logger.addAppender(a2);
    106     logger.removeAppender("testAppender2.1");
    107     Enumeration enumeration = logger.getAllAppenders();
    108     Appender aHat = (Appender) enumeration.nextElement();
    109     assertEquals(a2, aHat);
    110     assertTrue(!enumeration.hasMoreElements());
    111   }
    112
    113   /***
    114      Test if logger a.b inherits its appender from a.
    115    */
    116   public
    117   void testAdditivity1() {
    118     Logger a = Logger.getLogger("a");
    119     Logger ab = Logger.getLogger("a.b");
    120     CountingAppender ca = new CountingAppender();
    121     a.addAppender(ca);
    122
    123                    assertEquals(ca.counter, 0);
    124     ab.debug(MSG); assertEquals(ca.counter, 1);
    125     ab.info(MSG);  assertEquals(ca.counter, 2);
    126     ab.warn(MSG);  assertEquals(ca.counter, 3);
    127     ab.error(MSG); assertEquals(ca.counter, 4);
    128
    129
    130   }
    131
    132   /***
    133      Test multiple additivity.
    134
    135    */
    136   public
    137   void testAdditivity2() {
    138
    139     Logger a = Logger.getLogger("a");
    140     Logger ab = Logger.getLogger("a.b");
    141     Logger abc = Logger.getLogger("a.b.c");
    142     Logger x   = Logger.getLogger("x");
    143
    144     CountingAppender ca1 = new CountingAppender();
    145     CountingAppender ca2 = new CountingAppender();
    146
    147     a.addAppender(ca1);
    148     abc.addAppender(ca2);
    149
    150     assertEquals(ca1.counter, 0);
    151     assertEquals(ca2.counter, 0);
    152
    153     ab.debug(MSG);
    154     assertEquals(ca1.counter, 1);
    155     assertEquals(ca2.counter, 0);
    156
    157     abc.debug(MSG);
    158     assertEquals(ca1.counter, 2);
    159     assertEquals(ca2.counter, 1);
    160
    161     x.debug(MSG);
    162     assertEquals(ca1.counter, 2);
    163     assertEquals(ca2.counter, 1);
    164   }
    165
    166   /***
    167      Test additivity flag.
    168
    169    */
    170   public
    171   void testAdditivity3() {
    172
    173     Logger root = Logger.getRootLogger();
    174     Logger a = Logger.getLogger("a");
    175     Logger ab = Logger.getLogger("a.b");
    176     Logger abc = Logger.getLogger("a.b.c");
    177     Logger x   = Logger.getLogger("x");
    178
    179     CountingAppender caRoot = new CountingAppender();
    180     CountingAppender caA = new CountingAppender();
    181     CountingAppender caABC = new CountingAppender();
    182
    183     root.addAppender(caRoot);
    184     a.addAppender(caA);
    185     abc.addAppender(caABC);
    186
    187     assertEquals(caRoot.counter, 0);
    188     assertEquals(caA.counter, 0);
    189     assertEquals(caABC.counter, 0);
    190
    191     ab.setAdditivity(false);
    192
    193
    194     a.debug(MSG);
    195     assertEquals(caRoot.counter, 1);
    196     assertEquals(caA.counter, 1);
    197     assertEquals(caABC.counter, 0);
    198
    199     ab.debug(MSG);
    200     assertEquals(caRoot.counter, 1);
    201     assertEquals(caA.counter, 1);
    202     assertEquals(caABC.counter, 0);
    203
    204     abc.debug(MSG);
    205     assertEquals(caRoot.counter, 1);
    206     assertEquals(caA.counter, 1);
    207     assertEquals(caABC.counter, 1);
    208
    209   }
    210
    211
    212   public
    213   void testDisable1() {
    214     CountingAppender caRoot = new CountingAppender();
    215     Logger root = Logger.getRootLogger();
    216     root.addAppender(caRoot);
    217
    218     LoggerRepository h = LogManager.getLoggerRepository();
    219     //h.disableDebug();
    220     h.setThreshold((Level) Level.INFO);
    221     assertEquals(caRoot.counter, 0);
    222
    223     root.debug(MSG); assertEquals(caRoot.counter, 0);
    224     root.info(MSG); assertEquals(caRoot.counter, 1);
    225     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 2);
    226     root.warn(MSG); assertEquals(caRoot.counter, 3);
    227
    228     //h.disableInfo();
    229     h.setThreshold((Level) Level.WARN);
    230     root.debug(MSG); assertEquals(caRoot.counter, 3);
    231     root.info(MSG); assertEquals(caRoot.counter, 3);
    232     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 4);
    233     root.error(MSG); assertEquals(caRoot.counter, 5);
    234     root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);
    235
    236     //h.disableAll();
    237     h.setThreshold(Level.OFF);
    238     root.debug(MSG); assertEquals(caRoot.counter, 6);
    239     root.info(MSG); assertEquals(caRoot.counter, 6);
    240     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);
    241     root.error(MSG); assertEquals(caRoot.counter, 6);
    242     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    243     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    244
    245     //h.disable(Level.FATAL);
    246     h.setThreshold(Level.OFF);
    247     root.debug(MSG); assertEquals(caRoot.counter, 6);
    248     root.info(MSG); assertEquals(caRoot.counter, 6);
    249     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);
    250     root.error(MSG); assertEquals(caRoot.counter, 6);
    251     root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);
    252     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    253   }
    254
    255
    256   public
    257   void testRB1() {
    258     Logger root = Logger.getRootLogger();
    259     root.setResourceBundle(rbUS);
    260     ResourceBundle t = root.getResourceBundle();
    261     assertSame(t, rbUS);
    262
    263     Logger x = Logger.getLogger("x");
    264     Logger x_y = Logger.getLogger("x.y");
    265     Logger x_y_z = Logger.getLogger("x.y.z");
    266
    267     t = x.getResourceBundle();     assertSame(t, rbUS);
    268     t = x_y.getResourceBundle();   assertSame(t, rbUS);
    269     t = x_y_z.getResourceBundle(); assertSame(t, rbUS);
    270   }
    271
    272   public
    273   void testRB2() {
    274     Logger root = Logger.getRootLogger();
    275     root.setResourceBundle(rbUS);
    276     ResourceBundle t = root.getResourceBundle();
    277     assertSame(t, rbUS);
    278
    279     Logger x = Logger.getLogger("x");
    280     Logger x_y = Logger.getLogger("x.y");
    281     Logger x_y_z = Logger.getLogger("x.y.z");
    282
    283     x_y.setResourceBundle(rbFR);
    284     t = x.getResourceBundle();     assertSame(t, rbUS);
    285     t = x_y.getResourceBundle();   assertSame(t, rbFR);
    286     t = x_y_z.getResourceBundle(); assertSame(t, rbFR);
    287   }
    288
    289
    290   public
    291   void testRB3() {
    292     Logger root = Logger.getRootLogger();
    293     root.setResourceBundle(rbUS);
    294     ResourceBundle t = root.getResourceBundle();
    295     assertSame(t, rbUS);
    296
    297     Logger x = Logger.getLogger("x");
    298     Logger x_y = Logger.getLogger("x.y");
    299     Logger x_y_z = Logger.getLogger("x.y.z");
    300
    301     x_y.setResourceBundle(rbFR);
    302     x_y_z.setResourceBundle(rbCH);
    303     t = x.getResourceBundle();     assertSame(t, rbUS);
    304     t = x_y.getResourceBundle();   assertSame(t, rbFR);
    305     t = x_y_z.getResourceBundle(); assertSame(t, rbCH);
    306   }
    307
    308   public
    309   void testExists() {
    310     Logger a = Logger.getLogger("a");
    311     Logger a_b = Logger.getLogger("a.b");
    312     Logger a_b_c = Logger.getLogger("a.b.c");
    313
    314     Logger t;
    315     t = LogManager.exists("xx");    assertNull(t);
    316     t = LogManager.exists("a");     assertSame(a, t);
    317     t = LogManager.exists("a.b");   assertSame(a_b, t);
    318     t = LogManager.exists("a.b.c"); assertSame(a_b_c, t);
    319   }
    320
    321   public
    322   void testHierarchy1() {
    323     Hierarchy h = new Hierarchy(new RootLogger((Level) Level.ERROR));
    324     Logger a0 = h.getLogger("a");
    325     assertEquals("a", a0.getName());
    326     assertNull(a0.getLevel());
    327     assertSame(Level.ERROR, a0.getEffectiveLevel());
    328
    329     Logger a1 = h.getLogger("a");
    330     assertSame(a0, a1);
    331   }
    332
    333   /***
    334    * Tests logger.trace(Object).
    335    * @since 1.2.12
    336    */
    337   public void testTrace() {
    338       VectorAppender appender = new VectorAppender();
    339       appender.activateOptions();
    340       Logger root = Logger.getRootLogger();
    341       root.addAppender(appender);
    342       root.setLevel(Level.INFO);
    343
    344       Logger tracer = Logger.getLogger("com.example.Tracer");
    345       tracer.setLevel(Level.TRACE);
    346
    347       tracer.trace("Message 1");
    348       root.trace("Discarded Message");
    349       root.trace("Discarded Message");
    350
    351       Vector msgs = appender.getVector();
    352       assertEquals(1, msgs.size());
    353       LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
    354       assertEquals(Level.TRACE, event.getLevel());
    355       assertEquals("Message 1", event.getMessage());
    356   }
    357
    358     /***
    359      * Tests logger.trace(Object, Exception).
    360      * @since 1.2.12
    361      */
    362     public void testTraceWithException() {
    363         VectorAppender appender = new VectorAppender();
    364         appender.activateOptions();
    365         Logger root = Logger.getRootLogger();
    366         root.addAppender(appender);
    367         root.setLevel(Level.INFO);
    368
    369         Logger tracer = Logger.getLogger("com.example.Tracer");
    370         tracer.setLevel(Level.TRACE);
    371         NullPointerException ex = new NullPointerException();
    372
    373         tracer.trace("Message 1", ex);
    374         root.trace("Discarded Message", ex);
    375         root.trace("Discarded Message", ex);
    376
    377         Vector msgs = appender.getVector();
    378         assertEquals(1, msgs.size());
    379         LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
    380         assertEquals(Level.TRACE, event.getLevel());
    381         assertEquals("Message 1", event.getMessage());
    382     }
    383
    384     /***
    385      * Tests isTraceEnabled.
    386      * @since 1.2.12
    387      */
    388     public void testIsTraceEnabled() {
    389         VectorAppender appender = new VectorAppender();
    390         appender.activateOptions();
    391         Logger root = Logger.getRootLogger();
    392         root.addAppender(appender);
    393         root.setLevel(Level.INFO);
    394
    395         Logger tracer = Logger.getLogger("com.example.Tracer");
    396         tracer.setLevel(Level.TRACE);
    397
    398         assertTrue(tracer.isTraceEnabled());
    399         assertFalse(root.isTraceEnabled());
    400     }
    401
    402   private static final class CountingHierarchyEventListener implements HierarchyEventListener {
    403       private int addEventCount;
    404       private int removeEventCount;
    405
    406       public CountingHierarchyEventListener() {
    407           addEventCount = removeEventCount = 0;
    408       }
    409       public void addAppenderEvent(Category cat, Appender appender) {
    410           addEventCount++;
    411       }
    412
    413       public void removeAppenderEvent(Category cat, Appender appender) {
    414           removeEventCount++;
    415       }
    416
    417       public int getAddEventCount() {
    418           return addEventCount;
    419       }
    420       public int getRemoveEventCount() {
    421           return removeEventCount;
    422       }
    423   }
    424
    425
    426   public void testAppenderEvent1() {
    427       CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    428       LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    429       CountingAppender appender = new CountingAppender();
    430       Logger root =  Logger.getRootLogger();
    431       root.addAppender(appender);
    432       assertEquals(1, listener.getAddEventCount());
    433       assertEquals(0, listener.getRemoveEventCount());
    434       root.removeAppender(appender);
    435       assertEquals(1, listener.getAddEventCount());
    436       assertEquals(1, listener.getRemoveEventCount());
    437   }
    438
    439   public void testAppenderEvent2() {
    440         CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    441         LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    442         CountingAppender appender = new CountingAppender();
    443         appender.setName("A1");
    444         Logger root =  Logger.getRootLogger();
    445         root.addAppender(appender);
    446         assertEquals(1, listener.getAddEventCount());
    447         assertEquals(0, listener.getRemoveEventCount());
    448         root.removeAppender(appender.getName());
    449         assertEquals(1, listener.getAddEventCount());
    450         assertEquals(1, listener.getRemoveEventCount());
    451   }
    452
    453   public void testAppenderEvent3() {
    454           CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    455           LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    456           CountingAppender appender = new CountingAppender();
    457           Logger root =  Logger.getRootLogger();
    458           root.addAppender(appender);
    459           assertEquals(1, listener.getAddEventCount());
    460           assertEquals(0, listener.getRemoveEventCount());
    461           root.removeAllAppenders();
    462           assertEquals(1, listener.getAddEventCount());
    463           assertEquals(1, listener.getRemoveEventCount());
    464     }
    465
    466
    467     public void testAppenderEvent4() {
    468             CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    469             LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    470             CountingAppender appender = new CountingAppender();
    471             Logger root =  Logger.getRootLogger();
    472             root.addAppender(appender);
    473             assertEquals(1, listener.getAddEventCount());
    474             assertEquals(0, listener.getRemoveEventCount());
    475             LogManager.resetConfiguration();
    476             assertEquals(1, listener.getAddEventCount());
    477             assertEquals(1, listener.getRemoveEventCount());
    478       }
    479
    480   static private class CountingAppender extends AppenderSkeleton {
    481
    482     int counter;
    483
    484     CountingAppender() {
    485       counter = 0;
    486     }
    487     public void close() {
    488     }
    489
    490     public
    491     void append(LoggingEvent event) {
    492       counter++;
    493     }
    494
    495     public
    496     boolean requiresLayout() {
    497       return true;
    498     }
    499   }
    500 }