1   /* 
2    * Copyright (c) 2004-2005 SLF4J.ORG
3    * Copyright (c) 2004-2005 QOS.CH
4    * 
5    * All rights reserved.
6    * 
7    * Permission is hereby granted, free of charge, to any person obtaining
8    * a copy of this software and associated documentation files (the
9    * "Software"), to  deal in  the Software without  restriction, including
10   * without limitation  the rights to  use, copy, modify,  merge, publish,
11   * distribute, and/or sell copies of  the Software, and to permit persons
12   * to whom  the Software is furnished  to do so, provided  that the above
13   * copyright notice(s) and this permission notice appear in all copies of
14   * the  Software and  that both  the above  copyright notice(s)  and this
15   * permission notice appear in supporting documentation.
16   * 
17   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
18   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
19   * MERCHANTABILITY, FITNESS FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT
20   * OF  THIRD PARTY  RIGHTS. IN  NO EVENT  SHALL THE  COPYRIGHT  HOLDER OR
21   * HOLDERS  INCLUDED IN  THIS  NOTICE BE  LIABLE  FOR ANY  CLAIM, OR  ANY
22   * SPECIAL INDIRECT  OR CONSEQUENTIAL DAMAGES, OR  ANY DAMAGES WHATSOEVER
23   * RESULTING FROM LOSS  OF USE, DATA OR PROFITS, WHETHER  IN AN ACTION OF
24   * CONTRACT, NEGLIGENCE  OR OTHER TORTIOUS  ACTION, ARISING OUT OF  OR IN
25   * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26   * 
27   * Except as  contained in  this notice, the  name of a  copyright holder
28   * shall not be used in advertising or otherwise to promote the sale, use
29   * or other dealings in this Software without prior written authorization
30   * of the copyright holder.
31   *
32   */
33  
34  package org.slf4j;
35  
36  import java.util.logging.Level;
37  
38  import junit.framework.TestCase;
39  
40  
41  /**
42   * Test whether invoking the SLF4J API causes problems or not.
43   * 
44   * @author Ceki Gulcu
45   *
46   */
47  public class InvocationTest extends TestCase {
48  
49    Level oldLevel;
50    java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
51    
52    
53    public InvocationTest (String arg0) {
54      super(arg0);
55    }
56  
57    protected void setUp() throws Exception {
58      super.setUp();
59      oldLevel = root.getLevel();
60      root.setLevel(Level.OFF);
61    }
62  
63    protected void tearDown() throws Exception {
64      super.tearDown();
65      root.setLevel(oldLevel);
66    }
67    
68    public void test1() {
69      Logger logger = LoggerFactory.getLogger("test1");
70      logger.debug("Hello world.");
71    }
72    
73    public void test2() {
74      Integer i1 = new Integer(1);
75      Integer i2 = new Integer(2);
76      Integer i3 = new Integer(3);
77      Exception e = new Exception("This is a test exception.");
78      Logger logger = LoggerFactory.getLogger("test2");
79      
80      logger.debug("Hello world 1.");
81      logger.debug("Hello world {}", i1);
82      logger.debug("val={} val={}", i1, i2);
83      logger.debug("val={} val={} val={}", new Object[]{i1, i2, i3});
84      
85      logger.debug("Hello world 2", e);
86      logger.info("Hello world 2.");
87   
88      
89      logger.warn("Hello world 3.");
90      logger.warn("Hello world 3", e);
91   
92    
93      logger.error("Hello world 4.");
94      logger.error("Hello world {}", new Integer(3)); 
95      logger.error("Hello world 4.", e);
96    }
97    
98    public void testNull() {
99      Logger logger = LoggerFactory.getLogger("testNull");
100     logger.debug(null);
101     logger.info(null);
102     logger.warn(null);
103     logger.error(null);
104     
105     Exception e = new Exception("This is a test exception.");
106     logger.debug(null, e);
107     logger.info(null, e);
108     logger.warn(null, e);
109     logger.error(null, e);
110   }
111   
112   public void testMarker() {
113     Logger logger = LoggerFactory.getLogger("testMarker");
114     Marker blue = MarkerFactory.getMarker("BLUE");
115     logger.debug(blue, "hello");
116     logger.info(blue, "hello");
117     logger.warn(blue, "hello");
118     logger.error(blue, "hello");
119     
120     logger.debug(blue, "hello {}", "world");
121     logger.info(blue, "hello {}", "world");
122     logger.warn(blue, "hello {}", "world");
123     logger.error(blue, "hello {}", "world");
124 
125     logger.debug(blue, "hello {} and {} ", "world", "universe");
126     logger.info(blue, "hello {} and {} ", "world", "universe");
127     logger.warn(blue, "hello {} and {} ", "world", "universe");
128     logger.error(blue, "hello {} and {} ", "world", "universe");
129   }
130   
131   public void testMDC() {
132     MDC.put("k", "v");
133     assertNull(MDC.get("k"));
134     MDC.remove("k");
135     assertNull(MDC.get("k"));
136     MDC.clear();
137   }
138 }