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() {
*/
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;
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;
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
@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() {
}
@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");
}
@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");
}
@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");
}
@Test
- public void failModuleBaseDirDoesNotExist() throws IOException {
+ public void failModuleBaseDirDoesNotExist() {
args.setProperty("sonar.modules", "module1");
args.setProperty("module1.sonar.projectBaseDir", "invalid");
}
@Test
- public void failModulePropertyFileDoesNotExist() throws IOException {
+ public void failModulePropertyFileDoesNotExist() {
args.setProperty("sonar.modules", "module1");
args.setProperty("module1.sonar.projectConfigFile", "invalid");
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);
}
}
}
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;
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);
}
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");
*/
package org.sonarsource.scanner.cli;
-import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.junit.Before;
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);
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);
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);
}
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);
}
@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);
}
@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);
}
@Test
- public void shouldLogServerVersion() throws IOException {
+ public void shouldLogServerVersion() {
when(scanner.serverVersion()).thenReturn("5.5");
Properties p = new Properties();
when(cli.isDisplayVersionOnly()).thenReturn(true);
}
@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);
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;
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() {
*/
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: "));
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);
}