This is an automated email from the ASF dual-hosted git repository. rec pushed a commit to branch bugfix/238-Form-6-serializes-non-reachable-FSes-but-should-not in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git
commit 82bb28eaa24549b995c36cbdaa0983372fad4301 Author: Richard Eckart de Castilho <[email protected]> AuthorDate: Fri Jul 29 23:11:16 2022 +0200 #238 - Form 6 serializes non-reachable FSes but should not - Instead of only setting the default v2IdRef flag (which has no effect), set the flag on the CAS currently being processed - Added unit test --- .../org/apache/uima/cas/impl/BinaryCasSerDes6.java | 2 +- .../java/org/apache/uima/util/CasIOUtilsTest.java | 40 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java index 6d0cb9fbc..e6bcec21c 100644 --- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java +++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java @@ -3044,7 +3044,7 @@ public class BinaryCasSerDes6 implements SlotKindsConstants { if (!isWrite) { // always have form 6 do just reachables, to mimic what v2 did AllFSs allFSs; - try (AutoCloseableNoException a = LowLevelCAS.ll_defaultV2IdRefs(false)) { + try (AutoCloseableNoException a = cas1.ll_enableV2IdRefs(false)) { allFSs = new AllFSs(cas1, mark, isTypeMapping ? fs -> isTypeInTgt(fs) : null, isTypeMapping ? typeMapper : null).getAllFSsAllViews_sofas_reachable(); // AllFSs internally already causes _save_to_cas_data() to be called, so we have to add all diff --git a/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java b/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java index 345ebf0f2..d386b1c2c 100644 --- a/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java +++ b/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.uima.util; import static java.util.Arrays.asList; +import static org.apache.uima.cas.SerialFormat.COMPRESSED_FILTERED_TSI; import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayInputStream; @@ -32,14 +33,18 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import org.apache.uima.UIMAFramework; import org.apache.uima.cas.CAS; import org.apache.uima.cas.CASRuntimeException; import org.apache.uima.cas.FeatureStructure; import org.apache.uima.cas.SerialFormat; +import org.apache.uima.cas.impl.CASImpl; import org.apache.uima.jcas.cas.TOP; +import org.apache.uima.jcas.tcas.Annotation; import org.apache.uima.resource.metadata.FsIndexDescription; import org.apache.uima.resource.metadata.TypeDescription; import org.apache.uima.resource.metadata.TypeSystemDescription; @@ -337,6 +342,41 @@ public class CasIOUtilsTest { .extracting(fs -> fs.getType().getName()).containsExactly(customDocAnnoTypeName); } + @Test + public void thatBinaryForm6DoesOnlyIncludeReachableFSes() throws Exception { + CASImpl cas = (CASImpl) CasCreationUtils.createCas(); + byte[] buf; + try (AutoCloseableNoException a = cas.ll_enableV2IdRefs(true)) { + Annotation ann = cas.createAnnotation(cas.getAnnotationType(), 0, 1); + ann.addToIndexes(); + ann.removeFromIndexes(); + + Set<FeatureStructure> allFSes = new LinkedHashSet<>(); + cas.walkReachablePlusFSsSorted(allFSes::add, null, null, null); + + assertThat(allFSes) // + .as("The annotation that was added and then removed before serialization should be found") // + .containsExactly(cas.getSofa(), ann); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + CasIOUtils.save(cas, bos, COMPRESSED_FILTERED_TSI); + buf = bos.toByteArray(); + } + + cas.reset(); + + try (AutoCloseableNoException a = cas.ll_enableV2IdRefs(true)) { + CasIOUtils.load(new ByteArrayInputStream(buf), cas); + + Set<FeatureStructure> allFSes = new LinkedHashSet<>(); + cas.walkReachablePlusFSsSorted(allFSes::add, null, null, null); + + assertThat(allFSes) // + .as("The annotation that was added and then removed before serialization should not be found") // + .containsExactly(cas.getSofa()); + } + } + @AfterEach public void tearDown() throws Exception { cas.release();
