d9a45737b0c9d7e8f17aa9b48028ed8becfa12f6
[jalview.git] / test / jalview / datamodel / SequenceTest.java
1 package jalview.datamodel;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
7
8 import java.util.List;
9
10 import org.junit.Before;
11 import org.junit.Test;
12
13 public class SequenceTest
14 {
15   SequenceI seq;
16
17   @Before
18   public void setUp()
19   {
20     seq = new Sequence("FER1", "AKPNGVL");
21   }
22   @Test
23   public void testGetAnnotation()
24   {
25     // initial state returns null not an empty array
26     assertNull(seq.getAnnotation());
27     AlignmentAnnotation ann = addAnnotation("label1", "desc1", "calcId1",
28             1f);
29     AlignmentAnnotation[] anns = seq.getAnnotation();
30     assertEquals(1, anns.length);
31     assertSame(ann, anns[0]);
32
33     // removing all annotations reverts array to null
34     seq.removeAlignmentAnnotation(ann);
35     assertNull(seq.getAnnotation());
36   }
37
38   @Test
39   public void testGetAnnotation_forLabel()
40   {
41     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1", 1f);
42     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2", 1f);
43     AlignmentAnnotation ann3 = addAnnotation("label1", "desc3", "calcId3", 1f);
44     AlignmentAnnotation[] anns = seq.getAnnotation("label1");
45     assertEquals(2, anns.length);
46     assertSame(ann1, anns[0]);
47     assertSame(ann3, anns[1]);
48   }
49
50   private AlignmentAnnotation addAnnotation(String label,
51           String description, String calcId,
52           float value)
53   {
54     final AlignmentAnnotation annotation = new AlignmentAnnotation(label, description,
55             value);
56     annotation.setCalcId(calcId);
57     seq.addAlignmentAnnotation(annotation);
58     return annotation;
59   }
60
61   @Test
62   public void testGetAlignmentAnnotations_forCalcIdAndLabel()
63   {
64     AlignmentAnnotation ann1 = addAnnotation("label1", "desc1", "calcId1",
65             1f);
66     AlignmentAnnotation ann2 = addAnnotation("label2", "desc2", "calcId2",
67             1f);
68     AlignmentAnnotation ann3 = addAnnotation("label2", "desc3", "calcId3",
69             1f);
70     AlignmentAnnotation ann4 = addAnnotation("label2", "desc3", "calcId2",
71             1f);
72     AlignmentAnnotation ann5 = addAnnotation("label5", "desc3", null,
73             1f);
74     AlignmentAnnotation ann6 = addAnnotation(null, "desc3", "calcId3",
75             1f);
76     List<AlignmentAnnotation> anns = seq.getAlignmentAnnotations("calcId2",
77             "label2");
78     assertEquals(2, anns.size());
79     assertSame(ann2, anns.get(0));
80     assertSame(ann4, anns.get(1));
81     
82     assertTrue(seq.getAlignmentAnnotations("calcId2", "label3").isEmpty());
83     assertTrue(seq.getAlignmentAnnotations("calcId3", "label5").isEmpty());
84     assertTrue(seq.getAlignmentAnnotations("calcId2", null).isEmpty());
85     assertTrue(seq.getAlignmentAnnotations(null, "label3").isEmpty());
86     assertTrue(seq.getAlignmentAnnotations(null, null).isEmpty());
87   }
88
89   /**
90    * Tests for addAlignmentAnnotation. Note this method has the side-effect of
91    * setting the sequenceRef on the annotation. Adding the same annotation twice
92    * should be ignored.
93    */
94   @Test
95   public void testAddAlignmentAnnotation()
96   {
97     assertNull(seq.getAnnotation());
98     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
99             "b", 2d);
100     assertNull(annotation.sequenceRef);
101     seq.addAlignmentAnnotation(annotation);
102     assertSame(seq, annotation.sequenceRef);
103     AlignmentAnnotation[] anns = seq.getAnnotation();
104     assertEquals(1, anns.length);
105     assertSame(annotation, anns[0]);
106
107     // re-adding does nothing
108     seq.addAlignmentAnnotation(annotation);
109     anns = seq.getAnnotation();
110     assertEquals(1, anns.length);
111     assertSame(annotation, anns[0]);
112
113     // an identical but different annotation can be added
114     final AlignmentAnnotation annotation2 = new AlignmentAnnotation("a",
115             "b", 2d);
116     seq.addAlignmentAnnotation(annotation2);
117     anns = seq.getAnnotation();
118     assertEquals(2, anns.length);
119     assertSame(annotation, anns[0]);
120     assertSame(annotation2, anns[1]);
121
122   }
123 }