From c2f1acc5a32d8f117a7af375d5317a1b5730624b Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Tue, 24 Jul 2012 08:08:20 +0000 Subject: [PATCH] Sonar Runner - Increase test coverage --- src/main/java/org/sonar/runner/Launcher.java | 4 +- src/main/java/org/sonar/runner/Runner.java | 2 +- .../java/org/sonar/runner/LauncherTest.java | 20 ++++++++++ .../java/org/sonar/runner/RunnerTest.java | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sonar/runner/Launcher.java b/src/main/java/org/sonar/runner/Launcher.java index a429d0f..bb353d5 100644 --- a/src/main/java/org/sonar/runner/Launcher.java +++ b/src/main/java/org/sonar/runner/Launcher.java @@ -23,6 +23,7 @@ package org.sonar.runner; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; +import com.google.common.annotations.VisibleForTesting; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.EnvironmentConfiguration; @@ -100,7 +101,8 @@ public class Launcher { return showSql ? "DEBUG" : "WARN"; } - private ProjectDefinition defineProject() { + @VisibleForTesting + protected ProjectDefinition defineProject() { File baseDir = runner.getProjectDir(); Properties properties = runner.getProperties(); ProjectDefinition definition = new ProjectDefinition(baseDir, runner.getWorkDir(), properties); diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index b4cb17e..60b6a45 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -148,7 +148,7 @@ public final class Runner { } } - private void checkSonarVersion(Bootstrapper bootstrapper) { + protected void checkSonarVersion(Bootstrapper bootstrapper) { String serverVersion = bootstrapper.getServerVersion(); if (isUnsupportedVersion(serverVersion)) { throw new BootstrapException("Sonar " + serverVersion diff --git a/src/test/java/org/sonar/runner/LauncherTest.java b/src/test/java/org/sonar/runner/LauncherTest.java index ea3de33..c22ce16 100644 --- a/src/test/java/org/sonar/runner/LauncherTest.java +++ b/src/test/java/org/sonar/runner/LauncherTest.java @@ -22,9 +22,12 @@ package org.sonar.runner; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.junit.Test; +import org.sonar.batch.bootstrapper.ProjectDefinition; import java.io.File; +import java.util.Properties; +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; @@ -59,6 +62,23 @@ public class LauncherTest { } } + @Test + public void shouldDefineProject() { + Properties conf = new Properties(); + conf.setProperty("sources", "src/main/java"); + conf.setProperty("tests", "src/test/java"); + conf.setProperty("binaries", "target/classes"); + conf.setProperty("libraries", "./*.xml"); + Runner runner = Runner.create(conf); + + Launcher launcher = new Launcher(runner); + ProjectDefinition projectDefinition = launcher.defineProject(); + assertThat(projectDefinition.getSourceDirs()).contains("src/main/java"); + assertThat(projectDefinition.getTestDirs()).contains("src/test/java"); + assertThat(projectDefinition.getBinaries()).contains("target/classes"); + assertThat(projectDefinition.getLibraries()).contains(new File("assembly.xml").getAbsolutePath(), new File("pom.xml").getAbsolutePath()); + } + @Test public void testGetSqlLevel() throws Exception { Configuration conf = new BaseConfiguration(); diff --git a/src/test/java/org/sonar/runner/RunnerTest.java b/src/test/java/org/sonar/runner/RunnerTest.java index d7fde1c..c0495fc 100644 --- a/src/test/java/org/sonar/runner/RunnerTest.java +++ b/src/test/java/org/sonar/runner/RunnerTest.java @@ -20,7 +20,11 @@ package org.sonar.runner; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.batch.bootstrapper.BootstrapException; +import org.sonar.batch.bootstrapper.Bootstrapper; import java.io.File; import java.util.Properties; @@ -32,9 +36,14 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class RunnerTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void shouldThrowExceptionIfMandatoryPropertyNotSpecified() { try { @@ -114,11 +123,22 @@ public class RunnerTest { File home = new File(getClass().getResource("/org/sonar/runner/RunnerTest/shouldInitDirs/").toURI()); props.setProperty("project.home", home.getCanonicalPath()); Runner runner = Runner.create(props); + assertThat(runner.getProperties().get("project.home")).isEqualTo(home.getCanonicalPath()); assertThat(runner.getProjectDir(), is(home)); assertThat(runner.getWorkDir(), is(new File(home, ".sonar"))); } + @Test + public void shouldFailInitDirsIfNotExist() throws Exception { + Properties props = new Properties(); + + props.setProperty("project.home", new File("target/foo/").getCanonicalPath()); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Project home must be an existing directory: "); + Runner.create(props); + } + @Test public void shouldInitProjectDirWithCurrentDir() throws Exception { Runner runner = Runner.create(new Properties()); @@ -138,4 +158,21 @@ public class RunnerTest { assertThat(runner.getWorkDir()).isEqualTo(new File(".", "temp-dir")); } + @Test + public void shouldCheckSonarVersion() { + Properties properties = new Properties(); + Runner runner = Runner.create(properties); + Bootstrapper bootstrapper = mock(Bootstrapper.class); + + // nothing happens, OK + when(bootstrapper.getServerVersion()).thenReturn("3.1"); + runner.checkSonarVersion(bootstrapper); + + // but fails with older versions + when(bootstrapper.getServerVersion()).thenReturn("2.1"); + thrown.expect(BootstrapException.class); + thrown.expectMessage("Sonar 2.1 does not support Standalone Runner. Please upgrade Sonar to version 2.6 or more."); + runner.checkSonarVersion(bootstrapper); + } + } -- 2.39.5