Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.util.matcher

File MatcherTest.java

 

Code metrics

2
212
10
1
424
286
15
0.07
21.2
10
1.5

Classes

Class Line # Actions
MatcherTest 17 212 15
0.9821428798.2%
 

Contributing tests

This file is covered by 10 tests. .

Source view

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   
 
17    public class MatcherTest
18    {
 
19  1 toggle @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    // expected
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    // expected - see Long.valueOf()
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    // expected - see Long.valueOf()
90    }
91   
92  1 try
93    {
94  1 new Matcher(Condition.LT, "123456L");
95  0 fail("Expected exception");
96    } catch (NumberFormatException e)
97    {
98    // expected - see Long.valueOf()
99    }
100    }
101   
102    /**
103    * Tests for float comparison conditions
104    */
 
105  1 toggle @Test(groups = "Functional")
106    public void testMatches_float()
107    {
108    /*
109    * EQUALS test
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    * NOT EQUALS test
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    * >= test
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    * > test
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    * <= test
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    * < test
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    * Verifies that all numeric match conditions fail when applied to non-numeric
159    * or null values
160    */
 
161  1 toggle @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    * Tests for string comparison conditions
182    */
 
183  1 toggle @Test(groups = "Functional")
184    public void testMatches_pattern()
185    {
186    /*
187    * Contains
188    */
189  1 MatcherI m = new Matcher(Condition.Contains, "benign");
190  1 assertTrue(m.matches("benign"));
191  1 assertTrue(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
192  1 assertFalse(m.matches("pathogenic"));
193  1 assertFalse(m.matches(null));
194   
195    /*
196    * does not contain
197    */
198  1 m = new Matcher(Condition.NotContains, "benign");
199  1 assertFalse(m.matches("benign"));
200  1 assertFalse(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
201  1 assertTrue(m.matches("pathogenic"));
202  1 assertTrue(m.matches(null)); // null value passes this condition
203   
204    /*
205    * matches
206    */
207  1 m = new Matcher(Condition.Matches, "benign");
208  1 assertTrue(m.matches("benign"));
209  1 assertTrue(m.matches(" Benign ")); // trim before testing
210  1 assertFalse(m.matches("MOSTLY BENIGN"));
211  1 assertFalse(m.matches("pathogenic"));
212  1 assertFalse(m.matches(null));
213   
214    /*
215    * does not match
216    */
217  1 m = new Matcher(Condition.NotMatches, "benign");
218  1 assertFalse(m.matches("benign"));
219  1 assertFalse(m.matches(" Benign ")); // trimmed before testing
220  1 assertTrue(m.matches("MOSTLY BENIGN"));
221  1 assertTrue(m.matches("pathogenic"));
222  1 assertTrue(m.matches(null));
223   
224    /*
225    * value is present (is not null)
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    * value is not present (is null)
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    * a number with a string match condition will be treated as string
242    * (these cases shouldn't arise as the match() method is coded)
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    * If a float is passed with a string condition it gets converted to a string
257    */
 
258  1 toggle @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   
 
276  1 toggle @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   
 
294  1 toggle @Test(groups = "Functional")
295    public void testEquals()
296    {
297    /*
298    * string condition
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    // not case-sensitive:
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    * numeric conditions - float values
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    * numeric conditions - integer values
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   
 
340  1 toggle @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    * Tests for integer comparison conditions
355    */
 
356  1 toggle @Test(groups = "Functional")
357    public void testMatches_long()
358    {
359    /*
360    * EQUALS test
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    // a float value may be passed to an integer matcher
367  1 assertTrue(m.matches("2.0"));
368  1 assertTrue(m.matches("2.000000f"));
369  1 assertFalse(m.matches("2.01"));
370   
371    /*
372    * NOT EQUALS test
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    * >= test
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    * > test
389    */
390  1 m = new Matcher(Condition.GT, 113890813);
391  1 assertFalse(m.matches("113890813"));
392  1 assertTrue(m.matches("113890814"));
393   
394    /*
395    * <= test
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    * < test
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    * Tests comparing a float value with an integer condition
413    */
 
414  1 toggle @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    }