Class CompareMatcher
- All Implemented Interfaces:
org.hamcrest.Matcher<Object>
,org.hamcrest.SelfDescribing
,DifferenceEngineConfigurer<CompareMatcher>
Matcher
compares two XML sources with each others.
The Test-Object and Control-Object can be all types of input supported by Input.from(Object)
.
Simple Example
This example will throw an AssertionError: "Expected attribute value 'abc' but was 'xyz'".
final String control = "<a><b attr=\"abc\"></b></a>"; final String test = "<a><b attr=\"xyz\"></b></a>"; assertThat(test, CompareMatcher.isIdenticalTo(control));
Complex Example
In some cases you may have a static factory method for your project which wraps all project-specific configurations
like customized ElementSelector
or DifferenceEvaluator
.
public static CompareMatcher isMyProjSimilarTo(final File file) { return CompareMatcher.isSimilarTo(file) .throwComparisonFailure() .normalizeWhitespace() .ignoreComments() .withNodeMatcher(new DefaultNodeMatcher(new MyElementSelector())) .withDifferenceEvaluator(DifferenceEvaluators.chain( DifferenceEvaluators.Default, new MyDifferenceEvaluator())); }And then somewhere in your Tests:
assertThat(test, isMyProjSimilarTo(controlFile));
-
Method Summary
Modifier and TypeMethodDescriptionvoid
describeMismatch
(Object item, org.hamcrest.Description description) void
describeTo
(org.hamcrest.Description description) ignoreCommentsUsingXSLTVersion
(String xsltVersion) static CompareMatcher
isIdenticalTo
(Object control) Create aCompareMatcher
which compares the test-Object with the given control Object for identity.static CompareMatcher
isSimilarTo
(Object control) Create aCompareMatcher
which compares the test-Object with the given control Object for similarity.boolean
Instead of Matcher returningfalse
aorg.junit.ComparisonFailure
will be thrown.withAttributeFilter
(Predicate<Attr> attributeFilter) Registers a filter for attributes.withComparisonController
(ComparisonController comparisonController) Throws an exception as you theComparisonController
is completely determined by the factory method used.withComparisonFormatter
(ComparisonFormatter comparisonFormatter) Use a custom Formatter for the Error Messages.withComparisonListeners
(ComparisonListener... comparisonListeners) Registers listeners that are notified of each comparison.withDifferenceEvaluator
(DifferenceEvaluator differenceEvaluator) Provide your own customDifferenceEvaluator
implementation.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.Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
-
Method Details
-
isIdenticalTo
Create aCompareMatcher
which compares the test-Object with the given control Object for identity.As input all types are supported which are supported by
Input.from(Object)
.- Parameters:
control
- the control XML source- Returns:
- a CompareMatcher
-
isSimilarTo
Create aCompareMatcher
which compares the test-Object with the given control Object 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
. SeeDiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)
As input all types are supported which are supported by
Input.from(Object)
.- Parameters:
control
- the control XML source- Returns:
- a CompareMatcher
-
ignoreWhitespace
- Returns:
- this
- See Also:
-
normalizeWhitespace
- Returns:
- this
- See Also:
-
ignoreComments
- Returns:
- this
- See Also:
-
ignoreElementContentWhitespace
- Returns:
- this
- Since:
- XMLUnit 2.6.0
- See Also:
-
ignoreCommentsUsingXSLTVersion
- Parameters:
xsltVersion
- use this version for the stylesheet- Returns:
- this
- Since:
- XMLUnit 2.5.0
- See Also:
-
withNodeMatcher
Description copied from interface:DifferenceEngineConfigurer
Sets the strategy for selecting nodes to compare.Example with
DefaultNodeMatcher
:.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
This overwrites any
NodeMatcher
set via earlier invocations ofwithNodeMatcher
.- Specified by:
withNodeMatcher
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
nodeMatcher
- the NodeMatcher to use- Returns:
- this
- See Also:
-
withDifferenceEvaluator
Description copied from interface:DifferenceEngineConfigurer
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 use
DifferenceEvaluators.chain(DifferenceEvaluator...)
orDifferenceEvaluators.first(DifferenceEvaluator...)
to combine them:.withDifferenceEvaluator( DifferenceEvaluators.chain( DifferenceEvaluators.Default, new MyCustomDifferenceEvaluator())) ....
This overwrites any
DifferenceEvaluator
set via earlier invocations ofwithDifferenceEvaluator
.- Specified by:
withDifferenceEvaluator
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
differenceEvaluator
- the DifferenceEvaluator to use- Returns:
- this
- See Also:
-
withComparisonListeners
Description copied from interface:DifferenceEngineConfigurer
Registers listeners that are notified of each comparison.- Specified by:
withComparisonListeners
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
comparisonListeners
- ComparisonListeners to use- Returns:
- this
- See Also:
-
withDifferenceListeners
Description copied from interface:DifferenceEngineConfigurer
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL
.This overwrites any
ComparisonListener
s set via earlier invocations ofwithDifferenceListeners
.- Specified by:
withDifferenceListeners
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
comparisonListeners
- ComparisonListeners to use- Returns:
- this
- See Also:
-
withNamespaceContext
Description copied from interface:DifferenceEngineConfigurer
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
Map
set via earlier invocations ofwithNamespaceContext
.- Specified by:
withNamespaceContext
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
prefix2Uri
- mapping between prefix and namespace URI- Returns:
- this
- Since:
- XMLUnit 2.1.0
- See Also:
-
withAttributeFilter
Description copied from interface:DifferenceEngineConfigurer
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
Predicate
set via earlier invocations ofwithAttributeFilter
.- Specified by:
withAttributeFilter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
attributeFilter
- attribute filter to use- Returns:
- this
- See Also:
-
withNodeFilter
Description copied from interface:DifferenceEngineConfigurer
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
Predicate
set via earlier invocations ofwithNodeFilter
.- Specified by:
withNodeFilter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
nodeFilter
- node filter to use- Returns:
- this
- See Also:
-
throwComparisonFailure
Instead of Matcher returningfalse
aorg.junit.ComparisonFailure
will be thrown.The advantage over the standard Matcher behavior is, that the ComparisonFailure can provide the effected Control-Node and Test-Node in separate Properties.
Eclipse, NetBeans and IntelliJ can provide a nice DIFF-View for the two values.
ComparisonFailure is also used inorg.junit.Assert#assertEquals(Object, Object)
if both values areString
s.The only disadvantage is, that you can't combine the
CompareMatcher
with other Matchers (likeCoreMatchers.not(Object)
) anymore. The following code will NOT WORK properly:assertThat(test, not(isSimilarTo(control).throwComparisonFailure()))
- Returns:
- this
-
withComparisonFormatter
Use a custom Formatter for the Error Messages. The defaultFormatter isDefaultComparisonFormatter
.- Specified by:
withComparisonFormatter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
comparisonFormatter
- formatter to use- Returns:
- this
-
withDocumentBuilderFactory
- Parameters:
f
- the DocumentBuilderFactory to use- Returns:
- this
- Since:
- XMLUnit 2.2.0
- See Also:
-
withComparisonController
Throws an exception as you theComparisonController
is completely determined by the factory method used.- Specified by:
withComparisonController
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
comparisonController
- ComparisonController to use- Returns:
- this
- Since:
- XMLUnit 2.6.0
-
matches
- Specified by:
matches
in interfaceorg.hamcrest.Matcher<Object>
-
describeTo
public void describeTo(org.hamcrest.Description description) - Specified by:
describeTo
in interfaceorg.hamcrest.SelfDescribing
-
describeMismatch
-