/** * Asserts that the given xhtml matches the schema. * * @param xhtml the xhtmlto validate. * * @throws SAXException if there is a parsing error */ public void assertSchemaMatch(final String xhtml) throws SAXException { // Load the schema. Object schema = getClass().getResource("/schema/ui/v1/schema.xsd").toString(); // Validate the xhtml. Validator validator; StringReader reader = new StringReader(xhtml); validator = new Validator(reader); validator.useXMLSchema(true); validator.setJAXP12SchemaSource(schema); validator.assertIsValid(); }
/** * Creates an XML Unit validator for the given xml. * * @param xml the xml to validate * @return an XML Unit validator that for validating the xml. */ public Validator setupValidator(final String xml) { try { StringReader reader = new StringReader(xml); Validator validator = new Validator(reader); validator.useXMLSchema(true); validator.setJAXP12SchemaSource(new File(getSchemaPath())); return validator; } catch (SAXException ex) { LOG.error("Unexpected error while testing", ex); throw new IllegalStateException("Unable to set up validator"); } }
/** * Obtains an XMLUnit schema validator for validating the output. * * @param xhtml the html to validate * @return the validator to use. */ protected Validator getSchemaValidator(final String xhtml) { // Some web components generate a fragment of xhtml markup that does not // have a single root element, so we add a "body" root element to the // markup fragment and the schema fragment. try { // Load the schema. String schemaPath = getSchemaPath(); Object schema = AbstractWebXmlRendererTestCase.class.getResource(schemaPath).toString(); String wrappedXhtml = wrapXHtml(xhtml); // Validate the xhtml. Validator validator; StringReader reader = new StringReader(wrappedXhtml); validator = new Validator(reader); validator.useXMLSchema(true); validator.setJAXP12SchemaSource(schema); // validator. return validator; } catch (Exception e) { return null; } }