JAL-2327 add isContactFeature
[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     String restores = sf2.getType();
116     sf2.setType("Type");
117     assertFalse(sf1.equals(sf2));
118     sf2.setType(restores);
119
120     // changing description breaks equals:
121     restores = sf2.getDescription();
122     sf2.setDescription("Desc");
123     assertFalse(sf1.equals(sf2));
124     sf2.setDescription(restores);
125
126     // changing score breaks equals:
127     float restoref = sf2.getScore();
128     sf2.setScore(12.4f);
129     assertFalse(sf1.equals(sf2));
130     sf2.setScore(restoref);
131
132     // NaN doesn't match a number
133     restoref = sf2.getScore();
134     sf2.setScore(Float.NaN);
135     assertFalse(sf1.equals(sf2));
136
137     // NaN matches NaN
138     sf1.setScore(Float.NaN);
139     assertTrue(sf1.equals(sf2));
140     sf1.setScore(restoref);
141     sf2.setScore(restoref);
142
143     // changing start position breaks equals:
144     int restorei = sf2.getBegin();
145     sf2.setBegin(21);
146     assertFalse(sf1.equals(sf2));
147     sf2.setBegin(restorei);
148
149     // changing end position breaks equals:
150     restorei = sf2.getEnd();
151     sf2.setEnd(32);
152     assertFalse(sf1.equals(sf2));
153     sf2.setEnd(restorei);
154
155     // changing feature group breaks equals:
156     restores = sf2.getFeatureGroup();
157     sf2.setFeatureGroup("Group");
158     assertFalse(sf1.equals(sf2));
159     sf2.setFeatureGroup(restores);
160
161     // changing ID breaks equals:
162     restores = (String) sf2.getValue("ID");
163     sf2.setValue("ID", "id2");
164     assertFalse(sf1.equals(sf2));
165     sf2.setValue("ID", restores);
166
167     // changing Name breaks equals:
168     restores = (String) sf2.getValue("Name");
169     sf2.setValue("Name", "Name");
170     assertFalse(sf1.equals(sf2));
171     sf2.setValue("Name", restores);
172
173     // changing Parent breaks equals:
174     restores = (String) sf1.getValue("Parent");
175     sf1.setValue("Parent", "Parent");
176     assertFalse(sf1.equals(sf2));
177     sf1.setValue("Parent", restores);
178
179     // changing strand breaks equals:
180     restorei = sf2.getStrand();
181     sf2.setStrand("-");
182     assertFalse(sf1.equals(sf2));
183     sf2.setStrand(restorei == 1 ? "+" : "-");
184
185     // changing phase breaks equals:
186     restores = sf1.getPhase();
187     sf1.setPhase("2");
188     assertFalse(sf1.equals(sf2));
189     sf1.setPhase(restores);
190
191     // restore equality as sanity check:
192     assertTrue(sf1.equals(sf2));
193     assertTrue(sf2.equals(sf1));
194     assertEquals(sf1.hashCode(), sf2.hashCode());
195
196     // changing status doesn't change equals:
197     sf1.setStatus("new");
198     assertTrue(sf1.equals(sf2));
199   }
200
201   @Test(groups = { "Functional" })
202   public void testIsContactFeature()
203   {
204     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
205             "group");
206     assertFalse(sf.isContactFeature());
207     sf.setType("");
208     assertFalse(sf.isContactFeature());
209     sf.setType(null);
210     assertFalse(sf.isContactFeature());
211     sf.setType("Disulfide Bond");
212     assertTrue(sf.isContactFeature());
213     sf.setType("disulfide bond");
214     assertTrue(sf.isContactFeature());
215     sf.setType("Disulphide Bond");
216     assertTrue(sf.isContactFeature());
217     sf.setType("disulphide bond");
218     assertTrue(sf.isContactFeature());
219   }
220 }