Merge branch 'JAL-1601-direct-jpred4-rest-service' into development/Release_2_12_Branch
[jalview.git] / test / jalview / testutils / AnnotationsMatcherTest.java
diff --git a/test/jalview/testutils/AnnotationsMatcherTest.java b/test/jalview/testutils/AnnotationsMatcherTest.java
new file mode 100644 (file)
index 0000000..3fbdab6
--- /dev/null
@@ -0,0 +1,174 @@
+package jalview.testutils;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import jalview.datamodel.Annotation;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.awt.Color;
+import java.util.*;
+
+public class AnnotationsMatcherTest
+{
+  @DataProvider
+  public Object[][] matchingAnnotationsLists()
+  {
+    return new Object[][] {
+        {
+            new Annotation[] {
+                new Annotation("C", "", 'C', 0.0f),
+                new Annotation("C", "", 'C', 0.1f),
+                new Annotation("B", "", 'B', 0.2f) },
+            new Annotation[] {
+                new Annotation("C", "", 'C', 0.0f),
+                new Annotation("C", "", 'C', 0.1f),
+                new Annotation("B", "", 'B', 0.2f) } },
+        {
+            new Annotation[] {
+                new Annotation("X", "xxx", 'X', 1.3f),
+                new Annotation("V", "vvv", 'V', 1.5f),
+                new Annotation("B", "bbb", 'B', 2.5f) },
+            new Annotation[] {
+                new Annotation("X", "xxx", 'X', 1.3f),
+                new Annotation("V", "vvv", 'V', 1.5f),
+                new Annotation("B", "bbb", 'B', 2.5f) } },
+        {
+            new Annotation[] {
+                new Annotation("A", "", 'A', 0.2f, Color.RED),
+                new Annotation("A", "", 'A', 0.3f, Color.GREEN),
+                new Annotation("C", "", 'C', 0.4f, Color.YELLOW),
+                new Annotation("C", "", 'C', 0.5f) },
+            new Annotation[] {
+                new Annotation("A", "", 'A', 0.2f, Color.RED),
+                new Annotation("A", "", 'A', 0.3f, Color.GREEN),
+                new Annotation("C", "", 'C', 0.4f, Color.YELLOW),
+                new Annotation("C", "", 'C', 0.5f) } },
+        {
+            new Annotation[] {
+                Annotation.EMPTY_ANNOTATION, Annotation.EMPTY_ANNOTATION },
+            new Annotation[] {
+                Annotation.EMPTY_ANNOTATION, Annotation.EMPTY_ANNOTATION } },
+        {
+            new Annotation[] { null, null, null, null },
+            new Annotation[] { null, null, null, null } } };
+  }
+
+  @Test(groups = { "Functional" }, dataProvider = "matchingAnnotationsLists")
+  public void testMatchesSafely_matchingAnnotations(Annotation[] template,
+      Annotation[] testdata)
+  {
+    assert new AnnotationsMatcher(Arrays.asList(template))
+        .matchesSafely(testdata);
+  }
+
+  @DataProvider
+  public Object[][] matchingAnnotations()
+  {
+    return new Object[][] {
+        {
+            new Annotation("A", "aaa", 'A', 0.1f),
+            new Annotation("A", "aaa", 'A', 0.1f) },
+        {
+            new Annotation("A", "abcdef", 'A', 0.1f, Color.RED),
+            new Annotation("A", "abcdef", 'A', 0.1f, Color.RED) },
+        {
+            new Annotation("", "xxxx", ' ', 0.0f),
+            new Annotation("", "xxxx", ' ', 0.0f) },
+        {
+            new Annotation("", "", ' ', 0.0f),
+            new Annotation("", "", ' ', 0.0f) },
+        {
+            new Annotation(null, null, ' ', 0.0f),
+            new Annotation(null, null, ' ', 0.0f) },
+        {
+            new Annotation(null, "", ' ', 0.0f),
+            new Annotation("", null, ' ', 0.0f) },
+        { new Annotation(0f), Annotation.EMPTY_ANNOTATION },
+        { new Annotation(1f), new Annotation("", "", ' ', 1f, null) }, };
+  }
+
+  @Test(groups = { "Functional" }, dataProvider = "matchingAnnotations")
+  public void testAnnotationsEqual_matchingAnnotations(Annotation first,
+      Annotation second)
+  {
+    assert AnnotationsMatcher.annotationsEqual(first, second);
+  }
+
+  @DataProvider
+  public Object[][] mismatchingAnnotations()
+  {
+    return new Object[][] {
+        {
+            new Annotation("A", "aaa", 'A', 1f),
+            new Annotation("A", "aaa", 'B', 1f) },
+        { new Annotation(1f), new Annotation(2f) },
+        { new Annotation(1f), new Annotation("A", "", 'A', 1f) } };
+  }
+
+  @Test(groups = { "Functional" }, dataProvider = "mismatchingAnnotations")
+  public void testAnnotationsEqual_mismatchingAnnotations(Annotation first,
+      Annotation second)
+  {
+    assert !AnnotationsMatcher.annotationsEqual(first, second);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_nullsMatch()
+  {
+    assert AnnotationsMatcher.annotationsEqual(null, null);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_nullNotMatchEmpty()
+  {
+    assert !AnnotationsMatcher
+        .annotationsEqual(null, Annotation.EMPTY_ANNOTATION);
+    assert !AnnotationsMatcher
+        .annotationsEqual(Annotation.EMPTY_ANNOTATION, null);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_compareNullDisplayCharToEmpty()
+  {
+    assert AnnotationsMatcher
+        .annotationsEqual(new Annotation("", "description", 'A', 0.1f),
+            new Annotation(null, "description", 'A', 0.1f));
+    assert AnnotationsMatcher
+        .annotationsEqual(new Annotation(null, "description", 'A', 0.1f),
+            new Annotation("", "description", 'A', 0.1f));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_compareNullDescriptionToEmpty()
+  {
+    assert AnnotationsMatcher
+        .annotationsEqual(new Annotation("A", null, 'A', 0.2f),
+            new Annotation("A", "", 'A', 0.2f));
+    assert AnnotationsMatcher
+        .annotationsEqual(new Annotation("A", "", 'A', 0.2f),
+            new Annotation("A", null, 'A', 0.2f));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_compareNullDisplayCharToBlank()
+  {
+    assert !AnnotationsMatcher
+        .annotationsEqual(new Annotation(" ", "aaa", 'A', 0.03f),
+            new Annotation(null, "aaa", 'A', 0.03f));
+    assert !AnnotationsMatcher
+        .annotationsEqual(new Annotation(null, "aaa", 'A', 0.04f),
+            new Annotation(" ", "aaa", 'A', 0.04f));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testAnnotationsEqual_compareNullDescriptionToBlank()
+  {
+    assert !AnnotationsMatcher
+        .annotationsEqual(new Annotation("A", null, 'A', 0.0f),
+            new Annotation("A", " ", 'A', 0.0f));
+    assert !AnnotationsMatcher
+        .annotationsEqual(new Annotation("A", " ", 'A', 0.01f),
+            new Annotation("A", null, 'A', 0.01f));
+  }
+}