From 265b9abd6d508d5ae83495958c27363fdd7d6740 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 28 Jul 2017 11:56:24 +0200 Subject: [PATCH] SQSCANNER-42 Stop support of SQ < 5.2 --- pom.xml | 14 ++-- .../org/sonarsource/scanner/cli/Main.java | 21 ++--- .../scanner/cli/ScannerFactory.java | 6 +- .../org/sonarsource/scanner/cli/MainTest.java | 79 +++++++------------ 4 files changed, 45 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index c4437e3..0f2dcfd 100644 --- a/pom.xml +++ b/pom.xml @@ -61,12 +61,12 @@ org.sonarsource.scanner.api sonar-scanner-api - 2.9.0.887 + 2.10.0.1049 com.google.code.findbugs jsr305 - 2.0.1 + 2.0.3 provided @@ -74,7 +74,7 @@ junit junit - 4.11 + 4.12 test @@ -91,8 +91,8 @@ org.mockito - mockito-all - 1.9.5 + mockito-core + 2.8.47 test @@ -173,8 +173,8 @@ - 490000 - 510000 + 530000 + 550000 ${project.build.directory}/sonar-scanner-${project.version}.zip diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index d93116b..7144ec8 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -19,6 +19,7 @@ */ package org.sonarsource.scanner.cli; +import java.util.Map; import java.util.Properties; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.ScanProperties; @@ -64,32 +65,20 @@ public class Main { Stats stats = new Stats(logger).start(); int status = Exit.ERROR; - boolean started = false; try { Properties p = conf.properties(); checkSkip(p); configureLogging(p); init(p); runner.start(); - started = true; logger.info("SonarQube server " + runner.serverVersion()); - runAnalysis(stats, p); + execute(stats, p); status = Exit.SUCCESS; } catch (Throwable e) { - status = Exit.ERROR; displayExecutionResult(stats, "FAILURE"); showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled()); } finally { - try { - if (started) { - runner.stop(); - } - } catch (Throwable e) { - status = Exit.ERROR; - logger.error("Unable to properly stop the scanner", e); - } finally { - exit.exit(status); - } + exit.exit(status); } } @@ -118,8 +107,8 @@ public class Main { } } - private void runAnalysis(Stats stats, Properties p) { - runner.runAnalysis(p); + private void execute(Stats stats, Properties p) { + runner.execute((Map) p); displayExecutionResult(stats, "SUCCESS"); } diff --git a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java b/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java index 04a6684..7fe4d9f 100644 --- a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java +++ b/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java @@ -19,6 +19,7 @@ */ package org.sonarsource.scanner.cli; +import java.util.Map; import java.util.Properties; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.LogOutput; @@ -32,9 +33,8 @@ class ScannerFactory { } EmbeddedScanner create(Properties props) { - return EmbeddedScanner.create(new DefaultLogOutput()) - .addGlobalProperties(props) - .setApp("ScannerCli", ScannerVersion.version()); + return EmbeddedScanner.create("ScannerCli", ScannerVersion.version(), new DefaultLogOutput()) + .addGlobalProperties((Map) props); } class DefaultLogOutput implements LogOutput { diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 26e1a71..869a183 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -20,6 +20,7 @@ package org.sonarsource.scanner.cli; import java.io.IOException; +import java.util.Map; import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -33,7 +34,7 @@ import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.ScanProperties; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -52,82 +53,63 @@ public class MainTest { @Mock private Properties properties; @Mock - private ScannerFactory runnerFactory; + private ScannerFactory scannerFactory; @Mock - private EmbeddedScanner runner; + private EmbeddedScanner scanner; @Mock private Logs logs; @Before public void setUp() throws IOException { MockitoAnnotations.initMocks(this); - when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + when(scannerFactory.create(any(Properties.class))).thenReturn(scanner); when(conf.properties()).thenReturn(properties); } @Test public void should_execute_runner() { - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); verify(exit).exit(Exit.SUCCESS); - verify(runnerFactory).create(properties); + verify(scannerFactory).create(properties); - verify(runner, times(1)).start(); - verify(runner, times(1)).runAnalysis(properties); - verify(runner, times(1)).stop(); + verify(scanner, times(1)).start(); + verify(scanner, times(1)).execute((Map) properties); } @Test - public void should_call_stop_on_error_during_analysis() { + public void should_exit_with_error_on_error_during_analysis() { EmbeddedScanner runner = mock(EmbeddedScanner.class); Exception e = new NullPointerException("NPE"); e = new IllegalStateException("Error", e); - doThrow(e).when(runner).runAnalysis(any(Properties.class)); - when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + doThrow(e).when(runner).execute(any(Map.class)); + when(scannerFactory.create(any(Properties.class))).thenReturn(runner); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); - verify(runner).stop(); verify(exit).exit(Exit.ERROR); verify(logs).error("Error during SonarQube Scanner execution", e); } @Test - public void should_not_call_stop_on_error_during_start() { + public void should_exit_with_error_on_error_during_start() { EmbeddedScanner runner = mock(EmbeddedScanner.class); Exception e = new NullPointerException("NPE"); e = new IllegalStateException("Error", e); doThrow(e).when(runner).start(); - when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + when(scannerFactory.create(any(Properties.class))).thenReturn(runner); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); verify(runner).start(); - verify(runner, never()).runAnalysis(any()); - verify(runner, never()).stop(); + verify(runner, never()).execute(any(Map.class)); verify(exit).exit(Exit.ERROR); verify(logs).error("Error during SonarQube Scanner execution", e); } - @Test - public void should_exit_on_error() { - EmbeddedScanner runner = mock(EmbeddedScanner.class); - Exception e = new NullPointerException("NPE"); - e = new IllegalStateException("Error", e); - doThrow(e).when(runner).stop(); - when(runnerFactory.create(any(Properties.class))).thenReturn(runner); - - Main main = new Main(exit, cli, conf, runnerFactory, logs); - main.execute(); - - verify(runner).stop(); - verify(exit).exit(Exit.ERROR); - verify(logs).error("Unable to properly stop the scanner", e); - } - @Test public void show_error_with_stacktrace() { Exception e = createException(false); @@ -160,13 +142,12 @@ public class MainTest { when(cli.isDebugEnabled()).thenReturn(debugEnabled); EmbeddedScanner runner = mock(EmbeddedScanner.class); - doThrow(e).when(runner).runAnalysis(any(Properties.class)); - when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + doThrow(e).when(runner).execute(any(Map.class)); + when(scannerFactory.create(any(Properties.class))).thenReturn(runner); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); - verify(runner).stop(); verify(exit).exit(Exit.ERROR); } @@ -188,13 +169,13 @@ 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(exit, cli, conf, scannerFactory, logs); main.execute(); - InOrder inOrder = Mockito.inOrder(exit, runnerFactory); + InOrder inOrder = Mockito.inOrder(exit, scannerFactory); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); - inOrder.verify(runnerFactory, times(1)).create(p); + inOrder.verify(scannerFactory, times(1)).create(p); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); } @@ -204,25 +185,25 @@ public class MainTest { p.setProperty(ScanProperties.SKIP, "true"); when(conf.properties()).thenReturn(p); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); verify(logs).info("SonarQube Scanner analysis skipped"); - InOrder inOrder = Mockito.inOrder(exit, runnerFactory); + InOrder inOrder = Mockito.inOrder(exit, scannerFactory); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); - inOrder.verify(runnerFactory, times(1)).create(p); + inOrder.verify(scannerFactory, times(1)).create(p); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); } @Test public void shouldLogServerVersion() throws IOException { - when(runner.serverVersion()).thenReturn("5.5"); + when(scanner.serverVersion()).thenReturn("5.5"); Properties p = new Properties(); when(cli.isDisplayVersionOnly()).thenReturn(true); when(conf.properties()).thenReturn(p); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); verify(logs).info("SonarQube server 5.5"); } @@ -250,14 +231,14 @@ public class MainTest { p.put(propKey, propValue); when(conf.properties()).thenReturn(p); - Main main = new Main(exit, cli, conf, runnerFactory, logs); + Main main = new Main(exit, cli, conf, scannerFactory, logs); main.execute(); // Logger used for callback should have debug enabled verify(logs).setDebugEnabled(true); ArgumentCaptor propertiesCapture = ArgumentCaptor.forClass(Properties.class); - verify(runner).runAnalysis(propertiesCapture.capture()); + verify(scanner).execute((Map) propertiesCapture.capture()); return propertiesCapture.getValue(); } -- 2.39.5