use of org.cerberus.service.xmlunit.InputTranslatorException in project cerberus-source by cerberustesting.
the class XmlUnitService method initInputTranslator.
/**
* Initializes {@link #inputTranslator} by two {@link InputTranslator}
* <ul>
* <li>One for handle the <code>url</code> prefix</li>
* <li>One for handle without prefix</li>
* </ul>
*/
private void initInputTranslator() {
inputTranslator = new InputTranslatorManager<Document>();
// Add handling on the "url" prefix, to get URL input
inputTranslator.addTranslator(new AInputTranslator<Document>("url") {
@Override
public Document translate(String input) throws InputTranslatorException {
try {
URL urlInput = new URL(InputTranslatorUtil.getValue(input));
return XmlUtil.fromURL(urlInput);
} catch (MalformedURLException e) {
throw new InputTranslatorException(e);
} catch (XmlUtilException e) {
throw new InputTranslatorException(e);
}
}
});
// Add handling for raw XML input
inputTranslator.addTranslator(new AInputTranslator<Document>(null) {
@Override
public Document translate(String input) throws InputTranslatorException {
try {
return XmlUtil.fromString(input);
} catch (XmlUtilException e) {
throw new InputTranslatorException(e);
}
}
});
}
use of org.cerberus.service.xmlunit.InputTranslatorException 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