]> source.dussan.org Git - sonarqube.git/blob
a9c81f2682c01ab2b40afda73ed6df66e18ad613
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.api.batch.fs.internal.predicates;
21
22 import com.google.common.io.Files;
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.URI;
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;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41
42 public class DefaultFilePredicatesTest {
43
44   @Rule
45   public TemporaryFolder temp = new TemporaryFolder();
46
47   private Path moduleBasePath;
48
49   @Before
50   public void setUp() throws IOException {
51     moduleBasePath = temp.newFolder().toPath();
52   }
53
54   InputFile javaFile;
55   FilePredicates predicates;
56
57   @Before
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)
62       .setLanguage("java")
63       .setStatus(Status.SAME)
64       .build();
65
66   }
67
68   @Test
69   public void all() {
70     assertThat(predicates.all().apply(javaFile)).isTrue();
71   }
72
73   @Test
74   public void none() {
75     assertThat(predicates.none().apply(javaFile)).isFalse();
76   }
77
78   @Test
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();
83   }
84
85   @Test
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();
90   }
91
92   @Test
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();
97   }
98
99   @Test
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();
104   }
105
106   @Test
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();
110
111     // path is normalized
112     assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
113
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();
117   }
118
119   @Test
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();
124
125     assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
126     assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
127   }
128
129   @Test
130   public void has_uri() throws Exception {
131     URI uri = javaFile.uri();
132     assertThat(predicates.hasURI(uri).apply(javaFile)).isTrue();
133
134     assertThat(predicates.hasURI(temp.newFile().toURI()).apply(javaFile)).isFalse();
135   }
136
137   @Test
138   public void has_path() throws Exception {
139     // is relative path
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();
142
143     // is absolute path
144     String path = javaFile.file().getAbsolutePath();
145     assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
146     assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
147   }
148
149   @Test
150   public void is_file() throws Exception {
151
152     Files.createParentDirs(javaFile.file());
153     Files.touch(javaFile.file());
154
155     // relative 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();
159
160     // absolute file
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();
165   }
166
167   @Test
168   public void has_language() {
169     assertThat(predicates.hasLanguage("java").apply(javaFile)).isTrue();
170     assertThat(predicates.hasLanguage("php").apply(javaFile)).isFalse();
171   }
172
173   @Test
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();
178   }
179
180   @Test
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();
184   }
185
186   @Test
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();
191   }
192
193   @Test
194   public void not() {
195     assertThat(predicates.not(predicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
196     assertThat(predicates.not(predicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
197   }
198
199   @Test
200   public void and() {
201     // empty
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();
205
206     // two arguments
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();
210
211     // collection
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();
214
215     // array
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();
218   }
219
220   @Test
221   public void or() {
222     // empty
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();
226
227     // two arguments
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();
232
233     // collection
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();
237
238     // array
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();
242   }
243
244   @Test
245   public void hasFilename() {
246     assertThat(predicates.hasFilename("Action.java").apply(javaFile)).isTrue();
247   }
248
249   @Test
250   public void hasExtension() {
251     assertThat(predicates.hasExtension("java").apply(javaFile)).isTrue();
252   }
253 }