From fe6edeb839d98c4c8a5b4bf66fb92349fc9e58fa Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 15 Mar 2019 14:24:15 +0100 Subject: [PATCH] Fix Quality flaws in tests --- .../org/sonarsource/scanner/cli/CliTest.java | 6 +-- .../org/sonarsource/scanner/cli/ConfTest.java | 37 +++++++++---------- .../org/sonarsource/scanner/cli/LogsTest.java | 6 +-- .../org/sonarsource/scanner/cli/MainTest.java | 24 ++++++------ .../scanner/cli/ScannerFactoryTest.java | 10 +---- .../sonarsource/scanner/cli/StatsTest.java | 14 +++---- .../scanner/cli/SystemInfoTest.java | 6 +-- 7 files changed, 43 insertions(+), 60 deletions(-) diff --git a/src/test/java/org/sonarsource/scanner/cli/CliTest.java b/src/test/java/org/sonarsource/scanner/cli/CliTest.java index fa43df3..8072be5 100644 --- a/src/test/java/org/sonarsource/scanner/cli/CliTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/CliTest.java @@ -26,9 +26,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; public class CliTest { - Exit exit = mock(Exit.class); - Logs logs = new Logs(System.out, System.err); - Cli cli = new Cli(exit, logs); + private Exit exit = mock(Exit.class); + private Logs logs = new Logs(System.out, System.err); + private Cli cli = new Cli(exit, logs); @Test public void should_parse_empty_arguments() { diff --git a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java index 7d8c33f..371a6ba 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ConfTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ConfTest.java @@ -19,11 +19,6 @@ */ package org.sonarsource.scanner.cli; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assume.assumeTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; @@ -32,7 +27,6 @@ import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Properties; - import org.apache.commons.lang.SystemUtils; import org.junit.Before; import org.junit.Rule; @@ -40,6 +34,11 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class ConfTest { @Rule @@ -48,11 +47,11 @@ public class ConfTest { @Rule public ExpectedException exception = ExpectedException.none(); - Map env = new HashMap<>(); - Properties args = new Properties(); - Logs logs = new Logs(System.out, System.err); - Cli cli = mock(Cli.class); - Conf conf = new Conf(cli, logs, env); + private Map env = new HashMap<>(); + private Properties args = new Properties(); + private Logs logs = new Logs(System.out, System.err); + private Cli cli = mock(Cli.class); + private Conf conf = new Conf(cli, logs, env); @Before public void initConf() { @@ -70,14 +69,14 @@ public class ConfTest { } @Test - public void should_not_fail_if_no_home() throws Exception { + public void should_not_fail_if_no_home() { assertThat(conf.properties()).isNotEmpty(); // worst case, use current path assertThat(conf.properties().getProperty("sonar.projectBaseDir")).isEqualTo(Paths.get("").toAbsolutePath().toString()); } @Test - public void base_dir_can_be_relative() throws URISyntaxException, IOException { + public void base_dir_can_be_relative() throws URISyntaxException { Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI()); args.setProperty("project.home", projectHome.getParent().toAbsolutePath().toString()); args.setProperty("sonar.projectBaseDir", "project"); @@ -125,7 +124,7 @@ public class ConfTest { } @Test - public void shouldLoadEnvironmentProperties() throws IOException { + public void shouldLoadEnvironmentProperties() { env.put("SONARQUBE_SCANNER_PARAMS", "{\"sonar.key1\" : \"v1\", \"sonar.key2\" : \"v2\"}"); args.put("sonar.key2", "v3"); @@ -136,7 +135,7 @@ public class ConfTest { } @Test - public void shouldFailWithInvalidEnvironmentProperties() throws IOException { + public void shouldFailWithInvalidEnvironmentProperties() { env.put("SONARQUBE_SCANNER_PARAMS", "{sonar.key1: \"v1\", \"sonar.key2\" : \"v2\"}"); exception.expect(IllegalStateException.class); exception.expectMessage("JSON"); @@ -232,7 +231,7 @@ public class ConfTest { } @Test - public void failModuleBaseDirDoesNotExist() throws IOException { + public void failModuleBaseDirDoesNotExist() { args.setProperty("sonar.modules", "module1"); args.setProperty("module1.sonar.projectBaseDir", "invalid"); @@ -242,7 +241,7 @@ public class ConfTest { } @Test - public void failModulePropertyFileDoesNotExist() throws IOException { + public void failModulePropertyFileDoesNotExist() { args.setProperty("sonar.modules", "module1"); args.setProperty("module1.sonar.projectConfigFile", "invalid"); @@ -299,9 +298,7 @@ public class ConfTest { assertThat(properties.getProperty("module1.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module1").toString()); assertThat(properties.getProperty("module2.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module2").toString()); } finally { - if (linkProjectHome != null) { - Files.delete(linkProjectHome); - } + Files.delete(linkProjectHome); } } } diff --git a/src/test/java/org/sonarsource/scanner/cli/LogsTest.java b/src/test/java/org/sonarsource/scanner/cli/LogsTest.java index 5e1e0e3..53d89d1 100644 --- a/src/test/java/org/sonarsource/scanner/cli/LogsTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/LogsTest.java @@ -22,7 +22,7 @@ package org.sonarsource.scanner.cli; import java.io.PrintStream; import org.junit.Before; import org.junit.Test; -import org.mockito.Matchers; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -62,7 +62,7 @@ public class LogsTest { public void testWarnWithTimestamp() { logs.setDebugEnabled(true); logs.warn("warn"); - verify(stdOut).println(Matchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d WARN: warn")); + verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d WARN: warn")); verifyNoMoreInteractions(stdOut, stdErr); } @@ -83,7 +83,7 @@ public class LogsTest { logs.setDebugEnabled(true); logs.debug("debug"); - verify(stdOut).println(Matchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$")); + verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$")); logs.setDebugEnabled(false); logs.debug("debug"); diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 2c88157..e8f19a6 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -19,7 +19,6 @@ */ package org.sonarsource.scanner.cli; -import java.io.IOException; import java.util.Map; import java.util.Properties; import org.junit.Before; @@ -60,7 +59,7 @@ public class MainTest { private Logs logs; @Before - public void setUp() throws IOException { + public void setUp() { MockitoAnnotations.initMocks(this); when(scannerFactory.create(any(Properties.class))).thenReturn(scanner); when(conf.properties()).thenReturn(properties); @@ -83,7 +82,7 @@ public class MainTest { EmbeddedScanner runner = mock(EmbeddedScanner.class); Exception e = new NullPointerException("NPE"); e = new IllegalStateException("Error", e); - doThrow(e).when(runner).execute(any(Map.class)); + doThrow(e).when(runner).execute(any()); when(scannerFactory.create(any(Properties.class))).thenReturn(runner); when(cli.isDebugEnabled()).thenReturn(true); Main main = new Main(exit, cli, conf, scannerFactory, logs); @@ -106,7 +105,7 @@ public class MainTest { main.execute(); verify(runner).start(); - verify(runner, never()).execute(any(Map.class)); + verify(runner, never()).execute(any()); verify(exit).exit(Exit.ERROR); verify(logs).error("Error during SonarQube Scanner execution", e); } @@ -164,7 +163,7 @@ public class MainTest { when(cli.isEmbedded()).thenReturn(isEmbedded); EmbeddedScanner runner = mock(EmbeddedScanner.class); - doThrow(e).when(runner).execute(any(Map.class)); + doThrow(e).when(runner).execute(any()); when(scannerFactory.create(any(Properties.class))).thenReturn(runner); Main main = new Main(exit, cli, conf, scannerFactory, logs); @@ -185,8 +184,7 @@ public class MainTest { } @Test - public void should_only_display_version() throws IOException { - + public void should_only_display_version() { Properties p = new Properties(); when(cli.isDisplayVersionOnly()).thenReturn(true); when(conf.properties()).thenReturn(p); @@ -202,7 +200,7 @@ public class MainTest { } @Test - public void should_skip() throws IOException { + public void should_skip() { Properties p = new Properties(); p.setProperty(ScanProperties.SKIP, "true"); when(conf.properties()).thenReturn(p); @@ -219,7 +217,7 @@ public class MainTest { } @Test - public void shouldLogServerVersion() throws IOException { + public void shouldLogServerVersion() { when(scanner.serverVersion()).thenReturn("5.5"); Properties p = new Properties(); when(cli.isDisplayVersionOnly()).thenReturn(true); @@ -231,24 +229,24 @@ public class MainTest { } @Test - public void should_configure_logging() throws IOException { + public void should_configure_logging() { Properties analysisProps = testLogging("sonar.verbose", "true"); assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true"); } @Test - public void should_configure_logging_trace() throws IOException { + public void should_configure_logging_trace() { Properties analysisProps = testLogging("sonar.log.level", "TRACE"); assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE"); } @Test - public void should_configure_logging_debug() throws IOException { + public void should_configure_logging_debug() { Properties analysisProps = testLogging("sonar.log.level", "DEBUG"); assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG"); } - private Properties testLogging(String propKey, String propValue) throws IOException { + private Properties testLogging(String propKey, String propValue) { Properties p = new Properties(); p.put(propKey, propValue); when(conf.properties()).thenReturn(p); diff --git a/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java b/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java index d65cad6..4dbb435 100644 --- a/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java @@ -20,7 +20,6 @@ package org.sonarsource.scanner.cli; import java.util.Properties; -import org.junit.Before; import org.junit.Test; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.LogOutput; @@ -34,13 +33,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; public class ScannerFactoryTest { - Properties props = new Properties(); - Logs logs; - - @Before - public void setUp() { - logs = mock(Logs.class); - } + private Properties props = new Properties(); + private Logs logs = mock(Logs.class); @Test public void should_create_embedded_runner() { diff --git a/src/test/java/org/sonarsource/scanner/cli/StatsTest.java b/src/test/java/org/sonarsource/scanner/cli/StatsTest.java index 1038491..e68c777 100644 --- a/src/test/java/org/sonarsource/scanner/cli/StatsTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/StatsTest.java @@ -19,25 +19,21 @@ */ package org.sonarsource.scanner.cli; -import org.mockito.Mockito; -import org.sonarsource.scanner.cli.Logs; -import org.sonarsource.scanner.cli.Stats; import java.io.PrintStream; -import java.io.UnsupportedEncodingException; - -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.mock; import org.junit.Test; +import org.mockito.Mockito; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class StatsTest { private PrintStream stdOut = mock(PrintStream.class); - private PrintStream stdErr; + private PrintStream stdErr = mock(PrintStream.class); private Logs logs = new Logs(stdOut, stdErr); @Test - public void shouldPrintStats() throws UnsupportedEncodingException { + public void shouldPrintStats() { new Stats(logs).start().stop(); verify(stdOut).println(Mockito.contains("Total time: ")); diff --git a/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java b/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java index 388392f..b9cfb8d 100644 --- a/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java @@ -31,13 +31,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class SystemInfoTest { - System2 mockSystem; - Logs logs; + private System2 mockSystem = mock(System2.class); + private Logs logs = mock(Logs.class); @Before public void setUp() { - mockSystem = mock(System2.class); - logs = mock(Logs.class); SystemInfo.setSystem(mockSystem); } -- 2.39.5