This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch 
bugfix/240-Helper-annotation-created-by-SelectFS-should-not-survive
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit b7225a49cccb995f90d8f76fbfc1a9b51cacdfa4
Author: Richard Eckart de Castilho <[email protected]>
AuthorDate: Sat Jul 30 00:35:32 2022 +0200

    Issue #240: Helper annotation created by SelectFS should not survive
    
    - Prevent helper annotation from getting an ID and being held onto even 
when holding onto FSes is enabled
    - Added unit test
---
 .../java/org/apache/uima/cas/impl/CASImpl.java     | 13 +++++++++--
 .../org/apache/uima/cas/impl/SelectFSs_impl.java   |  5 +++-
 .../apache/uima/cas/impl/SelectFsNoJCasTest.java   | 27 ++++++++++++++++++----
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
index b8c6236b5..e3f57f52e 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
@@ -5240,8 +5240,10 @@ public class CASImpl extends AbstractCas_ImplBase
     List<TOP> all = new AllFSs(this, mark, includeFilter, typeMapper)
             .getAllFSsAllViews_sofas_reachable().getAllFSsSorted();
     List<TOP> filtered = filterAboveMark(all, mark);
-    for (TOP fs : filtered) {
-      action_filtered.accept(fs);
+    if (action_filtered != null) {
+      for (TOP fs : filtered) {
+        action_filtered.accept(fs);
+      }
     }
     return all;
   }
@@ -6169,6 +6171,13 @@ public class CASImpl extends AbstractCas_ImplBase
     return r;
   }
 
+  AutoCloseableNoException ll_forceEnableV2IdRefs(boolean enable) {
+    final boolean restoreState = svd.isId2Fs;
+    AutoCloseableNoException r = () -> svd.isId2Fs = restoreState;
+    svd.isId2Fs = enable;
+    return r;
+  }
+
   // int allocIntData(int sz) {
   //
   // if (sz > INT_DATA_FOR_ALLOC_SIZE / 4) {
diff --git 
a/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
index 0322c2da1..c0d6abaa8 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
@@ -63,6 +63,7 @@ import org.apache.uima.jcas.cas.NonEmptyFSList;
 import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.jcas.impl.JCasImpl;
 import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.util.AutoCloseableNoException;
 
 // @formatter:off
 /**
@@ -1020,7 +1021,9 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
     if (end < begin) {
       throw new IllegalArgumentException("End value must be >= Begin value");
     }
-    return new Annotation(jcas, begin, end);
+    try (AutoCloseableNoException c = ((CASImpl) 
jcas.getCas()).ll_forceEnableV2IdRefs(false)) {
+      return new Annotation(jcas, begin, end);
+    }
   }
 
 //@formatter:off
diff --git 
a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsNoJCasTest.java 
b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsNoJCasTest.java
index 79bc7d8ae..cac79b523 100644
--- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsNoJCasTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsNoJCasTest.java
@@ -19,7 +19,7 @@
 
 package org.apache.uima.cas.impl;
 
-import static org.junit.Assert.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.File;
 
@@ -30,9 +30,11 @@ import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.resource.metadata.impl.TypePriorities_impl;
 import org.apache.uima.test.junit_extension.JUnitExtension;
+import org.apache.uima.util.AutoCloseableNoException;
 import org.apache.uima.util.CasCreationUtils;
 import org.apache.uima.util.XMLInputSource;
 import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 // tests without initializing JCas
@@ -53,16 +55,33 @@ public class SelectFsNoJCasTest {
             null);
   }
 
+  @BeforeEach
+  public void setup() {
+    cas.reset();
+  }
+
   @Test
   public void testOpsNeedingAnnotation() {
     Type type = cas.getTypeSystem().getType("x.y.z.SentenceNoJCas");
     FeatureStructure s = cas.createAnnotation(type, 0, 4);
     cas.indexRepository.addFS(s);
 
-    boolean b = cas.<Annotation> select(type).covering(1, 2).map(f -> 
f.getBegin()).findFirst()
+    assertThat(cas.<Annotation> select(type).covering(1, 2).map(f -> 
f.getBegin()).findFirst()) //
             .isPresent();
-
-    assertTrue(b);
   }
 
+  @Test
+  public void thatHelperAnnotationsDoNotRemainInCas() {
+    try (AutoCloseableNoException a = cas.ll_enableV2IdRefs(true)) {
+      cas.setDocumentText("text");
+      assertThat(cas.walkReachablePlusFSsSorted(null, null, null, null))
+              .containsExactly(cas.getSofa(), cas.getDocumentAnnotation());
+
+      cas.select(Annotation.class).at(0, 1);
+
+      assertThat(cas.walkReachablePlusFSsSorted(null, null, null, null))
+              .as("The helper annotation created by select must not be 
discovarably")
+              .containsExactly(cas.getSofa(), cas.getDocumentAnnotation());
+    }
+  }
 }

Reply via email to