-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add feature and steps to test the XML report
- 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
Showing
4 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/org/w3c/epubcheck/test/XMLReportAssertionSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|