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