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());
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ComparisonResult[]private static final ComparisonResult[]private ComparisonControllerprivate List<ComparisonListener>private ComparisonResult[]private final Sourceprivate DifferenceEvaluatorprivate List<ComparisonListener>private DocumentBuilderFactoryprivate ComparisonFormatterprivate booleanprivate Stringprivate booleanprivate booleanprivate NodeMatcherprivate booleanprivate Source -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDiffBuilder(Source controlSource) Create a DiffBuilder instance. -
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).private static SourceWill remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.ignoreCommentsUsingXSLTVersion(String xsltVersion) Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.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).private Source
-
Field Details
-
CHECK_FOR_SIMILAR
-
CHECK_FOR_IDENTICAL
-
controlSource
-
testSource
-
nodeMatcher
-
comparisonController
-
differenceEvaluator
-
comparisonListeners
-
differenceListeners
-
comparisonResultsToCheck
-
namespaceContext
-
attributeFilter
-
nodeFilter
-
formatter
-
ignoreWhitespace
private boolean ignoreWhitespace -
normalizeWhitespace
private boolean normalizeWhitespace -
ignoreECW
private boolean ignoreECW -
ignoreComments
private boolean ignoreComments -
ignoreCommentVersion
-
documentBuilderFactory
-
-
Constructor Details
-
DiffBuilder
Create a DiffBuilder instance.- Parameters:
controlSource- the expected reference Result.- See Also:
-
-
Method Details
-
compare
Create a DiffBuilder from all kind of types supported byInput.from(Object).- Parameters:
control- the expected reference document.- See Also:
-
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.
-
getSource
-
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. -
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.
-
ignoreElementContentWhitespace
Ignore element content whitespace by removing all text nodes solely consisting of whitespace.- 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. -
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- Since:
- XMLUnit 2.5.0
-
withNodeMatcher
Sets the strategy for selecting nodes to compare.Example with
DefaultNodeMatcher:.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
- Specified by:
withNodeMatcherin interfaceDifferenceEngineConfigurer<DiffBuilder>- 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();- Specified by:
withDifferenceEvaluatorin interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonController
Replace theComparisonControllers.Defaultwith your own ComparisonController.Example use:
Diff myDiff = DiffBuilder.compare(control).withTest(test) .withComparisonController(ComparisonControllers.StopWhenDifferent) .build();- Specified by:
withComparisonControllerin interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonListeners
Registers listeners that are notified of each comparison.- Specified by:
withComparisonListenersin interfaceDifferenceEngineConfigurer<DiffBuilder>- See Also:
-
withDifferenceListeners
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.- Specified by:
withDifferenceListenersin interfaceDifferenceEngineConfigurer<DiffBuilder>- 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(). -
checkForIdentical
check test source with the control source for identically.This is the Default.
-
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.
- Specified by:
withNamespaceContextin interfaceDifferenceEngineConfigurer<DiffBuilder>- Parameters:
prefix2Uri- mapping between prefix and namespace URI
-
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.- Specified by:
withAttributeFilterin interfaceDifferenceEngineConfigurer<DiffBuilder>
-
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.
- Specified by:
withNodeFilterin interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonFormatter
Sets a non-default formatter for the differences found.- Specified by:
withComparisonFormatterin interfaceDifferenceEngineConfigurer<DiffBuilder>
-
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.- Since:
- XMLUnit 2.2.0
-
build
Compare the Test-XMLwithTest(Object)with the Control-XMLcompare(Object)and return the collected differences in aDiffobject. -
wrap
-