Package org.custommonkey.xmlunit
Class Diff
- java.lang.Object
-
- org.custommonkey.xmlunit.Diff
-
- All Implemented Interfaces:
ComparisonController
,DifferenceListener
- Direct Known Subclasses:
DetailedDiff
public class Diff extends java.lang.Object implements DifferenceListener, ComparisonController
Compares and describes any difference between XML documents. Two documents are either:- identical: the content and sequence of the nodes in the documents are exactly the same
- similar: the content of the nodes in the documents are the same, but minor differences exist e.g. sequencing of sibling elements, values of namespace prefixes, use of implied attribute values
- different: the contents of the documents are fundamentally different
appendMessage
ortoString
methods. NB: When comparing documents, the comparison is halted as soon as the status (identical / similar / different) is known with certainty. For a list of all differences between the documents an instance ofthe DetailedDiff class
can be used instead.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
compared
private org.w3c.dom.Document
controlDoc
private DifferenceEngineContract
differenceEngine
private DifferenceListener
differenceListenerDelegate
private ElementQualifier
elementQualifierDelegate
private boolean
haltComparison
private boolean
identical
private MatchTracker
matchTrackerDelegate
private java.lang.StringBuilder
messages
private boolean
similar
private org.w3c.dom.Document
testDoc
-
Fields inherited from interface org.custommonkey.xmlunit.DifferenceListener
RETURN_ACCEPT_DIFFERENCE, RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL, RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR, RETURN_UPGRADE_DIFFERENCE_NODES_DIFFERENT
-
-
Constructor Summary
Constructors Modifier Constructor Description Diff(java.io.Reader control, java.io.Reader test)
Construct a Diff that compares the XML read from two ReadersDiff(java.lang.String control, java.lang.String test)
Construct a Diff that compares the XML in two StringsDiff(java.lang.String control, Transform testTransform)
Construct a Diff that compares the XML in a control Document against the result of a transformationDiff(javax.xml.transform.dom.DOMSource control, javax.xml.transform.dom.DOMSource test)
Construct a Diff that compares the XML in two JAXP DOMSourcesprotected
Diff(Diff prototype)
Construct a Diff from a prototypical instance.Diff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc)
Construct a Diff that compares the XML in two DocumentsDiff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc, DifferenceEngineContract comparator)
Construct a Diff that compares the XML in two Documents using a specific DifferenceEngineDiff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc, DifferenceEngineContract comparator, ElementQualifier elementQualifier)
Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine and ElementQualifierDiff(org.xml.sax.InputSource control, org.xml.sax.InputSource test)
Construct a Diff that compares the XML read from two JAXP InputSources
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
appendDifference(java.lang.StringBuilder appendTo, Difference difference)
Append a meaningful message to the buffer of messagesjava.lang.StringBuffer
appendMessage(java.lang.StringBuffer toAppendTo)
Append the message from the result of this Diff instance to a specified StringBufferprotected void
compare()
Top of the recursive comparison execution treeint
differenceFound(Difference difference)
DifferenceListener implementation.int
evaluate(Difference difference)
private org.w3c.dom.Document
getCommentlessDocument(org.w3c.dom.Document orig)
Removes all comment nodes ifcomments are ignored
.private DifferenceEngineContract
getDifferenceEngine()
Lazily initializes the difference engine if it hasn't been set via a constructor.private org.w3c.dom.Document
getManipulatedDocument(org.w3c.dom.Document orig)
Manipulates the given document according to the setting in the XMLUnit class.private org.w3c.dom.Document
getNormalizedDocument(org.w3c.dom.Document orig)
private org.w3c.dom.Document
getWhitespaceManipulatedDocument(org.w3c.dom.Document originalDoc)
Ifwhitespace is ignored
in differences then manipulate the content to strip the redundant whitespaceboolean
haltComparison(Difference afterDifference)
ComparisonController implementation.boolean
identical()
Return the result of a comparison.void
overrideDifferenceListener(DifferenceListener delegate)
Override theDifferenceListener
used to determine how to handle differences that are found.void
overrideElementQualifier(ElementQualifier delegate)
Override theElementQualifier
used to determine which control and test nodes are comparable for this difference comparison.void
overrideMatchTracker(MatchTracker delegate)
Override theMatchTracker
used to track successfully matched nodes.boolean
similar()
Return the result of a comparison.void
skippedComparison(org.w3c.dom.Node control, org.w3c.dom.Node test)
DifferenceListener implementation.private static org.w3c.dom.Document
toDocument(javax.xml.transform.dom.DOMSource d)
java.lang.String
toString()
Get the result of this Diff instance as a Stringprivate boolean
usesUnknownElementQualifier()
-
-
-
Field Detail
-
controlDoc
private final org.w3c.dom.Document controlDoc
-
testDoc
private final org.w3c.dom.Document testDoc
-
similar
private boolean similar
-
identical
private boolean identical
-
compared
private boolean compared
-
haltComparison
private boolean haltComparison
-
messages
private java.lang.StringBuilder messages
-
differenceEngine
private DifferenceEngineContract differenceEngine
-
differenceListenerDelegate
private DifferenceListener differenceListenerDelegate
-
elementQualifierDelegate
private ElementQualifier elementQualifierDelegate
-
matchTrackerDelegate
private MatchTracker matchTrackerDelegate
-
-
Constructor Detail
-
Diff
public Diff(java.lang.String control, java.lang.String test) throws org.xml.sax.SAXException, java.io.IOException
Construct a Diff that compares the XML in two Strings- Throws:
org.xml.sax.SAXException
java.io.IOException
-
Diff
public Diff(java.io.Reader control, java.io.Reader test) throws org.xml.sax.SAXException, java.io.IOException
Construct a Diff that compares the XML read from two Readers- Throws:
org.xml.sax.SAXException
java.io.IOException
-
Diff
public Diff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc)
Construct a Diff that compares the XML in two Documents
-
Diff
public Diff(java.lang.String control, Transform testTransform) throws java.io.IOException, javax.xml.transform.TransformerException, org.xml.sax.SAXException
Construct a Diff that compares the XML in a control Document against the result of a transformation- Throws:
java.io.IOException
javax.xml.transform.TransformerException
org.xml.sax.SAXException
-
Diff
public Diff(org.xml.sax.InputSource control, org.xml.sax.InputSource test) throws org.xml.sax.SAXException, java.io.IOException
Construct a Diff that compares the XML read from two JAXP InputSources- Throws:
org.xml.sax.SAXException
java.io.IOException
-
Diff
public Diff(javax.xml.transform.dom.DOMSource control, javax.xml.transform.dom.DOMSource test)
Construct a Diff that compares the XML in two JAXP DOMSources
-
Diff
public Diff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc, DifferenceEngineContract comparator)
Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine
-
Diff
public Diff(org.w3c.dom.Document controlDoc, org.w3c.dom.Document testDoc, DifferenceEngineContract comparator, ElementQualifier elementQualifier)
Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine and ElementQualifier
-
Diff
protected Diff(Diff prototype)
Construct a Diff from a prototypical instance. Used by extension subclasses- Parameters:
prototype
- a prototypical instance
-
-
Method Detail
-
getWhitespaceManipulatedDocument
private org.w3c.dom.Document getWhitespaceManipulatedDocument(org.w3c.dom.Document originalDoc)
Ifwhitespace is ignored
in differences then manipulate the content to strip the redundant whitespace- Parameters:
originalDoc
- a document making up one half of this difference- Returns:
- the original document with redundant whitespace removed if differences ignore whitespace
-
getManipulatedDocument
private org.w3c.dom.Document getManipulatedDocument(org.w3c.dom.Document orig)
Manipulates the given document according to the setting in the XMLUnit class.This may involve:
- Parameters:
orig
- a document making up one half of this difference- Returns:
- manipulated doc
-
getCommentlessDocument
private org.w3c.dom.Document getCommentlessDocument(org.w3c.dom.Document orig)
Removes all comment nodes ifcomments are ignored
.- Parameters:
orig
- a document making up one half of this difference- Returns:
- manipulated doc
-
getNormalizedDocument
private org.w3c.dom.Document getNormalizedDocument(org.w3c.dom.Document orig)
-
compare
protected final void compare()
Top of the recursive comparison execution tree
-
similar
public boolean similar()
Return the result of a comparison. Two documents are considered to be "similar" if they contain the same elements and attributes regardless of order.
-
identical
public boolean identical()
Return the result of a comparison. Two documents are considered to be "identical" if they contain the same elements and attributes in the same order.
-
appendDifference
private void appendDifference(java.lang.StringBuilder appendTo, Difference difference)
Append a meaningful message to the buffer of messages- Parameters:
appendTo
- the messages bufferdifference
-
-
differenceFound
public int differenceFound(Difference difference)
DifferenceListener implementation. If theoverrideDifferenceListener
method has been called then the interpretation of the difference will be delegated.- Specified by:
differenceFound
in interfaceDifferenceListener
- Parameters:
difference
-- Returns:
- a DifferenceListener.RETURN_... constant indicating how the difference was interpreted. Always RETURN_ACCEPT_DIFFERENCE if the call is not delegated.
-
evaluate
public int evaluate(Difference difference)
-
skippedComparison
public void skippedComparison(org.w3c.dom.Node control, org.w3c.dom.Node test)
DifferenceListener implementation. If theoverrideDifferenceListener
method has been called then the call will be delegated otherwise a message is printed toSystem.err
.- Specified by:
skippedComparison
in interfaceDifferenceListener
- Parameters:
control
-test
-- See Also:
DifferenceEngine
-
haltComparison
public boolean haltComparison(Difference afterDifference)
ComparisonController implementation.- Specified by:
haltComparison
in interfaceComparisonController
- Parameters:
afterDifference
-- Returns:
- true if the difference is not recoverable and the comparison should be halted, or false if the difference is recoverable and the comparison can continue
- See Also:
Difference.isRecoverable()
-
appendMessage
public java.lang.StringBuffer appendMessage(java.lang.StringBuffer toAppendTo)
Append the message from the result of this Diff instance to a specified StringBuffer- Parameters:
toAppendTo
-- Returns:
- specified StringBuffer with message appended
-
toString
public java.lang.String toString()
Get the result of this Diff instance as a StringNote: This method will perform the comparison and cache the result if it hasn't been performed already. Any configuration changes made after calling this method will be ignored.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- result of this Diff
-
overrideDifferenceListener
public void overrideDifferenceListener(DifferenceListener delegate)
Override theDifferenceListener
used to determine how to handle differences that are found.- Parameters:
delegate
- the DifferenceListener instance to delegate handling to.
-
overrideElementQualifier
public void overrideElementQualifier(ElementQualifier delegate)
Override theElementQualifier
used to determine which control and test nodes are comparable for this difference comparison.- Parameters:
delegate
- the ElementQualifier instance to delegate to.
-
overrideMatchTracker
public void overrideMatchTracker(MatchTracker delegate)
Override theMatchTracker
used to track successfully matched nodes.- Parameters:
delegate
- the MatchTracker instance to delegate handling to.
-
getDifferenceEngine
private DifferenceEngineContract getDifferenceEngine()
Lazily initializes the difference engine if it hasn't been set via a constructor.
-
usesUnknownElementQualifier
private boolean usesUnknownElementQualifier()
-
toDocument
private static org.w3c.dom.Document toDocument(javax.xml.transform.dom.DOMSource d)
-
-