public final class CompareMatcher extends org.hamcrest.BaseMatcher<java.lang.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 | Field and 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 |
| Modifier | Constructor and Description |
|---|---|
private |
CompareMatcher(java.lang.Object control) |
| Modifier and Type | Method and 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 Constructor
org.junit.ComparisonFailure#ComparisonFailure(String, String, String) with
reflections and return null if the org.junit.ComparisonFailure 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 a
CompareMatcher which compares the test-Object with the given control Object for identity. |
static CompareMatcher |
isSimilarTo(java.lang.Object control)
Create a
CompareMatcher 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 returning
false a org.junit.ComparisonFailure 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 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(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 in
Comparison.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.
|
private static final java.util.logging.Logger LOGGER
private final DiffBuilder diffBuilder
private boolean throwComparisonFailure
private ComparisonResult checkFor
private Diff diffResult
private boolean formatXml
private static final ComparisonFormatter DEFAULT_FORMATTER
private ComparisonFormatter comparisonFormatter
private static java.lang.reflect.Constructor<?> comparisonFailureConstructor
public static CompareMatcher isIdenticalTo(java.lang.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(java.lang.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).
private CompareMatcher checkForSimilar()
private CompareMatcher checkForIdentical()
public CompareMatcher ignoreWhitespace()
DiffBuilder.ignoreWhitespace()public CompareMatcher normalizeWhitespace()
DiffBuilder.normalizeWhitespace()public CompareMatcher ignoreComments()
DiffBuilder.ignoreComments()public CompareMatcher ignoreElementContentWhitespace()
DiffBuilder.ignoreElementContentWhitespace()public CompareMatcher ignoreCommentsUsingXSLTVersion(java.lang.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(java.util.Map<java.lang.String,java.lang.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<org.w3c.dom.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<org.w3c.dom.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(javax.xml.parsers.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(java.lang.Object item)
matches in interface org.hamcrest.Matcher<java.lang.Object>private java.lang.AssertionError createComparisonFailure()
org.junit.ComparisonFailure or null if the class is not available.private static java.lang.AssertionError createComparisonFailure(java.lang.String reason,
java.lang.String controlString,
java.lang.String testString)
org.junit.ComparisonFailure#ComparisonFailure(String, String, String) with
reflections and return null if the org.junit.ComparisonFailure class is not available.public void describeTo(org.hamcrest.Description description)
describeTo in interface org.hamcrest.SelfDescribingprivate java.lang.String createReasonPrefix(java.lang.String systemId,
Comparison difference)
public void describeMismatch(java.lang.Object item,
org.hamcrest.Description description)
describeMismatch in interface org.hamcrest.Matcher<java.lang.Object>describeMismatch in class org.hamcrest.BaseMatcher<java.lang.Object>private Comparison firstComparison()