Clover icon

Coverage Report

  1. Project Clover database Tue Mar 10 2026 14:58:44 GMT
  2. Package jalview.util.matcher

File MatcherTest.java

 

Code metrics

2
212
10
1
444
286
15
0.07
21.2
10
1.5

Classes

Class Line # Actions
MatcherTest 37 212 15
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
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   
 
37    public class MatcherTest
38    {
 
39  0 toggle @Test(groups = "Functional")
40    public void testConstructor()
41    {
42  0 MatcherI m = new Matcher(Condition.Contains, "foo");
43  0 assertEquals(m.getCondition(), Condition.Contains);
44  0 assertEquals(m.getPattern(), "foo");
45  0 assertEquals(PA.getValue(m, "uppercasePattern"), "FOO");
46  0 assertEquals(PA.getValue(m, "floatValue"), 0f);
47  0 assertEquals(PA.getValue(m, "longValue"), 0L);
48  0 assertSame(PA.getValue(m, "patternType"), PatternType.String);
49   
50  0 m = new Matcher(Condition.GT, -2.1f);
51  0 assertEquals(m.getCondition(), Condition.GT);
52  0 assertEquals(m.getPattern(), "-2.1");
53  0 assertEquals(PA.getValue(m, "floatValue"), -2.1f);
54  0 assertEquals(PA.getValue(m, "longValue"), 0L);
55  0 assertSame(PA.getValue(m, "patternType"), PatternType.Float);
56   
57  0 m = new Matcher(Condition.NotContains, "-1.2f");
58  0 assertEquals(m.getCondition(), Condition.NotContains);
59  0 assertEquals(m.getPattern(), "-1.2f");
60  0 assertEquals(PA.getValue(m, "floatValue"), 0f);
61  0 assertEquals(PA.getValue(m, "longValue"), 0L);
62  0 assertSame(PA.getValue(m, "patternType"), PatternType.String);
63   
64  0 m = new Matcher(Condition.GE, "-1.2f");
65  0 assertEquals(m.getCondition(), Condition.GE);
66  0 assertEquals(m.getPattern(), "-1.2");
67  0 assertEquals(PA.getValue(m, "floatValue"), -1.2f);
68  0 assertEquals(PA.getValue(m, "longValue"), 0L);
69  0 assertSame(PA.getValue(m, "patternType"), PatternType.Float);
70   
71  0 m = new Matcher(Condition.GE, "113890813");
72  0 assertEquals(m.getCondition(), Condition.GE);
73  0 assertEquals(m.getPattern(), "113890813");
74  0 assertEquals(PA.getValue(m, "floatValue"), 0f);
75  0 assertEquals(PA.getValue(m, "longValue"), 113890813L);
76  0 assertSame(PA.getValue(m, "patternType"), PatternType.Integer);
77   
78  0 m = new Matcher(Condition.GE, "-987f");
79  0 assertEquals(m.getCondition(), Condition.GE);
80  0 assertEquals(m.getPattern(), "-987.0");
81  0 assertEquals(PA.getValue(m, "floatValue"), -987f);
82  0 assertEquals(PA.getValue(m, "longValue"), 0L);
83  0 assertSame(PA.getValue(m, "patternType"), PatternType.Float);
84   
85  0 try
86    {
87  0 new Matcher(null, 0f);
88  0 fail("Expected exception");
89    } catch (NullPointerException e)
90    {
91    // expected
92    }
93   
94  0 try
95    {
96  0 new Matcher(Condition.LT, "123,456");
97  0 fail("Expected exception");
98    } catch (NumberFormatException e)
99    {
100    // expected - see Long.valueOf()
101    }
102   
103  0 try
104    {
105  0 new Matcher(Condition.LT, "123_456");
106  0 fail("Expected exception");
107    } catch (NumberFormatException e)
108    {
109    // expected - see Long.valueOf()
110    }
111   
112  0 try
113    {
114  0 new Matcher(Condition.LT, "123456L");
115  0 fail("Expected exception");
116    } catch (NumberFormatException e)
117    {
118    // expected - see Long.valueOf()
119    }
120    }
121   
122    /**
123    * Tests for float comparison conditions
124    */
 
125  0 toggle @Test(groups = "Functional")
126    public void testMatches_float()
127    {
128    /*
129    * EQUALS test
130    */
131  0 MatcherI m = new Matcher(Condition.EQ, 2f);
132  0 assertTrue(m.matches("2"));
133  0 assertTrue(m.matches("2.0"));
134  0 assertFalse(m.matches("2.01"));
135   
136    /*
137    * NOT EQUALS test
138    */
139  0 m = new Matcher(Condition.NE, 2f);
140  0 assertFalse(m.matches("2"));
141  0 assertFalse(m.matches("2.0"));
142  0 assertTrue(m.matches("2.01"));
143   
144    /*
145    * >= test
146    */
147  0 m = new Matcher(Condition.GE, "2f");
148  0 assertTrue(m.matches("2"));
149  0 assertTrue(m.matches("2.1"));
150  0 assertFalse(m.matches("1.9"));
151   
152    /*
153    * > test
154    */
155  0 m = new Matcher(Condition.GT, 2f);
156  0 assertFalse(m.matches("2"));
157  0 assertTrue(m.matches("2.1"));
158  0 assertFalse(m.matches("1.9"));
159   
160    /*
161    * <= test
162    */
163  0 m = new Matcher(Condition.LE, "2.0f");
164  0 assertTrue(m.matches("2"));
165  0 assertFalse(m.matches("2.1"));
166  0 assertTrue(m.matches("1.9"));
167   
168    /*
169    * < test
170    */
171  0 m = new Matcher(Condition.LT, 2f);
172  0 assertFalse(m.matches("2"));
173  0 assertFalse(m.matches("2.1"));
174  0 assertTrue(m.matches("1.9"));
175    }
176   
177    /**
178    * Verifies that all numeric match conditions fail when applied to non-numeric
179    * or null values
180    */
 
181  0 toggle @Test(groups = "Functional")
182    public void testNumericMatch_nullOrInvalidValue()
183    {
184  0 for (Condition cond : Condition.values())
185    {
186  0 if (cond.isNumeric())
187    {
188  0 MatcherI m1 = new Matcher(cond, 2.1f);
189  0 MatcherI m2 = new Matcher(cond, 2345L);
190  0 assertFalse(m1.matches(null));
191  0 assertFalse(m1.matches(""));
192  0 assertFalse(m1.matches("two"));
193  0 assertFalse(m2.matches(null));
194  0 assertFalse(m2.matches(""));
195  0 assertFalse(m2.matches("two"));
196    }
197    }
198    }
199   
200    /**
201    * Tests for string comparison conditions
202    */
 
203  0 toggle @Test(groups = "Functional")
204    public void testMatches_pattern()
205    {
206    /*
207    * Contains
208    */
209  0 MatcherI m = new Matcher(Condition.Contains, "benign");
210  0 assertTrue(m.matches("benign"));
211  0 assertTrue(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
212  0 assertFalse(m.matches("pathogenic"));
213  0 assertFalse(m.matches(null));
214   
215    /*
216    * does not contain
217    */
218  0 m = new Matcher(Condition.NotContains, "benign");
219  0 assertFalse(m.matches("benign"));
220  0 assertFalse(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
221  0 assertTrue(m.matches("pathogenic"));
222  0 assertTrue(m.matches(null)); // null value passes this condition
223   
224    /*
225    * matches
226    */
227  0 m = new Matcher(Condition.Matches, "benign");
228  0 assertTrue(m.matches("benign"));
229  0 assertTrue(m.matches(" Benign ")); // trim before testing
230  0 assertFalse(m.matches("MOSTLY BENIGN"));
231  0 assertFalse(m.matches("pathogenic"));
232  0 assertFalse(m.matches(null));
233   
234    /*
235    * does not match
236    */
237  0 m = new Matcher(Condition.NotMatches, "benign");
238  0 assertFalse(m.matches("benign"));
239  0 assertFalse(m.matches(" Benign ")); // trimmed before testing
240  0 assertTrue(m.matches("MOSTLY BENIGN"));
241  0 assertTrue(m.matches("pathogenic"));
242  0 assertTrue(m.matches(null));
243   
244    /*
245    * value is present (is not null)
246    */
247  0 m = new Matcher(Condition.Present, null);
248  0 assertTrue(m.matches("benign"));
249  0 assertTrue(m.matches(""));
250  0 assertFalse(m.matches(null));
251   
252    /*
253    * value is not present (is null)
254    */
255  0 m = new Matcher(Condition.NotPresent, null);
256  0 assertFalse(m.matches("benign"));
257  0 assertFalse(m.matches(""));
258  0 assertTrue(m.matches(null));
259   
260    /*
261    * a number with a string match condition will be treated as string
262    * (these cases shouldn't arise as the match() method is coded)
263    */
264  0 Matcher m1 = new Matcher(Condition.Contains, "32");
265  0 assertFalse(m1.matchesFloat("-203f", 0f));
266  0 assertTrue(m1.matchesFloat("-4321.0f", 0f));
267  0 assertFalse(m1.matchesFloat("-203", 0f));
268  0 assertTrue(m1.matchesFloat("-4321", 0f));
269  0 assertFalse(m1.matchesLong("-203"));
270  0 assertTrue(m1.matchesLong("-4321"));
271  0 assertFalse(m1.matchesLong("-203f"));
272  0 assertTrue(m1.matchesLong("-4321.0f"));
273    }
274   
275    /**
276    * If a float is passed with a string condition it gets converted to a string
277    */
 
278  0 toggle @Test(groups = "Functional")
279    public void testMatches_floatWithStringCondition()
280    {
281  0 MatcherI m = new Matcher(Condition.Contains, 1.2e-6f);
282  0 assertEquals(m.getPattern(), "1.2E-6");
283  0 assertEquals(PA.getValue(m, "uppercasePattern"), "1.2E-6");
284  0 assertEquals(PA.getValue(m, "floatValue"), 0f);
285  0 assertEquals(PA.getValue(m, "longValue"), 0L);
286  0 assertSame(PA.getValue(m, "patternType"), PatternType.String);
287  0 assertTrue(m.matches("1.2e-6"));
288   
289  0 m = new Matcher(Condition.Contains, 0.0000001f);
290  0 assertEquals(m.getPattern(), "1.0E-7");
291  0 assertTrue(m.matches("1.0e-7"));
292  0 assertTrue(m.matches("1.0E-7"));
293  0 assertFalse(m.matches("0.0000001f"));
294    }
295   
 
296  0 toggle @Test(groups = "Functional")
297    public void testToString()
298    {
299  0 Locale.setDefault(Locale.ENGLISH);
300   
301  0 MatcherI m = new Matcher(Condition.LT, 1.2e-6f);
302  0 assertEquals(m.toString(), "< 1.2E-6");
303   
304  0 m = new Matcher(Condition.GE, "20200413");
305  0 assertEquals(m.toString(), ">= 20200413");
306   
307  0 m = new Matcher(Condition.NotMatches, "ABC");
308  0 assertEquals(m.toString(), "Does not match 'ABC'");
309   
310  0 m = new Matcher(Condition.Contains, -1.2f);
311  0 assertEquals(m.toString(), "Contains '-1.2'");
312    }
313   
 
314  0 toggle @Test(groups = "Functional")
315    public void testEquals()
316    {
317    /*
318    * string condition
319    */
320  0 MatcherI m = new Matcher(Condition.NotMatches, "ABC");
321  0 assertFalse(m.equals(null));
322  0 assertFalse(m.equals("foo"));
323  0 assertTrue(m.equals(m));
324  0 assertTrue(m.equals(new Matcher(Condition.NotMatches, "ABC")));
325    // not case-sensitive:
326  0 assertTrue(m.equals(new Matcher(Condition.NotMatches, "abc")));
327  0 assertFalse(m.equals(new Matcher(Condition.Matches, "ABC")));
328  0 assertFalse(m.equals(new Matcher(Condition.NotMatches, "def")));
329   
330    /*
331    * numeric conditions - float values
332    */
333  0 m = new Matcher(Condition.LT, -1f);
334  0 assertFalse(m.equals(null));
335  0 assertFalse(m.equals("foo"));
336  0 assertTrue(m.equals(m));
337  0 assertTrue(m.equals(new Matcher(Condition.LT, -1f)));
338  0 assertTrue(m.equals(new Matcher(Condition.LT, "-1f")));
339  0 assertTrue(m.equals(new Matcher(Condition.LT, "-1.00f")));
340  0 assertFalse(m.equals(new Matcher(Condition.LE, -1f)));
341  0 assertFalse(m.equals(new Matcher(Condition.GE, -1f)));
342  0 assertFalse(m.equals(new Matcher(Condition.NE, -1f)));
343  0 assertFalse(m.equals(new Matcher(Condition.LT, 1f)));
344  0 assertFalse(m.equals(new Matcher(Condition.LT, -1.1f)));
345   
346    /*
347    * numeric conditions - integer values
348    */
349  0 m = new Matcher(Condition.LT, -123456);
350  0 assertFalse(m.equals(null));
351  0 assertFalse(m.equals("foo"));
352  0 assertTrue(m.equals(m));
353  0 assertTrue(m.equals(new Matcher(Condition.LT, -123456)));
354  0 assertFalse(m.equals(new Matcher(Condition.LT, +123456)));
355  0 assertTrue(m.equals(new Matcher(Condition.LT, "-123456")));
356  0 assertFalse(m.equals(new Matcher(Condition.LT, -123456f)));
357  0 assertFalse(m.equals(new Matcher(Condition.LT, "-123456f")));
358    }
359   
 
360  0 toggle @Test(groups = "Functional")
361    public void testHashCode()
362    {
363  0 MatcherI m1 = new Matcher(Condition.NotMatches, "ABC");
364  0 MatcherI m2 = new Matcher(Condition.NotMatches, "ABC");
365  0 MatcherI m3 = new Matcher(Condition.NotMatches, "AB");
366  0 MatcherI m4 = new Matcher(Condition.Matches, "ABC");
367  0 assertEquals(m1.hashCode(), m2.hashCode());
368  0 assertNotEquals(m1.hashCode(), m3.hashCode());
369  0 assertNotEquals(m1.hashCode(), m4.hashCode());
370  0 assertNotEquals(m3.hashCode(), m4.hashCode());
371    }
372   
373    /**
374    * Tests for integer comparison conditions
375    */
 
376  0 toggle @Test(groups = "Functional")
377    public void testMatches_long()
378    {
379    /*
380    * EQUALS test
381    */
382  0 MatcherI m = new Matcher(Condition.EQ, 2);
383  0 assertTrue(m.matches("2"));
384  0 assertTrue(m.matches("+2"));
385  0 assertFalse(m.matches("3"));
386    // a float value may be passed to an integer matcher
387  0 assertTrue(m.matches("2.0"));
388  0 assertTrue(m.matches("2.000000f"));
389  0 assertFalse(m.matches("2.01"));
390   
391    /*
392    * NOT EQUALS test
393    */
394  0 m = new Matcher(Condition.NE, 123);
395  0 assertFalse(m.matches("123"));
396  0 assertFalse(m.matches("123.0"));
397  0 assertTrue(m.matches("-123"));
398   
399    /*
400    * >= test
401    */
402  0 m = new Matcher(Condition.GE, "113890813");
403  0 assertTrue(m.matches("113890813"));
404  0 assertTrue(m.matches("113890814"));
405  0 assertFalse(m.matches("-113890813"));
406   
407    /*
408    * > test
409    */
410  0 m = new Matcher(Condition.GT, 113890813);
411  0 assertFalse(m.matches("113890813"));
412  0 assertTrue(m.matches("113890814"));
413   
414    /*
415    * <= test
416    */
417  0 m = new Matcher(Condition.LE, "113890813");
418  0 assertTrue(m.matches("113890813"));
419  0 assertFalse(m.matches("113890814"));
420  0 assertTrue(m.matches("113890812"));
421   
422    /*
423    * < test
424    */
425  0 m = new Matcher(Condition.LT, 113890813);
426  0 assertFalse(m.matches("113890813"));
427  0 assertFalse(m.matches("113890814"));
428  0 assertTrue(m.matches("113890812"));
429    }
430   
431    /**
432    * Tests comparing a float value with an integer condition
433    */
 
434  0 toggle @Test(groups = "Functional")
435    public void testMatches_floatValueIntegerCondition()
436    {
437  0 MatcherI m = new Matcher(Condition.GT, 1234);
438  0 assertEquals(PA.getValue(m, "longValue"), 1234L);
439  0 assertSame(PA.getValue(m, "patternType"), PatternType.Integer);
440  0 assertTrue(m.matches("1235"));
441  0 assertTrue(m.matches("9867.345"));
442  0 assertTrue(m.matches("9867.345f"));
443    }
444    }