public class IgnoreNamedElementsDifferenceListener implements DifferenceListener { private Set<String> blackList = new HashSet<String>(); public IgnoreNamedElementsDifferenceListener(String ... elementNames) { for (String name : elementNames) { blackList.add(name); } } public int differenceFound(Difference difference) { if (difference.getId() == DifferenceConstants.TEXT_VALUE_ID) { if (blackList.contains(difference.getControlNodeDetail().getNode().getParentNode().getNodeName())) { return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } } return DifferenceListener.RETURN_ACCEPT_DIFFERENCE; } public void skippedComparison(Node node, Node node1) { } }
@Override public int differenceFound(Difference difference) { if (excludedNodes.contains(difference.getControlNodeDetail().getNode().getLocalName())) return 1; else { return 0; } }
@Override public int differenceFound(Difference difference) { String value = difference.getControlNodeDetail().getValue(); String tvalue = null; if (difference.getControlNodeDetail().getNode() != null) { tvalue = difference.getControlNodeDetail().getNode().getTextContent(); } if ("*".equals(value) || "*".equals(tvalue)) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } else { return RETURN_ACCEPT_DIFFERENCE; } } });
public int differenceFound(Difference difference) { Node controlNode = difference.getControlNodeDetail().getNode(); Node testNode = difference.getTestNodeDetail().getNode(); if (controlNode.getNodeType() == Node.TEXT_NODE && testNode.getNodeType() == Node.TEXT_NODE) { String controlText = controlNode.getNodeValue().trim(); String testText = testNode.getNodeValue().trim(); if (controlText.equals("") && testText.equals("")) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } } return RETURN_ACCEPT_DIFFERENCE; }
XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreAttributeOrder(true); Diff xmlDiff = new Diff(expected, test); xmlDiff.overrideDifferenceListener(new DifferenceListener() { @Override public int differenceFound(Difference diff) { System.err.println("called: " + diff); int diffResult = RETURN_ACCEPT_DIFFERENCE; //ignore a node difference if(diff.getControlNodeDetail().getNode().getParentNode().getNodeName().contains("myNodeName")){ skippedComparison(diff.getControlNodeDetail().getNode().getParentNode(), diff.getTestNodeDetail().getNode().getParentNode()); diffResult = RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } return diffResult; } @Override public void skippedComparison(Node arg0, Node arg1) {} }); assertTrue(xmlDiff.similar());
protected boolean ignoreDifference(final Difference d) { if ("text value".equals(d.getDescription())) { final String controlValue = d.getControlNodeDetail().getValue(); final String testValue = d.getTestNodeDetail().getValue(); return (controlValue == null || controlValue.trim().isEmpty()) && (testValue == null || testValue.trim().isEmpty()); } return false; }
/** * If a difference is found when comparing xml it will trigger a call to * differenceFound. Determine if the difference can be ignored (part of * the IGNORED_NODES) or if it's a 'true' difference. */ @Override public int differenceFound(Difference arg0) { String elemXpathVal = arg0.getControlNodeDetail() .getXpathLocation(); if (IGNORED_NODES.contains(elemXpathVal)) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } System.out.println(" ** Difference Found:"); System.out.println("\t" + arg0.toString() + "\n"); return RETURN_ACCEPT_DIFFERENCE; }
final List<String> xpaths = new ArrayList<String>(); DifferenceListener myDifferenceListener = new DifferenceListener() { public void skippedComparison(Node arg0, Node arg1) { // Do nothing } public int differenceFound(Difference pDifference) { xpaths.add(pDifference.getControlNodeDetail().getXpathLocation()); return RETURN_ACCEPT_DIFFERENCE; } }; Diff myDiff = new Diff(xmlDoc1, xmlDoc2); myDiff.overrideDifferenceListener(myDifferenceListener); myDiff.similar(); return paths;
@Override public int differenceFound(Difference difference) { if (difference.getControlNodeDetail() == null || difference.getControlNodeDetail().getNode() == null || difference.getTestNodeDetail() == null || difference.getTestNodeDetail().getNode() == null) { return RETURN_ACCEPT_DIFFERENCE; } if (ignoreAttributes.contains(difference.getTestNodeDetail().getNode() .getNodeName()) || ignoreAttributes.contains(difference.getControlNodeDetail() .getNode().getNodeName())) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } return RETURN_ACCEPT_DIFFERENCE; } });
protected int textualDifference(Difference d) { String control = d.getControlNodeDetail().getValue(); String test = d.getTestNodeDetail().getValue(); if (control != null && test != null) { try { double controlVal = Double.parseDouble(control); double testVal = Double.parseDouble(test); return Math.abs(controlVal - testVal) <= tolerance ? DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : DifferenceListener.RETURN_ACCEPT_DIFFERENCE; } catch (NumberFormatException nfe) { // ignore, delegate to nested DifferenceListener } } // no numbers or null, delegate return super.textualDifference(d); } }
protected int textualDifference(Difference d) { String control = d.getControlNodeDetail().getValue(); String test = d.getTestNodeDetail().getValue(); if (control != null && test != null) { try { double controlVal = Double.parseDouble(control); double testVal = Double.parseDouble(test); return Math.abs(controlVal - testVal) < tolerance ? DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : DifferenceListener.RETURN_ACCEPT_DIFFERENCE; } catch (NumberFormatException nfe) { // ignore, delegate to nested DifferenceListener } } // no numbers or null, delegate return super.textualDifference(d); } }
protected int textualDifference(Difference d) { String control = d.getControlNodeDetail().getValue(); if (control != null) { control = control.toLowerCase(Locale.US); if (d.getTestNodeDetail().getValue() != null && control.equals(d.getTestNodeDetail().getValue() .toLowerCase(Locale.US))) { return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } } // some string is null, delegate return super.textualDifference(d); } }
protected int textualDifference(Difference d) { String control = d.getControlNodeDetail().getValue(); if (control != null) { control = control.toLowerCase(Locale.US); if (d.getTestNodeDetail().getValue() != null && control.equals(d.getTestNodeDetail().getValue() .toLowerCase(Locale.US))) { return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } } // some string is null, delegate return super.textualDifference(d); } }
public int differenceFound(Difference difference) { //ignore dissimilarities if (difference.isRecoverable()) { return RETURN_ACCEPT_DIFFERENCE; } if ("${IGNORE}".equals(difference.getControlNodeDetail().getValue())) { return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } else if (isDifferenceOnlyInAttributeValuePrefix(difference)) { return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } else { return super.differenceFound(difference); } }
public int differenceFound(Difference difference) { //ignore dissimilarities if (difference.isRecoverable()) { return RETURN_ACCEPT_DIFFERENCE; } if ("${IGNORE}".equals(difference.getControlNodeDetail().getValue())) { return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } else if (isDifferenceOnlyInAttributeValuePrefix(difference)) { return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } else { return super.differenceFound(difference); } }
boolean isDifferenceOnlyInAttributeValuePrefix(Difference difference) { Node testNode = difference.getTestNodeDetail().getNode(); if (testNode==null) { return false; } String testNodeValue = testNode.getNodeValue(); Node controlNode = difference.getControlNodeDetail().getNode(); String controlNodeValue = controlNode.getNodeValue(); if (isAttr(testNode) && isAttr(controlNode) && (hasNsPrefix(testNodeValue) || hasNsPrefix(controlNodeValue))) { String testValueNsResolved = resolveNamespaces(testNode, testNodeValue); String controlValueNsResolved = resolveNamespaces(controlNode, controlNodeValue); return testValueNsResolved.equals(controlValueNsResolved); } return false; }
/** * Ignores differences that are only due to missing xsi:type. */ private int attributeDifferenceFound(Difference difference) { NamedNodeMap controlAttributes = difference.getControlNodeDetail().getNode().getAttributes(); NamedNodeMap testAttributes = difference.getTestNodeDetail().getNode().getAttributes(); Map<QName, Node> controlAttributesMap = createAttributesMapExcludingXsiType(controlAttributes); Map<QName, Node> testAttributesMap = createAttributesMapExcludingXsiType(testAttributes); if (controlAttributesMap.size() != testAttributesMap.size()) { return DifferenceListener.RETURN_ACCEPT_DIFFERENCE; } return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; }
@Override public int differenceFound(Difference diff) { if (diff.getId() == DifferenceConstants.SCHEMA_LOCATION_ID || diff.getId() == DifferenceConstants.NAMESPACE_PREFIX_ID) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } if (diff.getId() == DifferenceConstants.TEXT_VALUE_ID) { for (NodeDetail detail : ImmutableSet.of(diff.getControlNodeDetail(), diff.getTestNodeDetail())) { if (detail.getNode().getParentNode().getChildNodes().getLength() < 2 || !detail.getValue().trim().isEmpty()) { return RETURN_ACCEPT_DIFFERENCE; } } return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } return RETURN_ACCEPT_DIFFERENCE; }
/** * Checks for logical equivalence of the qualified node names. */ private int namespaceDifferenceFound(Difference difference) { Node controlNode = difference.getControlNodeDetail().getNode(); Node testNode = difference.getTestNodeDetail().getNode(); String controlNs = getNamespaceURI(controlNode); String testNs = getNamespaceURI(testNode); if (Objects.equal(controlNs, testNs)) { if (Objects.equal(controlNode.getLocalName(), testNode.getLocalName())) { return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } } return DifferenceListener.RETURN_ACCEPT_DIFFERENCE; }
@Override public int differenceFound(Difference diff) { if (diff.getId() == DifferenceConstants.SCHEMA_LOCATION_ID || diff.getId() == DifferenceConstants.NAMESPACE_PREFIX_ID) { return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } if (diff.getId() == DifferenceConstants.TEXT_VALUE_ID) { for (NodeDetail detail : ImmutableSet.of(diff.getControlNodeDetail(), diff.getTestNodeDetail())) { if (detail.getNode().getParentNode().getChildNodes().getLength() < 2 || !detail.getValue().trim().isEmpty()) { return RETURN_ACCEPT_DIFFERENCE; } } return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL; } return RETURN_ACCEPT_DIFFERENCE; }