1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
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
110
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 }