From 75f58103d4cd39199705071d6e710da9d318cca6 Mon Sep 17 00:00:00 2001 From: vb-linetco Date: Mon, 16 Mar 2015 19:59:08 +0100 Subject: [PATCH] avoid IndexOutOfBoundsException when called with 0 return an empty list when called with maxLines = 0 added unit test for getLog(0) returning an empty list --- core/src/main/java/hudson/model/Run.java | 3 +++ core/src/test/java/hudson/model/RunTest.java | 26 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index 8e229fb42a..a8c557c3f2 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -1930,6 +1930,9 @@ public abstract class Run ,RunT extends Run getLog(int maxLines) throws IOException { int lineCount = 0; List logLines = new LinkedList(); + if (maxLines == 0) { + return logLines; + } BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getLogFile()),getCharset())); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { diff --git a/core/src/test/java/hudson/model/RunTest.java b/core/src/test/java/hudson/model/RunTest.java index 0bef642ac3..4f4be5d4aa 100644 --- a/core/src/test/java/hudson/model/RunTest.java +++ b/core/src/test/java/hudson/model/RunTest.java @@ -25,6 +25,9 @@ package hudson.model; import java.io.IOException; +import hudson.model.Run.Artifact; +import java.io.File; +import java.io.PrintWriter; import java.util.List; import java.util.TimeZone; import java.util.concurrent.Callable; @@ -32,11 +35,17 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static org.junit.Assert.*; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.Issue; +import org.mockito.Mockito; + public class RunTest { + @Rule public TemporaryFolder tmp = new TemporaryFolder(); + @Issue("JENKINS-15816") @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void timezoneOfID() throws Exception { @@ -128,4 +137,21 @@ public class RunTest { msg = r.getDurationString(); assertFalse(msg, msg.endsWith(" and counting")); } + + @Issue("JENKINS-27441") + @Test + public void getLogReturnsAnEmptyListWhenCalledWith0() throws Exception { + Job j = Mockito.mock(Job.class); + File tempBuildDir = tmp.newFolder(); + Mockito.when(j.getBuildDir()).thenReturn(tempBuildDir); + Run r = new Run(j, 0) {}; + File f = r.getLogFile(); + f.getParentFile().mkdirs(); + PrintWriter w = new PrintWriter(f, "utf-8"); + w.println("dummy"); + w.close(); + List logLines = r.getLog(0); + assertTrue(logLines.isEmpty()); + } + } -- GitLab