public final class XMLUnit
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static boolean |
compareUnmatched |
private static javax.xml.parsers.DocumentBuilderFactory |
controlBuilderFactory |
private static org.xml.sax.EntityResolver |
controlEntityResolver |
private static boolean |
enableXXEProtection |
private static boolean |
expandEntities |
private static boolean |
ignoreAttributeOrder |
private static boolean |
ignoreComments |
private static boolean |
ignoreDiffBetweenTextAndCDATA |
private static boolean |
ignoreWhitespace |
private static NamespaceContext |
namespaceContext |
private static boolean |
normalize |
private static boolean |
normalizeWhitespace |
private static javax.xml.parsers.SAXParserFactory |
saxParserFactory |
private static java.lang.String |
STRIP_COMMENTS_STYLESHEET_END |
private static java.lang.String |
STRIP_COMMENTS_STYLESHEET_START |
private static java.lang.String |
STRIP_WHITESPACE_STYLESHEET_END |
private static java.lang.String |
STRIP_WHITESPACE_STYLESHEET_START |
private static javax.xml.parsers.DocumentBuilderFactory |
testBuilderFactory |
private static org.xml.sax.EntityResolver |
testEntityResolver |
private static javax.xml.transform.TransformerFactory |
transformerFactory |
private static javax.xml.transform.URIResolver |
uriResolver |
private static java.lang.String |
xpathFactoryName |
private static java.lang.String |
XSLT_VERSION_END |
private static java.lang.String |
XSLT_VERSION_START |
private static java.lang.String |
xsltVersion |
Modifier | Constructor and Description |
---|---|
private |
XMLUnit()
Private constructor.
|
Modifier and Type | Method and Description |
---|---|
static org.w3c.dom.Document |
buildControlDocument(org.xml.sax.InputSource fromSource)
Utility method to build a Document using the control DocumentBuilder
and the specified InputSource
|
static org.w3c.dom.Document |
buildControlDocument(java.lang.String fromXML)
Utility method to build a Document using the control DocumentBuilder
to parse the specified String.
|
static org.w3c.dom.Document |
buildDocument(javax.xml.parsers.DocumentBuilder withBuilder,
org.xml.sax.InputSource fromSource)
Utility method to build a Document using a specific DocumentBuilder
and a specific InputSource
|
static org.w3c.dom.Document |
buildDocument(javax.xml.parsers.DocumentBuilder withBuilder,
java.io.Reader fromReader)
Utility method to build a Document using a specific DocumentBuilder
and reading characters from a specific Reader.
|
static org.w3c.dom.Document |
buildTestDocument(org.xml.sax.InputSource fromSource)
Utility method to build a Document using the test DocumentBuilder
and the specified InputSource
|
static org.w3c.dom.Document |
buildTestDocument(java.lang.String fromXML)
Utility method to build a Document using the test DocumentBuilder
to parse the specified String.
|
static Diff |
compareXML(org.w3c.dom.Document control,
org.w3c.dom.Document test)
Compare two XML documents provided as strings
|
static Diff |
compareXML(org.xml.sax.InputSource control,
org.xml.sax.InputSource test)
Compare XML documents provided by two InputSource classes
|
static Diff |
compareXML(java.io.Reader control,
java.io.Reader test)
Compare XML documents provided by two Reader classes
|
static Diff |
compareXML(java.io.Reader control,
java.lang.String test)
Compare XML documents provided by two Reader classes
|
static Diff |
compareXML(java.lang.String control,
java.io.Reader test)
Compare XML documents provided by two Reader classes
|
static Diff |
compareXML(java.lang.String control,
java.lang.String test)
Compare two XML documents provided as strings
|
static boolean |
getCompareUnmatched()
Whether unmatched control nodes should be compared to unmatched
test nodes.
|
static javax.xml.parsers.DocumentBuilderFactory |
getControlDocumentBuilderFactory()
Get the
DocumentBuilderFactory instance used to instantiate
parsers for the control XML in an XMLTestCase. |
static org.xml.sax.EntityResolver |
getControlEntityResolver()
Obtains the EntityResolver to be added to all new control parsers.
|
static boolean |
getEnableXXEProtection()
Whether XXE protection is enabled on the factories used by this class.
|
static boolean |
getExpandEntityReferences()
Whether the parser shall be instructed to expand entity references.
|
static boolean |
getIgnoreAttributeOrder()
Whether to ignore the order of attributes on an element.
|
static boolean |
getIgnoreComments()
Whether comments should be ignored.
|
static boolean |
getIgnoreDiffBetweenTextAndCDATA()
Whether CDATA sections and Text nodes should be considered the same.
|
static boolean |
getIgnoreWhitespace()
Whether to ignore whitespace when comparing node values.
|
static boolean |
getNormalize()
Whether Text nodes should be normalized.
|
static boolean |
getNormalizeWhitespace()
Whether whitespace characters inside text nodes or attributes
should be "normalized".
|
static javax.xml.parsers.SAXParserFactory |
getSAXParserFactory()
Get the SAX parser to use in tests.
|
private static java.lang.String |
getStripCommentsStylesheet() |
static Transform |
getStripCommentsTransform(org.w3c.dom.Document forDocument)
Obtain the transformation that will strip comments from a DOM.
|
private static java.lang.String |
getStripWhitespaceStylesheet() |
static Transform |
getStripWhitespaceTransform(org.w3c.dom.Document forDocument)
Obtain the transformation that will strip whitespace from a DOM
containing empty Text nodes
|
static javax.xml.parsers.DocumentBuilderFactory |
getTestDocumentBuilderFactory()
Get the
DocumentBuilderFactory instance used to instantiate
parsers for the test XML in an XMLTestCase. |
static org.xml.sax.EntityResolver |
getTestEntityResolver()
Obtains the EntityResolver to be added to all new test parsers.
|
static javax.xml.transform.TransformerFactory |
getTransformerFactory()
Get the transformer to use for XSLT transformations (and by
implication serialization and XPaths).
|
static javax.xml.transform.URIResolver |
getURIResolver()
Gets the URIResolver used during Transformations.
|
static java.lang.String |
getVersion()
Place holder for current version info.
|
static org.w3c.dom.Document |
getWhitespaceStrippedDocument(org.w3c.dom.Document forDoc)
Returns a new Document instance that is identical to the one
passed in with element content whitespace removed.
|
static java.lang.String |
getXPathFactory()
Gets the class to use as XPathFactory when using JAXP 1.3.
|
static NamespaceContext |
getXpathNamespaceContext()
Get the NamespaceContext to use in XPath tests.
|
(package private) static java.lang.String |
getXSLTStart()
XSLT stylesheet element using the configured XSLT version.
|
static java.lang.String |
getXSLTVersion()
The XSLT version set on stylesheets used internally.
|
static javax.xml.parsers.DocumentBuilder |
newControlParser()
Get the
DocumentBuilder instance used to parse the control
XML in an XMLTestCase. |
static javax.xml.parsers.DocumentBuilder |
newTestParser()
Get the
DocumentBuilder instance used to parse the test XML
in an XMLTestCase. |
(package private) static javax.xml.transform.TransformerFactory |
newTransformerFactory()
Get a fresh transformer to use for XSLT transformations (and by
implication serialization and XPaths).
|
static XpathEngine |
newXpathEngine()
Obtains an XpathEngine to use in XPath tests.
|
static void |
setCompareUnmatched(boolean b)
Whether to compare unmatched control nodes to unmatched test nodes.
|
static void |
setControlDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory factory)
Override the
DocumentBuilderFactory used to instantiate
parsers for the control XML in an XMLTestCase. |
static void |
setControlEntityResolver(org.xml.sax.EntityResolver resolver)
Sets an EntityResolver to be added to all new control parsers.
|
static void |
setControlParser(java.lang.String className)
Overide the DocumentBuilder to use to parse control documents.
|
static void |
setEnableXXEProtection(boolean b)
Whether to enable XXE protection on the factories used by this class.
|
static void |
setExpandEntityReferences(boolean b)
Whether the parser shall be instructed to expand entity references.
|
static void |
setIgnoreAttributeOrder(boolean b)
Whether to ignore the order of attributes on an element.
|
static void |
setIgnoreComments(boolean b)
Whether comments should be ignored.
|
static void |
setIgnoreDiffBetweenTextAndCDATA(boolean b)
Whether CDATA sections and Text nodes should be considered the same.
|
static void |
setIgnoreWhitespace(boolean ignore)
Whether to ignore whitespace when comparing node values.
|
static void |
setNormalize(boolean b)
Whether Text nodes should be normalized.
|
static void |
setNormalizeWhitespace(boolean b)
Whether whitespace characters inside text nodes or attributes
should be "normalized".
|
static void |
setSAXParserFactory(javax.xml.parsers.SAXParserFactory factory)
Override the SAX parser to use in tests.
|
static void |
setSAXParserFactory(java.lang.String className)
Override the SAX parser to use in tests.
|
static void |
setTestDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory factory)
Override the
DocumentBuilderFactory used to instantiate
parsers for the test XML in an XMLTestCase. |
static void |
setTestEntityResolver(org.xml.sax.EntityResolver resolver)
Sets an EntityResolver to be added to all new test parsers.
|
static void |
setTestParser(java.lang.String className)
Overide the DocumentBuilder to use to parser test documents.
|
static void |
setTransformerFactory(java.lang.String className)
Overide the transformer to use for XSLT transformations (and by
implication serialization and XPaths).
|
static void |
setURIResolver(javax.xml.transform.URIResolver resolver)
Sets the URIResolver to use during transformations.
|
static void |
setXPathFactory(java.lang.String className)
Sets the class to use as XPathFactory when using JAXP 1.3.
|
static void |
setXpathNamespaceContext(NamespaceContext ctx)
Set the NamespaceContext to use in XPath tests.
|
static void |
setXSLTVersion(java.lang.String s)
Sets the XSLT version to set on stylesheets used internally.
|
private static void |
stripEmptyTextNodes(org.w3c.dom.Node n) |
private static org.w3c.dom.Document |
stripWhiteSpaceUsingXSLT(org.w3c.dom.Document forDoc) |
private static org.w3c.dom.Document |
stripWhiteSpaceWithoutXSLT(org.w3c.dom.Document forDoc) |
private static javax.xml.parsers.DocumentBuilderFactory controlBuilderFactory
private static javax.xml.parsers.DocumentBuilderFactory testBuilderFactory
private static javax.xml.transform.TransformerFactory transformerFactory
private static javax.xml.parsers.SAXParserFactory saxParserFactory
private static boolean ignoreWhitespace
private static javax.xml.transform.URIResolver uriResolver
private static org.xml.sax.EntityResolver testEntityResolver
private static org.xml.sax.EntityResolver controlEntityResolver
private static NamespaceContext namespaceContext
private static boolean ignoreDiffBetweenTextAndCDATA
private static boolean ignoreComments
private static boolean normalize
private static boolean normalizeWhitespace
private static boolean ignoreAttributeOrder
private static java.lang.String xsltVersion
private static java.lang.String xpathFactoryName
private static boolean expandEntities
private static boolean compareUnmatched
private static boolean enableXXEProtection
private static final java.lang.String XSLT_VERSION_START
private static final java.lang.String XSLT_VERSION_END
private static final java.lang.String STRIP_WHITESPACE_STYLESHEET_START
private static final java.lang.String STRIP_WHITESPACE_STYLESHEET_END
private static final java.lang.String STRIP_COMMENTS_STYLESHEET_START
private static final java.lang.String STRIP_COMMENTS_STYLESHEET_END
public static void setControlParser(java.lang.String className)
public static javax.xml.parsers.DocumentBuilder newControlParser() throws ConfigurationException
DocumentBuilder
instance used to parse the control
XML in an XMLTestCase.ConfigurationException
public static void setTestEntityResolver(org.xml.sax.EntityResolver resolver)
public static void setControlEntityResolver(org.xml.sax.EntityResolver resolver)
public static org.xml.sax.EntityResolver getControlEntityResolver()
public static org.xml.sax.EntityResolver getTestEntityResolver()
public static javax.xml.parsers.DocumentBuilderFactory getControlDocumentBuilderFactory()
DocumentBuilderFactory
instance used to instantiate
parsers for the control XML in an XMLTestCase.public static void setControlDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory factory)
DocumentBuilderFactory
used to instantiate
parsers for the control XML in an XMLTestCase.public static void setTestParser(java.lang.String className)
public static javax.xml.parsers.DocumentBuilder newTestParser() throws ConfigurationException
DocumentBuilder
instance used to parse the test XML
in an XMLTestCase.ConfigurationException
public static javax.xml.parsers.DocumentBuilderFactory getTestDocumentBuilderFactory()
DocumentBuilderFactory
instance used to instantiate
parsers for the test XML in an XMLTestCase.public static void setTestDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory factory)
DocumentBuilderFactory
used to instantiate
parsers for the test XML in an XMLTestCase.public static void setIgnoreWhitespace(boolean ignore)
This method also invokes
setIgnoringElementContentWhitespace()
on the
underlying control AND test document builder factories.
Setting this parameter has no effect on whitespace inside texts
.
public static boolean getIgnoreWhitespace()
public static org.w3c.dom.Document buildControlDocument(java.lang.String fromXML) throws org.xml.sax.SAXException, java.io.IOException
fromXML
- org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document buildControlDocument(org.xml.sax.InputSource fromSource) throws java.io.IOException, org.xml.sax.SAXException
fromSource
- org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document buildTestDocument(java.lang.String fromXML) throws org.xml.sax.SAXException, java.io.IOException
fromXML
- org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document buildTestDocument(org.xml.sax.InputSource fromSource) throws java.io.IOException, org.xml.sax.SAXException
fromSource
- org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document buildDocument(javax.xml.parsers.DocumentBuilder withBuilder, java.io.Reader fromReader) throws org.xml.sax.SAXException, java.io.IOException
withBuilder
- fromReader
- org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document buildDocument(javax.xml.parsers.DocumentBuilder withBuilder, org.xml.sax.InputSource fromSource) throws java.io.IOException, org.xml.sax.SAXException
withBuilder
- fromSource
- org.xml.sax.SAXException
java.io.IOException
public static void setTransformerFactory(java.lang.String className)
public static javax.xml.transform.TransformerFactory getTransformerFactory()
static javax.xml.transform.TransformerFactory newTransformerFactory()
public static void setURIResolver(javax.xml.transform.URIResolver resolver)
public static javax.xml.transform.URIResolver getURIResolver()
public static void setSAXParserFactory(java.lang.String className)
Validator class
className
- public static void setSAXParserFactory(javax.xml.parsers.SAXParserFactory factory)
Validator class
factory
- public static javax.xml.parsers.SAXParserFactory getSAXParserFactory()
Unless an instance has been given via setSAXParserFactory
explicitly, the returned factory will be namespace aware.
Validator
to perform DTD validationprivate static java.lang.String getStripWhitespaceStylesheet()
public static Transform getStripWhitespaceTransform(org.w3c.dom.Document forDocument)
forDocument
- Transform
to do the whitespace strippingpublic static org.w3c.dom.Document getWhitespaceStrippedDocument(org.w3c.dom.Document forDoc)
Will use getStripWhitespaceTransform
unless we are operating under the
severly broken XSLTC Transformer shipping with JDK 1.5.
private static org.w3c.dom.Document stripWhiteSpaceUsingXSLT(org.w3c.dom.Document forDoc)
private static org.w3c.dom.Document stripWhiteSpaceWithoutXSLT(org.w3c.dom.Document forDoc)
private static void stripEmptyTextNodes(org.w3c.dom.Node n)
private static java.lang.String getStripCommentsStylesheet()
public static Transform getStripCommentsTransform(org.w3c.dom.Document forDocument)
forDocument
- Transform
to do the whitespace strippingpublic static java.lang.String getVersion()
public static Diff compareXML(org.xml.sax.InputSource control, org.xml.sax.InputSource test) throws org.xml.sax.SAXException, java.io.IOException
control
- Control documenttest
- Document to testorg.xml.sax.SAXException
java.io.IOException
public static Diff compareXML(java.io.Reader control, java.io.Reader test) throws org.xml.sax.SAXException, java.io.IOException
control
- Control documenttest
- Document to testorg.xml.sax.SAXException
java.io.IOException
public static Diff compareXML(java.lang.String control, java.io.Reader test) throws org.xml.sax.SAXException, java.io.IOException
control
- Control documenttest
- Document to testorg.xml.sax.SAXException
java.io.IOException
public static Diff compareXML(java.io.Reader control, java.lang.String test) throws org.xml.sax.SAXException, java.io.IOException
control
- Control documenttest
- Document to testorg.xml.sax.SAXException
java.io.IOException
public static Diff compareXML(java.lang.String control, java.lang.String test) throws org.xml.sax.SAXException, java.io.IOException
control
- Control documenttest
- Document to testorg.xml.sax.SAXException
java.io.IOException
public static Diff compareXML(org.w3c.dom.Document control, org.w3c.dom.Document test)
control
- Control documenttest
- Document to testpublic static NamespaceContext getXpathNamespaceContext()
public static void setXpathNamespaceContext(NamespaceContext ctx)
public static XpathEngine newXpathEngine()
public static void setIgnoreDiffBetweenTextAndCDATA(boolean b)
The default is false.
This also set the DocumentBuilderFactory's coalescing
flag on the factories for the control and test
document.
public static boolean getIgnoreDiffBetweenTextAndCDATA()
public static void setIgnoreComments(boolean b)
The default value is false
public static boolean getIgnoreComments()
The default value is false
public static void setNormalize(boolean b)
The default value is false
Note: if you are only working with documents read
from streams (like files or network connections) or working
with strings, there is no reason to change the default since
the XML parser is required to normalize the documents. If you
are testing Document
instances you've
created in code, you may want to alter the default
behavior.
Note2: depending on the XML parser or XSLT
transformer you use, setting ignoreWhitespace
or ignoreComments
to true may have already normalized your document and this
setting doesn't have any effect anymore.
public static boolean getNormalize()
The default value is false
public static void setNormalizeWhitespace(boolean b)
Normalized in this context means that all whitespace is replaced by the space character and adjacent whitespace characters are collapsed to a single space character. It will also trim the resulting character content on both ends.
The default value is false.
Setting this parameter has no effect on ignorable whitespace
.
public static boolean getNormalizeWhitespace()
Normalized in this context means that all whitespace is replaced by the space character and adjacent whitespace characters are collapsed to a single space character.
The default value is false.
public static void setIgnoreAttributeOrder(boolean b)
The order of attributes has never been relevant for XML documents, still XMLUnit can consider two pieces of XML not-identical (but similar) if they differ in order of attributes. Set this option to true to compare the order.
The default value is true
public static boolean getIgnoreAttributeOrder()
The order of attributes has never been relevant for XML documents, still XMLUnit can consider two pieces of XML not-identical (but similar) if they differ in order of attributes. Set this option to true to compare the order.
The default value is true
public static void setXSLTVersion(java.lang.String s)
Defaults to "1.0".
ConfigurationException
- if the argument cannot be parsed
as a positive number.public static java.lang.String getXSLTVersion()
Defaults to "1.0".
public static void setXPathFactory(java.lang.String className)
public static java.lang.String getXPathFactory()
static java.lang.String getXSLTStart()
public static void setExpandEntityReferences(boolean b)
Defaults to false.
DocumentBuilderFactory.setExpandEntityReferences(boolean)
public static boolean getExpandEntityReferences()
public static void setCompareUnmatched(boolean b)
Defaults to true.
public static boolean getCompareUnmatched()
public static void setEnableXXEProtection(boolean b)
public static boolean getEnableXXEProtection()