use of org.cerberus.service.xmlunit.Differences in project cerberus-source by cerberustesting.
the class XmlUnitService method removeDifference.
@Override
public String removeDifference(String pattern, String differences) {
if (pattern == null || differences == null) {
LOG.warn("Null argument");
return null;
}
try {
// Gets the difference list from the differences
Differences current = Differences.fromString(differences);
Differences returned = new Differences();
// Compiles the given pattern
Pattern compiledPattern = Pattern.compile(pattern);
for (org.cerberus.service.xmlunit.Difference currentDiff : current.getDifferences()) {
if (compiledPattern.matcher(currentDiff.getDiff()).matches()) {
continue;
}
returned.addDifference(currentDiff);
}
// String XML representation
return returned.mkString();
} catch (DifferencesException e) {
LOG.warn("Unable to remove differences", e);
}
return null;
}
use of org.cerberus.service.xmlunit.Differences in project cerberus-source by cerberustesting.
the class XmlUnitService method getDifferencesFromXml.
@Override
public String getDifferencesFromXml(String left, String right) {
try {
// Gets the detailed diff between left and right argument
Document leftDocument = inputTranslator.translate(left);
Document rightDocument = inputTranslator.translate(right);
DetailedDiff diffs = new DetailedDiff(XMLUnit.compareXML(leftDocument, rightDocument));
// Creates the result structure which will contain difference list
Differences resultDiff = new Differences();
// Add each difference to our result structure
for (Object diff : diffs.getAllDifferences()) {
if (!(diff instanceof Difference)) {
LOG.warn("Unable to handle no XMLUnit Difference " + diff);
continue;
}
Difference wellTypedDiff = (Difference) diff;
String xPathLocation = wellTypedDiff.getControlNodeDetail().getXpathLocation();
// Then we retrieve XPath from the right structure.
if (xPathLocation == null) {
xPathLocation = wellTypedDiff.getTestNodeDetail().getXpathLocation();
}
// This case should never happen
if (xPathLocation == null) {
LOG.warn("Null left and right differences found");
xPathLocation = NULL_XPATH;
}
resultDiff.addDifference(new org.cerberus.service.xmlunit.Difference(xPathLocation));
}
// Finally returns the String representation of our result structure
return resultDiff.mkString();
} catch (InputTranslatorException e) {
LOG.warn("Unable to get differences from XML", e);
}
return null;
}
Aggregations