Home | History | Annotate | Download | only in slf4j
      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.io.Closeable;
     28 import java.io.IOException;
     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     public InvocationTest(String arg0) {
     40         super(arg0);
     41     }
     42 
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45     }
     46 
     47     protected void tearDown() throws Exception {
     48         super.tearDown();
     49     }
     50 
     51     public void test1() {
     52         Logger logger = LoggerFactory.getLogger("test1");
     53         logger.debug("Hello world.");
     54     }
     55 
     56     public void test2() {
     57         Integer i1 = new Integer(1);
     58         Integer i2 = new Integer(2);
     59         Integer i3 = new Integer(3);
     60         Exception e = new Exception("This is a test exception.");
     61         Logger logger = LoggerFactory.getLogger("test2");
     62 
     63         logger.debug("Hello world 1.");
     64         logger.debug("Hello world {}", i1);
     65         logger.debug("val={} val={}", i1, i2);
     66         logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
     67 
     68         logger.debug("Hello world 2", e);
     69         logger.info("Hello world 2.");
     70 
     71         logger.warn("Hello world 3.");
     72         logger.warn("Hello world 3", e);
     73 
     74         logger.error("Hello world 4.");
     75         logger.error("Hello world {}", new Integer(3));
     76         logger.error("Hello world 4.", e);
     77     }
     78 
     79     public void testNull() {
     80         Logger logger = LoggerFactory.getLogger("testNull");
     81         logger.debug(null);
     82         logger.info(null);
     83         logger.warn(null);
     84         logger.error(null);
     85 
     86         Exception e = new Exception("This is a test exception.");
     87         logger.debug(null, e);
     88         logger.info(null, e);
     89         logger.warn(null, e);
     90         logger.error(null, e);
     91     }
     92 
     93     public void testMarker() {
     94         Logger logger = LoggerFactory.getLogger("testMarker");
     95         Marker blue = MarkerFactory.getMarker("BLUE");
     96         logger.debug(blue, "hello");
     97         logger.info(blue, "hello");
     98         logger.warn(blue, "hello");
     99         logger.error(blue, "hello");
    100 
    101         logger.debug(blue, "hello {}", "world");
    102         logger.info(blue, "hello {}", "world");
    103         logger.warn(blue, "hello {}", "world");
    104         logger.error(blue, "hello {}", "world");
    105 
    106         logger.debug(blue, "hello {} and {} ", "world", "universe");
    107         logger.info(blue, "hello {} and {} ", "world", "universe");
    108         logger.warn(blue, "hello {} and {} ", "world", "universe");
    109         logger.error(blue, "hello {} and {} ", "world", "universe");
    110     }
    111 
    112     public void testMDC() {
    113         MDC.put("k", "v");
    114         assertNull(MDC.get("k"));
    115         MDC.remove("k");
    116         assertNull(MDC.get("k"));
    117         MDC.clear();
    118     }
    119 
    120     public void testMDCCloseable() {
    121         MDC.MDCCloseable closeable = MDC.putCloseable("k", "v");
    122         assertNull(MDC.get("k"));
    123         closeable.close();
    124         assertNull(MDC.get("k"));
    125         MDC.clear();
    126     }
    127 }
    128