]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 24 Feb 2014 16:00:06 +0000 (17:00 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 24 Feb 2014 16:00:06 +0000 (17:00 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/PathPattern.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/PathPatternTest.java [new file with mode: 0644]

index ad5486a21e3d7c542f8e8db240a2750d890d68e1..6a21a8d6c15da73c98b37e3e01e45d83550c6d6a 100644 (file)
@@ -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 {
index 186226fb47b5a3be3352463b4e18ca41a5e983bd..6b34b4b1d3b40de440df5ab1120a0810ef69d6e8 100644 (file)
@@ -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 (file)
index 0000000..ca63ffb
--- /dev/null
@@ -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");
+  }
+}