]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 fix FilePredicates.or() with empty predicates
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 20 Feb 2014 11:40:17 +0000 (12:40 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 20 Feb 2014 11:40:17 +0000 (12:40 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/AndPredicate.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/OrPredicate.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/FilePredicatesTest.java [new file with mode: 0644]

index f1221c1fe2caf5a78ded047a325e38c8e79517ca..56611be61f77543e94c2092368837c9d023b7f25 100644 (file)
  */
 package org.sonar.api.batch.fs;
 
-import javax.annotation.Nullable;
+import java.util.Collection;
 
 /**
  * @since 4.2
  */
 class AndPredicate implements FilePredicate {
 
-  private final Iterable<FilePredicate> predicates;
+  private final Collection<FilePredicate> predicates;
 
-  AndPredicate(@Nullable Iterable<FilePredicate> predicates) {
+  AndPredicate(Collection<FilePredicate> predicates) {
     this.predicates = predicates;
   }
 
index 16c79cb962e2b6175f1603fcf4e5e85ecd7e5519..24ff6fcc25b740e323f959d573014c6f88f82650 100644 (file)
@@ -34,6 +34,7 @@ import java.util.List;
  */
 public class FilePredicates {
   private static final FilePredicate ALWAYS_TRUE = new AlwaysTruePredicate();
+  private static final FilePredicate ALWAYS_FALSE = not(ALWAYS_TRUE);
 
   private FilePredicates() {
     // only static stuff
@@ -46,6 +47,13 @@ public class FilePredicates {
     return ALWAYS_TRUE;
   }
 
+  /**
+   * Returns a predicate that always evaluates to false
+   */
+  public static FilePredicate none() {
+    return ALWAYS_FALSE;
+  }
+
   /**
    * Warning - not efficient because absolute path is not indexed yet.
    */
@@ -72,7 +80,7 @@ public class FilePredicates {
     for (int i = 0; i < inclusionPatterns.length; i++) {
       predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
     }
-    return and(predicates);
+    return or(predicates);
   }
 
   public static FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
@@ -125,7 +133,7 @@ public class FilePredicates {
     return new NotPredicate(p);
   }
 
-  public static FilePredicate or(Iterable<FilePredicate> or) {
+  public static FilePredicate or(Collection<FilePredicate> or) {
     return new OrPredicate(or);
   }
 
@@ -137,7 +145,7 @@ public class FilePredicates {
     return new OrPredicate(Arrays.asList(first, second));
   }
 
-  public static FilePredicate and(Iterable<FilePredicate> and) {
+  public static FilePredicate and(Collection<FilePredicate> and) {
     return new AndPredicate(and);
   }
 
index af364527a2a87a44bb4586d68aec8f599e9820ae..1109258be91cac97c848d941582ac36d1f2177e1 100644 (file)
  */
 package org.sonar.api.batch.fs;
 
-import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Collection;
 
 /**
  * @since 4.2
  */
 class OrPredicate implements FilePredicate {
 
-  private final Iterable<FilePredicate> predicates;
+  private final Collection<FilePredicate> predicates;
 
-  OrPredicate(@Nullable Iterable<FilePredicate> predicates) {
-    this.predicates = predicates;
+  OrPredicate(Collection<FilePredicate> predicates) {
+    if (predicates.isEmpty()) {
+      this.predicates = Arrays.asList(FilePredicates.all());
+    } else {
+      this.predicates = predicates;
+    }
   }
 
   @Override
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/FilePredicatesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/FilePredicatesTest.java
new file mode 100644 (file)
index 0000000..9c1db6e
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ * 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;
+
+import org.apache.commons.io.FilenameUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class FilePredicatesTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  DefaultInputFile javaFile;
+
+  @Before
+  public void before() throws IOException {
+    javaFile = new DefaultInputFile("src/main/java/struts/Action.java")
+      .setFile(temp.newFile("Action.java"))
+      .setLanguage("java")
+      .setStatus(InputFile.Status.ADDED);
+  }
+
+  @Test
+  public void all() throws Exception {
+    assertThat(FilePredicates.all().apply(javaFile)).isTrue();
+  }
+
+  @Test
+  public void none() throws Exception {
+    assertThat(FilePredicates.none().apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void matches_inclusion_pattern() throws Exception {
+    assertThat(FilePredicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse();
+    assertThat(FilePredicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void matches_inclusion_patterns() throws Exception {
+    assertThat(FilePredicates.matchesPathPatterns(new String[]{"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.matchesPathPatterns(new String[]{}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.matchesPathPatterns(new String[]{"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void does_not_match_exclusion_pattern() throws Exception {
+    assertThat(FilePredicates.doesNotMatchPathPattern("src/main/**/Action.java").apply(javaFile)).isFalse();
+    assertThat(FilePredicates.doesNotMatchPathPattern("Action.java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.doesNotMatchPathPattern("src/**/*.php").apply(javaFile)).isTrue();
+  }
+
+  @Test
+  public void does_not_match_exclusion_patterns() throws Exception {
+    assertThat(FilePredicates.doesNotMatchPathPatterns(new String[]{}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.doesNotMatchPathPatterns(new String[]{"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.doesNotMatchPathPatterns(new String[]{"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void has_relative_path() throws Exception {
+    assertThat(FilePredicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+    // path is normalized
+    assertThat(FilePredicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
+
+    assertThat(FilePredicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
+    assertThat(FilePredicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
+  }
+
+  @Test
+  public void has_absolute_path() throws Exception {
+    String path = javaFile.file().getAbsolutePath();
+    assertThat(FilePredicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasAbsolutePath(FilenameUtils.separatorsToWindows(path)).apply(javaFile)).isTrue();
+
+    assertThat(FilePredicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+    assertThat(FilePredicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void has_path() throws Exception {
+    // is relative path
+    assertThat(FilePredicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+    // is absolute path
+    String path = javaFile.file().getAbsolutePath();
+    assertThat(FilePredicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void is_file() throws Exception {
+    // relative file
+    assertThat(FilePredicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue();
+
+
+    // absolute file
+    assertThat(FilePredicates.is(javaFile.file()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.is(javaFile.file().getCanonicalFile()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.is(temp.newFile()).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void has_language() throws Exception {
+    assertThat(FilePredicates.hasLanguage("java").apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasLanguage("php").apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void has_languages() throws Exception {
+    assertThat(FilePredicates.hasLanguages(Arrays.asList("java", "php")).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasLanguages(Arrays.asList("cobol", "php")).apply(javaFile)).isFalse();
+    assertThat(FilePredicates.hasLanguages(Collections.<String>emptyList()).apply(javaFile)).isTrue();
+  }
+
+  @Test
+  public void has_status() throws Exception {
+    assertThat(FilePredicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasStatus(InputFile.Status.CHANGED).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void has_type() throws Exception {
+    assertThat(FilePredicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.hasType(InputFile.Type.TEST).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void not() throws Exception {
+    assertThat(FilePredicates.not(FilePredicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
+    assertThat(FilePredicates.not(FilePredicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
+  }
+
+  @Test
+  public void and() throws Exception {
+    // empty
+    assertThat(FilePredicates.and().apply(javaFile)).isTrue();
+    assertThat(FilePredicates.and(new FilePredicate[0]).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.and(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+    // two arguments
+    assertThat(FilePredicates.and(FilePredicates.all(), FilePredicates.all()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.and(FilePredicates.all(), FilePredicates.none()).apply(javaFile)).isFalse();
+    assertThat(FilePredicates.and(FilePredicates.none(), FilePredicates.all()).apply(javaFile)).isFalse();
+
+    // collection
+    assertThat(FilePredicates.and(Arrays.asList(FilePredicates.all(), FilePredicates.all())).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.and(Arrays.asList(FilePredicates.all(), FilePredicates.none())).apply(javaFile)).isFalse();
+
+    // array
+    assertThat(FilePredicates.and(new FilePredicate[]{FilePredicates.all(), FilePredicates.all()}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.and(new FilePredicate[]{FilePredicates.all(), FilePredicates.none()}).apply(javaFile)).isFalse();
+  }
+
+  @Test
+  public void or() throws Exception {
+    // empty
+    assertThat(FilePredicates.or().apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(new FilePredicate[0]).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+    // two arguments
+    assertThat(FilePredicates.or(FilePredicates.all(), FilePredicates.all()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(FilePredicates.all(), FilePredicates.none()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(FilePredicates.none(), FilePredicates.all()).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(FilePredicates.none(), FilePredicates.none()).apply(javaFile)).isFalse();
+
+    // collection
+    assertThat(FilePredicates.or(Arrays.asList(FilePredicates.all(), FilePredicates.all())).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(Arrays.asList(FilePredicates.all(), FilePredicates.none())).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(Arrays.asList(FilePredicates.none(), FilePredicates.none())).apply(javaFile)).isFalse();
+
+    // array
+    assertThat(FilePredicates.or(new FilePredicate[]{FilePredicates.all(), FilePredicates.all()}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(new FilePredicate[]{FilePredicates.all(), FilePredicates.none()}).apply(javaFile)).isTrue();
+    assertThat(FilePredicates.or(new FilePredicate[]{FilePredicates.none(), FilePredicates.none()}).apply(javaFile)).isFalse();
+  }
+}