diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-09-07 15:20:42 +0200 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-09-07 15:20:42 +0200 |
commit | b0ea3c75021e2325a4170749fe4b2cd5e96e0f56 (patch) | |
tree | afdbf06acac270966f8fbca42c014bc870eb57ea /src | |
parent | c8c554c3f13d8ec44926bb20f0b48dbec9bb4b83 (diff) | |
download | sonar-scanner-cli-b0ea3c75021e2325a4170749fe4b2cd5e96e0f56.tar.gz sonar-scanner-cli-b0ea3c75021e2325a4170749fe4b2cd5e96e0f56.zip |
Refactor code and add some API documentation.
Diffstat (limited to 'src')
13 files changed, 213 insertions, 49 deletions
diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index 12580b7..83f989c 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -20,11 +20,9 @@ package org.sonar.runner; -import org.sonar.runner.utils.SonarRunnerIOUtils; - -import org.sonar.runner.utils.SonarRunnerVersion; - import org.sonar.runner.bootstrapper.BootstrapException; +import org.sonar.runner.internal.PrivateIOUtils; +import org.sonar.runner.utils.SonarRunnerVersion; import java.io.File; import java.io.FileInputStream; @@ -161,7 +159,7 @@ public final class Main { throw new BootstrapException(e); } finally { - SonarRunnerIOUtils.closeQuietly(in); + PrivateIOUtils.closeQuietly(in); } } diff --git a/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java b/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java index 8bf7466..18b5ccc 100644 --- a/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java +++ b/src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java @@ -19,11 +19,15 @@ */ package org.sonar.runner.bootstrapper; -import org.sonar.runner.utils.SonarRunnerIOUtils; - +import org.sonar.runner.internal.PrivateIOUtils; import org.sonar.runner.utils.SonarRunnerVersion; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -110,14 +114,14 @@ public class Bootstrapper { HttpURLConnection connection = newHttpConnection(new URL(fullUrl)); output = new FileOutputStream(toFile, false); input = connection.getInputStream(); - SonarRunnerIOUtils.copyLarge(input, output); + PrivateIOUtils.copyLarge(input, output); } catch (IOException e) { - SonarRunnerIOUtils.closeQuietly(output); - SonarRunnerIOUtils.deleteFileQuietly(toFile); + PrivateIOUtils.closeQuietly(output); + PrivateIOUtils.deleteFileQuietly(toFile); throw new BootstrapException("Fail to download the file: " + fullUrl, e); } finally { - SonarRunnerIOUtils.closeQuietly(input); - SonarRunnerIOUtils.closeQuietly(output); + PrivateIOUtils.closeQuietly(input); + PrivateIOUtils.closeQuietly(output); } } @@ -130,9 +134,9 @@ public class Bootstrapper { if (statusCode != HttpURLConnection.HTTP_OK) { throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); } - return SonarRunnerIOUtils.toString(reader); + return PrivateIOUtils.toString(reader); } finally { - SonarRunnerIOUtils.closeQuietly(reader); + PrivateIOUtils.closeQuietly(reader); conn.disconnect(); } } diff --git a/src/main/java/org/sonar/runner/bootstrapper/package-info.java b/src/main/java/org/sonar/runner/bootstrapper/package-info.java index b685e7d..58094c4 100644 --- a/src/main/java/org/sonar/runner/bootstrapper/package-info.java +++ b/src/main/java/org/sonar/runner/bootstrapper/package-info.java @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ /** - * Provides API to bootstrap Sonar Batch. + * Internal package that provides API to bootstrap Sonar Batch. + * Should not be used by consumers. */ package org.sonar.runner.bootstrapper;
\ No newline at end of file diff --git a/src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java b/src/main/java/org/sonar/runner/internal/PrivateIOUtils.java index 3fac758..7b7f1de 100644 --- a/src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java +++ b/src/main/java/org/sonar/runner/internal/PrivateIOUtils.java @@ -17,7 +17,7 @@ * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.runner.utils; +package org.sonar.runner.internal; import java.io.Closeable; import java.io.File; @@ -28,9 +28,13 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; -public final class SonarRunnerIOUtils { +/** + * Internal class used only by the Runner as we don't want it to depend on third-party libs. + * This class should not be used by Sonar Runner consumers. + */ +public final class PrivateIOUtils { - private SonarRunnerIOUtils() { + private PrivateIOUtils() { // only static methods } diff --git a/src/main/java/org/sonar/runner/internal/package-info.java b/src/main/java/org/sonar/runner/internal/package-info.java new file mode 100644 index 0000000..64ccad4 --- /dev/null +++ b/src/main/java/org/sonar/runner/internal/package-info.java @@ -0,0 +1,24 @@ +/* + * Sonar Standalone Runner + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +/** + * Internal package that provides utils for internal purposes. + * Should not be used by consumers. + */ +package org.sonar.runner.internal;
\ No newline at end of file diff --git a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java index f279b84..4236848 100644 --- a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java +++ b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java @@ -31,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.runner.RunnerException; +import org.sonar.runner.utils.SonarRunnerUtils; import java.io.File; import java.io.FileFilter; @@ -145,7 +146,7 @@ public final class SonarProjectBuilder { private void defineChildren(ProjectDefinition parentProject) { Properties parentProps = parentProject.getProperties(); if (parentProps.containsKey(PROPERTY_SONAR_MODULES)) { - for (String module : getListFromProperty(parentProps, PROPERTY_SONAR_MODULES)) { + for (String module : SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_SONAR_MODULES)) { Properties moduleProps = extractModuleProperties(module, parentProps); ProjectDefinition childProject = null; if (moduleProps.containsKey(PROPERTY_MODULE_FILE)) { @@ -234,12 +235,12 @@ public final class SonarProjectBuilder { Properties properties = project.getProperties(); // We need to check the existence of source directories - String[] sourceDirs = getListFromProperty(properties, PROPERTY_SOURCES); + String[] sourceDirs = SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SOURCES); checkExistenceOfDirectories(project.getKey(), project.getBaseDir(), sourceDirs); // And we need to resolve patterns that may have been used in "sonar.libraries" List<String> libPaths = Lists.newArrayList(); - for (String pattern : getListFromProperty(properties, PROPERTY_LIBRARIES)) { + for (String pattern : SonarRunnerUtils.getListFromProperty(properties, PROPERTY_LIBRARIES)) { for (File file : getLibraries(project.getBaseDir(), pattern)) { libPaths.add(file.getAbsolutePath()); } @@ -260,7 +261,7 @@ public final class SonarProjectBuilder { // and they don't need properties related to their modules either Properties clone = (Properties) properties.clone(); - List<String> moduleIds = Lists.newArrayList(getListFromProperty(properties, PROPERTY_SONAR_MODULES)); + List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SONAR_MODULES)); for (Entry<Object, Object> entry : clone.entrySet()) { String key = (String) entry.getKey(); if (isKeyPrefixedByModuleId(key, moduleIds)) { @@ -289,7 +290,7 @@ public final class SonarProjectBuilder { @VisibleForTesting protected static void mergeParentProperties(Properties childProps, Properties parentProps) { - List<String> moduleIds = Lists.newArrayList(getListFromProperty(parentProps, PROPERTY_SONAR_MODULES)); + List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_SONAR_MODULES)); for (Map.Entry<Object, Object> entry : parentProps.entrySet()) { String key = (String) entry.getKey(); if (!childProps.containsKey(key) @@ -371,14 +372,6 @@ public final class SonarProjectBuilder { } /** - * Returns a list of comma-separated values, even if they are separated by whitespace characters (space char, EOL, ...) - */ - @VisibleForTesting - protected static String[] getListFromProperty(Properties properties, String key) { - return StringUtils.stripAll(StringUtils.split(properties.getProperty(key, ""), ',')); - } - - /** * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute. */ @VisibleForTesting diff --git a/src/main/java/org/sonar/runner/model/package-info.java b/src/main/java/org/sonar/runner/model/package-info.java new file mode 100644 index 0000000..4827391 --- /dev/null +++ b/src/main/java/org/sonar/runner/model/package-info.java @@ -0,0 +1,24 @@ +/* + * Sonar Standalone Runner + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +/** + * Internal package that creates the project definition and launches the analyses based on it. + * Should not be used by consumers. + */ +package org.sonar.runner.model;
\ No newline at end of file diff --git a/src/main/java/org/sonar/runner/utils/SonarRunnerUtils.java b/src/main/java/org/sonar/runner/utils/SonarRunnerUtils.java new file mode 100644 index 0000000..d0a2faf --- /dev/null +++ b/src/main/java/org/sonar/runner/utils/SonarRunnerUtils.java @@ -0,0 +1,45 @@ +/* + * Sonar Standalone Runner + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.utils; + +import org.apache.commons.lang.StringUtils; + +import java.util.Properties; + +/** + * Public utility that can be used by consumers of the Sonar Runner. + */ +public final class SonarRunnerUtils { + + private SonarRunnerUtils() { + // only static methods + } + + /** + * Transforms a comma-separated list String property in to a array of trimmed strings. + * + * This works even if they are separated by whitespace characters (space char, EOL, ...) + * + */ + public static String[] getListFromProperty(Properties properties, String key) { + return StringUtils.stripAll(StringUtils.split(properties.getProperty(key, ""), ',')); + } + +} diff --git a/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java index 12622fa..5cc1577 100644 --- a/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java +++ b/src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java @@ -19,6 +19,7 @@ */ package org.sonar.runner.utils; +import org.sonar.runner.internal.PrivateIOUtils; import java.io.IOException; import java.io.InputStream; @@ -47,7 +48,7 @@ public enum SonarRunnerVersion { this.version = ""; } finally { - SonarRunnerIOUtils.closeQuietly(input); + PrivateIOUtils.closeQuietly(input); } } } diff --git a/src/main/java/org/sonar/runner/utils/package-info.java b/src/main/java/org/sonar/runner/utils/package-info.java new file mode 100644 index 0000000..54da5c6 --- /dev/null +++ b/src/main/java/org/sonar/runner/utils/package-info.java @@ -0,0 +1,23 @@ +/* + * Sonar Standalone Runner + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +/** + * Public package that provides utils and can be used by consumers. + */ +package org.sonar.runner.utils;
\ No newline at end of file diff --git a/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java b/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java index d27e88c..b3f91bd 100644 --- a/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java +++ b/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java @@ -233,22 +233,6 @@ public class SonarProjectBuilderTest { } @Test - public void shouldGetList() { - Properties props = new Properties(); - - props.put("prop", " foo , bar , \n\ntoto,tutu"); - assertThat(SonarProjectBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); - } - - @Test - public void shouldGetListFromFile() throws IOException { - String filePath = "shouldGetList/foo.properties"; - Properties props = loadPropsFromFile(filePath); - - assertThat(SonarProjectBuilder.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); - } - - @Test public void shouldGetRelativeFile() { assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/"))) .isEqualTo(TestUtils.getResource("org/sonar/runner/model/SonarProjectBuilderTest/shouldGetFile/foo.properties")); diff --git a/src/test/java/org/sonar/runner/utils/SonarRunnerUtilsTest.java b/src/test/java/org/sonar/runner/utils/SonarRunnerUtilsTest.java new file mode 100644 index 0000000..c98c8f8 --- /dev/null +++ b/src/test/java/org/sonar/runner/utils/SonarRunnerUtilsTest.java @@ -0,0 +1,63 @@ +/* + * Sonar Standalone Runner + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.utils; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.sonar.test.TestUtils; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +import static org.fest.assertions.Assertions.assertThat; + +public class SonarRunnerUtilsTest { + + @Test + public void shouldGetList() { + Properties props = new Properties(); + + props.put("prop", " foo , bar , \n\ntoto,tutu"); + assertThat(SonarRunnerUtils.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); + } + + @Test + public void shouldGetListFromFile() throws IOException { + String filePath = "shouldGetList/foo.properties"; + Properties props = loadPropsFromFile(filePath); + + assertThat(SonarRunnerUtils.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu"); + } + + private Properties loadPropsFromFile(String filePath) throws FileNotFoundException, IOException { + Properties props = new Properties(); + FileInputStream fileInputStream = null; + try { + fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath)); + props.load(fileInputStream); + } finally { + IOUtils.closeQuietly(fileInputStream); + } + return props; + } + +} diff --git a/src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/shouldGetList/foo.properties b/src/test/resources/org/sonar/runner/utils/SonarRunnerUtilsTest/shouldGetList/foo.properties index 8fbb104..8fbb104 100644 --- a/src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/shouldGetList/foo.properties +++ b/src/test/resources/org/sonar/runner/utils/SonarRunnerUtilsTest/shouldGetList/foo.properties |