JAL-4313 Patch AnnotationsMatcher to match nulls
[jalview.git] / test / jalview / testutils / AnnotationsMatcherTest.java
1 package jalview.testutils;
2
3 import org.testng.annotations.DataProvider;
4 import org.testng.annotations.Test;
5 import jalview.datamodel.Annotation;
6
7 import static org.hamcrest.MatcherAssert.assertThat;
8
9 import java.awt.Color;
10 import java.util.*;
11
12 public class AnnotationsMatcherTest
13 {
14   @DataProvider
15   public Object[][] matchingAnnotationsLists()
16   {
17     return new Object[][] {
18         {
19             new Annotation[] {
20                 new Annotation("C", "", 'C', 0.0f),
21                 new Annotation("C", "", 'C', 0.1f),
22                 new Annotation("B", "", 'B', 0.2f) },
23             new Annotation[] {
24                 new Annotation("C", "", 'C', 0.0f),
25                 new Annotation("C", "", 'C', 0.1f),
26                 new Annotation("B", "", 'B', 0.2f) } },
27         {
28             new Annotation[] {
29                 new Annotation("X", "xxx", 'X', 1.3f),
30                 new Annotation("V", "vvv", 'V', 1.5f),
31                 new Annotation("B", "bbb", 'B', 2.5f) },
32             new Annotation[] {
33                 new Annotation("X", "xxx", 'X', 1.3f),
34                 new Annotation("V", "vvv", 'V', 1.5f),
35                 new Annotation("B", "bbb", 'B', 2.5f) } },
36         {
37             new Annotation[] {
38                 new Annotation("A", "", 'A', 0.2f, Color.RED),
39                 new Annotation("A", "", 'A', 0.3f, Color.GREEN),
40                 new Annotation("C", "", 'C', 0.4f, Color.YELLOW),
41                 new Annotation("C", "", 'C', 0.5f) },
42             new Annotation[] {
43                 new Annotation("A", "", 'A', 0.2f, Color.RED),
44                 new Annotation("A", "", 'A', 0.3f, Color.GREEN),
45                 new Annotation("C", "", 'C', 0.4f, Color.YELLOW),
46                 new Annotation("C", "", 'C', 0.5f) } },
47         {
48             new Annotation[] {
49                 Annotation.EMPTY_ANNOTATION, Annotation.EMPTY_ANNOTATION },
50             new Annotation[] {
51                 Annotation.EMPTY_ANNOTATION, Annotation.EMPTY_ANNOTATION } },
52         {
53             new Annotation[] { null, null, null, null },
54             new Annotation[] { null, null, null, null } } };
55   }
56
57   @Test(groups = { "Functional" }, dataProvider = "matchingAnnotationsLists")
58   public void testMatchesSafely_matchingAnnotations(Annotation[] template,
59       Annotation[] testdata)
60   {
61     assert new AnnotationsMatcher(Arrays.asList(template))
62         .matchesSafely(testdata);
63   }
64
65   @DataProvider
66   public Object[][] matchingAnnotations()
67   {
68     return new Object[][] {
69         {
70             new Annotation("A", "aaa", 'A', 0.1f),
71             new Annotation("A", "aaa", 'A', 0.1f) },
72         {
73             new Annotation("A", "abcdef", 'A', 0.1f, Color.RED),
74             new Annotation("A", "abcdef", 'A', 0.1f, Color.RED) },
75         {
76             new Annotation("", "xxxx", ' ', 0.0f),
77             new Annotation("", "xxxx", ' ', 0.0f) },
78         {
79             new Annotation("", "", ' ', 0.0f),
80             new Annotation("", "", ' ', 0.0f) },
81         {
82             new Annotation(null, null, ' ', 0.0f),
83             new Annotation(null, null, ' ', 0.0f) },
84         {
85             new Annotation(null, "", ' ', 0.0f),
86             new Annotation("", null, ' ', 0.0f) },
87         { new Annotation(0f), Annotation.EMPTY_ANNOTATION },
88         { new Annotation(1f), new Annotation("", "", ' ', 1f, null) }, };
89   }
90
91   @Test(groups = { "Functional" }, dataProvider = "matchingAnnotations")
92   public void testAnnotationsEqual_matchingAnnotations(Annotation first,
93       Annotation second)
94   {
95     assert AnnotationsMatcher.annotationsEqual(first, second);
96   }
97
98   @DataProvider
99   public Object[][] mismatchingAnnotations()
100   {
101     return new Object[][] {
102         {
103             new Annotation("A", "aaa", 'A', 1f),
104             new Annotation("A", "aaa", 'B', 1f) },
105         { new Annotation(1f), new Annotation(2f) },
106         { new Annotation(1f), new Annotation("A", "", 'A', 1f) } };
107   }
108
109   @Test(groups = { "Functional" }, dataProvider = "mismatchingAnnotations")
110   public void testAnnotationsEqual_mismatchingAnnotations(Annotation first,
111       Annotation second)
112   {
113     assert !AnnotationsMatcher.annotationsEqual(first, second);
114   }
115
116   @Test(groups = { "Functional" })
117   public void testAnnotationsEqual_nullsMatch()
118   {
119     assert AnnotationsMatcher.annotationsEqual(null, null);
120   }
121
122   @Test(groups = { "Functional" })
123   public void testAnnotationsEqual_nullNotMatchEmpty()
124   {
125     assert !AnnotationsMatcher
126         .annotationsEqual(null, Annotation.EMPTY_ANNOTATION);
127     assert !AnnotationsMatcher
128         .annotationsEqual(Annotation.EMPTY_ANNOTATION, null);
129   }
130
131   @Test(groups = { "Functional" })
132   public void testAnnotationsEqual_compareNullDisplayCharToEmpty()
133   {
134     assert AnnotationsMatcher
135         .annotationsEqual(new Annotation("", "description", 'A', 0.1f),
136             new Annotation(null, "description", 'A', 0.1f));
137     assert AnnotationsMatcher
138         .annotationsEqual(new Annotation(null, "description", 'A', 0.1f),
139             new Annotation("", "description", 'A', 0.1f));
140   }
141
142   @Test(groups = { "Functional" })
143   public void testAnnotationsEqual_compareNullDescriptionToEmpty()
144   {
145     assert AnnotationsMatcher
146         .annotationsEqual(new Annotation("A", null, 'A', 0.2f),
147             new Annotation("A", "", 'A', 0.2f));
148     assert AnnotationsMatcher
149         .annotationsEqual(new Annotation("A", "", 'A', 0.2f),
150             new Annotation("A", null, 'A', 0.2f));
151   }
152
153   @Test(groups = { "Functional" })
154   public void testAnnotationsEqual_compareNullDisplayCharToBlank()
155   {
156     assert !AnnotationsMatcher
157         .annotationsEqual(new Annotation(" ", "aaa", 'A', 0.03f),
158             new Annotation(null, "aaa", 'A', 0.03f));
159     assert !AnnotationsMatcher
160         .annotationsEqual(new Annotation(null, "aaa", 'A', 0.04f),
161             new Annotation(" ", "aaa", 'A', 0.04f));
162   }
163
164   @Test(groups = { "Functional" })
165   public void testAnnotationsEqual_compareNullDescriptionToBlank()
166   {
167     assert !AnnotationsMatcher
168         .annotationsEqual(new Annotation("A", null, 'A', 0.0f),
169             new Annotation("A", " ", 'A', 0.0f));
170     assert !AnnotationsMatcher
171         .annotationsEqual(new Annotation("A", " ", 'A', 0.01f),
172             new Annotation("A", null, 'A', 0.01f));
173   }
174 }