001package org.slf4j.jul;
002
003import static org.junit.Assert.assertEquals;
004
005import java.util.function.Supplier;
006import java.util.logging.Handler;
007import java.util.logging.Level;
008import java.util.logging.LogRecord;
009
010import org.junit.After;
011import org.junit.Assert;
012import org.junit.Before;
013import org.junit.Test;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017public class FluentApiInvocationTest {
018
019    ListHandler listHandler = new ListHandler();
020    java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
021    Level oldLevel;
022    Logger logger = LoggerFactory.getLogger(this.getClass());
023
024    @Before
025    public void setUp() throws Exception {
026        oldLevel = root.getLevel();
027        root.setLevel(Level.FINE);
028        // removeAllHandlers(root);
029        root.addHandler(listHandler);
030    }
031
032    @After
033    public void tearDown() throws Exception {
034        root.setLevel(oldLevel);
035        removeListHandlers(root);
036    }
037
038    void removeListHandlers(java.util.logging.Logger logger) {
039        Handler[] handlers = logger.getHandlers();
040        for (Handler h : handlers) {
041            if (h instanceof ListHandler)
042                logger.removeHandler(h);
043        }
044    }
045
046    @Test
047    public void singleMessage() {
048        String msg = "Hello world.";
049        logger.atDebug().log(msg);
050        assertLogMessage(msg, 0);
051    }
052
053    @Test
054    public void messageWithArguments() {
055        String msg = "Hello {}.";
056        logger.atDebug().addArgument("world").log(msg);
057        assertLogMessage("Hello world.", 0);
058    }
059
060    @Test
061    public void messageWithTwoArguments() {
062        int old = 15;
063        int t = 16;
064
065        {
066            String msg = "Temperature set to {}. Old temperature was {}.";
067            logger.atDebug().addArgument(t).addArgument(old).log(msg);
068            assertLogMessage("Temperature set to 16. Old temperature was 15.", 0);
069        }
070
071        {
072            String msg = "Temperature set to {}. Old temperature was {}.";
073            logger.atDebug().log(msg, t, old);
074            assertLogMessage("Temperature set to 16. Old temperature was 15.", 0);
075        }
076
077        {
078            String msg = "Temperature set to {}. Old temperature was {}.";
079            logger.atDebug().addArgument(t).log(msg, old);
080            assertLogMessage("Temperature set to 16. Old temperature was 15.", 0);
081        }
082
083        {
084            String msg = "Temperature set to {}. Old temperature was {}.";
085            logger.atDebug().addArgument(() -> t16()).log(msg, old);
086            assertLogMessage("Temperature set to 16. Old temperature was 15.", 0);
087        }
088    }
089
090    @Test
091    public void supplierArguments() {
092        Supplier<String> stringSupplier = () -> "world";
093        logger.atInfo().addArgument(stringSupplier).log("hello {}");
094        assertLogMessage("hello world", 0);
095    }
096
097    public int t16() {
098        return 16;
099    }
100
101    @Test
102    public void messageWithThrowable() {
103        String msg = "Hello world.";
104        Throwable t = new IllegalStateException();
105        logger.atDebug().setCause(t).log(msg);
106        assertLogMessage("Hello world.", 0);
107        assertThrowable(t, 0);
108    }
109
110    @Test
111    public void messageWithArgumentsAndThrowable() {
112        String msg = "Hello {}.";
113        Throwable t = new IllegalStateException();
114
115        logger.atDebug().setCause(t).addArgument("world").log(msg);
116        assertLogMessage("Hello world.", 0);
117        assertThrowable(t, 0);
118    }
119
120    @Test
121    public void messageWithKeyValuePair() {
122        String msg = "Hello world.";
123        logger.atDebug().addKeyValue("k", "v").log(msg);
124        assertLogMessage("k=v Hello world.", 0);
125
126        int oldT = 15;
127        int newT = 16;
128        logger.atDebug().addKeyValue("oldT", oldT).addKeyValue("newT", newT).log("Temperature changed.");
129        assertLogMessage("oldT=15 newT=16 Temperature changed.", 1);
130
131    }
132
133    private void assertLogMessage(String expected, int index) {
134        LogRecord logRecord = listHandler.recordList.get(index);
135        Assert.assertNotNull(logRecord);
136        assertEquals(expected, logRecord.getMessage());
137    }
138
139    private void assertThrowable(Throwable expected, int index) {
140        LogRecord logRecord = listHandler.recordList.get(index);
141        Assert.assertNotNull(logRecord);
142        assertEquals(expected, logRecord.getThrown());
143    }
144}