Search in sources :

Example 1 with Node

use of org.w3c.tidy.Node in project jmeter by apache.

the class HTMLAssertion method getResult.

/**
     * Returns the result of the Assertion. If so an AssertionResult containing
     * a FailureMessage will be returned. Otherwise the returned AssertionResult
     * will reflect the success of the Sample.
     */
@Override
public AssertionResult getResult(SampleResult inResponse) {
    log.debug("HTMLAssertions.getResult() called");
    // no error as default
    AssertionResult result = new AssertionResult(getName());
    if (inResponse.getResponseData().length == 0) {
        return result.setResultForNull();
    }
    result.setFailure(false);
    // create parser
    Tidy tidy = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Setting up tidy... doctype: {}, errors only: {}, error threshold: {}, warning threshold: {}, html mode: {}, xhtml mode: {}, xml mode: {}.", getDoctype(), isErrorsOnly(), getErrorThreshold(), getWarningThreshold(), isHTML(), isXHTML(), isXML());
        }
        tidy = new Tidy();
        tidy.setInputEncoding(StandardCharsets.UTF_8.name());
        tidy.setOutputEncoding(StandardCharsets.UTF_8.name());
        tidy.setQuiet(false);
        tidy.setShowWarnings(true);
        tidy.setOnlyErrors(isErrorsOnly());
        tidy.setDocType(getDoctype());
        if (isXHTML()) {
            tidy.setXHTML(true);
        } else if (isXML()) {
            tidy.setXmlTags(true);
        }
        tidy.setErrfile(getFilename());
        if (log.isDebugEnabled()) {
            log.debug("Tidy instance created... err file: {}, tidy parser: {}", getFilename(), tidy);
        }
    } catch (Exception e) {
        log.error("Unable to instantiate tidy parser", e);
        result.setFailure(true);
        result.setFailureMessage("Unable to instantiate tidy parser");
        // return with an error
        return result;
    }
    /*
         * Run tidy.
         */
    try {
        log.debug("HTMLAssertions.getResult(): start parsing with tidy ...");
        StringWriter errbuf = new StringWriter();
        tidy.setErrout(new PrintWriter(errbuf));
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        log.debug("Parsing with tidy starting...");
        Node node = tidy.parse(new ByteArrayInputStream(inResponse.getResponseData()), os);
        log.debug("Parsing with tidy done! node: {}, output: {}", node, os);
        // write output to file
        writeOutput(errbuf.toString());
        // evaluate result
        if ((tidy.getParseErrors() > getErrorThreshold()) || (!isErrorsOnly() && (tidy.getParseWarnings() > getWarningThreshold()))) {
            log.debug("Errors/warnings detected while parsing with tidy: {}", errbuf);
            result.setFailure(true);
            result.setFailureMessage(MessageFormat.format("Tidy Parser errors:   " + tidy.getParseErrors() + " (allowed " + getErrorThreshold() + ") " + "Tidy Parser warnings: " + tidy.getParseWarnings() + " (allowed " + getWarningThreshold() + ")", new Object[0]));
        // return with an error
        } else if ((tidy.getParseErrors() > 0) || (tidy.getParseWarnings() > 0)) {
            // return with no error
            log.debug("HTMLAssertions.getResult(): there were errors/warnings but threshold to high");
            result.setFailure(false);
        } else {
            // return with no error
            log.debug("HTMLAssertions.getResult(): no errors/warnings detected:");
            result.setFailure(false);
        }
    } catch (Exception e) {
        // return with an error
        log.warn("Cannot parse result content", e);
        result.setFailure(true);
        result.setFailureMessage(e.getMessage());
    }
    return result;
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.w3c.tidy.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Tidy(org.w3c.tidy.Tidy) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Node (org.w3c.tidy.Node)1 Tidy (org.w3c.tidy.Tidy)1