Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.util.matcher

File MatcherTest.java

 

Code metrics

2
127
8
1
273
183
11
0.09
15.88
8
1.38

Classes

Class Line # Actions
MatcherTest 15 127 11 2
0.9854014598.5%
 

Contributing tests

This file is covered by 8 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.assertTrue;
7    import static org.testng.Assert.fail;
8   
9    import java.util.Locale;
10   
11    import org.testng.annotations.Test;
12   
13    import junit.extensions.PA;
14   
 
15    public class MatcherTest
16    {
 
17  1 toggle @Test(groups = "Functional")
18    public void testConstructor()
19    {
20  1 MatcherI m = new Matcher(Condition.Contains, "foo");
21  1 assertEquals(m.getCondition(), Condition.Contains);
22  1 assertEquals(m.getPattern(), "foo");
23  1 assertEquals(PA.getValue(m, "uppercasePattern"), "FOO");
24  1 assertEquals(m.getFloatValue(), 0f);
25   
26  1 m = new Matcher(Condition.GT, -2.1f);
27  1 assertEquals(m.getCondition(), Condition.GT);
28  1 assertEquals(m.getPattern(), "-2.1");
29  1 assertEquals(m.getFloatValue(), -2.1f);
30   
31  1 m = new Matcher(Condition.NotContains, "-1.2f");
32  1 assertEquals(m.getCondition(), Condition.NotContains);
33  1 assertEquals(m.getPattern(), "-1.2f");
34  1 assertEquals(m.getFloatValue(), 0f);
35   
36  1 m = new Matcher(Condition.GE, "-1.2f");
37  1 assertEquals(m.getCondition(), Condition.GE);
38  1 assertEquals(m.getPattern(), "-1.2");
39  1 assertEquals(m.getFloatValue(), -1.2f);
40   
41  1 try
42    {
43  1 new Matcher(null, 0f);
44  0 fail("Expected exception");
45    } catch (NullPointerException e)
46    {
47    // expected
48    }
49   
50  1 try
51    {
52  1 new Matcher(Condition.LT, "123,456");
53  0 fail("Expected exception");
54    } catch (NumberFormatException e)
55    {
56    // expected
57    }
58    }
59   
60    /**
61    * Tests for float comparison conditions
62    */
 
63  1 toggle @Test(groups = "Functional")
64    public void testMatches_float()
65    {
66    /*
67    * EQUALS test
68    */
69  1 MatcherI m = new Matcher(Condition.EQ, 2f);
70  1 assertTrue(m.matches("2"));
71  1 assertTrue(m.matches("2.0"));
72  1 assertFalse(m.matches("2.01"));
73   
74    /*
75    * NOT EQUALS test
76    */
77  1 m = new Matcher(Condition.NE, 2f);
78  1 assertFalse(m.matches("2"));
79  1 assertFalse(m.matches("2.0"));
80  1 assertTrue(m.matches("2.01"));
81   
82    /*
83    * >= test
84    */
85  1 m = new Matcher(Condition.GE, 2f);
86  1 assertTrue(m.matches("2"));
87  1 assertTrue(m.matches("2.1"));
88  1 assertFalse(m.matches("1.9"));
89   
90    /*
91    * > test
92    */
93  1 m = new Matcher(Condition.GT, 2f);
94  1 assertFalse(m.matches("2"));
95  1 assertTrue(m.matches("2.1"));
96  1 assertFalse(m.matches("1.9"));
97   
98    /*
99    * <= test
100    */
101  1 m = new Matcher(Condition.LE, 2f);
102  1 assertTrue(m.matches("2"));
103  1 assertFalse(m.matches("2.1"));
104  1 assertTrue(m.matches("1.9"));
105   
106    /*
107    * < test
108    */
109  1 m = new Matcher(Condition.LT, 2f);
110  1 assertFalse(m.matches("2"));
111  1 assertFalse(m.matches("2.1"));
112  1 assertTrue(m.matches("1.9"));
113    }
114   
 
115  1 toggle @Test(groups = "Functional")
116    public void testMatches_floatNullOrInvalid()
117    {
118  1 for (Condition cond : Condition.values())
119    {
120  12 if (cond.isNumeric())
121    {
122  6 MatcherI m = new Matcher(cond, 2f);
123  6 assertFalse(m.matches(null));
124  6 assertFalse(m.matches(""));
125  6 assertFalse(m.matches("two"));
126    }
127    }
128    }
129   
130    /**
131    * Tests for string comparison conditions
132    */
 
133  1 toggle @Test(groups = "Functional")
134    public void testMatches_pattern()
135    {
136    /*
137    * Contains
138    */
139  1 MatcherI m = new Matcher(Condition.Contains, "benign");
140  1 assertTrue(m.matches("benign"));
141  1 assertTrue(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
142  1 assertFalse(m.matches("pathogenic"));
143  1 assertFalse(m.matches(null));
144   
145    /*
146    * does not contain
147    */
148  1 m = new Matcher(Condition.NotContains, "benign");
149  1 assertFalse(m.matches("benign"));
150  1 assertFalse(m.matches("MOSTLY BENIGN OBSERVED")); // not case-sensitive
151  1 assertTrue(m.matches("pathogenic"));
152  1 assertTrue(m.matches(null)); // null value passes this condition
153   
154    /*
155    * matches
156    */
157  1 m = new Matcher(Condition.Matches, "benign");
158  1 assertTrue(m.matches("benign"));
159  1 assertTrue(m.matches(" Benign ")); // trim before testing
160  1 assertFalse(m.matches("MOSTLY BENIGN"));
161  1 assertFalse(m.matches("pathogenic"));
162  1 assertFalse(m.matches(null));
163   
164    /*
165    * does not match
166    */
167  1 m = new Matcher(Condition.NotMatches, "benign");
168  1 assertFalse(m.matches("benign"));
169  1 assertFalse(m.matches(" Benign ")); // trim before testing
170  1 assertTrue(m.matches("MOSTLY BENIGN"));
171  1 assertTrue(m.matches("pathogenic"));
172  1 assertTrue(m.matches(null));
173   
174    /*
175    * value is present (is not null)
176    */
177  1 m = new Matcher(Condition.Present, null);
178  1 assertTrue(m.matches("benign"));
179  1 assertTrue(m.matches(""));
180  1 assertFalse(m.matches(null));
181   
182    /*
183    * value is not present (is null)
184    */
185  1 m = new Matcher(Condition.NotPresent, null);
186  1 assertFalse(m.matches("benign"));
187  1 assertFalse(m.matches(""));
188  1 assertTrue(m.matches(null));
189   
190    /*
191    * a float with a string match condition will be treated as string
192    */
193  1 Matcher m1 = new Matcher(Condition.Contains, "32");
194  1 assertFalse(m1.matches(-203f));
195  1 assertTrue(m1.matches(-4321.0f));
196    }
197   
198    /**
199    * If a float is passed with a string condition it gets converted to a string
200    */
 
201  1 toggle @Test(groups = "Functional")
202    public void testMatches_floatWithStringCondition()
203    {
204  1 MatcherI m = new Matcher(Condition.Contains, 1.2e-6f);
205  1 assertTrue(m.matches("1.2e-6"));
206   
207  1 m = new Matcher(Condition.Contains, 0.0000001f);
208  1 assertTrue(m.matches("1.0e-7"));
209  1 assertTrue(m.matches("1.0E-7"));
210  1 assertFalse(m.matches("0.0000001f"));
211    }
212   
 
213  1 toggle @Test(groups = "Functional")
214    public void testToString()
215    {
216  1 Locale.setDefault(Locale.ENGLISH);
217   
218  1 MatcherI m = new Matcher(Condition.LT, 1.2e-6f);
219  1 assertEquals(m.toString(), "< 1.2E-6");
220   
221  1 m = new Matcher(Condition.NotMatches, "ABC");
222  1 assertEquals(m.toString(), "Does not match 'ABC'");
223   
224  1 m = new Matcher(Condition.Contains, -1.2f);
225  1 assertEquals(m.toString(), "Contains '-1.2'");
226    }
227   
 
228  1 toggle @Test(groups = "Functional")
229    public void testEquals()
230    {
231    /*
232    * string condition
233    */
234  1 MatcherI m = new Matcher(Condition.NotMatches, "ABC");
235  1 assertFalse(m.equals(null));
236  1 assertFalse(m.equals("foo"));
237  1 assertTrue(m.equals(m));
238  1 assertTrue(m.equals(new Matcher(Condition.NotMatches, "ABC")));
239    // not case-sensitive:
240  1 assertTrue(m.equals(new Matcher(Condition.NotMatches, "abc")));
241  1 assertFalse(m.equals(new Matcher(Condition.Matches, "ABC")));
242  1 assertFalse(m.equals(new Matcher(Condition.NotMatches, "def")));
243   
244    /*
245    * numeric conditions
246    */
247  1 m = new Matcher(Condition.LT, -1f);
248  1 assertFalse(m.equals(null));
249  1 assertFalse(m.equals("foo"));
250  1 assertTrue(m.equals(m));
251  1 assertTrue(m.equals(new Matcher(Condition.LT, -1f)));
252  1 assertTrue(m.equals(new Matcher(Condition.LT, "-1f")));
253  1 assertTrue(m.equals(new Matcher(Condition.LT, "-1.00f")));
254  1 assertFalse(m.equals(new Matcher(Condition.LE, -1f)));
255  1 assertFalse(m.equals(new Matcher(Condition.GE, -1f)));
256  1 assertFalse(m.equals(new Matcher(Condition.NE, -1f)));
257  1 assertFalse(m.equals(new Matcher(Condition.LT, 1f)));
258  1 assertFalse(m.equals(new Matcher(Condition.LT, -1.1f)));
259    }
260   
 
261  1 toggle @Test(groups = "Functional")
262    public void testHashCode()
263    {
264  1 MatcherI m1 = new Matcher(Condition.NotMatches, "ABC");
265  1 MatcherI m2 = new Matcher(Condition.NotMatches, "ABC");
266  1 MatcherI m3 = new Matcher(Condition.NotMatches, "AB");
267  1 MatcherI m4 = new Matcher(Condition.Matches, "ABC");
268  1 assertEquals(m1.hashCode(), m2.hashCode());
269  1 assertNotEquals(m1.hashCode(), m3.hashCode());
270  1 assertNotEquals(m1.hashCode(), m4.hashCode());
271  1 assertNotEquals(m3.hashCode(), m4.hashCode());
272    }
273    }