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.spi;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  
25  public class LoggingEventTest {
26  
27      LoggerContext loggerContext = new LoggerContext();
28      Logger logger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
29  
30      @BeforeEach
31      public void setUp() {
32      }
33  
34      @Test
35      public void testFormattingOneArg() {
36          String message = "x={}";
37          Throwable throwable = null;
38          Object[] argArray = new Object[] { 12 };
39  
40          LoggingEvent event = new LoggingEvent("", logger, Level.INFO, message, throwable, argArray);
41          assertNull(event.formattedMessage);
42          assertEquals("x=12", event.getFormattedMessage());
43      }
44  
45      @Test
46      public void testFormattingTwoArg() {
47          String message = "{}-{}";
48          Throwable throwable = null;
49          Object[] argArray = new Object[] { 12, 13 };
50          LoggingEvent event = new LoggingEvent("", logger, Level.INFO, message, throwable, argArray);
51  
52          assertNull(event.formattedMessage);
53          assertEquals("12-13", event.getFormattedMessage());
54      }
55  
56      @Test
57      public void testNoFormattingWithArgs() {
58          String message = "testNoFormatting";
59          Throwable throwable = null;
60          Object[] argArray = new Object[] { 12, 13 };
61          LoggingEvent event = new LoggingEvent("", logger, Level.INFO, message, throwable, argArray);
62          assertNull(event.formattedMessage);
63          assertEquals(message, event.getFormattedMessage());
64      }
65  
66      @Test
67      public void testNoFormattingWithoutArgs() {
68          String message = "testNoFormatting";
69          Throwable throwable = null;
70          Object[] argArray = null;
71          LoggingEvent event = new LoggingEvent("", logger, Level.INFO, message, throwable, argArray);
72          assertNull(event.formattedMessage);
73          assertEquals(message, event.getFormattedMessage());
74      }
75  }