]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-3012 Fix UT on Windows because exit code is not 143
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 26 Jun 2013 15:58:47 +0000 (17:58 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 26 Jun 2013 15:58:47 +0000 (17:58 +0200)
when process is killed

sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
sonar-runner-api/src/test/java/org/sonar/runner/api/CommandExecutorTest.java
sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java

index 7a35f17db62e6930aabe8865d8737340878779ad..d5e28f99347a33f4ee7fe25f6e148d07d96d8fd5 100644 (file)
@@ -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<ForkedRunner> {
 
   private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
-  private static final int TERMINATED_STATUS = 143;
 
   private final Map<String, String> jvmEnvVariables = new HashMap<String, String>();
   private final List<String> jvmArguments = new ArrayList<String>();
@@ -154,11 +154,11 @@ public class ForkedRunner extends Runner<ForkedRunner> {
       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<ForkedRunner> {
       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);
+      }
     }
   }
 
index e3607952e21ed58c2ce871b8d814f1f6368b1046..571a45eeea95c4de859838c3117bf26c5b20ab69 100644 (file)
@@ -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
index de613e63bba8c47ce0d3c5b48def6c472b4ee0c3..d6f7a1f6329f3b30d619f38cdd936e8cf3d3e3f7 100644 (file)
@@ -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]");
   }
 }