aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/test
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-15 14:59:17 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-06-18 17:26:05 +0200
commite4ba9869d57dc4dd92648b5c8406d26fcd550d46 (patch)
tree489e0e87f6b7df0a95095de50d0264ff355fa568 /sonar-runner-api/src/test
parent5b6747b46db7fef9238e3bbaf66d2822e03d0a6e (diff)
downloadsonar-scanner-cli-e4ba9869d57dc4dd92648b5c8406d26fcd550d46.tar.gz
sonar-scanner-cli-e4ba9869d57dc4dd92648b5c8406d26fcd550d46.zip
SONARUNNER-136 and SONARUNNER-137 - Provide a new API to run several analysis with same process and an interactive mode
Diffstat (limited to 'sonar-runner-api/src/test')
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java42
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java102
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java29
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java16
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java24
5 files changed, 147 insertions, 66 deletions
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
index b66d075..8cd7630 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/DirsTest.java
@@ -24,47 +24,46 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
+import java.util.Properties;
import static org.fest.assertions.Assertions.assertThat;
public class DirsTest {
- Runner<?> runner = new SimpleRunner();
- Dirs dirs = new Dirs();
+ Properties p = new Properties();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Test
public void should_init_default_task_work_dir() throws Exception {
- runner.setProperty("sonar.task", "views");
- dirs.init(runner);
+ p.setProperty("sonar.task", "views");
+ Dirs.init(p);
- File workDir = new File(runner.property("sonar.working.directory", null));
+ File workDir = new File(p.getProperty(RunnerProperties.WORK_DIR, null));
assertThat(workDir).isNotNull().isDirectory();
assertThat(workDir.getCanonicalPath()).isEqualTo(new File(".").getCanonicalPath());
}
@Test
public void should_use_parameterized_task_work_dir() throws Exception {
- runner.setProperty("sonar.task", "views");
- runner.setProperty("sonar.working.directory", "generated/reports");
- dirs.init(runner);
+ p.setProperty("sonar.task", "views");
+ p.setProperty(RunnerProperties.WORK_DIR, "generated/reports");
+ Dirs.init(p);
- File workDir = new File(runner.property("sonar.working.directory", null));
+ File workDir = new File(p.getProperty(RunnerProperties.WORK_DIR, null));
assertThat(workDir).isNotNull();
- //separators from windows to unix
+ // separators from windows to unix
assertThat(workDir.getCanonicalPath().replace("\\", "/")).contains("generated/reports");
}
@Test
public void should_init_default_project_dirs() throws Exception {
- runner.setProperty("sonar.task", "scan");
- dirs.init(runner);
+ p.setProperty("sonar.task", "scan");
+ Dirs.init(p);
-
- File projectDir = new File(runner.property("sonar.projectBaseDir", null));
- File workDir = new File(runner.property("sonar.working.directory", null));
+ File projectDir = new File(p.getProperty(ScanProperties.PROJECT_BASEDIR, null));
+ File workDir = new File(p.getProperty(RunnerProperties.WORK_DIR, null));
assertThat(projectDir).isNotNull().isDirectory();
assertThat(workDir).isNotNull();
@@ -77,14 +76,13 @@ public class DirsTest {
@Test
public void should_set_relative_path_to_project_work_dir() throws Exception {
File initialProjectDir = temp.newFolder();
- runner.setProperty("sonar.task", "scan");
- runner.setProperty("sonar.working.directory", "relative/path");
- runner.setProperty("sonar.projectBaseDir", initialProjectDir.getAbsolutePath());
- dirs.init(runner);
-
+ p.setProperty("sonar.task", "scan");
+ p.setProperty(RunnerProperties.WORK_DIR, "relative/path");
+ p.setProperty(ScanProperties.PROJECT_BASEDIR, initialProjectDir.getAbsolutePath());
+ Dirs.init(p);
- File projectDir = new File(runner.property("sonar.projectBaseDir", null));
- File workDir = new File(runner.property("sonar.working.directory", null));
+ File projectDir = new File(p.getProperty(ScanProperties.PROJECT_BASEDIR, null));
+ File workDir = new File(p.getProperty(RunnerProperties.WORK_DIR, null));
assertThat(projectDir).isNotNull().isDirectory();
assertThat(projectDir.getCanonicalPath()).isEqualTo(initialProjectDir.getCanonicalPath());
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java
index 2a4302b..2082b29 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java
@@ -19,19 +19,22 @@
*/
package org.sonar.runner.api;
+import org.junit.Before;
+import org.sonar.runner.batch.IsolatedLauncher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatcher;
-import org.sonar.runner.impl.BatchLauncher;
+import org.sonar.runner.impl.IsolatedLauncherFactory;
import org.sonar.runner.impl.InternalProperties;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.util.List;
import java.util.Properties;
+import static org.mockito.Mockito.when;
+import static org.mockito.Matchers.any;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
@@ -47,6 +50,18 @@ public class EmbeddedRunnerTest {
assertThat(EmbeddedRunner.create()).isNotNull().isInstanceOf(EmbeddedRunner.class);
}
+ private IsolatedLauncherFactory batchLauncher;
+ private IsolatedLauncher launcher;
+ private EmbeddedRunner runner;
+
+ @Before
+ public void setUp() {
+ batchLauncher = mock(IsolatedLauncherFactory.class);
+ launcher = mock(IsolatedLauncher.class);
+ when(batchLauncher.createLauncher(any(Properties.class))).thenReturn(launcher);
+ runner = new EmbeddedRunner(batchLauncher);
+ }
+
@Test
public void test_app() {
EmbeddedRunner runner = EmbeddedRunner.create().setApp("Eclipse", "3.1");
@@ -57,23 +72,23 @@ public class EmbeddedRunnerTest {
@Test
public void should_set_unmasked_packages() {
EmbeddedRunner runner = EmbeddedRunner.create();
- assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isNull();
+ assertThat(runner.globalProperty(InternalProperties.RUNNER_MASK_RULES, null)).isNull();
runner = EmbeddedRunner.create().setUnmaskedPackages("org.apache.ant", "org.ant");
- assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.apache.ant.,UNMASK|org.ant.");
+ assertThat(runner.globalProperty(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.apache.ant.,UNMASK|org.ant.");
}
@Test
public void should_set_mask_rules() {
EmbeddedRunner runner = EmbeddedRunner.create();
- assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isNull();
+ assertThat(runner.globalProperty(InternalProperties.RUNNER_MASK_RULES, null)).isNull();
runner = EmbeddedRunner.create()
.unmask("org.slf4j.Logger")
.mask("org.slf4j.")
.mask("ch.qos.logback.")
.unmask("");
- assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.slf4j.Logger,MASK|org.slf4j.,MASK|ch.qos.logback.,UNMASK|");
+ assertThat(runner.globalProperty(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.slf4j.Logger,MASK|org.slf4j.,MASK|ch.qos.logback.,UNMASK|");
}
@Test
@@ -89,38 +104,76 @@ public class EmbeddedRunnerTest {
@Test
public void should_set_properties() {
EmbeddedRunner runner = EmbeddedRunner.create();
- runner.setProperty("sonar.projectKey", "foo");
- runner.addProperties(new Properties() {
+ runner.setGlobalProperty("sonar.projectKey", "foo");
+ runner.addGlobalProperties(new Properties() {
{
setProperty("sonar.login", "admin");
setProperty("sonar.password", "gniark");
}
});
- assertThat(runner.property("sonar.projectKey", null)).isEqualTo("foo");
- assertThat(runner.property("sonar.login", null)).isEqualTo("admin");
- assertThat(runner.property("sonar.password", null)).isEqualTo("gniark");
- assertThat(runner.property("not.set", "this_is_default")).isEqualTo("this_is_default");
+ assertThat(runner.globalProperty("sonar.projectKey", null)).isEqualTo("foo");
+ assertThat(runner.globalProperty("sonar.login", null)).isEqualTo("admin");
+ assertThat(runner.globalProperty("sonar.password", null)).isEqualTo("gniark");
+ assertThat(runner.globalProperty("not.set", "this_is_default")).isEqualTo("this_is_default");
}
@Test
public void should_launch_batch() {
- BatchLauncher batchLauncher = mock(BatchLauncher.class);
- EmbeddedRunner runner = new EmbeddedRunner(batchLauncher);
final FakeExtension fakeExtension = new FakeExtension();
runner.addExtensions(fakeExtension);
- runner.setProperty("sonar.projectKey", "foo");
- runner.execute();
+ runner.setGlobalProperty("sonar.projectKey", "foo");
+ runner.start();
+ runner.runAnalysis(new Properties());
+ runner.stop();
- verify(batchLauncher).execute(argThat(new ArgumentMatcher<Properties>() {
+ verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() {
@Override
public boolean matches(Object o) {
return "foo".equals(((Properties) o).getProperty("sonar.projectKey"));
}
- }), argThat(new ArgumentMatcher<List<Object>>() {
+ }));
+
+ // it should have added a few properties to analysisProperties
+ final String[] mustHaveKeys = {"sonar.working.directory", "sonar.sourceEncoding", "sonar.projectBaseDir"};
+
+ verify(launcher).execute(argThat(new ArgumentMatcher<Properties>() {
+ @Override
+ public boolean matches(Object o) {
+ Properties m = (Properties) o;
+ for (String s : mustHaveKeys) {
+ if (!m.containsKey(s)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }));
+ }
+
+ @Test
+ public void should_launch_batch_analysisProperties() {
+ final FakeExtension fakeExtension = new FakeExtension();
+ runner.addExtensions(fakeExtension);
+ runner.setGlobalProperty("sonar.projectKey", "foo");
+ runner.start();
+
+ Properties analysisProperties = new Properties();
+ analysisProperties.put("sonar.projectKey", "value1");
+ runner.runAnalysis(analysisProperties);
+ runner.stop();
+
+ verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() {
+ @Override
+ public boolean matches(Object o) {
+ return "foo".equals(((Properties) o).getProperty("sonar.projectKey"));
+ }
+ }));
+
+ verify(launcher).execute(argThat(new ArgumentMatcher<Properties>() {
@Override
public boolean matches(Object o) {
- return ((List) o).contains(fakeExtension);
+ return "value1".equals(((Properties) o).getProperty("sonar.projectKey"));
}
}));
}
@@ -128,12 +181,13 @@ public class EmbeddedRunnerTest {
@Test
public void should_launch_in_simulation_mode() throws IOException {
File dump = temp.newFile();
+ Properties p = new Properties();
- BatchLauncher batchLauncher = mock(BatchLauncher.class);
- EmbeddedRunner runner = new EmbeddedRunner(batchLauncher);
- runner.setProperty("sonar.projectKey", "foo");
- runner.setProperty("sonarRunner.dumpToFile", dump.getAbsolutePath());
- runner.execute();
+ p.setProperty("sonar.projectKey", "foo");
+ p.setProperty("sonarRunner.dumpToFile", dump.getAbsolutePath());
+ runner.start();
+ runner.runAnalysis(p);
+ runner.stop();
Properties props = new Properties();
props.load(new FileInputStream(dump));
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java
index d6f7a1f..0c27273 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java
@@ -19,6 +19,9 @@
*/
package org.sonar.runner.api;
+import org.mockito.Mockito;
+
+import org.mockito.ArgumentCaptor;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -99,13 +102,13 @@ public class ForkedRunnerTest {
JarExtractor jarExtractor = createMockExtractor();
ForkedRunner runner = new ForkedRunner(jarExtractor, mock(CommandExecutor.class), any(ProcessMonitor.class));
- runner.setProperty("sonar.dynamicAnalysis", "false");
- runner.setProperty("sonar.login", "admin");
+ runner.setGlobalProperty("sonar.dynamicAnalysis", "false");
+ runner.setGlobalProperty("sonar.login", "admin");
runner.addJvmArguments("-Xmx512m");
runner.addJvmEnvVariables(System.getenv());
runner.setJvmEnvVariable("SONAR_HOME", "/path/to/sonar");
- ForkedRunner.ForkCommand forkCommand = runner.createCommand();
+ ForkedRunner.ForkCommand forkCommand = runner.createCommand(runner.globalProperties());
Properties properties = new Properties();
properties.load(new FileInputStream(forkCommand.propertiesFile));
@@ -115,7 +118,21 @@ public class ForkedRunnerTest {
assertThat(properties.getProperty("-Xmx512m")).isNull();
assertThat(properties.getProperty("SONAR_HOME")).isNull();
}
-
+
+ @Test
+ public void should_merge_properties() throws IOException {
+ JarExtractor jarExtractor = createMockExtractor();
+ ForkedRunner runner = new ForkedRunner(jarExtractor, mock(CommandExecutor.class), null);
+
+ ForkedRunner spy = Mockito.spy(runner);
+ spy.setGlobalProperty("sonar.login", "admin");
+ spy.execute();
+ // generated analysis properties should have been added
+ ArgumentCaptor<Properties> properties = ArgumentCaptor.forClass(Properties.class);
+ verify(spy).writeProperties(properties.capture());
+ assertThat(properties.getValue().keySet()).contains("sonar.working.directory", "sonar.host.url", "sonar.sourceEncoding", "sonar.login");
+ }
+
@Test
public void test_java_command() throws IOException {
JarExtractor jarExtractor = mock(JarExtractor.class);
@@ -126,8 +143,8 @@ public class ForkedRunnerTest {
ForkedRunner runner = new ForkedRunner(jarExtractor, commandExecutor);
runner.setJavaExecutable("java");
- runner.setProperty("sonar.dynamicAnalysis", "false");
- runner.setProperty("sonar.login", "admin");
+ runner.setGlobalProperty("sonar.dynamicAnalysis", "false");
+ runner.setGlobalProperty("sonar.login", "admin");
runner.addJvmArguments("-Xmx512m");
runner.addJvmEnvVariables(System.getenv());
runner.setJvmEnvVariable("SONAR_HOME", "/path/to/sonar");
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java
index 25b8c6b..5d3f0c2 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/SimpleRunner.java
@@ -19,8 +19,20 @@
*/
package org.sonar.runner.api;
-class SimpleRunner extends Runner<SimpleRunner> {
+import java.util.Properties;
+
+class SimpleRunner extends Runner {
+ @Override
+ protected void doExecute(Properties props) {
+ }
+
+ @Override
+ protected void doStart() {
+
+ }
+
@Override
- protected void doExecute() {
+ protected void doStop() {
+
}
}
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java
index 78b0ec4..027a87f 100644
--- a/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java
@@ -22,33 +22,33 @@ package org.sonar.runner.api;
import org.junit.Test;
import java.nio.charset.Charset;
+import java.util.Properties;
import static org.fest.assertions.Assertions.assertThat;
public class SourceEncodingTest {
- SourceEncoding encoding = new SourceEncoding();
- Runner<?> runner = new SimpleRunner();
+ Properties p = new Properties();
@Test
public void should_set_default_platform_encoding() throws Exception {
- runner.setProperty("sonar.task", "scan");
- encoding.init(runner);
- assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name());
+ p.setProperty("sonar.task", "scan");
+ SourceEncoding.init(p);
+ assertThat(p.getProperty("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name());
}
@Test
public void should_use_parameterized_encoding() throws Exception {
- runner.setProperty("sonar.task", "scan");
- runner.setProperty("sonar.sourceEncoding", "THE_ISO_1234");
- encoding.init(runner);
- assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234");
+ p.setProperty("sonar.task", "scan");
+ p.setProperty("sonar.sourceEncoding", "THE_ISO_1234");
+ SourceEncoding.init(p);
+ assertThat(p.getProperty("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234");
}
@Test
public void should_not_init_encoding_if_not_project_task() throws Exception {
- runner.setProperty("sonar.task", "views");
- encoding.init(runner);
- assertThat(runner.property("sonar.sourceEncoding", null)).isNull();
+ p.setProperty("sonar.task", "views");
+ SourceEncoding.init(p);
+ assertThat(p.getProperty("sonar.sourceEncoding", null)).isNull();
}
}