View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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  package ch.qos.logback.classic.encoder;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  import java.nio.charset.Charset;
19  
20  import ch.qos.logback.classic.PatternLayout;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  
24  import ch.qos.logback.classic.Level;
25  import ch.qos.logback.classic.Logger;
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.spi.ILoggingEvent;
28  import ch.qos.logback.classic.spi.LoggingEvent;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  public class PatternLayoutEncoderTest {
34  
35      PatternLayoutEncoder ple = new PatternLayoutEncoder();
36      LoggerContext context = new LoggerContext();
37      ByteArrayOutputStream baos = new ByteArrayOutputStream();
38      Logger logger = context.getLogger(PatternLayoutEncoderTest.class);
39      Charset utf8Charset = Charset.forName("UTF-8");
40  
41      @BeforeEach
42      public void setUp() {
43          ple.setPattern("%m");
44          ple.setContext(context);
45      }
46  
47      ILoggingEvent makeLoggingEvent(String message) {
48          return new LoggingEvent("", logger, Level.DEBUG, message, null, null);
49      }
50  
51      @Test
52      public void smoke() throws IOException {
53          init(baos);
54          String msg = "hello";
55          ILoggingEvent event = makeLoggingEvent(msg);
56          byte[] eventBytes = ple.encode(event);
57          baos.write(eventBytes);
58          ple.footerBytes();
59          assertEquals(msg, baos.toString());
60      }
61  
62      void init(ByteArrayOutputStream baos) throws IOException {
63          ple.start();
64          ((PatternLayout) ple.getLayout()).setOutputPatternAsHeader(false);
65          byte[] header = ple.headerBytes();
66          baos.write(header);
67      }
68  
69      @Test
70      public void charset() throws IOException {
71          ple.setCharset(utf8Charset);
72          init(baos);
73          String msg = "\u03b1";
74          ILoggingEvent event = makeLoggingEvent(msg);
75          byte[] eventBytes = ple.encode(event);
76          baos.write(eventBytes);
77          ple.footerBytes();
78          assertEquals(msg, new String(baos.toByteArray(), utf8Charset));
79      }
80  
81      @Test
82      public void isStarted() throws IOException {
83          assertTrue(!ple.isStarted());
84          ple.start();
85          assertTrue(ple.isStarted());
86      }
87  }