JAL-2835 spike updated with latest
[jalview.git] / test / jalview / util / matcher / KeyedMatcherSetTest.java
index 3018cb6..3d597d2 100644 (file)
@@ -12,13 +12,13 @@ import org.testng.annotations.Test;
 
 public class KeyedMatcherSetTest
 {
-  @Test
+  @Test(groups = "Functional")
   public void testMatches()
   {
     /*
      * a numeric matcher - MatcherTest covers more conditions
      */
-    KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
     KeyedMatcherSetI kms = new KeyedMatcherSet();
     kms.and(km);
     assertTrue(kms.matches(key -> "-2"));
@@ -31,24 +31,25 @@ public class KeyedMatcherSetTest
     /*
      * a string pattern matcher
      */
-    km = new KeyedMatcher("AF", Condition.Contains, "Cat");
+    km = new KeyedMatcher(Condition.Contains, "Cat", "AF");
     kms = new KeyedMatcherSet();
     kms.and(km);
     assertTrue(kms
-            .matches(key -> "AF".equals(key) ? "raining cats and dogs"
+            .matches(key -> "AF".equals(key[0]) ? "raining cats and dogs"
             : "showers"));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testAnd()
   {
     // condition1: AF value contains "dog" (matches)
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
     // condition 2: CSQ value does not contain "how" (does not match)
-    KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
-            "how");
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
+            "CSQ");
 
-    Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
+    Function<String[], String> vp = key -> "AF".equals(key[0])
+            ? "raining cats and dogs"
             : "showers";
     assertTrue(km1.matches(vp));
     assertFalse(km2.matches(vp));
@@ -61,13 +62,14 @@ public class KeyedMatcherSetTest
     assertFalse(kms.matches(vp));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testToString()
   {
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.LT, 1.2f);
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.LT, 1.2f, "AF");
     assertEquals(km1.toString(), "AF < 1.2");
 
-    KeyedMatcher km2 = new KeyedMatcher("CLIN_SIG", Condition.NotContains, "path");
+    KeyedMatcher km2 = new KeyedMatcher(Condition.NotContains, "path",
+            "CLIN_SIG");
     assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'");
 
     /*
@@ -93,16 +95,17 @@ public class KeyedMatcherSetTest
             "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testOr()
   {
     // condition1: AF value contains "dog" (matches)
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.Contains, "dog");
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
     // condition 2: CSQ value does not contain "how" (does not match)
-    KeyedMatcherI km2 = new KeyedMatcher("CSQ", Condition.NotContains,
-            "how");
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
+            "CSQ");
 
-    Function<String, String> vp = key -> "AF".equals(key) ? "raining cats and dogs"
+    Function<String[], String> vp = key -> "AF".equals(key[0])
+            ? "raining cats and dogs"
             : "showers";
     assertTrue(km1.matches(vp));
     assertFalse(km2.matches(vp));
@@ -114,17 +117,17 @@ public class KeyedMatcherSetTest
     assertTrue(kms.matches(vp));
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testIsEmpty()
   {
-    KeyedMatcherI km = new KeyedMatcher("AF", Condition.GE, -2F);
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
     KeyedMatcherSetI kms = new KeyedMatcherSet();
     assertTrue(kms.isEmpty());
     kms.and(km);
     assertFalse(kms.isEmpty());
   }
 
-  @Test
+  @Test(groups = "Functional")
   public void testGetMatchers()
   {
     KeyedMatcherSetI kms = new KeyedMatcherSet();
@@ -138,7 +141,7 @@ public class KeyedMatcherSetTest
     /*
      * one matcher:
      */
-    KeyedMatcherI km1 = new KeyedMatcher("AF", Condition.GE, -2F);
+    KeyedMatcherI km1 = new KeyedMatcher(Condition.GE, -2F, "AF");
     kms.and(km1);
     iterator = kms.getMatchers().iterator();
     assertSame(km1, iterator.next());
@@ -147,11 +150,44 @@ public class KeyedMatcherSetTest
     /*
      * two matchers:
      */
-    KeyedMatcherI km2 = new KeyedMatcher("AF", Condition.LT, 8F);
+    KeyedMatcherI km2 = new KeyedMatcher(Condition.LT, 8F, "AF");
     kms.and(km2);
     iterator = kms.getMatchers().iterator();
     assertSame(km1, iterator.next());
     assertSame(km2, iterator.next());
     assertFalse(iterator.hasNext());
   }
+
+  /**
+   * Tests for the 'compound attribute' key i.e. where first key's value is a map
+   * from which we take the value for the second key, e.g. CSQ : Consequence
+   */
+  @Test(groups = "Functional")
+  public void testMatches_compoundKey()
+  {
+    /*
+     * a numeric matcher - MatcherTest covers more conditions
+     */
+    KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "CSQ",
+            "Consequence");
+    KeyedMatcherSetI kms = new KeyedMatcherSet();
+    kms.and(km);
+    assertTrue(kms.matches(key -> "-2"));
+    assertTrue(kms.matches(key -> "-1"));
+    assertFalse(kms.matches(key -> "-3"));
+    assertFalse(kms.matches(key -> ""));
+    assertFalse(kms.matches(key -> "junk"));
+    assertFalse(kms.matches(key -> null));
+  
+    /*
+     * a string pattern matcher
+     */
+    km = new KeyedMatcher(Condition.Contains, "Cat", "CSQ", "Consequence");
+    kms = new KeyedMatcherSet();
+    kms.and(km);
+    assertTrue(kms.matches(key -> "csq".equalsIgnoreCase(key[0])
+            && "Consequence".equalsIgnoreCase(key[1])
+                    ? "raining cats and dogs"
+                    : "showers"));
+  }
 }