org.xmlunit.matchers.CompareMatcher Maven / Gradle / Ivy
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.matchers;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.DifferenceEngineConfigurer;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Comparison;
import org.xmlunit.diff.ComparisonController;
import org.xmlunit.diff.ComparisonControllers;
import org.xmlunit.diff.ComparisonFormatter;
import org.xmlunit.diff.ComparisonListener;
import org.xmlunit.diff.ComparisonResult;
import org.xmlunit.diff.DefaultComparisonFormatter;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.DifferenceEvaluator;
import org.xmlunit.diff.DifferenceEvaluators;
import org.xmlunit.diff.ElementSelector;
import org.xmlunit.diff.NodeMatcher;
import org.xmlunit.util.Predicate;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
/**
* This Hamcrest {@link Matcher} compares two XML sources with each others.
*
* The Test-Object and Control-Object can be all types of input supported by {@link 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 {@link ElementSelector} or {@link 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 final class CompareMatcher extends BaseMatcher