1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.utils;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class WildcardPatternTest {
private boolean match(String pattern, String value, String separator) {
return new WildcardPattern(pattern, separator).match(value);
}
private boolean match(String pattern, String value) {
return new WildcardPattern(pattern, "/").match(value);
}
@Test
public void examples() {
assertTrue(match("org/T?st.java", "org/Test.java"));
assertTrue(match("org/T?st.java", "org/Tost.java"));
assertTrue(match("org/*.java", "org/Foo.java"));
assertTrue(match("org/*.java", "org/Bar.java"));
assertTrue(match("org/**", "org/Foo.java"));
assertTrue(match("org/**", "org/foo/bar.jsp"));
assertTrue(match("org/**/Test.java", "org/Test.java"));
assertTrue(match("org/**/Test.java", "org/foo/Test.java"));
assertTrue(match("org/**/Test.java", "org/foo/bar/Test.java"));
assertTrue(match("org/**/*.java", "org/Foo.java"));
assertTrue(match("org/**/*.java", "org/foo/Bar.java"));
assertTrue(match("org/**/*.java", "org/foo/bar/Baz.java"));
}
@Test
public void javaResourcesShouldMatchWildcards() {
assertTrue(match("Foo", "Foo", "."));
assertFalse(match("Foo", "Bar", "."));
assertTrue(match("org/sonar/**", "org.sonar.commons.Foo", "."));
assertTrue(match("org/sonar/**", "org.sonar.Foo", "."));
assertFalse(match("xxx/org/sonar/**", "org.sonar.Foo", "."));
assertTrue(match("org/sonar/**/**", "org.sonar.commons.Foo", "."));
assertTrue(match("org/sonar/**/**", "org.sonar.commons.sub.Foo", "."));
assertTrue(match("org/sonar/**/Foo", "org.sonar.commons.sub.Foo", "."));
assertTrue(match("org/sonar/**/Foo", "org.sonar.Foo", "."));
assertTrue(match("*/foo/*", "org.foo.Bar", "."));
assertFalse(match("*/foo/*", "foo.Bar", "."));
assertFalse(match("*/foo/*", "foo", "."));
assertFalse(match("*/foo/*", "org.foo.bar.Hello", "."));
assertTrue(match("hell?", "hello", "."));
assertFalse(match("hell?", "helloworld", "."));
assertFalse(match("hell?", "hell", "."));
assertTrue(match("a.b.c", "a.b.c", "."));
assertTrue(match("*/a.b.c", "foo.a.b.c", "."));
assertFalse(match("*/a.b.c", "foo/aabbc", "."));
assertTrue(match("**/Reader", "java.io.Reader", "."));
assertFalse(match("**/Reader", "org.sonar.channel.CodeReader", "."));
assertTrue(match("**", "java.io.Reader", "."));
}
@Test
public void directoriesShouldMatchWildcards() {
assertTrue(match("Foo", "Foo"));
assertFalse(match("Foo", "Bar"));
assertTrue(match("org/sonar/**", "org/sonar/commons/Foo"));
assertTrue(match("org/sonar/**", "org/sonar/Foo.java"));
assertFalse(match("xxx/org/sonar/**", "org/sonar/Foo"));
assertTrue(match("org/sonar/**/**", "org/sonar/commons/Foo"));
assertTrue(match("org/sonar/**/**", "org/sonar/commons/sub/Foo.java"));
assertTrue(match("org/sonar/**/Foo", "org/sonar/commons/sub/Foo"));
assertTrue(match("org/sonar/**/Foo", "org/sonar/Foo"));
assertTrue(match("*/foo/*", "org/foo/Bar"));
assertFalse(match("*/foo/*", "foo/Bar"));
assertFalse(match("*/foo/*", "foo"));
assertFalse(match("*/foo/*", "org/foo/bar/Hello"));
assertTrue(match("hell?", "hello"));
assertFalse(match("hell?", "helloworld"));
assertFalse(match("hell?", "hell"));
assertTrue(match("a.b.c", "a.b.c"));
assertTrue(match("*/a.b.c", "foo/a.b.c"));
assertFalse(match("*/a.b.c", "foo/aabbc"));
assertTrue(match("**/Reader", "java/io/Reader"));
assertFalse(match("**/Reader", "org/sonar/channel/CodeReader"));
assertTrue(match("**", "java/io/Reader"));
}
/**
* See http://jira.sonarsource.com/browse/SONAR-2193
*/
@Test
public void issue2193() {
assertTrue(match("**/app/**", "com.app.Utils", "."));
assertFalse(match("**/app/**", "com.application.MyService", "."));
assertTrue(match("**/app/**", "com/app/Utils"));
assertFalse(match("**/app/**", "com/application/MyService"));
}
/**
* See SONAR-2762
*/
@Test
public void shouldEscapeRegexpSpecificCharacters() {
assertFalse(match("**/*$*", "foo/bar"));
assertTrue(match("**/*$*", "foo/bar$baz"));
assertFalse(match("a+", "aa"));
assertTrue(match("a+", "a+"));
assertFalse(match("[ab]", "a"));
assertTrue(match("[ab]", "[ab]"));
assertTrue("all regexp-specific characters", match("()[]^$.{}+|", "()[]^$.{}+|"));
}
@Test
public void backslash() {
assertFalse("backslash is not an escape character", match("\\n", "\n"));
assertTrue("backslash is the same as forward slash", match("foo\\bar", "foo/bar"));
}
@Test
public void shouldIgnoreStartingSlash() {
assertTrue(match("/foo", "foo"));
assertTrue(match("\\foo", "foo"));
}
/**
* Godin: in my opinion this is invalid pattern, however it might be constructed by {@link org.sonar.api.resources.JavaFile#matchFilePattern(String)},
* so it should be supported at least for now for backward compatibility.
*/
@Test
public void cornerCase() {
assertTrue(match("org/**.*", "org.sonar.commons.Foo.java", "."));
}
@Test
public void multiplePatterns() {
WildcardPattern[] patterns = WildcardPattern.create(new String[] { "Foo", "Bar" });
assertTrue(WildcardPattern.match(patterns, "Foo"));
assertTrue(WildcardPattern.match(patterns, "Bar"));
assertFalse(WildcardPattern.match(patterns, "Other"));
assertThat(WildcardPattern.create((String[]) null).length, is(0));
}
@Test
public void testToString() {
assertThat(WildcardPattern.create("foo*").toString(), is("foo*"));
}
}
|