private static boolean isAdded( final Comparison comparison ) { return comparison.getControlDetails().getTarget() == null; }
private static boolean isAttrAdded( final Comparison comparison ) { return comparison.getControlDetails().getValue() == null; }
private static boolean isSequenceOfRootElement(Comparison comparison) { return comparison.getType() == ComparisonType.CHILD_NODELIST_SEQUENCE && comparison.getControlDetails().getTarget() instanceof Element && comparison.getControlDetails().getTarget().getParentNode() instanceof Document; }
@Override public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (COUNTED_COMPARISONS.contains(comparison.getType()) && comparison.getControlDetails().getValue() != null) { return outcome; } return ComparisonResult.EQUAL; } };
private boolean isDocumentWithDocTypeDifference(Comparison comparison) { if (!(comparison.getControlDetails().getTarget() instanceof Document)) { return false; } return hasDoctypeChild(comparison.getControlDetails().getTarget()) || hasDoctypeChild(comparison.getTestDetails().getTarget()); }
@Override public void describeTo(Description description) { if (diffResult == null || !diffResult.hasDifferences()) { description.appendText(" is ") .appendText(checkFor == ComparisonResult.EQUAL ? "equal" : "similar") .appendText(" to the control document"); return; } final Comparison difference = firstComparison(); final String reason = createReasonPrefix(diffResult.getControlSource().getSystemId(), difference); final String testString = comparisonFormatter.getDetails(difference.getControlDetails(), difference.getType(), formatXml); description.appendText(String.format("%s:\n%s", reason, testString)); }
public void comparisonPerformed(Comparison comparison, ComparisonResult outcome) { if (comparison.getType() == ComparisonType.XML_ENCODING) { haveSeenXmlEncoding = true; } else if (comparison.getControlDetails().getTarget() instanceof Element && (comparison.getType() == ComparisonType.NODE_TYPE || comparison.getType() == ComparisonType.CHILD_LOOKUP) ) { haveSeenElementNodeComparison = true; } }
private void printNodeDiff( final Comparison comparison ) { final String oldText = printNode.nodeToString( comparison.getControlDetails().getTarget() ); final String newText = printNode.nodeToString( comparison.getTestDetails().getTarget() ); printFullNodeDiff( testDoc, comparison, oldText, newText ); }
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (outcome == ComparisonResult.EQUAL) { return outcome; } if (comparison.getType() == ComparisonType.CHILD_LOOKUP && (isDoctype(comparison.getControlDetails()) || isDoctype(comparison.getTestDetails()))) { return ComparisonResult.EQUAL; } if (comparison.getType() == ComparisonType.CHILD_NODELIST_LENGTH && isDocumentWithDocTypeDifference(comparison) && Math.abs((Integer) comparison.getControlDetails().getValue() - (Integer) comparison.getTestDetails().getValue()) == 1) { return ComparisonResult.EQUAL; } if (comparison.getType() == ComparisonType.CHILD_NODELIST_SEQUENCE && isDocumentWithDocTypeDifference(comparison)) { return ComparisonResult.EQUAL; } return outcome; }
break; case TEXT_VALUE: if (comp.getControlDetails().getTarget() instanceof CDATASection) { proto = CDATA_VALUE; } else if (comp.getControlDetails().getTarget() instanceof Comment) { proto = COMMENT_VALUE; break; case CHILD_NODELIST_LENGTH: Comparison.Detail cd = comp.getControlDetails(); Comparison.Detail td = comp.getTestDetails(); if (ZERO.equals(cd.getValue()) diffs.add(new Difference(proto, toNodeDetail(comp.getControlDetails()), toNodeDetail(comp.getTestDetails())));
private static boolean belongsToProlog(Comparison comparison, boolean ignoreDoctypeDeclarationAsWell) { if (comparison.getType().isDoctypeComparison()) { return ignoreDoctypeDeclarationAsWell; } return belongsToProlog(comparison.getControlDetails().getTarget(), ignoreDoctypeDeclarationAsWell) || belongsToProlog(comparison.getTestDetails().getTarget(), ignoreDoctypeDeclarationAsWell); }
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() ) ); }
/** * @return an instants of {@link org.junit.ComparisonFailure} or <code>null</code> if the class is not available. */ private AssertionError createComparisonFailure() { final Comparison difference = firstComparison(); final String reason = createReasonPrefix(diffResult.getControlSource().getSystemId(), difference); final String controlString = comparisonFormatter.getDetails(difference.getControlDetails(), difference .getType(), formatXml); final String testString = comparisonFormatter.getDetails(difference.getTestDetails(), difference.getType(), formatXml); return createComparisonFailure(reason, controlString, testString); }
/** * Compares the detail values for object equality, lets the * difference evaluator and comparison controller evaluate the * result, notifies all listeners and returns the outcome. * * @return the outcome as pair of result and a flag that says * "stop the whole comparison process" when true. */ protected final ComparisonState compare(Comparison comp) { Object controlValue = comp.getControlDetails().getValue(); Object testValue = comp.getTestDetails().getValue(); boolean equal = controlValue == null ? testValue == null : controlValue.equals(testValue); ComparisonResult initial = equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; ComparisonResult altered = getDifferenceEvaluator().evaluate(comp, initial); listeners.fireComparisonPerformed(comp, altered); return altered != ComparisonResult.EQUAL && getComparisonController().stopDiffing(new Difference(comp, altered)) ? new FinishedComparisonState(altered) : new OngoingComparisonState(altered); }
final ComparisonType type = difference.getType(); String description = type.getDescription(); final Detail controlDetails = difference.getControlDetails(); final Detail testDetails = difference.getTestDetails(); final String controlTarget =
@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); }
case NODE_TYPE: Short control = (Short) comparison .getControlDetails().getValue(); Short test = (Short) comparison .getTestDetails().getValue();
changeHolder.addedAttribute( nodeText, attributeText ); semanticDiff.updateHolder( changeHolder, NodeChangesHolder.OpType.REMOVED, holderNodeText( controlDoc, comparison.getControlDetails() ) ); changeHolder.attachAutoDiffs(); 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 ); changeHolder.addedAttribute( nodeText, attributeText ); final String controlNodeText = printNode.nodeToString( xmlDomUtils.findNode( controlDoc, comparison.getControlDetails().getParentXPath() ) ); final String controlAttributeText = printNode.attrToString( (Attr)comparison.getControlDetails().getTarget() ); changeHolder.removedAttribute( controlNodeText, controlAttributeText ); semanticDiff.updateHolder( changeHolder, NodeChangesHolder.OpType.REMOVED, holderNodeText( controlDoc, comparison.getControlDetails() ) ); changeHolder.attachAutoDiffs();
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 } }
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 ); } } }