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