JAL-2189 apply license
[jalview.git] / test / jalview / datamodel / SequenceFeatureTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import org.testng.annotations.Test;
30
31 public class SequenceFeatureTest
32 {
33   @Test(groups = { "Functional" })
34   public void testCopyConstructor()
35   {
36     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
37             12.5f, "group");
38     sf1.setValue("STRAND", "+");
39     sf1.setValue("Note", "Testing");
40     Integer count = new Integer(7);
41     sf1.setValue("Count", count);
42
43     SequenceFeature sf2 = new SequenceFeature(sf1);
44     assertEquals("type", sf2.getType());
45     assertEquals("desc", sf2.getDescription());
46     assertEquals(22, sf2.getBegin());
47     assertEquals(33, sf2.getEnd());
48     assertEquals("+", sf2.getValue("STRAND"));
49     assertEquals("Testing", sf2.getValue("Note"));
50     // shallow clone of otherDetails map - contains the same object values!
51     assertSame(count, sf2.getValue("Count"));
52   }
53
54   /**
55    * Tests for retrieving a 'miscellaneous details' property value, with or
56    * without a supplied default
57    */
58   @Test(groups = { "Functional" })
59   public void testGetValue()
60   {
61     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
62             12.5f, "group");
63     sf1.setValue("STRAND", "+");
64     assertEquals("+", sf1.getValue("STRAND"));
65     assertNull(sf1.getValue("strand")); // case-sensitive
66     assertEquals(".", sf1.getValue("unknown", "."));
67     Integer i = new Integer(27);
68     assertSame(i, sf1.getValue("Unknown", i));
69   }
70
71   /**
72    * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other
73    */
74   @Test(groups = { "Functional" })
75   public void testGetStrand()
76   {
77     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
78             "group");
79     assertEquals(0, sf.getStrand());
80     sf.setValue("STRAND", "+");
81     assertEquals(1, sf.getStrand());
82     sf.setValue("STRAND", "-");
83     assertEquals(-1, sf.getStrand());
84     sf.setValue("STRAND", ".");
85     assertEquals(0, sf.getStrand());
86   }
87
88   /**
89    * Tests for equality, and that equal objects have the same hashCode
90    */
91   @Test(groups = { "Functional" })
92   public void testEqualsAndHashCode()
93   {
94     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
95             12.5f, "group");
96     sf1.setValue("ID", "id");
97     sf1.setValue("Name", "name");
98     sf1.setValue("Parent", "parent");
99     sf1.setStrand("+");
100     sf1.setPhase("1");
101     SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33,
102             12.5f, "group");
103     sf2.setValue("ID", "id");
104     sf2.setValue("Name", "name");
105     sf2.setValue("Parent", "parent");
106     sf2.setStrand("+");
107     sf2.setPhase("1");
108
109     assertFalse(sf1.equals(null));
110     assertTrue(sf1.equals(sf2));
111     assertTrue(sf2.equals(sf1));
112     assertEquals(sf1.hashCode(), sf2.hashCode());
113
114     // changing type breaks equals:
115     sf2.setType("Type");
116     assertFalse(sf1.equals(sf2));
117
118     // changing description breaks equals:
119     sf2.setType("type");
120     sf2.setDescription("Desc");
121     assertFalse(sf1.equals(sf2));
122
123     // changing start position breaks equals:
124     sf2.setDescription("desc");
125     sf2.setBegin(21);
126     assertFalse(sf1.equals(sf2));
127
128     // changing end position breaks equals:
129     sf2.setBegin(22);
130     sf2.setEnd(32);
131     assertFalse(sf1.equals(sf2));
132
133     // changing feature group breaks equals:
134     sf2.setEnd(33);
135     sf2.setFeatureGroup("Group");
136     assertFalse(sf1.equals(sf2));
137
138     // changing ID breaks equals:
139     sf2.setFeatureGroup("group");
140     sf2.setValue("ID", "id2");
141     assertFalse(sf1.equals(sf2));
142
143     // changing Name breaks equals:
144     sf2.setValue("ID", "id");
145     sf2.setValue("Name", "Name");
146     assertFalse(sf1.equals(sf2));
147
148     // changing Parent breaks equals:
149     sf2.setValue("Name", "name");
150     sf1.setValue("Parent", "Parent");
151     assertFalse(sf1.equals(sf2));
152
153     // changing strand breaks equals:
154     sf1.setValue("Parent", "parent");
155     sf2.setStrand("-");
156     assertFalse(sf1.equals(sf2));
157
158     // changing phase breaks equals:
159     sf2.setStrand("+");
160     sf1.setPhase("2");
161     assertFalse(sf1.equals(sf2));
162
163     // restore equality as sanity check:
164     sf1.setPhase("1");
165     assertTrue(sf1.equals(sf2));
166     assertTrue(sf2.equals(sf1));
167     assertEquals(sf1.hashCode(), sf2.hashCode());
168
169     // changing status doesn't change equals:
170     sf1.setStatus("new");
171     assertTrue(sf1.equals(sf2));
172   }
173 }