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