diff options
Diffstat (limited to 'sonar-runner-api/src/test/java/org/sonar/runner/api')
4 files changed, 96 insertions, 16 deletions
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 2082b29..84a256a 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 @@ -31,8 +31,11 @@ import org.sonar.runner.impl.InternalProperties; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; import java.util.Properties; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; import static org.mockito.Matchers.any; import static org.fest.assertions.Assertions.assertThat; @@ -58,6 +61,7 @@ public class EmbeddedRunnerTest { public void setUp() { batchLauncher = mock(IsolatedLauncherFactory.class); launcher = mock(IsolatedLauncher.class); + when(launcher.getVersion()).thenReturn("5.2"); when(batchLauncher.createLauncher(any(Properties.class))).thenReturn(launcher); runner = new EmbeddedRunner(batchLauncher); } @@ -68,6 +72,48 @@ public class EmbeddedRunnerTest { assertThat(runner.app()).isEqualTo("Eclipse"); assertThat(runner.appVersion()).isEqualTo("3.1"); } + + @Test + public void test_back_compatibility() { + when(launcher.getVersion()).thenReturn("4.5"); + + final FakeExtension fakeExtension = new FakeExtension(); + List<Object> extensionList = new LinkedList<>(); + extensionList.add(fakeExtension); + + Properties analysisProps = new Properties(); + analysisProps.put("sonar.dummy", "summy"); + + runner.addExtensions(fakeExtension); + runner.setGlobalProperty("sonar.projectKey", "foo"); + runner.start(); + runner.runAnalysis(analysisProps); + runner.stop(); + + verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() { + @Override + public boolean matches(Object o) { + return "foo".equals(((Properties) o).getProperty("sonar.projectKey")); + } + })); + + // it should have added a few properties to analysisProperties, and have merged global props + final String[] mustHaveKeys = {"sonar.working.directory", "sonar.sourceEncoding", "sonar.projectBaseDir", + "sonar.projectKey", "sonar.dummy"}; + + verify(launcher).executeOldVersion(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; + } + }), eq(extensionList)); + } @Test public void should_set_unmasked_packages() { 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 0c27273..e7c3a56 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,8 +19,10 @@ */ package org.sonar.runner.api; +import org.sonar.home.log.LogListener.Level; +import org.sonar.home.log.LogListener; +import org.sonar.runner.impl.Logs; import org.mockito.Mockito; - import org.mockito.ArgumentCaptor; import org.junit.Before; import org.junit.Rule; @@ -29,12 +31,16 @@ import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentMatcher; import org.sonar.runner.impl.JarExtractor; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.LinkedList; +import java.util.List; import java.util.Properties; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; import static org.mockito.Matchers.any; @@ -74,27 +80,46 @@ public class ForkedRunnerTest { } @Test - public void should_print_to_standard_outputs_by_default() throws IOException { + public void should_use_log_listener() throws IOException { JarExtractor jarExtractor = createMockExtractor(); CommandExecutor commandExecutor = mock(CommandExecutor.class); ForkedRunner runner = new ForkedRunner(jarExtractor, commandExecutor); runner.execute(); - - verify(commandExecutor).execute(any(Command.class), argThat(new StdConsumerMatcher(System.out)), argThat(new StdConsumerMatcher(System.err)), anyLong(), - any(ProcessMonitor.class)); + LogListener listener = mock(LogListener.class); + Logs.setListener(listener); + + ArgumentCaptor<StreamConsumer> arg1 = ArgumentCaptor.forClass(StreamConsumer.class); + ArgumentCaptor<StreamConsumer> arg2 = ArgumentCaptor.forClass(StreamConsumer.class); + + verify(commandExecutor).execute(any(Command.class), arg1.capture(), arg2.capture(), anyLong(), any(ProcessMonitor.class)); + arg1.getValue().consumeLine("test1"); + arg2.getValue().consumeLine("test2"); + + verify(listener).log("test1", Level.INFO); + verify(listener).log("test2", Level.ERROR); + verifyNoMoreInteractions(listener); } + + @Test + public void should_print_to_consumers_by_default() throws IOException { + final List<String> printedLines = new LinkedList<>(); + StreamConsumer consumer = new StreamConsumer() { + @Override + public void consumeLine(String line) { + printedLines.add(line); + } + }; + JarExtractor jarExtractor = createMockExtractor(); - static class StdConsumerMatcher extends ArgumentMatcher<StreamConsumer> { - PrintStream output; - - StdConsumerMatcher(PrintStream output) { - this.output = output; - } + CommandExecutor commandExecutor = mock(CommandExecutor.class); + ForkedRunner runner = new ForkedRunner(jarExtractor, commandExecutor); + runner.setStdOut(consumer); + runner.setStdErr(consumer); + runner.execute(); - public boolean matches(Object o) { - return ((PrintStreamConsumer) o).output == output; - } + verify(commandExecutor).execute(any(Command.class), eq(consumer), eq(consumer), anyLong(), + any(ProcessMonitor.class)); } @Test diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java index 31e2c8f..79550e0 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/OsTest.java @@ -40,7 +40,6 @@ public class OsTest { @Test public void testUsedJavaExe() throws Exception { - System.out.println(System.getProperty("java.io.tmpdir")); File javaExe = new Os().thisJavaExe(); assertThat(javaExe).isNotNull().isFile().exists(); assertThat(javaExe.getName()).contains("java"); diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java index a56030d..a268ec3 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java @@ -42,6 +42,16 @@ public class UtilsTest { } @Test + public void parse_version() { + assertThat(Utils.isAtLeast52("5.2")).isTrue(); + assertThat(Utils.isAtLeast52(null)).isFalse(); + assertThat(Utils.isAtLeast52("52")).isTrue(); + assertThat(Utils.isAtLeast52("5.0")).isFalse(); + assertThat(Utils.isAtLeast52("6.0.0")).isTrue(); + assertThat(Utils.isAtLeast52("5.2-SNAPSHOT")).isTrue(); + } + + @Test public void task_should_require_project() { Properties props = new Properties(); assertThat(Utils.taskRequiresProject(props)).isTrue(); @@ -64,7 +74,7 @@ public class UtilsTest { Utils.closeQuietly(c); verify(c).close(); } - + @Test public void close_quietly_null() throws IOException { Utils.closeQuietly(null); |