Skip to content

Commit

Permalink
Issue #231: CAS not reset if validation fails in managed CASes
Browse files Browse the repository at this point in the history
- Fix issue that CAS is not reset if validation fails
- Added unit tests
  • Loading branch information
reckart committed Jul 5, 2023
1 parent 6742ffc commit f00e963
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void test() throws Exception {

assertThat(processed.get()) //
.as("CPE stop processing soon after reader threw an exception") //
.isBetween(failAfter, failAfter + (4 * getRuntime().availableProcessors()));
.isBetween(failAfter, failAfter + (12 * getRuntime().availableProcessors()));
}

public static class Reader extends JCasCollectionReader_ImplBase {
Expand Down
9 changes: 9 additions & 0 deletions uimafit-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,14 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.apache.uima.fit.validation.ValidationResult.Severity.ERROR;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.invoke.MethodHandles;
import java.util.Set;
import java.util.WeakHashMap;

Expand All @@ -41,6 +42,8 @@
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;

/**
* Provides a {@link CAS} object which is automatically reset before the test. The idea of this
Expand All @@ -53,6 +56,8 @@
public final class ManagedCas implements TestWatcher, AfterTestExecutionCallback, AfterAllCallback {
private final ThreadLocal<CAS> casHolder;

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final Set<CAS> managedCases = synchronizedSet(newSetFromMap(new WeakHashMap<>()));

private Validator defaultValidator = new Validator.Builder().build();
Expand Down Expand Up @@ -117,9 +122,20 @@ public void afterAll(ExtensionContext aContext) throws Exception {
public void afterTestExecution(ExtensionContext context) throws Exception {
try {
managedCases.forEach(this::assertValid);
managedCases.forEach(CAS::reset);
} finally {
this.validator = null;
reset();
}
}

private void reset() {
this.validator = null;

for (CAS cas : managedCases) {
try {
cas.reset();
} catch (Exception e) {
LOG.error(e, () -> "Unable to reset managed CAS");
}
}
}

Expand Down Expand Up @@ -165,7 +181,7 @@ public ManagedCas withValidator(Validator aValidator) {
return this;
}

private Validator getValidator() {
Validator getValidator() {
if (validator != null) {
return validator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.apache.uima.fit.validation.ValidationResult.Severity.ERROR;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.invoke.MethodHandles;
import java.util.Set;
import java.util.WeakHashMap;

Expand All @@ -41,6 +42,8 @@
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;

/**
* Provides a {@link JCas} object which is automatically reset before the test. The idea of this
Expand All @@ -54,6 +57,8 @@ public final class ManagedJCas
implements TestWatcher, AfterTestExecutionCallback, AfterAllCallback {
private final ThreadLocal<JCas> casHolder;

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final Set<JCas> managedCases = synchronizedSet(newSetFromMap(new WeakHashMap<>()));

private Validator defaultValidator = new Validator.Builder().build();
Expand Down Expand Up @@ -118,9 +123,20 @@ public void afterAll(ExtensionContext aContext) throws Exception {
public void afterTestExecution(ExtensionContext context) throws Exception {
try {
managedCases.forEach(this::assertValid);
managedCases.forEach(JCas::reset);
} finally {
this.validator = null;
reset();
}
}

private void reset() {
this.validator = null;

for (JCas cas : managedCases) {
try {
cas.reset();
} catch (Exception e) {
LOG.error(e, () -> "Unable to reset managed CAS");
}
}
}

Expand Down Expand Up @@ -166,7 +182,7 @@ public ManagedJCas withValidator(Validator aValidator) {
return this;
}

private Validator getValidator() {
Validator getValidator() {
if (validator != null) {
return validator;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.uima.fit.testing.junit;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.apache.uima.cas.CAS;
import org.apache.uima.fit.validation.ValidationResult;
import org.apache.uima.fit.validation.Validator;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

class ManagedCasTest {

private ManagedCas sut = new ManagedCas();

@Test
void thatValidatorIsResetBeforeNextTest() throws Exception {
Validator defaultValidator = sut.getValidator();
Validator transientValidator = new Validator.Builder().build();
sut.withValidator(transientValidator);
assertThat(sut.getValidator()).isSameAs(transientValidator);
sut.afterTestExecution(null);
assertThat(sut.getValidator()).isSameAs(defaultValidator);
}

@Test
void thatCasIsResetBeforeNextTest() throws Exception {
CAS cas = sut.get();
cas.setDocumentText("test");
sut.afterTestExecution(null);
assertThat(cas.getDocumentText()).isNull();
}

@Test
void thatCasIsResetBeforeNextTestIfValidationFails() throws Exception {
sut.withValidator(new Validator.Builder()
.withCheck(cas -> asList(ValidationResult.error(this, "fail"))).build());
CAS cas = sut.get();
cas.setDocumentText("test");
assertThatExceptionOfType(AssertionFailedError.class) //
.as("Validation should fail") //
.isThrownBy(() -> sut.afterTestExecution(null));
assertThat(cas.getDocumentText()) //
.as("Despite failed validation, CAS should have been reset") //
.isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.uima.fit.testing.junit;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.apache.uima.fit.validation.ValidationResult;
import org.apache.uima.fit.validation.Validator;
import org.apache.uima.jcas.JCas;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

class ManagedJCasTest {

private ManagedJCas sut = new ManagedJCas();

@Test
void thatValidatorIsResetBeforeNextTest() throws Exception {
Validator defaultValidator = sut.getValidator();
Validator transientValidator = new Validator.Builder().build();
sut.withValidator(transientValidator);
assertThat(sut.getValidator()).isSameAs(transientValidator);
sut.afterTestExecution(null);
assertThat(sut.getValidator()).isSameAs(defaultValidator);
}

@Test
void thatCasIsResetBeforeNextTest() throws Exception {
JCas cas = sut.get();
cas.setDocumentText("test");
sut.afterTestExecution(null);
assertThat(cas.getDocumentText()).isNull();
}

@Test
void thatCasIsResetBeforeNextTestIfValidationFails() throws Exception {
sut.withValidator(new Validator.Builder()
.withCheck(cas -> asList(ValidationResult.error(this, "fail"))).build());
JCas cas = sut.get();
cas.setDocumentText("test");
assertThatExceptionOfType(AssertionFailedError.class) //
.as("Validation should fail") //
.isThrownBy(() -> sut.afterTestExecution(null));
assertThat(cas.getDocumentText()) //
.as("Despite failed validation, JCAS should have been reset") //
.isNull();
}
}
12 changes: 12 additions & 0 deletions uimafit-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
<commons-io-version>2.11.0</commons-io-version>
<commons-lang3-version>3.12.0</commons-lang3-version>
<junit-version>5.8.2</junit-version>
<junit-platform-version>1.8.2</junit-platform-version>
<groovy-version>3.0.10</groovy-version>
<mockito-version>4.4.0</mockito-version>
<opentest4j-version>1.2.0</opentest4j-version>
<spring-version>5.3.25</spring-version>
<slf4j-version>1.7.36</slf4j-version>
<uima-version>3.4.0</uima-version>
Expand Down Expand Up @@ -111,6 +113,16 @@
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit-platform-version}</version>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>${opentest4j-version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down

0 comments on commit f00e963

Please sign in to comment.