|
| 1 | +/* |
| 2 | + * Logback: the reliable, generic, fast and flexible logging framework. |
| 3 | + * Copyright (C) 1999-2024, 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.pattern; |
| 16 | + |
| 17 | +import ch.qos.logback.classic.Level; |
| 18 | +import ch.qos.logback.classic.Logger; |
| 19 | +import ch.qos.logback.classic.LoggerContext; |
| 20 | +import ch.qos.logback.classic.spi.LoggingEvent; |
| 21 | +import ch.qos.logback.core.status.Status; |
| 22 | +import ch.qos.logback.core.status.testUtil.StatusChecker; |
| 23 | +import ch.qos.logback.core.util.StatusPrinter2; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.slf4j.event.KeyValuePair; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | + |
| 33 | +public class MaskedKeyValuePairConverterTest { |
| 34 | + |
| 35 | + LoggerContext lc = new LoggerContext(); |
| 36 | + MaskedKeyValuePairConverter converter; |
| 37 | + LoggingEvent event; |
| 38 | + |
| 39 | + StatusChecker statusChecker = new StatusChecker(lc); |
| 40 | + StatusPrinter2 statusPrinter2 = new StatusPrinter2(); |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + public void setUp() throws Exception { |
| 44 | + converter = new MaskedKeyValuePairConverter(); |
| 45 | + converter.setContext(lc); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public void tearDown() throws Exception { |
| 50 | + lc = null; |
| 51 | + converter.stop(); |
| 52 | + converter = null; |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void smoke() { |
| 57 | + event = createLoggingEvent(); |
| 58 | + converter.setOptionList(List.of("k1")); |
| 59 | + converter.start(); |
| 60 | + |
| 61 | + event.addKeyValuePair(new KeyValuePair("k1", "v1")); |
| 62 | + event.addKeyValuePair(new KeyValuePair("k2", "v2")); |
| 63 | + |
| 64 | + String result = converter.convert(event); |
| 65 | + assertEquals("k1=\""+MaskedKeyValuePairConverter.MASK+"\" k2=\"v2\"", result); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void smokeSingle() { |
| 70 | + event = createLoggingEvent(); |
| 71 | + converter.setOptionList(List.of("SINGLE", "k1")); |
| 72 | + converter.start(); |
| 73 | + |
| 74 | + event.addKeyValuePair(new KeyValuePair("k1", "v1")); |
| 75 | + event.addKeyValuePair(new KeyValuePair("k2", "v2")); |
| 76 | + |
| 77 | + String result = converter.convert(event); |
| 78 | + assertEquals("k1='"+MaskedKeyValuePairConverter.MASK+"' k2='v2'", result); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void wrongOrder() { |
| 83 | + event = createLoggingEvent(); |
| 84 | + converter.setOptionList(List.of("k1", "SINGLE")); |
| 85 | + converter.start(); |
| 86 | + |
| 87 | + event.addKeyValuePair(new KeyValuePair("k1", "v1")); |
| 88 | + event.addKeyValuePair(new KeyValuePair("k2", "v2")); |
| 89 | + |
| 90 | + statusPrinter2.print(lc); |
| 91 | + statusChecker.assertContainsMatch(Status.WARN, "extra quote spec SINGLE found in the wrong order"); |
| 92 | + String result = converter.convert(event); |
| 93 | + assertEquals("k1=\""+MaskedKeyValuePairConverter.MASK+"\" k2=\"v2\"", result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testWithOnelKVP() { |
| 98 | + event = createLoggingEvent(); |
| 99 | + converter.setOptionList(List.of("k")); |
| 100 | + converter.start(); |
| 101 | + event.addKeyValuePair(new KeyValuePair("k", "v")); |
| 102 | + String result = converter.convert(event); |
| 103 | + assertEquals("k=\""+MaskedKeyValuePairConverter.MASK+"\"", result); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + private LoggingEvent createLoggingEvent() { |
| 109 | + LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), |
| 110 | + Level.DEBUG, "test message", null, null); |
| 111 | + return le; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments