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 static org.junit.Assert.assertNull;
    28
    29  import java.util.logging.Level;
    30
    31  import org.junit.After;
    32  import org.junit.Before;
    33  import org.junit.Test;
    34
    35  /**
    36   * Test whether invoking the SLF4J API causes problems or not.
    37   *
    38   * @author Ceki Gulcu
    39   *
    40   */
    41  public class InvocationTest {
    42
    43      Level oldLevel;
    44      java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
    45
    46      @Before
    47      public void setUp() throws Exception {
    48          oldLevel = root.getLevel();
    49          root.setLevel(Level.OFF);
    50      }
    51
    52      @After
    53      public void tearDown() throws Exception {
    54          root.setLevel(oldLevel);
    55      }
    56
    57      @Test
    58      public void test1() {
    59          Logger logger = LoggerFactory.getLogger("test1");
    60          logger.debug("Hello world.");
    61      }
    62
    63      @Test
    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          logger.warn("Hello world 3.");
    80          logger.warn("Hello world 3", e);
    81
    82          logger.error("Hello world 4.");
    83          logger.error("Hello world {}", new Integer(3));
    84          logger.error("Hello world 4.", e);
    85      }
    86
    87      @Test
    88      public void testNull() {
    89          Logger logger = LoggerFactory.getLogger("testNull");
    90          logger.debug(null);
    91          logger.info(null);
    92          logger.warn(null);
    93          logger.error(null);
    94
    95          Exception e = new Exception("This is a test exception.");
    96          logger.debug(null, e);
    97          logger.info(null, e);
    98          logger.warn(null, e);
    99          logger.error(null, e);
    100     }
    101
    102     @Test
    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     @Test
    123     public void testMDC() {
    124         MDC.put("k", "v");
    125         assertNull(MDC.get("k"));
    126         MDC.remove("k");
    127         assertNull(MDC.get("k"));
    128         MDC.clear();
    129     }
    130 }