From dee520098e4765da4181b6a3f705d1d6d5b3af85 Mon Sep 17 00:00:00 2001 From: SimonBrandhof Date: Tue, 9 Apr 2013 15:04:00 +0200 Subject: [PATCH] SONARPLUGINS-2775 Support "**" pattern for ".class" files in "sonar.libraries" --- .../org/sonar/runner/batch/FilePattern.java | 131 ++++++++++++++++++ .../runner/batch/ProjectReactorBuilder.java | 28 +--- .../sonar/runner/batch/FilePatternTest.java | 72 ++++++++++ .../batch/ProjectReactorBuilderTest.java | 10 +- .../runner/batch/FilePatternTest/root.txt | 0 .../batch/FilePatternTest/subdir/subfile.txt | 0 sonar-runner-dist/pom.xml | 4 +- 7 files changed, 215 insertions(+), 30 deletions(-) create mode 100644 sonar-runner-batch/src/main/java/org/sonar/runner/batch/FilePattern.java create mode 100644 sonar-runner-batch/src/test/java/org/sonar/runner/batch/FilePatternTest.java create mode 100644 sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/root.txt create mode 100644 sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/FilePattern.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/FilePattern.java new file mode 100644 index 0000000..6c4bebc --- /dev/null +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/FilePattern.java @@ -0,0 +1,131 @@ +/* + * Sonar Runner - Batch + * 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.batch; + +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.filefilter.AndFileFilter; +import org.apache.commons.io.filefilter.FileFileFilter; +import org.apache.commons.io.filefilter.IOFileFilter; +import org.apache.commons.io.filefilter.TrueFileFilter; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.utils.WildcardPattern; + +import java.io.File; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +class FilePattern { + + Collection listFiles(File basedir, String pattern) { + File absoluteBasedir = absoluteBasedir(pattern); + IOFileFilter filter; + if (absoluteBasedir != null) { + filter = new AbsolutePathFilter(WildcardPattern.create(pattern, "/")); + basedir = absoluteBasedir.isFile() ? absoluteBasedir.getParentFile() : absoluteBasedir; + } else { + filter = new RelativePathFilter(basedir, WildcardPattern.create(pattern, "/")); + } + if (basedir.isDirectory() && basedir.exists()) { + return FileUtils.listFiles(basedir, new AndFileFilter(FileFileFilter.FILE, filter), TrueFileFilter.TRUE); + } + return Collections.emptyList(); + } + + + private File absoluteBasedir(String pattern) { + File absoluteBasedir = null; + int wildcard = StringUtils.indexOfAny(pattern, new char[]{'*', '?'}); + + if (wildcard == 0) { + // relative path + + } else if (wildcard == -1) { + absoluteBasedir = new File(pattern); + + } else { + int lastSlashBeforeWildcard = pattern.substring(0, wildcard).lastIndexOf("/"); + if (lastSlashBeforeWildcard >= 0) { + String path = pattern.substring(0, lastSlashBeforeWildcard); + absoluteBasedir = new File(path); + } + } + if (absoluteBasedir != null && !absoluteBasedir.isAbsolute()) { + absoluteBasedir = null; + } + return absoluteBasedir; + } + + private static class RelativePathFilter implements IOFileFilter { + private final File basedir; + private final WildcardPattern pattern; + + private RelativePathFilter(File basedir, WildcardPattern pattern) { + this.basedir = basedir; + this.pattern = pattern; + } + + public boolean accept(File file) { + return pattern.match(relativePath(file)); + } + + public boolean accept(File file, String filename) { + return true; + } + + String relativePath(File file) { + List stack = Lists.newArrayList(); + String path = FilenameUtils.normalize(file.getAbsolutePath()); + File cursor = new File(path); + while (cursor != null) { + if (containsFile(cursor)) { + return Joiner.on("/").join(stack); + } + stack.add(0, cursor.getName()); + cursor = cursor.getParentFile(); + } + return null; + } + + private boolean containsFile(File cursor) { + return FilenameUtils.equalsNormalizedOnSystem(basedir.getAbsolutePath(), cursor.getAbsolutePath()); + } + + } + + private static class AbsolutePathFilter implements IOFileFilter { + private final WildcardPattern pattern; + + private AbsolutePathFilter(WildcardPattern pattern) { + this.pattern = pattern; + } + + public boolean accept(File file) { + return pattern.match(file.getAbsolutePath()); + } + + public boolean accept(File file, String filename) { + return true; + } + } +} diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java index 227e577..efea03c 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/ProjectReactorBuilder.java @@ -35,11 +35,8 @@ import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Properties; /** * Class that creates a Sonar project definition based on a set of properties. @@ -325,8 +322,8 @@ class ProjectReactorBuilder { // We need to resolve patterns that may have been used in "sonar.libraries" for (String pattern : Utils.getListFromProperty(props, PROPERTY_LIBRARIES)) { - File[] files = getLibraries(baseDir, pattern); - if (files == null || files.length == 0) { + Collection files = getLibraries(baseDir, pattern); + if (files.isEmpty()) { LOG.error("Invalid value of " + PROPERTY_LIBRARIES + " for " + projectId); throw new IllegalStateException("No file matching pattern \"" + pattern + "\" in directory \"" + baseDir + "\""); } @@ -478,23 +475,8 @@ class ProjectReactorBuilder { * Returns files matching specified pattern. */ @VisibleForTesting - protected static File[] getLibraries(File baseDir, String pattern) { - final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\')); - final String dirPath, filePattern; - if (i == -1) { - dirPath = "."; - filePattern = pattern; - } else { - dirPath = pattern.substring(0, i); - filePattern = pattern.substring(i + 1); - } - FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern)); - File dir = resolvePath(baseDir, dirPath); - File[] files = dir.listFiles(fileFilter); - if (files == null) { - files = new File[0]; - } - return files; + protected static Collection getLibraries(File baseDir, String pattern) { + return new FilePattern().listFiles(baseDir, pattern); } private static File resolvePath(File baseDir, String path) { diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/FilePatternTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/FilePatternTest.java new file mode 100644 index 0000000..d1cea9e --- /dev/null +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/FilePatternTest.java @@ -0,0 +1,72 @@ +/* + * Sonar Runner - Batch + * 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.batch; + +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.Collection; + +import static org.fest.assertions.Assertions.assertThat; + +public class FilePatternTest { + + File basedir, rootFile, subFile; + + @Before + public void init() throws Exception { + rootFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/root.txt").toURI()); + subFile = new File(getClass().getResource("/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt").toURI()); + basedir = rootFile.getParentFile(); + } + + @Test + public void should_list_files_by_relative_path() throws Exception { + assertThat(new FilePattern().listFiles(basedir, "subdir/*.txt")).containsOnly(subFile); + assertThat(new FilePattern().listFiles(basedir, "*.txt")).containsOnly(rootFile); + assertThat(new FilePattern().listFiles(basedir, "root.txt")).containsOnly(rootFile); + assertThat(new FilePattern().listFiles(basedir, "ro*t.txt")).containsOnly(rootFile); + assertThat(new FilePattern().listFiles(basedir, "ro?t.txt")).containsOnly(rootFile); + assertThat(new FilePattern().listFiles(basedir, "r?t.txt")).isEmpty(); + assertThat(new FilePattern().listFiles(basedir, "*")).containsOnly(rootFile); + assertThat(new FilePattern().listFiles(basedir, "**/*")).containsOnly(rootFile, subFile); + assertThat(new FilePattern().listFiles(basedir, "**/*.txt")).containsOnly(subFile, rootFile); + assertThat(new FilePattern().listFiles(basedir, "**/*.jar")).isEmpty(); + assertThat(new FilePattern().listFiles(basedir, "elsewhere/root.txt")).isEmpty(); + assertThat(new FilePattern().listFiles(basedir, "elsewhere/subfile.txt")).isEmpty(); + } + + @Test + public void should_list_files_by_absolute_path() throws Exception { + assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/*.txt"), rootFile); + assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/**/subdir/*"), subFile); + assertOnly(new FilePattern().listFiles(basedir, rootFile.getCanonicalPath()), rootFile); + assertOnly(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/*/subfile.txt"), subFile); + assertThat(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/**/*.txt")).containsOnly(subFile, rootFile); + assertThat(new FilePattern().listFiles(basedir, basedir.getCanonicalPath() + "/ElseWhere/**/*.txt")).isEmpty(); + assertThat(new FilePattern().listFiles(basedir, "/ElseWhere/**/*.txt")).isEmpty(); + } + + private void assertOnly(Collection files, File file) throws Exception { + assertThat(files).hasSize(1); + assertThat(files.iterator().next().getCanonicalPath()).isEqualTo(file.getCanonicalPath()); + } +} diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java index a2a55da..1b35422 100644 --- a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/ProjectReactorBuilderTest.java @@ -447,17 +447,17 @@ public class ProjectReactorBuilderTest { @Test public void shouldFilterFiles() throws Exception { File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles"); - assertThat(ProjectReactorBuilder.getLibraries(baseDir, "in*.txt").length).isEqualTo(1); - assertThat(ProjectReactorBuilder.getLibraries(baseDir, "*.txt").length).isEqualTo(2); - assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length).isEqualTo(1); - assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length).isEqualTo(2); + assertThat(ProjectReactorBuilder.getLibraries(baseDir, "in*.txt")).hasSize(1); + assertThat(ProjectReactorBuilder.getLibraries(baseDir, "*.txt")).hasSize(2); + assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt")).hasSize(1); + assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt")).hasSize(2); } @Test public void shouldWorkWithAbsolutePath() throws Exception { File baseDir = new File("not-exists"); String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt"; - assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), absolutePattern).length).isEqualTo(1); + assertThat(ProjectReactorBuilder.getLibraries(baseDir.getParentFile(), absolutePattern)).hasSize(1); } @Test diff --git a/sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/root.txt b/sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/root.txt new file mode 100644 index 0000000..e69de29 diff --git a/sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt b/sonar-runner-batch/src/test/resources/org/sonar/runner/batch/FilePatternTest/subdir/subfile.txt new file mode 100644 index 0000000..e69de29 diff --git a/sonar-runner-dist/pom.xml b/sonar-runner-dist/pom.xml index 4a016d0..f00e61c 100644 --- a/sonar-runner-dist/pom.xml +++ b/sonar-runner-dist/pom.xml @@ -113,8 +113,8 @@ - 250000 - 300000 + 280000 + 310000 ${pom.build.directory}/sonar-runner-${pom.version}.zip -- 2.39.5