Browse Source

Refactor code and add some API documentation.

tags/2.5-rc1
Fabrice Bellingard 11 years ago
parent
commit
b0ea3c7502

+ 3
- 5
src/main/java/org/sonar/runner/Main.java View File

@@ -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);
}
}


+ 14
- 10
src/main/java/org/sonar/runner/bootstrapper/Bootstrapper.java View File

@@ -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();
}
}

+ 2
- 1
src/main/java/org/sonar/runner/bootstrapper/package-info.java View File

@@ -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;

src/main/java/org/sonar/runner/utils/SonarRunnerIOUtils.java → src/main/java/org/sonar/runner/internal/PrivateIOUtils.java View File

@@ -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
}


+ 24
- 0
src/main/java/org/sonar/runner/internal/package-info.java View File

@@ -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;

+ 6
- 13
src/main/java/org/sonar/runner/model/SonarProjectBuilder.java View File

@@ -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)
@@ -370,14 +371,6 @@ public final class SonarProjectBuilder {
return file;
}

/**
* 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.
*/

+ 24
- 0
src/main/java/org/sonar/runner/model/package-info.java View File

@@ -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;

+ 45
- 0
src/main/java/org/sonar/runner/utils/SonarRunnerUtils.java View File

@@ -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, ""), ','));
}

}

+ 2
- 1
src/main/java/org/sonar/runner/utils/SonarRunnerVersion.java View File

@@ -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);
}
}
}

+ 23
- 0
src/main/java/org/sonar/runner/utils/package-info.java View File

@@ -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;

+ 0
- 16
src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java View File

@@ -232,22 +232,6 @@ public class SonarProjectBuilderTest {
SonarProjectBuilder.getLibraries(baseDir, "*.jar");
}

@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(), "/")))

+ 63
- 0
src/test/java/org/sonar/runner/utils/SonarRunnerUtilsTest.java View File

@@ -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;
}

}

src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/shouldGetList/foo.properties → src/test/resources/org/sonar/runner/utils/SonarRunnerUtilsTest/shouldGetList/foo.properties View File


Loading…
Cancel
Save