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.simple;
26  
27  import static org.junit.Assert.assertNull;
28  
29  import java.io.PrintStream;
30  
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.slf4j.MDC;
37  import org.slf4j.Marker;
38  import org.slf4j.MarkerFactory;
39  
40  /**
41   * Test whether invoking the SLF4J API causes problems or not.
42   * 
43   * @author Ceki Gulcu
44   *
45   */
46  public class InvocationTest {
47  
48      PrintStream old = System.err;
49  
50      @Before
51      public void setUp() throws Exception {
52          System.setErr(new SilentPrintStream(old));
53      }
54  
55      @After
56      public void tearDown() throws Exception {
57          System.setErr(old);
58      }
59  
60      @Test
61      public void test1() {
62          Logger logger = LoggerFactory.getLogger("test1");
63          logger.debug("Hello world.");
64      }
65  
66      @Test
67      public void test2() {
68          Integer i1 = Integer.valueOf(1);
69          Integer i2 = Integer.valueOf(2);
70          Integer i3 = Integer.valueOf(3);
71          Exception e = new Exception("This is a test exception.");
72          Logger logger = LoggerFactory.getLogger("test2");
73  
74          logger.debug("Hello world 1.");
75          logger.debug("Hello world {}", i1);
76          logger.debug("val={} val={}", i1, i2);
77          logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
78  
79          logger.debug("Hello world 2", e);
80          logger.info("Hello world 2.");
81  
82          logger.warn("Hello world 3.");
83          logger.warn("Hello world 3", e);
84  
85          logger.error("Hello world 4.");
86          logger.error("Hello world {}", Integer.valueOf(3));
87          logger.error("Hello world 4.", e);
88      }
89  
90      // http://jira.qos.ch/browse/SLF4J-69
91      // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=78
92      @Test
93      public void testNullParameter_BUG78() {
94          Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78");
95          String[] parameters = null;
96          String msg = "hello {}";
97          logger.info(msg, (Object[]) parameters);
98      }
99  
100     @Test
101     public void testNull() {
102         Logger logger = LoggerFactory.getLogger("testNull");
103         logger.debug(null);
104         logger.info(null);
105         logger.warn(null);
106         logger.error(null);
107 
108         Exception e = new Exception("This is a test exception.");
109         logger.debug(null, e);
110         logger.info(null, e);
111         logger.warn(null, e);
112         logger.error(null, e);
113     }
114 
115     @Test
116     public void testMarker() {
117         Logger logger = LoggerFactory.getLogger("testMarker");
118         Marker blue = MarkerFactory.getMarker("BLUE");
119         logger.debug(blue, "hello");
120         logger.info(blue, "hello");
121         logger.warn(blue, "hello");
122         logger.error(blue, "hello");
123 
124         logger.debug(blue, "hello {}", "world");
125         logger.info(blue, "hello {}", "world");
126         logger.warn(blue, "hello {}", "world");
127         logger.error(blue, "hello {}", "world");
128 
129         logger.debug(blue, "hello {} and {} ", "world", "universe");
130         logger.info(blue, "hello {} and {} ", "world", "universe");
131         logger.warn(blue, "hello {} and {} ", "world", "universe");
132         logger.error(blue, "hello {} and {} ", "world", "universe");
133     }
134 
135     @Test
136     public void testMDC() {
137         MDC.put("k", "v");
138         assertNull(MDC.get("k"));
139         MDC.remove("k");
140         assertNull(MDC.get("k"));
141         MDC.clear();
142     }
143 }