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.jul;
26  
27  import org.junit.After;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.slf4j.*;
32  
33  import java.util.logging.Handler;
34  import java.util.logging.Level;
35  import java.util.logging.LogRecord;
36  
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertNull;
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.fail;
41  
42  /**
43   * Test whether invoking the SLF4J API causes problems or not.
44   *
45   * @author Ceki Gulcu
46   */
47  public class InvocationTest {
48  
49      Level oldLevel;
50      java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
51  
52      ListHandler listHandler = new ListHandler();
53  
54      @Before
55      public void setUp() throws Exception {
56          oldLevel = root.getLevel();
57          root.setLevel(Level.FINE);
58          // removeAllHandlers(root);
59          root.addHandler(listHandler);
60      }
61  
62      @After
63      public void tearDown() throws Exception {
64          root.setLevel(oldLevel);
65          removeListHandlers(root);
66      }
67  
68      void removeListHandlers(java.util.logging.Logger logger) {
69          Handler[] handlers = logger.getHandlers();
70          for (Handler h : handlers) {
71              if (h instanceof ListHandler)
72                  logger.removeHandler(h);
73          }
74      }
75  
76      @Test
77      public void smoke() {
78          Logger logger = LoggerFactory.getLogger("test1");
79          logger.debug("Hello world.");
80          assertLogMessage("Hello world.", 0);
81      }
82  
83      @Test
84      public void verifyMessageFormatting() {
85          Integer i1 = Integer.valueOf(1);
86          Integer i2 = Integer.valueOf(2);
87          Integer i3 = Integer.valueOf(3);
88          Exception e = new Exception("This is a test exception.");
89          Logger logger = LoggerFactory.getLogger("test2");
90  
91          int index = 0;
92          logger.debug("Hello world");
93          assertLogMessage("Hello world", index++);
94  
95          logger.debug("Hello world {}", i1);
96          assertLogMessage("Hello world " + i1, index++);
97  
98          logger.debug("val={} val={}", i1, i2);
99          assertLogMessage("val=1 val=2", index++);
100 
101         logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
102         assertLogMessage("val=1 val=2 val=3", index++);
103 
104         logger.debug("Hello world 2", e);
105         assertLogMessage("Hello world 2", index);
106         assertException(e.getClass(), index++);
107         logger.info("Hello world 2.");
108 
109         logger.warn("Hello world 3.");
110         logger.warn("Hello world 3", e);
111 
112         logger.error("Hello world 4.");
113         logger.error("Hello world {}", Integer.valueOf(3));
114         logger.error("Hello world 4.", e);
115     }
116 
117     @Test
118     public void testNull() {
119         Logger logger = LoggerFactory.getLogger("testNull");
120         logger.debug(null);
121         logger.info(null);
122         logger.warn(null);
123         logger.error(null);
124 
125         Exception e = new Exception("This is a test exception.");
126         logger.debug(null, e);
127         logger.info(null, e);
128         logger.warn(null, e);
129         logger.error(null, e);
130     }
131 
132     @Test
133     public void testMarker() {
134         Logger logger = LoggerFactory.getLogger("testMarker");
135         Marker blue = MarkerFactory.getMarker("BLUE");
136         logger.debug(blue, "hello");
137         logger.info(blue, "hello");
138         logger.warn(blue, "hello");
139         logger.error(blue, "hello");
140 
141         logger.debug(blue, "hello {}", "world");
142         logger.info(blue, "hello {}", "world");
143         logger.warn(blue, "hello {}", "world");
144         logger.error(blue, "hello {}", "world");
145 
146         logger.debug(blue, "hello {} and {} ", "world", "universe");
147         logger.info(blue, "hello {} and {} ", "world", "universe");
148         logger.warn(blue, "hello {} and {} ", "world", "universe");
149         logger.error(blue, "hello {} and {} ", "world", "universe");
150     }
151 
152     @Test
153     public void testMDC() {
154         MDC.put("k", "v");
155         assertNotNull(MDC.get("k"));
156         assertEquals("v", MDC.get("k"));
157 
158         MDC.remove("k");
159         assertNull(MDC.get("k"));
160 
161         MDC.put("k1", "v1");
162         assertEquals("v1", MDC.get("k1"));
163         MDC.clear();
164         assertNull(MDC.get("k1"));
165 
166         try {
167             MDC.put(null, "x");
168             fail("null keys are invalid");
169         } catch (IllegalArgumentException e) {
170         }
171     }
172 
173     private void assertLogMessage(String expected, int index) {
174         LogRecord logRecord = listHandler.recordList.get(index);
175         Assert.assertNotNull(logRecord);
176         assertEquals(expected, logRecord.getMessage());
177     }
178 
179     private void assertException(Class<? extends Throwable> exceptionType, int index) {
180         LogRecord logRecord = listHandler.recordList.get(index);
181         Assert.assertNotNull(logRecord);
182         assertEquals(exceptionType, logRecord.getThrown().getClass());
183     }
184 
185 }