import fr.aerow.opentext.apmclass.ApmTestCase import org.junit.runner.Description import org.junit.runner.JUnitCore import org.junit.runner.notification.Failure import org.junit.runner.notification.RunListener import org.junit.runner.notification.RunNotifier import org.junit.runner.Result import static groovy.xml.XmlUtil.escapeXml @groovy.transform.CompileStatic // Use TypeChecked if metaClass is needed class ApmTestsListenerHtml extends RunListener { private PrintWriter output private Map allTests // Map of test names and success status ApmTestsListenerHtml(PrintWriter writer) { allTests = [:] output = writer } // Called before any tests have been run. void testRunStarted(Description desc) { output.println '''\ | '''.stripMargin() def numOfTests = desc.testCount() output.println "

Running ${numOfTests} unit test${numOfTests != 1 ? 's' : ''}…

" } // Called when all tests have finished. void testRunFinished(Result res) { output.println "

Unit tests ${res.wasSuccessful() ? 'OK' : 'KO'}" output.println '

Tests

' output.println '''\ | | | | | | | | '''.stripMargin() allTests.each { name, wasSuccessful -> output.println """\ | | | | """.stripMargin() } output.println '
Method nameResult
| ${name} | | | ${wasSuccessful ? 'PASSED' : 'FAILED'} | |
' output.println '

Failures

' res.failures.each() { Failure fail -> output.println """\ |
  • |
    | | ${fail.getTestHeader()} | |
    ${escapeXml(fail.getTrace())}
    |
    |
  • """.stripMargin() } output.println '

    Summary

    ' def passCount = res.getRunCount() - res.getFailureCount() output.println "

    Test results: ${passCount} passed, ${res.getFailureCount()} failed.

    " } // Called when an atomic test is about to be started. void testStarted(Description desc) { String methodName = desc.getMethodName() allTests[methodName] = true } // Called when an atomic test has finished, whether the test succeeds or fails. void testFinished(Description desc) { // output.println "

    Test ${desc.getMethodName()} done

    " } // Called when an atomic test fails, or when a listener throws an exception. void testFailure(Failure fail) { String methodName = fail.getDescription().getMethodName() allTests[methodName] = false //output.println """${fail.getDescription().getMethodName()} FAILED""" } // Called when an atomic test flags that it assumes a condition that is false. // Such tests should not really be considered “failed” (for example, checking for Windows paths while tests run on Linux) void testAssumptionFailure(Failure fail) { // output.print "Failed: ${fail.getDescription().getMethodName()}" } // Called when a test will not be run, generally because a test method is annotated with @org.junit.Ignore void testIgnored(Description desc) { //output.println "Ignored: ${desc.getMethodName()}" } // Called when a test suite is about to be started. void testSuiteStarted(Description desc) { // TODO } // Called when a test suite has finished, whether the test suite succeeds or fails. void testSuiteFinished(Description desc) { // TODO } } /* ** @author Dimitri Torterat */ def classToLoad long testScriptId try { testScriptId = Long.parseLong(params.dataId) } catch (Exception e) { /* gui.contentType = 'application/problem+json' def jsonOutput = JsonOutput.toJson(// See RFC 7807 [ title: 'Bad request – Invalid document identifier', status: 403, detail: "params.dataId (${params.dataId}) is invalid. Your URL should contain dataId=123456", instance: "${params?.myurl}", ]) response.success(jsonOutput, 403) return */ out << "

    params.dataId (${params.dataId}) is invalid. Your URL should contain dataId=123456.

    " return } try { classToLoad = docman.runContentScript(testScriptId, binding) } catch (com.answer.modules.cscript.api.exception.InvalidParamException e) { out << "

    Invalid node ID ${testScriptId}.

    " out << '

    '
        printError e
        out << '
    ' return } out.withPrintWriter { writer -> if (classToLoad && classToLoad in ApmTestCase) { def apmTestsListener = new ApmTestsListenerHtml(writer) def runner = new JUnitCore() runner.addListener(apmTestsListener) runner.run(classToLoad) writer.flush() } else { writer.println '''

    No test classes have been found.

    Make sure your script contains the word “test” in its name (case-insensitive) and returns a class extending ApmTestCase.

    ''' } }