diff --git a/src/main/java/hudson/plugins/timestamper/annotator/ConsoleLogParser.java b/src/main/java/hudson/plugins/timestamper/annotator/ConsoleLogParser.java index 493c8390..2e22d713 100644 --- a/src/main/java/hudson/plugins/timestamper/annotator/ConsoleLogParser.java +++ b/src/main/java/hudson/plugins/timestamper/annotator/ConsoleLogParser.java @@ -114,6 +114,7 @@ private ConsoleLogParser.Result parseFromStart(InputStream inputStream, private ConsoleLogParser.Result parseFromFinish(InputStream inputStream) throws IOException { ConsoleLogParser.Result result = new ConsoleLogParser.Result(); + result.lineNumber = -1; int value = inputStream.read(); result.atNewLine = isNewLine(value); diff --git a/src/main/java/hudson/plugins/timestamper/io/TimestampsReader.java b/src/main/java/hudson/plugins/timestamper/io/TimestampsReader.java index 8c14f45a..2feae51b 100644 --- a/src/main/java/hudson/plugins/timestamper/io/TimestampsReader.java +++ b/src/main/java/hudson/plugins/timestamper/io/TimestampsReader.java @@ -104,7 +104,7 @@ public void skip(int count) throws IOException { public int getAbs(int lineNumber) throws IOException { int toSkip = lineNumber * (-1); - for (int i = 0; i <= toSkip; i++) { + for (int i = 0; i < toSkip; i++) { read(); } diff --git a/src/test/java/hudson/plugins/timestamper/annotator/ConsoleLogParserTest.java b/src/test/java/hudson/plugins/timestamper/annotator/ConsoleLogParserTest.java index c3f6779b..56c0493f 100644 --- a/src/test/java/hudson/plugins/timestamper/annotator/ConsoleLogParserTest.java +++ b/src/test/java/hudson/plugins/timestamper/annotator/ConsoleLogParserTest.java @@ -185,7 +185,7 @@ public void testSeekWithinLineNegative_isBuilding() throws Exception { public void testSeekWithinLineNegative_notBuilding() throws Exception { assumeThat(isBuilding, is(false)); ConsoleLogParser.Result result = new ConsoleLogParser.Result(); - result.lineNumber = -4; + result.lineNumber = -5; assertThat(seek(1 - logLength), is(result)); } @@ -208,7 +208,7 @@ public void testSeekNextLineNegative_isBuilding() throws Exception { public void testSeekNextLineNegative_notBuilding() throws Exception { assumeThat(isBuilding, is(false)); ConsoleLogParser.Result result = new ConsoleLogParser.Result(); - result.lineNumber = -3; + result.lineNumber = -4; result.atNewLine = true; assertThat(seek(2 - logLength), is(result)); } diff --git a/src/test/java/hudson/plugins/timestamper/annotator/TimestampAnnotatorTest.java b/src/test/java/hudson/plugins/timestamper/annotator/TimestampAnnotatorTest.java index 866e88e1..2583e2bb 100644 --- a/src/test/java/hudson/plugins/timestamper/annotator/TimestampAnnotatorTest.java +++ b/src/test/java/hudson/plugins/timestamper/annotator/TimestampAnnotatorTest.java @@ -128,6 +128,17 @@ public void testStartOfLogFile() throws Exception { assertThat(annotate(), is(timestamps)); } + /** + * @throws Exception + */ + @Test + public void testStartOfLogFile_negativeLineNumber() throws Exception { + List timestamps = writeTimestamps(2); + logPosition.lineNumber = -2; + logPosition.atNewLine = true; + assertThat(annotate(), is(timestamps)); + } + /** * @throws Exception */ @@ -139,6 +150,17 @@ public void testWithinFirstLine() throws Exception { assertThat(annotate(), is(timestamps.subList(1, 2))); } + /** + * @throws Exception + */ + @Test + public void testWithinFirstLine_negativeLineNumber() throws Exception { + List timestamps = writeTimestamps(2); + logPosition.lineNumber = -2; + logPosition.atNewLine = false; + assertThat(annotate(), is(timestamps.subList(1, 2))); + } + /** * @throws Exception */ @@ -150,6 +172,17 @@ public void testNextLine() throws Exception { assertThat(annotate(), is(timestamps.subList(1, 2))); } + /** + * @throws Exception + */ + @Test + public void testNextLine_negativeLineNumber() throws Exception { + List timestamps = writeTimestamps(2); + logPosition.lineNumber = -1; + logPosition.atNewLine = true; + assertThat(annotate(), is(timestamps.subList(1, 2))); + } + /** * @throws Exception */