import org.sonar.api.utils.System2;
import org.sonar.api.utils.UriReader;
import org.sonar.api.utils.internal.TempFolderCleaner;
+import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.batch.components.PastMeasuresLoader;
import org.sonar.batch.components.PastSnapshotFinder;
import org.sonar.batch.components.PastSnapshotFinderByDate;
import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis;
import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion;
import org.sonar.batch.components.PastSnapshotFinderByVersion;
+import org.sonar.batch.scan.DeprecatedProjectReactorBuilder;
import org.sonar.batch.scan.ProjectReactorBuilder;
import org.sonar.core.config.Logback;
import org.sonar.core.i18n.DefaultI18n;
UriReader.class,
new FileCacheProvider(),
System2.INSTANCE,
- ProjectReactorBuilder.class);
+ projectReactorBuilder());
+ }
+
+ private Class<? extends ProjectReactorBuilder> projectReactorBuilder() {
+ if (isRunnerPre2_4()) {
+ return DeprecatedProjectReactorBuilder.class;
+ }
+ return ProjectReactorBuilder.class;
+ }
+
+ private boolean isRunnerPre2_4() {
+ EnvironmentInformation env = this.getComponentByType(EnvironmentInformation.class);
+ // Starting from SQ Runner 2.4 the key is "SonarQubeRunner"
+ return env != null && "SonarRunner".equals(env.getKey());
}
private void addDatabaseComponents() {
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.scan;
+
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.batch.bootstrap.BootstrapSettings;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+/**
+ * Class that creates a project definition based on a set of properties. This class is intended to work with
+ * SQ Runner <= 2.3. Starting from SQ Runner 2.4 loading of sonar-project.properties file should not be done by
+ * the core.
+ * @deprecated since 4.3 should be removed when we will require SQ Runner 2.4+
+ */
+@Deprecated
+public class DeprecatedProjectReactorBuilder extends ProjectReactorBuilder {
+
+ private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
+
+ public DeprecatedProjectReactorBuilder(BootstrapSettings settings) {
+ super(settings);
+ }
+
+ @Override
+ protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
+ final File baseDir;
+ if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
+ baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
+ setProjectBaseDir(baseDir, moduleProps, moduleId);
+ try {
+ if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
+ tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Error when resolving baseDir", e);
+ }
+ } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
+ baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
+ } else {
+ baseDir = new File(parentProject.getBaseDir(), moduleId);
+ setProjectBaseDir(baseDir, moduleProps, moduleId);
+ tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
+ }
+
+ setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey());
+
+ // and finish
+ checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
+ validateDirectories(moduleProps, baseDir, moduleId);
+
+ mergeParentProperties(moduleProps, parentProject.getProperties());
+
+ return defineProject(moduleProps, parentProject);
+ }
+
+ /**
+ * @return baseDir
+ */
+ private File loadPropsFile(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
+ File propertyFile = resolvePath(parentProject.getBaseDir(), moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE));
+ if (propertyFile.isFile()) {
+ Properties propsFromFile = toProperties(propertyFile);
+ for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
+ moduleProps.put(entry.getKey(), entry.getValue());
+ }
+ File baseDir = null;
+ if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
+ baseDir = resolvePath(propertyFile.getParentFile(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
+ } else {
+ baseDir = propertyFile.getParentFile();
+ }
+ setProjectBaseDir(baseDir, moduleProps, moduleId);
+ return baseDir;
+ } else {
+ throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
+ }
+ }
+
+ private void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
+ File propertyFile = new File(baseDir, "sonar-project.properties");
+ if (propertyFile.isFile()) {
+ Properties propsFromFile = toProperties(propertyFile);
+ for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
+ moduleProps.put(entry.getKey(), entry.getValue());
+ }
+ if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
+ File overwrittenBaseDir = resolvePath(propertyFile.getParentFile(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
+ setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
+ }
+ }
+ }
+
+}
*/
private static final String MODULE_KEY_PROPERTY = "sonar.moduleKey";
- private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
+ protected static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
private static final String PROPERTY_PROJECT_BUILDDIR = "sonar.projectBuildDir";
- private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
private static final String PROPERTY_MODULES = "sonar.modules";
/**
/**
* Array of all mandatory properties required for a child project before its properties get merged with its parent ones.
*/
- private static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {MODULE_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY};
+ protected static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {MODULE_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY};
/**
* Properties that must not be passed from the parent project to its children.
return new ProjectReactor(rootProject);
}
- private ProjectDefinition defineProject(Properties properties, ProjectDefinition parent) {
+ protected ProjectDefinition defineProject(Properties properties, ProjectDefinition parent) {
File baseDir = new File(properties.getProperty(PROPERTY_PROJECT_BASEDIR));
if (properties.containsKey(PROPERTY_MODULES)) {
checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT);
}
}
- private ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
+ protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
final File baseDir;
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), parentProject.getBaseDir());
+ baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR));
setProjectBaseDir(baseDir, moduleProps, moduleId);
- try {
- if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Error when resolving baseDir", e);
- }
- } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
- baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
} else {
baseDir = new File(parentProject.getBaseDir(), moduleId);
setProjectBaseDir(baseDir, moduleProps, moduleId);
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
}
setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey());
return defineProject(moduleProps, parentProject);
}
- /**
- * @return baseDir
- */
- protected File loadPropsFile(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
- File propertyFile = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentProject.getBaseDir());
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey(), entry.getValue());
- }
- File baseDir = null;
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
- } else {
- baseDir = propertyFile.getParentFile();
- }
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- return baseDir;
- } else {
- throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
- }
- }
-
- private void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
- File propertyFile = new File(baseDir, "sonar-project.properties");
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey(), entry.getValue());
- }
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- File overwrittenBaseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
- setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
- }
- }
- }
-
@VisibleForTesting
protected static Properties toProperties(File propertyFile) {
Properties propsFromFile = new Properties();
}
}
- private static void setProjectBaseDir(File baseDir, Properties childProps, String moduleId) {
+ protected static void setProjectBaseDir(File baseDir, Properties childProps, String moduleId) {
if (!baseDir.isDirectory()) {
throw new IllegalStateException("The base directory of the module '" + moduleId + "' does not exist: " + baseDir.getAbsolutePath());
}
}
}
- private static void validateDirectories(Properties props, File baseDir, String projectId) {
+ protected static void validateDirectories(Properties props, File baseDir, String projectId) {
if (!props.containsKey(PROPERTY_MODULES)) {
// SONARPLUGINS-2285 Not an aggregator project so we can validate that paths are correct if defined
// SONARPLUGINS-2295
String[] sourceDirs = getListFromProperty(properties, PROPERTY_SOURCES);
for (String path : sourceDirs) {
- File sourceFolder = getFileFromPath(path, project.getBaseDir());
+ File sourceFolder = resolvePath(project.getBaseDir(), path);
if (sourceFolder.isDirectory()) {
LOG.warn("/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " +
"If you want to analyse files of this folder, you should create another sub-module and move them inside it.",
@VisibleForTesting
protected static void checkExistenceOfDirectories(String moduleRef, File baseDir, String[] sourceDirs, String propName) {
for (String path : sourceDirs) {
- File sourceFolder = getFileFromPath(path, baseDir);
+ File sourceFolder = resolvePath(baseDir, path);
if (!sourceFolder.isDirectory()) {
LOG.error("Invalid value of " + propName + " for " + moduleRef);
throw new IllegalStateException("The folder '" + path + "' does not exist for '" + moduleRef +
return files;
}
- private static File resolvePath(File baseDir, String path) {
+ protected static File resolvePath(File baseDir, String path) {
File file = new File(path);
if (!file.isAbsolute()) {
try {
return file;
}
- /**
- * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
- */
- @VisibleForTesting
- protected static File getFileFromPath(String path, File baseDir) {
- File propertyFile = new File(path.trim());
- if (!propertyFile.isAbsolute()) {
- propertyFile = new File(baseDir, propertyFile.getPath());
- }
- return propertyFile;
- }
-
/**
* Transforms a comma-separated list String property in to a array of trimmed strings.
*
import org.sonar.api.SonarPlugin;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.utils.TempFolder;
+import org.sonar.batch.bootstrapper.EnvironmentInformation;
+import org.sonar.batch.scan.DeprecatedProjectReactorBuilder;
+import org.sonar.batch.scan.ProjectReactorBuilder;
import org.sonar.core.config.Logback;
import java.util.Arrays;
assertThat(container.getComponentsByType(Plugin.class)).containsOnly(plugin);
}
+ @Test
+ public void should_add_project_reactor_builder_by_default() {
+ BootstrapContainer container = BootstrapContainer.create(Lists.newArrayList());
+ container.add(new BootstrapProperties(Collections.<String, String>emptyMap()));
+ container.doBeforeStart();
+
+ assertThat(container.getComponentByType(ProjectReactorBuilder.class)).isNotNull().isInstanceOf(ProjectReactorBuilder.class);
+
+ container = BootstrapContainer.create(Lists.newArrayList(new EnvironmentInformation("SonarQubeRunner", "2.4")));
+ container.add(new BootstrapProperties(Collections.<String, String>emptyMap()));
+ container.doBeforeStart();
+
+ assertThat(container.getComponentByType(ProjectReactorBuilder.class)).isNotNull().isInstanceOf(ProjectReactorBuilder.class);
+ }
+
+ @Test
+ public void should_add_deprecated_project_reactor_builder_if_old_runner() {
+ BootstrapContainer container = BootstrapContainer.create(Lists.newArrayList(new EnvironmentInformation("SonarRunner", "2.3")));
+ container.add(new BootstrapProperties(Collections.<String, String>emptyMap()));
+ container.doBeforeStart();
+
+ assertThat(container.getComponentByType(DeprecatedProjectReactorBuilder.class)).isNotNull().isInstanceOf(DeprecatedProjectReactorBuilder.class);
+ }
+
public static class Foo implements BatchExtension {
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.scan;
+
+import com.google.common.collect.Maps;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.batch.bootstrap.BootstrapProperties;
+import org.sonar.batch.bootstrap.BootstrapSettings;
+import org.sonar.test.TestUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DeprecatedProjectReactorBuilderTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void shouldDefineMultiModuleProjectWithDefinitionsAllInEachModule() throws IOException {
+ ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module");
+
+ // CHECK ROOT
+ assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
+ assertThat(rootProject.getName()).isEqualTo("Foo Project");
+ assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
+ // root project must not contain some properties - even if they are defined in the root properties file
+ assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
+ assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
+ assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
+ // and module properties must have been cleaned
+ assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+
+ // CHECK MODULES
+ List<ProjectDefinition> modules = rootProject.getSubProjects();
+ assertThat(modules.size()).isEqualTo(2);
+
+ // Module 1
+ ProjectDefinition module1 = modules.get(0);
+ assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
+ assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
+ assertThat(module1.getName()).isEqualTo("Foo Module 1");
+ assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ // Description should not be inherited from parent if not set
+ assertThat(module1.getDescription()).isEqualTo("Description of Module 1");
+ assertThat(module1.getSourceDirs()).contains("sources");
+ assertThat(module1.getTestDirs()).contains("tests");
+ assertThat(module1.getBinaries()).contains("target/classes");
+ // and module properties must have been cleaned
+ assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+
+ // Module 2
+ ProjectDefinition module2 = modules.get(1);
+ assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module2/newBaseDir"));
+ assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
+ assertThat(module2.getName()).isEqualTo("Foo Module 2");
+ assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
+ assertThat(module2.getSourceDirs()).contains("src");
+ assertThat(module2.getTestDirs()).contains("tests");
+ assertThat(module2.getBinaries()).contains("target/classes");
+ // and module properties must have been cleaned
+ assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+ }
+
+ @Test
+ public void shouldDefineMultiModuleProjectWithDefinitionsModule1Inherited() throws IOException {
+ ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module-inherited");
+
+ // CHECK ROOT
+ assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
+ assertThat(rootProject.getName()).isEqualTo("Foo Project");
+ assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
+ // root project must not contain some properties - even if they are defined in the root properties file
+ assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
+ assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
+ assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
+ // and module properties must have been cleaned
+ assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+
+ // CHECK MODULES
+ List<ProjectDefinition> modules = rootProject.getSubProjects();
+ assertThat(modules.size()).isEqualTo(2);
+
+ // Module 1
+ ProjectDefinition module1 = modules.get(0);
+ assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module1"));
+ assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
+ assertThat(module1.getName()).isEqualTo("module1");
+ assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ // Description should not be inherited from parent if not set
+ assertThat(module1.getDescription()).isNull();
+ assertThat(module1.getSourceDirs()).contains("sources");
+ assertThat(module1.getTestDirs()).contains("tests");
+ assertThat(module1.getBinaries()).contains("target/classes");
+ // and module properties must have been cleaned
+ assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+
+ // Module 2
+ ProjectDefinition module2 = modules.get(1);
+ assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module2/newBaseDir"));
+ assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
+ assertThat(module2.getName()).isEqualTo("Foo Module 2");
+ assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
+ assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
+ assertThat(module2.getSourceDirs()).contains("src");
+ assertThat(module2.getTestDirs()).contains("tests");
+ assertThat(module2.getBinaries()).contains("target/classes");
+ // and module properties must have been cleaned
+ assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
+ assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
+ }
+
+ @Test
+ public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
+ ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
+ List<ProjectDefinition> modules = rootProject.getSubProjects();
+ assertThat(modules.size()).isEqualTo(1);
+ ProjectDefinition module = modules.get(0);
+ assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
+ // verify the base directory that has been changed in this config file
+ assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
+ }
+
+ @Test
+ public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
+ ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
+ List<ProjectDefinition> modules = rootProject.getSubProjects();
+ assertThat(modules.size()).isEqualTo(1);
+ ProjectDefinition module = modules.get(0);
+ assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
+ // verify the base directory that has been changed in this config file
+ assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
+ }
+
+ @Test
+ public void shouldFailIfUnexistingModuleFile() throws IOException {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("The properties file of the module 'module1' does not exist: "
+ + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
+ + File.separator + "any-file.properties");
+
+ loadProjectDefinition("multi-module-with-unexisting-file");
+ }
+
+ private ProjectDefinition loadProjectDefinition(String projectFolder) throws IOException {
+ Map<String, String> props = Maps.<String, String>newHashMap();
+ Properties runnerProps = ProjectReactorBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
+ for (final String name : runnerProps.stringPropertyNames()) {
+ props.put(name, runnerProps.getProperty(name));
+ }
+ props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
+ BootstrapProperties bootstrapProps = new BootstrapProperties(props);
+ ProjectReactor projectReactor = new DeprecatedProjectReactorBuilder(new BootstrapSettings(bootstrapProps)).execute();
+ return projectReactor.getRoot();
+ }
+
+}
.isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2"));
}
- @Test
- public void shouldDefineMultiModuleProjectWithDefinitionsAllInEachModule() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module");
-
- // CHECK ROOT
- assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
- assertThat(rootProject.getName()).isEqualTo("Foo Project");
- assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
- // root project must not contain some properties - even if they are defined in the root properties file
- assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
- assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
- assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
- // and module properties must have been cleaned
- assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // CHECK MODULES
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(2);
-
- // Module 1
- ProjectDefinition module1 = modules.get(0);
- assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
- assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- assertThat(module1.getName()).isEqualTo("Foo Module 1");
- assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
- // Description should not be inherited from parent if not set
- assertThat(module1.getDescription()).isEqualTo("Description of Module 1");
- assertThat(module1.getSourceDirs()).contains("sources");
- assertThat(module1.getTestDirs()).contains("tests");
- assertThat(module1.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // Module 2
- ProjectDefinition module2 = modules.get(1);
- assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module2/newBaseDir"));
- assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
- assertThat(module2.getName()).isEqualTo("Foo Module 2");
- assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
- assertThat(module2.getSourceDirs()).contains("src");
- assertThat(module2.getTestDirs()).contains("tests");
- assertThat(module2.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
- }
-
// SONAR-4876
@Test
public void shouldDefineMultiModuleProjectWithModuleKey() throws IOException {
assertThat(module2.getKey()).isEqualTo("com.foo.project.module2");
}
- @Test
- public void shouldDefineMultiModuleProjectWithDefinitionsModule1Inherited() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module-inherited");
-
- // CHECK ROOT
- assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
- assertThat(rootProject.getName()).isEqualTo("Foo Project");
- assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
- // root project must not contain some properties - even if they are defined in the root properties file
- assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
- assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
- assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
- // and module properties must have been cleaned
- assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // CHECK MODULES
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(2);
-
- // Module 1
- ProjectDefinition module1 = modules.get(0);
- assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module1"));
- assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
- assertThat(module1.getName()).isEqualTo("module1");
- assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
- // Description should not be inherited from parent if not set
- assertThat(module1.getDescription()).isNull();
- assertThat(module1.getSourceDirs()).contains("sources");
- assertThat(module1.getTestDirs()).contains("tests");
- assertThat(module1.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
-
- // Module 2
- ProjectDefinition module2 = modules.get(1);
- assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module2/newBaseDir"));
- assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
- assertThat(module2.getName()).isEqualTo("Foo Module 2");
- assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
- assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
- assertThat(module2.getSourceDirs()).contains("src");
- assertThat(module2.getTestDirs()).contains("tests");
- assertThat(module2.getBinaries()).contains("target/classes");
- // and module properties must have been cleaned
- assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
- assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
- }
-
// SONARPLUGINS-2421
@Test
public void shouldDefineMultiLanguageProjectWithDefinitionsAllInRootProject() throws IOException {
assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
}
- @Test
- public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(1);
- ProjectDefinition module = modules.get(0);
- assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- // verify the base directory that has been changed in this config file
- assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
- }
-
- @Test
- public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
- ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
- List<ProjectDefinition> modules = rootProject.getSubProjects();
- assertThat(modules.size()).isEqualTo(1);
- ProjectDefinition module = modules.get(0);
- assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
- // verify the base directory that has been changed in this config file
- assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
- }
-
@Test
public void shouldFailIfUnexistingModuleBaseDir() throws IOException {
thrown.expect(IllegalStateException.class);
loadProjectDefinition("multi-module-with-unexisting-basedir");
}
- @Test
- public void shouldFailIfUnexistingModuleFile() throws IOException {
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("The properties file of the module 'module1' does not exist: "
- + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
- + File.separator + "any-file.properties");
-
- loadProjectDefinition("multi-module-with-unexisting-file");
- }
-
@Test
public void shouldFailIfUnexistingSourceFolderInheritedInMultimodule() throws IOException {
thrown.expect(IllegalStateException.class);
@Test
public void shouldGetRelativeFile() {
- assertThat(ProjectReactorBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
+ assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), "shouldGetFile/foo.properties"))
.isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"));
}
public void shouldGetAbsoluteFile() {
File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties");
- assertThat(ProjectReactorBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
+ assertThat(ProjectReactorBuilder.resolvePath(TestUtils.getResource(this.getClass(), "/"), file.getAbsolutePath()))
.isEqualTo(file);
}
assertThat(ProjectReactorBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
}
+ @Test
+ public void shouldDefineProjectWithBuildDir() throws IOException {
+ ProjectDefinition rootProject = loadProjectDefinition("simple-project-with-build-dir");
+ File buildDir = rootProject.getBuildDir();
+ assertThat(buildDir).isDirectory().exists();
+ assertThat(new File(buildDir, "report.txt")).isFile().exists();
+ assertThat(buildDir.getName()).isEqualTo("build");
+ }
+
private Properties loadPropsFromFile(String filePath) throws IOException {
Properties props = new Properties();
FileInputStream fileInputStream = null;
--- /dev/null
+# Mandatory properties for module1 are all inferred from the module ID
--- /dev/null
+sonar.projectKey=com.foo.project.module2
+sonar.projectName=Foo Module 2
+# redefine some properties
+sonar.projectBaseDir=newBaseDir
+sonar.projectDescription=Description of Module 2
+sonar.sources=src
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.sources=sources
+sonar.tests=tests
+sonar.binaries=target/classes
+
+sonar.modules=module1,\
+ module2
--- /dev/null
+sonar.projectKey=com.foo.project.module1
+sonar.projectName=Foo Module 1
+sonar.projectDescription=Description of Module 1
+sonar.sources=sources
--- /dev/null
+sonar.projectKey=com.foo.project.module2
+sonar.projectName=Foo Module 2
+# redefine some properties
+sonar.projectBaseDir=newBaseDir
+sonar.projectDescription=Description of Module 2
+sonar.sources=src
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.tests=tests
+sonar.binaries=target/classes
+
+sonar.modules=module1,\
+ module2
--- /dev/null
+sonar.projectKey=com.foo.project.module1
+sonar.projectName=Foo Module 1
+
+# and specify a different baseDir
+sonar.projectBaseDir=..
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.sources=sources
+sonar.tests=tests
+sonar.binaries=target/classes
+
+sonar.modules=module1
+
+module1.sonar.projectConfigFile=any-folder/generated/any-file.properties
--- /dev/null
+sonar.projectKey=com.foo.project.module1
+sonar.projectName=Foo Module 1
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.sources=sources
+sonar.tests=tests
+sonar.binaries=target/classes
+
+sonar.modules=module1
+
+module1.sonar.projectConfigFile=any-folder/any-file.properties
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.sources=sources
+sonar.tests=tests
+sonar.binaries=target/classes
+
+sonar.modules=module1
+
+module1.sonar.projectConfigFile=any-folder/any-file.properties
+++ /dev/null
-# Mandatory properties for module1 are all inferred from the module ID
+++ /dev/null
-sonar.projectKey=com.foo.project.module2
-sonar.projectName=Foo Module 2
-# redefine some properties
-sonar.projectBaseDir=newBaseDir
-sonar.projectDescription=Description of Module 2
-sonar.sources=src
+++ /dev/null
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.sources=sources
-sonar.tests=tests
-sonar.binaries=target/classes
-
-sonar.modules=module1,\
- module2
+++ /dev/null
-sonar.projectKey=com.foo.project.module1
-sonar.projectName=Foo Module 1
-sonar.projectDescription=Description of Module 1
-sonar.sources=sources
+++ /dev/null
-sonar.projectKey=com.foo.project.module2
-sonar.projectName=Foo Module 2
-# redefine some properties
-sonar.projectBaseDir=newBaseDir
-sonar.projectDescription=Description of Module 2
-sonar.sources=src
+++ /dev/null
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.tests=tests
-sonar.binaries=target/classes
-
-sonar.modules=module1,\
- module2
+++ /dev/null
-sonar.projectKey=com.foo.project.module1
-sonar.projectName=Foo Module 1
-
-# and specify a different baseDir
-sonar.projectBaseDir=..
+++ /dev/null
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.sources=sources
-sonar.tests=tests
-sonar.binaries=target/classes
-
-sonar.modules=module1
-
-module1.sonar.projectConfigFile=any-folder/generated/any-file.properties
+++ /dev/null
-sonar.projectKey=com.foo.project.module1
-sonar.projectName=Foo Module 1
+++ /dev/null
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.sources=sources
-sonar.tests=tests
-sonar.binaries=target/classes
-
-sonar.modules=module1
-
-module1.sonar.projectConfigFile=any-folder/any-file.properties
+++ /dev/null
-sonar.projectKey=com.foo.project
-sonar.projectName=Foo Project
-sonar.projectVersion=1.0-SNAPSHOT
-sonar.projectDescription=Description of Foo Project
-
-sonar.sources=sources
-sonar.tests=tests
-sonar.binaries=target/classes
-
-sonar.modules=module1
-
-module1.sonar.projectConfigFile=any-folder/any-file.properties
--- /dev/null
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+sonar.projectBuildDir=build
+
+sonar.sources=sources
+
--- /dev/null
+package org.sonar.runner.batch.ProjectReactorBuilderTest.simple
+
+Fake