From fae72d56be0414e34849b36f7b345ec23c87515b Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 28 Sep 2015 18:06:36 +0200 Subject: [PATCH] SONARUNNER-137 Don't prevent aborting process in non interactive mode --- .../main/java/org/sonar/runner/cli/Cli.java | 8 ++-- .../main/java/org/sonar/runner/cli/Main.java | 4 +- .../java/org/sonar/runner/cli/Shutdown.java | 10 +++-- .../java/org/sonar/runner/cli/CliTest.java | 14 +++---- .../java/org/sonar/runner/cli/MainTest.java | 40 +++++++++---------- .../org/sonar/runner/cli/ShutdownTest.java | 25 +++++------- 6 files changed, 48 insertions(+), 53 deletions(-) diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java index d0a350e..3b627df 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java @@ -29,11 +29,11 @@ class Cli { private boolean displayStackTrace = false; private boolean interactive = false; private final Properties props = new Properties(); - private final Shutdown shutdown; + private final Exit exit; private final Logs logger; - public Cli(Shutdown shutdown, Logs logger) { - this.shutdown = shutdown; + public Cli(Exit exit, Logs logger) { + this.exit = exit; this.logger = logger; } @@ -147,6 +147,6 @@ class Cli { logger.info(" -v,--version Display version information"); logger.info(" -X,--debug Produce execution debug output"); logger.info(" -i,--interactive Run interactively"); - shutdown.exit(Exit.SUCCESS); + exit.exit(Exit.SUCCESS); } } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java index 71488b3..457380b 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java @@ -58,10 +58,10 @@ public class Main { public static void main(String[] args) { Exit exit = new Exit(); - Shutdown shutdown = new Shutdown(exit); Logs logs = new Logs(); - Cli cli = new Cli(shutdown, logs).parse(args); + Cli cli = new Cli(exit, logs).parse(args); cli.verify(); + Shutdown shutdown = new Shutdown(exit, cli.isInteractive()); Main main = new Main(shutdown, cli, new Conf(cli, logs), new RunnerFactory(logs), logs); main.execute(); } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java index 3c52bc5..7da4f93 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java @@ -31,14 +31,16 @@ class Shutdown { private Object lock = new Object(); private Exit exit; - Shutdown(Exit exit) { - this(exit, DEFAULT_MAX_WAIT); + Shutdown(Exit exit, boolean isInteractive) { + this(exit, isInteractive, DEFAULT_MAX_WAIT); } - Shutdown(Exit exit, long maxWait) { + Shutdown(Exit exit, boolean isInteractive, long maxWait) { this.maxWait = maxWait; this.exit = exit; - Runtime.getRuntime().addShutdownHook(hook); + if (isInteractive) { + Runtime.getRuntime().addShutdownHook(hook); + } } void exit(int status) { diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java index b8f6901..52f46e0 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java @@ -19,20 +19,16 @@ */ package org.sonar.runner.cli; -import static org.mockito.Mockito.mock; - -import static org.mockito.Mockito.verify; - import org.junit.Test; -import org.sonar.runner.cli.Cli; -import org.sonar.runner.cli.Exit; import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class CliTest { - Shutdown shutdown = mock(Shutdown.class); + Exit exit = mock(Exit.class); Logs logs = new Logs(); - Cli cli = new Cli(shutdown, logs); + Cli cli = new Cli(exit, logs); @Test public void should_parse_empty_arguments() { @@ -55,7 +51,7 @@ public class CliTest { public void dont_allow_interactive_fork() { cli.parse(new String[] {"-i", "-DsonarRunner.mode=fork"}); cli.verify(); - verify(shutdown).exit(Exit.SUCCESS); + verify(exit).exit(Exit.SUCCESS); } @Test diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java index a0dec5c..7f1995c 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Properties; - import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; @@ -33,6 +32,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.sonar.runner.api.EmbeddedRunner; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -43,7 +43,7 @@ import static org.mockito.Mockito.when; public class MainTest { @Mock - private Shutdown exit; + private Shutdown shutdown; @Mock private Cli cli; @Mock @@ -67,10 +67,10 @@ public class MainTest { @Test public void should_execute_runner() { - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); main.execute(); - verify(exit).exit(Exit.SUCCESS); + verify(shutdown).exit(Exit.SUCCESS); verify(runnerFactory).create(properties); verify(runner, times(1)).start(); @@ -86,30 +86,30 @@ public class MainTest { doThrow(e).when(runner).runAnalysis(any(Properties.class)); when(runnerFactory.create(any(Properties.class))).thenReturn(runner); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); main.execute(); verify(runner).stop(); - verify(exit).exit(Exit.ERROR); - verify(logs).error("Caused by: NPE" ); - + verify(shutdown).exit(Exit.ERROR); + verify(logs).error("Caused by: NPE"); + } - + @Test public void show_error_stacktrace() { Exception e = new NullPointerException("NPE"); e = new IllegalStateException("Error", e); when(cli.isDisplayStackTrace()).thenReturn(true); - + EmbeddedRunner runner = mock(EmbeddedRunner.class); doThrow(e).when(runner).runAnalysis(any(Properties.class)); when(runnerFactory.create(any(Properties.class))).thenReturn(runner); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); main.execute(); verify(runner).stop(); - verify(exit).exit(Exit.ERROR); + verify(shutdown).exit(Exit.ERROR); verify(logs).error("Error during Sonar runner execution", e); } @@ -120,16 +120,16 @@ public class MainTest { when(runnerFactory.create(any(Properties.class))).thenReturn(runner); when(cli.isInteractive()).thenReturn(true); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); BufferedReader inputReader = mock(BufferedReader.class); when(inputReader.readLine()).thenReturn(""); - when(exit.shouldExit()).thenReturn(false).thenReturn(true); + when(shutdown.shouldExit()).thenReturn(false).thenReturn(true); main.setInputReader(inputReader); main.execute(); verify(runner, times(2)).runAnalysis(any(Properties.class)); verify(runner).stop(); - verify(exit).exit(Exit.SUCCESS); + verify(shutdown).exit(Exit.SUCCESS); } @Test @@ -139,14 +139,14 @@ public class MainTest { when(cli.isDisplayVersionOnly()).thenReturn(true); when(conf.properties()).thenReturn(p); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); main.execute(); - InOrder inOrder = Mockito.inOrder(exit, runnerFactory); + InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory); - inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); + inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS); inOrder.verify(runnerFactory, times(1)).create(p); - inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); + inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS); } @Test(timeout = 30000) @@ -160,7 +160,7 @@ public class MainTest { when(cli.isDebugMode()).thenReturn(true); when(cli.isDisplayStackTrace()).thenReturn(true); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(shutdown, cli, conf, runnerFactory, logs); main.execute(); verify(runner, times(1)).start(); diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java index ec401db..5f739fc 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java @@ -19,20 +19,17 @@ */ package org.sonar.runner.cli; -import static org.mockito.Mockito.verify; -import static org.fest.assertions.Assertions.assertThat; -import static com.jayway.awaitility.Awaitility.await; - +import com.jayway.awaitility.Duration; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; - -import com.jayway.awaitility.Duration; -import org.mockito.MockitoAnnotations; -import org.sonar.runner.cli.Exit; -import org.sonar.runner.cli.Shutdown; -import org.mockito.Mock; -import org.junit.Test; import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static com.jayway.awaitility.Awaitility.await; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.verify; public class ShutdownTest { @Mock @@ -42,7 +39,7 @@ public class ShutdownTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - shutdown = new Shutdown(exit); + shutdown = new Shutdown(exit, true); } @Test @@ -53,7 +50,7 @@ public class ShutdownTest { @Test(timeout = 60_000) public void testWaitReady() throws InterruptedException { - shutdown = new Shutdown(exit, 100_000); + shutdown = new Shutdown(exit, true, 100_000); shutdown.signalReady(false); assertThat(shutdown.shouldExit()).isFalse(); @@ -75,7 +72,7 @@ public class ShutdownTest { @Test(timeout = 60_000) public void testTimeout() throws InterruptedException { - shutdown = new Shutdown(exit, 0); + shutdown = new Shutdown(exit, true, 0); Thread t = new HookCaller(); t.start(); -- 2.39.5