This construction was used in one unit-test. I had to check whether the XML string returned by tested function is in accordance with xsd scheme defined, the routine below:
try
{
String actualResult = testedFunction();
InputStream inputStream = new ByteArrayInputStream(actualResult.getBytes());
StreamSource ss = new StreamSource(inputStream);
// get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
// create schema by reading it from an XSD file:
Schema schema = factory.newSchema(new StreamSource("xsd_file_path"));
Validator validator = schema.newValidator();
// at last perform validation:
validator.validate(ss);
}
catch(SAXException ex) {
// we are here if the document is not valid:
// ... process validation error...
fail("Output result is not valid");
ex.printStackTrace();
}
catch(Exception e) {
// Mustn't reach this code
fail();
}
No comments:
Post a Comment