2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
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;
29 import jalview.gui.JvOptionPane;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 public class SequenceFeatureTest
37 @BeforeClass(alwaysRun = true)
38 public void setUpJvOptionPane()
40 JvOptionPane.setInteractiveMode(false);
41 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44 @Test(groups = { "Functional" })
45 public void testCopyConstructor()
47 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
49 sf1.setValue("STRAND", "+");
50 sf1.setValue("Note", "Testing");
51 Integer count = new Integer(7);
52 sf1.setValue("Count", count);
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"));
66 * Tests for retrieving a 'miscellaneous details' property value, with or
67 * without a supplied default
69 @Test(groups = { "Functional" })
70 public void testGetValue()
72 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
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));
83 * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other
85 @Test(groups = { "Functional" })
86 public void testGetStrand()
88 SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
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());
100 * Tests for equality, and that equal objects have the same hashCode
102 @Test(groups = { "Functional" })
103 public void testEqualsAndHashCode()
105 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
107 sf1.setValue("ID", "id");
108 sf1.setValue("Name", "name");
109 sf1.setValue("Parent", "parent");
112 SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33,
114 sf2.setValue("ID", "id");
115 sf2.setValue("Name", "name");
116 sf2.setValue("Parent", "parent");
120 assertFalse(sf1.equals(null));
121 assertTrue(sf1.equals(sf2));
122 assertTrue(sf2.equals(sf1));
123 assertEquals(sf1.hashCode(), sf2.hashCode());
125 // changing type breaks equals:
126 String restores = sf2.getType();
128 assertFalse(sf1.equals(sf2));
129 sf2.setType(restores);
131 // changing description breaks equals:
132 restores = sf2.getDescription();
133 sf2.setDescription("Desc");
134 assertFalse(sf1.equals(sf2));
135 sf2.setDescription(restores);
137 // changing score breaks equals:
138 float restoref = sf2.getScore();
140 assertFalse(sf1.equals(sf2));
141 sf2.setScore(restoref);
143 // NaN doesn't match a number
144 restoref = sf2.getScore();
145 sf2.setScore(Float.NaN);
146 assertFalse(sf1.equals(sf2));
149 sf1.setScore(Float.NaN);
150 assertTrue(sf1.equals(sf2));
151 sf1.setScore(restoref);
152 sf2.setScore(restoref);
154 // changing start position breaks equals:
155 int restorei = sf2.getBegin();
157 assertFalse(sf1.equals(sf2));
158 sf2.setBegin(restorei);
160 // changing end position breaks equals:
161 restorei = sf2.getEnd();
163 assertFalse(sf1.equals(sf2));
164 sf2.setEnd(restorei);
166 // changing feature group breaks equals:
167 restores = sf2.getFeatureGroup();
168 sf2.setFeatureGroup("Group");
169 assertFalse(sf1.equals(sf2));
170 sf2.setFeatureGroup(restores);
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);
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);
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);
190 // changing strand breaks equals:
191 restorei = sf2.getStrand();
193 assertFalse(sf1.equals(sf2));
194 sf2.setStrand(restorei == 1 ? "+" : "-");
196 // changing phase breaks equals:
197 restores = sf1.getPhase();
199 assertFalse(sf1.equals(sf2));
200 sf1.setPhase(restores);
202 // restore equality as sanity check:
203 assertTrue(sf1.equals(sf2));
204 assertTrue(sf2.equals(sf1));
205 assertEquals(sf1.hashCode(), sf2.hashCode());
207 // changing status doesn't change equals:
208 sf1.setStatus("new");
209 assertTrue(sf1.equals(sf2));
212 @Test(groups = { "Functional" })
213 public void testIsContactFeature()
215 SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
217 assertFalse(sf.isContactFeature());
219 assertFalse(sf.isContactFeature());
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());