JAL-4313 Clamp start and end ranges to array size bug/JAL-4313-make-visible-annot-fix
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 1 Nov 2023 15:15:21 +0000 (16:15 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 1 Nov 2023 15:15:21 +0000 (16:15 +0100)
src/jalview/datamodel/AlignmentAnnotation.java
test/jalview/datamodel/AlignmentAnnotationTests.java

index e832c2f..fdb62a1 100755 (executable)
@@ -1625,6 +1625,8 @@ public class AlignmentAnnotation
   private void removeHiddenAnnotation(int start, int end,
           HiddenColumns hiddenColumns)
   {
+    start = Math.min(Math.max(0, start), annotations.length);
+    end = Math.min(Math.max(-1, end), annotations.length - 1);
     // mangle the alignmentAnnotation annotation array
     ArrayList<Annotation[]> annels = new ArrayList<>();
     Annotation[] els = null;
index 2948f83..74c2b63 100644 (file)
@@ -626,6 +626,50 @@ public class AlignmentAnnotationTests
     assertThat(ann.annotations, is(emptyArray()));
   }
 
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_HiddenRegionBeyondSize()
+  {
+    Annotation[] annots = annotsRange(4);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    hc.hideColumns(6, 7);
+    ann.makeVisibleAnnotation(hc);
+    assertThat(ann.annotations, matchesAnnotations(annots));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_HiddenRegionAndTrimEndBeyondSize()
+  {
+    Annotation[] annots = annotsRange(4);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    hc.hideColumns(6, 7);
+    ann.makeVisibleAnnotation(0, 10, hc);
+    assertThat(ann.annotations, matchesAnnotations(annots));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_HiddenRegionBeforeStart()
+  {
+    Annotation[] annots = annotsRange(4);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    hc.hideColumns(-3, -2);
+    ann.makeVisibleAnnotation(hc);
+    assertThat(ann.annotations, matchesAnnotations(annots));
+  }
+
+  @Test(groups = "Functional")
+  public void testMakeVisibleAnnotation_HiddenRegionAndTrimStartBeforeStart()
+  {
+    Annotation[] annots = annotsRange(4);
+    AlignmentAnnotation ann = new AlignmentAnnotation("label", "desc", annots);
+    HiddenColumns hc = new HiddenColumns();
+    hc.hideColumns(-3, -2);
+    ann.makeVisibleAnnotation(-5, annots.length - 1, hc);
+    assertThat(ann.annotations, matchesAnnotations(annots));
+  }
+
   static Annotation[] annotsRange(int stop)
   {
     Annotation[] annotations = new Annotation[stop];