Class DiffBuilder

  • All Implemented Interfaces:
    DifferenceEngineConfigurer<DiffBuilder>

    public class DiffBuilder
    extends java.lang.Object
    implements DifferenceEngineConfigurer<DiffBuilder>
    DiffBuilder to create a Diff instance.

    Valid inputs for control and test are all Objects supported by Input.from(Object).

    Example Usage:

     String controlXml = "<a><b>Test Value</b></a>";
     String testXml = "<a>\n <b>\n  Test Value\n </b>\n</a>";
     Diff myDiff = DiffBuilder.compare(Input.fromString(controlXml)).withTest(Input.fromString(testXml))
         .checkForSimilar()
         .ignoreWhitespace()
         .build();
     assertFalse("XML similar " + myDiff.toString(), myDiff.hasDifferences());
     
    • Field Detail

      • CHECK_FOR_IDENTICAL

        private static final ComparisonResult[] CHECK_FOR_IDENTICAL
      • controlSource

        private final javax.xml.transform.Source controlSource
      • testSource

        private javax.xml.transform.Source testSource
      • namespaceContext

        private java.util.Map<java.lang.String,​java.lang.String> namespaceContext
      • attributeFilter

        private Predicate<org.w3c.dom.Attr> attributeFilter
      • nodeFilter

        private Predicate<org.w3c.dom.Node> nodeFilter
      • ignoreWhitespace

        private boolean ignoreWhitespace
      • normalizeWhitespace

        private boolean normalizeWhitespace
      • ignoreECW

        private boolean ignoreECW
      • ignoreComments

        private boolean ignoreComments
      • ignoreCommentVersion

        private java.lang.String ignoreCommentVersion
      • documentBuilderFactory

        private javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory
    • Constructor Detail

      • DiffBuilder

        private DiffBuilder​(javax.xml.transform.Source controlSource)
        Create a DiffBuilder instance.
        Parameters:
        controlSource - the expected reference Result.
    • Method Detail

      • compare

        public static DiffBuilder compare​(java.lang.Object control)
        Create a DiffBuilder from all kind of types supported by Input.from(Object).
        Parameters:
        control - the expected reference document.
        Returns:
        a new builder
      • withTest

        public DiffBuilder withTest​(java.lang.Object test)
        Set the Test-Source from all kind of types supported by Input.from(Object).
        Parameters:
        test - the test document which must be compared with the control document.
        Returns:
        this
      • getSource

        private static javax.xml.transform.Source getSource​(java.lang.Object object)
      • ignoreWhitespace

        public DiffBuilder ignoreWhitespace()
        Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.

        If you only want to remove text nodes consisting solely of whitespace (AKA element content whitespace) but leave all other text nodes alone you should use ignoreElementContentWhitespace() instead.

        Returns:
        this
      • normalizeWhitespace

        public DiffBuilder normalizeWhitespace()
        Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.

        "normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed.

        Returns:
        this
      • ignoreElementContentWhitespace

        public DiffBuilder ignoreElementContentWhitespace()
        Ignore element content whitespace by removing all text nodes solely consisting of whitespace.
        Returns:
        this
        Since:
        XMLUnit 2.6.0
      • ignoreCommentsUsingXSLTVersion

        public DiffBuilder ignoreCommentsUsingXSLTVersion​(java.lang.String xsltVersion)
        Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.

        Comments are ignored by applying an XSLT transformation on the source which may reduce the effect of withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory). This uses the CommentLessSource constructor with two arguments using xsltVersion as second argument.

        Parameters:
        xsltVersion - use this version for the stylesheet
        Returns:
        this
        Since:
        XMLUnit 2.5.0
      • checkForIdentical

        public DiffBuilder checkForIdentical()
        check test source with the control source for identically.

        This is the Default.

        Returns:
        this
      • withNamespaceContext

        public DiffBuilder withNamespaceContext​(java.util.Map<java.lang.String,​java.lang.String> prefix2Uri)
        Establish a namespace context that will be used in Comparison.Detail#getXPath.

        Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.

        This overwrites any Map set via earlier invocations of withNamespaceContext.

        Specified by:
        withNamespaceContext in interface DifferenceEngineConfigurer<DiffBuilder>
        Parameters:
        prefix2Uri - mapping between prefix and namespace URI
        Returns:
        this
      • withAttributeFilter

        public DiffBuilder withAttributeFilter​(Predicate<org.w3c.dom.Attr> attributeFilter)
        Registers 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.

        This overwrites any Predicate set via earlier invocations of withAttributeFilter.

        Specified by:
        withAttributeFilter in interface DifferenceEngineConfigurer<DiffBuilder>
        Parameters:
        attributeFilter - attribute filter to use
        Returns:
        this
      • withNodeFilter

        public DiffBuilder withNodeFilter​(Predicate<org.w3c.dom.Node> nodeFilter)
        Registers 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.

        This overwrites any Predicate set via earlier invocations of withNodeFilter.

        Specified by:
        withNodeFilter in interface DifferenceEngineConfigurer<DiffBuilder>
        Parameters:
        nodeFilter - node filter to use
        Returns:
        this
      • withDocumentBuilderFactory

        public DiffBuilder withDocumentBuilderFactory​(javax.xml.parsers.DocumentBuilderFactory f)
        Sets the DocumentBuilderFactory to use when creating a Document from the Sources to compare.

        This is only used if the Sources used for control and test not already are DOMSources.

        Parameters:
        f - the DocumentBuilderFactory to use
        Returns:
        this
        Since:
        XMLUnit 2.2.0
      • wrap

        private javax.xml.transform.Source wrap​(javax.xml.transform.Source source)