X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=test%2Fjalview%2Fdatamodel%2FColumnSelectionTest.java;h=4d3f6115cf590ad151e75f90d26ac802d2897e62;hb=9a20339fe0483d2757c6c6b11b60e7332f828811;hp=5e10337c086286638387e3d8967ffad86ed20169;hpb=3412b273e964fb1a9d22564b04a5f0c827ec2461;p=jalview.git
diff --git a/test/jalview/datamodel/ColumnSelectionTest.java b/test/jalview/datamodel/ColumnSelectionTest.java
index 5e10337..4d3f611 100644
--- a/test/jalview/datamodel/ColumnSelectionTest.java
+++ b/test/jalview/datamodel/ColumnSelectionTest.java
@@ -1,29 +1,69 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.datamodel;
import static org.testng.AssertJUnit.assertEquals;
-import org.testng.annotations.Test;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertSame;
+import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.AssertJUnit.fail;
+
+import jalview.gui.JvOptionPane;
+
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collections;
+import java.util.ConcurrentModificationException;
import java.util.List;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
public class ColumnSelectionTest
{
- @Test
+ @BeforeClass(alwaysRun = true)
+ public void setUpJvOptionPane()
+ {
+ JvOptionPane.setInteractiveMode(false);
+ JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+ }
+
+ @Test(groups = { "Functional" })
public void testAddElement()
{
ColumnSelection cs = new ColumnSelection();
cs.addElement(2);
cs.addElement(5);
+ cs.addElement(3);
+ cs.addElement(5); // ignored
List sel = cs.getSelected();
- assertEquals(2, sel.size());
- assertEquals(new Integer(2), sel.get(0));
- assertEquals(new Integer(5), sel.get(1));
+ assertEquals("[2, 5, 3]", sel.toString());
}
/**
* Test the remove method - in particular to verify that remove(int i) removes
* the element whose value is i, _NOT_ the i'th element.
*/
- @Test
+ @Test(groups = { "Functional" })
public void testRemoveElement()
{
ColumnSelection cs = new ColumnSelection();
@@ -40,6 +80,9 @@ public class ColumnSelectionTest
// removing an element in the list removes it
cs.removeElement(2);
+ // ...and also from the read-only view
+ assertEquals(1, sel.size());
+ sel = cs.getSelected();
assertEquals(1, sel.size());
assertEquals(new Integer(5), sel.get(0));
}
@@ -48,7 +91,7 @@ public class ColumnSelectionTest
* Test the method that finds the visible column position of an alignment
* column, allowing for hidden columns.
*/
- @Test
+ @Test(groups = { "Functional" })
public void testFindColumnPosition()
{
ColumnSelection cs = new ColumnSelection();
@@ -58,16 +101,869 @@ public class ColumnSelectionTest
cs.hideColumns(6, 6);
assertEquals(5, cs.findColumnPosition(5));
- // hiding column 5 makes no difference
- cs.hideColumns(5, 5);
- assertEquals(5, cs.findColumnPosition(5));
-
// hiding column 4 moves column 5 to column 4
cs.hideColumns(4, 4);
assertEquals(4, cs.findColumnPosition(5));
+ // hiding column 4 moves column 4 to position 3
+ assertEquals(3, cs.findColumnPosition(4));
+
// hiding columns 1 and 2 moves column 5 to column 2
cs.hideColumns(1, 2);
assertEquals(2, cs.findColumnPosition(5));
+
+ // check with > 1 hidden column regions
+ // where some columns are in the hidden regions
+ ColumnSelection cs2 = new ColumnSelection();
+ cs2.hideColumns(5, 10);
+ cs2.hideColumns(20, 27);
+ cs2.hideColumns(40, 44);
+
+ // hiding columns 5-10 and 20-27 moves column 8 to column 4
+ assertEquals(4, cs2.findColumnPosition(8));
+
+ // and moves column 24 to 13
+ assertEquals(13, cs2.findColumnPosition(24));
+
+ // and moves column 28 to 14
+ assertEquals(14, cs2.findColumnPosition(28));
+
+ // and moves column 40 to 25
+ assertEquals(25, cs2.findColumnPosition(40));
+
+ // check when hidden columns start at 0 that the visible column
+ // is returned as 0
+ ColumnSelection cs3 = new ColumnSelection();
+ cs3.hideColumns(0, 4);
+ assertEquals(0, cs3.findColumnPosition(2));
+
+ }
+
+ /**
+ * Test the method that finds the visible column position a given distance
+ * before another column
+ */
+ @Test(groups = { "Functional" })
+ public void testFindColumnNToLeft()
+ {
+ ColumnSelection cs = new ColumnSelection();
+
+ // test that without hidden columns, findColumnNToLeft returns
+ // position n to left of provided position
+ int pos = cs.subtractVisibleColumns(3, 10);
+ assertEquals(7, pos);
+
+ // 0 returns same position
+ pos = cs.subtractVisibleColumns(0, 10);
+ assertEquals(10, pos);
+
+ // overflow to left returns negative number
+ pos = cs.subtractVisibleColumns(3, 0);
+ assertEquals(-3, pos);
+
+ // test that with hidden columns to left of result column
+ // behaviour is the same as above
+ cs.hideColumns(1, 3);
+
+ // position n to left of provided position
+ pos = cs.subtractVisibleColumns(3, 10);
+ assertEquals(7, pos);
+
+ // 0 returns same position
+ pos = cs.subtractVisibleColumns(0, 10);
+ assertEquals(10, pos);
+
+ // test with one set of hidden columns between start and required position
+ cs.hideColumns(12, 15);
+ pos = cs.subtractVisibleColumns(8, 17);
+ assertEquals(5, pos);
+
+ // test with two sets of hidden columns between start and required position
+ cs.hideColumns(20, 21);
+ pos = cs.subtractVisibleColumns(8, 23);
+ assertEquals(9, pos);
+
+ // repeat last 2 tests with no hidden columns to left of required position
+ cs.revealAllHiddenColumns();
+
+ // test with one set of hidden columns between start and required position
+ cs.hideColumns(12, 15);
+ pos = cs.subtractVisibleColumns(8, 17);
+ assertEquals(5, pos);
+
+ // test with two sets of hidden columns between start and required position
+ cs.hideColumns(20, 21);
+ pos = cs.subtractVisibleColumns(8, 23);
+ assertEquals(9, pos);
+
+ }
+
+ /**
+ * Test the code used to locate the reference sequence ruler origin
+ */
+ @Test(groups = { "Functional" })
+ public void testLocateVisibleBoundsofSequence()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
+ assertEquals(2, seq.findIndex(seq.getStart()));
+
+ // no hidden columns
+ assertEquals(
+ Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
+ seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1 }),
+ Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
+
+ // hidden column on gap after end of sequence - should not affect bounds
+ cs.hideColumns(13);
+ assertEquals(
+ Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
+ seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1 }),
+ Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
+
+ cs.revealAllHiddenColumns();
+ // hidden column on gap before beginning of sequence - should vis bounds by
+ // one
+ cs.hideColumns(0);
+ assertEquals(
+ Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 2,
+ seq.findIndex(seq.getEnd()) - 2, seq.getStart(),
+ seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1 }),
+ Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
+
+ cs.revealAllHiddenColumns();
+ // hide columns around most of sequence - leave one residue remaining
+ cs.hideColumns(1, 3);
+ cs.hideColumns(6, 11);
+ assertEquals("-D",
+ cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
+ assertEquals(
+ Arrays.toString(new int[] { 1, 1, 3, 3,
+ seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1 }),
+ Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
+ cs.revealAllHiddenColumns();
+
+ // hide whole sequence - should just get location of hidden region
+ // containing sequence
+ cs.hideColumns(1, 11);
+ assertEquals(
+ Arrays.toString(new int[] { 0, 1, 0, 0,
+ seq.findIndex(seq.getStart()) - 1,
+ seq.findIndex(seq.getEnd()) - 1 }),
+ Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
+
+ }
+
+ @Test(groups = { "Functional" })
+ public void testLocateVisibleBoundsPathologicals()
+ {
+ // test some pathological cases we missed
+ AlignmentI al = new Alignment(new SequenceI[] { new Sequence(
+ "refseqGaptest", "KTDVTI----------NFI-----G----L") });
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideInsertionsFor(al.getSequenceAt(0));
+ assertEquals(
+ "G",
+ ""
+ + al.getSequenceAt(0).getCharAt(
+ cs.adjustForHiddenColumns(9)));
+
+ }
+
+ @Test(groups = { "Functional" })
+ public void testHideColumns()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(5);
+ List hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[5, 5]", Arrays.toString(hidden.get(0)));
+
+ cs.hideColumns(3);
+ assertEquals(2, hidden.size());
+ // two hidden ranges, in order:
+ assertSame(hidden, cs.getHiddenColumns());
+ assertEquals("[3, 3]", Arrays.toString(hidden.get(0)));
+ assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
+
+ // hiding column 4 expands [3, 3] to [3, 4]
+ // and merges to [5, 5] to make [3, 5]
+ cs.hideColumns(4);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[3, 5]", Arrays.toString(hidden.get(0)));
+
+ // clear hidden columns (note they are added to selected)
+ cs.revealAllHiddenColumns();
+ // it is now actually null but getter returns an empty list
+ assertTrue(cs.getHiddenColumns().isEmpty());
+
+ cs.hideColumns(3, 6);
+ hidden = cs.getHiddenColumns();
+ int[] firstHiddenRange = hidden.get(0);
+ assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
+
+ // adding a subrange of already hidden should do nothing
+ cs.hideColumns(4, 5);
+ assertEquals(1, hidden.size());
+ assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
+ cs.hideColumns(3, 5);
+ assertEquals(1, hidden.size());
+ assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
+ cs.hideColumns(4, 6);
+ assertEquals(1, hidden.size());
+ assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
+ cs.hideColumns(3, 6);
+ assertEquals(1, hidden.size());
+ assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
+
+ cs.revealAllHiddenColumns();
+ cs.hideColumns(2, 4);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
+
+ // extend contiguous with 2 positions overlap
+ cs.hideColumns(3, 5);
+ assertEquals(1, hidden.size());
+ assertEquals("[2, 5]", Arrays.toString(hidden.get(0)));
+
+ // extend contiguous with 1 position overlap
+ cs.hideColumns(5, 6);
+ assertEquals(1, hidden.size());
+ assertEquals("[2, 6]", Arrays.toString(hidden.get(0)));
+
+ // extend contiguous with overlap both ends:
+ cs.hideColumns(1, 7);
+ assertEquals(1, hidden.size());
+ assertEquals("[1, 7]", Arrays.toString(hidden.get(0)));
+ }
+
+ /**
+ * Test the method that hides a specified column including any adjacent
+ * selected columns. This is a convenience method for the case where multiple
+ * column regions are selected and then hidden using menu option View | Hide |
+ * Selected Columns.
+ */
+ @Test(groups = { "Functional" })
+ public void testHideColumns_withSelection()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ // select columns 4-6
+ cs.addElement(4);
+ cs.addElement(5);
+ cs.addElement(6);
+ // hide column 5 (and adjacent):
+ cs.hideColumns(5);
+ // 4,5,6 now hidden:
+ List hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
+ // none now selected:
+ assertTrue(cs.getSelected().isEmpty());
+
+ // repeat, hiding column 4 (5 and 6)
+ cs = new ColumnSelection();
+ cs.addElement(4);
+ cs.addElement(5);
+ cs.addElement(6);
+ cs.hideColumns(4);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
+ assertTrue(cs.getSelected().isEmpty());
+
+ // repeat, hiding column (4, 5 and) 6
+ cs = new ColumnSelection();
+ cs.addElement(4);
+ cs.addElement(5);
+ cs.addElement(6);
+ cs.hideColumns(6);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
+ assertTrue(cs.getSelected().isEmpty());
+
+ // repeat, with _only_ adjacent columns selected
+ cs = new ColumnSelection();
+ cs.addElement(4);
+ cs.addElement(6);
+ cs.hideColumns(5);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
+ assertTrue(cs.getSelected().isEmpty());
+ }
+
+ /**
+ * Test the method that hides all (possibly disjoint) selected column ranges
+ */
+ @Test(groups = { "Functional" })
+ public void testHideSelectedColumns()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ int[] sel = { 2, 3, 4, 7, 8, 9, 20, 21, 22 };
+ for (int col : sel)
+ {
+ cs.addElement(col);
+ }
+ cs.hideColumns(15, 18);
+
+ cs.hideSelectedColumns();
+ assertTrue(cs.getSelected().isEmpty());
+ List hidden = cs.getHiddenColumns();
+ assertEquals(4, hidden.size());
+ assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
+ assertEquals("[7, 9]", Arrays.toString(hidden.get(1)));
+ assertEquals("[15, 18]", Arrays.toString(hidden.get(2)));
+ assertEquals("[20, 22]", Arrays.toString(hidden.get(3)));
+ }
+
+ /**
+ * Test the method that gets runs of selected columns ordered by column. If
+ * this fails, HideSelectedColumns may also fail
+ */
+ @Test(groups = { "Functional" })
+ public void testGetSelectedRanges()
+ {
+ /*
+ * getSelectedRanges returns ordered columns regardless
+ * of the order in which they are added
+ */
+ ColumnSelection cs = new ColumnSelection();
+ int[] sel = { 4, 3, 7, 21, 9, 20, 8, 22, 2 };
+ for (int col : sel)
+ {
+ cs.addElement(col);
+ }
+ List range;
+ range = cs.getSelectedRanges();
+ assertEquals(3, range.size());
+ assertEquals("[2, 4]", Arrays.toString(range.get(0)));
+ assertEquals("[7, 9]", Arrays.toString(range.get(1)));
+ assertEquals("[20, 22]", Arrays.toString(range.get(2)));
+ cs.addElement(0);
+ cs.addElement(1);
+ range = cs.getSelectedRanges();
+ assertEquals(3, range.size());
+ assertEquals("[0, 4]", Arrays.toString(range.get(0)));
+ }
+
+ /**
+ * Test the method that reveals a range of hidden columns given the start
+ * column of the range
+ */
+ @Test(groups = { "Functional" })
+ public void testRevealHiddenColumns()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(5, 8);
+ cs.addElement(10);
+ cs.revealHiddenColumns(5);
+ // hidden columns list now null but getter returns empty list:
+ assertTrue(cs.getHiddenColumns().isEmpty());
+ // revealed columns are marked as selected (added to selection):
+ assertEquals("[10, 5, 6, 7, 8]", cs.getSelected().toString());
+
+ // calling with a column other than the range start does nothing:
+ cs = new ColumnSelection();
+ cs.hideColumns(5, 8);
+ List hidden = cs.getHiddenColumns();
+ cs.revealHiddenColumns(6);
+ assertSame(hidden, cs.getHiddenColumns());
+ assertTrue(cs.getSelected().isEmpty());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testRevealAllHiddenColumns()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(5, 8);
+ cs.hideColumns(2, 3);
+ cs.addElement(11);
+ cs.addElement(1);
+ cs.revealAllHiddenColumns();
+
+ /*
+ * revealing hidden columns adds them (in order) to the (unordered)
+ * selection list
+ */
+ assertTrue(cs.getHiddenColumns().isEmpty());
+ assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", cs.getSelected().toString());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testIsVisible()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(2, 4);
+ cs.hideColumns(6, 7);
+ assertTrue(cs.isVisible(0));
+ assertTrue(cs.isVisible(-99));
+ assertTrue(cs.isVisible(1));
+ assertFalse(cs.isVisible(2));
+ assertFalse(cs.isVisible(3));
+ assertFalse(cs.isVisible(4));
+ assertTrue(cs.isVisible(5));
+ assertFalse(cs.isVisible(6));
+ assertFalse(cs.isVisible(7));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testGetVisibleContigs()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(3, 6);
+ cs.hideColumns(8, 9);
+ cs.hideColumns(12, 12);
+
+ // start position is inclusive, end position exclusive:
+ int[] visible = cs.getVisibleContigs(1, 13);
+ assertEquals("[1, 2, 7, 7, 10, 11]", Arrays.toString(visible));
+
+ visible = cs.getVisibleContigs(4, 14);
+ assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
+
+ visible = cs.getVisibleContigs(3, 10);
+ assertEquals("[7, 7]", Arrays.toString(visible));
+
+ visible = cs.getVisibleContigs(4, 6);
+ assertEquals("[]", Arrays.toString(visible));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testInvertColumnSelection()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(4);
+ cs.addElement(6);
+ cs.addElement(8);
+ cs.hideColumns(3, 3);
+ cs.hideColumns(6, 6);
+
+ // invert selection from start (inclusive) to end (exclusive)
+ // hidden columns are _not_ changed
+ cs.invertColumnSelection(2, 9);
+ assertEquals("[2, 5, 7]", cs.getSelected().toString());
+
+ cs.invertColumnSelection(1, 9);
+ assertEquals("[1, 4, 8]", cs.getSelected().toString());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testMaxColumnSelection()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(0);
+ cs.addElement(513);
+ cs.addElement(1);
+ assertEquals(513, cs.getMax());
+ cs.removeElement(513);
+ assertEquals(1, cs.getMax());
+ cs.removeElement(1);
+ assertEquals(0, cs.getMax());
+ cs.addElement(512);
+ cs.addElement(513);
+ assertEquals(513, cs.getMax());
+
+ }
+
+ @Test(groups = { "Functional" })
+ public void testMinColumnSelection()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(0);
+ cs.addElement(513);
+ cs.addElement(1);
+ assertEquals(0, cs.getMin());
+ cs.removeElement(0);
+ assertEquals(1, cs.getMin());
+ cs.addElement(0);
+ assertEquals(0, cs.getMin());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testEquals()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(0);
+ cs.addElement(513);
+ cs.addElement(1);
+ cs.hideColumns(3);
+ cs.hideColumns(7);
+ cs.hideColumns(5, 9);
+
+ // same selections added in a different order
+ ColumnSelection cs2 = new ColumnSelection();
+ cs2.addElement(1);
+ cs2.addElement(513);
+ cs2.addElement(0);
+
+ // with no hidden columns
+ assertFalse(cs.equals(cs2));
+ assertFalse(cs2.equals(cs));
+
+ // with hidden columns added in a different order
+ cs2.hideColumns(6, 9);
+ cs2.hideColumns(5, 8);
+ cs2.hideColumns(3);
+
+ assertTrue(cs.equals(cs2));
+ assertTrue(cs.equals(cs));
+ assertTrue(cs2.equals(cs));
+ assertTrue(cs2.equals(cs2));
+
+ cs2.addElement(12);
+ assertFalse(cs.equals(cs2));
+ assertFalse(cs2.equals(cs));
+
+ cs2.removeElement(12);
+ assertTrue(cs.equals(cs2));
+
+ cs2.hideColumns(88);
+ assertFalse(cs.equals(cs2));
+ /*
+ * unhiding a column adds it to selection!
+ */
+ cs2.revealHiddenColumns(88);
+ assertFalse(cs.equals(cs2));
+ cs.addElement(88);
+ assertTrue(cs.equals(cs2));
+ }
+
+ /**
+ * Test the method that returns selected columns, in the order in which they
+ * were added
+ */
+ @Test(groups = { "Functional" })
+ public void testGetSelected()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ int[] sel = { 4, 3, 7, 21 };
+ for (int col : sel)
+ {
+ cs.addElement(col);
+ }
+
+ List selected = cs.getSelected();
+ assertEquals(4, selected.size());
+ assertEquals("[4, 3, 7, 21]", selected.toString());
+
+ /*
+ * getSelected returns a read-only view of the list
+ * verify the view follows any changes in it
+ */
+ cs.removeElement(7);
+ cs.addElement(1);
+ cs.removeElement(4);
+ assertEquals("[3, 21, 1]", selected.toString());
+ }
+
+ /**
+ * Test to verify that the list returned by getSelection cannot be modified
+ */
+ @Test(groups = { "Functional" })
+ public void testGetSelected_isReadOnly()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(3);
+
+ List selected = cs.getSelected();
+ try
+ {
+ selected.clear();
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ try
+ {
+ selected.add(1);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ try
+ {
+ selected.remove(3);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ try
+ {
+ Collections.sort(selected);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ }
+
+ /**
+ * Test that demonstrates a ConcurrentModificationException is thrown if you
+ * change the selection while iterating over it
+ */
+ @Test(
+ groups = "Functional",
+ expectedExceptions = { ConcurrentModificationException.class })
+ public void testGetSelected_concurrentModification()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(0);
+ cs.addElement(1);
+ cs.addElement(2);
+
+ /*
+ * simulate changing the list under us (e.g. in a separate
+ * thread) while iterating over it -> ConcurrentModificationException
+ */
+ List selected = cs.getSelected();
+ for (Integer col : selected)
+ {
+ if (col.intValue() == 0)
+ {
+ cs.removeElement(1);
+ }
+ }
+ }
+
+ @Test(groups = "Functional")
+ public void testMarkColumns()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(5); // this will be cleared
+ BitSet toMark = new BitSet();
+ toMark.set(1);
+ toMark.set(3);
+ toMark.set(6);
+ toMark.set(9);
+
+ assertTrue(cs.markColumns(toMark, 3, 8, false, false, false));
+ List selected = cs.getSelected();
+ assertEquals(2, selected.size());
+ assertTrue(selected.contains(3));
+ assertTrue(selected.contains(6));
+ }
+
+ @Test(groups = "Functional")
+ public void testMarkColumns_extend()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(1);
+ cs.addElement(5);
+ BitSet toMark = new BitSet();
+ toMark.set(1);
+ toMark.set(3);
+ toMark.set(6);
+ toMark.set(9);
+
+ /*
+ * extending selection of {3, 6} should leave {1, 3, 5, 6} selected
+ */
+ assertTrue(cs.markColumns(toMark, 3, 8, false, true, false));
+ List selected = cs.getSelected();
+ assertEquals(4, selected.size());
+ assertTrue(selected.contains(1));
+ assertTrue(selected.contains(3));
+ assertTrue(selected.contains(5));
+ assertTrue(selected.contains(6));
+ }
+
+ @Test(groups = "Functional")
+ public void testMarkColumns_invert()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(5); // this will be cleared
+ BitSet toMark = new BitSet();
+ toMark.set(1);
+ toMark.set(3);
+ toMark.set(6);
+ toMark.set(9);
+
+ /*
+ * inverted selection of {3, 6} should select {4, 5, 7, 8}
+ */
+ assertTrue(cs.markColumns(toMark, 3, 8, true, false, false));
+ List selected = cs.getSelected();
+ assertEquals(4, selected.size());
+ assertTrue(selected.contains(4));
+ assertTrue(selected.contains(5));
+ assertTrue(selected.contains(7));
+ assertTrue(selected.contains(8));
+ }
+
+ @Test(groups = "Functional")
+ public void testMarkColumns_toggle()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(1); // outside change range
+ cs.addElement(3);
+ cs.addElement(4);
+ cs.addElement(10); // outside change range
+ BitSet toMark = new BitSet();
+ toMark.set(1);
+ toMark.set(3);
+ toMark.set(6);
+ toMark.set(9);
+
+ /*
+ * toggling state of {3, 6} should leave {1, 4, 6, 10} selected
+ */
+ assertTrue(cs.markColumns(toMark, 3, 8, false, false, true));
+ List selected = cs.getSelected();
+ assertEquals(4, selected.size());
+ assertTrue(selected.contains(1));
+ assertTrue(selected.contains(4));
+ assertTrue(selected.contains(6));
+ assertTrue(selected.contains(10));
+ }
+
+ @Test(groups = "Functional")
+ public void testCopyConstructor()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(3);
+ cs.addElement(1);
+ cs.hideColumns(10, 11);
+ cs.hideColumns(5, 7);
+ assertEquals("[5, 7]", Arrays.toString(cs.getHiddenColumns().get(0)));
+
+ ColumnSelection cs2 = new ColumnSelection(cs);
+ assertTrue(cs2.hasSelectedColumns());
+ assertTrue(cs2.hasHiddenColumns());
+ // order of column selection is preserved
+ assertEquals("[3, 1]", cs2.getSelected().toString());
+ assertEquals(2, cs2.getHiddenColumns().size());
+ // hidden columns are held in column order
+ assertEquals("[5, 7]", Arrays.toString(cs2.getHiddenColumns().get(0)));
+ assertEquals("[10, 11]", Arrays.toString(cs2.getHiddenColumns().get(1)));
+ }
+
+ /**
+ * Test for the case when a hidden range encloses more one already hidden
+ * range
+ */
+ @Test(groups = { "Functional" })
+ public void testHideColumns_subsumingHidden()
+ {
+ /*
+ * JAL-2370 bug scenario:
+ * two hidden ranges subsumed by a third
+ */
+ ColumnSelection cs = new ColumnSelection();
+ cs.hideColumns(49, 59);
+ cs.hideColumns(69, 79);
+ List hidden = cs.getHiddenColumns();
+ assertEquals(2, hidden.size());
+ assertEquals("[49, 59]", Arrays.toString(hidden.get(0)));
+ assertEquals("[69, 79]", Arrays.toString(hidden.get(1)));
+
+ cs.hideColumns(48, 80);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
+
+ /*
+ * another...joining hidden ranges
+ */
+ cs = new ColumnSelection();
+ cs.hideColumns(10, 20);
+ cs.hideColumns(30, 40);
+ cs.hideColumns(50, 60);
+ // hiding 21-49 should merge to one range
+ cs.hideColumns(21, 49);
+ hidden = cs.getHiddenColumns();
+ assertEquals(1, hidden.size());
+ assertEquals("[10, 60]", Arrays.toString(hidden.get(0)));
+
+ /*
+ * another...lef overlap, subsumption, right overlap,
+ * no overlap of existing hidden ranges
+ */
+ cs = new ColumnSelection();
+ cs.hideColumns(10, 20);
+ cs.hideColumns(10, 20);
+ cs.hideColumns(30, 35);
+ cs.hideColumns(40, 50);
+ cs.hideColumns(60, 70);
+
+ cs.hideColumns(15, 45);
+ hidden = cs.getHiddenColumns();
+ assertEquals(2, hidden.size());
+ assertEquals("[10, 50]", Arrays.toString(hidden.get(0)));
+ assertEquals("[60, 70]", Arrays.toString(hidden.get(1)));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testStretchGroup_expand()
+ {
+ /*
+ * test that emulates clicking column 4 (selected)
+ * and dragging right to column 5 (all base 0)
+ */
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(4);
+ SequenceGroup sg = new SequenceGroup();
+ sg.setStartRes(4);
+ sg.setEndRes(4);
+ cs.stretchGroup(5, sg, 4, 4);
+ assertEquals(cs.getSelected().size(), 2);
+ assertTrue(cs.contains(4));
+ assertTrue(cs.contains(5));
+ assertEquals(sg.getStartRes(), 4);
+ assertEquals(sg.getEndRes(), 5);
+
+ /*
+ * emulate drag right with columns 10-20 already selected
+ */
+ cs.clear();
+ for (int i = 10; i <= 20; i++)
+ {
+ cs.addElement(i);
+ }
+ assertEquals(cs.getSelected().size(), 11);
+ sg = new SequenceGroup();
+ sg.setStartRes(10);
+ sg.setEndRes(20);
+ cs.stretchGroup(21, sg, 10, 20);
+ assertEquals(cs.getSelected().size(), 12);
+ assertTrue(cs.contains(10));
+ assertTrue(cs.contains(21));
+ assertEquals(sg.getStartRes(), 10);
+ assertEquals(sg.getEndRes(), 21);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testStretchGroup_shrink()
+ {
+ /*
+ * emulate drag left to 19 with columns 10-20 already selected
+ */
+ ColumnSelection cs = new ColumnSelection();
+ for (int i = 10; i <= 20; i++)
+ {
+ cs.addElement(i);
+ }
+ assertEquals(cs.getSelected().size(), 11);
+ SequenceGroup sg = new SequenceGroup();
+ sg.setStartRes(10);
+ sg.setEndRes(20);
+ cs.stretchGroup(19, sg, 10, 20);
+ assertEquals(cs.getSelected().size(), 10);
+ assertTrue(cs.contains(10));
+ assertTrue(cs.contains(19));
+ assertFalse(cs.contains(20));
+ assertEquals(sg.getStartRes(), 10);
+ assertEquals(sg.getEndRes(), 19);
}
}