Browse Source

Fix Quality flaws in tests

tags/3.4.0.1729
Simon Brandhof 5 years ago
parent
commit
fe6edeb839

+ 3
- 3
src/test/java/org/sonarsource/scanner/cli/CliTest.java View File

@@ -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() {

+ 17
- 20
src/test/java/org/sonarsource/scanner/cli/ConfTest.java View File

@@ -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<String, String> 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<String, String> 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);
}
}
}

+ 3
- 3
src/test/java/org/sonarsource/scanner/cli/LogsTest.java View File

@@ -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");

+ 11
- 13
src/test/java/org/sonarsource/scanner/cli/MainTest.java View File

@@ -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);

+ 2
- 8
src/test/java/org/sonarsource/scanner/cli/ScannerFactoryTest.java View File

@@ -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() {

+ 5
- 9
src/test/java/org/sonarsource/scanner/cli/StatsTest.java View File

@@ -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: "));

+ 2
- 4
src/test/java/org/sonarsource/scanner/cli/SystemInfoTest.java View File

@@ -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);
}


Loading…
Cancel
Save