Skip to content

Commit

Permalink
Fix off-by-one error in TimestampsReader.getAbs
Browse files Browse the repository at this point in the history
Unlikely to cause problems when running Jenkins, but it made it more difficult
to write the unit tests.
  • Loading branch information
StevenGBrown committed Sep 19, 2016
1 parent 47dffcf commit 5a5e7b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ public void testStartOfLogFile() throws Exception {
assertThat(annotate(), is(timestamps));
}

/**
* @throws Exception
*/
@Test
public void testStartOfLogFile_negativeLineNumber() throws Exception {
List<Timestamp> timestamps = writeTimestamps(2);
logPosition.lineNumber = -2;
logPosition.atNewLine = true;
assertThat(annotate(), is(timestamps));
}

/**
* @throws Exception
*/
Expand All @@ -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<Timestamp> timestamps = writeTimestamps(2);
logPosition.lineNumber = -2;
logPosition.atNewLine = false;
assertThat(annotate(), is(timestamps.subList(1, 2)));
}

/**
* @throws Exception
*/
Expand All @@ -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<Timestamp> timestamps = writeTimestamps(2);
logPosition.lineNumber = -1;
logPosition.atNewLine = true;
assertThat(annotate(), is(timestamps.subList(1, 2)));
}

/**
* @throws Exception
*/
Expand Down

0 comments on commit 5a5e7b9

Please sign in to comment.