|
| 1 | +/* |
| 2 | + * Logback: the reliable, generic, fast and flexible logging framework. |
| 3 | + * Copyright (C) 1999-2022, QOS.ch. All rights reserved. |
| 4 | + * |
| 5 | + * This program and the accompanying materials are dual-licensed under |
| 6 | + * either the terms of the Eclipse Public License v1.0 as published by |
| 7 | + * the Eclipse Foundation |
| 8 | + * |
| 9 | + * or (per the licensee's choosing) |
| 10 | + * |
| 11 | + * under the terms of the GNU Lesser General Public License version 2.1 |
| 12 | + * as published by the Free Software Foundation. |
| 13 | + */ |
| 14 | + |
| 15 | +package ch.qos.logback.classic.blackbox.joran; |
| 16 | + |
| 17 | +import ch.qos.logback.classic.Logger; |
| 18 | +import ch.qos.logback.classic.LoggerContext; |
| 19 | +import ch.qos.logback.classic.blackbox.BlackboxClassicTestConstants; |
| 20 | +import ch.qos.logback.classic.joran.JoranConfigurator; |
| 21 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 22 | +import ch.qos.logback.core.joran.spi.JoranException; |
| 23 | +import ch.qos.logback.core.testUtil.RandomUtil; |
| 24 | +import ch.qos.logback.core.testUtil.StringListAppender; |
| 25 | +import org.junit.jupiter.api.Disabled; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +public class BlackboxJoranConfiguratorTest { |
| 33 | + LoggerContext loggerContext = new LoggerContext(); |
| 34 | + Logger logger = loggerContext.getLogger(this.getClass().getName()); |
| 35 | + Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); |
| 36 | + //StatusChecker checker = new StatusChecker(loggerContext); |
| 37 | + int diff = RandomUtil.getPositiveInt(); |
| 38 | + |
| 39 | + void configure(String file) throws JoranException { |
| 40 | + JoranConfigurator jc = new JoranConfigurator(); |
| 41 | + jc.setContext(loggerContext); |
| 42 | + loggerContext.putProperty("diff", "" + diff); |
| 43 | + jc.doConfigure(file); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void eval() throws JoranException { |
| 49 | + configure(BlackboxClassicTestConstants.JORAN_INPUT_PREFIX + "callerData.xml"); |
| 50 | + String msg = "hello world"; |
| 51 | + logger.debug("toto"); |
| 52 | + logger.debug(msg); |
| 53 | + |
| 54 | + StringListAppender<ILoggingEvent> slAppender = (StringListAppender<ILoggingEvent>) loggerContext |
| 55 | + .getLogger("root").getAppender("STR_LIST"); |
| 56 | + assertNotNull(slAppender); |
| 57 | + assertEquals(2, slAppender.strList.size()); |
| 58 | + assertTrue(slAppender.strList.get(0).contains(" DEBUG - toto")); |
| 59 | + |
| 60 | + String str1 = slAppender.strList.get(1); |
| 61 | + assertTrue(str1.contains("Caller+0")); |
| 62 | + assertTrue(str1.contains(" DEBUG - hello world")); |
| 63 | + } |
| 64 | +} |
0 commit comments