001/**
002 * Copyright (c) 2004-2021 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.jdk.platform.logging.test;
026
027import static org.junit.Assert.assertEquals;
028import static org.junit.Assert.assertTrue;
029
030import java.io.IOException;
031import java.io.PrintStream;
032import java.lang.System.Logger;
033import java.lang.System.Logger.Level;
034import java.lang.System.LoggerFinder;
035import java.util.List;
036import java.util.Random;
037
038import org.junit.After;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.Test;
042
043/**
044 * The present test is fragile in the sense that it sets up SimpleLogger
045 * with a StringPrintStream and reverts to the old stream when done.
046 * 
047 * Any tests running simultaneously (and using SimpleLogger) will be affected 
048 * by this. Moreover, since SimpleLogger is initialized by the call to LoggerFactory
049 * and tests also using LoggerFactory will also be affected.
050 * 
051 * @author Ceki Gülcü
052 *
053 */
054public class SLF4JPlatformLoggingTest {
055
056    static final String PREFIX = "org.slf4j.simpleLogger.";
057    static final String SIMPLE_LOGGER_FILE_PROPERTY = PREFIX + "logFile";
058    static final String SIMPLE_LOGGER_THREAD_NAME_PROPERTY = PREFIX + "showThreadName";
059    
060    static final String EXPECTED_FINDER_CLASS = "org.slf4j.jdk.platform.logging.SLF4JSystemLoggerFinder";
061    
062    static int diff = new Random().nextInt(100*1000*1000);
063   
064    static final PrintStream oldErr = System.err;
065    static StringPrintStream SPS = new StringPrintStream(oldErr, false);
066    
067    @BeforeClass
068    static public void beforeClass() throws Exception {
069        System.setErr(SPS);
070        //System.setProperty(SIMPLE_LOGGER_FILE_PROPERTY, targetFile);
071        System.setProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY, "false");
072    }
073
074    @AfterClass
075    static public void afterClass() {
076        System.setErr(oldErr);
077        System.clearProperty(SIMPLE_LOGGER_THREAD_NAME_PROPERTY);
078    }
079
080    @After
081    public void tearDown() {
082        SPS.stringList.clear();
083    }
084    
085    @Test
086    public void smoke() throws IOException {
087        LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
088        assertEquals(EXPECTED_FINDER_CLASS, finder.getClass().getName());
089        Logger systemLogger = finder.getLogger("smoke", null);
090        systemLogger.log(Level.INFO, "hello");
091        systemLogger.log(Level.INFO, "hello %s", "world");
092        
093        List<String> results = SPS.stringList;
094        assertEquals(2, results.size());
095        assertEquals("INFO smoke - hello", results.get(0));
096        assertEquals("INFO smoke - hello world", results.get(1));
097    }
098
099    @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}