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.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
28 import jalview.gui.JvOptionPane;
30 import java.util.BitSet;
32 import org.junit.Assert;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
36 public class SearchResultsTest
39 @BeforeClass(alwaysRun = true)
40 public void setUpJvOptionPane()
42 JvOptionPane.setInteractiveMode(false);
43 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46 @Test(groups = { "Functional" })
47 public void testToString()
49 SequenceI seq = new Sequence("Seq1", "abcdefghijklm");
50 SearchResultsI sr = new SearchResults();
51 sr.addResult(seq, 1, 1);
52 assertEquals("[Seq1/1-1]", sr.toString());
53 sr.addResult(seq, 3, 5);
54 assertEquals("[Seq1/1-1, Seq1/3-5]", sr.toString());
56 seq = new Sequence("Seq2", "pqrstuvwxy");
57 sr.addResult(seq, 6, 7);
58 assertEquals("[Seq1/1-1, Seq1/3-5, Seq2/6-7]", sr.toString());
61 @Test(groups = { "Functional" })
62 public void testEquals()
64 SequenceI seq1 = new Sequence("", "abcdefghijklm");
65 SearchResultsI sr1 = new SearchResults();
66 SearchResultsI sr2 = new SearchResults();
68 assertFalse(sr1.equals(null)); // null object
69 assertFalse(sr1.equals(seq1)); // wrong type
70 assertTrue(sr1.equals(sr1)); // self
71 assertTrue(sr1.equals(sr2)); // empty
72 assertTrue(sr2.equals(sr1)); // reflexive
75 * if only one result is not empty
77 sr1.addResult(seq1, 1, 1);
78 assertTrue(sr1.equals(sr1));
79 assertFalse(sr1.equals(sr2));
80 assertFalse(sr2.equals(sr1));
85 sr2.addResult(seq1, 1, 1);
86 assertTrue(sr1.equals(sr2));
87 assertTrue(sr2.equals(sr1));
90 * both have three matches
92 sr1.addResult(seq1, 3, 4);
93 sr1.addResult(seq1, 6, 8);
94 sr2.addResult(seq1, 3, 4);
95 sr2.addResult(seq1, 6, 8);
96 assertTrue(sr1.equals(sr1));
97 assertTrue(sr2.equals(sr2));
98 assertTrue(sr1.equals(sr2));
99 assertTrue(sr2.equals(sr1));
103 * Matches that are similar but for distinct sequences are not equal
105 @Test(groups = { "Functional" })
106 public void testEquals_distinctSequences()
108 SequenceI seq1 = new Sequence("", "abcdefghijklm");
109 SequenceI seq2 = new Sequence("", "abcdefghijklm");
110 SearchResultsI sr1 = new SearchResults();
111 SearchResultsI sr2 = new SearchResults();
113 sr1.addResult(seq1, 1, 1);
114 sr2.addResult(seq2, 1, 1);
115 assertFalse(sr1.equals(sr2));
116 assertFalse(sr2.equals(sr1));
120 * Matches that are the same except for ordering are not equal
122 @Test(groups = { "Functional" })
123 public void testEquals_orderDiffers()
125 SequenceI seq1 = new Sequence("", "abcdefghijklm");
126 SearchResultsI sr1 = new SearchResults();
127 SearchResultsI sr2 = new SearchResults();
129 sr1.addResult(seq1, 1, 1);
130 sr1.addResult(seq1, 2, 2);
131 sr2.addResult(seq1, 2, 2);
132 sr2.addResult(seq1, 1, 1);
133 assertFalse(sr1.equals(sr2));
134 assertFalse(sr2.equals(sr1));
138 * Verify that hashCode matches for equal objects
140 @Test(groups = { "Functional" })
141 public void testHashcode()
143 SequenceI seq1 = new Sequence("", "abcdefghijklm");
144 SearchResultsI sr1 = new SearchResults();
145 SearchResultsI sr2 = new SearchResults();
150 assertEquals(sr1.hashCode(), sr2.hashCode());
155 sr1.addResult(seq1, 1, 1);
156 sr2.addResult(seq1, 1, 1);
157 assertEquals(sr1.hashCode(), sr2.hashCode());
162 sr1.addResult(seq1, 3, 4);
163 sr1.addResult(seq1, 6, 8);
164 sr2.addResult(seq1, 3, 4);
165 sr2.addResult(seq1, 6, 8);
166 assertEquals(sr1.hashCode(), sr2.hashCode());
170 * Verify that SearchResults$Match constructor normalises start/end to the
171 * 'forwards' direction
173 @Test(groups = { "Functional" })
174 public void testMatchConstructor()
176 SequenceI seq1 = new Sequence("", "abcdefghijklm");
177 SearchResultMatchI m = new SearchResults().new Match(seq1, 2, 5);
178 assertSame(seq1, m.getSequence());
179 assertEquals(2, m.getStart());
180 assertEquals(5, m.getEnd());
182 // now a reverse mapping:
183 m = new SearchResults().new Match(seq1, 5, 2);
184 assertSame(seq1, m.getSequence());
185 assertEquals(2, m.getStart());
186 assertEquals(5, m.getEnd());
190 * test markColumns for creating column selections
192 @Test(groups = { "Functional" })
193 public void testMarkColumns()
196 SequenceI seq1 = new Sequence("", "abcdefghijklm");
197 SequenceI seq2 = new Sequence("", "abcdefghijklm");
198 SequenceGroup s1g=new SequenceGroup(), s2g=new SequenceGroup(), sallg=new SequenceGroup();
199 s1g.addSequence(seq1, false);
200 s2g.addSequence(seq2, false);
201 sallg.addSequence(seq1, false);
202 sallg.addSequence(seq2, false);
204 SearchResultsI sr = new SearchResults();
205 BitSet bs = new BitSet();
207 SearchResultMatchI srm = null;
208 srm = sr.addResult(seq1, 1, 1);
209 Assert.assertNotNull("addResult didn't return Match", srm);
210 srm = sr.addResult(seq2, 1, 2);
211 assertEquals("Sequence reference not set", seq2, srm.getSequence());
212 assertEquals("match start incorrect", 1, srm.getStart());
213 assertEquals("match end incorrect", 2, srm.getEnd());
215 // set start/end range for groups to cover matches
221 sallg.setStartRes(0);
227 marked = sr.markColumns(s1g, bs);
228 // check the bitset cardinality before checking the return value
229 assertEquals("Didn't mark expected number", 1, bs.cardinality());
230 assertEquals("Didn't return count of number of bits marked", 1, marked);
231 assertTrue("Didn't mark expected position", bs.get(0));
232 // now check return value for marking the same again
234 "Didn't count number of bits marked for existing marked set",
236 sr.markColumns(s1g, bs));
242 marked = sr.markColumns(s2g, bs);
243 assertEquals("Didn't mark expected number", 2, bs.cardinality());
244 assertEquals("Didn't return count of number of bits marked", 2, marked);
245 assertTrue("Didn't mark expected position (1)", bs.get(0));
246 assertTrue("Didn't mark expected position (2)", bs.get(1));
250 * should be same as seq2
252 BitSet allbs = new BitSet();
253 assertEquals(2, sr.markColumns(sallg, allbs));
254 assertEquals(bs, allbs);
256 // now check range selection
259 * limit s2g to just the second column, sallg to the first column
264 BitSet tbs = new BitSet();
265 assertEquals("Group start/end didn't select columns to mark",1, sr.markColumns(s2g, tbs));
266 assertEquals("Group start/end didn't select columns to mark", 1, sr.markColumns(sallg, tbs));
268 "Didn't set expected number of columns in total for two successive marks",
269 2, tbs.cardinality());