From bd940b0436c733b7d9edb7ceddf581185a954151 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Mon, 24 Jan 2011 21:24:59 +0300 Subject: [PATCH] Use CoreProperties for in-memory POM construction --- .../java/org/sonar/batch/ProjectTree.java | 36 +++++++--- .../java/org/sonar/batch/InMemoryPomTest.java | 67 +++++++++++++++++++ .../java/org/sonar/api/CoreProperties.java | 5 ++ 3 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java index 444cebde88c..d0c6f118d3b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java @@ -21,11 +21,14 @@ package org.sonar.batch; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.apache.commons.lang.StringUtils; import org.apache.maven.model.Reporting; import org.apache.maven.project.MavenProject; import org.slf4j.LoggerFactory; +import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.resources.Project; +import org.sonar.api.utils.SonarException; import org.sonar.batch.bootstrapper.ProjectDefinition; import org.sonar.batch.bootstrapper.Reactor; @@ -60,27 +63,34 @@ public class ProjectTree { return new MavenReactor(mavenProjects); } - private static MavenProject createInMemoryPom(ProjectDefinition project) { - MavenProject pom = new MavenProject(); + static MavenProject createInMemoryPom(final ProjectDefinition project) { + MavenProject pom = new MavenProject() { + /** + * This allows to specify base directory without specifying location of a pom.xml + */ + public File getBasedir() { + return project.getBaseDir(); + }; + }; - String key = project.getProperties().getProperty("project.key"); // TODO constant + Properties properties = project.getProperties(); + + String key = getPropertyOrDie(properties, CoreProperties.PROJECT_KEY_PROPERTY); String[] keys = key.split(":"); pom.setGroupId(keys[0]); pom.setArtifactId(keys[1]); - pom.setVersion("0.1-SNAPSHOT"); // TODO hard-coded value + pom.setVersion(getPropertyOrDie(properties, CoreProperties.PROJECT_VERSION_PROPERTY)); - pom.getModel().setProperties(project.getProperties()); + pom.getModel().setProperties(properties); pom.setArtifacts(Collections.EMPTY_SET); // Configure fake directories - String buildDirectory = project.getProperties().getProperty("project.build.directory"); - File sonarDir = new File(buildDirectory, "sonar"); - pom.setFile(new File(sonarDir, "fake-pom.xml")); + String buildDirectory = getPropertyOrDie(properties, "project.build.directory"); pom.getBuild().setDirectory(buildDirectory); pom.getBuild().setOutputDirectory(buildDirectory + "/classes"); // TODO hard-coded value Reporting reporting = new Reporting(); - reporting.setOutputDirectory(buildDirectory + "/target/site"); // TODO hard-coded value + reporting.setOutputDirectory(buildDirectory + "/site"); // TODO hard-coded value pom.setReporting(reporting); // Configure source directories @@ -96,6 +106,14 @@ public class ProjectTree { return pom; } + private static String getPropertyOrDie(Properties properties, String key) { + String value = properties.getProperty(key); + if (StringUtils.isBlank(value)) { + throw new SonarException("Property '" + key + "' must be specified"); + } + return value; + } + /** * for unit tests */ diff --git a/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java b/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java new file mode 100644 index 00000000000..69d5a32826d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/InMemoryPomTest.java @@ -0,0 +1,67 @@ +package org.sonar.batch; + +import org.apache.maven.project.MavenProject; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.bootstrapper.ProjectDefinition; + +import java.io.File; +import java.util.Properties; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class InMemoryPomTest { + + private Properties properties; + private ProjectDefinition project; + + @Before + public void setUp() { + properties = new Properties(); + File baseDir = new File("."); + project = new ProjectDefinition(baseDir, properties); + } + + @Test + public void shouldCreate() { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); + properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0-SNAPSHOT"); + properties.setProperty("project.build.directory", "build"); + + project.addSourceDir("src"); + project.addTestDir("test"); + + MavenProject pom = ProjectTree.createInMemoryPom(project); + + assertThat(pom.getBasedir(), is(project.getBaseDir())); + assertThat(pom.getGroupId(), is("org.example")); + assertThat(pom.getArtifactId(), is("example")); + assertThat(pom.getProperties(), is(project.getProperties())); + assertThat(pom.getBuild().getDirectory(), is("build")); + + assertThat(pom.getCompileSourceRoots().size(), is(1)); + assertThat(pom.getTestCompileSourceRoots().size(), is(1)); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenKeyNotSpecified() { + ProjectTree.createInMemoryPom(project); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenVersionNotSpecified() { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); + ProjectTree.createInMemoryPom(project); + } + + @Test(expected = SonarException.class) + public void shouldFailWhenBuildDirectoryNotSpecified() { + properties.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "org.example:example"); + properties.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "0.1"); + ProjectTree.createInMemoryPom(project); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index d1455f00aec..235126c79e1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -32,6 +32,11 @@ public interface CoreProperties { String PROJECT_BRANCH_PROPERTY = "sonar.branch"; String PROJECT_VERSION_PROPERTY = "sonar.projectVersion"; + /** + * @since 2.6 + */ + String PROJECT_KEY_PROPERTY = "sonar.projectKey"; + /** * Value format is yyyy-MM-dd */ -- 2.39.5