From: Evgeny Mandrikov Date: Mon, 19 Dec 2011 10:06:23 +0000 (+0000) Subject: SONARPLUGINS-1376 Allow to use pattern like "lib/*.jar" for property "libraries" X-Git-Tag: 2.5-rc1~318 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=058098984bae5172dd98d4293da1073600644537;p=sonar-scanner-cli.git SONARPLUGINS-1376 Allow to use pattern like "lib/*.jar" for property "libraries" --- diff --git a/src/main/java/org/sonar/runner/Launcher.java b/src/main/java/org/sonar/runner/Launcher.java index 7aabda7..ea6f9ce 100644 --- a/src/main/java/org/sonar/runner/Launcher.java +++ b/src/main/java/org/sonar/runner/Launcher.java @@ -21,6 +21,7 @@ package org.sonar.runner; import java.io.File; +import java.io.FileFilter; import java.io.InputStream; import java.util.Properties; @@ -29,6 +30,9 @@ import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; import org.apache.commons.configuration.*; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.filefilter.AndFileFilter; +import org.apache.commons.io.filefilter.FileFileFilter; +import org.apache.commons.io.filefilter.WildcardFileFilter; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; import org.sonar.api.utils.SonarException; @@ -91,12 +95,32 @@ public class Launcher { for (String dir : getList(properties, "binaries")) { definition.addBinaryDir(dir); } - for (String file : getList(properties, "libraries")) { - definition.addLibrary(file); + for (String pattern : getList(properties, "libraries")) { + for (File file : getLibraries(pattern)) { + definition.addLibrary(file.getAbsolutePath()); + } } return definition; } + /** + * Returns files matching specified pattern. + * Visibility has been relaxed to make code testable. + */ + static File[] getLibraries(String pattern) { + final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\')); + final String dir, filePattern; + if (i == -1) { + dir = "."; + filePattern = pattern; + } else { + dir = pattern.substring(0, i); + filePattern = pattern.substring(i + 1); + } + FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern)); + return new File(dir).listFiles(fileFilter); + } + private String[] getList(Properties properties, String key) { return StringUtils.split(properties.getProperty(key, ""), ','); } diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index 64bb52f..a4d02b8 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -51,7 +51,7 @@ public final class Runner { /** * Array of prefixes of versions of Sonar without support of this runner. */ - private static final String[] unsupportedVersions = { "1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5" }; + private static final String[] UNSUPPORTED_VERSIONS = { "1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5" }; /** * Array of all mandatory properties required to execute runner. @@ -151,7 +151,7 @@ public final class Runner { } static boolean isUnsupportedVersion(String version) { - for (String unsupportedVersion : unsupportedVersions) { + for (String unsupportedVersion : UNSUPPORTED_VERSIONS) { if (isVersion(version, unsupportedVersion)) { return true; } diff --git a/src/test/java/org/sonar/runner/LauncherTest.java b/src/test/java/org/sonar/runner/LauncherTest.java new file mode 100644 index 0000000..8b04691 --- /dev/null +++ b/src/test/java/org/sonar/runner/LauncherTest.java @@ -0,0 +1,42 @@ +/* + * 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; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.io.File; +import org.junit.Test; + +public class LauncherTest { + + @Test + public void shouldNotFailWhenPathNotSpecified() { + Launcher.getLibraries("file.jar"); + } + + @Test + public void shouldFilterFiles() throws Exception { + File dir = new File(getClass().getResource("/org/sonar/runner/LauncherTest/shouldFilterFiles/").toURI()); + assertThat(Launcher.getLibraries(dir.getAbsolutePath() + "/in*.txt").length, is(1)); + assertThat(Launcher.getLibraries(dir.getAbsolutePath() + "/*.txt").length, is(2)); + } + +} diff --git a/src/test/java/org/sonar/runner/RunnerTest.java b/src/test/java/org/sonar/runner/RunnerTest.java index c98adc8..2026269 100644 --- a/src/test/java/org/sonar/runner/RunnerTest.java +++ b/src/test/java/org/sonar/runner/RunnerTest.java @@ -68,7 +68,8 @@ public class RunnerTest { } /** - * This test can only be executed by Maven, not by IDE + * Simon: This test can only be executed by Maven, not by IDE + * Godin: This test can be executed by Eclipse */ @Test public void shouldGetVersion() { @@ -124,4 +125,5 @@ public class RunnerTest { assertThat(runner.getProjectDir().isDirectory(), is(true)); assertThat(runner.getProjectDir().exists(), is(true)); } + } diff --git a/src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/exclude.txt b/src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/exclude.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/include.txt b/src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/include.txt new file mode 100644 index 0000000..e69de29