Class DiffBuilder
- java.lang.Object
-
- org.xmlunit.builder.DiffBuilder
-
- All Implemented Interfaces:
DifferenceEngineConfigurer<DiffBuilder>
public class DiffBuilder extends Object implements DifferenceEngineConfigurer<DiffBuilder>
DiffBuilder to create aDiffinstance.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Diffbuild()Compare the Test-XMLwithTest(Object)with the Control-XMLcompare(Object)and return the collected differences in aDiffobject.DiffBuildercheckForIdentical()check test source with the control source for identically.DiffBuildercheckForSimilar()check test source with the control source for similarity.static DiffBuildercompare(Object control)Create a DiffBuilder from all kind of types supported byInput.from(Object).DiffBuilderignoreComments()Will remove all comment-Tags "<!DiffBuilderignoreCommentsUsingXSLTVersion(String xsltVersion)Will remove all comment-Tags "<!DiffBuilderignoreElementContentWhitespace()Ignore element content whitespace by removing all text nodes solely consisting of whitespace.DiffBuilderignoreWhitespace()Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.DiffBuildernormalizeWhitespace()Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.DiffBuilderwithAttributeFilter(Predicate<Attr> attributeFilter)Registers a filter for attributes.DiffBuilderwithComparisonController(ComparisonController comparisonController)Replace theComparisonControllers.Defaultwith your own ComparisonController.DiffBuilderwithComparisonFormatter(ComparisonFormatter formatter)Sets a non-default formatter for the differences found.DiffBuilderwithComparisonListeners(ComparisonListener... comparisonListeners)Registers listeners that are notified of each comparison.DiffBuilderwithDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)Provide your own customDifferenceEvaluatorimplementation.DiffBuilderwithDifferenceListeners(ComparisonListener... comparisonListeners)Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.DiffBuilderwithDocumentBuilderFactory(DocumentBuilderFactory f)DiffBuilderwithNamespaceContext(Map<String,String> prefix2Uri)Establish a namespace context that will be used inComparison.Detail#getXPath.DiffBuilderwithNodeFilter(Predicate<Node> nodeFilter)Registers a filter for nodes.DiffBuilderwithNodeMatcher(NodeMatcher nodeMatcher)Sets the strategy for selecting nodes to compare.DiffBuilderwithTest(Object test)Set the Test-Source from all kind of types supported byInput.from(Object).
-
-
-
Method Detail
-
compare
public static DiffBuilder compare(Object control)
Create a DiffBuilder from all kind of types supported byInput.from(Object).- Parameters:
control- the expected reference document.- Returns:
- a new builder
-
withTest
public DiffBuilder withTest(Object test)
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
public DiffBuilder 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
public DiffBuilder 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
public DiffBuilder ignoreElementContentWhitespace()
Ignore element content whitespace by removing all text nodes solely consisting of whitespace.- Returns:
- this
- Since:
- XMLUnit 2.6.0
-
ignoreComments
public DiffBuilder 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
public DiffBuilder ignoreCommentsUsingXSLTVersion(String xsltVersion)
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
public DiffBuilder withNodeMatcher(NodeMatcher nodeMatcher)
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:
DifferenceEngine.setNodeMatcher(NodeMatcher)
-
withDifferenceEvaluator
public DiffBuilder withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
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
public DiffBuilder withComparisonController(ComparisonController comparisonController)
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
public DiffBuilder withComparisonListeners(ComparisonListener... comparisonListeners)
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:
DifferenceEngine.addComparisonListener(ComparisonListener)
-
withDifferenceListeners
public DiffBuilder withDifferenceListeners(ComparisonListener... comparisonListeners)
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:
DifferenceEngine.addDifferenceListener(ComparisonListener)
-
checkForSimilar
public DiffBuilder 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
public DiffBuilder checkForIdentical()
check test source with the control source for identically.This is the Default.
- Returns:
- this
-
withNamespaceContext
public DiffBuilder withNamespaceContext(Map<String,String> prefix2Uri)
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
public DiffBuilder withAttributeFilter(Predicate<Attr> attributeFilter)
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
public DiffBuilder withNodeFilter(Predicate<Node> nodeFilter)
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
public DiffBuilder withComparisonFormatter(ComparisonFormatter formatter)
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
public DiffBuilder withDocumentBuilderFactory(DocumentBuilderFactory f)
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
public Diff build()
Compare the Test-XMLwithTest(Object)with the Control-XMLcompare(Object)and return the collected differences in aDiffobject.- Returns:
- the collected differences
-
-