1 |
|
package jalview.util.matcher; |
2 |
|
|
3 |
|
import static org.testng.Assert.assertEquals; |
4 |
|
import static org.testng.Assert.assertFalse; |
5 |
|
import static org.testng.Assert.assertNotEquals; |
6 |
|
import static org.testng.Assert.assertSame; |
7 |
|
import static org.testng.Assert.assertTrue; |
8 |
|
import static org.testng.Assert.fail; |
9 |
|
|
10 |
|
import java.util.Locale; |
11 |
|
|
12 |
|
import org.testng.annotations.Test; |
13 |
|
|
14 |
|
import jalview.util.matcher.Matcher.PatternType; |
15 |
|
import junit.extensions.PA; |
16 |
|
|
|
|
| 98.2% |
Uncovered Elements: 4 (224) |
Complexity: 15 |
Complexity Density: 0.07 |
|
17 |
|
public class MatcherTest |
18 |
|
{ |
|
|
| 91.8% |
Uncovered Elements: 4 (49) |
Complexity: 5 |
Complexity Density: 0.1 |
1PASS
|
|
19 |
1 |
@Test(groups = "Functional")... |
20 |
|
public void testConstructor() |
21 |
|
{ |
22 |
1 |
MatcherI m = new Matcher(Condition.Contains, "foo"); |
23 |
1 |
assertEquals(m.getCondition(), Condition.Contains); |
24 |
1 |
assertEquals(m.getPattern(), "foo"); |
25 |
1 |
assertEquals(PA.getValue(m, "uppercasePattern"), "FOO"); |
26 |
1 |
assertEquals(PA.getValue(m, "floatValue"), 0f); |
27 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
28 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.String); |
29 |
|
|
30 |
1 |
m = new Matcher(Condition.GT, -2.1f); |
31 |
1 |
assertEquals(m.getCondition(), Condition.GT); |
32 |
1 |
assertEquals(m.getPattern(), "-2.1"); |
33 |
1 |
assertEquals(PA.getValue(m, "floatValue"), -2.1f); |
34 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
35 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.Float); |
36 |
|
|
37 |
1 |
m = new Matcher(Condition.NotContains, "-1.2f"); |
38 |
1 |
assertEquals(m.getCondition(), Condition.NotContains); |
39 |
1 |
assertEquals(m.getPattern(), "-1.2f"); |
40 |
1 |
assertEquals(PA.getValue(m, "floatValue"), 0f); |
41 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
42 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.String); |
43 |
|
|
44 |
1 |
m = new Matcher(Condition.GE, "-1.2f"); |
45 |
1 |
assertEquals(m.getCondition(), Condition.GE); |
46 |
1 |
assertEquals(m.getPattern(), "-1.2"); |
47 |
1 |
assertEquals(PA.getValue(m, "floatValue"), -1.2f); |
48 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
49 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.Float); |
50 |
|
|
51 |
1 |
m = new Matcher(Condition.GE, "113890813"); |
52 |
1 |
assertEquals(m.getCondition(), Condition.GE); |
53 |
1 |
assertEquals(m.getPattern(), "113890813"); |
54 |
1 |
assertEquals(PA.getValue(m, "floatValue"), 0f); |
55 |
1 |
assertEquals(PA.getValue(m, "longValue"), 113890813L); |
56 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.Integer); |
57 |
|
|
58 |
1 |
m = new Matcher(Condition.GE, "-987f"); |
59 |
1 |
assertEquals(m.getCondition(), Condition.GE); |
60 |
1 |
assertEquals(m.getPattern(), "-987.0"); |
61 |
1 |
assertEquals(PA.getValue(m, "floatValue"), -987f); |
62 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
63 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.Float); |
64 |
|
|
65 |
1 |
try |
66 |
|
{ |
67 |
1 |
new Matcher(null, 0f); |
68 |
0 |
fail("Expected exception"); |
69 |
|
} catch (NullPointerException e) |
70 |
|
{ |
71 |
|
|
72 |
|
} |
73 |
|
|
74 |
1 |
try |
75 |
|
{ |
76 |
1 |
new Matcher(Condition.LT, "123,456"); |
77 |
0 |
fail("Expected exception"); |
78 |
|
} catch (NumberFormatException e) |
79 |
|
{ |
80 |
|
|
81 |
|
} |
82 |
|
|
83 |
1 |
try |
84 |
|
{ |
85 |
1 |
new Matcher(Condition.LT, "123_456"); |
86 |
0 |
fail("Expected exception"); |
87 |
|
} catch (NumberFormatException e) |
88 |
|
{ |
89 |
|
|
90 |
|
} |
91 |
|
|
92 |
1 |
try |
93 |
|
{ |
94 |
1 |
new Matcher(Condition.LT, "123456L"); |
95 |
0 |
fail("Expected exception"); |
96 |
|
} catch (NumberFormatException e) |
97 |
|
{ |
98 |
|
|
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
105 |
1 |
@Test(groups = "Functional")... |
106 |
|
public void testMatches_float() |
107 |
|
{ |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
1 |
MatcherI m = new Matcher(Condition.EQ, 2f); |
112 |
1 |
assertTrue(m.matches("2")); |
113 |
1 |
assertTrue(m.matches("2.0")); |
114 |
1 |
assertFalse(m.matches("2.01")); |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
1 |
m = new Matcher(Condition.NE, 2f); |
120 |
1 |
assertFalse(m.matches("2")); |
121 |
1 |
assertFalse(m.matches("2.0")); |
122 |
1 |
assertTrue(m.matches("2.01")); |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
1 |
m = new Matcher(Condition.GE, "2f"); |
128 |
1 |
assertTrue(m.matches("2")); |
129 |
1 |
assertTrue(m.matches("2.1")); |
130 |
1 |
assertFalse(m.matches("1.9")); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
1 |
m = new Matcher(Condition.GT, 2f); |
136 |
1 |
assertFalse(m.matches("2")); |
137 |
1 |
assertTrue(m.matches("2.1")); |
138 |
1 |
assertFalse(m.matches("1.9")); |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
1 |
m = new Matcher(Condition.LE, "2.0f"); |
144 |
1 |
assertTrue(m.matches("2")); |
145 |
1 |
assertFalse(m.matches("2.1")); |
146 |
1 |
assertTrue(m.matches("1.9")); |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
1 |
m = new Matcher(Condition.LT, 2f); |
152 |
1 |
assertFalse(m.matches("2")); |
153 |
1 |
assertFalse(m.matches("2.1")); |
154 |
1 |
assertTrue(m.matches("1.9")); |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
1PASS
|
|
161 |
1 |
@Test(groups = "Functional")... |
162 |
|
public void testNumericMatch_nullOrInvalidValue() |
163 |
|
{ |
164 |
1 |
for (Condition cond : Condition.values()) |
165 |
|
{ |
166 |
12 |
if (cond.isNumeric()) |
167 |
|
{ |
168 |
6 |
MatcherI m1 = new Matcher(cond, 2.1f); |
169 |
6 |
MatcherI m2 = new Matcher(cond, 2345L); |
170 |
6 |
assertFalse(m1.matches(null)); |
171 |
6 |
assertFalse(m1.matches("")); |
172 |
6 |
assertFalse(m1.matches("two")); |
173 |
6 |
assertFalse(m2.matches(null)); |
174 |
6 |
assertFalse(m2.matches("")); |
175 |
6 |
assertFalse(m2.matches("two")); |
176 |
|
} |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (39) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
183 |
1 |
@Test(groups = "Functional")... |
184 |
|
public void testMatches_pattern() |
185 |
|
{ |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
1 |
MatcherI m = new Matcher(Condition.Contains, "benign"); |
190 |
1 |
assertTrue(m.matches("benign")); |
191 |
1 |
assertTrue(m.matches("MOSTLY BENIGN OBSERVED")); |
192 |
1 |
assertFalse(m.matches("pathogenic")); |
193 |
1 |
assertFalse(m.matches(null)); |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
1 |
m = new Matcher(Condition.NotContains, "benign"); |
199 |
1 |
assertFalse(m.matches("benign")); |
200 |
1 |
assertFalse(m.matches("MOSTLY BENIGN OBSERVED")); |
201 |
1 |
assertTrue(m.matches("pathogenic")); |
202 |
1 |
assertTrue(m.matches(null)); |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
1 |
m = new Matcher(Condition.Matches, "benign"); |
208 |
1 |
assertTrue(m.matches("benign")); |
209 |
1 |
assertTrue(m.matches(" Benign ")); |
210 |
1 |
assertFalse(m.matches("MOSTLY BENIGN")); |
211 |
1 |
assertFalse(m.matches("pathogenic")); |
212 |
1 |
assertFalse(m.matches(null)); |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
1 |
m = new Matcher(Condition.NotMatches, "benign"); |
218 |
1 |
assertFalse(m.matches("benign")); |
219 |
1 |
assertFalse(m.matches(" Benign ")); |
220 |
1 |
assertTrue(m.matches("MOSTLY BENIGN")); |
221 |
1 |
assertTrue(m.matches("pathogenic")); |
222 |
1 |
assertTrue(m.matches(null)); |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
1 |
m = new Matcher(Condition.Present, null); |
228 |
1 |
assertTrue(m.matches("benign")); |
229 |
1 |
assertTrue(m.matches("")); |
230 |
1 |
assertFalse(m.matches(null)); |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
1 |
m = new Matcher(Condition.NotPresent, null); |
236 |
1 |
assertFalse(m.matches("benign")); |
237 |
1 |
assertFalse(m.matches("")); |
238 |
1 |
assertTrue(m.matches(null)); |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
1 |
Matcher m1 = new Matcher(Condition.Contains, "32"); |
245 |
1 |
assertFalse(m1.matchesFloat("-203f", 0f)); |
246 |
1 |
assertTrue(m1.matchesFloat("-4321.0f", 0f)); |
247 |
1 |
assertFalse(m1.matchesFloat("-203", 0f)); |
248 |
1 |
assertTrue(m1.matchesFloat("-4321", 0f)); |
249 |
1 |
assertFalse(m1.matchesLong("-203")); |
250 |
1 |
assertTrue(m1.matchesLong("-4321")); |
251 |
1 |
assertFalse(m1.matchesLong("-203f")); |
252 |
1 |
assertTrue(m1.matchesLong("-4321.0f")); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
258 |
1 |
@Test(groups = "Functional")... |
259 |
|
public void testMatches_floatWithStringCondition() |
260 |
|
{ |
261 |
1 |
MatcherI m = new Matcher(Condition.Contains, 1.2e-6f); |
262 |
1 |
assertEquals(m.getPattern(), "1.2E-6"); |
263 |
1 |
assertEquals(PA.getValue(m, "uppercasePattern"), "1.2E-6"); |
264 |
1 |
assertEquals(PA.getValue(m, "floatValue"), 0f); |
265 |
1 |
assertEquals(PA.getValue(m, "longValue"), 0L); |
266 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.String); |
267 |
1 |
assertTrue(m.matches("1.2e-6")); |
268 |
|
|
269 |
1 |
m = new Matcher(Condition.Contains, 0.0000001f); |
270 |
1 |
assertEquals(m.getPattern(), "1.0E-7"); |
271 |
1 |
assertTrue(m.matches("1.0e-7")); |
272 |
1 |
assertTrue(m.matches("1.0E-7")); |
273 |
1 |
assertFalse(m.matches("0.0000001f")); |
274 |
|
} |
275 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
276 |
1 |
@Test(groups = "Functional")... |
277 |
|
public void testToString() |
278 |
|
{ |
279 |
1 |
Locale.setDefault(Locale.ENGLISH); |
280 |
|
|
281 |
1 |
MatcherI m = new Matcher(Condition.LT, 1.2e-6f); |
282 |
1 |
assertEquals(m.toString(), "< 1.2E-6"); |
283 |
|
|
284 |
1 |
m = new Matcher(Condition.GE, "20200413"); |
285 |
1 |
assertEquals(m.toString(), ">= 20200413"); |
286 |
|
|
287 |
1 |
m = new Matcher(Condition.NotMatches, "ABC"); |
288 |
1 |
assertEquals(m.toString(), "Does not match 'ABC'"); |
289 |
|
|
290 |
1 |
m = new Matcher(Condition.Contains, -1.2f); |
291 |
1 |
assertEquals(m.toString(), "Contains '-1.2'"); |
292 |
|
} |
293 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
294 |
1 |
@Test(groups = "Functional")... |
295 |
|
public void testEquals() |
296 |
|
{ |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
1 |
MatcherI m = new Matcher(Condition.NotMatches, "ABC"); |
301 |
1 |
assertFalse(m.equals(null)); |
302 |
1 |
assertFalse(m.equals("foo")); |
303 |
1 |
assertTrue(m.equals(m)); |
304 |
1 |
assertTrue(m.equals(new Matcher(Condition.NotMatches, "ABC"))); |
305 |
|
|
306 |
1 |
assertTrue(m.equals(new Matcher(Condition.NotMatches, "abc"))); |
307 |
1 |
assertFalse(m.equals(new Matcher(Condition.Matches, "ABC"))); |
308 |
1 |
assertFalse(m.equals(new Matcher(Condition.NotMatches, "def"))); |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
1 |
m = new Matcher(Condition.LT, -1f); |
314 |
1 |
assertFalse(m.equals(null)); |
315 |
1 |
assertFalse(m.equals("foo")); |
316 |
1 |
assertTrue(m.equals(m)); |
317 |
1 |
assertTrue(m.equals(new Matcher(Condition.LT, -1f))); |
318 |
1 |
assertTrue(m.equals(new Matcher(Condition.LT, "-1f"))); |
319 |
1 |
assertTrue(m.equals(new Matcher(Condition.LT, "-1.00f"))); |
320 |
1 |
assertFalse(m.equals(new Matcher(Condition.LE, -1f))); |
321 |
1 |
assertFalse(m.equals(new Matcher(Condition.GE, -1f))); |
322 |
1 |
assertFalse(m.equals(new Matcher(Condition.NE, -1f))); |
323 |
1 |
assertFalse(m.equals(new Matcher(Condition.LT, 1f))); |
324 |
1 |
assertFalse(m.equals(new Matcher(Condition.LT, -1.1f))); |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
1 |
m = new Matcher(Condition.LT, -123456); |
330 |
1 |
assertFalse(m.equals(null)); |
331 |
1 |
assertFalse(m.equals("foo")); |
332 |
1 |
assertTrue(m.equals(m)); |
333 |
1 |
assertTrue(m.equals(new Matcher(Condition.LT, -123456))); |
334 |
1 |
assertFalse(m.equals(new Matcher(Condition.LT, +123456))); |
335 |
1 |
assertTrue(m.equals(new Matcher(Condition.LT, "-123456"))); |
336 |
1 |
assertFalse(m.equals(new Matcher(Condition.LT, -123456f))); |
337 |
1 |
assertFalse(m.equals(new Matcher(Condition.LT, "-123456f"))); |
338 |
|
} |
339 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
340 |
1 |
@Test(groups = "Functional")... |
341 |
|
public void testHashCode() |
342 |
|
{ |
343 |
1 |
MatcherI m1 = new Matcher(Condition.NotMatches, "ABC"); |
344 |
1 |
MatcherI m2 = new Matcher(Condition.NotMatches, "ABC"); |
345 |
1 |
MatcherI m3 = new Matcher(Condition.NotMatches, "AB"); |
346 |
1 |
MatcherI m4 = new Matcher(Condition.Matches, "ABC"); |
347 |
1 |
assertEquals(m1.hashCode(), m2.hashCode()); |
348 |
1 |
assertNotEquals(m1.hashCode(), m3.hashCode()); |
349 |
1 |
assertNotEquals(m1.hashCode(), m4.hashCode()); |
350 |
1 |
assertNotEquals(m3.hashCode(), m4.hashCode()); |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
356 |
1 |
@Test(groups = "Functional")... |
357 |
|
public void testMatches_long() |
358 |
|
{ |
359 |
|
|
360 |
|
|
361 |
|
|
362 |
1 |
MatcherI m = new Matcher(Condition.EQ, 2); |
363 |
1 |
assertTrue(m.matches("2")); |
364 |
1 |
assertTrue(m.matches("+2")); |
365 |
1 |
assertFalse(m.matches("3")); |
366 |
|
|
367 |
1 |
assertTrue(m.matches("2.0")); |
368 |
1 |
assertTrue(m.matches("2.000000f")); |
369 |
1 |
assertFalse(m.matches("2.01")); |
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
1 |
m = new Matcher(Condition.NE, 123); |
375 |
1 |
assertFalse(m.matches("123")); |
376 |
1 |
assertFalse(m.matches("123.0")); |
377 |
1 |
assertTrue(m.matches("-123")); |
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
1 |
m = new Matcher(Condition.GE, "113890813"); |
383 |
1 |
assertTrue(m.matches("113890813")); |
384 |
1 |
assertTrue(m.matches("113890814")); |
385 |
1 |
assertFalse(m.matches("-113890813")); |
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
|
390 |
1 |
m = new Matcher(Condition.GT, 113890813); |
391 |
1 |
assertFalse(m.matches("113890813")); |
392 |
1 |
assertTrue(m.matches("113890814")); |
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
1 |
m = new Matcher(Condition.LE, "113890813"); |
398 |
1 |
assertTrue(m.matches("113890813")); |
399 |
1 |
assertFalse(m.matches("113890814")); |
400 |
1 |
assertTrue(m.matches("113890812")); |
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
|
405 |
1 |
m = new Matcher(Condition.LT, 113890813); |
406 |
1 |
assertFalse(m.matches("113890813")); |
407 |
1 |
assertFalse(m.matches("113890814")); |
408 |
1 |
assertTrue(m.matches("113890812")); |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
414 |
1 |
@Test(groups = "Functional")... |
415 |
|
public void testMatches_floatValueIntegerCondition() |
416 |
|
{ |
417 |
1 |
MatcherI m = new Matcher(Condition.GT, 1234); |
418 |
1 |
assertEquals(PA.getValue(m, "longValue"), 1234L); |
419 |
1 |
assertSame(PA.getValue(m, "patternType"), PatternType.Integer); |
420 |
1 |
assertTrue(m.matches("1235")); |
421 |
1 |
assertTrue(m.matches("9867.345")); |
422 |
1 |
assertTrue(m.matches("9867.345f")); |
423 |
|
} |
424 |
|
} |