public class DiffBuilder extends Object implements 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());
| Modifier and Type | Method and Description |
|---|---|
Diff |
build()
Compare the Test-XML
withTest(Object) with the Control-XML compare(Object) and return the
collected differences in a Diff object. |
DiffBuilder |
checkForIdentical()
check test source with the control source for identically.
|
DiffBuilder |
checkForSimilar()
check test source with the control source for similarity.
|
static DiffBuilder |
compare(Object control)
Create a DiffBuilder from all kind of types supported by
Input.from(Object). |
DiffBuilder |
ignoreComments()
Will remove all comment-Tags "<!
|
DiffBuilder |
ignoreCommentsUsingXSLTVersion(String xsltVersion)
Will remove all comment-Tags "<!
|
DiffBuilder |
ignoreElementContentWhitespace()
Ignore element content whitespace by removing all text nodes solely consisting of whitespace.
|
DiffBuilder |
ignoreWhitespace()
Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.
|
DiffBuilder |
normalizeWhitespace()
Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.
|
DiffBuilder |
withAttributeFilter(Predicate<Attr> attributeFilter)
Registers a filter for attributes.
|
DiffBuilder |
withComparisonController(ComparisonController comparisonController)
Replace the
ComparisonControllers.Default with your own ComparisonController. |
DiffBuilder |
withComparisonFormatter(ComparisonFormatter formatter)
Sets a non-default formatter for the differences found.
|
DiffBuilder |
withComparisonListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison.
|
DiffBuilder |
withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
Provide your own custom
DifferenceEvaluator implementation. |
DiffBuilder |
withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison with
outcome other than
ComparisonResult.EQUAL. |
DiffBuilder |
withDocumentBuilderFactory(DocumentBuilderFactory f)
|
DiffBuilder |
withNamespaceContext(Map<String,String> prefix2Uri)
Establish a namespace context that will be used in
Comparison.Detail#getXPath. |
DiffBuilder |
withNodeFilter(Predicate<Node> nodeFilter)
Registers a filter for nodes.
|
DiffBuilder |
withNodeMatcher(NodeMatcher nodeMatcher)
Sets the strategy for selecting nodes to compare.
|
DiffBuilder |
withTest(Object test)
Set the Test-Source from all kind of types supported by
Input.from(Object). |
public static DiffBuilder compare(Object control)
Input.from(Object).control - the expected reference document.public DiffBuilder withTest(Object test)
Input.from(Object).test - the test document which must be compared with the control document.public DiffBuilder ignoreWhitespace()
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.
public DiffBuilder normalizeWhitespace()
"normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed.
public DiffBuilder ignoreElementContentWhitespace()
public DiffBuilder ignoreComments()
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 the Source using a transformation
yourself, using CommentLessSource.STYLE.
public DiffBuilder ignoreCommentsUsingXSLTVersion(String xsltVersion)
Comments are ignored by applying an XSLT transformation on
the source which may reduce the effect of withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory). This uses the CommentLessSource constructor with two arguments using xsltVersion as second argument.
xsltVersion - use this version for the stylesheetpublic DiffBuilder withNodeMatcher(NodeMatcher nodeMatcher)
Example with DefaultNodeMatcher:
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
This overwrites any NodeMatcher set via earlier invocations of withNodeMatcher.
withNodeMatcher in interface DifferenceEngineConfigurer<DiffBuilder>nodeMatcher - the NodeMatcher to useDifferenceEngine.setNodeMatcher(NodeMatcher)public DiffBuilder withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
DifferenceEvaluator implementation.
This overwrites the Default DifferenceEvaluator.
If you want use your custom DifferenceEvaluator in combination with the default or another DifferenceEvaluator
you should use DifferenceEvaluators.chain(DifferenceEvaluator...)
or DifferenceEvaluators.first(DifferenceEvaluator...) to combine them:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.withDifferenceEvaluator(
DifferenceEvaluators.chain(
DifferenceEvaluators.Default,
new MyCustomDifferenceEvaluator()))
....
.build();
This overwrites any DifferenceEvaluator set via earlier invocations of withDifferenceEvaluator.
withDifferenceEvaluator in interface DifferenceEngineConfigurer<DiffBuilder>differenceEvaluator - the DifferenceEvaluator to usepublic DiffBuilder withComparisonController(ComparisonController comparisonController)
ComparisonControllers.Default with your own ComparisonController.
Example use:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.withComparisonController(ComparisonControllers.StopWhenDifferent)
.build();
This overwrites any ComparisonController set via earlier invocations of withComparisonController.
withComparisonController in interface DifferenceEngineConfigurer<DiffBuilder>comparisonController - ComparisonController to usepublic DiffBuilder withComparisonListeners(ComparisonListener... comparisonListeners)
This overwrites any ComparisonListeners set via earlier invocations of withComparisonListeners.
withComparisonListeners in interface DifferenceEngineConfigurer<DiffBuilder>comparisonListeners - ComparisonListeners to useDifferenceEngine.addComparisonListener(ComparisonListener)public DiffBuilder withDifferenceListeners(ComparisonListener... comparisonListeners)
ComparisonResult.EQUAL.
This overwrites any ComparisonListeners set via earlier invocations of withDifferenceListeners.
withDifferenceListeners in interface DifferenceEngineConfigurer<DiffBuilder>comparisonListeners - ComparisonListeners to useDifferenceEngine.addDifferenceListener(ComparisonListener)public DiffBuilder checkForSimilar()
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.
See withDifferenceEvaluator(DifferenceEvaluator)
Default is checkForIdentical().
public DiffBuilder checkForIdentical()
This is the Default.
public DiffBuilder withNamespaceContext(Map<String,String> prefix2Uri)
Comparison.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 Map set via earlier invocations of withNamespaceContext.
withNamespaceContext in interface DifferenceEngineConfigurer<DiffBuilder>prefix2Uri - mapping between prefix and namespace URIpublic DiffBuilder withAttributeFilter(Predicate<Attr> attributeFilter)
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 Predicate set via earlier invocations of withAttributeFilter.
withAttributeFilter in interface DifferenceEngineConfigurer<DiffBuilder>attributeFilter - attribute filter to usepublic DiffBuilder withNodeFilter(Predicate<Node> nodeFilter)
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 Predicate set via earlier invocations of withNodeFilter.
withNodeFilter in interface DifferenceEngineConfigurer<DiffBuilder>nodeFilter - node filter to usepublic DiffBuilder withComparisonFormatter(ComparisonFormatter formatter)
This overwrites any ComparisonFormatter set via earlier invocations of withComparisonFormatter.
withComparisonFormatter in interface DifferenceEngineConfigurer<DiffBuilder>formatter - formatter to usepublic DiffBuilder withDocumentBuilderFactory(DocumentBuilderFactory f)
DocumentBuilderFactory to use when creating a
Document from the Sources to compare.
This is only used if the Sources used for control
and test not already are DOMSources.
f - the DocumentBuilderFactory to usepublic Diff build()
withTest(Object) with the Control-XML compare(Object) and return the
collected differences in a Diff object.Copyright © 2001–2025 XMLUnit. All rights reserved.