• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test;
2 
3 import junit.framework.TestCase;
4 
5 import org.slf4j.LoggerFactory;
6 
7 import ch.qos.logback.classic.LoggerContext;
8 import ch.qos.logback.classic.spi.LoggingEvent;
9 import ch.qos.logback.core.read.ListAppender;
10 import org.apache.log4j.Level;
11 
12 /**
13  * A class that tests the invocation of the org.apache.log4j.Logger class that
14  * belongs to the log4j-bridge package
15  *
16  * @author Sébastien Pennec
17  * @author Ceki Gülcü
18  */
19 
20 public class LoggerTest extends TestCase {
21 
22   LoggerContext context;
23   ListAppender<LoggingEvent> appender;
24   ch.qos.logback.classic.Logger logbackLogger;
25   org.apache.log4j.Logger log4jLogger;
26 
setUp()27   public void setUp() throws Exception {
28     context = (LoggerContext) LoggerFactory.getILoggerFactory();
29     context.shutdownAndReset();
30     appender = new ListAppender<LoggingEvent>();
31     appender.setContext(context);
32     appender.setName("listAppender");
33     appender.start();
34     ch.qos.logback.classic.Logger lbLogger = context
35         .getLogger(LoggerContext.ROOT_NAME);
36     lbLogger.addAppender(appender);
37 
38     log4jLogger = org.apache.log4j.Logger.getLogger(LoggerTest.class);
39     logbackLogger = context.getLogger(LoggerTest.class);
40     super.setUp();
41   }
42 
tearDown()43   public void tearDown() throws Exception {
44     appender.stop();
45     context.stop();
46     appender = null;
47     context = null;
48     logbackLogger = null;
49     log4jLogger = null;
50     super.tearDown();
51   }
52 
testLogWithObjectMessages()53   public void testLogWithObjectMessages() {
54     LoggingEvent event;
55 
56     log4jLogger.debug("test");
57     event = appender.list.get(0);
58     assertEquals("test", event.getMessage());
59     appender.list.clear();
60 
61     log4jLogger.debug(null);
62     event = appender.list.get(0);
63     assertEquals(null, event.getMessage());
64     appender.list.clear();
65 
66     DummyObject dummy = new DummyObject();
67     log4jLogger.debug(dummy);
68     event = appender.list.get(0);
69     assertEquals(dummy.toString(), event.getMessage());
70     appender.list.clear();
71   }
72 
testIsEnabledAPI()73   public void testIsEnabledAPI() {
74     assertFalse(log4jLogger.isTraceEnabled());
75     assertTrue(log4jLogger.isDebugEnabled());
76     assertTrue(log4jLogger.isInfoEnabled());
77     assertTrue(log4jLogger.isWarnEnabled());
78     assertTrue(log4jLogger.isErrorEnabled());
79   }
80 
testPrintAPI()81   public void testPrintAPI() {
82     Exception e = new Exception("just testing");
83 
84     log4jLogger.trace(null);
85     assertEquals(1, appender.list.size());
86     appender.list.clear();
87 
88     log4jLogger.debug(null);
89     assertEquals(1, appender.list.size());
90     appender.list.clear();
91 
92     log4jLogger.debug("debug message");
93     assertEquals(1, appender.list.size());
94     appender.list.clear();
95 
96     log4jLogger.info(null);
97     assertEquals(1, appender.list.size());
98     appender.list.clear();
99 
100     log4jLogger.info("info  message");
101     assertEquals(1, appender.list.size());
102     appender.list.clear();
103 
104     log4jLogger.warn(null);
105     assertEquals(1, appender.list.size());
106     appender.list.clear();
107 
108     log4jLogger.warn("warn message");
109     assertEquals(1, appender.list.size());
110     appender.list.clear();
111 
112     log4jLogger.error(null);
113     assertEquals(1, appender.list.size());
114     appender.list.clear();
115 
116     log4jLogger.error("error message");
117     assertEquals(1, appender.list.size());
118     appender.list.clear();
119 
120     log4jLogger.debug(null, e);
121     assertEquals(1, appender.list.size());
122     appender.list.clear();
123 
124     log4jLogger.debug("debug message", e);
125     assertEquals(1, appender.list.size());
126     appender.list.clear();
127 
128     log4jLogger.info(null, e);
129     assertEquals(1, appender.list.size());
130     appender.list.clear();
131 
132     log4jLogger.info("info  message", e);
133     assertEquals(1, appender.list.size());
134     appender.list.clear();
135 
136     log4jLogger.warn(null, e);
137     assertEquals(1, appender.list.size());
138     appender.list.clear();
139 
140     log4jLogger.warn("warn message", e);
141     assertEquals(1, appender.list.size());
142     appender.list.clear();
143 
144     log4jLogger.error(null, e);
145     assertEquals(1, appender.list.size());
146     appender.list.clear();
147 
148     log4jLogger.error("error message", e);
149     assertEquals(1, appender.list.size());
150     appender.list.clear();
151 
152   }
153 
testLogAPI()154   public void testLogAPI() {
155     log4jLogger.log("x", Level.TRACE, "x", null);
156     assertEquals(0, appender.list.size());
157 
158     log4jLogger.log("x", Level.DEBUG, "x", null);
159     log4jLogger.log("x", Level.INFO, "x", null);
160     log4jLogger.log("x", Level.WARN, "x", null);
161     log4jLogger.log("x", Level.ERROR, "x", null);
162     log4jLogger.log("x", Level.FATAL, "x", null);
163 
164     assertEquals(5, appender.list.size());
165     appender.list.clear();
166 
167   }
168 
169 }
170