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,DifferenceEngineConfigurer<CompareMatcher>
public final class CompareMatcher extends org.hamcrest.BaseMatcher<java.lang.Object> implements 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));
-
-
Field Summary
Fields Modifier and Type Field Description private ComparisonResultcheckForprivate static java.lang.reflect.Constructor<?>comparisonFailureConstructorprivate ComparisonFormattercomparisonFormatterprivate static ComparisonFormatterDEFAULT_FORMATTERprivate DiffBuilderdiffBuilderprivate DiffdiffResultprivate booleanformatXmlprivate static java.util.logging.LoggerLOGGERprivate booleanthrowComparisonFailure
-
Constructor Summary
Constructors Modifier Constructor Description privateCompareMatcher(java.lang.Object control)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private CompareMatchercheckForIdentical()private CompareMatchercheckForSimilar()private java.lang.AssertionErrorcreateComparisonFailure()private static java.lang.AssertionErrorcreateComparisonFailure(java.lang.String reason, java.lang.String controlString, java.lang.String testString)Calls the ConstructorComparisonFailure(String, String, String)with reflections and returnnullif theComparisonFailureclass is not available.private java.lang.StringcreateReasonPrefix(java.lang.String systemId, Comparison difference)voiddescribeMismatch(java.lang.Object item, org.hamcrest.Description description)voiddescribeTo(org.hamcrest.Description description)private ComparisonfirstComparison()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 returningfalseaComparisonFailurewill be thrown.CompareMatcherwithAttributeFilter(Predicate<org.w3c.dom.Attr> attributeFilter)Registers a filter for attributes.CompareMatcherwithComparisonController(ComparisonController comparisonController)Throws an exception as you theComparisonControlleris completely determined by the factory method used.CompareMatcherwithComparisonFormatter(ComparisonFormatter comparisonFormatter)Use a custom Formatter for the Error Messages.CompareMatcherwithComparisonListeners(ComparisonListener... comparisonListeners)Registers listeners that are notified of each comparison.CompareMatcherwithDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)Provide your own customDifferenceEvaluatorimplementation.CompareMatcherwithDifferenceListeners(ComparisonListener... comparisonListeners)Registers listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.CompareMatcherwithDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)CompareMatcherwithNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)Establish a namespace context that will be used inComparison.Detail#getXPath.CompareMatcherwithNodeFilter(Predicate<org.w3c.dom.Node> nodeFilter)Registers a filter for nodes.CompareMatcherwithNodeMatcher(NodeMatcher nodeMatcher)Sets the strategy for selecting nodes to compare.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
diffBuilder
private final DiffBuilder diffBuilder
-
throwComparisonFailure
private boolean throwComparisonFailure
-
checkFor
private ComparisonResult checkFor
-
diffResult
private Diff diffResult
-
formatXml
private boolean formatXml
-
DEFAULT_FORMATTER
private static final ComparisonFormatter DEFAULT_FORMATTER
-
comparisonFormatter
private ComparisonFormatter comparisonFormatter
-
comparisonFailureConstructor
private static java.lang.reflect.Constructor<?> comparisonFailureConstructor
-
-
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).
-
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).
-
checkForSimilar
private CompareMatcher checkForSimilar()
-
checkForIdentical
private CompareMatcher checkForIdentical()
-
ignoreWhitespace
public CompareMatcher ignoreWhitespace()
- See Also:
DiffBuilder.ignoreWhitespace()
-
normalizeWhitespace
public CompareMatcher normalizeWhitespace()
- See Also:
DiffBuilder.normalizeWhitespace()
-
ignoreComments
public CompareMatcher ignoreComments()
- See Also:
DiffBuilder.ignoreComments()
-
ignoreElementContentWhitespace
public CompareMatcher ignoreElementContentWhitespace()
- Since:
- XMLUnit 2.6.0
- See Also:
DiffBuilder.ignoreElementContentWhitespace()
-
ignoreCommentsUsingXSLTVersion
public CompareMatcher ignoreCommentsUsingXSLTVersion(java.lang.String xsltVersion)
- Since:
- XMLUnit 2.5.0
- See Also:
DiffBuilder.ignoreCommentsUsingXSLTVersion(String)
-
withNodeMatcher
public CompareMatcher withNodeMatcher(NodeMatcher nodeMatcher)
Description copied from interface:DifferenceEngineConfigurerSets the strategy for selecting nodes to compare.Example with
DefaultNodeMatcher:.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
- Specified by:
withNodeMatcherin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withNodeMatcher(NodeMatcher)
-
withDifferenceEvaluator
public CompareMatcher withDifferenceEvaluator(DifferenceEvaluator differenceEvaluator)
Description copied from interface:DifferenceEngineConfigurerProvide your own customDifferenceEvaluatorimplementation.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...)orDifferenceEvaluators.first(DifferenceEvaluator...)to combine them:.withDifferenceEvaluator( DifferenceEvaluators.chain( DifferenceEvaluators.Default, new MyCustomDifferenceEvaluator())) ....- Specified by:
withDifferenceEvaluatorin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withDifferenceEvaluator(DifferenceEvaluator)
-
withComparisonListeners
public CompareMatcher withComparisonListeners(ComparisonListener... comparisonListeners)
Description copied from interface:DifferenceEngineConfigurerRegisters listeners that are notified of each comparison.- Specified by:
withComparisonListenersin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withComparisonListeners(ComparisonListener...)
-
withDifferenceListeners
public CompareMatcher withDifferenceListeners(ComparisonListener... comparisonListeners)
Description copied from interface:DifferenceEngineConfigurerRegisters listeners that are notified of each comparison with outcome other thanComparisonResult.EQUAL.- Specified by:
withDifferenceListenersin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withDifferenceListeners(ComparisonListener...)
-
withNamespaceContext
public CompareMatcher withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Description copied from interface:DifferenceEngineConfigurerEstablish a namespace context that will be used inComparison.Detail#getXPath.Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.
- Specified by:
withNamespaceContextin interfaceDifferenceEngineConfigurer<CompareMatcher>- Parameters:
prefix2Uri- mapping between prefix and namespace URI- Since:
- XMLUnit 2.1.0
- See Also:
DiffBuilder.withNamespaceContext(Map)
-
withAttributeFilter
public CompareMatcher withAttributeFilter(Predicate<org.w3c.dom.Attr> attributeFilter)
Description copied from interface:DifferenceEngineConfigurerRegisters a filter for attributes.Only 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.- Specified by:
withAttributeFilterin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withAttributeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Attr>)
-
withNodeFilter
public CompareMatcher withNodeFilter(Predicate<org.w3c.dom.Node> nodeFilter)
Description copied from interface:DifferenceEngineConfigurerRegisters a filter for nodes.Only nodes for which the predicate returns true are part of the comparison. By default nodes that are not document types are considered.
- Specified by:
withNodeFilterin interfaceDifferenceEngineConfigurer<CompareMatcher>- See Also:
DiffBuilder.withNodeFilter(org.xmlunit.util.Predicate<org.w3c.dom.Node>)
-
throwComparisonFailure
public CompareMatcher throwComparisonFailure()
Instead of Matcher returningfalseaComparisonFailurewill 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 inAssert.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()))
-
withComparisonFormatter
public CompareMatcher withComparisonFormatter(ComparisonFormatter comparisonFormatter)
Use a custom Formatter for the Error Messages. The defaultFormatter isDefaultComparisonFormatter.- Specified by:
withComparisonFormatterin interfaceDifferenceEngineConfigurer<CompareMatcher>
-
withDocumentBuilderFactory
public CompareMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
- Since:
- XMLUnit 2.2.0
- See Also:
DiffBuilder.withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)
-
withComparisonController
public CompareMatcher withComparisonController(ComparisonController comparisonController)
Throws an exception as you theComparisonControlleris completely determined by the factory method used.- Specified by:
withComparisonControllerin interfaceDifferenceEngineConfigurer<CompareMatcher>- Since:
- XMLUnit 2.6.0
-
matches
public boolean matches(java.lang.Object item)
- Specified by:
matchesin interfaceorg.hamcrest.Matcher<java.lang.Object>
-
createComparisonFailure
private java.lang.AssertionError createComparisonFailure()
- Returns:
- an instants of
ComparisonFailureornullif the class is not available.
-
createComparisonFailure
private static java.lang.AssertionError createComparisonFailure(java.lang.String reason, java.lang.String controlString, java.lang.String testString)Calls the ConstructorComparisonFailure(String, String, String)with reflections and returnnullif theComparisonFailureclass is not available.
-
describeTo
public void describeTo(org.hamcrest.Description description)
- Specified by:
describeToin interfaceorg.hamcrest.SelfDescribing
-
createReasonPrefix
private java.lang.String createReasonPrefix(java.lang.String systemId, Comparison difference)
-
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>
-
firstComparison
private Comparison firstComparison()
-
-