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,org.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>
public final class CompareMatcher extends org.hamcrest.BaseMatcher<java.lang.Object> implements org.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>
This HamcrestMatchercompares 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 customizedElementSelectororDifferenceEvaluator.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));
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddescribeMismatch(java.lang.Object item, org.hamcrest.Description description)voiddescribeTo(org.hamcrest.Description description)CompareMatcherignoreComments()CompareMatcherignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)CompareMatcherignoreElementContentWhitespace()CompareMatcherignoreWhitespace()static CompareMatcherisIdenticalTo(java.lang.Object control)Create aCompareMatcherwhich compares the test-Object with the given control Object for identity.static CompareMatcherisSimilarTo(java.lang.Object control)Create aCompareMatcherwhich compares the test-Object with the given control Object for similarity.booleanmatches(java.lang.Object item)CompareMatchernormalizeWhitespace()CompareMatcherthrowComparisonFailure()Instead of Matcher returningfalseaorg.junit.ComparisonFailurewill be thrown.CompareMatcherwithAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr> attributeFilter)CompareMatcherwithComparisonController(org.xmlunit.diff.ComparisonController comparisonController)Throws an exception as you theComparisonControlleris completely determined by the factory method used.CompareMatcherwithComparisonFormatter(org.xmlunit.diff.ComparisonFormatter comparisonFormatter)Use a custom Formatter for the Error Messages.CompareMatcherwithComparisonListeners(org.xmlunit.diff.ComparisonListener... comparisonListeners)CompareMatcherwithDifferenceEvaluator(org.xmlunit.diff.DifferenceEvaluator differenceEvaluator)CompareMatcherwithDifferenceListeners(org.xmlunit.diff.ComparisonListener... comparisonListeners)CompareMatcherwithDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)CompareMatcherwithNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)CompareMatcherwithNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node> nodeFilter)CompareMatcherwithNodeMatcher(org.xmlunit.diff.NodeMatcher nodeMatcher)
-
-
-
Method Detail
-
isIdenticalTo
public static CompareMatcher isIdenticalTo(java.lang.Object control)
Create aCompareMatcherwhich compares the test-Object with the given control Object for identity.As input all types are supported which are supported by
Input.from(Object).- Parameters:
control- the control XML source- Returns:
- a CompareMatcher
-
isSimilarTo
public static CompareMatcher isSimilarTo(java.lang.Object control)
Create aCompareMatcherwhich 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).- Parameters:
control- the control XML source- Returns:
- a CompareMatcher
-
ignoreWhitespace
public CompareMatcher ignoreWhitespace()
- Returns:
- this
- See Also:
DiffBuilder.ignoreWhitespace()
-
normalizeWhitespace
public CompareMatcher normalizeWhitespace()
- Returns:
- this
- See Also:
DiffBuilder.normalizeWhitespace()
-
ignoreComments
public CompareMatcher ignoreComments()
- Returns:
- this
- See Also:
DiffBuilder.ignoreComments()
-
ignoreElementContentWhitespace
public CompareMatcher ignoreElementContentWhitespace()
- Returns:
- this
- Since:
- XMLUnit 2.6.0
- See Also:
DiffBuilder.ignoreElementContentWhitespace()
-
ignoreCommentsUsingXSLTVersion
public CompareMatcher ignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)
- Parameters:
xsltVersion- use this version for the stylesheet- Returns:
- this
- Since:
- XMLUnit 2.5.0
- See Also:
DiffBuilder.ignoreCommentsUsingXSLTVersion(String)
-
withNodeMatcher
public CompareMatcher withNodeMatcher(org.xmlunit.diff.NodeMatcher nodeMatcher)
- Specified by:
withNodeMatcherin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withNodeMatcher(NodeMatcher)
-
withDifferenceEvaluator
public CompareMatcher withDifferenceEvaluator(org.xmlunit.diff.DifferenceEvaluator differenceEvaluator)
- Specified by:
withDifferenceEvaluatorin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)
-
withComparisonListeners
public CompareMatcher withComparisonListeners(org.xmlunit.diff.ComparisonListener... comparisonListeners)
- Specified by:
withComparisonListenersin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withComparisonListeners(ComparisonListener...)
-
withDifferenceListeners
public CompareMatcher withDifferenceListeners(org.xmlunit.diff.ComparisonListener... comparisonListeners)
- Specified by:
withDifferenceListenersin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withDifferenceListeners(ComparisonListener...)
-
withNamespaceContext
public CompareMatcher withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
- Specified by:
withNamespaceContextin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- Since:
- XMLUnit 2.1.0
- See Also:
DiffBuilder.withNamespaceContext(Map)
-
withAttributeFilter
public CompareMatcher withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr> attributeFilter)
- Specified by:
withAttributeFilterin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr>)
-
withNodeFilter
public CompareMatcher withNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node> nodeFilter)
- Specified by:
withNodeFilterin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node>)
-
throwComparisonFailure
public CompareMatcher throwComparisonFailure()
Instead of Matcher returningfalseaorg.junit.ComparisonFailurewill 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 inorg.junit.Assert#assertEquals(Object, Object)if both values areStrings.The only disadvantage is, that you can't combine the
CompareMatcherwith other Matchers (likeCoreMatchers.not(Object)) anymore. The following code will NOT WORK properly:assertThat(test, not(isSimilarTo(control).throwComparisonFailure()))- Returns:
- this
-
withComparisonFormatter
public CompareMatcher withComparisonFormatter(org.xmlunit.diff.ComparisonFormatter comparisonFormatter)
Use a custom Formatter for the Error Messages. The defaultFormatter isDefaultComparisonFormatter.- Specified by:
withComparisonFormatterin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>
-
withDocumentBuilderFactory
public CompareMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
- Parameters:
f- the DocumentBuilderFactory to use- Returns:
- this
- Since:
- XMLUnit 2.2.0
- See Also:
DiffBuilder.withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)
-
withComparisonController
public CompareMatcher withComparisonController(org.xmlunit.diff.ComparisonController comparisonController)
Throws an exception as you theComparisonControlleris completely determined by the factory method used.- Specified by:
withComparisonControllerin interfaceorg.xmlunit.builder.DifferenceEngineConfigurer<CompareMatcher>- Since:
- XMLUnit 2.6.0
-
matches
public boolean matches(java.lang.Object item)
- Specified by:
matchesin interfaceorg.hamcrest.Matcher<java.lang.Object>
-
describeTo
public void describeTo(org.hamcrest.Description description)
- Specified by:
describeToin interfaceorg.hamcrest.SelfDescribing
-
describeMismatch
public void describeMismatch(java.lang.Object item, org.hamcrest.Description description)- Specified by:
describeMismatchin interfaceorg.hamcrest.Matcher<java.lang.Object>- Overrides:
describeMismatchin classorg.hamcrest.BaseMatcher<java.lang.Object>
-
-