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

rec pushed a commit to branch 
bugfix/UIMA-6423-Selecting-a-non-existing-type-returns-all-types
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 3170f55d2401c56ba97c570861f1d48abedae9ca
Author: Richard Eckart de Castilho <[email protected]>
AuthorDate: Wed Mar 2 10:05:33 2022 +0100

    [UIMA-6423] Selecting a non-existing type returns all types
    
    - Added exceptions and tests to check the exceptions are really thrown
---
 .../org/apache/uima/cas/impl/SelectFSs_impl.java   | 16 +++++++-
 .../org/apache/uima/cas/impl/SelectFsTest.java     | 47 +++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)

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 6c3f714..0322c2d 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
@@ -204,6 +204,14 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
     if (uimaType == null) {
       throw new IllegalArgumentException("Must specify a type");
     }
+
+    if (view.getTypeSystemImpl() != ((TypeImpl) uimaType).getTypeSystem()) {
+      Type type = view.getTypeSystem().getType(uimaType.getName());
+      if (type == null) {
+        throw new IllegalArgumentException("Undefined type: [" + 
uimaType.getName() + "]");
+      }
+    }
+
     this.ti = (TypeImpl) uimaType;
     return (SelectFSs_impl<N>) this;
   }
@@ -212,7 +220,13 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
     if (fullyQualifiedTypeName == null) {
       throw new IllegalArgumentException("Must specify a type");
     }
-    this.ti = view.getTypeSystemImpl().getType(fullyQualifiedTypeName);
+
+    TypeImpl type = view.getTypeSystemImpl().getType(fullyQualifiedTypeName);
+    if (type == null) {
+      throw new IllegalArgumentException("Undefined type: [" + 
fullyQualifiedTypeName + "]");
+    }
+
+    this.ti = type;
     return (SelectFSs_impl<N>) this;
   }
 
diff --git 
a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java 
b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
index b716f73..45b729c 100644
--- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
@@ -27,6 +27,7 @@ import static org.apache.uima.cas.CAS.TYPE_NAME_ANNOTATION;
 import static org.apache.uima.cas.text.AnnotationPredicates.coveredBy;
 import static org.apache.uima.cas.text.AnnotationPredicates.overlappingAtEnd;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -36,12 +37,14 @@ import java.util.stream.Stream;
 
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.CASRuntimeException;
 import org.apache.uima.cas.FSIterator;
 import org.apache.uima.cas.FeatureStructure;
 import org.apache.uima.cas.SelectFSs;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.resource.metadata.TypePriorities;
 import org.apache.uima.resource.metadata.TypePriorityList;
@@ -50,6 +53,7 @@ import org.apache.uima.test.junit_extension.JUnitExtension;
 import org.apache.uima.util.CasCreationUtils;
 import org.apache.uima.util.XMLInputSource;
 import org.junit.FixMethodOrder;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
@@ -81,7 +85,7 @@ public class SelectFsTest {
   static File typeSystemFile1 = JUnitExtension
           .getFile("ExampleCas/testTypeSystem_token_sentence_no_features.xml");
 
-  public void setup(Mode aMode, String[] aPrioTypeNames) throws Exception {
+  public void setup(Mode aMode, String... aPrioTypeNames) throws Exception {
     mode = aMode;
     typeSystemDescription = UIMAFramework.getXMLParser()
             .parseTypeSystemDescription(new XMLInputSource(typeSystemFile1));
@@ -1772,6 +1776,47 @@ public class SelectFsTest {
     assertThat(it.isValid()).isFalse();
   }
 
+  @Test
+  public void thatSelectingNonExistingTypeCreatesException() throws Exception {
+    setup(Mode.ANNOTATION_FIRST);
+
+    Type tokenType = cas.getCasType(Token.class);
+    CAS localCas = CasCreationUtils.createCas();
+
+    assertThat((Iterable<Annotation>) localCas.select(Annotation.class))
+            .as("Select existing type by JCas cover-class") //
+            .isEmpty();
+
+    assertThatExceptionOfType(CASRuntimeException.class) //
+            .isThrownBy(() -> localCas.select(Token.class))
+            .as("Select non-existing type by JCas cover-class");
+
+    assertThat((Iterable<TOP>) localCas.select(localCas.getAnnotationType()))
+            .as("Select existing type by CAS type") //
+            .isEmpty();
+
+    assertThatExceptionOfType(IllegalArgumentException.class) //
+            .isThrownBy(() -> localCas.select(tokenType))
+            .as("Select non-existing type by CAS type (obtained from another 
CAS)");
+
+    assertThat((Iterable<TOP>) localCas.select(Annotation.type))
+            .as("Select existing type by JCas type ID") //
+            .isEmpty();
+
+    assertThatExceptionOfType(CASRuntimeException.class) //
+            .isThrownBy(() -> localCas.select(Token.type))
+            .as("Select non-existing type by JCas type ID");
+
+    assertThat((Iterable<TOP>) localCas.select(Annotation._TypeName))
+            .as("Select existing type by type name") //
+            .isEmpty();
+
+    assertThatExceptionOfType(IllegalArgumentException.class) //
+            .isThrownBy(() -> localCas.select(Token._TypeName))
+            .as("Select non-existing type by type name");
+
+  }
+
   @SuppressWarnings("unchecked")
   private static <T extends AnnotationFS, R extends AnnotationFS> List<R> 
toListBackwards(
           SelectFSs<T> select) {

Reply via email to