|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * |
| 4 | + * Copyright 2020 Red Hat, Inc., and individual contributors |
| 5 | + * as indicated by the @author tags. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.slf4j.impl; |
| 21 | + |
| 22 | +import java.util.concurrent.BlockingDeque; |
| 23 | +import java.util.concurrent.LinkedBlockingDeque; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.LogRecord; |
| 26 | + |
| 27 | +import org.jboss.logmanager.ExtHandler; |
| 28 | +import org.jboss.logmanager.ExtLogRecord; |
| 29 | +import org.jboss.logmanager.LogContext; |
| 30 | +import org.jboss.logmanager.LogContextSelector; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | +import org.slf4j.MDC; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author <a href="mailto:[email protected]">James R. Perkins</a> |
| 42 | + */ |
| 43 | +public class LoggerTestCase { |
| 44 | + private static final LogContext LOG_CONTEXT = LogContext.create(); |
| 45 | + private static final java.util.logging.Logger ROOT = LOG_CONTEXT.getLogger(""); |
| 46 | + private static final QueueHandler HANDLER = new QueueHandler(); |
| 47 | + |
| 48 | + private static final LogContextSelector DEFAULT_SELECTOR = LogContext.getLogContextSelector(); |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void configureLogManager() { |
| 52 | + LogContext.setLogContextSelector(() -> LOG_CONTEXT); |
| 53 | + ROOT.addHandler(HANDLER); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + public static void cleanup() throws Exception { |
| 58 | + LOG_CONTEXT.close(); |
| 59 | + LogContext.setLogContextSelector(DEFAULT_SELECTOR); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void clearHandler() { |
| 64 | + HANDLER.close(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testLogger() { |
| 69 | + final Logger logger = LoggerFactory.getLogger(LoggerTestCase.class); |
| 70 | + Assert.assertTrue(expectedTypeMessage(Slf4jLogger.class, logger.getClass()), logger instanceof Slf4jLogger); |
| 71 | + |
| 72 | + // Ensure the logger logs something |
| 73 | + final String testMsg = "This is a test message"; |
| 74 | + logger.info(testMsg); |
| 75 | + ExtLogRecord record = HANDLER.messages.poll(); |
| 76 | + Assert.assertNotNull(record); |
| 77 | + Assert.assertEquals(testMsg, record.getMessage()); |
| 78 | + Assert.assertNull(record.getParameters()); |
| 79 | + |
| 80 | + // Test a formatted message |
| 81 | + logger.info("This is a test formatted {}", "{message}"); |
| 82 | + record = HANDLER.messages.poll(); |
| 83 | + Assert.assertNotNull(record); |
| 84 | + Assert.assertEquals("This is a test formatted {message}", record.getFormattedMessage()); |
| 85 | + Assert.assertArrayEquals("Expected parameter not found.", new Object[] {"{message}"}, record.getParameters()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testLoggerWithExceptions() { |
| 90 | + final Logger logger = LoggerFactory.getLogger(LoggerTestCase.class); |
| 91 | + |
| 92 | + final RuntimeException e = new RuntimeException("Test exception"); |
| 93 | + final String testMsg = "This is a test message"; |
| 94 | + logger.info(testMsg, e); |
| 95 | + LogRecord record = HANDLER.messages.poll(); |
| 96 | + Assert.assertNotNull(record); |
| 97 | + Assert.assertEquals(testMsg, record.getMessage()); |
| 98 | + Assert.assertEquals("Cause is different from the expected cause", e, record.getThrown()); |
| 99 | + |
| 100 | + // Test format with the last parameter being the throwable which should set be set on the record |
| 101 | + logger.info("This is a test formatted {}", "{message}", e); |
| 102 | + record = HANDLER.messages.poll(); |
| 103 | + Assert.assertNotNull(record); |
| 104 | + Assert.assertEquals("This is a test formatted {message}", record.getMessage()); |
| 105 | + Assert.assertEquals("Cause is different from the expected cause", e, record.getThrown()); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testMDC() { |
| 111 | + Assert.assertSame(expectedTypeMessage(Slf4jMDCAdapter.class, MDC.getMDCAdapter().getClass()), MDC.getMDCAdapter().getClass(), Slf4jMDCAdapter.class); |
| 112 | + final String key = Long.toHexString(System.currentTimeMillis()); |
| 113 | + MDC.put(key, "value"); |
| 114 | + Assert.assertEquals("MDC value should be \"value\"", "value", MDC.get(key)); |
| 115 | + Assert.assertEquals("MDC value should be \"value\"", "value", org.jboss.logmanager.MDC.get(key)); |
| 116 | + } |
| 117 | + |
| 118 | + private static String expectedTypeMessage(final Class<?> expected, final Class<?> found) { |
| 119 | + return String.format("Expected type %s but found type %s", expected.getName(), found.getName()); |
| 120 | + } |
| 121 | + |
| 122 | + private static class QueueHandler extends ExtHandler { |
| 123 | + final BlockingDeque<ExtLogRecord> messages = new LinkedBlockingDeque<>(); |
| 124 | + |
| 125 | + @Override |
| 126 | + protected void doPublish(final ExtLogRecord record) { |
| 127 | + messages.add(record); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void flush() { |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void close() throws SecurityException { |
| 136 | + messages.clear(); |
| 137 | + setLevel(Level.ALL); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments