Class DiffBuilder
- java.lang.Object
-
- org.xmlunit.builder.DiffBuilder
-
- All Implemented Interfaces:
DifferenceEngineConfigurer<DiffBuilder>
public class DiffBuilder extends java.lang.Object implements DifferenceEngineConfigurer<DiffBuilder>
DiffBuilder to create aDiff
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 Modifier and Type Class Description private static class
DiffBuilder.CollectResultsListener
-
Field Summary
Fields Modifier and Type Field Description private Predicate<org.w3c.dom.Attr>
attributeFilter
private static ComparisonResult[]
CHECK_FOR_IDENTICAL
private static ComparisonResult[]
CHECK_FOR_SIMILAR
private ComparisonController
comparisonController
private java.util.List<ComparisonListener>
comparisonListeners
private ComparisonResult[]
comparisonResultsToCheck
private javax.xml.transform.Source
controlSource
private DifferenceEvaluator
differenceEvaluator
private java.util.List<ComparisonListener>
differenceListeners
private javax.xml.parsers.DocumentBuilderFactory
documentBuilderFactory
private ComparisonFormatter
formatter
private boolean
ignoreComments
private java.lang.String
ignoreCommentVersion
private boolean
ignoreECW
private boolean
ignoreWhitespace
private java.util.Map<java.lang.String,java.lang.String>
namespaceContext
private Predicate<org.w3c.dom.Node>
nodeFilter
private NodeMatcher
nodeMatcher
private boolean
normalizeWhitespace
private javax.xml.transform.Source
testSource
-
Constructor Summary
Constructors Modifier Constructor Description private
DiffBuilder(javax.xml.transform.Source controlSource)
Create a DiffBuilder instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Diff
build()
Compare the Test-XMLwithTest(Object)
with the Control-XMLcompare(Object)
and return the collected differences in aDiff
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(java.lang.Object control)
Create a DiffBuilder from all kind of types supported byInput.from(Object)
.private static javax.xml.transform.Source
getSource(java.lang.Object object)
DiffBuilder
ignoreComments()
Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.DiffBuilder
ignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)
Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.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<org.w3c.dom.Attr> attributeFilter)
Registers a filter for attributes.DiffBuilder
withComparisonController(ComparisonController comparisonController)
Replace theComparisonControllers.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 customDifferenceEvaluator
implementation.DiffBuilder
withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL
.DiffBuilder
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets theDocumentBuilderFactory
to use when creating aDocument
from theSource
s to compare.DiffBuilder
withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Establish a namespace context that will be used inComparison.Detail#getXPath
.DiffBuilder
withNodeFilter(Predicate<org.w3c.dom.Node> nodeFilter)
Registers a filter for nodes.DiffBuilder
withNodeMatcher(NodeMatcher nodeMatcher)
Sets the strategy for selecting nodes to compare.DiffBuilder
withTest(java.lang.Object test)
Set the Test-Source from all kind of types supported byInput.from(Object)
.private javax.xml.transform.Source
wrap(javax.xml.transform.Source source)
-
-
-
Field Detail
-
CHECK_FOR_SIMILAR
private static final ComparisonResult[] CHECK_FOR_SIMILAR
-
CHECK_FOR_IDENTICAL
private static final ComparisonResult[] CHECK_FOR_IDENTICAL
-
controlSource
private final javax.xml.transform.Source controlSource
-
testSource
private javax.xml.transform.Source testSource
-
nodeMatcher
private NodeMatcher nodeMatcher
-
comparisonController
private ComparisonController comparisonController
-
differenceEvaluator
private DifferenceEvaluator differenceEvaluator
-
comparisonListeners
private java.util.List<ComparisonListener> comparisonListeners
-
differenceListeners
private java.util.List<ComparisonListener> differenceListeners
-
comparisonResultsToCheck
private ComparisonResult[] comparisonResultsToCheck
-
namespaceContext
private java.util.Map<java.lang.String,java.lang.String> namespaceContext
-
attributeFilter
private Predicate<org.w3c.dom.Attr> attributeFilter
-
nodeFilter
private Predicate<org.w3c.dom.Node> nodeFilter
-
formatter
private ComparisonFormatter formatter
-
ignoreWhitespace
private boolean ignoreWhitespace
-
normalizeWhitespace
private boolean normalizeWhitespace
-
ignoreECW
private boolean ignoreECW
-
ignoreComments
private boolean ignoreComments
-
ignoreCommentVersion
private java.lang.String ignoreCommentVersion
-
documentBuilderFactory
private javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory
-
-
Constructor Detail
-
DiffBuilder
private DiffBuilder(javax.xml.transform.Source controlSource)
Create a DiffBuilder instance.- Parameters:
controlSource
- the expected reference Result.- See Also:
DiffBuilder
-
-
Method Detail
-
compare
public static DiffBuilder compare(java.lang.Object control)
Create a DiffBuilder from all kind of types supported byInput.from(Object)
.- Parameters:
control
- the expected reference document.- See Also:
DiffBuilder
-
withTest
public DiffBuilder withTest(java.lang.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.
-
getSource
private static javax.xml.transform.Source getSource(java.lang.Object object)
-
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.
-
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.
-
ignoreElementContentWhitespace
public DiffBuilder ignoreElementContentWhitespace()
Ignore element content whitespace by removing all text nodes solely consisting of whitespace.- 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 theSource
using a transformation yourself, usingCommentLessSource.STYLE
.
-
ignoreCommentsUsingXSLTVersion
public DiffBuilder ignoreCommentsUsingXSLTVersion(java.lang.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 theCommentLessSource
constructor with two arguments usingxsltVersion
as second argument.- Parameters:
xsltVersion
- use this version for the stylesheet- 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))
- Specified by:
withNodeMatcher
in interfaceDifferenceEngineConfigurer<DiffBuilder>
- See Also:
DifferenceEngine.setNodeMatcher(NodeMatcher)
-
withDifferenceEvaluator
public DiffBuilder withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
Provide your own customDifferenceEvaluator
implementation. 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:
withDifferenceEvaluator
in interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonController
public DiffBuilder withComparisonController(ComparisonController comparisonController)
Replace theComparisonControllers.Default
with your own ComparisonController.Example use:
Diff myDiff = DiffBuilder.compare(control).withTest(test) .withComparisonController(ComparisonControllers.StopWhenDifferent) .build();
- Specified by:
withComparisonController
in interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonListeners
public DiffBuilder withComparisonListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison.- Specified by:
withComparisonListeners
in interfaceDifferenceEngineConfigurer<DiffBuilder>
- See Also:
DifferenceEngine.addComparisonListener(ComparisonListener)
-
withDifferenceListeners
public DiffBuilder withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL
.- Specified by:
withDifferenceListeners
in interfaceDifferenceEngineConfigurer<DiffBuilder>
- 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()
.
-
checkForIdentical
public DiffBuilder checkForIdentical()
check test source with the control source for identically.This is the Default.
-
withNamespaceContext
public DiffBuilder withNamespaceContext(java.util.Map<java.lang.String,java.lang.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.
- Specified by:
withNamespaceContext
in interfaceDifferenceEngineConfigurer<DiffBuilder>
- Parameters:
prefix2Uri
- mapping between prefix and namespace URI
-
withAttributeFilter
public DiffBuilder withAttributeFilter(Predicate<org.w3c.dom.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
.- Specified by:
withAttributeFilter
in interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withNodeFilter
public DiffBuilder withNodeFilter(Predicate<org.w3c.dom.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.
- Specified by:
withNodeFilter
in interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withComparisonFormatter
public DiffBuilder withComparisonFormatter(ComparisonFormatter formatter)
Sets a non-default formatter for the differences found.- Specified by:
withComparisonFormatter
in interfaceDifferenceEngineConfigurer<DiffBuilder>
-
withDocumentBuilderFactory
public DiffBuilder withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets theDocumentBuilderFactory
to use when creating aDocument
from theSource
s to compare.This is only used if the
Source
s used for control and test not already areDOMSource
s.- 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 aDiff
object.
-
wrap
private javax.xml.transform.Source wrap(javax.xml.transform.Source source)
-
-