From a91b3c40a2822b8ef4eace4c92895030b16574ef Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 26 Jun 2013 17:58:47 +0200 Subject: [PATCH] SONARPLUGINS-3012 Fix UT on Windows because exit code is not 143 when process is killed --- .../org/sonar/runner/api/ForkedRunner.java | 24 ++++++++++--------- .../sonar/runner/api/CommandExecutorTest.java | 4 +--- .../sonar/runner/api/ForkedRunnerTest.java | 11 +++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 7a35f17..d5e28f9 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -25,6 +25,7 @@ import org.sonar.runner.impl.BatchLauncherMain; import org.sonar.runner.impl.JarExtractor; import javax.annotation.Nullable; + import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; @@ -42,7 +43,6 @@ import java.util.Map; public class ForkedRunner extends Runner { private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000; - private static final int TERMINATED_STATUS = 143; private final Map jvmEnvVariables = new HashMap(); private final List jvmArguments = new ArrayList(); @@ -154,11 +154,11 @@ public class ForkedRunner extends Runner { javaExecutable = new Os().thisJavaExe().getAbsolutePath(); } Command command = Command.builder() - .setExecutable(javaExecutable) - .addEnvVariables(jvmEnvVariables) - .addArguments(jvmArguments) - .addArguments("-cp", jarFile.getAbsolutePath(), BatchLauncherMain.class.getName(), propertiesFile.getAbsolutePath()) - .build(); + .setExecutable(javaExecutable) + .addEnvVariables(jvmEnvVariables) + .addArguments(jvmArguments) + .addArguments("-cp", jarFile.getAbsolutePath(), BatchLauncherMain.class.getName(), propertiesFile.getAbsolutePath()) + .build(); return new ForkCommand(command, jarFile, propertiesFile); } @@ -191,11 +191,13 @@ public class ForkedRunner extends Runner { stdErr = new PrintStreamConsumer(System.err); } int status = commandExecutor.execute(forkCommand.command, stdOut, stdErr, ONE_DAY_IN_MILLISECONDS, processMonitor); - - if (status == TERMINATED_STATUS) { - stdOut.consumeLine(String.format("Sonar runner terminated with exit code %d", status)); - } else if (status != 0) { - throw new IllegalStateException("Error status [command: " + forkCommand.command + "]: " + status); + if (status != 0) { + if (processMonitor.stop()) { + stdOut.consumeLine(String.format("SonarQube Runner was stopped [status=%s]", status)); + } + else { + throw new IllegalStateException("Error status [command: " + forkCommand.command + "]: " + status); + } } } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java index e360795..571a45e 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java @@ -38,8 +38,6 @@ public class CommandExecutorTest { public static final ProcessMonitor ACTIVITY_CONTROLLER = mock(ProcessMonitor.class); - public static final int PROCESS_TERMINATED_CODE = 143; - @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); @@ -162,7 +160,7 @@ public class CommandExecutorTest { }); assertThat(System.currentTimeMillis() - start).isGreaterThan(1000); - assertThat(result).isEqualTo(PROCESS_TERMINATED_CODE); + assertThat(result).isNotEqualTo(0); } @Test diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java index de613e6..d6f7a1f 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java @@ -19,6 +19,7 @@ */ package org.sonar.runner.api; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -46,12 +47,17 @@ public class ForkedRunnerTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private ProcessMonitor processMonitor = mock(ProcessMonitor.class); + private ProcessMonitor processMonitor; private final StreamConsumer out = mock(StreamConsumer.class); private final StreamConsumer err = mock(StreamConsumer.class); + @Before + public void prepare() { + processMonitor = mock(ProcessMonitor.class); + } + @Test public void should_create_forked_runner() { ForkedRunner runner = ForkedRunner.create(); @@ -184,10 +190,11 @@ public class ForkedRunnerTest { @Test public void test_runner_was_requested_to_stop() throws Exception { + when(processMonitor.stop()).thenReturn(true); ForkedRunner runner = new ForkedRunner(createMockExtractor(), createMockRunnerWithExecutionStatus(143), processMonitor); runner.setStdOut(out); runner.setStdErr(err); runner.execute(); - verify(out).consumeLine("Sonar runner terminated with exit code 143"); + verify(out).consumeLine("SonarQube Runner was stopped [status=143]"); } } -- 2.39.5