Class CompareMatcher
- java.lang.Object
-
- org.hamcrest.BaseMatcher<java.lang.Object>
-
- org.xmlunit.matchers.CompareMatcher
-
- All Implemented Interfaces:
org.hamcrest.Matcher<java.lang.Object>
,org.hamcrest.SelfDescribing
,DifferenceEngineConfigurer<CompareMatcher>
public final class CompareMatcher extends org.hamcrest.BaseMatcher<java.lang.Object> implements DifferenceEngineConfigurer<CompareMatcher>
This HamcrestMatcher
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 customizedElementSelector
orDifferenceEvaluator
.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));
-
-
Field Summary
Fields Modifier and Type Field Description private ComparisonResult
checkFor
private static java.lang.reflect.Constructor<?>
comparisonFailureConstructor
private ComparisonFormatter
comparisonFormatter
private static ComparisonFormatter
DEFAULT_FORMATTER
private DiffBuilder
diffBuilder
private Diff
diffResult
private boolean
formatXml
private static java.util.logging.Logger
LOGGER
private boolean
throwComparisonFailure
-
Constructor Summary
Constructors Modifier Constructor Description private
CompareMatcher(java.lang.Object control)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private CompareMatcher
checkForIdentical()
private CompareMatcher
checkForSimilar()
private java.lang.AssertionError
createComparisonFailure()
private static java.lang.AssertionError
createComparisonFailure(java.lang.String reason, java.lang.String controlString, java.lang.String testString)
Calls the ConstructorComparisonFailure(String, String, String)
with reflections and returnnull
if theComparisonFailure
class is not available.private java.lang.String
createReasonPrefix(java.lang.String systemId, Comparison difference)
void
describeMismatch(java.lang.Object item, org.hamcrest.Description description)
void
describeTo(org.hamcrest.Description description)
private Comparison
firstComparison()
CompareMatcher
ignoreComments()
CompareMatcher
ignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)
CompareMatcher
ignoreElementContentWhitespace()
CompareMatcher
ignoreWhitespace()
static CompareMatcher
isIdenticalTo(java.lang.Object control)
Create aCompareMatcher
which compares the test-Object with the given control Object for identity.static CompareMatcher
isSimilarTo(java.lang.Object control)
Create aCompareMatcher
which compares the test-Object with the given control Object for similarity.boolean
matches(java.lang.Object item)
CompareMatcher
normalizeWhitespace()
CompareMatcher
throwComparisonFailure()
Instead of Matcher returningfalse
aComparisonFailure
will be thrown.CompareMatcher
withAttributeFilter(Predicate<org.w3c.dom.Attr> attributeFilter)
Registers a filter for attributes.CompareMatcher
withComparisonController(ComparisonController comparisonController)
Throws an exception as you theComparisonController
is completely determined by the factory method used.CompareMatcher
withComparisonFormatter(ComparisonFormatter comparisonFormatter)
Use a custom Formatter for the Error Messages.CompareMatcher
withComparisonListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison.CompareMatcher
withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
Provide your own customDifferenceEvaluator
implementation.CompareMatcher
withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL
.CompareMatcher
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
CompareMatcher
withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Establish a namespace context that will be used inComparison.Detail#getXPath
.CompareMatcher
withNodeFilter(Predicate<org.w3c.dom.Node> nodeFilter)
Registers a filter for nodes.CompareMatcher
withNodeMatcher(NodeMatcher nodeMatcher)
Sets the strategy for selecting nodes to compare.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
diffBuilder
private final DiffBuilder diffBuilder
-
throwComparisonFailure
private boolean throwComparisonFailure
-
checkFor
private ComparisonResult checkFor
-
diffResult
private Diff diffResult
-
formatXml
private boolean formatXml
-
DEFAULT_FORMATTER
private static final ComparisonFormatter DEFAULT_FORMATTER
-
comparisonFormatter
private ComparisonFormatter comparisonFormatter
-
comparisonFailureConstructor
private static java.lang.reflect.Constructor<?> comparisonFailureConstructor
-
-
Method Detail
-
isIdenticalTo
public static CompareMatcher isIdenticalTo(java.lang.Object control)
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)
.
-
isSimilarTo
public static CompareMatcher isSimilarTo(java.lang.Object control)
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)
.
-
checkForSimilar
private CompareMatcher checkForSimilar()
-
checkForIdentical
private CompareMatcher checkForIdentical()
-
ignoreWhitespace
public CompareMatcher ignoreWhitespace()
- See Also:
DiffBuilder.ignoreWhitespace()
-
normalizeWhitespace
public CompareMatcher normalizeWhitespace()
- See Also:
DiffBuilder.normalizeWhitespace()
-
ignoreComments
public CompareMatcher ignoreComments()
- See Also:
DiffBuilder.ignoreComments()
-
ignoreElementContentWhitespace
public CompareMatcher ignoreElementContentWhitespace()
- Since:
- XMLUnit 2.6.0
- See Also:
DiffBuilder.ignoreElementContentWhitespace()
-
ignoreCommentsUsingXSLTVersion
public CompareMatcher ignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)
- Since:
- XMLUnit 2.5.0
- See Also:
DiffBuilder.ignoreCommentsUsingXSLTVersion(String)
-
withNodeMatcher
public CompareMatcher withNodeMatcher(NodeMatcher nodeMatcher)
Description copied from interface:DifferenceEngineConfigurer
Sets the strategy for selecting nodes to compare.Example with
DefaultNodeMatcher
:.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
- Specified by:
withNodeMatcher
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withNodeMatcher(NodeMatcher)
-
withDifferenceEvaluator
public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
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())) ....
- Specified by:
withDifferenceEvaluator
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)
-
withComparisonListeners
public CompareMatcher withComparisonListeners(ComparisonListener... comparisonListeners)
Description copied from interface:DifferenceEngineConfigurer
Registers listeners that are notified of each comparison.- Specified by:
withComparisonListeners
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withComparisonListeners(ComparisonListener...)
-
withDifferenceListeners
public CompareMatcher withDifferenceListeners(ComparisonListener... comparisonListeners)
Description copied from interface:DifferenceEngineConfigurer
Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL
.- Specified by:
withDifferenceListeners
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withDifferenceListeners(ComparisonListener...)
-
withNamespaceContext
public CompareMatcher withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
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.
- Specified by:
withNamespaceContext
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Parameters:
prefix2Uri
- mapping between prefix and namespace URI- Since:
- XMLUnit 2.1.0
- See Also:
DiffBuilder.withNamespaceContext(Map)
-
withAttributeFilter
public CompareMatcher withAttributeFilter(Predicate<org.w3c.dom.Attr> attributeFilter)
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
.- Specified by:
withAttributeFilter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr>)
-
withNodeFilter
public CompareMatcher withNodeFilter(Predicate<org.w3c.dom.Node> nodeFilter)
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.
- Specified by:
withNodeFilter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- See Also:
DiffBuilder.withNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node>)
-
throwComparisonFailure
public CompareMatcher throwComparisonFailure()
Instead of Matcher returningfalse
aComparisonFailure
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 inAssert.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()))
-
withComparisonFormatter
public CompareMatcher withComparisonFormatter(ComparisonFormatter comparisonFormatter)
Use a custom Formatter for the Error Messages. The defaultFormatter isDefaultComparisonFormatter
.- Specified by:
withComparisonFormatter
in interfaceDifferenceEngineConfigurer<CompareMatcher>
-
withDocumentBuilderFactory
public CompareMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
- Since:
- XMLUnit 2.2.0
- See Also:
DiffBuilder.withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)
-
withComparisonController
public CompareMatcher withComparisonController(ComparisonController comparisonController)
Throws an exception as you theComparisonController
is completely determined by the factory method used.- Specified by:
withComparisonController
in interfaceDifferenceEngineConfigurer<CompareMatcher>
- Since:
- XMLUnit 2.6.0
-
matches
public boolean matches(java.lang.Object item)
- Specified by:
matches
in interfaceorg.hamcrest.Matcher<java.lang.Object>
-
createComparisonFailure
private java.lang.AssertionError createComparisonFailure()
- Returns:
- an instants of
ComparisonFailure
ornull
if the class is not available.
-
createComparisonFailure
private static java.lang.AssertionError createComparisonFailure(java.lang.String reason, java.lang.String controlString, java.lang.String testString)
Calls the ConstructorComparisonFailure(String, String, String)
with reflections and returnnull
if theComparisonFailure
class is not available.
-
describeTo
public void describeTo(org.hamcrest.Description description)
- Specified by:
describeTo
in interfaceorg.hamcrest.SelfDescribing
-
createReasonPrefix
private java.lang.String createReasonPrefix(java.lang.String systemId, Comparison difference)
-
describeMismatch
public void describeMismatch(java.lang.Object item, org.hamcrest.Description description)
- Specified by:
describeMismatch
in interfaceorg.hamcrest.Matcher<java.lang.Object>
- Overrides:
describeMismatch
in classorg.hamcrest.BaseMatcher<java.lang.Object>
-
firstComparison
private Comparison firstComparison()
-
-