From 9409d0c2e94126127b0546117a8089f6bff5a637 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 24 Feb 2014 17:00:06 +0100 Subject: [PATCH] Fix some quality flaws --- .../batch/fs/internal/DefaultFileSystem.java | 2 +- .../api/batch/fs/internal/PathPattern.java | 2 +- .../batch/fs/internal/PathPatternTest.java | 110 ++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java index ad5486a21e3..6a21a8d6c15 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java @@ -161,7 +161,7 @@ public class DefaultFileSystem implements FileSystem { * This method is called before each search of files. */ protected void doPreloadFiles() { - + // nothing to do by default } public static abstract class Cache { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java index 186226fb47b..6b34b4b1d3b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java @@ -79,7 +79,7 @@ public abstract class PathPattern { if (!caseSensitiveFileExtension) { String extension = sanitizeExtension(FilenameUtils.getExtension(inputFile.file().getName())); if (StringUtils.isNotBlank(extension)) { - StringUtils.removeEndIgnoreCase(path, extension); + path = StringUtils.removeEndIgnoreCase(path, extension); path = path + extension; } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java new file mode 100644 index 00000000000..ca63ffb344a --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java @@ -0,0 +1,110 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 02110-1301, USA. + */ +package org.sonar.api.batch.fs.internal; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile; + +import java.io.File; + +import static org.fest.assertions.Assertions.assertThat; + +public class PathPatternTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void match_relative_path() throws Exception { + PathPattern pattern = PathPattern.create("**/*Foo.java"); + assertThat(pattern.toString()).isEqualTo("**/*Foo.java"); + + File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java"); + InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file); + assertThat(pattern.match(inputFile)).isTrue(); + + // case sensitive by default + file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA"); + inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file); + assertThat(pattern.match(inputFile)).isFalse(); + + file = new File(temp.newFolder(), "src/main/java/org/Other.java"); + inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file); + assertThat(pattern.match(inputFile)).isFalse(); + } + + @Test + public void match_relative_path_and_insensitive_file_extension() throws Exception { + PathPattern pattern = PathPattern.create("**/*Foo.java"); + + File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA"); + InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file); + assertThat(pattern.match(inputFile, false)).isTrue(); + + file = new File(temp.newFolder(), "src/main/java/org/Other.java"); + inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file); + assertThat(pattern.match(inputFile, false)).isFalse(); + } + + @Test + public void match_absolute_path() throws Exception { + PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java"); + assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java"); + + File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.java"); + InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.java").setFile(file); + assertThat(pattern.match(inputFile)).isTrue(); + + // case sensitive by default + file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA"); + inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file); + assertThat(pattern.match(inputFile)).isFalse(); + + file = new File(temp.newFolder(), "src/main/java/org/Other.java"); + inputFile = new DefaultInputFile("src/main/java/org/Other.java").setFile(file); + assertThat(pattern.match(inputFile)).isFalse(); + } + + @Test + public void match_absolute_path_and_insensitive_file_extension() throws Exception { + PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java"); + assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java"); + + File file = new File(temp.newFolder(), "src/main/java/org/MyFoo.JAVA"); + InputFile inputFile = new DefaultInputFile("src/main/java/org/MyFoo.JAVA").setFile(file); + assertThat(pattern.match(inputFile, false)).isTrue(); + + file = new File(temp.newFolder(), "src/main/java/org/Other.JAVA"); + inputFile = new DefaultInputFile("src/main/java/org/Other.JAVA").setFile(file); + assertThat(pattern.match(inputFile, false)).isFalse(); + } + + @Test + public void create_array_of_patterns() throws Exception { + PathPattern[] patterns = PathPattern.create(new String[]{ + "**/src/main/**Foo.java", + "file:**/src/main/**Bar.java" + }); + assertThat(patterns).hasSize(2); + assertThat(patterns[0].toString()).isEqualTo("**/src/main/**Foo.java"); + assertThat(patterns[1].toString()).isEqualTo("file:**/src/main/**Bar.java"); + } +} -- 2.39.5