1 package jalview.util.matcher;
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;
9 import java.util.Locale;
11 import org.testng.annotations.Test;
13 import junit.extensions.PA;
15 public class MatcherTest
17 @Test(groups = "Functional")
18 public void testConstructor()
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);
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);
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);
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);
43 new Matcher(null, 0f);
44 fail("Expected exception");
45 } catch (NullPointerException e)
52 new Matcher(Condition.LT, "123,456");
53 fail("Expected exception");
54 } catch (NumberFormatException e)
61 * Tests for float comparison conditions
63 @Test(groups = "Functional")
64 public void testMatches_float()
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"));
77 m = new Matcher(Condition.NE, 2f);
78 assertFalse(m.matches("2"));
79 assertFalse(m.matches("2.0"));
80 assertTrue(m.matches("2.01"));
85 m = new Matcher(Condition.GE, 2f);
86 assertTrue(m.matches("2"));
87 assertTrue(m.matches("2.1"));
88 assertFalse(m.matches("1.9"));
93 m = new Matcher(Condition.GT, 2f);
94 assertFalse(m.matches("2"));
95 assertTrue(m.matches("2.1"));
96 assertFalse(m.matches("1.9"));
101 m = new Matcher(Condition.LE, 2f);
102 assertTrue(m.matches("2"));
103 assertFalse(m.matches("2.1"));
104 assertTrue(m.matches("1.9"));
109 m = new Matcher(Condition.LT, 2f);
110 assertFalse(m.matches("2"));
111 assertFalse(m.matches("2.1"));
112 assertTrue(m.matches("1.9"));
115 @Test(groups = "Functional")
116 public void testMatches_floatNullOrInvalid()
118 for (Condition cond : Condition.values())
120 if (cond.isNumeric())
122 MatcherI m = new Matcher(cond, 2f);
123 assertFalse(m.matches(null));
124 assertFalse(m.matches(""));
125 assertFalse(m.matches("two"));
131 * Tests for string comparison conditions
133 @Test(groups = "Functional")
134 public void testMatches_pattern()
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));
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
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));
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));
175 * value is present (is not null)
177 m = new Matcher(Condition.Present, null);
178 assertTrue(m.matches("benign"));
179 assertTrue(m.matches(""));
180 assertFalse(m.matches(null));
183 * value is not present (is null)
185 m = new Matcher(Condition.NotPresent, null);
186 assertFalse(m.matches("benign"));
187 assertFalse(m.matches(""));
188 assertTrue(m.matches(null));
191 * a float with a string match condition will be treated as string
193 Matcher m1 = new Matcher(Condition.Contains, "32");
194 assertFalse(m1.matches(-203f));
195 assertTrue(m1.matches(-4321.0f));
199 * If a float is passed with a string condition it gets converted to a string
201 @Test(groups = "Functional")
202 public void testMatches_floatWithStringCondition()
204 MatcherI m = new Matcher(Condition.Contains, 1.2e-6f);
205 assertTrue(m.matches("1.2e-6"));
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"));
213 @Test(groups = "Functional")
214 public void testToString()
216 Locale.setDefault(Locale.ENGLISH);
218 MatcherI m = new Matcher(Condition.LT, 1.2e-6f);
219 assertEquals(m.toString(), "< 1.2E-6");
221 m = new Matcher(Condition.NotMatches, "ABC");
222 assertEquals(m.toString(), "Does not match 'ABC'");
224 m = new Matcher(Condition.Contains, -1.2f);
225 assertEquals(m.toString(), "Contains '-1.2'");
228 @Test(groups = "Functional")
229 public void testEquals()
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")));
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)));
261 @Test(groups = "Functional")
262 public void testHashCode()
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());