Package org.custommonkey.xmlunit
Class Diff
java.lang.Object
org.custommonkey.xmlunit.Diff
- All Implemented Interfaces:
ComparisonController,DifferenceListener
- Direct Known Subclasses:
DetailedDiff
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
or toString 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 of the DetailedDiff class can be used
instead.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final Documentprivate DifferenceEngineContractprivate DifferenceListenerprivate ElementQualifierprivate booleanprivate booleanprivate MatchTrackerprivate StringBuilderprivate booleanprivate final DocumentFields 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
ConstructorsModifierConstructorDescriptionConstruct a Diff that compares the XML read from two ReadersConstruct a Diff that compares the XML in two StringsConstruct a Diff that compares the XML in a control Document against the result of a transformationConstruct a Diff that compares the XML in two JAXP DOMSourcesprotectedConstruct a Diff from a prototypical instance.Construct a Diff that compares the XML in two DocumentsDiff(Document controlDoc, Document testDoc, DifferenceEngineContract comparator) Construct a Diff that compares the XML in two Documents using a specific DifferenceEngineDiff(Document controlDoc, Document testDoc, DifferenceEngineContract comparator, ElementQualifier elementQualifier) Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine and ElementQualifier DifferenceEngineDiff(InputSource control, InputSource test) Construct a Diff that compares the XML read from two JAXP InputSources -
Method Summary
Modifier and TypeMethodDescriptionprivate voidappendDifference(StringBuilder appendTo, Difference difference) Append a meaningful message to the buffer of messagesappendMessage(StringBuffer toAppendTo) Append the message from the result of this Diff instance to a specified StringBufferprotected final voidcompare()Top of the recursive comparison execution treeintdifferenceFound(Difference difference) DifferenceListener implementation.intevaluate(Difference difference) Evaluate the difference.private DocumentRemoves all comment nodes ifcomments are ignored.private DifferenceEngineContractLazily initializes the difference engine if it hasn't been set via a constructor.private DocumentManipulates the given document according to the setting in the XMLUnit class.private Documentprivate DocumentgetWhitespaceManipulatedDocument(Document originalDoc) Ifwhitespace is ignoredin differences then manipulate the content to strip the redundant whitespacebooleanhaltComparison(Difference afterDifference) ComparisonController implementation.booleanReturn the result of a comparison.voidoverrideDifferenceListener(DifferenceListener delegate) Override theDifferenceListenerused to determine how to handle differences that are found.voidoverrideElementQualifier(ElementQualifier delegate) Override theElementQualifierused to determine which control and test nodes are comparable for this difference comparison.voidoverrideMatchTracker(MatchTracker delegate) Override theMatchTrackerused to track successfully matched nodes.booleansimilar()Return the result of a comparison.voidskippedComparison(Node control, Node test) DifferenceListener implementation.private static DocumenttoString()Get the result of this Diff instance as a String
-
Field Details
-
controlDoc
-
testDoc
-
similar
private boolean similar -
identical
private boolean identical -
compared
private boolean compared -
haltComparison
private boolean haltComparison -
messages
-
differenceEngine
-
differenceListenerDelegate
-
elementQualifierDelegate
-
matchTrackerDelegate
-
-
Constructor Details
-
Diff
Construct a Diff that compares the XML in two Strings- Parameters:
control- the control XMLtest- the test XML- Throws:
SAXException- if the parser feels like itIOException- on i/o errors
-
Diff
Construct a Diff that compares the XML read from two Readers- Parameters:
control- the control XMLtest- the test XML- Throws:
SAXException- if the parser feels like itIOException- on i/o errors
-
Diff
Construct a Diff that compares the XML in two Documents- Parameters:
controlDoc- the control XMLtestDoc- the test XML
-
Diff
public Diff(String control, Transform testTransform) throws IOException, TransformerException, SAXException Construct a Diff that compares the XML in a control Document against the result of a transformation- Parameters:
control- the control XMLtestTransform- the test XML- Throws:
SAXException- if the parser feels like itTransformerException- if the transformer feels like itIOException- on i/o errors
-
Diff
Construct a Diff that compares the XML read from two JAXP InputSources- Parameters:
control- the control XMLtest- the test XML- Throws:
SAXException- if the parser feels like itIOException- on i/o errors
-
Diff
Construct a Diff that compares the XML in two JAXP DOMSources- Parameters:
control- the control XMLtest- the test XML
-
Diff
Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine- Parameters:
controlDoc- the control XMLtestDoc- the test XMLcomparator- the difference engine
-
Diff
public Diff(Document controlDoc, Document testDoc, DifferenceEngineContract comparator, ElementQualifier elementQualifier) Construct a Diff that compares the XML in two Documents using a specific DifferenceEngine and ElementQualifier DifferenceEngine- Parameters:
controlDoc- the control XMLtestDoc- the test XMLcomparator- the difference engineelementQualifier- the element qualifier
-
Diff
Construct a Diff from a prototypical instance. Used by extension subclasses- Parameters:
prototype- a prototypical instance
-
-
Method Details
-
getWhitespaceManipulatedDocument
Ifwhitespace is ignoredin 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
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
Removes all comment nodes ifcomments are ignored.- Parameters:
orig- a document making up one half of this difference- Returns:
- manipulated doc
-
getNormalizedDocument
-
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.- Returns:
- whether the two pieces of XML are similar
-
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.- Returns:
- whether the two pieces of XML are identical
-
appendDifference
Append a meaningful message to the buffer of messages- Parameters:
appendTo- the messages bufferdifference-
-
differenceFound
DifferenceListener implementation. If theoverrideDifferenceListenermethod has been called then the interpretation of the difference will be delegated.- Specified by:
differenceFoundin interfaceDifferenceListener- Parameters:
difference- a Difference instance as defined inDifferenceConstantsdescribing the cause of the difference and containing the detail of the nodes that differ- Returns:
- a DifferenceListener.RETURN_... constant indicating how the difference was interpreted. Always RETURN_ACCEPT_DIFFERENCE if the call is not delegated.
-
evaluate
Evaluate the difference.- Parameters:
difference- the difference- Returns:
- a DifferenceListener.RETURN_... constant indicating how the difference was interpreted.
-
skippedComparison
DifferenceListener implementation. If theoverrideDifferenceListenermethod has been called then the call will be delegated otherwise a message is printed toSystem.err.- Specified by:
skippedComparisonin interfaceDifferenceListener- Parameters:
control- the control node being comparedtest- the test node being compared- See Also:
-
haltComparison
ComparisonController implementation.- Specified by:
haltComparisonin interfaceComparisonController- Parameters:
afterDifference- the last Difference passed todifferenceFound- 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:
-
appendMessage
Append the message from the result of this Diff instance to a specified StringBuffer- Parameters:
toAppendTo- buffer to append messages to- Returns:
- specified StringBuffer with message appended
-
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.
-
overrideDifferenceListener
Override theDifferenceListenerused to determine how to handle differences that are found.- Parameters:
delegate- the DifferenceListener instance to delegate handling to.
-
overrideElementQualifier
Override theElementQualifierused to determine which control and test nodes are comparable for this difference comparison.- Parameters:
delegate- the ElementQualifier instance to delegate to.
-
overrideMatchTracker
Override theMatchTrackerused to track successfully matched nodes.- Parameters:
delegate- the MatchTracker instance to delegate handling to.
-
getDifferenceEngine
Lazily initializes the difference engine if it hasn't been set via a constructor. -
toDocument
-