public final class CompareMatcher extends org.hamcrest.BaseMatcher<Object>
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 ignoreCommentsUsingXSLTVersion(String xsltVersion)
DiffBuilder.ignoreCommentsUsingXSLTVersion(String)
public CompareMatcher withNodeMatcher(NodeMatcher nodeMatcher)
DiffBuilder.withNodeMatcher(NodeMatcher)
public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
public CompareMatcher withComparisonListeners(ComparisonListener... comparisonListeners)
public CompareMatcher withDifferenceListeners(ComparisonListener... comparisonListeners)
public CompareMatcher withNamespaceContext(Map<String,String> prefix2Uri)
DiffBuilder.withNamespaceContext(Map)
public CompareMatcher withAttributeFilter(Predicate<Attr> attributeFilter)
public CompareMatcher withNodeFilter(Predicate<Node> nodeFilter)
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
String
s.
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
.public CompareMatcher withDocumentBuilderFactory(DocumentBuilderFactory f)
DiffBuilder.withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)
public boolean matches(Object item)
public void describeTo(org.hamcrest.Description description)
Copyright © 2001–2017 XMLUnit. All rights reserved.