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