JAL-3383 tests and fixes for overloaded SequenceFeatures.findFeatures
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 3 Oct 2019 10:16:12 +0000 (11:16 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 3 Oct 2019 10:16:12 +0000 (11:16 +0100)
src/jalview/datamodel/features/SequenceFeatures.java
test/jalview/datamodel/features/FeatureStoreTest.java
test/jalview/datamodel/features/SequenceFeaturesTest.java

index e747d5f..d283aaa 100644 (file)
@@ -473,6 +473,10 @@ public class SequenceFeatures implements SequenceFeaturesI
           List<SequenceFeature> list)
   {
     FeatureStore fs = featureStore.get(type);
+    if (fs == null)
+    {
+      return list == null ? new ArrayList<>() : list;
+    }
     return fs.findOverlappingFeatures(pos, pos, list);
   }
 
index 2eba1c3..8342018 100644 (file)
@@ -886,4 +886,28 @@ public class FeatureStoreTest
     assertFalse(fs.contains(sf1));
     assertTrue(fs.contains(sf7));
   }
+
+  @Test(groups = "Functional")
+  public void testFindFeatures_withResultList()
+  {
+    FeatureStore fs = newFeatureStore();
+    SequenceFeature sf1 = addFeature(fs, 10, 50);
+    SequenceFeature sf2 = addFeature(fs, 150, 250);
+  
+    List<SequenceFeature> overlaps = new ArrayList<>();
+    List<SequenceFeature> result = fs.findOverlappingFeatures(200, 200,
+            overlaps);
+    assertSame(result, overlaps);
+    assertEquals(result.size(), 1);
+    assertTrue(result.contains(sf2));
+    assertFalse(result.contains(sf1));
+
+    /*
+     * if no list supplied, method creates one
+     */
+    result = fs.findOverlappingFeatures(200, 200, null);
+    assertEquals(result.size(), 1);
+    assertTrue(result.contains(sf2));
+    assertFalse(result.contains(sf1));
+  }
 }
index 29e76bb..923a5fe 100644 (file)
@@ -1275,4 +1275,60 @@ public class SequenceFeaturesTest
     store.deleteAll();
     assertFalse(store.hasFeatures());
   }
+
+  @Test(groups = "Functional")
+  public void testFindFeatures_withResultList()
+  {
+    SequenceFeaturesI sf = new SequenceFeatures();
+    SequenceFeature sf1 = addFeature(sf, "Pfam", 10, 50);
+    SequenceFeature sf2 = addFeature(sf, "Pfam", 150, 250);
+  
+    /*
+     * findFeatures with result list to append to
+     */
+    List<SequenceFeature> overlaps = new ArrayList<>();
+    List<SequenceFeature> result = sf.findFeatures(200, "Pfam", overlaps);
+    assertSame(result, overlaps);
+    assertEquals(result.size(), 1);
+    assertTrue(result.contains(sf2));
+    assertFalse(result.contains(sf1));
+
+    /*
+     * with no result list - method creates one
+     */
+    result = sf.findFeatures(200, "Pfam", null);
+    assertEquals(result.size(), 1);
+    assertTrue(result.contains(sf2));
+    assertFalse(result.contains(sf1));
+
+    /*
+     * verify the created list is extendable (is not immutable)
+     * notice it may contain duplicates
+     */
+    result = sf.findFeatures(220, "Pfam", result);
+    assertEquals(result.size(), 2);
+    assertSame(result.get(0), sf2); // in input list
+    assertSame(result.get(1), sf2); // duplicate added
+
+    /*
+     * no feature of requested type - with result list
+     */
+    overlaps.clear();
+    result = sf.findFeatures(200, "Rfam", overlaps);
+    assertTrue(result.isEmpty());
+
+    /*
+     * no feature of requested type - with no result list
+     */
+    result = sf.findFeatures(200, "Rfam", null);
+    assertTrue(result.isEmpty());
+
+    /*
+     * verify the created list is extendable (is not immutable)
+     */
+    result = sf.findFeatures(210, "Pfam", result);
+    assertEquals(result.size(), 1);
+    assertTrue(result.contains(sf2));
+    assertFalse(result.contains(sf1));
+  }
 }