From f5c4a8a268a77ac474addbc778e85b75ffa05da3 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 31 Mar 2017 12:26:45 +0100 Subject: [PATCH] JAL-2405 additional checks for circular reference in context or dataset --- src/jalview/datamodel/Alignment.java | 4 ++++ src/jalview/datamodel/SequenceGroup.java | 2 +- test/jalview/datamodel/AlignmentTest.java | 9 +++++++++ test/jalview/datamodel/SequenceGroupTest.java | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 41488ea..2dafc0c 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -1011,6 +1011,10 @@ public class Alignment implements AlignmentI } else if (dataset == null && data != null) { + if (data == this) + { + throw new IllegalArgumentException("Circular dataset reference"); + } if (!(data instanceof Alignment)) { throw new Error( diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index e37c55e..1246d23 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -1358,7 +1358,7 @@ public class SequenceGroup implements AnnotatedCollectionI AnnotatedCollectionI ref = ctx; while (ref != null) { - if (ref == this) + if (ref == this || ref.getContext() == ctx) { throw new IllegalArgumentException( "Circular reference in SequenceGroup.context"); diff --git a/test/jalview/datamodel/AlignmentTest.java b/test/jalview/datamodel/AlignmentTest.java index 68933bd..c5d09c1 100644 --- a/test/jalview/datamodel/AlignmentTest.java +++ b/test/jalview/datamodel/AlignmentTest.java @@ -1260,4 +1260,13 @@ public class AlignmentTest assertEquals(a.getHiddenSequences().getSize(), 1); } + @Test( + groups = "Functional", + expectedExceptions = { IllegalArgumentException.class }) + public void testSetDataset_selfReference() + { + SequenceI seq = new Sequence("a", "a"); + AlignmentI alignment = new Alignment(new SequenceI[] { seq }); + alignment.setDataset(alignment); + } } diff --git a/test/jalview/datamodel/SequenceGroupTest.java b/test/jalview/datamodel/SequenceGroupTest.java index 65549f2..6e1c2db 100644 --- a/test/jalview/datamodel/SequenceGroupTest.java +++ b/test/jalview/datamodel/SequenceGroupTest.java @@ -10,6 +10,8 @@ import static org.testng.Assert.fail; import jalview.schemes.NucleotideColourScheme; +import junit.extensions.PA; + import org.testng.annotations.Test; public class SequenceGroupTest @@ -111,6 +113,21 @@ public class SequenceGroupTest // expected assertNull(sg3.getContext()); } + + /* + * use PrivilegedAccessor to 'force' a SequenceGroup with + * a circular context reference + */ + PA.setValue(sg2, "context", sg2); + try + { + sg3.setContext(sg2); // circular reference in sg2 + fail("Expected exception"); + } catch (IllegalArgumentException e) + { + // expected + assertNull(sg3.getContext()); + } } @Test(groups = { "Functional" }) -- 1.7.10.2