]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Sonar Runner - Increase test coverage
authorFabrice Bellingard <bellingard@gmail.com>
Tue, 24 Jul 2012 08:08:20 +0000 (08:08 +0000)
committerFabrice Bellingard <bellingard@gmail.com>
Tue, 24 Jul 2012 08:08:20 +0000 (08:08 +0000)
src/main/java/org/sonar/runner/Launcher.java
src/main/java/org/sonar/runner/Runner.java
src/test/java/org/sonar/runner/LauncherTest.java
src/test/java/org/sonar/runner/RunnerTest.java

index a429d0f15e9f8150ba83ba427f2a7c4c5a4dcc80..bb353d55409ea6766fd0bc4feaa5f25e262c80e7 100644 (file)
@@ -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);
index b4cb17ee28a262e895a2008b409138db3ff52b97..60b6a454901d6cbe712e18ea8420e5075bf251a3 100644 (file)
@@ -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
index ea3de33831a1e24f26e56ebc7267dedac4610801..c22ce166858bf7d9406f582b9ec3b73725456699 100644 (file)
@@ -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();
index d7fde1cb03ca70a5d3220a8eb94563560323c115..c0495fc1a570b3b84bededc74554c08f1c397b4b 100644 (file)
 
 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);
+  }
+
 }