FileDocCategorySizeDatePackage
LoggerTestCase.javaAPI DocApache log4j 1.2.1515273Sat Aug 25 00:09:34 BST 2007org.apache.log4j

LoggerTestCase

public class LoggerTestCase extends TestCase
Used for internal unit testing the Logger class.
author
Ceki Gülcü

Fields Summary
Logger
logger
Appender
a1
Appender
a2
ResourceBundle
rbUS
ResourceBundle
rbFR
ResourceBundle
rbCH
static String
MSG
Constructors Summary
public LoggerTestCase(String name)

  

     
    super(name);
  
Methods Summary
public voidsetUp()

    rbUS = ResourceBundle.getBundle("L7D", new Locale("en", "US"));
    assertNotNull(rbUS);

    rbFR = ResourceBundle.getBundle("L7D", new Locale("fr", "FR"));
    assertNotNull("Got a null resource bundle.", rbFR);

    rbCH = ResourceBundle.getBundle("L7D", new Locale("fr", "CH"));
    assertNotNull("Got a null resource bundle.", rbCH);

  
public voidtearDown()

    // Regular users should not use the clear method lightly!
    //Logger.getDefaultHierarchy().clear();
    BasicConfigurator.resetConfiguration();
    a1 = null;
    a2 = null;
  
public voidtestAdditivity1()
Test if logger a.b inherits its appender from a.

    Logger a = Logger.getLogger("a");
    Logger ab = Logger.getLogger("a.b");
    CountingAppender ca = new CountingAppender();
    a.addAppender(ca);
    
                   assertEquals(ca.counter, 0);
    ab.debug(MSG); assertEquals(ca.counter, 1);
    ab.info(MSG);  assertEquals(ca.counter, 2);
    ab.warn(MSG);  assertEquals(ca.counter, 3);
    ab.error(MSG); assertEquals(ca.counter, 4);    
    

  
public voidtestAdditivity2()
Test multiple additivity.

    
    Logger a = Logger.getLogger("a");
    Logger ab = Logger.getLogger("a.b");
    Logger abc = Logger.getLogger("a.b.c");
    Logger x   = Logger.getLogger("x");

    CountingAppender ca1 = new CountingAppender();
    CountingAppender ca2 = new CountingAppender();

    a.addAppender(ca1);
    abc.addAppender(ca2);

    assertEquals(ca1.counter, 0); 
    assertEquals(ca2.counter, 0);        
    
    ab.debug(MSG);  
    assertEquals(ca1.counter, 1); 
    assertEquals(ca2.counter, 0);        

    abc.debug(MSG);
    assertEquals(ca1.counter, 2); 
    assertEquals(ca2.counter, 1);        

    x.debug(MSG);
    assertEquals(ca1.counter, 2); 
    assertEquals(ca2.counter, 1);    
  
public voidtestAdditivity3()
Test additivity flag.


    Logger root = Logger.getRootLogger();    
    Logger a = Logger.getLogger("a");
    Logger ab = Logger.getLogger("a.b");
    Logger abc = Logger.getLogger("a.b.c");
    Logger x   = Logger.getLogger("x");

    CountingAppender caRoot = new CountingAppender();
    CountingAppender caA = new CountingAppender();
    CountingAppender caABC = new CountingAppender();

    root.addAppender(caRoot);
    a.addAppender(caA);
    abc.addAppender(caABC);

    assertEquals(caRoot.counter, 0); 
    assertEquals(caA.counter, 0); 
    assertEquals(caABC.counter, 0);        
    
    ab.setAdditivity(false);


    a.debug(MSG);  
    assertEquals(caRoot.counter, 1); 
    assertEquals(caA.counter, 1); 
    assertEquals(caABC.counter, 0);        

    ab.debug(MSG);  
    assertEquals(caRoot.counter, 1); 
    assertEquals(caA.counter, 1); 
    assertEquals(caABC.counter, 0);        

    abc.debug(MSG);  
    assertEquals(caRoot.counter, 1); 
    assertEquals(caA.counter, 1); 
    assertEquals(caABC.counter, 1);        
    
  
public voidtestAppender1()
Add an appender and see if it can be retrieved.

    logger = Logger.getLogger("test");
    a1 = new FileAppender();
    a1.setName("testAppender1");             
    logger.addAppender(a1);

    Enumeration enumeration = logger.getAllAppenders();
    Appender aHat = (Appender) enumeration.nextElement();    
    assertEquals(a1, aHat);    
  
