Skip to content

Commit

Permalink
Merge pull request #241 from apache/bugfix/240-Helper-annotation-crea…
Browse files Browse the repository at this point in the history
…ted-by-SelectFS-should-not-survive

Issue #240: Helper annotation created by SelectFS should not survive
  • Loading branch information
reckart authored Aug 15, 2022
2 parents 6baa0c1 + 819c18d commit 39531d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
13 changes: 11 additions & 2 deletions uimaj-core/src/main/java/org/apache/uima/cas/impl/CASImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5240,8 +5240,10 @@ public List<TOP> walkReachablePlusFSsSorted(Consumer<TOP> action_filtered, Marke
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;
}
Expand Down Expand Up @@ -6169,6 +6171,13 @@ public AutoCloseableNoException ll_enableV2IdRefs(boolean enable) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
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
/**
Expand Down Expand Up @@ -1020,7 +1021,9 @@ private Annotation makePosAnnot(int begin, int end) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,9 +30,11 @@
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
Expand All @@ -53,16 +55,33 @@ public static void setUpClass() throws Exception {
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());
}
}
}

0 comments on commit 39531d4

Please sign in to comment.