Merge branch 'features/JAL-2326_JOptionPane-refactoring' into develop
[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 jalview.gui.JvOptionPane;
30
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
33
34 public class SequenceFeatureTest
35 {
36
37   @BeforeClass(alwaysRun = true)
38   public void setUpJvOptionPane()
39   {
40     JvOptionPane.setInteractiveMode(false);
41     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42   }
43
44   @Test(groups = { "Functional" })
45   public void testCopyConstructor()
46   {
47     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
48             12.5f, "group");
49     sf1.setValue("STRAND", "+");
50     sf1.setValue("Note", "Testing");
51     Integer count = new Integer(7);
52     sf1.setValue("Count", count);
53
54     SequenceFeature sf2 = new SequenceFeature(sf1);
55     assertEquals("type", sf2.getType());
56     assertEquals("desc", sf2.getDescription());
57     assertEquals(22, sf2.getBegin());
58     assertEquals(33, sf2.getEnd());
59     assertEquals("+", sf2.getValue("STRAND"));
60     assertEquals("Testing", sf2.getValue("Note"));
61     // shallow clone of otherDetails map - contains the same object values!
62     assertSame(count, sf2.getValue("Count"));
63   }
64
65   /**
66    * Tests for retrieving a 'miscellaneous details' property value, with or
67    * without a supplied default
68    */
69   @Test(groups = { "Functional" })
70   public void testGetValue()
71   {
72     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
73             12.5f, "group");
74     sf1.setValue("STRAND", "+");
75     assertEquals("+", sf1.getValue("STRAND"));
76     assertNull(sf1.getValue("strand")); // case-sensitive
77     assertEquals(".", sf1.getValue("unknown", "."));
78     Integer i = new Integer(27);
79     assertSame(i, sf1.getValue("Unknown", i));
80   }
81
82   /**
83    * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other
84    */
85   @Test(groups = { "Functional" })
86   public void testGetStrand()
87   {
88     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
89             "group");
90     assertEquals(0, sf.getStrand());
91     sf.setValue("STRAND", "+");
92     assertEquals(1, sf.getStrand());
93     sf.setValue("STRAND", "-");
94     assertEquals(-1, sf.getStrand());
95     sf.setValue("STRAND", ".");
96     assertEquals(0, sf.getStrand());
97   }
98
99   /**
100    * Tests for equality, and that equal objects have the same hashCode
101    */
102   @Test(groups = { "Functional" })
103   public void testEqualsAndHashCode()
104   {
105     SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
106             12.5f, "group");
107     sf1.setValue("ID", "id");
108     sf1.setValue("Name", "name");
109     sf1.setValue("Parent", "parent");
110     sf1.setStrand("+");
111     sf1.setPhase("1");
112     SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33,
113             12.5f, "group");
114     sf2.setValue("ID", "id");
115     sf2.setValue("Name", "name");
116     sf2.setValue("Parent", "parent");
117     sf2.setStrand("+");
118     sf2.setPhase("1");
119
120     assertFalse(sf1.equals(null));
121     assertTrue(sf1.equals(sf2));
122     assertTrue(sf2.equals(sf1));
123     assertEquals(sf1.hashCode(), sf2.hashCode());
124
125     // changing type breaks equals:
126     String restores = sf2.getType();
127     sf2.setType("Type");
128     assertFalse(sf1.equals(sf2));
129     sf2.setType(restores);
130
131     // changing description breaks equals:
132     restores = sf2.getDescription();
133     sf2.setDescription("Desc");
134     assertFalse(sf1.equals(sf2));
135     sf2.setDescription(restores);
136
137     // changing score breaks equals:
138     float restoref = sf2.getScore();
139     sf2.setScore(12.4f);
140     assertFalse(sf1.equals(sf2));
141     sf2.setScore(restoref);
142
143     // NaN doesn't match a number
144     restoref = sf2.getScore();
145     sf2.setScore(Float.NaN);
146     assertFalse(sf1.equals(sf2));
147
148     // NaN matches NaN
149     sf1.setScore(Float.NaN);
150     assertTrue(sf1.equals(sf2));
151     sf1.setScore(restoref);
152     sf2.setScore(restoref);
153
154     // changing start position breaks equals:
155     int restorei = sf2.getBegin();
156     sf2.setBegin(21);
157     assertFalse(sf1.equals(sf2));
158     sf2.setBegin(restorei);
159
160     // changing end position breaks equals:
161     restorei = sf2.getEnd();
162     sf2.setEnd(32);
163     assertFalse(sf1.equals(sf2));
164     sf2.setEnd(restorei);
165
166     // changing feature group breaks equals:
167     restores = sf2.getFeatureGroup();
168     sf2.setFeatureGroup("Group");
169     assertFalse(sf1.equals(sf2));
170     sf2.setFeatureGroup(restores);
171
172     // changing ID breaks equals:
173     restores = (String) sf2.getValue("ID");
174     sf2.setValue("ID", "id2");
175     assertFalse(sf1.equals(sf2));
176     sf2.setValue("ID", restores);
177
178     // changing Name breaks equals:
179     restores = (String) sf2.getValue("Name");
180     sf2.setValue("Name", "Name");
181     assertFalse(sf1.equals(sf2));
182     sf2.setValue("Name", restores);
183
184     // changing Parent breaks equals:
185     restores = (String) sf1.getValue("Parent");
186     sf1.setValue("Parent", "Parent");
187     assertFalse(sf1.equals(sf2));
188     sf1.setValue("Parent", restores);
189
190     // changing strand breaks equals:
191     restorei = sf2.getStrand();
192     sf2.setStrand("-");
193     assertFalse(sf1.equals(sf2));
194     sf2.setStrand(restorei == 1 ? "+" : "-");
195
196     // changing phase breaks equals:
197     restores = sf1.getPhase();
198     sf1.setPhase("2");
199     assertFalse(sf1.equals(sf2));
200     sf1.setPhase(restores);
201
202     // restore equality as sanity check:
203     assertTrue(sf1.equals(sf2));
204     assertTrue(sf2.equals(sf1));
205     assertEquals(sf1.hashCode(), sf2.hashCode());
206
207     // changing status doesn't change equals:
208     sf1.setStatus("new");
209     assertTrue(sf1.equals(sf2));
210   }
211
212   @Test(groups = { "Functional" })
213   public void testIsContactFeature()
214   {
215     SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
216             "group");
217     assertFalse(sf.isContactFeature());
218     sf.setType("");
219     assertFalse(sf.isContactFeature());
220     sf.setType(null);
221     assertFalse(sf.isContactFeature());
222     sf.setType("Disulfide Bond");
223     assertTrue(sf.isContactFeature());
224     sf.setType("disulfide bond");
225     assertTrue(sf.isContactFeature());
226     sf.setType("Disulphide Bond");
227     assertTrue(sf.isContactFeature());
228     sf.setType("disulphide bond");
229     assertTrue(sf.isContactFeature());
230   }
231 }