LoggerTestCase xref

    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
    178     CountingAppender caRoot = new CountingAppender();
    179     CountingAppender caA = new CountingAppender();
    180     CountingAppender caABC = new CountingAppender();
    181
    182     root.addAppender(caRoot);
    183     a.addAppender(caA);
    184     abc.addAppender(caABC);
    185
    186     assertEquals(caRoot.counter, 0);
    187     assertEquals(caA.counter, 0);
    188     assertEquals(caABC.counter, 0);
    189
    190     ab.setAdditivity(false);
    191
    192
    193     a.debug(MSG);
    194     assertEquals(caRoot.counter, 1);
    195     assertEquals(caA.counter, 1);
    196     assertEquals(caABC.counter, 0);
    197
    198     ab.debug(MSG);
    199     assertEquals(caRoot.counter, 1);
    200     assertEquals(caA.counter, 1);
    201     assertEquals(caABC.counter, 0);
    202
    203     abc.debug(MSG);
    204     assertEquals(caRoot.counter, 1);
    205     assertEquals(caA.counter, 1);
    206     assertEquals(caABC.counter, 1);
    207
    208   }
    209
    210
    211   public
    212   void testDisable1() {
    213     CountingAppender caRoot = new CountingAppender();
    214     Logger root = Logger.getRootLogger();
    215     root.addAppender(caRoot);
    216
    217     LoggerRepository h = LogManager.getLoggerRepository();
    218     //h.disableDebug();
    219     h.setThreshold((Level) Level.INFO);
    220     assertEquals(caRoot.counter, 0);
    221
    222     root.debug(MSG); assertEquals(caRoot.counter, 0);
    223     root.info(MSG); assertEquals(caRoot.counter, 1);
    224     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 2);
    225     root.warn(MSG); assertEquals(caRoot.counter, 3);
    226
    227     //h.disableInfo();
    228     h.setThreshold((Level) Level.WARN);
    229     root.debug(MSG); assertEquals(caRoot.counter, 3);
    230     root.info(MSG); assertEquals(caRoot.counter, 3);
    231     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 4);
    232     root.error(MSG); assertEquals(caRoot.counter, 5);
    233     root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);
    234
    235     //h.disableAll();
    236     h.setThreshold(Level.OFF);
    237     root.debug(MSG); assertEquals(caRoot.counter, 6);
    238     root.info(MSG); assertEquals(caRoot.counter, 6);
    239     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);
    240     root.error(MSG); assertEquals(caRoot.counter, 6);
    241     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    242     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    243
    244     //h.disable(Level.FATAL);
    245     h.setThreshold(Level.OFF);
    246     root.debug(MSG); assertEquals(caRoot.counter, 6);
    247     root.info(MSG); assertEquals(caRoot.counter, 6);
    248     root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);
    249     root.error(MSG); assertEquals(caRoot.counter, 6);
    250     root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);
    251     root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);
    252   }
    253
    254
    255   public
    256   void testRB1() {
    257     Logger root = Logger.getRootLogger();
    258     root.setResourceBundle(rbUS);
    259     ResourceBundle t = root.getResourceBundle();
    260     assertSame(t, rbUS);
    261
    262     Logger x = Logger.getLogger("x");
    263     Logger x_y = Logger.getLogger("x.y");
    264     Logger x_y_z = Logger.getLogger("x.y.z");
    265
    266     t = x.getResourceBundle();     assertSame(t, rbUS);
    267     t = x_y.getResourceBundle();   assertSame(t, rbUS);
    268     t = x_y_z.getResourceBundle(); assertSame(t, rbUS);
    269   }
    270
    271   public
    272   void testRB2() {
    273     Logger root = Logger.getRootLogger();
    274     root.setResourceBundle(rbUS);
    275     ResourceBundle t = root.getResourceBundle();
    276     assertSame(t, rbUS);
    277
    278     Logger x = Logger.getLogger("x");
    279     Logger x_y = Logger.getLogger("x.y");
    280     Logger x_y_z = Logger.getLogger("x.y.z");
    281
    282     x_y.setResourceBundle(rbFR);
    283     t = x.getResourceBundle();     assertSame(t, rbUS);
    284     t = x_y.getResourceBundle();   assertSame(t, rbFR);
    285     t = x_y_z.getResourceBundle(); assertSame(t, rbFR);
    286   }
    287
    288
    289   public
    290   void testRB3() {
    291     Logger root = Logger.getRootLogger();
    292     root.setResourceBundle(rbUS);
    293     ResourceBundle t = root.getResourceBundle();
    294     assertSame(t, rbUS);
    295
    296     Logger x = Logger.getLogger("x");
    297     Logger x_y = Logger.getLogger("x.y");
    298     Logger x_y_z = Logger.getLogger("x.y.z");
    299
    300     x_y.setResourceBundle(rbFR);
    301     x_y_z.setResourceBundle(rbCH);
    302     t = x.getResourceBundle();     assertSame(t, rbUS);
    303     t = x_y.getResourceBundle();   assertSame(t, rbFR);
    304     t = x_y_z.getResourceBundle(); assertSame(t, rbCH);
    305   }
    306
    307   public
    308   void testExists() {
    309     Logger a = Logger.getLogger("a");
    310     Logger a_b = Logger.getLogger("a.b");
    311     Logger a_b_c = Logger.getLogger("a.b.c");
    312
    313     Logger t;
    314     t = LogManager.exists("xx");    assertNull(t);
    315     t = LogManager.exists("a");     assertSame(a, t);
    316     t = LogManager.exists("a.b");   assertSame(a_b, t);
    317     t = LogManager.exists("a.b.c"); assertSame(a_b_c, t);
    318   }
    319
    320   public
    321   void testHierarchy1() {
    322     Hierarchy h = new Hierarchy(new RootLogger((Level) Level.ERROR));
    323     Logger a0 = h.getLogger("a");
    324     assertEquals("a", a0.getName());
    325     assertNull(a0.getLevel());
    326     assertSame(Level.ERROR, a0.getEffectiveLevel());
    327
    328     Logger a1 = h.getLogger("a");
    329     assertSame(a0, a1);
    330   }
    331
    332   /**
    333    * Tests logger.trace(Object).
    334    * @since 1.2.12
    335    */
    336   public void testTrace() {
    337       VectorAppender appender = new VectorAppender();
    338       appender.activateOptions();
    339       Logger root = Logger.getRootLogger();
    340       root.addAppender(appender);
    341       root.setLevel(Level.INFO);
    342
    343       Logger tracer = Logger.getLogger("com.example.Tracer");
    344       tracer.setLevel(Level.TRACE);
    345
    346       tracer.trace("Message 1");
    347       root.trace("Discarded Message");
    348       root.trace("Discarded Message");
    349
    350       Vector msgs = appender.getVector();
    351       assertEquals(1, msgs.size());
    352       LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
    353       assertEquals(Level.TRACE, event.getLevel());
    354       assertEquals("Message 1", event.getMessage());
    355   }
    356
    357     /**
    358      * Tests logger.trace(Object, Exception).
    359      * @since 1.2.12
    360      */
    361     public void testTraceWithException() {
    362         VectorAppender appender = new VectorAppender();
    363         appender.activateOptions();
    364         Logger root = Logger.getRootLogger();
    365         root.addAppender(appender);
    366         root.setLevel(Level.INFO);
    367
    368         Logger tracer = Logger.getLogger("com.example.Tracer");
    369         tracer.setLevel(Level.TRACE);
    370         NullPointerException ex = new NullPointerException();
    371
    372         tracer.trace("Message 1", ex);
    373         root.trace("Discarded Message", ex);
    374         root.trace("Discarded Message", ex);
    375
    376         Vector msgs = appender.getVector();
    377         assertEquals(1, msgs.size());
    378         LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
    379         assertEquals(Level.TRACE, event.getLevel());
    380         assertEquals("Message 1", event.getMessage());
    381     }
    382
    383     /**
    384      * Tests isTraceEnabled.
    385      * @since 1.2.12
    386      */
    387     public void testIsTraceEnabled() {
    388         VectorAppender appender = new VectorAppender();
    389         appender.activateOptions();
    390         Logger root = Logger.getRootLogger();
    391         root.addAppender(appender);
    392         root.setLevel(Level.INFO);
    393
    394         Logger tracer = Logger.getLogger("com.example.Tracer");
    395         tracer.setLevel(Level.TRACE);
    396
    397         assertTrue(tracer.isTraceEnabled());
    398         assertFalse(root.isTraceEnabled());
    399     }
    400
    401   private static final class CountingHierarchyEventListener implements HierarchyEventListener {
    402       private int addEventCount;
    403       private int removeEventCount;
    404
    405       public CountingHierarchyEventListener() {
    406           addEventCount = removeEventCount = 0;
    407       }
    408       public void addAppenderEvent(Category cat, Appender appender) {
    409           addEventCount++;
    410       }
    411
    412       public void removeAppenderEvent(Category cat, Appender appender) {
    413           removeEventCount++;
    414       }
    415
    416       public int getAddEventCount() {
    417           return addEventCount;
    418       }
    419       public int getRemoveEventCount() {
    420           return removeEventCount;
    421       }
    422   }
    423
    424
    425   public void testAppenderEvent1() {
    426       CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    427       LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    428       CountingAppender appender = new CountingAppender();
    429       Logger root =  Logger.getRootLogger();
    430       root.addAppender(appender);
    431       assertEquals(1, listener.getAddEventCount());
    432       assertEquals(0, listener.getRemoveEventCount());
    433       root.removeAppender(appender);
    434       assertEquals(1, listener.getAddEventCount());
    435       assertEquals(1, listener.getRemoveEventCount());
    436   }
    437
    438   public void testAppenderEvent2() {
    439         CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    440         LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    441         CountingAppender appender = new CountingAppender();
    442         appender.setName("A1");
    443         Logger root =  Logger.getRootLogger();
    444         root.addAppender(appender);
    445         assertEquals(1, listener.getAddEventCount());
    446         assertEquals(0, listener.getRemoveEventCount());
    447         root.removeAppender(appender.getName());
    448         assertEquals(1, listener.getAddEventCount());
    449         assertEquals(1, listener.getRemoveEventCount());
    450   }
    451
    452   public void testAppenderEvent3() {
    453           CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    454           LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    455           CountingAppender appender = new CountingAppender();
    456           Logger root =  Logger.getRootLogger();
    457           root.addAppender(appender);
    458           assertEquals(1, listener.getAddEventCount());
    459           assertEquals(0, listener.getRemoveEventCount());
    460           root.removeAllAppenders();
    461           assertEquals(1, listener.getAddEventCount());
    462           assertEquals(1, listener.getRemoveEventCount());
    463     }
    464
    465
    466     public void testAppenderEvent4() {
    467             CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
    468             LogManager.getLoggerRepository().addHierarchyEventListener(listener);
    469             CountingAppender appender = new CountingAppender();
    470             Logger root =  Logger.getRootLogger();
    471             root.addAppender(appender);
    472             assertEquals(1, listener.getAddEventCount());
    473             assertEquals(0, listener.getRemoveEventCount());
    474             LogManager.resetConfiguration();
    475             assertEquals(1, listener.getAddEventCount());
    476             assertEquals(1, listener.getRemoveEventCount());
    477       }
    478
    479   static private class CountingAppender extends AppenderSkeleton {
    480
    481     int counter;
    482
    483     CountingAppender() {
    484       counter = 0;
    485     }
    486     public void close() {
    487     }
    488
    489     public
    490     void append(LoggingEvent event) {
    491       counter++;
    492     }
    493
    494     public
    495     boolean requiresLayout() {
    496       return true;
    497     }
    498   }
    499 }