public class DiffBuilder extends Object
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 |
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 a listener that is notified of each comparison.
|
DiffBuilder |
withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
Provide your own custom
DifferenceEvaluator implementation. |
DiffBuilder |
withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers a listener that is 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.DiffBuilder
public DiffBuilder withTest(Object test)
Input.from(Object)
.test
- the test document which must be compared with the control document.public DiffBuilder ignoreWhitespace()
public DiffBuilder normalizeWhitespace()
"normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed.
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))
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();
public DiffBuilder withComparisonController(ComparisonController comparisonController)
ComparisonControllers.Default
with your own ComparisonController.
Example use:
Diff myDiff = DiffBuilder.compare(control).withTest(test) .withComparisonController(ComparisonControllers.StopWhenDifferent) .build();
public DiffBuilder withComparisonListeners(ComparisonListener... comparisonListeners)
public DiffBuilder withDifferenceListeners(ComparisonListener... comparisonListeners)
ComparisonResult.EQUAL
.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.
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
.
public 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.
public DiffBuilder withComparisonFormatter(ComparisonFormatter formatter)
public DiffBuilder withDocumentBuilderFactory(DocumentBuilderFactory f)
DocumentBuilderFactory
to use when creating a
Document
from the Source
s to compare.
This is only used if the Source
s used for control
and test not already are DOMSource
s.
public Diff build()
withTest(Object)
with the Control-XML compare(Object)
and return the
collected differences in a Diff
object.Copyright © 2001–2017 XMLUnit. All rights reserved.