Hopefully fix failing tests (due to timezone differences)

This commit is contained in:
Mark Nellemann 2021-01-27 20:31:25 +01:00
parent 6fc55f14cb
commit 49b8888032
6 changed files with 19 additions and 14 deletions

View File

@ -28,7 +28,7 @@ public abstract class SyslogParser {
public abstract SyslogMessage parse(final String input); public abstract SyslogMessage parse(final String input);
abstract Instant parseTimestamp(final String dateString); public abstract Instant parseTimestamp(final String dateString);
/** /**

View File

@ -32,7 +32,7 @@ public class SyslogParserRfc3164 extends SyslogParser {
private final static Logger log = LoggerFactory.getLogger(SyslogParserRfc3164.class); private final static Logger log = LoggerFactory.getLogger(SyslogParserRfc3164.class);
private final Pattern pattern = Pattern.compile("^<(\\d{1,3})>(\\D{3}\\s+\\d{1,2} \\d{2}:\\d{2}:\\d{2})\\s+(Message forwarded from \\S+:|\\S+)\\s+([^\\s:]+):?\\s+(.*)", Pattern.CASE_INSENSITIVE); private final Pattern pattern = Pattern.compile("^<(\\d{1,3})>(\\D{3}\\s+\\d{1,2} \\d{2}:\\d{2}:\\d{2})\\s+(Message forwarded from \\S+:|\\S+)\\s+([^\\s:]+):?\\s+(.*)", Pattern.CASE_INSENSITIVE);
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy MMM [ ]d HH:mm:ss").withZone(ZoneId.systemDefault()); //.withZone(ZoneOffset.UTC); private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy MMM [ ]d HH:mm:ss").withZone(ZoneId.systemDefault());
/** /**
* Parses [rfc3164](https://tools.ietf.org/html/rfc3164) syslog messages. * Parses [rfc3164](https://tools.ietf.org/html/rfc3164) syslog messages.
@ -84,7 +84,7 @@ public class SyslogParserRfc3164 extends SyslogParser {
* @param dateString * @param dateString
* @return * @return
*/ */
protected Instant parseTimestamp(String dateString) { public Instant parseTimestamp(String dateString) {
// We need to add year to parse date correctly // We need to add year to parse date correctly
OffsetDateTime odt = OffsetDateTime.now(); OffsetDateTime odt = OffsetDateTime.now();

View File

@ -93,7 +93,7 @@ public class SyslogParserRfc5424 extends SyslogParser {
* @param dateString * @param dateString
* @return * @return
*/ */
protected Instant parseTimestamp(String dateString) { public Instant parseTimestamp(String dateString) {
/* /*
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

View File

@ -4,8 +4,12 @@ import biz.nellemann.syslogd.msg.SyslogMessage
import biz.nellemann.syslogd.parser.SyslogParser import biz.nellemann.syslogd.parser.SyslogParser
import biz.nellemann.syslogd.parser.SyslogParserRfc3164 import biz.nellemann.syslogd.parser.SyslogParserRfc3164
import spock.lang.Specification import spock.lang.Specification
import java.text.DateFormat
import java.time.Instant import java.time.Instant
import java.time.OffsetDateTime; import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter;
class SyslogParserRfc3164Test extends Specification { class SyslogParserRfc3164Test extends Specification {
@ -97,13 +101,15 @@ class SyslogParserRfc3164Test extends Specification {
void "test parseRfc3164Timestamp"() { void "test parseRfc3164Timestamp"() {
setup: setup:
OffsetDateTime odt = OffsetDateTime.now();
String dateString = "Sep 12 20:50:13" String dateString = "Sep 12 20:50:13"
when: when:
Instant inst = syslogParser.parseTimestamp(dateString) Instant instant = syslogParser.parseTimestamp(dateString)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY MMM dd HH:mm:ss").withZone(ZoneId.systemDefault());
then: then:
inst.epochSecond == 1631472613 assert formatter.format(instant).equals(odt.getYear() + " " + dateString);
} }
} }

View File

@ -67,8 +67,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString) Instant inst = syslogParser.parseTimestamp(dateString)
then: then:
inst.toString() == "1985-04-12T21:20:50.052Z"
inst.toEpochMilli() == 482188850052 inst.toEpochMilli() == 482188850052
//inst.toString() == "1985-04-12T21:20:50.052Z"
} }
void "test parseRfc5424Timestamp ex2"() { void "test parseRfc5424Timestamp ex2"() {
@ -79,8 +79,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString) Instant inst = syslogParser.parseTimestamp(dateString)
then: then:
inst.toString() == "1985-04-12T23:20:50.052Z"
inst.toEpochMilli() == 482196050052 inst.toEpochMilli() == 482196050052
//inst.toString() == "1985-04-12T23:20:50.052Z"
} }
void "test parseRfc5424Timestamp ex3"() { void "test parseRfc5424Timestamp ex3"() {
@ -91,8 +91,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString) Instant inst = syslogParser.parseTimestamp(dateString)
then: then:
inst.toString() == "2003-10-11T20:14:15.003Z"
inst.toEpochMilli() == 1065903255003 inst.toEpochMilli() == 1065903255003
//inst.toString() == "2003-10-11T20:14:15.003Z"
} }
void "test parseRfc5424Timestamp ex4"() { void "test parseRfc5424Timestamp ex4"() {
@ -103,8 +103,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString) Instant inst = syslogParser.parseTimestamp(dateString)
then: then:
inst.toString() == "2003-08-24T12:14:15.003Z"
inst.toEpochMilli() == 1061727255003 inst.toEpochMilli() == 1061727255003
//inst.toString() == "2003-08-24T12:14:15.003Z"
} }
void "test parseRfc5424Timestamp ex5"() { void "test parseRfc5424Timestamp ex5"() {
@ -115,11 +115,10 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString) Instant inst = syslogParser.parseTimestamp(dateString)
then: then:
inst.toString() == "2003-08-24T12:14:15.003Z"
inst.toEpochMilli() == 1061727255003 inst.toEpochMilli() == 1061727255003
//inst.toString() == "2003-08-24T12:14:15.003Z"
} }
} }

View File

@ -1,4 +1,4 @@
org.slf4j.simpleLogger.logFile=System.err org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.showDateTime=false org.slf4j.simpleLogger.showDateTime=false
org.slf4j.simpleLogger.showShortLogName=true org.slf4j.simpleLogger.showShortLogName=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS