diff options
author | Jeremias Maerki <jeremias@apache.org> | 2009-01-30 11:27:56 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2009-01-30 11:27:56 +0000 |
commit | 77c49600624d70f8de7a19757342bd897c6de20f (patch) | |
tree | e349f451d8f1ac591b017ee494ac9358fcdb642c /test/java | |
parent | 258df651e15f515d6eb925ce32038116d5cc73df (diff) | |
download | xmlgraphics-fop-77c49600624d70f8de7a19757342bd897c6de20f.tar.gz xmlgraphics-fop-77c49600624d70f8de7a19757342bd897c6de20f.zip |
Added validation against XML Schema of the intermediate format so we notice when either changes incompatibly.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@739244 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/java')
-rw-r--r-- | test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java | 14 | ||||
-rw-r--r-- | test/java/org/apache/fop/intermediate/IFParserTestCase.java | 53 |
2 files changed, 67 insertions, 0 deletions
diff --git a/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java b/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java index 63e293afe..465e1405c 100644 --- a/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java +++ b/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java @@ -22,6 +22,7 @@ package org.apache.fop.intermediate; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; @@ -41,6 +42,8 @@ import javax.xml.transform.stream.StreamSource; import org.custommonkey.xmlunit.XMLTestCase; import org.w3c.dom.Document; +import org.xml.sax.SAXException; + import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; @@ -94,6 +97,7 @@ public abstract class AbstractIntermediateTestCase extends XMLTestCase { saveDOM(intermediate, new File(outputDir, getName() + ".1" + getIntermediateFileExtension())); } + validate(intermediate); } /** {@inheritDoc} */ @@ -117,6 +121,16 @@ public abstract class AbstractIntermediateTestCase extends XMLTestCase { } /** + * Validates the intermediate format file. + * @param doc the intermediate file + * @throws IOException if an IO error occurs while loading the schema + * @throws SAXException if a SAX-related exception (including a validation error) occurs + */ + protected void validate(Document doc) throws SAXException, IOException { + //nop by default + } + + /** * Builds an intermediate format document from a source file. * @param source the source file * @param templates the (optional) stylesheet diff --git a/test/java/org/apache/fop/intermediate/IFParserTestCase.java b/test/java/org/apache/fop/intermediate/IFParserTestCase.java index 76d72c804..0dff71ce6 100644 --- a/test/java/org/apache/fop/intermediate/IFParserTestCase.java +++ b/test/java/org/apache/fop/intermediate/IFParserTestCase.java @@ -20,18 +20,28 @@ package org.apache.fop.intermediate; import java.io.File; +import java.io.IOException; import java.io.OutputStream; +import javax.xml.XMLConstants; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; import org.w3c.dom.Document; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.MimeConstants; @@ -47,6 +57,24 @@ import org.apache.fop.render.intermediate.IFSerializer; */ public class IFParserTestCase extends AbstractIntermediateTestCase { + private static Schema ifSchema; + + private static Schema getIFSchema() throws SAXException { + if (ifSchema == null) { + SchemaFactory sFactory; + try { + sFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + } catch (IllegalArgumentException iae) { + System.out.println("No suitable SchemaFactory for XML Schema validation found!"); + return null; + } + File ifSchemaFile = new File( + "src/documentation/intermediate-format-ng/fop-intermediate-format-ng.xsd"); + ifSchema = sFactory.newSchema(ifSchemaFile); + } + return ifSchema; + } + /** * Constructor for the test suite that is used for each test file. * @param testFile the test file to run @@ -107,6 +135,31 @@ public class IFParserTestCase extends AbstractIntermediateTestCase { } /** {@inheritDoc} */ + protected void validate(Document doc) throws SAXException, IOException { + Schema schema = getIFSchema(); + if (schema == null) { + return; //skip validation; + } + Validator validator = schema.newValidator(); + validator.setErrorHandler(new ErrorHandler() { + + public void error(SAXParseException exception) throws SAXException { + throw exception; + } + + public void fatalError(SAXParseException exception) throws SAXException { + throw exception; + } + + public void warning(SAXParseException exception) throws SAXException { + //ignore + } + + }); + validator.validate(new DOMSource(doc)); + } + + /** {@inheritDoc} */ protected void parseAndRender(Source src, OutputStream out) throws Exception { IFParser parser = new IFParser(); |