1 package jalview.datamodel.features;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertSame;
6 import static org.testng.Assert.assertTrue;
7 import static org.testng.Assert.fail;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.util.matcher.Condition;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Locale;
17 import org.testng.annotations.Test;
19 public class FeatureMatcherSetTest
21 @Test(groups = "Functional")
22 public void testMatches_byAttribute()
25 * a numeric matcher - MatcherTest covers more conditions
27 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
29 FeatureMatcherSetI fms = new FeatureMatcherSet();
31 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
32 assertFalse(fms.matches(sf));
33 sf.setValue("AF", "foobar");
34 assertFalse(fms.matches(sf));
35 sf.setValue("AF", "-2");
36 assertTrue(fms.matches(sf));
37 sf.setValue("AF", "-1");
38 assertTrue(fms.matches(sf));
39 sf.setValue("AF", "-3");
40 assertFalse(fms.matches(sf));
41 sf.setValue("AF", "");
42 assertFalse(fms.matches(sf));
45 * a string pattern matcher
47 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
48 fms = new FeatureMatcherSet();
50 assertFalse(fms.matches(sf));
51 sf.setValue("AF", "raining cats and dogs");
52 assertTrue(fms.matches(sf));
55 @Test(groups = "Functional")
58 // condition1: AF value contains "dog" (matches)
59 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.Contains,
61 // condition 2: CSQ value does not contain "how" (does not match)
62 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
65 SequenceFeature sf = new SequenceFeature("Cath", "helix domain", 11, 12,
67 sf.setValue("AF", "raining cats and dogs");
68 sf.setValue("CSQ", "showers");
70 assertTrue(fm1.matches(sf));
71 assertFalse(fm2.matches(sf));
73 FeatureMatcherSetI fms = new FeatureMatcherSet();
74 assertTrue(fms.matches(sf)); // if no conditions, then 'all' pass
76 assertTrue(fms.matches(sf));
78 assertFalse(fms.matches(sf));
81 * OR a failed attribute condition with a matched label condition
83 fms = new FeatureMatcherSet();
85 assertFalse(fms.matches(sf));
86 FeatureMatcher byLabelPass = FeatureMatcher.byLabel(Condition.Contains,
89 assertTrue(fms.matches(sf));
92 * OR a failed attribute condition with a failed score condition
94 fms = new FeatureMatcherSet();
96 assertFalse(fms.matches(sf));
97 FeatureMatcher byScoreFail = FeatureMatcher.byScore(Condition.LT,
100 assertFalse(fms.matches(sf));
103 * OR failed attribute and score conditions with matched label condition
105 fms = new FeatureMatcherSet();
106 fms.or(fm2).or(byScoreFail);
107 assertFalse(fms.matches(sf));
109 assertTrue(fms.matches(sf));
112 @Test(groups = "Functional")
113 public void testToString()
115 Locale.setDefault(Locale.ENGLISH);
116 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.LT, "1.2",
118 assertEquals(fm1.toString(), "AF < 1.2");
120 FeatureMatcher fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
122 assertEquals(fm2.toString(), "CLIN_SIG does not contain 'path'");
127 FeatureMatcherSetI fms = new FeatureMatcherSet();
128 assertEquals(fms.toString(), "");
130 assertEquals(fms.toString(), "(AF < 1.2)");
132 assertEquals(fms.toString(),
133 "(AF < 1.2) and (CLIN_SIG does not contain 'path')");
138 fms = new FeatureMatcherSet();
139 assertEquals(fms.toString(), "");
141 assertEquals(fms.toString(), "(AF < 1.2)");
143 assertEquals(fms.toString(),
144 "(AF < 1.2) or (CLIN_SIG does not contain 'path')");
149 fail("Expected exception");
150 } catch (IllegalStateException e)
156 @Test(groups = "Functional")
159 // condition1: AF value contains "dog" (matches)
160 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.Contains,
162 // condition 2: CSQ value does not contain "how" (does not match)
163 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
166 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
167 sf.setValue("AF", "raining cats and dogs");
168 sf.setValue("CSQ", "showers");
170 assertTrue(fm1.matches(sf));
171 assertFalse(fm2.matches(sf));
173 FeatureMatcherSetI fms = new FeatureMatcherSet();
174 assertTrue(fms.matches(sf)); // if no conditions, then 'all' pass
176 assertTrue(fms.matches(sf));
178 assertTrue(fms.matches(sf)); // true or false makes true
180 fms = new FeatureMatcherSet();
182 assertFalse(fms.matches(sf));
184 assertTrue(fms.matches(sf)); // false or true makes true
189 fail("Expected exception");
190 } catch (IllegalStateException e)
196 @Test(groups = "Functional")
197 public void testIsEmpty()
199 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2.0",
201 FeatureMatcherSetI fms = new FeatureMatcherSet();
202 assertTrue(fms.isEmpty());
204 assertFalse(fms.isEmpty());
207 @Test(groups = "Functional")
208 public void testGetMatchers()
210 FeatureMatcherSetI fms = new FeatureMatcherSet();
215 Iterator<FeatureMatcherI> iterator = fms.getMatchers().iterator();
216 assertFalse(iterator.hasNext());
221 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.GE, "-2",
224 iterator = fms.getMatchers().iterator();
225 assertSame(fm1, iterator.next());
226 assertFalse(iterator.hasNext());
231 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.LT, "8f",
234 iterator = fms.getMatchers().iterator();
235 assertSame(fm1, iterator.next());
236 assertSame(fm2, iterator.next());
237 assertFalse(iterator.hasNext());
241 * Tests for the 'compound attribute' key i.e. where first key's value is a map
242 * from which we take the value for the second key, e.g. CSQ : Consequence
244 @Test(groups = "Functional")
245 public void testMatches_compoundKey()
248 * a numeric matcher - MatcherTest covers more conditions
250 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
251 "CSQ", "Consequence");
252 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 10, "grp");
253 FeatureMatcherSetI fms = new FeatureMatcherSet();
255 assertFalse(fms.matches(sf));
256 Map<String, String> csq = new HashMap<>();
257 sf.setValue("CSQ", csq);
258 assertFalse(fms.matches(sf));
259 csq.put("Consequence", "-2");
260 assertTrue(fms.matches(sf));
261 csq.put("Consequence", "-1");
262 assertTrue(fms.matches(sf));
263 csq.put("Consequence", "-3");
264 assertFalse(fms.matches(sf));
265 csq.put("Consequence", "");
266 assertFalse(fms.matches(sf));
267 csq.put("Consequence", "junk");
268 assertFalse(fms.matches(sf));
271 * a string pattern matcher
273 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "CSQ",
275 fms = new FeatureMatcherSet();
277 assertFalse(fms.matches(sf));
278 csq.put("PolyPhen", "damaging");
279 assertFalse(fms.matches(sf));
280 csq.put("Consequence", "damaging");
281 assertFalse(fms.matches(sf));
282 csq.put("Consequence", "Catastrophic");
283 assertTrue(fms.matches(sf));