001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.pattern; 015 016import ch.qos.logback.classic.Level; 017import ch.qos.logback.classic.Logger; 018import ch.qos.logback.classic.LoggerContext; 019import ch.qos.logback.classic.spi.LoggingEvent; 020import org.junit.After; 021import org.junit.Before; 022import org.junit.Ignore; 023import org.junit.Test; 024 025import java.net.InetAddress; 026import java.net.UnknownHostException; 027import java.util.Arrays; 028import java.util.Calendar; 029import java.util.Locale; 030 031import static org.junit.Assert.assertEquals; 032 033public class SyslogStartConverterTest { 034 035 private LoggerContext lc; 036 private SyslogStartConverter converter; 037 private final String HOSTNAME = findHostname(); 038 private final Calendar calendar = Calendar.getInstance(Locale.US); 039 040 @Before 041 public void setUp() throws Exception { 042 lc = new LoggerContext(); 043 converter = new SyslogStartConverter(); 044 converter.setOptionList(Arrays.asList("local7")); 045 converter.start(); 046 } 047 048 @After 049 public void tearDown() throws Exception { 050 lc = null; 051 converter.stop(); 052 converter = null; 053 } 054 055 @Test 056 public void datesLessThanTen() { 057 // RFC 3164, section 4.1.2: 058 // If the day of the month is less than 10, then it MUST be represented as 059 // a space and then the number. For example, the 7th day of August would be 060 // represented as "Aug 7", with two spaces between the "g" and the "7". 061 LoggingEvent le = createLoggingEvent(); 062 calendar.set(2012, Calendar.AUGUST, 7, 13, 15, 0); 063 le.setTimeStamp(calendar.getTimeInMillis()); 064 assertEquals("<191>Aug 7 13:15:00 " + HOSTNAME + " ", converter.convert(le)); 065 } 066 067 @Test 068 public void datesGreaterThanTen() { 069 LoggingEvent le = createLoggingEvent(); 070 calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15); 071 le.setTimeStamp(calendar.getTimeInMillis()); 072 assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le)); 073 } 074 075 @Test 076 public void multipleConversions() { 077 LoggingEvent le = createLoggingEvent(); 078 calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15); 079 le.setTimeStamp(calendar.getTimeInMillis()); 080 assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le)); 081 assertEquals("<191>Oct 11 22:14:15 " + HOSTNAME + " ", converter.convert(le)); 082 083 calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 16); 084 le.setTimeStamp(calendar.getTimeInMillis()); 085 assertEquals("<191>Oct 11 22:14:16 " + HOSTNAME + " ", converter.convert(le)); 086 } 087 088 @Test 089 public void ignoreDefaultLocale() { 090 Locale originalDefaultLocale = Locale.getDefault(); 091 Locale.setDefault(Locale.TRADITIONAL_CHINESE); 092 093 try { 094 converter.start(); 095 096 LoggingEvent le = createLoggingEvent(); 097 calendar.set(2012, Calendar.OCTOBER, 11, 22, 14, 15); 098 le.setTimeStamp(calendar.getTimeInMillis()); 099 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 @Ignore 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, "test message", null, null); 124 } 125 126 private static String findHostname() { 127 try { 128 return InetAddress.getLocalHost().getHostName(); 129 } catch (UnknownHostException e) { 130 return "UNKNOWN_LOCALHOST"; 131 } 132 } 133}