public voidtestAppender2()
Add an appender X, Y, remove X and check if Y is the only remaining appender.

    a1 = new FileAppender();
    a1.setName("testAppender2.1");           
    a2 = new FileAppender();
    a2.setName("testAppender2.2");           

    logger = Logger.getLogger("test");
    logger.addAppender(a1);
    logger.addAppender(a2);    
    logger.removeAppender("testAppender2.1");
    Enumeration enumeration = logger.getAllAppenders();
    Appender aHat = (Appender) enumeration.nextElement();    
    assertEquals(a2, aHat);
    assertTrue(!enumeration.hasMoreElements());
  
public voidtestAppenderEvent1()

      CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
      LogManager.getLoggerRepository().addHierarchyEventListener(listener);
      CountingAppender appender = new CountingAppender();
      Logger root =  Logger.getRootLogger();
      root.addAppender(appender);
      assertEquals(1, listener.getAddEventCount());
      assertEquals(0, listener.getRemoveEventCount());
      root.removeAppender(appender);
      assertEquals(1, listener.getAddEventCount());
      assertEquals(1, listener.getRemoveEventCount());
  
public voidtestAppenderEvent2()

        CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
        LogManager.getLoggerRepository().addHierarchyEventListener(listener);
        CountingAppender appender = new CountingAppender();
        appender.setName("A1");
        Logger root =  Logger.getRootLogger();
        root.addAppender(appender);
        assertEquals(1, listener.getAddEventCount());
        assertEquals(0, listener.getRemoveEventCount());
        root.removeAppender(appender.getName());
        assertEquals(1, listener.getAddEventCount());
        assertEquals(1, listener.getRemoveEventCount());
  
public voidtestAppenderEvent3()

          CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
          LogManager.getLoggerRepository().addHierarchyEventListener(listener);
          CountingAppender appender = new CountingAppender();
          Logger root =  Logger.getRootLogger();
          root.addAppender(appender);
          assertEquals(1, listener.getAddEventCount());
          assertEquals(0, listener.getRemoveEventCount());
          root.removeAllAppenders();
          assertEquals(1, listener.getAddEventCount());
          assertEquals(1, listener.getRemoveEventCount());
    
public voidtestAppenderEvent4()

            CountingHierarchyEventListener listener = new CountingHierarchyEventListener();
            LogManager.getLoggerRepository().addHierarchyEventListener(listener);
            CountingAppender appender = new CountingAppender();
            Logger root =  Logger.getRootLogger();
            root.addAppender(appender);
            assertEquals(1, listener.getAddEventCount());
            assertEquals(0, listener.getRemoveEventCount());
            LogManager.resetConfiguration();
            assertEquals(1, listener.getAddEventCount());
            assertEquals(1, listener.getRemoveEventCount());
      
public voidtestDisable1()

    CountingAppender caRoot = new CountingAppender();
    Logger root = Logger.getRootLogger();    
    root.addAppender(caRoot);

    LoggerRepository h = LogManager.getLoggerRepository();
    //h.disableDebug();
    h.setThreshold((Level) Level.INFO);
    assertEquals(caRoot.counter, 0);     

    root.debug(MSG); assertEquals(caRoot.counter, 0);  
    root.info(MSG); assertEquals(caRoot.counter, 1);  
    root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 2);  
    root.warn(MSG); assertEquals(caRoot.counter, 3);  

    //h.disableInfo();
    h.setThreshold((Level) Level.WARN);
    root.debug(MSG); assertEquals(caRoot.counter, 3);  
    root.info(MSG); assertEquals(caRoot.counter, 3);  
    root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 4);  
    root.error(MSG); assertEquals(caRoot.counter, 5);  
    root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);  

    //h.disableAll();
    h.setThreshold(Level.OFF);
    root.debug(MSG); assertEquals(caRoot.counter, 6);  
    root.info(MSG); assertEquals(caRoot.counter, 6);  
    root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);  
    root.error(MSG); assertEquals(caRoot.counter, 6);  
    root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);  
    root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);  

    //h.disable(Level.FATAL);
    h.setThreshold(Level.OFF);
    root.debug(MSG); assertEquals(caRoot.counter, 6);  
    root.info(MSG); assertEquals(caRoot.counter, 6);  
    root.log(Level.WARN, MSG); assertEquals(caRoot.counter, 6);  
    root.error(MSG); assertEquals(caRoot.counter, 6);
    root.log(Level.ERROR, MSG); assertEquals(caRoot.counter, 6);  
    root.log(Level.FATAL, MSG); assertEquals(caRoot.counter, 6);  
  
