View Javadoc
1   /**
2    * Copyright (c) 2004-2021 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.jdk.platform.logging.test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  
30  import java.io.IOException;
31  import java.io.PrintStream;
32  import java.lang.System.Logger;
33  import java.lang.System.Logger.Level;
34  import java.lang.System.LoggerFinder;
35  import java.util.List;
36  import java.util.Random;
37  
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  
43  /**
44   * The present test is fragile in the sense that it sets up SimpleLogger
45   * with a StringPrintStream and reverts to the old stream when done.
46   * 
47   * Any tests running simultaneously (and using SimpleLogger) will be affected 
48   * by this. Moreover, since SimpleLogger is initialized by the call to LoggerFactory
49   * and tests also using LoggerFactory will also be affected.
50   * 
51   * @author Ceki Gülcü
52   *
53   */
54  public class SLF4JPlatformLoggingTest {
55  
56      static final String PREFIX = "org.slf4j.simpleLogger.";
57      static final String SIMPLE_LOGGER_FILE_PROPERTY = PREFIX + "logFile";
58      static final String SIMPLE_LOGGER_THREAD_NAME_PROPERTY = PREFIX + "showThreadName";
59      
60      static final String EXPECTED_FINDER_CLASS = "org.slf4j.jdk.platform.logging.SLF4JSystemLoggerFinder";
61      
62      static int diff = new Random().nextInt(100*1000*1000);
63     
64      static final PrintStream oldErr = System.err;
65      static StringPrintStream SPS = new StringPrintStream(oldErr, false);
66      
67      @BeforeClass
68      static public void beforeClass() throws Exception {
69          System.setErr(SPS);
70          //System.setProperty(SIMPLE_LOGGER_FILE_PROPERTY, targetFile);
71          System.setProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY, "false");
72      }
73  
74      @AfterClass
75      static public void afterClass() {
76          System.setErr(oldErr);
77          System.clearProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY);
78      }
79  
80      @After
81      public void tearDown() {
82          SPS.stringList.clear();
83      }
84      
85      @Test
86      public void smoke() throws IOException {
87          LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
88          assertEquals(EXPECTED_FINDER_CLASS, finder.getClass().getName());
89          Logger systemLogger = finder.getLogger("smoke", null);
90          systemLogger.log(Level.INFO, "hello");
91          systemLogger.log(Level.INFO, "hello %s", "world");
92          
93          List<String> results = SPS.stringList;
94          assertEquals(2, results.size());
95          assertEquals("INFO smoke - hello", results.get(0));
96          assertEquals("INFO smoke - hello world", results.get(1));
97      }
98  
99      @Test
100     public void throwTest() throws IOException {
101         LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
102         assertEquals(EXPECTED_FINDER_CLASS, finder.getClass().getName());
103 
104         Logger systemLogger = finder.getLogger("throwTest", null);
105         systemLogger.log(Level.INFO, "we have a problem", new Exception());
106         
107         List<String> results = SPS.stringList;
108         //INFO throwTest - a problem
109         //java.lang.Exception
110         //        at org.slf4j.jdk.platform.logging/org.slf4j.jdk.platform.logging.SLF4JPlatformLoggingTest.throwTest(SLF4JPlatformLoggingTest.java:92)
111          
112         assertEquals("INFO throwTest - we have a problem", results.get(0));
113         assertEquals(Exception.class.getName(), results.get(1));
114         assertTrue(results.get(2).contains("at "));
115         assertTrue(results.get(2).contains(this.getClass().getName()));
116     }
117 
118 
119 }