Skip to content

Commit

Permalink
test: add feature and steps to test the XML report
Browse files Browse the repository at this point in the history
- add new assertion steps to test the XML report, mostly based
  on XPath evaluated vis Saxon's API
- remove the xmlunit dependency that was not in use
  • Loading branch information
rdeltour committed Dec 23, 2024
1 parent e1871e2 commit f0fb7b7
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@
<artifactId>json-path-assert</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class JSONReportAssertionSteps
{

private TestReport report;
final private TestReport report;

public JSONReportAssertionSteps(TestConfiguration configuration)
{
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/org/w3c/epubcheck/test/XMLReportAssertionSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.w3c.epubcheck.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.function.Supplier;

import javax.xml.transform.stream.StreamSource;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathSelector;
import net.sf.saxon.s9api.XdmNode;

public class XMLReportAssertionSteps
{

private final TestReport report;
private final Processor processor;
private final XPathCompiler xpathCompiler;
Supplier<XdmNode> xml = new Supplier<XdmNode>()
{
XdmNode xml = null;

@Override
public XdmNode get()
{
if (xml == null)
{

// Parse the XML report
DocumentBuilder docBuilder = processor.newDocumentBuilder();
try
{
xml = docBuilder.build(new StreamSource(new StringReader(report.getOutput())));
} catch (SaxonApiException e)
{
assertThat(e.getMessage(), false);
}
}
return xml;
}
};

public XMLReportAssertionSteps(TestConfiguration configuration)
{
this.report = configuration.getReport();

// Configure Saxon
this.processor = new Processor(false);
processor.getUnderlyingConfiguration()
.setStandardErrorOutput(new PrintStream(new OutputStream()
{
// TODO when upgrading to Java 11, replace by
// OutputStream#nullOutputStream()
@Override
public void write(int b)
throws IOException
{
}
}));
xpathCompiler = processor.newXPathCompiler();
}

@Given("the default namespace is {string}")
public void setDefaultNamespace(String namespace)
{
xpathCompiler.declareNamespace("", namespace);
}

@Then("the XML report is well-formed")
public void xmlIsWellFormed()
{
// Parsing errors would be raised in the constructor already
}

@Then("(the )XPath (value of ){string} is true")
public void xpathIsTrue(String xpath)
throws SaxonApiException
{
assertThat(eval(xpath).effectiveBooleanValue(), is(true));
}

@Then("(the )XPath {string} exists")
public void xpathExists(String xpath)
throws SaxonApiException
{
assertThat(eval(xpath), is(not(emptyIterable())));

}

private XPathSelector eval(String xpath)
{
try
{
XPathSelector result = xpathCompiler.compile(xpath).load();
result.setContextItem(xml.get());
return result;
} catch (SaxonApiException e)
{
throw new IllegalArgumentException(e);
}
}

}
16 changes: 16 additions & 0 deletions src/test/resources/reporting/xml-report.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: EPUBCheck - XML Report tests

Checks the XML (JHove) report format


Background:
Given EPUB test files located at '/reporting/files/'
And the reporting format is set to XML
And EPUBCheck with default settings
Given the default namespace is 'http://schema.openpreservation.org/ois/xml/ns/jhove'

Scenario: Basic well-formedness checks
When checking EPUB 'minimal'
Then the XML report is well-formed
And XPath '//repInfo' exists

0 comments on commit f0fb7b7

Please sign in to comment.