, . - . , . junit, , junit.
,
assertEquivalentXml(expectedXML, testXML, null, null);
, / :
assertEquivalentXml(expectedXML, testXML,
new String[]{"dataset", "categories"}, new String[]{"color", "anchorBorderColor", "anchorBgColor"});
:
public class XmlProviderAssertions extends Assert {
public static void assertEquivalentXml(String expectedXML, String testXML, String[] nodesWithOrderedChildren, String[] attributesToIgnore) {
Set<String> setOfNodesWithOrderedChildren = new HashSet<String>();
if(nodesWithOrderedChildren != null ) {
Collections.addAll(setOfNodesWithOrderedChildren, nodesWithOrderedChildren);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setIgnoringComments(true);
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
fail("Error testing XML");
}
Document expectedXMLDoc = null;
Document testXMLDoc = null;
try {
expectedXMLDoc = db.parse(new ByteArrayInputStream(expectedXML.getBytes()));
expectedXMLDoc.normalizeDocument();
testXMLDoc = db.parse(new ByteArrayInputStream(testXML.getBytes()));
testXMLDoc.normalizeDocument();
} catch (SAXException e) {
fail("Could not parse testXML");
} catch (IOException e) {
fail("Could not read testXML");
}
NodeList expectedChildNodes = expectedXMLDoc.getLastChild().getChildNodes();
NodeList testChildNodes = testXMLDoc.getLastChild().getChildNodes();
assertEquals("Test XML does not have expected amount of child nodes", expectedChildNodes.getLength(), testChildNodes.getLength());
Document expectedDEDoc = getNodeAsDocument(expectedXMLDoc.getDocumentElement(), db, false);
Document testDEDoc = getNodeAsDocument(testXMLDoc.getDocumentElement(), db, false);
Diff diff = new Diff(expectedDEDoc, testDEDoc);
assertTrue("Test XML parent node doesn't match expected XML parent node. " + diff.toString(), diff.similar());
for(int i=0; i < expectedChildNodes.getLength(); i++) {
Node expectedChildNode = expectedChildNodes.item(i);
if( expectedChildNode.getNodeType() == Node.TEXT_NODE ) {
continue;
}
Document expectedChildDoc = getNodeAsDocument(expectedChildNode, db, true);
boolean hasSimilar = false;
StringBuilder messages = new StringBuilder();
for(int j=0; j < testChildNodes.getLength(); j++) {
Node testChildNode = testChildNodes.item(j);
if( testChildNode.getNodeType() == Node.TEXT_NODE ) {
continue;
}
Document testChildDoc = getNodeAsDocument(testChildNode, db, true);
diff = new Diff(expectedChildDoc, testChildDoc);
if( !setOfNodesWithOrderedChildren.contains( expectedChildDoc.getDocumentElement().getNodeName() ) ) {
diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
}
if(attributesToIgnore != null) {
diff.overrideDifferenceListener(new IgnoreNamedAttributesDifferenceListener(attributesToIgnore));
}
messages.append(diff.toString());
boolean similar = diff.similar();
if(similar) {
hasSimilar = true;
}
}
assertTrue("Test XML does not match expected XML. " + messages, hasSimilar);
}
}
private static Document getNodeAsDocument(Node node, DocumentBuilder db, boolean deep) {
Document nodeDoc = db.newDocument();
Node importedNode = nodeDoc.importNode(node, deep);
nodeDoc.appendChild(importedNode);
return nodeDoc;
}
}
class IgnoreNamedAttributesDifferenceListener implements DifferenceListener {
Set<String> attributeBlackList;
public IgnoreNamedAttributesDifferenceListener(String[] attributeNames) {
attributeBlackList = new HashSet<String>();
Collections.addAll(attributeBlackList, attributeNames);
}
public int differenceFound(Difference difference) {
int differenceId = difference.getId();
if (differenceId == DifferenceConstants.ATTR_VALUE_ID) {
if(attributeBlackList.contains(difference.getControlNodeDetail().getNode().getNodeName())) {
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
}
}
return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
}
public void skippedComparison(Node node, Node node1) {
}
}