View Javadoc
1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.reload4j;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertNull;
30  import static org.junit.Assert.fail;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.apache.log4j.spi.LoggingEvent;
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  import org.slf4j.MDC;
42  import org.slf4j.Marker;
43  import org.slf4j.MarkerFactory;
44  
45  /**
46   * Test whether invoking the SLF4J API causes problems or not.
47   * 
48   * @author Ceki Gulcu
49   * 
50   */
51  public class InvocationTest {
52  
53      ListAppender listAppender = new ListAppender();
54      org.apache.log4j.Logger root;
55  
56      @Before
57      public void setUp() throws Exception {
58          root = org.apache.log4j.Logger.getRootLogger();
59          root.addAppender(listAppender);
60      }
61  
62      @After
63      public void tearDown() throws Exception {
64          root.getLoggerRepository().resetConfiguration();
65      }
66  
67      @Test
68      public void test1() {
69  
70          Logger logger = LoggerFactory.getLogger("test1");
71          logger.debug("Hello world.");
72          assertEquals(1, listAppender.list.size());
73      }
74  
75      @Test
76      public void test2() {
77          Integer i1 = Integer.valueOf(1);
78          Integer i2 = Integer.valueOf(2);
79          Integer i3 = Integer.valueOf(3);
80          Exception e = new Exception("This is a test exception.");
81          Logger logger = LoggerFactory.getLogger("test2");
82  
83          logger.trace("Hello trace.");
84  
85          logger.debug("Hello world 1.");
86          logger.debug("Hello world {}", i1);
87          logger.debug("val={} val={}", i1, i2);
88          logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
89  
90          logger.debug("Hello world 2", e);
91          logger.info("Hello world 2.");
92  
93          logger.warn("Hello world 3.");
94          logger.warn("Hello world 3", e);
95  
96          logger.error("Hello world 4.");
97          logger.error("Hello world {}", Integer.valueOf(3));
98          logger.error("Hello world 4.", e);
99          assertEquals(11, listAppender.list.size());
100     }
101 
102     @Test
103     public void testNull() {
104         Logger logger = LoggerFactory.getLogger("testNull");
105         logger.trace(null);
106         logger.debug(null);
107         logger.info(null);
108         logger.warn(null);
109         logger.error(null);
110 
111         Exception e = new Exception("This is a test exception.");
112         logger.debug(null, e);
113         logger.info(null, e);
114         logger.warn(null, e);
115         logger.error(null, e);
116         assertEquals(8, listAppender.list.size());
117     }
118 
119     // http://jira.qos.ch/browse/SLF4J-69
120     // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=78
121     @Test
122     public void testNullParameter_BUG78() {
123         Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78");
124         String[] parameters = null;
125         String msg = "hello {}";
126 
127         logger.debug(msg, (Object[]) parameters);
128 
129         assertEquals(1, listAppender.list.size());
130         LoggingEvent e = (LoggingEvent) listAppender.list.get(0);
131         assertEquals(msg, e.getMessage());
132     }
133 
134     @Test
135     public void testMarker() {
136         Logger logger = LoggerFactory.getLogger("testMarker");
137         Marker blue = MarkerFactory.getMarker("BLUE");
138         logger.trace(blue, "hello");
139         logger.debug(blue, "hello");
140         logger.info(blue, "hello");
141         logger.warn(blue, "hello");
142         logger.error(blue, "hello");
143 
144         logger.debug(blue, "hello {}", "world");
145         logger.info(blue, "hello {}", "world");
146         logger.warn(blue, "hello {}", "world");
147         logger.error(blue, "hello {}", "world");
148 
149         logger.debug(blue, "hello {} and {} ", "world", "universe");
150         logger.info(blue, "hello {} and {} ", "world", "universe");
151         logger.warn(blue, "hello {} and {} ", "world", "universe");
152         logger.error(blue, "hello {} and {} ", "world", "universe");
153         assertEquals(12, listAppender.list.size());
154     }
155 
156     @Test
157     public void testMDC() {
158         MDC.put("k", "v");
159         assertNotNull(MDC.get("k"));
160         assertEquals("v", MDC.get("k"));
161 
162         MDC.remove("k");
163         assertNull(MDC.get("k"));
164 
165         MDC.put("k1", "v1");
166         assertEquals("v1", MDC.get("k1"));
167         MDC.clear();
168         assertNull(MDC.get("k1"));
169 
170         try {
171             MDC.put(null, "x");
172             fail("null keys are invalid");
173         } catch (IllegalArgumentException e) {
174         }
175     }
176 
177     @Test
178     public void testMDCContextMapValues() {
179         Map<String, String> map = new HashMap<>();
180         map.put("ka", "va");
181         map.put("kb", "vb");
182 
183         MDC.put("k", "v");
184         assertEquals("v", MDC.get("k"));
185         MDC.setContextMap(map);
186         assertNull(MDC.get("k"));
187         assertEquals("va", MDC.get("ka"));
188         assertEquals("vb", MDC.get("kb"));
189     }
190 
191     @Test
192     public void testCallerInfo() {
193         Logger logger = LoggerFactory.getLogger("testMarker");
194         listAppender.extractLocationInfo = true;
195         logger.debug("hello");
196         LoggingEvent event = listAppender.list.get(0);
197         assertEquals(this.getClass().getName(), event.getLocationInformation().getClassName());
198     }
199 
200     @Test
201     public void testCallerInfoWithFluentAPI() {
202         Logger logger = LoggerFactory.getLogger("testMarker");
203         listAppender.extractLocationInfo = true;
204         logger.atDebug().log("hello");
205         LoggingEvent event = listAppender.list.get(0);
206         assertEquals(this.getClass().getName(), event.getLocationInformation().getClassName());
207     }
208 
209 }