/** * Assert that an InputSource containing XML contains valid XML: * the document must contain a DOCTYPE declaration to be validated * @param xml * @throws SAXException * @throws ConfigurationException if validation could not be turned on * @see Validator */ public static void assertXMLValid(InputSource xml) throws SAXException, ConfigurationException { assertXMLValid(new Validator(xml)); }
/** * 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(); }
/** * Actually perform validation. */ private void validate() { if (isValid != null) { return; } try { parser.parse(validationInputSource, this); } catch (SAXException e) { parserException(e); } catch (IOException e) { parserException(e); } if (isValid == null) { isValid = Boolean.TRUE; } else if (usingDoctypeReader) { try { messages.append("\nContent was: ") .append(getOriginalContent(validationInputSource)); } catch (IOException e) { // silent but deadly? } } }
/** * 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"); } }
Validator validator = new Validator(result); validator.useXMLSchema(true); validator.assertIsValid();
/** * Append any validation message(s) to the specified StringBuffer. * * @param toAppendTo * @return specified StringBuffer with message(s) appended */ private StringBuilder appendMessage(StringBuilder toAppendTo) { if (isValid()) { return toAppendTo.append("[valid]"); } return toAppendTo.append(messages); }
schemaSourceList.add(new StreamSource(systemId)); addSchemaSources(schemaSource, schemaSourceList); v.setSchemaSources(schemaSourceList.toArray(new Source[0])); isValid = r.isValid() ? Boolean.TRUE : Boolean.FALSE; for (ValidationProblem p : r.getProblems()) { validationProblem(p); try { messages.append("\nContent was: ") .append(getOriginalContent(validationInputSource)); } catch (IOException e) {
/** * Asserts that the given xhtml matches the schema. * * @param xhtml the xhtml to validate. * * @throws IOException if there is an I/O error * @throws SAXException if there is a parsing error */ public void assertSchemaMatch(final String xhtml) throws IOException, SAXException { Validator validator = getSchemaValidator(xhtml); validator.assertIsValid(); }
/** * Deal with any parser exceptions not handled by the ErrorHandler interface. * * @param e */ private void parserException(Exception e) { invalidate(e.getMessage()); }
/** * @return class name appended with validation messages */ public String toString() { StringBuilder builder = new StringBuilder(super.toString()).append(':'); return appendMessage(builder).toString(); }
/** * 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; } }
/** * Assert that a document is valid. */ public void assertIsValid(){ if(!isValid()){ junit.framework.Assert.fail(messages.toString()); } }
/** * Deal with exceptions passed to the ErrorHandler interface by the parser. */ private void errorHandlerException(SAXParseException e) { String msg = "At line " + e.getLineNumber() + ", column: " + e.getColumnNumber() + " ==> " + e.getMessage(); if (!msg.endsWith("\n")) msg += "\n"; invalidate(msg); }
/** * @return class name appended with validation messages */ public String toString() { StringBuffer buf = new StringBuffer(super.toString()).append(':'); return appendMessage(buf).toString(); }
/** * Assert that a String containing XML contains valid XML: the String must * contain a DOCTYPE declaration to be validated * @param xmlString * @throws SAXException * @throws ConfigurationException if validation could not be turned on * @see Validator */ public static void assertXMLValid(String xmlString) throws SAXException, ConfigurationException { assertXMLValid(new Validator(xmlString)); }
/** * Assert that a document is valid. */ public void assertIsValid(){ if(!isValid()){ junit.framework.Assert.fail(messages.toString()); } }
private void validationProblem(ValidationProblem p) { String msg = "At line " + p.getLine() + ", column: " + p.getColumn() + " ==> " + p.getMessage(); if (!msg.endsWith("\n")) { msg += "\n"; } invalidate(msg); }
/** * Assert that a String containing XML contains valid XML: the String must * contain a DOCTYPE declaration to be validated * @param xmlString * @throws SAXException * @throws ConfigurationException if validation could not be turned on * @see Validator */ public static void assertXMLValid(String xmlString) throws SAXException, ConfigurationException { assertXMLValid(new Validator(xmlString)); }