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 org.testng.annotations.Test;
31 public class SequenceFeatureTest
33 @Test(groups = { "Functional" })
34 public void testCopyConstructor()
36 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
38 sf1.setValue("STRAND", "+");
39 sf1.setValue("Note", "Testing");
40 Integer count = new Integer(7);
41 sf1.setValue("Count", count);
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"));
55 * Tests for retrieving a 'miscellaneous details' property value, with or
56 * without a supplied default
58 @Test(groups = { "Functional" })
59 public void testGetValue()
61 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
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));
72 * Tests the method that returns 1 / -1 / 0 for strand "+" / "-" / other
74 @Test(groups = { "Functional" })
75 public void testGetStrand()
77 SequenceFeature sf = new SequenceFeature("type", "desc", 22, 33, 12.5f,
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());
89 * Tests for equality, and that equal objects have the same hashCode
91 @Test(groups = { "Functional" })
92 public void testEqualsAndHashCode()
94 SequenceFeature sf1 = new SequenceFeature("type", "desc", 22, 33,
96 sf1.setValue("ID", "id");
97 sf1.setValue("Name", "name");
98 sf1.setValue("Parent", "parent");
101 SequenceFeature sf2 = new SequenceFeature("type", "desc", 22, 33,
103 sf2.setValue("ID", "id");
104 sf2.setValue("Name", "name");
105 sf2.setValue("Parent", "parent");
109 assertFalse(sf1.equals(null));
110 assertTrue(sf1.equals(sf2));
111 assertTrue(sf2.equals(sf1));
112 assertEquals(sf1.hashCode(), sf2.hashCode());
114 // changing type breaks equals:
115 String restores = sf2.getType();
117 assertFalse(sf1.equals(sf2));
118 sf2.setType(restores);
120 // changing description breaks equals:
121 restores = sf2.getDescription();
122 sf2.setDescription("Desc");
123 assertFalse(sf1.equals(sf2));
124 sf2.setDescription(restores);
126 // changing score breaks equals:
127 float restoref = sf2.getScore();
129 assertFalse(sf1.equals(sf2));
130 sf2.setScore(restoref);
132 // NaN doesn't match a number
133 restoref = sf2.getScore();
134 sf2.setScore(Float.NaN);
135 assertFalse(sf1.equals(sf2));
138 sf1.setScore(Float.NaN);
139 assertTrue(sf1.equals(sf2));
140 sf1.setScore(restoref);
141 sf2.setScore(restoref);
143 // changing start position breaks equals:
144 int restorei = sf2.getBegin();
146 assertFalse(sf1.equals(sf2));
147 sf2.setBegin(restorei);
149 // changing end position breaks equals:
150 restorei = sf2.getEnd();
152 assertFalse(sf1.equals(sf2));
153 sf2.setEnd(restorei);
155 // changing feature group breaks equals:
156 restores = sf2.getFeatureGroup();
157 sf2.setFeatureGroup("Group");
158 assertFalse(sf1.equals(sf2));
159 sf2.setFeatureGroup(restores);
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);
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);
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);
179 // changing strand breaks equals:
180 restorei = sf2.getStrand();
182 assertFalse(sf1.equals(sf2));
183 sf2.setStrand(restorei == 1 ? "+" : "-");
185 // changing phase breaks equals:
186 restores = sf1.getPhase();
188 assertFalse(sf1.equals(sf2));
189 sf1.setPhase(restores);
191 // restore equality as sanity check:
192 assertTrue(sf1.equals(sf2));
193 assertTrue(sf2.equals(sf1));
194 assertEquals(sf1.hashCode(), sf2.hashCode());
196 // changing status doesn't change equals:
197 sf1.setStatus("new");
198 assertTrue(sf1.equals(sf2));