From 0063d921b56539cfc26c8a15be5c4d0e81f3775f Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Fri, 11 Feb 2022 14:21:55 +0100 Subject: [PATCH] add a test in relation with LOGBACK-1582 Signed-off-by: Ceki Gulcu --- .../pattern/CompositeConverterTest.java | 62 +++++++++++++++++++ .../pattern/EnsureExceptionHandlingTest.java | 13 ++++ 2 files changed, 75 insertions(+) create mode 100644 logback-classic/src/test/java/ch/qos/logback/classic/pattern/CompositeConverterTest.java diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/CompositeConverterTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/CompositeConverterTest.java new file mode 100644 index 0000000000..8005c7199e --- /dev/null +++ b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/CompositeConverterTest.java @@ -0,0 +1,62 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2022, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ +package ch.qos.logback.classic.pattern; + +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.PatternLayout; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.classic.spi.LoggingEvent; +import ch.qos.logback.core.testUtil.StatusChecker; +import ch.qos.logback.core.util.StatusPrinter; + +public class CompositeConverterTest { + + private PatternLayout pl = new PatternLayout(); + private LoggerContext lc = new LoggerContext(); + Logger logger = lc.getLogger(this.getClass()); + StatusChecker sc = new StatusChecker(lc); + + + @Before + public void setUp() { + pl.setContext(lc); + } + + ILoggingEvent makeLoggingEvent(String msg, Exception ex) { + return new LoggingEvent(CompositeConverterTest.class.getName(), logger, Level.INFO, msg, ex, null); + } + + + + @Test + public void testLogback1582() { + // EVAL_REF is searched within the context, if context is not set (== null), then + // a NullPointerException will be thrown + pl.setPattern("%m %replace(%rootException{5, EVAL_REF}){'\\n', 'XYZ'}\""); + pl.start(); + ILoggingEvent le = makeLoggingEvent("assert", new Exception("test")); + + String result = pl.doLayout(le); + sc.assertIsErrorFree(); + assertTrue(result.contains("XYZ")); + } + +} diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/EnsureExceptionHandlingTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/EnsureExceptionHandlingTest.java index 077416c140..fc45c9f735 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/EnsureExceptionHandlingTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/EnsureExceptionHandlingTest.java @@ -1,3 +1,16 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2022, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ package ch.qos.logback.classic.pattern; import org.junit.Before;