提交 75f58103 编写于 作者: V vb-linetco

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
上级 38c2f98f
...@@ -1930,6 +1930,9 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run ...@@ -1930,6 +1930,9 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
public @Nonnull List<String> getLog(int maxLines) throws IOException { public @Nonnull List<String> getLog(int maxLines) throws IOException {
int lineCount = 0; int lineCount = 0;
List<String> logLines = new LinkedList<String>(); List<String> logLines = new LinkedList<String>();
if (maxLines == 0) {
return logLines;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getLogFile()),getCharset())); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getLogFile()),getCharset()));
try { try {
for (String line = reader.readLine(); line != null; line = reader.readLine()) { for (String line = reader.readLine(); line != null; line = reader.readLine()) {
......
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
package hudson.model; package hudson.model;
import java.io.IOException; import java.io.IOException;
import hudson.model.Run.Artifact;
import java.io.File;
import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
...@@ -32,11 +35,17 @@ import java.util.concurrent.ExecutorService; ...@@ -32,11 +35,17 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.Issue;
import org.mockito.Mockito;
public class RunTest { public class RunTest {
@Rule public TemporaryFolder tmp = new TemporaryFolder();
@Issue("JENKINS-15816") @Issue("JENKINS-15816")
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
@Test public void timezoneOfID() throws Exception { @Test public void timezoneOfID() throws Exception {
...@@ -128,4 +137,21 @@ public class RunTest { ...@@ -128,4 +137,21 @@ public class RunTest {
msg = r.getDurationString(); msg = r.getDurationString();
assertFalse(msg, msg.endsWith(" and counting")); 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<String> logLines = r.getLog(0);
assertTrue(logLines.isEmpty());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册