DomUtils { static void validateDocument(Document doc) throws SAXException, IOException { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); InputStream schemaIs = null; try { schemaIs = XCodeVersionInfoMojo.class .getResourceAsStream("/com/sap/tip/production/xcode/VersionInfoSchema.xsd"); StreamSource[] inputSources = new StreamSource[1]; inputSources[0] = new StreamSource(schemaIs); Schema schema = schemaFactory.newSchema(inputSources); Validator validator = schema.newValidator(); validator.validate(new DOMSource(doc)); } finally { if (schemaIs != null) { schemaIs.close(); } } } private  DomUtils();   }
@Test public void testInvalidDoc() throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new File("src/test/resources/invalidXML.xml")); try { DomUtils.validateDocument(doc); Assert.fail("Invalid document not detected"); } catch (SAXException e) { Assert.assertEquals( "cvc-complex-type.2.4.b: The content of element 'scm' is not complete. One of '{revision}' is expected.", e.getMessage()); } }
@Test public void testValidDoc() throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new File("src/test/resources/validXML.xml")); DomUtils.validateDocument(doc); }