From 019453b021b2cbc3e4db393521e29aebf522f8ba Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Sun, 13 Nov 2011 23:22:34 +0100 Subject: Fix loading of Gradle global properties --- .../src/main/java/org/sonar/batch/Batch.java | 35 ++++++++++++++++++---- .../src/test/java/org/sonar/batch/BatchTest.java | 18 +++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) (limited to 'sonar-batch/src') diff --git a/sonar-batch/src/main/java/org/sonar/batch/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/Batch.java index 70c92d06097..d10b89398f9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/Batch.java @@ -20,20 +20,29 @@ package org.sonar.batch; import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.batch.bootstrap.BootstrapModule; import org.sonar.batch.bootstrap.Module; import org.sonar.batch.bootstrapper.Reactor; +import java.util.Iterator; +import java.util.Properties; + public final class Batch { private Module bootstrapModule; + public Batch(ProjectReactor reactor, Object... bootstrapperComponents) { + this.bootstrapModule = new BootstrapModule(reactor, bootstrapperComponents).init(); + } + /** - * @deprecated since 2.9. Replaced by the factory method. Use by Ant Task 1.1 + * @deprecated since 2.9 because commons-configuration is replaced by ProjectDefinition#properties. Used by Ant Task 1.1 */ @Deprecated - public Batch(Configuration configuration, Object... bootstrapperComponents) { + public Batch(Configuration configuration, Object... bootstrapperComponents) {//NOSONAR configuration is not needed + // because it's already included in ProjectDefinition. this.bootstrapModule = new BootstrapModule(extractProjectReactor(bootstrapperComponents), bootstrapperComponents).init(); } @@ -54,14 +63,28 @@ public final class Batch { return deprecatedReactor.toProjectReactor(); } - private Batch(ProjectReactor reactor, Object... bootstrapperComponents) { - this.bootstrapModule = new BootstrapModule(reactor, bootstrapperComponents).init(); - } - + /** + * Used by Gradle 1.0 + * @deprecated + */ + @Deprecated public static Batch create(ProjectReactor projectReactor, Configuration configuration, Object... bootstrapperComponents) { + projectReactor.getRoot().setProperties(convertToProperties(configuration)); return new Batch(projectReactor, bootstrapperComponents); } + static Properties convertToProperties(Configuration configuration) { + Properties props = new Properties(); + Iterator keys = configuration.getKeys(); + while (keys.hasNext()) { + String key = (String)keys.next(); + // Configuration#getString() automatically splits strings by comma separator. + String value = StringUtils.join(configuration.getStringArray(key), ","); + props.setProperty(key, value); + } + return props; + } + /** * for unit tests */ diff --git a/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java b/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java index 094bbb71f87..97495c022dc 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java @@ -19,9 +19,14 @@ */ package org.sonar.batch; +import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.batch.bootstrap.Module; +import java.util.Properties; + import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -59,4 +64,17 @@ public class BatchTest { } } + @Test + public void shouldConvertCommonsConfigurationToProperties() { + PropertiesConfiguration commonsConf = new PropertiesConfiguration(); + commonsConf.setProperty("foo", "Foo"); + commonsConf.setProperty("list", "One,Two"); + assertThat(commonsConf.getString("list"), is("One")); + assertThat(commonsConf.getStringArray("list")[0], is("One")); + assertThat(commonsConf.getStringArray("list")[1], is("Two")); + + Properties props = Batch.convertToProperties(commonsConf); + assertThat(props.getProperty("foo"), is("Foo")); + assertThat(props.getProperty("list"), is("One,Two")); + } } -- cgit v1.2.3