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.nop;
26  
27  import static org.junit.Assert.assertNull;
28  
29  import org.junit.Test;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.slf4j.MDC;
33  import org.slf4j.Marker;
34  import org.slf4j.MarkerFactory;
35  
36  /**
37   * Test whether invoking the SLF4J API causes problems or not.
38   * 
39   * @author Ceki Gulcu
40   *
41   */
42  public class InvocationTest {
43  
44      @Test
45      public void test1() {
46          Logger logger = LoggerFactory.getLogger("test1");
47          logger.debug("Hello world.");
48      }
49  
50      @Test
51      public void test2() {
52          Integer i1 = Integer.valueOf(1);
53          Integer i2 = Integer.valueOf(2);
54          Integer i3 = Integer.valueOf(3);
55          Exception e = new Exception("This is a test exception.");
56          Logger logger = LoggerFactory.getLogger("test2");
57  
58          logger.debug("Hello world 1.");
59          logger.debug("Hello world {}", i1);
60          logger.debug("val={} val={}", i1, i2);
61          logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
62  
63          logger.debug("Hello world 2", e);
64          logger.info("Hello world 2.");
65  
66          logger.warn("Hello world 3.");
67          logger.warn("Hello world 3", e);
68  
69          logger.error("Hello world 4.");
70          logger.error("Hello world {}", Integer.valueOf(3));
71          logger.error("Hello world 4.", e);
72      }
73  
74      @Test
75      public void testNull() {
76          Logger logger = LoggerFactory.getLogger("testNull");
77          logger.debug(null);
78          logger.info(null);
79          logger.warn(null);
80          logger.error(null);
81  
82          Exception e = new Exception("This is a test exception.");
83          logger.debug(null, e);
84          logger.info(null, e);
85          logger.warn(null, e);
86          logger.error(null, e);
87      }
88  
89      @Test
90      public void testMarker() {
91          Logger logger = LoggerFactory.getLogger("testMarker");
92          Marker blue = MarkerFactory.getMarker("BLUE");
93          logger.debug(blue, "hello");
94          logger.info(blue, "hello");
95          logger.warn(blue, "hello");
96          logger.error(blue, "hello");
97  
98          logger.debug(blue, "hello {}", "world");
99          logger.info(blue, "hello {}", "world");
100         logger.warn(blue, "hello {}", "world");
101         logger.error(blue, "hello {}", "world");
102 
103         logger.debug(blue, "hello {} and {} ", "world", "universe");
104         logger.info(blue, "hello {} and {} ", "world", "universe");
105         logger.warn(blue, "hello {} and {} ", "world", "universe");
106         logger.error(blue, "hello {} and {} ", "world", "universe");
107     }
108 
109     @Test
110     public void testMDC() {
111         MDC.put("k", "v");
112         assertNull(MDC.get("k"));
113         MDC.remove("k");
114         assertNull(MDC.get("k"));
115         MDC.clear();
116     }
117 
118     @Test
119     public void testMDCCloseable() {
120         MDC.MDCCloseable closeable = MDC.putCloseable("k", "v");
121         assertNull(MDC.get("k"));
122         closeable.close();
123         assertNull(MDC.get("k"));
124         MDC.clear();
125     }
126 }