Skip to content

Commit

Permalink
Merge pull request #236 from apache/bugfix/235-Misleading-error-messa…
Browse files Browse the repository at this point in the history
…ge-when-JCas-type-is-not-registered

Issue #235: Misleading error message when JCas type is not registered
  • Loading branch information
reckart authored Aug 15, 2022
2 parents 39531d4 + 46b1503 commit e063329
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,18 @@ public class CASRuntimeException extends UIMARuntimeException {
* supertypes "{3}".
*/
public static final String JCAS_MISMATCH_SUPERTYPE = "JCAS_MISMATCH_SUPERTYPE";

/**
* JCas type "{0}" used in Java code, but was not declared in the XML type descriptor.
*/
public static final String JCAS_TYPE_NOT_IN_CAS = "JCAS_TYPE_NOT_IN_CAS";

/**
* JCas type "{0}" defined in CAS type system and used in Java code, but was not registered in
* JCasRegistry.
*/
public static final String JCAS_TYPE_NOT_IN_CAS_REGISTRY = "JCAS_TYPE_NOT_IN_CAS_REGISTRY";

/**
* CAS type system type "{0}" defines field "{1}" with range "{2}", but JCas class has range
* "{3}".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ private void throwMissingUIMAtype(int typeindex) {
"Missing UIMA type, JCas Class name: %s, index: %d, jcasRegisteredTypes size: %d%n",
className, typeindex, jcasRegisteredTypes.size());
dumpTypeSystem();
throw new CASRuntimeException(CASRuntimeException.JCAS_TYPE_NOT_IN_CAS, className);
throw new CASRuntimeException(CASRuntimeException.JCAS_TYPE_NOT_IN_CAS_REGISTRY, className);
} else {
throw new CASRuntimeException(CASRuntimeException.JCAS_UNKNOWN_TYPE_NOT_IN_CAS);
}
Expand Down
10 changes: 4 additions & 6 deletions uimaj-core/src/main/java/org/apache/uima/pear/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ public boolean accept(File file) {
boolean extAccepted = true;
if (_dirPath != null) {
String parentDir = file.getParent();
dirAccepted = parentDir != null
&& parentDir.replace(WINDOWS_SEPARATOR_CHAR, UNIX_SEPARATOR_CHAR)
.startsWith(_dirPath);
dirAccepted = parentDir != null && parentDir
.replace(WINDOWS_SEPARATOR_CHAR, UNIX_SEPARATOR_CHAR).startsWith(_dirPath);
}
if (_fileExt != null) {
extAccepted = file.getPath().toLowerCase().endsWith(_fileExt);
Expand Down Expand Up @@ -789,9 +788,8 @@ public static long extractFilesFromJar(JarFile jarFile, File targetDir, FileFilt
File file = new File(targetDir, jarEntry.getName());

if (!normalizeToUnix(file.getCanonicalPath()).startsWith(prefix)) {
throw new IOException(
"Can only write within target folder [" + targetDir.getAbsolutePath()
+ "]. Please validate ZIP contents.");
throw new IOException("Can only write within target folder [" + targetDir.getAbsolutePath()
+ "]. Please validate ZIP contents.");
}

File dir = file.getParentFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ JCAS_CAS_NOT_V3 = JCas Class "{0}", loaded from "{1}", is missing required const
JCAS_MISSING_FIELD_ACCESSOR = JCas Class "{0}" is missing required field accessor, or access not permitted, for field "{1}" during {2} operation.
JCAS_CAS_MISMATCH = CAS type system doesn''t match JCas Type definition for type "{0}".
JCAS_TYPE_NOT_IN_CAS = JCas type "{0}" used in Java code, but was not declared in the XML type descriptor.
JCAS_TYPE_NOT_IN_CAS_REGISTRY = JCas type "{0}" defined in CAS type system and used in Java code, but was not registered in JCasRegistry.
JCAS_UNKNOWN_TYPE_NOT_IN_CAS = Unknown JCas type used in Java code but was not declared or imported in the XML descriptor for this component.
JCAS_FIELD_MISSING_IN_TYPE_SYSTEM = JCAS class "{0}" defines a UIMA field "{1}" but the UIMA type doesn''t define that field.
JCAS_FIELD_ADJ_OFFSET_CHANGED = In JCAS class "{0}", UIMA field "{1}" was set up when this class was previously loaded and initialized, to have an adjusted offset of "{2}" but now the feature has a different adjusted offset of "{3}"; this may be due to something else other than type system commit actions loading and initializing the JCas class, or to having a different non-compatible type system for this class, trying to use a common JCas cover class, which is not supported.
Expand Down
36 changes: 20 additions & 16 deletions uimaj-core/src/test/java/org/apache/uima/jcas/test/JCasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

package org.apache.uima.jcas.test;

import static org.apache.uima.util.CasCreationUtils.createCas;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.InstanceOfAssertFactories.throwable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -70,7 +73,6 @@
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.metadata.impl.TypeSystemDescription_impl;
import org.apache.uima.test.junit_extension.JUnitExtension;
import org.apache.uima.util.CasCreationUtils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -126,8 +128,9 @@ public void checkOkMissingImport(Exception e1) {
// initializing JCas type: aa.Root, feature: testMissingImport\n")) {
// assertTrue(false);
// }
} else
} else {
assertTrue(false);
}
}

public void checkExpectedBadCASError(Exception e1, String err) {
Expand All @@ -139,8 +142,9 @@ public void checkExpectedBadCASError(Exception e1, String err) {
if (!(e.getMessageKey().equals(err))) {
assertTrue(false);
}
} else
} else {
assertTrue(false);
}
}

@AfterEach
Expand Down Expand Up @@ -998,22 +1002,22 @@ public void testOtherListAPI() {
@Test
public void testUndefinedType() throws Exception {
// create jcas with no type system
JCas localJcas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null)
.getJCas();
JCas localJcas = createCas(new TypeSystemDescription_impl(), null, null).getJCas();
localJcas.setDocumentText("This is a test.");
try {
// this should throw an exception
localJcas.getCasType(Sentence.type);
fail();
} catch (CASRuntimeException e) {
assertEquals(CASRuntimeException.JCAS_TYPE_NOT_IN_CAS, e.getMessageKey());
}

assertThatExceptionOfType(CASRuntimeException.class) //
.isThrownBy(() -> localJcas.getCasType(Sentence.type)) //
.asInstanceOf(throwable(CASRuntimeException.class)) //
.extracting(CASRuntimeException::getMessageKey) //
.isEqualTo(CASRuntimeException.JCAS_TYPE_NOT_IN_CAS_REGISTRY);

// check that this does not leave JCAS in an inconsistent state
// (a check for bug UIMA-738)
Iterator<Annotation> iter = localJcas.getAnnotationIndex().iterator();
assertTrue(iter.hasNext());
Annotation annot = iter.next();
assertEquals("This is a test.", annot.getCoveredText());
assertThat(iter).hasNext();
assertThat(iter.next()) //
.extracting(Annotation::getCoveredText) //
.isEqualTo("This is a test.");
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,25 @@
*/
public class PearInstallationVerificationTest {
@Test
public void testAePearVerification(@TempDir
File temp) throws Exception {
public void testAePearVerification(@TempDir File temp) throws Exception {
assertThatPearInstalls(getFile("pearTests/analysisEngine.pear"), temp);
}

@Test
public void testCcPearVerification(@TempDir
File temp) throws Exception {
public void testCcPearVerification(@TempDir File temp) throws Exception {
assertThatPearInstalls(getFile("pearTests/casConsumer.pear"), temp);
}

@Test
public void testTsPearVerification(@TempDir
File temp) throws Exception {
public void testTsPearVerification(@TempDir File temp) throws Exception {
assertThatPearInstalls(getFile("pearTests/typeSystem.pear"), temp);
}

// TODO: create testcases for ci, cr, cpe pear packages

@Test
public void thatSpecialXmlCharactersInTargetPathDoNotBreakInstallation(@TempDir
File temp) throws Exception {
public void thatSpecialXmlCharactersInTargetPathDoNotBreakInstallation(@TempDir File temp)
throws Exception {
File folder = new File(temp, "!'&");
folder.mkdirs();
assertThatPearInstalls(getFile("pearTests/analysisEngine.pear"),
Expand Down

0 comments on commit e063329

Please sign in to comment.