package org.sonar.runner;
import java.io.File;
+import java.io.FileFilter;
import java.io.InputStream;
import java.util.Properties;
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;
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, ""), ',');
}
/**
* 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.
}
static boolean isUnsupportedVersion(String version) {
- for (String unsupportedVersion : unsupportedVersions) {
+ for (String unsupportedVersion : UNSUPPORTED_VERSIONS) {
if (isVersion(version, unsupportedVersion)) {
return true;
}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
}
/**
- * 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() {
assertThat(runner.getProjectDir().isDirectory(), is(true));
assertThat(runner.getProjectDir().exists(), is(true));
}
+
}