Skip to content

Commit

Permalink
Issue #292: Make CAS serialization tests more robust
Browse files Browse the repository at this point in the history
- run SerRef tests using a content comparison
  • Loading branch information
reckart committed Jan 23, 2023
1 parent 8cea780 commit a955a0d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ private static List<SerDesTestScenario> randomSerDesScenarios() {
@ParameterizedTest
@MethodSource("serRefScenarios")
public void serializeAndCompareToReferenceTest(Runnable aScenario) throws Exception {
assumeNotKnownToFail(aScenario, //
".*casWithSofaDataArray",
"XCAS does not suport SofA data arrays during deserialiaztion");

aScenario.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static List<SerRefTestScenario> serRefScenarios(SerialFormat aFormat,
return ProgrammaticallyCreatedCasDataSuite.builder().build().stream()
.map(conf -> SerRefTestScenario.builder(caller, conf, SER_REF, aCasFileName)
.withSerializer((cas, path) -> ser(cas, path, aFormat)) //
.build())
.withAssertion(SerRefTestScenario::assertCasContentsAreEqual).build())
.collect(toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
*/
package org.apache.uima.cas.serdes.scenario;

import static org.apache.uima.cas.serdes.CasToComparableText.toComparableString;
import static org.apache.uima.cas.serdes.SerDesCasIOTestUtils.createCasMaybeWithTypesystem;
import static org.apache.uima.cas.serdes.SerDesCasIOTestUtils.des;
import static org.apache.uima.cas.serdes.SerDesCasIOTestUtils.writeTypeSystemDescription;
import static org.apache.uima.cas.serdes.SerDesCasIOTestUtils.writeXmi;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assumptions.assumeThat;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -32,6 +37,9 @@
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.serdes.TestType;
import org.apache.uima.cas.serdes.transitions.CasSourceTargetConfiguration;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.CasLoadMode;
import org.apache.uima.util.InvalidXMLException;
import org.assertj.core.internal.Failures;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,23 +51,15 @@ public class SerRefTestScenario implements Runnable {
private final Path referenceCasFile;
private final Path targetCasFile;
private final FailableBiConsumer<CAS, Path, ?> serializer;
private final FailableBiConsumer<Path, Path, ?> assertion;

private SerRefTestScenario(Builder builder) {
this.title = builder.title;
this.sourceCasSupplier = builder.sourceCasSupplier;
this.referenceCasFile = builder.referenceCasFile;
this.targetCasFile = builder.targetCasFile;
this.serializer = builder.serializer;
}

public SerRefTestScenario(Path aReferenceBasePath, Path aTargetBasePath,
CasSourceTargetConfiguration aSourceTargetConfiguration, String aTargetFileName,
FailableBiConsumer<CAS, Path, ?> aSerializer) {
title = aSourceTargetConfiguration.getTitle();
sourceCasSupplier = aSourceTargetConfiguration::createSourceCas;
referenceCasFile = aReferenceBasePath.resolve(title).resolve(aTargetFileName);
targetCasFile = aTargetBasePath.resolve(title).resolve(aTargetFileName);
serializer = aSerializer;
this.assertion = builder.assertion;
}

public CAS createSourceCas() {
Expand Down Expand Up @@ -102,8 +102,14 @@ public void run() {
assumeThat(referenceCasFile.toFile()) //
.as("Reference file must exists at %s", referenceCasFile) //
.exists();
assertThat(contentOf(targetCasFile.toFile())) //
.isEqualToNormalizingNewlines(contentOf(referenceCasFile.toFile()));

try {
assertion.accept(targetCasFile, referenceCasFile);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable e) {
fail("Unable to apply assertion", e);
}
}

private void serialize(CAS aSourceCas, Path aTargetCasFile) {
Expand All @@ -121,6 +127,21 @@ public static Path getDataBasePath(Class<?> aTestClass) {
return Paths.get("src", "test", "resources", aTestClass.getSimpleName(), "ser-ref");
}

public static void assertFileContentsAreEqualNormalizingNewlines(Path aTargetCasFile,
Path aReferenceCasFile) {
assertThat(contentOf(aTargetCasFile.toFile())) //
.isEqualToNormalizingNewlines(contentOf(aReferenceCasFile.toFile()));
}

public static void assertCasContentsAreEqual(Path aTargetCasFile, Path aReferenceCasFile)
throws ResourceInitializationException, InvalidXMLException, IOException {
CAS targetCas = createCasMaybeWithTypesystem(aReferenceCasFile);
CAS referenceCas = createCasMaybeWithTypesystem(aReferenceCasFile);
des(targetCas, aTargetCasFile, CasLoadMode.DEFAULT);
des(referenceCas, aReferenceCasFile, CasLoadMode.DEFAULT);
assertThat(toComparableString(targetCas)).isEqualTo(toComparableString(referenceCas));
}

/**
* Creates builder to build {@link SerRefTestScenario}.
*
Expand Down Expand Up @@ -153,8 +174,11 @@ public static final class Builder {
private Path referenceCasFile;
private Path targetCasFile;
private FailableBiConsumer<CAS, Path, ?> serializer;
private FailableBiConsumer<Path, Path, ?> assertion;

private Builder() {
// Compare the serialized CAS file against the reference
assertion = SerRefTestScenario::assertFileContentsAreEqualNormalizingNewlines;
}

public Builder withTitle(String title) {
Expand Down Expand Up @@ -182,6 +206,11 @@ public Builder withSerializer(FailableBiConsumer<CAS, Path, ?> serializer) {
return this;
}

public Builder withAssertion(FailableBiConsumer<Path, Path, ?> aAssertion) {
this.assertion = aAssertion;
return this;
}

public SerRefTestScenario build() {
return new SerRefTestScenario(this);
}
Expand Down

0 comments on commit a955a0d

Please sign in to comment.