Package org.custommonkey.xmlunit
Class DifferenceEngine
- java.lang.Object
-
- org.custommonkey.xmlunit.DifferenceEngine
-
- All Implemented Interfaces:
DifferenceConstants,DifferenceEngineContract
public class DifferenceEngine extends java.lang.Object implements DifferenceConstants, DifferenceEngineContract
Class that has responsibility for comparing Nodes and notifying a DifferenceListener of any differences or dissimilarities that are found. Knows how to compare namespaces and nested child nodes, but currently only compares nodes of type ELEMENT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_TYPE_NODE, PROCESSING_INSTRUCTION_NODE and TEXT_NODE. Nodes of other types (eg ENTITY_NODE) will be skipped.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDifferenceEngine.DifferenceFoundExceptionMarker exception thrown by the protected compare() method and passed upwards through the call stack to the public compare() method.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringATTRIBUTE_ABSENTprivate ComparisonControllercontrollerprivate XpathNodeTrackercontrolTrackerprivate static DifferenceEngine.DifferenceFoundExceptionflowControlExceptionException instance used internally to control flow when a difference is foundprivate MatchTrackermatchTrackerprivate static java.lang.StringNOT_NULL_NODEprivate static java.lang.StringNULL_NODEprivate XpathNodeTrackertestTracker-
Fields inherited from interface org.custommonkey.xmlunit.DifferenceConstants
ATTR_NAME_NOT_FOUND, ATTR_NAME_NOT_FOUND_ID, ATTR_SEQUENCE, ATTR_SEQUENCE_ID, ATTR_VALUE, ATTR_VALUE_EXPLICITLY_SPECIFIED, ATTR_VALUE_EXPLICITLY_SPECIFIED_ID, ATTR_VALUE_ID, CDATA_VALUE, CDATA_VALUE_ID, CHILD_NODE_NOT_FOUND, CHILD_NODE_NOT_FOUND_ID, CHILD_NODELIST_LENGTH, CHILD_NODELIST_LENGTH_ID, CHILD_NODELIST_SEQUENCE, CHILD_NODELIST_SEQUENCE_ID, COMMENT_VALUE, COMMENT_VALUE_ID, DOCTYPE_NAME, DOCTYPE_NAME_ID, DOCTYPE_PUBLIC_ID, DOCTYPE_PUBLIC_ID_ID, DOCTYPE_SYSTEM_ID, DOCTYPE_SYSTEM_ID_ID, ELEMENT_NUM_ATTRIBUTES, ELEMENT_NUM_ATTRIBUTES_ID, ELEMENT_TAG_NAME, ELEMENT_TAG_NAME_ID, HAS_CHILD_NODES, HAS_CHILD_NODES_ID, HAS_DOCTYPE_DECLARATION, HAS_DOCTYPE_DECLARATION_ID, NAMESPACE_PREFIX, NAMESPACE_PREFIX_ID, NAMESPACE_URI, NAMESPACE_URI_ID, NO_NAMESPACE_SCHEMA_LOCATION, NO_NAMESPACE_SCHEMA_LOCATION_ID, NODE_TYPE, NODE_TYPE_ID, PROCESSING_INSTRUCTION_DATA, PROCESSING_INSTRUCTION_DATA_ID, PROCESSING_INSTRUCTION_TARGET, PROCESSING_INSTRUCTION_TARGET_ID, SCHEMA_LOCATION, SCHEMA_LOCATION_ID, TEXT_VALUE, TEXT_VALUE_ID
-
-
Constructor Summary
Constructors Constructor Description DifferenceEngine(ComparisonController controller)Simple constructor that uses no MatchTracker at all.DifferenceEngine(ComparisonController controller, MatchTracker matchTracker)Simple constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcompare(java.lang.Object expected, java.lang.Object actual, org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, Difference difference)If the expected and actual values are unequal then inform the listener of a difference and throw a DifferenceFoundException.protected voidcompare(java.lang.Object expected, java.lang.Object actual, org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, Difference difference, XpathNodeTracker controlLoc, XpathNodeTracker testLoc)If the expected and actual values are unequal then inform the listener of a difference and throw a DifferenceFoundException.voidcompare(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier)Entry point for Node comparison testing.protected voidcompareAttribute(org.w3c.dom.Attr control, org.w3c.dom.Attr test, DifferenceListener listener)Compare two attributesprotected voidcompareCDataSection(org.w3c.dom.CDATASection control, org.w3c.dom.CDATASection test, DifferenceListener listener)Compare two CDATA sections - unused, kept for backwards compatibilityprivate voidcompareCharacterData(org.w3c.dom.CharacterData control, org.w3c.dom.CharacterData test, DifferenceListener listener, Difference difference)Character comparison method used by comments, text and CDATA sectionsprotected voidcompareComment(org.w3c.dom.Comment control, org.w3c.dom.Comment test, DifferenceListener listener)Compare two commentsprotected voidcompareDocument(org.w3c.dom.Document control, org.w3c.dom.Document test, DifferenceListener listener, ElementQualifier elementQualifier)Compare two Documents for doctype and then element differencesprotected voidcompareDocumentType(org.w3c.dom.DocumentType control, org.w3c.dom.DocumentType test, DifferenceListener listener)Compare two DocumentType nodesprotected voidcompareElement(org.w3c.dom.Element control, org.w3c.dom.Element test, DifferenceListener listener)Compare 2 elements and their attributes(package private) voidcompareElementAttributes(org.w3c.dom.Element control, org.w3c.dom.Element test, org.w3c.dom.NamedNodeMap controlAttr, org.w3c.dom.NamedNodeMap testAttr, DifferenceListener listener)protected voidcompareHasChildNodes(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener)Compare the number of children, and if the same, compare the actual children via their NodeLists.protected voidcompareNode(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier)First point of call: if nodes are comparable it compares node values then recurses to compare node children.protected booleancompareNodeBasics(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener)Compares node type and node namespace characteristics: basically determines if nodes are comparable furtherprotected voidcompareNodeChildren(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier)Compare the number of children, and if the same, compare the actual children via their NodeLists.protected voidcompareNodeList(java.util.List<org.w3c.dom.Node> controlChildren, java.util.List<org.w3c.dom.Node> testChildren, int numNodes, DifferenceListener listener, ElementQualifier elementQualifier)Compare the contents of two node list one by one, assuming that order of children is NOT important: matching begins at same position in test list as control list.protected voidcompareProcessingInstruction(org.w3c.dom.ProcessingInstruction control, org.w3c.dom.ProcessingInstruction test, DifferenceListener listener)Compare two processing instructionsprotected voidcompareRecognizedXMLSchemaInstanceAttribute(org.w3c.dom.Attr control, org.w3c.dom.Attr test, DifferenceListener listener)Compare two attributesprotected voidcompareText(org.w3c.dom.CharacterData control, org.w3c.dom.CharacterData test, DifferenceListener listener)Compare textprotected voidcompareText(org.w3c.dom.Text control, org.w3c.dom.Text test, DifferenceListener listener)Compare text - unused, kept for backwards compatibilityprivate booleancomparingTextAndCDATA(short controlType, short testType)private java.lang.IntegergetNonSpecialAttrLength(org.w3c.dom.NamedNodeMap attributes)The number of attributes not related to namespace declarations and/or Schema location.private static java.lang.StringgetNullOrNotNull(org.w3c.dom.Node aNode)private java.lang.StringgetQName(org.w3c.dom.Node aNode)private static java.lang.StringgetQName(org.w3c.dom.Node aNode, boolean isNamespacedNode)private java.lang.StringgetUnNamespacedNodeName(org.w3c.dom.Node aNode)private static java.lang.StringgetUnNamespacedNodeName(org.w3c.dom.Node aNode, boolean isNamespacedNode)private java.lang.BooleanhasChildNodes(org.w3c.dom.Node n)Tests whether a Node has children, taking ignoreComments setting into account.private static booleanisNamespaced(org.w3c.dom.Node aNode)private booleanisRecognizedXMLSchemaInstanceAttribute(org.w3c.dom.Attr attr)private booleanisXMLNSAttribute(org.w3c.dom.Attr attribute)private voidmissingNode(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener)(package private) static java.util.List<org.w3c.dom.Node>nodeList2List(org.w3c.dom.NodeList nl)Returns the NodeList's Nodes as List, taking ignoreComments into account.(package private) static java.lang.StringnormalizeWhitespace(java.lang.String orig)Replace all whitespace characters with SPACE and collapse consecutive whitespace chars to a single SPACE.voidsetMatchTracker(MatchTracker matchTracker)private booleanunequal(java.lang.Object expected, java.lang.Object actual)Test two possibly null values for inequalityprivate booleanunequalNotNull(java.lang.Object expected, java.lang.Object actual)Test two non-null values for inequality
-
-
-
Field Detail
-
flowControlException
private static final DifferenceEngine.DifferenceFoundException flowControlException
Exception instance used internally to control flow when a difference is found
-
NULL_NODE
private static final java.lang.String NULL_NODE
- See Also:
- Constant Field Values
-
NOT_NULL_NODE
private static final java.lang.String NOT_NULL_NODE
- See Also:
- Constant Field Values
-
ATTRIBUTE_ABSENT
private static final java.lang.String ATTRIBUTE_ABSENT
- See Also:
- Constant Field Values
-
controller
private final ComparisonController controller
-
matchTracker
private MatchTracker matchTracker
-
controlTracker
private final XpathNodeTracker controlTracker
-
testTracker
private final XpathNodeTracker testTracker
-
-
Constructor Detail
-
DifferenceEngine
public DifferenceEngine(ComparisonController controller)
Simple constructor that uses no MatchTracker at all.- Parameters:
controller- the instance used to determine whether a Difference detected by this class should halt further comparison or not- See Also:
ComparisonController.haltComparison(Difference)
-
DifferenceEngine
public DifferenceEngine(ComparisonController controller, MatchTracker matchTracker)
Simple constructor- Parameters:
controller- the instance used to determine whether a Difference detected by this class should halt further comparison or notmatchTracker- the instance that is notified on each successful match. May be null.- See Also:
ComparisonController.haltComparison(Difference),MatchTracker.matchFound(Difference)
-
-
Method Detail
-
setMatchTracker
public void setMatchTracker(MatchTracker matchTracker)
- Specified by:
setMatchTrackerin interfaceDifferenceEngineContract- Parameters:
matchTracker- the instance that is notified on each successful match. May be null.
-
compare
public void compare(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier)Entry point for Node comparison testing.- Specified by:
comparein interfaceDifferenceEngineContract- Parameters:
control- Control XML to comparetest- Test XML to comparelistener- Notified of anydifferencesdetected during node comparison testingelementQualifier- Used to determine which elements qualify for comparison e.g. when a node has repeated child elements that may occur in any sequence and that sequence is not considered important.
-
getNullOrNotNull
private static java.lang.String getNullOrNotNull(org.w3c.dom.Node aNode)
-
compareNode
protected void compareNode(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier) throws DifferenceEngine.DifferenceFoundExceptionFirst point of call: if nodes are comparable it compares node values then recurses to compare node children.- Parameters:
control-test-listener-elementQualifier-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareDocument
protected void compareDocument(org.w3c.dom.Document control, org.w3c.dom.Document test, DifferenceListener listener, ElementQualifier elementQualifier) throws DifferenceEngine.DifferenceFoundExceptionCompare two Documents for doctype and then element differences- Parameters:
control-test-listener-elementQualifier-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareNodeBasics
protected boolean compareNodeBasics(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompares node type and node namespace characteristics: basically determines if nodes are comparable further- Parameters:
control-test-listener-- Returns:
- true if the nodes are comparable further, false otherwise
- Throws:
DifferenceEngine.DifferenceFoundException
-
comparingTextAndCDATA
private boolean comparingTextAndCDATA(short controlType, short testType)
-
compareHasChildNodes
protected void compareHasChildNodes(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare the number of children, and if the same, compare the actual children via their NodeLists.- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
hasChildNodes
private java.lang.Boolean hasChildNodes(org.w3c.dom.Node n)
Tests whether a Node has children, taking ignoreComments setting into account.
-
nodeList2List
static java.util.List<org.w3c.dom.Node> nodeList2List(org.w3c.dom.NodeList nl)
Returns the NodeList's Nodes as List, taking ignoreComments into account.
-
compareNodeChildren
protected void compareNodeChildren(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, ElementQualifier elementQualifier) throws DifferenceEngine.DifferenceFoundExceptionCompare the number of children, and if the same, compare the actual children via their NodeLists.- Parameters:
control-test-listener-elementQualifier-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareNodeList
protected void compareNodeList(java.util.List<org.w3c.dom.Node> controlChildren, java.util.List<org.w3c.dom.Node> testChildren, int numNodes, DifferenceListener listener, ElementQualifier elementQualifier) throws DifferenceEngine.DifferenceFoundExceptionCompare the contents of two node list one by one, assuming that order of children is NOT important: matching begins at same position in test list as control list.- Parameters:
controlChildren-testChildren-numNodes- convenience parameter because the calling method should know the value alreadylistener-elementQualifier- used to determine which of the child elements in the test NodeList should be compared to the current child element in the control NodeList.- Throws:
DifferenceEngine.DifferenceFoundException
-
missingNode
private void missingNode(org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundException
-
isNamespaced
private static boolean isNamespaced(org.w3c.dom.Node aNode)
- Parameters:
aNode-- Returns:
- true if the node has a namespace
-
compareElement
protected void compareElement(org.w3c.dom.Element control, org.w3c.dom.Element test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare 2 elements and their attributes- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
getNonSpecialAttrLength
private java.lang.Integer getNonSpecialAttrLength(org.w3c.dom.NamedNodeMap attributes)
The number of attributes not related to namespace declarations and/or Schema location.
-
compareElementAttributes
void compareElementAttributes(org.w3c.dom.Element control, org.w3c.dom.Element test, org.w3c.dom.NamedNodeMap controlAttr, org.w3c.dom.NamedNodeMap testAttr, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundException
-
getUnNamespacedNodeName
private java.lang.String getUnNamespacedNodeName(org.w3c.dom.Node aNode)
-
getUnNamespacedNodeName
private static java.lang.String getUnNamespacedNodeName(org.w3c.dom.Node aNode, boolean isNamespacedNode)
-
getQName
private java.lang.String getQName(org.w3c.dom.Node aNode)
-
getQName
private static java.lang.String getQName(org.w3c.dom.Node aNode, boolean isNamespacedNode)
-
isXMLNSAttribute
private boolean isXMLNSAttribute(org.w3c.dom.Attr attribute)
- Parameters:
attribute-- Returns:
- true if the attribute represents a namespace declaration
-
isRecognizedXMLSchemaInstanceAttribute
private boolean isRecognizedXMLSchemaInstanceAttribute(org.w3c.dom.Attr attr)
- Parameters:
attr-- Returns:
- true if the attribute is an XML Schema Instance namespace attribute XMLUnit treats in a special way.
-
compareRecognizedXMLSchemaInstanceAttribute
protected void compareRecognizedXMLSchemaInstanceAttribute(org.w3c.dom.Attr control, org.w3c.dom.Attr test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two attributes- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareAttribute
protected void compareAttribute(org.w3c.dom.Attr control, org.w3c.dom.Attr test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two attributes- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareCDataSection
protected void compareCDataSection(org.w3c.dom.CDATASection control, org.w3c.dom.CDATASection test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two CDATA sections - unused, kept for backwards compatibility- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareComment
protected void compareComment(org.w3c.dom.Comment control, org.w3c.dom.Comment test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two comments- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareDocumentType
protected void compareDocumentType(org.w3c.dom.DocumentType control, org.w3c.dom.DocumentType test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two DocumentType nodes- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareProcessingInstruction
protected void compareProcessingInstruction(org.w3c.dom.ProcessingInstruction control, org.w3c.dom.ProcessingInstruction test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare two processing instructions- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareText
protected void compareText(org.w3c.dom.Text control, org.w3c.dom.Text test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare text - unused, kept for backwards compatibility- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareText
protected void compareText(org.w3c.dom.CharacterData control, org.w3c.dom.CharacterData test, DifferenceListener listener) throws DifferenceEngine.DifferenceFoundExceptionCompare text- Parameters:
control-test-listener-- Throws:
DifferenceEngine.DifferenceFoundException
-
compareCharacterData
private void compareCharacterData(org.w3c.dom.CharacterData control, org.w3c.dom.CharacterData test, DifferenceListener listener, Difference difference) throws DifferenceEngine.DifferenceFoundExceptionCharacter comparison method used by comments, text and CDATA sections- Parameters:
control-test-listener-difference-- Throws:
DifferenceEngine.DifferenceFoundException
-
compare
protected void compare(java.lang.Object expected, java.lang.Object actual, org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, Difference difference) throws DifferenceEngine.DifferenceFoundExceptionIf the expected and actual values are unequal then inform the listener of a difference and throw a DifferenceFoundException.- Parameters:
expected-actual-control-test-listener-difference-- Throws:
DifferenceEngine.DifferenceFoundException
-
compare
protected void compare(java.lang.Object expected, java.lang.Object actual, org.w3c.dom.Node control, org.w3c.dom.Node test, DifferenceListener listener, Difference difference, XpathNodeTracker controlLoc, XpathNodeTracker testLoc) throws DifferenceEngine.DifferenceFoundExceptionIf the expected and actual values are unequal then inform the listener of a difference and throw a DifferenceFoundException.- Parameters:
expected-actual-control-test-listener-difference-controlLoc-testLoc-- Throws:
DifferenceEngine.DifferenceFoundException
-
unequal
private boolean unequal(java.lang.Object expected, java.lang.Object actual)Test two possibly null values for inequality- Parameters:
expected-actual-- Returns:
- TRUE if the values are neither both null, nor equals() equal
-
unequalNotNull
private boolean unequalNotNull(java.lang.Object expected, java.lang.Object actual)Test two non-null values for inequality- Parameters:
expected-actual-- Returns:
- TRUE if the values are not equals() equal (taking whitespace into account if necessary)
-
normalizeWhitespace
static final java.lang.String normalizeWhitespace(java.lang.String orig)
Replace all whitespace characters with SPACE and collapse consecutive whitespace chars to a single SPACE.
-
-