From: kiramt Date: Fri, 17 Mar 2017 13:47:15 +0000 (+0000) Subject: JAL-2437 Synchronization corrections and unit tests X-Git-Tag: Release_2_10_2~3^2~92^2~31 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=bc096ecbcd3cf4bbf17a24f9e9df88806789d688;p=jalview.git JAL-2437 Synchronization corrections and unit tests --- diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index db6195a..41488ea 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -338,15 +338,18 @@ public class Alignment implements AlignmentI @Override public void deleteSequence(SequenceI s) { - deleteSequence(findIndex(s)); + synchronized (sequences) + { + deleteSequence(findIndex(s)); + } } @Override public void deleteSequence(int i) { - if (i > -1 && i < getHeight()) + synchronized (sequences) { - synchronized (sequences) + if (i > -1 && i < getHeight()) { sequences.remove(i); hiddenSequences.adjustHeightSequenceDeleted(i); @@ -357,9 +360,9 @@ public class Alignment implements AlignmentI @Override public void deleteHiddenSequence(int i) { - if (i > -1 && i < getHeight()) + synchronized (sequences) { - synchronized (sequences) + if (i > -1 && i < getHeight()) { sequences.remove(i); } diff --git a/test/jalview/datamodel/AlignmentTest.java b/test/jalview/datamodel/AlignmentTest.java index 7af77f5..68933bd 100644 --- a/test/jalview/datamodel/AlignmentTest.java +++ b/test/jalview/datamodel/AlignmentTest.java @@ -27,6 +27,7 @@ import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import jalview.analysis.AlignmentGenerator; import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping; import jalview.gui.JvOptionPane; import jalview.io.DataSourceType; @@ -1196,4 +1197,67 @@ public class AlignmentTest assertNull(a.findGroup(seq2, 8)); } + @Test(groups = { "Functional" }) + public void testDeleteSequenceByIndex() + { + // create random alignment + AlignmentGenerator gen = new AlignmentGenerator(false); + AlignmentI a = gen.generate(20, 15, 123, 5, 5); + + // delete sequence 10, alignment reduced by 1 + int height = a.getAbsoluteHeight(); + a.deleteSequence(10); + assertEquals(a.getAbsoluteHeight(), height - 1); + + // try to delete -ve index, nothing happens + a.deleteSequence(-1); + assertEquals(a.getAbsoluteHeight(), height - 1); + + // try to delete beyond end of alignment, nothing happens + a.deleteSequence(14); + assertEquals(a.getAbsoluteHeight(), height - 1); + } + + @Test(groups = { "Functional" }) + public void testDeleteSequenceBySeq() + { + // create random alignment + AlignmentGenerator gen = new AlignmentGenerator(false); + AlignmentI a = gen.generate(20, 15, 123, 5, 5); + + // delete sequence 10, alignment reduced by 1 + int height = a.getAbsoluteHeight(); + SequenceI seq = a.getSequenceAt(10); + a.deleteSequence(seq); + assertEquals(a.getAbsoluteHeight(), height - 1); + + // try to delete non-existent sequence, nothing happens + seq = new Sequence("cds", "GCCTCGGAT"); + assertEquals(a.getAbsoluteHeight(), height - 1); + } + + @Test(groups = { "Functional" }) + public void testDeleteHiddenSequence() + { + // create random alignment + AlignmentGenerator gen = new AlignmentGenerator(false); + AlignmentI a = gen.generate(20, 15, 123, 5, 5); + + // delete a sequence which is hidden, check it is NOT removed from hidden + // sequences + int height = a.getAbsoluteHeight(); + SequenceI seq = a.getSequenceAt(2); + a.getHiddenSequences().hideSequence(seq); + assertEquals(a.getHiddenSequences().getSize(), 1); + a.deleteSequence(2); + assertEquals(a.getAbsoluteHeight(), height - 1); + assertEquals(a.getHiddenSequences().getSize(), 1); + + // delete a sequence which is not hidden, check hiddenSequences are not + // affected + a.deleteSequence(10); + assertEquals(a.getAbsoluteHeight(), height - 2); + assertEquals(a.getHiddenSequences().getSize(), 1); + } + }