]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-1376 Allow to use pattern like "lib/*.jar" for property "libraries"
authorEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 19 Dec 2011 10:06:23 +0000 (10:06 +0000)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 19 Dec 2011 10:06:23 +0000 (10:06 +0000)
src/main/java/org/sonar/runner/Launcher.java
src/main/java/org/sonar/runner/Runner.java
src/test/java/org/sonar/runner/LauncherTest.java [new file with mode: 0644]
src/test/java/org/sonar/runner/RunnerTest.java
src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/exclude.txt [new file with mode: 0644]
src/test/resources/org/sonar/runner/LauncherTest/shouldFilterFiles/include.txt [new file with mode: 0644]

index 7aabda7874c76098e6bc8213b0f2ea0c715414ea..ea6f9cefcd9d9dd7366bc174f18e3471f0c8a072 100644 (file)
@@ -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, ""), ',');
   }
index 64bb52ff802c919611a4e1d9c2eda5cd3d863083..a4d02b8e676ee5d391b69885841482b30229432c 100644 (file)
@@ -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 (file)
index 0000000..8b04691
--- /dev/null
@@ -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));
+  }
+
+}
index c98adc8dce92471c03d950b97e3314c7871cf5ea..202626987f467bbf131c4e8aac318bcc6238c04c 100644 (file)
@@ -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 (file)
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 (file)
index 0000000..e69de29