Search in sources :

Example 1 with Validator

use of org.custommonkey.xmlunit.Validator in project jslint4java by happygiraffe.

the class XmlResultFormatterTest method assertValid.

private void assertValid(File output) throws FileNotFoundException, SAXException {
    InputSource xml = new InputSource(new FileInputStream(output));
    URL dtdUrl = getClass().getClassLoader().getResource(DTD_RESOURCE);
    assertThat("resource " + DTD_RESOURCE + " exists", dtdUrl, notNullValue());
    // Specify a validator as the documents don't have <!DOCTYPE jslint> in them.
    Validator validator = new Validator(xml, dtdUrl.toString(), "jslint");
    assertThat(validator.toString(), validator.isValid(), is(true));
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Validator(org.custommonkey.xmlunit.Validator)

Example 2 with Validator

use of org.custommonkey.xmlunit.Validator in project wcomponents by BorderTech.

the class ValidatorApp method assertSchemaMatch.

/**
 * 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();
}
Also used : StringReader(java.io.StringReader) Validator(org.custommonkey.xmlunit.Validator)

Example 3 with Validator

use of org.custommonkey.xmlunit.Validator in project wcomponents by BorderTech.

the class AbstractWebXmlRendererTestCase method setupValidator.

/**
 * 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");
    }
}
Also used : StringReader(java.io.StringReader) File(java.io.File) Validator(org.custommonkey.xmlunit.Validator) SAXException(org.xml.sax.SAXException)

Example 4 with Validator

use of org.custommonkey.xmlunit.Validator in project wcomponents by BorderTech.

the class AbstractWebXmlRendererTestCase method assertSchemaMismatch.

/**
 * Asserts that the given xhtml does not match 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 assertSchemaMismatch(final String xhtml) throws IOException, SAXException {
    Validator validator = getSchemaValidator(xhtml);
    Assert.assertFalse("xhtml should not validate", validator.isValid());
}
Also used : Validator(org.custommonkey.xmlunit.Validator)

Example 5 with Validator

use of org.custommonkey.xmlunit.Validator in project wcomponents by BorderTech.

the class AbstractWebXmlRendererTestCase method getSchemaValidator.

/**
 * 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) {
    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);
        return validator;
    } catch (Exception e) {
        return null;
    }
}
Also used : StringReader(java.io.StringReader) Validator(org.custommonkey.xmlunit.Validator) IOException(java.io.IOException) XpathException(org.custommonkey.xmlunit.exceptions.XpathException) SAXException(org.xml.sax.SAXException)

Aggregations

Validator (org.custommonkey.xmlunit.Validator)6 StringReader (java.io.StringReader)3 SAXException (org.xml.sax.SAXException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 XpathException (org.custommonkey.xmlunit.exceptions.XpathException)1 InputSource (org.xml.sax.InputSource)1