Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 11:45:30 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.9821428798.2%
 

Contributing tests

This file is covered by 10 tests. .

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  1 toggle @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    // expected
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    // expected - see Long.valueOf()
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    // expected - see Long.valueOf()
110    }
111   
112  1 try
113    {
114  1 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  1 toggle @Test(groups = "Functional")
126    public void testMatches_float()
127    {
128    /*
129    * EQUALS test
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    * NOT EQUALS test
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    * >= test
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    * > test
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    * <= test
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    * < test
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    * Verifies that all numeric match conditions fail when applied to non-numeric
179    * or null values
180    */
 
181  1 toggle @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    * Tests for string comparison conditions
202    */
 
203  1 toggle @Test(groups = "Functional")
204    public void testMatches_pattern()
205    {
206    /*
207    * Contains
208    */
209  1 MatcherI m = new Matcher(Condition.Contains, "benign");
210  1 assertTrue(m.matches("benign"));
211  1 assertTrue(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
212  1 assertFalse(m.matches("pathogenic"));
213  1 assertFalse(m.matches(null));
214   
215    /*
216    * does not contain
217    */
218  1 m = new Matcher(Condition.NotContains, "benign");
219  1 assertFalse(m.matches("benign"));
220  1 assertFalse(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
221  1 assertTrue(m.matches("pathogenic"));
222  1 assertTrue(m.matches(null)); // null value passes this condition
223   
224    /*
225    * matches
226    */
227  1 m = new Matcher(Condition.Matches, "benign");
228  1 assertTrue(m.matches("benign"));
229  1 assertTrue(m.matches(" Benign ")); // trim before testing
230  1 assertFalse(m.matches("MOSTLY BENIGN"));
231  1 assertFalse(m.matches("pathogenic"));
232  1 assertFalse(m.matches(null));
233   
234    /*
235    * does not match
236    */
237  1 m = new Matcher(Condition.NotMatches, "benign");
238  1 assertFalse(m.matches("benign"));
239  1 assertFalse(m.matches(" Benign ")); // trimmed before testing
240  1 assertTrue(m.matches("MOSTLY BENIGN"));
241  1 assertTrue(m.matches("pathogenic"));
242  1 assertTrue(m.matches(null));
243   
244    /*
245    * value is present (is not null)
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    * value is not present (is null)
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    * 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  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    * If a float is passed with a string condition it gets converted to a string
277    */
 
278  1 toggle @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   
 
296  1 toggle @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   
 
314  1 toggle @Test(groups = "Functional")
315    public void testEquals()
316    {
317    /*
318    * string condition
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    // not case-sensitive:
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    * numeric conditions - float values
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    * numeric conditions - integer values
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   
 
360  1 toggle @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    * Tests for integer comparison conditions
375    */
 
376  1 toggle @Test(groups = "Functional")
377    public void testMatches_long()
378    {
379    /*
380    * EQUALS test
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    // a float value may be passed to an integer matcher
387  1 assertTrue(m.matches("2.0"));
388  1 assertTrue(m.matches("2.000000f"));
389  1 assertFalse(m.matches("2.01"));
390   
391    /*
392    * NOT EQUALS test
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    * >= test
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    * > test
409    */
410  1 m = new Matcher(Condition.GT, 113890813);
411  1 assertFalse(m.matches("113890813"));
412  1 assertTrue(m.matches("113890814"));
413   
414    /*
415    * <= test
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    * < test
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    * Tests comparing a float value with an integer condition
433    */
 
434  1 toggle @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    }