diff options
Diffstat (limited to 'sonar-batch')
5 files changed, 18 insertions, 347 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilder.java deleted file mode 100644 index 38388b50ede..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilder.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.batch.analysis.AnalysisProperties; - -import org.sonar.api.batch.bootstrap.ProjectDefinition; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -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(AnalysisProperties props) { - super(props); - } - - @Override - protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) { - final File baseDir; - if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.get(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.properties()); - - return defineRootProject(moduleProps, parentProject); - } - - /** - * @return baseDir - */ - private File loadPropsFile(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) { - File propertyFile = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_CONFIG_FILE)); - if (propertyFile.isFile()) { - Properties propsFromFile = toProperties(propertyFile); - for (Entry<Object, Object> entry : propsFromFile.entrySet()) { - moduleProps.put(entry.getKey().toString(), entry.getValue().toString()); - } - File baseDir = null; - if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - baseDir = resolvePath(propertyFile.getParentFile(), moduleProps.get(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, Map<String, String> 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().toString(), entry.getValue().toString()); - } - if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - File overwrittenBaseDir = resolvePath(propertyFile.getParentFile(), moduleProps.get(PROPERTY_PROJECT_BASEDIR)); - setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId); - } - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java index a20e8abd3e4..96d7bad1c5c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java @@ -263,21 +263,6 @@ public class ProjectReactorBuilder { } @VisibleForTesting - protected static Properties toProperties(File propertyFile) { - Properties propsFromFile = new Properties(); - try (FileInputStream fileInputStream = new FileInputStream(propertyFile)) { - propsFromFile.load(fileInputStream); - } catch (IOException e) { - throw new IllegalStateException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e); - } - // Trim properties - for (String propKey : propsFromFile.stringPropertyNames()) { - propsFromFile.setProperty(propKey, StringUtils.trim(propsFromFile.getProperty(propKey))); - } - return propsFromFile; - } - - @VisibleForTesting protected static void setModuleKeyAndNameIfNotDefined(Map<String, String> childProps, String moduleId, String parentKey) { if (!childProps.containsKey(MODULE_KEY_PROPERTY)) { if (!childProps.containsKey(CoreProperties.PROJECT_KEY_PROPERTY)) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index c8ddc3d869e..b00888779cf 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -48,7 +48,6 @@ import org.sonar.batch.bootstrap.ExtensionInstaller; import org.sonar.batch.bootstrap.ExtensionMatcher; import org.sonar.batch.bootstrap.ExtensionUtils; import org.sonar.batch.bootstrap.MetricProvider; -import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; import org.sonar.batch.index.BatchComponentCache; @@ -112,24 +111,11 @@ public class ProjectScanContainer extends ComponentContainer { } } - private Class<?> projectReactorBuilder() { - if (isRunnerVersionLessThan2Dot4()) { - return DeprecatedProjectReactorBuilder.class; - } - return ProjectReactorBuilder.class; - } - - private boolean isRunnerVersionLessThan2Dot4() { - 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 addBatchComponents() { add( props, DefaultAnalysisMode.class, - projectReactorBuilder(), + ProjectReactorBuilder.class, new MutableProjectReactorProvider(), new ImmutableProjectReactorProvider(), ProjectBuildersExecutor.class, diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilderTest.java deleted file mode 100644 index 0aaf6e2dfd2..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/DeprecatedProjectReactorBuilderTest.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * 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.batch.analysis.AnalysisProperties; - -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.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.assertj.core.api.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() { - 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) { - 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()); - AnalysisProperties taskProps = new AnalysisProperties(props, null); - ProjectReactor projectReactor = new DeprecatedProjectReactorBuilder(taskProps).execute(); - return projectReactor.getRoot(); - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java index 82db9d87976..5ba26864ec4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorBuilderTest.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.scan; -import org.sonar.batch.analysis.AnalysisProperties; +import org.apache.commons.lang.StringUtils; +import org.sonar.batch.analysis.AnalysisProperties; import com.google.common.collect.Maps; import org.junit.Rule; import org.junit.Test; @@ -515,24 +516,30 @@ public class ProjectReactorBuilderTest { assertThat(props.get("sonar.projectName")).isEqualTo("foo"); } - @Test - public void shouldFailToLoadPropertiesFile() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Impossible to read the property file"); - - ProjectReactorBuilder.toProperties(new File("foo.properties")); - } - private ProjectDefinition loadProjectDefinition(String projectFolder) { Map<String, String> props = loadProps(projectFolder); AnalysisProperties bootstrapProps = new AnalysisProperties(props, null); ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps).execute(); return projectReactor.getRoot(); } + + protected static Properties toProperties(File propertyFile) { + Properties propsFromFile = new Properties(); + try (FileInputStream fileInputStream = new FileInputStream(propertyFile)) { + propsFromFile.load(fileInputStream); + } catch (IOException e) { + throw new IllegalStateException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e); + } + // Trim properties + for (String propKey : propsFromFile.stringPropertyNames()) { + propsFromFile.setProperty(propKey, StringUtils.trim(propsFromFile.getProperty(propKey))); + } + return propsFromFile; + } private Map<String, String> loadProps(String projectFolder) { Map<String, String> props = Maps.<String, String>newHashMap(); - Properties runnerProps = ProjectReactorBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties")); + Properties runnerProps = toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties")); for (final String name : runnerProps.stringPropertyNames()) { props.put(name, runnerProps.getProperty(name)); } |