public voidtestExists()

    Logger a = Logger.getLogger("a");
    Logger a_b = Logger.getLogger("a.b");
    Logger a_b_c = Logger.getLogger("a.b.c");
    
    Logger t;
    t = LogManager.exists("xx");    assertNull(t);
    t = LogManager.exists("a");     assertSame(a, t);
    t = LogManager.exists("a.b");   assertSame(a_b, t);
    t = LogManager.exists("a.b.c"); assertSame(a_b_c, t);
  
public voidtestHierarchy1()

    Hierarchy h = new Hierarchy(new RootLogger((Level) Level.ERROR));
    Logger a0 = h.getLogger("a");
    assertEquals("a", a0.getName());
    assertNull(a0.getLevel());
    assertSame(Level.ERROR, a0.getEffectiveLevel());

    Logger a1 = h.getLogger("a");
    assertSame(a0, a1);
  
public voidtestIsTraceEnabled()
Tests isTraceEnabled.

since
1.2.12

        VectorAppender appender = new VectorAppender();
        appender.activateOptions();
        Logger root = Logger.getRootLogger();
        root.addAppender(appender);
        root.setLevel(Level.INFO);

        Logger tracer = Logger.getLogger("com.example.Tracer");
        tracer.setLevel(Level.TRACE);

        assertTrue(tracer.isTraceEnabled());
        assertFalse(root.isTraceEnabled());
    
public voidtestRB1()

    Logger root = Logger.getRootLogger(); 
    root.setResourceBundle(rbUS);
    ResourceBundle t = root.getResourceBundle();
    assertSame(t, rbUS);

    Logger x = Logger.getLogger("x");
    Logger x_y = Logger.getLogger("x.y");
    Logger x_y_z = Logger.getLogger("x.y.z");

    t = x.getResourceBundle();     assertSame(t, rbUS);
    t = x_y.getResourceBundle();   assertSame(t, rbUS);
    t = x_y_z.getResourceBundle(); assertSame(t, rbUS);
  
public voidtestRB2()

    Logger root = Logger.getRootLogger(); 
    root.setResourceBundle(rbUS);
    ResourceBundle t = root.getResourceBundle();
    assertSame(t, rbUS);

    Logger x = Logger.getLogger("x");
    Logger x_y = Logger.getLogger("x.y");
    Logger x_y_z = Logger.getLogger("x.y.z");

    x_y.setResourceBundle(rbFR);
    t = x.getResourceBundle();     assertSame(t, rbUS);
    t = x_y.getResourceBundle();   assertSame(t, rbFR);
    t = x_y_z.getResourceBundle(); assertSame(t, rbFR);    
  
public voidtestRB3()

    Logger root = Logger.getRootLogger(); 
    root.setResourceBundle(rbUS);
    ResourceBundle t = root.getResourceBundle();
    assertSame(t, rbUS);

    Logger x = Logger.getLogger("x");
    Logger x_y = Logger.getLogger("x.y");
    Logger x_y_z = Logger.getLogger("x.y.z");

    x_y.setResourceBundle(rbFR);
    x_y_z.setResourceBundle(rbCH);
    t = x.getResourceBundle();     assertSame(t, rbUS);
    t = x_y.getResourceBundle();   assertSame(t, rbFR);
    t = x_y_z.getResourceBundle(); assertSame(t, rbCH);    
  
public voidtestTrace()
Tests logger.trace(Object).

since
1.2.12

      VectorAppender appender = new VectorAppender();
      appender.activateOptions();
      Logger root = Logger.getRootLogger();
      root.addAppender(appender);
      root.setLevel(Level.INFO);

      Logger tracer = Logger.getLogger("com.example.Tracer");
      tracer.setLevel(Level.TRACE);

      tracer.trace("Message 1");
      root.trace("Discarded Message");
      root.trace("Discarded Message");

      Vector msgs = appender.getVector();
      assertEquals(1, msgs.size());
      LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
      assertEquals(Level.TRACE, event.getLevel());
      assertEquals("Message 1", event.getMessage());
  
public voidtestTraceWithException()
Tests logger.trace(Object, Exception).

since
1.2.12

        VectorAppender appender = new VectorAppender();
        appender.activateOptions();
        Logger root = Logger.getRootLogger();
        root.addAppender(appender);
        root.setLevel(Level.INFO);

        Logger tracer = Logger.getLogger("com.example.Tracer");
        tracer.setLevel(Level.TRACE);
        NullPointerException ex = new NullPointerException();

        tracer.trace("Message 1", ex);
        root.trace("Discarded Message", ex);
        root.trace("Discarded Message", ex);

        Vector msgs = appender.getVector();
        assertEquals(1, msgs.size());
        LoggingEvent event = (LoggingEvent) msgs.elementAt(0);
        assertEquals(Level.TRACE, event.getLevel());
        assertEquals("Message 1", event.getMessage());