3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.fs.internal.predicates;
22 import com.google.common.io.Files;
24 import java.io.IOException;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.TemporaryFolder;
34 import org.sonar.api.batch.fs.FilePredicate;
35 import org.sonar.api.batch.fs.FilePredicates;
36 import org.sonar.api.batch.fs.InputFile;
37 import org.sonar.api.batch.fs.InputFile.Status;
38 import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
40 import static org.assertj.core.api.Assertions.assertThat;
42 public class DefaultFilePredicatesTest {
45 public TemporaryFolder temp = new TemporaryFolder();
47 private Path moduleBasePath;
50 public void setUp() throws IOException {
51 moduleBasePath = temp.newFolder().toPath();
55 FilePredicates predicates;
58 public void before() throws IOException {
59 predicates = new DefaultFilePredicates(temp.newFolder().toPath());
60 javaFile = new TestInputFileBuilder("foo", "src/main/java/struts/Action.java")
61 .setModuleBaseDir(moduleBasePath)
63 .setStatus(Status.SAME)
70 assertThat(predicates.all().apply(javaFile)).isTrue();
75 assertThat(predicates.none().apply(javaFile)).isFalse();
79 public void matches_inclusion_pattern() {
80 assertThat(predicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue();
81 assertThat(predicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse();
82 assertThat(predicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse();
86 public void matches_inclusion_patterns() {
87 assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isTrue();
88 assertThat(predicates.matchesPathPatterns(new String[] {}).apply(javaFile)).isTrue();
89 assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isFalse();
93 public void does_not_match_exclusion_pattern() {
94 assertThat(predicates.doesNotMatchPathPattern("src/main/**/Action.java").apply(javaFile)).isFalse();
95 assertThat(predicates.doesNotMatchPathPattern("Action.java").apply(javaFile)).isTrue();
96 assertThat(predicates.doesNotMatchPathPattern("src/**/*.php").apply(javaFile)).isTrue();
100 public void does_not_match_exclusion_patterns() {
101 assertThat(predicates.doesNotMatchPathPatterns(new String[] {}).apply(javaFile)).isTrue();
102 assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isTrue();
103 assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isFalse();
107 public void has_relative_path() {
108 assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
109 assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
111 // path is normalized
112 assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
114 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
115 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
116 assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
120 public void has_absolute_path() throws Exception {
121 String path = javaFile.file().getAbsolutePath();
122 assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
123 assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue();
125 assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
126 assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
130 public void has_uri() throws Exception {
131 URI uri = javaFile.uri();
132 assertThat(predicates.hasURI(uri).apply(javaFile)).isTrue();
134 assertThat(predicates.hasURI(temp.newFile().toURI()).apply(javaFile)).isFalse();
138 public void has_path() throws Exception {
140 assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
141 assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
144 String path = javaFile.file().getAbsolutePath();
145 assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
146 assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
150 public void is_file() throws Exception {
152 Files.createParentDirs(javaFile.file());
153 Files.touch(javaFile.file());
156 Path workingDir = Paths.get(System.getProperty("user.dir"));
157 Path relativePath = workingDir.relativize(javaFile.path());
158 assertThat(predicates.is(relativePath.toFile()).apply(javaFile)).isTrue();
161 assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue();
162 assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue();
163 assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue();
164 assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse();
168 public void has_language() {
169 assertThat(predicates.hasLanguage("java").apply(javaFile)).isTrue();
170 assertThat(predicates.hasLanguage("php").apply(javaFile)).isFalse();
174 public void has_languages() {
175 assertThat(predicates.hasLanguages(Arrays.asList("java", "php")).apply(javaFile)).isTrue();
176 assertThat(predicates.hasLanguages(Arrays.asList("cobol", "php")).apply(javaFile)).isFalse();
177 assertThat(predicates.hasLanguages(Collections.emptyList()).apply(javaFile)).isTrue();
181 public void has_type() {
182 assertThat(predicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
183 assertThat(predicates.hasType(InputFile.Type.TEST).apply(javaFile)).isFalse();
187 public void has_status() {
188 assertThat(predicates.hasAnyStatus().apply(javaFile)).isTrue();
189 assertThat(predicates.hasStatus(InputFile.Status.SAME).apply(javaFile)).isTrue();
190 assertThat(predicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isFalse();
195 assertThat(predicates.not(predicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
196 assertThat(predicates.not(predicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
202 assertThat(predicates.and().apply(javaFile)).isTrue();
203 assertThat(predicates.and(new FilePredicate[0]).apply(javaFile)).isTrue();
204 assertThat(predicates.and(Collections.emptyList()).apply(javaFile)).isTrue();
207 assertThat(predicates.and(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
208 assertThat(predicates.and(predicates.all(), predicates.none()).apply(javaFile)).isFalse();
209 assertThat(predicates.and(predicates.none(), predicates.all()).apply(javaFile)).isFalse();
212 assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
213 assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isFalse();
216 assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
217 assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isFalse();
223 assertThat(predicates.or().apply(javaFile)).isTrue();
224 assertThat(predicates.or(new FilePredicate[0]).apply(javaFile)).isTrue();
225 assertThat(predicates.or(Collections.emptyList()).apply(javaFile)).isTrue();
228 assertThat(predicates.or(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
229 assertThat(predicates.or(predicates.all(), predicates.none()).apply(javaFile)).isTrue();
230 assertThat(predicates.or(predicates.none(), predicates.all()).apply(javaFile)).isTrue();
231 assertThat(predicates.or(predicates.none(), predicates.none()).apply(javaFile)).isFalse();
234 assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
235 assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isTrue();
236 assertThat(predicates.or(Arrays.asList(predicates.none(), predicates.none())).apply(javaFile)).isFalse();
239 assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
240 assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isTrue();
241 assertThat(predicates.or(new FilePredicate[] {predicates.none(), predicates.none()}).apply(javaFile)).isFalse();
245 public void hasFilename() {
246 assertThat(predicates.hasFilename("Action.java").apply(javaFile)).isTrue();
250 public void hasExtension() {
251 assertThat(predicates.hasExtension("java").apply(javaFile)).isTrue();