Class DiffBuilder
- All Implemented Interfaces:
DifferenceEngineConfigurer<DiffBuilder>
Diff instance.
Valid inputs for control and test are all Objects supported by Input.from(Object).
Example Usage:
String controlXml = "<a><b>Test Value</b></a>";
String testXml = "<a>\n <b>\n Test Value\n </b>\n</a>";
Diff myDiff = DiffBuilder.compare(Input.fromString(controlXml)).withTest(Input.fromString(testXml))
.checkForSimilar()
.ignoreWhitespace()
.build();
assertFalse("XML similar " + myDiff.toString(), myDiff.hasDifferences());
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Compare the Test-XMLwithTest(Object)with the Control-XMLcompare(Object)and return the collected differences in aDiffobject.check test source with the control source for identically.check test source with the control source for similarity.static DiffBuilderCreate a DiffBuilder from all kind of types supported byInput.from(Object).Will remove all comment-Tags "<!ignoreCommentsUsingXSLTVersion(String xsltVersion) Will remove all comment-Tags "<!Ignore element content whitespace by removing all text nodes solely consisting of whitespace.Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.withAttributeFilter(Predicate<Attr> attributeFilter) Registers a filter for attributes.withComparisonController(ComparisonController comparisonController) Replace theComparisonControllers.Defaultwith your own ComparisonController.withComparisonFormatter(ComparisonFormatter formatter) Sets a non-default formatter for the differences found.withComparisonListeners(ComparisonListener... comparisonListeners) Registers listeners that are notified of each comparison.withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator) Provide your own customDifferenceEvaluatorimplementation.withDifferenceListeners(ComparisonListener... comparisonListeners) Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.withNamespaceContext(Map<String, String> prefix2Uri) Establish a namespace context that will be used inComparison.Detail#getXPath.withNodeFilter(Predicate<Node> nodeFilter) Registers a filter for nodes.withNodeMatcher(NodeMatcher nodeMatcher) Sets the strategy for selecting nodes to compare.Set the Test-Source from all kind of types supported byInput.from(Object).
-
Method Details
-
compare
Create a DiffBuilder from all kind of types supported byInput.from(Object).- Parameters:
control- the expected reference document.- Returns:
- a new builder
-
withTest
Set the Test-Source from all kind of types supported byInput.from(Object).- Parameters:
test- the test document which must be compared with the control document.- Returns:
- this
-
ignoreWhitespace
Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.If you only want to remove text nodes consisting solely of whitespace (AKA element content whitespace) but leave all other text nodes alone you should use
ignoreElementContentWhitespace()instead.- Returns:
- this
-
normalizeWhitespace
Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones."normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed.
- Returns:
- this
-
ignoreElementContentWhitespace
Ignore element content whitespace by removing all text nodes solely consisting of whitespace.- Returns:
- this
- Since:
- XMLUnit 2.6.0
-
ignoreComments
Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.Comments are ignored by applying an XSLT transformation on the source which may reduce the effect of
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory). If you need more control over the transformation build theSourceusing a transformation yourself, usingCommentLessSource.STYLE.- Returns:
- this
-
ignoreCommentsUsingXSLTVersion
Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.Comments are ignored by applying an XSLT transformation on the source which may reduce the effect of
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory). This uses theCommentLessSourceconstructor with two arguments usingxsltVersionas second argument.- Parameters:
xsltVersion- use this version for the stylesheet- Returns:
- this
- Since:
- XMLUnit 2.5.0
-
withNodeMatcher
Sets the strategy for selecting nodes to compare.Example with
DefaultNodeMatcher:.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
This overwrites any
NodeMatcherset via earlier invocations ofwithNodeMatcher.- Specified by:
withNodeMatcherin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
nodeMatcher- the NodeMatcher to use- Returns:
- this
- See Also:
-
withDifferenceEvaluator
Provide your own customDifferenceEvaluatorimplementation. This overwrites the Default DifferenceEvaluator. If you want use your custom DifferenceEvaluator in combination with the default or another DifferenceEvaluator you should useDifferenceEvaluators.chain(DifferenceEvaluator...)orDifferenceEvaluators.first(DifferenceEvaluator...)to combine them:Diff myDiff = DiffBuilder.compare(control).withTest(test) .withDifferenceEvaluator( DifferenceEvaluators.chain( DifferenceEvaluators.Default, new MyCustomDifferenceEvaluator())) .... .build();This overwrites any
DifferenceEvaluatorset via earlier invocations ofwithDifferenceEvaluator.- Specified by:
withDifferenceEvaluatorin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
differenceEvaluator- the DifferenceEvaluator to use- Returns:
- this
-
withComparisonController
Replace theComparisonControllers.Defaultwith your own ComparisonController.Example use:
Diff myDiff = DiffBuilder.compare(control).withTest(test) .withComparisonController(ComparisonControllers.StopWhenDifferent) .build();This overwrites any
ComparisonControllerset via earlier invocations ofwithComparisonController.- Specified by:
withComparisonControllerin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
comparisonController- ComparisonController to use- Returns:
- this
-
withComparisonListeners
Registers listeners that are notified of each comparison.This overwrites any
ComparisonListeners set via earlier invocations ofwithComparisonListeners.- Specified by:
withComparisonListenersin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
comparisonListeners- ComparisonListeners to use- Returns:
- this
- See Also:
-
withDifferenceListeners
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.This overwrites any
ComparisonListeners set via earlier invocations ofwithDifferenceListeners.- Specified by:
withDifferenceListenersin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
comparisonListeners- ComparisonListeners to use- Returns:
- this
- See Also:
-
checkForSimilar
check test source with the control source for similarity.Example for Similar: The XML node "<a>Text</a>" and "<a><![CDATA[Text]]></a>" are similar and the Test will not fail.
The rating, if a node is similar, will be done by the
DifferenceEvaluators.Default. SeewithDifferenceEvaluator(DifferenceEvaluator)Default is
checkForIdentical().- Returns:
- this
-
checkForIdentical
check test source with the control source for identically.This is the Default.
- Returns:
- this
-
withNamespaceContext
Establish a namespace context that will be used inComparison.Detail#getXPath.Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.
This overwrites any
Mapset via earlier invocations ofwithNamespaceContext.- Specified by:
withNamespaceContextin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
prefix2Uri- mapping between prefix and namespace URI- Returns:
- this
-
withAttributeFilter
Registers a filter for attributes.Only attributes for which the predicate returns true are part of the comparison. By default all attributes are considered.
The "special" namespace, namespace-location and schema-instance-type attributes can not be ignored this way. If you want to suppress comparison of them you'll need to implement
DifferenceEvaluator.This overwrites any
Predicateset via earlier invocations ofwithAttributeFilter.- Specified by:
withAttributeFilterin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
attributeFilter- attribute filter to use- Returns:
- this
-
withNodeFilter
Registers a filter for nodes.Only nodes for which the predicate returns true are part of the comparison. By default nodes that are not document types are considered.
This overwrites any
Predicateset via earlier invocations ofwithNodeFilter.- Specified by:
withNodeFilterin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
nodeFilter- node filter to use- Returns:
- this
-
withComparisonFormatter
Sets a non-default formatter for the differences found.This overwrites any
ComparisonFormatterset via earlier invocations ofwithComparisonFormatter.- Specified by:
withComparisonFormatterin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
formatter- formatter to use- Returns:
- this
-
withDocumentBuilderFactory
Sets theDocumentBuilderFactoryto use when creating aDocumentfrom theSources to compare.This is only used if the
Sources used for control and test not already areDOMSources.- Parameters:
f- the DocumentBuilderFactory to use- Returns:
- this
- Since:
- XMLUnit 2.2.0
-
build
Compare the Test-XMLwithTest(Object)with the Control-XMLcompare(Object)and return the collected differences in aDiffobject.- Returns:
- the collected differences
-