public static NodeDetail toNodeDetail(Comparison.Detail detail) { String value = String.valueOf(detail.getValue()); if (detail.getValue() instanceof Node) { value = ((Node) detail.getValue()).getNodeName(); } return new NodeDetail(value, detail.getTarget(), detail.getXPath()); }
final Detail testDetails = difference.getTestDetails(); final String controlTarget = getShortString(controlDetails.getTarget(), controlDetails.getXPath(), type); final String testTarget = getShortString(testDetails.getTarget(), testDetails.getXPath(), type); return String.format("Expected %s '%s' - comparing %s to %s", description, controlDetails.getXPath(), controlTarget, testTarget);
final String nodeText = printNode.nodeToString( xmlDomUtils.findNode( testDoc, comparison.getTestDetails().getParentXPath() ) ); final String attributeText = printNode.attrToString( comparison.getTestDetails().getTarget(), (QName)comparison.getTestDetails().getValue() ); output.startSpan( "MODIFIED ; new attribute [" + attributeText + "] <!-- xpath: " + comparison.getTestDetails().getXPath() + " -->" ); output.writeRaw( " . " ); output.endSpan(); final String parentNodeXpath = XmlDomUtils.wideContext( comparison.getTestDetails().getXPath() ); final NodeChangesHolder changeHolder = semanticDiff.updateHolder( semanticDiff.addChangeHolder( parentNodeXpath ), NodeChangesHolder.OpType.ADDED, holderNodeText( testDoc, comparison.getTestDetails() ) ); changeHolder.addedAttribute( nodeText, attributeText ); final String controlNodeText = printNode.nodeToString( xmlDomUtils.findNode( controlDoc, comparison.getControlDetails().getParentXPath() ) ); final String controlAttributeText = printNode.attrToString( comparison.getControlDetails().getTarget(), (QName)comparison.getControlDetails().getValue() ); output.startSpan( "MODIFIED ; removed attribute [" + controlAttributeText + "] <!-- xpath: " + comparison.getControlDetails().getXPath() + " -->" ); output.writeRaw( " . " ); output.endSpan(); final String parentNodeXpath = XmlDomUtils.wideContext( comparison.getControlDetails().getXPath() ); final NodeChangesHolder changeHolder = semanticDiff.updateHolder( semanticDiff.addChangeHolder( parentNodeXpath ), NodeChangesHolder.OpType.REMOVED, holderNodeText( controlDoc, comparison.getControlDetails() ) ); changeHolder.removedAttribute( controlNodeText, controlAttributeText ); output.startSpan( "MODIFIED ; changed attribute [" + attributeText + "] <!-- xpath: " + comparison.getTestDetails().getXPath() + " -->" ); output.writeRaw( " . " ); output.endSpan(); final String parentNodeXpath = XmlDomUtils.wideContext( comparison.getTestDetails().getXPath() ); final NodeChangesHolder changeHolder = semanticDiff.updateHolder( semanticDiff.addChangeHolder( parentNodeXpath ), NodeChangesHolder.OpType.ADDED, holderNodeText( testDoc, comparison.getTestDetails() ) ); changeHolder.addedAttribute( nodeText, attributeText );
@Override public String getDescription(Comparison difference) { final ComparisonType type = difference.getType(); String description = type.getDescription(); final Detail controlDetails = difference.getControlDetails(); final Detail testDetails = difference.getTestDetails(); final String controlTarget = getDetails(difference.getControlDetails(), type, true); final String testTarget = getDetails(difference.getTestDetails(), type, true); if (type == ComparisonType.ATTR_NAME_LOOKUP) { return String.format("Expected %s '%s'\nEXPEXTED:\n%s\nFOUND:\n%s", description, controlDetails.getXPath(), controlTarget, testTarget); } return String.format("Expected %s '%s' but was '%s'\nEXPEXTED:\n%s\nFOUND:\n%s", description, getValue(controlDetails.getValue(), type), getValue(testDetails.getValue(), type), controlTarget, testTarget); }
private void printModifiedNode( final Comparison comparison ) { final Comparison.Detail details = comparison.getControlDetails(); if( XmlDomUtils.xpathDepth( details.getXPath() ) == 1 ) { output.startSpan( "MODIFIED ; " + details.getXPath() + "." ); output.writeRaw( " _ " ); output.endSpan(); } else { if( comparison.getType() == ComparisonType.CHILD_NODELIST_SEQUENCE ) { output.startSpan( ". node order different: " + comparison.getTestDetails().getXPath() ); output.writeRaw( " * " ); output.endSpan(); } else if( comparison.getType() == ComparisonType.CHILD_NODELIST_LENGTH ) { printChildNodesChanged( comparison ); } else if( comparison.getType() == ComparisonType.ATTR_NAME_LOOKUP || comparison.getType() == ComparisonType.ATTR_VALUE ) { printAttrChanged( comparison ); } else { printNodeDiff( comparison ); } } }
public void printChildNodesChanged( final Comparison comparison ) { final String parentNodeXpath = comparison.getTestDetails().getXPath(); final int sizeControl = (int)comparison.getControlDetails().getValue(); final int sizeTest = (int)comparison.getTestDetails().getValue(); if( sizeTest > sizeControl ) { // nodes added output.startSpan( String.format( ". %s node(s) added: %s <!-- %s -->", sizeTest - sizeControl, printNode.printNodeSignature( comparison.getTestDetails().getTarget() ), parentNodeXpath ) ); output.writeRaw( " * " ); output.endSpan(); } else { // nodes removed output.startSpan( String.format( ". %s node(s) removed: %s <!-- %s -->", sizeControl - sizeTest, printNode.printNodeSignature( comparison.getTestDetails().getTarget() ), parentNodeXpath ) ); output.writeRaw( " * " ); output.endSpan(); } final NodeChangesHolder changeHolder = semanticDiff.updateHolder( semanticDiff.addChangeHolder( XmlDomUtils.wideContext( parentNodeXpath ) ), NodeChangesHolder.OpType.ADDED, holderNodeText( testDoc, comparison.getTestDetails() ) ); semanticDiff.updateHolder( changeHolder, NodeChangesHolder.OpType.REMOVED, holderNodeText( controlDoc, comparison.getControlDetails() ) ); changeHolder.attachAutoDiffs(); semanticDiff.updateHolder( semanticDiff.addChangeHolder( XmlDomUtils.wideContext( comparison.getControlDetails().getXPath() ) ), NodeChangesHolder.OpType.REMOVED, holderNodeText( controlDoc, comparison.getControlDetails() ) ); }
private void printAddedNode( final Comparison comparison ) { final Comparison.Detail details = comparison.getTestDetails(); final Node parentNode = xmlDomUtils.findNode( testDoc, details.getParentXPath() ); final String nodeText = printNode.nodeToString( xmlDomUtils.findNode( testDoc, details.getXPath() ) ); output.startSpan( "ADDED <!-- xpath: " + details.getXPath() + " (parent node: "+printNode.printNodeSignature( parentNode )+" - "+details.getParentXPath()+" ) -->"); output.writeRaw( "+ " ); output.endSpan(); // don't need to output anything //~ output.addedPart( nodeText ); //~ output.newline(); if( ! semanticDiff.markNodeAdded( XmlDomUtils.wideContext( details.getParentXPath() ), nodeText, testDoc ) ) { semanticDiff.markNodeAdded( details.getXPath(), nodeText, testDoc ); // make sure change is not lost } }
private void printDeletedNode( final Comparison comparison ) { final Comparison.Detail details = comparison.getControlDetails(); final Node parentNode = xmlDomUtils.findNode( controlDoc, details.getParentXPath() ); output.startSpan( "DELETED <!-- xpath: " + details.getXPath() + " (parent node: "+printNode.printNodeSignature( parentNode )+" - "+details.getParentXPath()+" ) -->" ); output.writeRaw( "- " ); output.endSpan(); final String nodeText = printNode.nodeToString( xmlDomUtils.findNode( controlDoc, details.getXPath() ) ); // don't need to output anything //~ output.removedPart( nodeText ); //~ output.newline(); if( ! semanticDiff.markNodeRemoved( XmlDomUtils.wideContext( details.getParentXPath() ), nodeText, controlDoc ) ) { semanticDiff.markNodeRemoved( details.getXPath(), nodeText, controlDoc ); // make sure change is not lost } }
/** this one is clever enough to expand node text up to parent node scope, to provide interesting context when changes are printed */ private String holderNodeText( final Document doc, final Detail details ) { final long xpathDepth = XmlDomUtils.xpathDepth( details.getXPath() ); final boolean shouldTakeParent = xpathDepth > 2; final String xpathExpr = shouldTakeParent ? XmlDomUtils.wideContext( details.getParentXPath() ) : details.getXPath(); final String nodeText = printNode.nodeToString( xmlDomUtils.findNode( doc, xpathExpr ) ); return nodeText; }