Class XMLUnit
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentbuildControlDocument(String fromXML) Utility method to build a Document using the control DocumentBuilder to parse the specified String.static DocumentbuildControlDocument(InputSource fromSource) Utility method to build a Document using the control DocumentBuilder and the specified InputSourcestatic DocumentbuildDocument(DocumentBuilder withBuilder, Reader fromReader) Utility method to build a Document using a specific DocumentBuilder and reading characters from a specific Reader.static DocumentbuildDocument(DocumentBuilder withBuilder, InputSource fromSource) Utility method to build a Document using a specific DocumentBuilder and a specific InputSourcestatic DocumentbuildTestDocument(String fromXML) Utility method to build a Document using the test DocumentBuilder to parse the specified String.static DocumentbuildTestDocument(InputSource fromSource) Utility method to build a Document using the test DocumentBuilder and the specified InputSourcestatic DiffcompareXML(Reader control, Reader test) Compare XML documents provided by two Reader classesstatic DiffcompareXML(Reader control, String test) Compare XML documents provided by two Reader classesstatic DiffcompareXML(String control, Reader test) Compare XML documents provided by two Reader classesstatic DiffcompareXML(String control, String test) Compare two XML documents provided as stringsstatic DiffcompareXML(Document control, Document test) Compare two XML documents provided as stringsstatic DiffcompareXML(InputSource control, InputSource test) Compare XML documents provided by two InputSource classesstatic booleanWhether unmatched control nodes should be compared to unmatched test nodes.static DocumentBuilderFactoryGet theDocumentBuilderFactoryinstance used to instantiate parsers for the control XML in an XMLTestCase.static EntityResolverObtains the EntityResolver to be added to all new control parsers.static booleanWhether XXE protection is enabled on the factories used by this class.static booleanWhether the parser shall be instructed to expand entity references.static booleanWhether to ignore the order of attributes on an element.static booleanWhether comments should be ignored.static booleanWhether CDATA sections and Text nodes should be considered the same.static booleanWhether to ignore whitespace when comparing node values.static booleanWhether Text nodes should be normalized.static booleanWhether whitespace characters inside text nodes or attributes should be "normalized".static SAXParserFactoryGet the SAX parser to use in tests.static TransformgetStripCommentsTransform(Document forDocument) Obtain the transformation that will strip comments from a DOM.static TransformgetStripWhitespaceTransform(Document forDocument) Obtain the transformation that will strip whitespace from a DOM containing empty Text nodesstatic DocumentBuilderFactoryGet theDocumentBuilderFactoryinstance used to instantiate parsers for the test XML in an XMLTestCase.static EntityResolverObtains the EntityResolver to be added to all new test parsers.static TransformerFactoryGet the transformer to use for XSLT transformations (and by implication serialization and XPaths).static URIResolverGets the URIResolver used during Transformations.static StringPlace holder for current version info.static DocumentReturns a new Document instance that is identical to the one passed in with element content whitespace removed.static StringGets the class to use as XPathFactory when using JAXP 1.3.static NamespaceContextGet the NamespaceContext to use in XPath tests.static StringThe XSLT version set on stylesheets used internally.static DocumentBuilderGet theDocumentBuilderinstance used to parse the control XML in an XMLTestCase.static DocumentBuilderGet theDocumentBuilderinstance used to parse the test XML in an XMLTestCase.static XpathEngineObtains an XpathEngine to use in XPath tests.static voidsetCompareUnmatched(boolean b) Whether to compare unmatched control nodes to unmatched test nodes.static voidOverride theDocumentBuilderFactoryused to instantiate parsers for the control XML in an XMLTestCase.static voidsetControlEntityResolver(EntityResolver resolver) Sets an EntityResolver to be added to all new control parsers.static voidsetControlParser(String className) Overide the DocumentBuilder to use to parse control documents.static voidsetEnableXXEProtection(boolean b) Whether to enable XXE protection on the factories used by this class.static voidsetExpandEntityReferences(boolean b) Whether the parser shall be instructed to expand entity references.static voidsetIgnoreAttributeOrder(boolean b) Whether to ignore the order of attributes on an element.static voidsetIgnoreComments(boolean b) Whether comments should be ignored.static voidsetIgnoreDiffBetweenTextAndCDATA(boolean b) Whether CDATA sections and Text nodes should be considered the same.static voidsetIgnoreWhitespace(boolean ignore) Whether to ignore whitespace when comparing node values.static voidsetNormalize(boolean b) Whether Text nodes should be normalized.static voidsetNormalizeWhitespace(boolean b) Whether whitespace characters inside text nodes or attributes should be "normalized".static voidsetSAXParserFactory(String className) Override the SAX parser to use in tests.static voidsetSAXParserFactory(SAXParserFactory factory) Override the SAX parser to use in tests.static voidOverride theDocumentBuilderFactoryused to instantiate parsers for the test XML in an XMLTestCase.static voidsetTestEntityResolver(EntityResolver resolver) Sets an EntityResolver to be added to all new test parsers.static voidsetTestParser(String className) Overide the DocumentBuilder to use to parser test documents.static voidsetTransformerFactory(String className) Overide the transformer to use for XSLT transformations (and by implication serialization and XPaths).static voidsetURIResolver(URIResolver resolver) Sets the URIResolver to use during transformations.static voidsetXPathFactory(String className) Sets the class to use as XPathFactory when using JAXP 1.3.static voidSet the NamespaceContext to use in XPath tests.static voidSets the XSLT version to set on stylesheets used internally.
-
Method Details
-
setControlParser
Overide the DocumentBuilder to use to parse control documents. This is useful when comparing the output of two different parsers. Note: setting the control parser before any test cases are run will affect the test parser as well.- Parameters:
className- class name of a DocumentBuilderFactory
-
newControlParser
Get theDocumentBuilderinstance used to parse the control XML in an XMLTestCase.- Returns:
- parser for control values
- Throws:
ConfigurationException- if anything is wrong
-
setTestEntityResolver
Sets an EntityResolver to be added to all new test parsers. Setting to null will reset to the default EntityResolver- Parameters:
resolver- EntityResolver
-
setControlEntityResolver
Sets an EntityResolver to be added to all new control parsers. Setting to null will reset to the default EntityResolver- Parameters:
resolver- EntityResolver
-
getControlEntityResolver
Obtains the EntityResolver to be added to all new control parsers.- Returns:
- EntityResolver to be added to all new control parsers
-
getTestEntityResolver
Obtains the EntityResolver to be added to all new test parsers.- Returns:
- EntityResolver to be added to all new test parsers
-
getControlDocumentBuilderFactory
Get theDocumentBuilderFactoryinstance used to instantiate parsers for the control XML in an XMLTestCase.- Returns:
- factory for control parsers
-
setControlDocumentBuilderFactory
Override theDocumentBuilderFactoryused to instantiate parsers for the control XML in an XMLTestCase.- Parameters:
factory- factory to use
-
setTestParser
Overide the DocumentBuilder to use to parser test documents. This is useful when comparing the output of two different parsers. Note: setting the test parser before any test cases are run will affect the control parser as well.- Parameters:
className- class name of a DocumentBuilderFactory
-
newTestParser
Get theDocumentBuilderinstance used to parse the test XML in an XMLTestCase.- Returns:
- parser for test values
- Throws:
ConfigurationException- if anything is wrong
-
getTestDocumentBuilderFactory
Get theDocumentBuilderFactoryinstance used to instantiate parsers for the test XML in an XMLTestCase.- Returns:
- factory for test parsers
-
setTestDocumentBuilderFactory
Override theDocumentBuilderFactoryused to instantiate parsers for the test XML in an XMLTestCase.- Parameters:
factory- factory to use
-
setIgnoreWhitespace
public static void setIgnoreWhitespace(boolean ignore) Whether to ignore whitespace when comparing node values.This method also invokes
setIgnoringElementContentWhitespace()on the underlying control AND test document builder factories.Setting this parameter has no effect on
whitespace inside texts.- Parameters:
ignore- whether to ignore whitespace
-
getIgnoreWhitespace
public static boolean getIgnoreWhitespace()Whether to ignore whitespace when comparing node values.- Returns:
- true if whitespace should be ignored when comparing nodes, false otherwise
-
buildControlDocument
Utility method to build a Document using the control DocumentBuilder to parse the specified String.- Parameters:
fromXML- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
buildControlDocument
public static Document buildControlDocument(InputSource fromSource) throws IOException, SAXException Utility method to build a Document using the control DocumentBuilder and the specified InputSource- Parameters:
fromSource- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
buildTestDocument
Utility method to build a Document using the test DocumentBuilder to parse the specified String.- Parameters:
fromXML- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
buildTestDocument
Utility method to build a Document using the test DocumentBuilder and the specified InputSource- Parameters:
fromSource- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
buildDocument
public static Document buildDocument(DocumentBuilder withBuilder, Reader fromReader) throws SAXException, IOException Utility method to build a Document using a specific DocumentBuilder and reading characters from a specific Reader.- Parameters:
withBuilder- DocumentBuilder to usefromReader- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
buildDocument
public static Document buildDocument(DocumentBuilder withBuilder, InputSource fromSource) throws IOException, SAXException Utility method to build a Document using a specific DocumentBuilder and a specific InputSource- Parameters:
withBuilder- DocumentBuilder to usefromSource- the XML to parse- Returns:
- Document parsed document
- Throws:
SAXException- if the parser says soIOException- on I/O errors
-
setTransformerFactory
Overide the transformer to use for XSLT transformations (and by implication serialization and XPaths). This is useful when comparing transformer implementations.- Parameters:
className- name of a TransformerFactory class
-
getTransformerFactory
Get the transformer to use for XSLT transformations (and by implication serialization and XPaths).- Returns:
- the current transformer factory in use a new instance of the default transformer factory
-
setURIResolver
Sets the URIResolver to use during transformations.- Parameters:
resolver- URIResolver
-
getURIResolver
Gets the URIResolver used during Transformations.- Returns:
- URIResolver used during Transformations
-
setSAXParserFactory
Override the SAX parser to use in tests. Currently only used byValidator class- Parameters:
className- name of a SAXParserFactory
-
setSAXParserFactory
Override the SAX parser to use in tests. Currently only used byValidator class- Parameters:
factory- factory to use
-
getSAXParserFactory
Get the SAX parser to use in tests.Unless an instance has been given via
setSAXParserFactoryexplicitly, the returned factory will be namespace aware.- Returns:
- the SAXParserFactory instance used by the
Validatorto perform DTD validation
-
getStripWhitespaceTransform
Obtain the transformation that will strip whitespace from a DOM containing empty Text nodes- Parameters:
forDocument- document to apply transformation to- Returns:
- a
Transformto do the whitespace stripping
-
getWhitespaceStrippedDocument
Returns a new Document instance that is identical to the one passed in with element content whitespace removed.Will use
getStripWhitespaceTransformunless we are operating under the severly broken XSLTC Transformer shipping with JDK 1.5.- Parameters:
forDoc- document to apply transformation to- Returns:
- document without any element content whitespace
-
getStripCommentsTransform
Obtain the transformation that will strip comments from a DOM.- Parameters:
forDocument- document to apply transformation to- Returns:
- a
Transformto do the whitespace stripping
-
getVersion
Place holder for current version info.- Returns:
- current version
-
compareXML
public static Diff compareXML(InputSource control, InputSource test) throws SAXException, IOException Compare XML documents provided by two InputSource classes- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
- Throws:
SAXException- if parsing failsIOException- on I/O errors
-
compareXML
Compare XML documents provided by two Reader classes- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
- Throws:
SAXException- if parsing failsIOException- on I/O errors
-
compareXML
Compare XML documents provided by two Reader classes- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
- Throws:
SAXException- if parsing failsIOException- on I/O errors
-
compareXML
Compare XML documents provided by two Reader classes- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
- Throws:
SAXException- if parsing failsIOException- on I/O errors
-
compareXML
Compare two XML documents provided as strings- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
- Throws:
SAXException- if parsing failsIOException- on I/O errors
-
compareXML
Compare two XML documents provided as strings- Parameters:
control- Control documenttest- Document to test- Returns:
- Diff object describing differences in documents
-
getXpathNamespaceContext
Get the NamespaceContext to use in XPath tests.- Returns:
- NamespaceContext to use in XPath tests
-
setXpathNamespaceContext
Set the NamespaceContext to use in XPath tests.- Parameters:
ctx- NamespaceContext to use in XPath tests
-
newXpathEngine
Obtains an XpathEngine to use in XPath tests.- Returns:
- XpathEngine
-
setIgnoreDiffBetweenTextAndCDATA
public static void setIgnoreDiffBetweenTextAndCDATA(boolean b) Whether CDATA sections and Text nodes should be considered the same.The default is false.
This also set the DocumentBuilderFactory's
coalescingflag on the factories for the control and test document.- Parameters:
b- whether CDATA sections and Text nodes should be considered the same
-
getIgnoreDiffBetweenTextAndCDATA
public static boolean getIgnoreDiffBetweenTextAndCDATA()Whether CDATA sections and Text nodes should be considered the same.- Returns:
- false by default
-
setIgnoreComments
public static void setIgnoreComments(boolean b) Whether comments should be ignored.The default value is false
- Parameters:
b- whether comments are ignored
-
getIgnoreComments
public static boolean getIgnoreComments()Whether comments should be ignored.The default value is false
- Returns:
- whether comments are ignored
-
setNormalize
public static void setNormalize(boolean b) Whether Text nodes should be normalized.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
Documentinstances 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
ignoreWhitespaceorignoreCommentsto true may have already normalized your document and this setting doesn't have any effect anymore.- Parameters:
b- whether Text nodes should be normalizedd
-
getNormalize
public static boolean getNormalize()Whether Text nodes should be normalized.The default value is false
- Returns:
- whether Text nodes should be normalizedd
-
setNormalizeWhitespace
public static void setNormalizeWhitespace(boolean b) Whether whitespace characters inside text nodes or attributes should be "normalized".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.- Parameters:
b- whether whitespace characters inside text nodes or attributes should be normalizedd
-
getNormalizeWhitespace
public static boolean getNormalizeWhitespace()Whether whitespace characters inside text nodes or attributes should be "normalized".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.
- Returns:
- whether whitespace characters inside text nodes or attributes should be normalizedd
-
setIgnoreAttributeOrder
public static void setIgnoreAttributeOrder(boolean b) Whether to ignore the order of attributes on an element.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
- Parameters:
b- whether to ignore the order of attributes on an element
-
getIgnoreAttributeOrder
public static boolean getIgnoreAttributeOrder()Whether to ignore the order of attributes on an element.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
- Returns:
- whether to ignore the order of attributes on an element
-
setXSLTVersion
Sets the XSLT version to set on stylesheets used internally.Defaults to "1.0".
- Parameters:
s- XSLT version to set on stylesheets- Throws:
ConfigurationException- if the argument cannot be parsed as a positive number.
-
getXSLTVersion
The XSLT version set on stylesheets used internally.Defaults to "1.0".
- Returns:
- XSLT version to set on stylesheets
-
setXPathFactory
Sets the class to use as XPathFactory when using JAXP 1.3.- Parameters:
className- name of an XPathFactory
-
getXPathFactory
Gets the class to use as XPathFactory when using JAXP 1.3.- Returns:
- name of the XPathFactory
-
setExpandEntityReferences
public static void setExpandEntityReferences(boolean b) Whether the parser shall be instructed to expand entity references.Defaults to false.
- Parameters:
b- whether the parser shall be instructed to expand entity references- See Also:
-
getExpandEntityReferences
public static boolean getExpandEntityReferences()Whether the parser shall be instructed to expand entity references.- Returns:
- whether the parser shall be instructed to expand entity references
-
setCompareUnmatched
public static void setCompareUnmatched(boolean b) Whether to compare unmatched control nodes to unmatched test nodes.Defaults to true.
- Parameters:
b- whether to compare unmatched control nodes to unmatched test nodes
-
getCompareUnmatched
public static boolean getCompareUnmatched()Whether unmatched control nodes should be compared to unmatched test nodes.- Returns:
- true by default
-
setEnableXXEProtection
public static void setEnableXXEProtection(boolean b) Whether to enable XXE protection on the factories used by this class.- Parameters:
b- whether to enable XXE protection on the factories used by this class- Since:
- XMLUnit 2.6.0
- See Also:
-
- "https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet"
-
getEnableXXEProtection
public static boolean getEnableXXEProtection()Whether XXE protection is enabled on the factories used by this class.- Returns:
- whether to enable XXE protection on the factories used by this class
- Since:
- XMLUnit 2.6.0
- See Also:
-
- "https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet"
-