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;
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);
}
}
- private void checkSonarVersion(Bootstrapper bootstrapper) {
+ protected void checkSonarVersion(Bootstrapper bootstrapper) {
String serverVersion = bootstrapper.getServerVersion();
if (isUnsupportedVersion(serverVersion)) {
throw new BootstrapException("Sonar " + serverVersion
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;
}
}
+ @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();
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;
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 {
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());
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);
+ }
+
}