PatternLayoutEncoderTest.java

/*
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v2.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.encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import ch.qos.logback.classic.PatternLayout;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PatternLayoutEncoderTest {

    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    LoggerContext context = new LoggerContext();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Logger logger = context.getLogger(PatternLayoutEncoderTest.class);
    Charset utf8Charset = Charset.forName("UTF-8");

    @BeforeEach
    public void setUp() {
        ple.setPattern("%m");
        ple.setContext(context);
    }

    ILoggingEvent makeLoggingEvent(String message) {
        return new LoggingEvent("", logger, Level.DEBUG, message, null, null);
    }

    @Test
    public void smoke() throws IOException {
        init(baos);
        String msg = "hello";
        ILoggingEvent event = makeLoggingEvent(msg);
        byte[] eventBytes = ple.encode(event);
        baos.write(eventBytes);
        ple.footerBytes();
        assertEquals(msg, baos.toString());
    }

    void init(ByteArrayOutputStream baos) throws IOException {
        ple.start();
        ((PatternLayout) ple.getLayout()).setOutputPatternAsHeader(false);
        byte[] header = ple.headerBytes();
        baos.write(header);
    }

    @Test
    public void charset() throws IOException {
        ple.setCharset(utf8Charset);
        init(baos);
        String msg = "\u03b1";
        ILoggingEvent event = makeLoggingEvent(msg);
        byte[] eventBytes = ple.encode(event);
        baos.write(eventBytes);
        ple.footerBytes();
        assertEquals(msg, new String(baos.toByteArray(), utf8Charset));
    }

    @Test
    public void isStarted() throws IOException {
        assertTrue(!ple.isStarted());
        ple.start();
        assertTrue(ple.isStarted());
    }
}