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.pattern;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.spi.LoggingEvent;
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Disabled;
23  import org.junit.jupiter.api.Test;
24  
25  import java.net.InetAddress;
26  import java.net.UnknownHostException;
27  import java.util.Arrays;
28  import java.util.Calendar;
29  import java.util.Locale;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  public class SyslogStartConverterTest {
34  
35      private LoggerContext lc;
36      private SyslogStartConverter converter;
37      private final String HOSTNAME = findHostname();
38      private final Calendar calendar = Calendar.getInstance(Locale.US);
39  
40      @BeforeEach
41      public void setUp() throws Exception {
42          lc = new LoggerContext();
43          converter = new SyslogStartConverter();
44          converter.setOptionList(Arrays.asList("local7"));
45          converter.start();
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 datesLessThanTen() {
57          // RFC 3164, section 4.1.2:
58          // If the day of the month is less than 10, then it MUST be represented as
59          // a space and then the number. For example, the 7th day of August would be
60          // represented as "Aug 7", with two spaces between the "g" and the "7".
61          LoggingEvent le = createLoggingEvent();
62          calendar.set(2012, Calendar.AUGUST, 7, 13, 15, 0);
63          le.setTimeStamp(calendar.getTimeInMillis());
64          assertEquals("<191>Aug  7 13:15:00 " + HOSTNAME + " ", converter.convert(le));
65      }
66  
67      @Test
68      public void datesGreaterThanTen() {
69          LoggingEvent le = createLoggingEvent();
70          calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
71          le.setTimeStamp(calendar.getTimeInMillis());
72          assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));
73      }
74  
75      @Test
76      public void multipleConversions() {
77          LoggingEvent le = createLoggingEvent();
78          calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
79          le.setTimeStamp(calendar.getTimeInMillis());
80          assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));
81          assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le));
82  
83          calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 16);
84          le.setTimeStamp(calendar.getTimeInMillis());
85          assertEquals("<191>Oct 11 22:14:16 " + HOSTNAME + " ", converter.convert(le));
86      }
87  
88      @Test
89      public void ignoreDefaultLocale() {
90          Locale originalDefaultLocale = Locale.getDefault();
91          Locale.setDefault(Locale.TRADITIONAL_CHINESE);
92  
93          try {
94              converter.start();
95  
96              LoggingEvent le = createLoggingEvent();
97              calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
98              le.setTimeStamp(calendar.getTimeInMillis());
99              String result = converter.convert(le);
100             assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", result);
101         } finally {
102             Locale.setDefault(originalDefaultLocale);
103         }
104     }
105 
106     @Test
107     @Disabled
108     public void hostnameShouldNotIncludeDomain() throws Exception {
109         // RFC 3164, section 4.1.2:
110         // The Domain Name MUST NOT be included in the HOSTNAME field.
111         String host = HOSTNAME;
112         final int firstPeriod = host.indexOf(".");
113         if (firstPeriod != -1) {
114             host = host.substring(0, firstPeriod);
115         }
116         LoggingEvent le = createLoggingEvent();
117         calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15);
118         le.setTimeStamp(calendar.getTimeInMillis());
119         assertEquals("<191>Oct 11 22:14:15 " + host + " ", converter.convert(le));
120     }
121 
122     private LoggingEvent createLoggingEvent() {
123         return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG,
124                 "test message", null, null);
125     }
126 
127     private static String findHostname() {
128         try {
129             return InetAddress.getLocalHost().getHostName();
130         } catch (UnknownHostException e) {
131             return "UNKNOWN_LOCALHOST";
132         }
133     }
134 }