JAL-1264 new unit tests
[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   Sequence 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.
92    */
93   @Test
94   public void testAddAlignmentAnnotation()
95   {
96     assertNull(seq.annotation);
97     final AlignmentAnnotation annotation = new AlignmentAnnotation("a",
98             "b", 2d);
99     assertNull(annotation.sequenceRef);
100     seq.addAlignmentAnnotation(annotation);
101     assertSame(seq, annotation.sequenceRef);
102     AlignmentAnnotation[] anns = seq.getAnnotation();
103     assertEquals(1, anns.length);
104     assertSame(annotation, anns[0]);
105   }
106 }