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));
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)
withNodeMatcher in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withNodeMatcher(NodeMatcher)public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
withDifferenceEvaluator in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)public CompareMatcher withComparisonListeners(ComparisonListener... comparisonListeners)
withComparisonListeners in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withComparisonListeners(ComparisonListener...)public CompareMatcher withDifferenceListeners(ComparisonListener... comparisonListeners)
withDifferenceListeners in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withDifferenceListeners(ComparisonListener...)public CompareMatcher withNamespaceContext(Map<String,String> prefix2Uri)
withNamespaceContext in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withNamespaceContext(Map)public CompareMatcher withAttributeFilter(Predicate<Attr> attributeFilter)
withAttributeFilter in interface DifferenceEngineConfigurer<CompareMatcher>DiffBuilder.withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr>)public CompareMatcher withNodeFilter(Predicate<Node> nodeFilter)
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–2020 XMLUnit. All rights reserved.