From fae72d56be0414e34849b36f7b345ec23c87515b Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 28 Sep 2015 18:06:36 +0200 Subject: SONARUNNER-137 Don't prevent aborting process in non interactive mode --- .../test/java/org/sonar/runner/cli/CliTest.java | 14 +++----- .../test/java/org/sonar/runner/cli/MainTest.java | 40 +++++++++++----------- .../java/org/sonar/runner/cli/ShutdownTest.java | 25 ++++++-------- 3 files changed, 36 insertions(+), 43 deletions(-) (limited to 'sonar-runner-cli/src/test/java') 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(); -- cgit v1.2.3