public final class CompareMatcher extends org.hamcrest.BaseMatcher<Object> implements 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));
| Modifier and Type | Method and Description |
|---|---|
void |
describeMismatch(Object item,
org.hamcrest.Description description) |
void |
describeTo(org.hamcrest.Description description) |
CompareMatcher |
ignoreComments() |
CompareMatcher |
ignoreCommentsUsingXSLTVersion(String xsltVersion) |
CompareMatcher |
ignoreElementContentWhitespace() |
CompareMatcher |
ignoreWhitespace() |
static CompareMatcher |
isIdenticalTo(Object control)
Create a
CompareMatcher which compares the test-Object with the given control Object for identity. |
static CompareMatcher |
isSimilarTo(Object control)
Create a
CompareMatcher which compares the test-Object with the given control Object for similarity. |
boolean |
matches(Object item) |
CompareMatcher |
normalizeWhitespace() |
CompareMatcher |
throwComparisonFailure()
Instead of Matcher returning
false a org.junit.ComparisonFailure will be thrown. |
CompareMatcher |
withAttributeFilter(Predicate<Attr> attributeFilter)
Registers a filter for attributes.
|
CompareMatcher |
withComparisonController(ComparisonController comparisonController)
Throws an exception as you the
ComparisonController 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 custom
DifferenceEvaluator implementation. |
CompareMatcher |
withDifferenceListeners(ComparisonListener... comparisonListeners)
Registers listeners that are notified of each comparison with
outcome other than
ComparisonResult.EQUAL. |
CompareMatcher |
withDocumentBuilderFactory(DocumentBuilderFactory f) |
CompareMatcher |
withNamespaceContext(Map<String,String> prefix2Uri)
Establish a namespace context that will be used in
Comparison.Detail#getXPath. |
CompareMatcher |
withNodeFilter(Predicate<Node> nodeFilter)
Registers a filter for nodes.
|
CompareMatcher |
withNodeMatcher(NodeMatcher nodeMatcher)
Sets the strategy for selecting nodes to compare.
|
public static CompareMatcher isIdenticalTo(Object control)
CompareMatcher 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).
public static CompareMatcher isSimilarTo(Object control)
CompareMatcher 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.
See DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)
As input all types are supported which are supported by Input.from(Object).
public CompareMatcher ignoreWhitespace()
DiffBuilder.ignoreWhitespace()public CompareMatcher normalizeWhitespace()
DiffBuilder.normalizeWhitespace()public CompareMatcher ignoreComments()
DiffBuilder.ignoreComments()public CompareMatcher ignoreElementContentWhitespace()
DiffBuilder.ignoreElementContentWhitespace()public CompareMatcher ignoreCommentsUsingXSLTVersion(String xsltVersion)
DiffBuilder.ignoreCommentsUsingXSLTVersion(String)public CompareMatcher withNodeMatcher(NodeMatcher nodeMatcher)
DifferenceEngineConfigurer
Example with DefaultNodeMatcher:
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
withNodeMatcher in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withNodeMatcher(NodeMatcher)public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
DifferenceEngineConfigurerDifferenceEvaluator 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...) or DifferenceEvaluators.first(DifferenceEvaluator...) to combine
them:
.withDifferenceEvaluator(
DifferenceEvaluators.chain(
DifferenceEvaluators.Default,
new MyCustomDifferenceEvaluator()))
....
withDifferenceEvaluator in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)public CompareMatcher withComparisonListeners(ComparisonListener... comparisonListeners)
DifferenceEngineConfigurerwithComparisonListeners in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withComparisonListeners(ComparisonListener...)public CompareMatcher withDifferenceListeners(ComparisonListener... comparisonListeners)
DifferenceEngineConfigurerComparisonResult.EQUAL.withDifferenceListeners in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withDifferenceListeners(ComparisonListener...)public CompareMatcher withNamespaceContext(Map<String,String> prefix2Uri)
DifferenceEngineConfigurerComparison.Detail#getXPath.
Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.
withNamespaceContext in interface DifferenceEngineConfigurer<CompareMatcher>prefix2Uri - mapping between prefix and namespace URIDiffBuilder.withNamespaceContext(Map)public CompareMatcher withAttributeFilter(Predicate<Attr> attributeFilter)
DifferenceEngineConfigurerOnly 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.
withAttributeFilter in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr>)public CompareMatcher withNodeFilter(Predicate<Node> nodeFilter)
DifferenceEngineConfigurerOnly nodes for which the predicate returns true are part of the comparison. By default nodes that are not document types are considered.
withNodeFilter in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node>)public CompareMatcher throwComparisonFailure()
false a org.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 in org.junit.Assert#assertEquals(Object, Object) if both values are
Strings.
The only disadvantage is, that you can't combine the CompareMatcher with other Matchers
(like CoreMatchers.not(Object)) anymore. The following code will NOT WORK properly:
assertThat(test, not(isSimilarTo(control).throwComparisonFailure()))
public CompareMatcher withComparisonFormatter(ComparisonFormatter comparisonFormatter)
DefaultComparisonFormatter.withComparisonFormatter in interface DifferenceEngineConfigurer<CompareMatcher>public CompareMatcher withDocumentBuilderFactory(DocumentBuilderFactory f)
DiffBuilder.withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)public CompareMatcher withComparisonController(ComparisonController comparisonController)
ComparisonController is
completely determined by the factory method used.withComparisonController in interface DifferenceEngineConfigurer<CompareMatcher>public boolean matches(Object item)
matches in interface org.hamcrest.Matcher<Object>public void describeTo(org.hamcrest.Description description)
describeTo in interface org.hamcrest.SelfDescribingCopyright © 2001–2021 XMLUnit. All rights reserved.