Advertisement
Diti

ApmTest classes runner

Sep 18th, 2019
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 6.75 KB | None | 0 0
  1. import fr.aerow.opentext.apmclass.ApmTestCase
  2. import org.junit.runner.Description
  3. import org.junit.runner.JUnitCore
  4. import org.junit.runner.notification.Failure
  5. import org.junit.runner.notification.RunListener
  6. import org.junit.runner.notification.RunNotifier
  7. import org.junit.runner.Result
  8. import static groovy.xml.XmlUtil.escapeXml
  9.  
  10. @groovy.transform.CompileStatic // Use TypeChecked if metaClass is needed
  11. class ApmTestsListenerHtml extends RunListener {
  12.    
  13.     private PrintWriter output
  14.     private Map<String,Boolean> allTests // Map of test names and success status
  15.    
  16.     ApmTestsListenerHtml(PrintWriter writer) {
  17.         allTests = [:]
  18.         output = writer
  19.     }
  20.    
  21.     // Called before any tests have been run.
  22.     void testRunStarted(Description desc) {
  23.         output.println '''\
  24.                       | <style>
  25.                       | .apm-testresult-failure details, .apm-testresult-error details {
  26.                       |   cursor: pointer
  27.                       | }
  28.                       | .apm-testresult-failure summary, .apm-testresult-error summary {
  29.                       |   display: inline-block;
  30.                       |   border-bottom: 1px dotted darkred;
  31.                       | }
  32.                       | </style>
  33.                       '''.stripMargin()
  34.         def numOfTests = desc.testCount()
  35.         output.println "<p>Running ${numOfTests} unit test${numOfTests != 1 ? 's' : ''}…</p>"
  36.     }
  37.  
  38.     // Called when all tests have finished.
  39.     void testRunFinished(Result res) {
  40.         output.println "<h2>Unit tests ${res.wasSuccessful() ? 'OK' : 'KO'}"  
  41.         output.println '<h3>Tests</h3>'
  42.         output.println '''\
  43.                       | <table>
  44.                       |   <thead>
  45.                       |     <tr>
  46.                       |       <th>Method name</th>
  47.                       |       <th>Result</th>
  48.                       |     </tr>
  49.                       |   </thead>
  50.                       |   <tbody>
  51.                       '''.stripMargin()
  52.         allTests.each { name, wasSuccessful ->
  53.             output.println """\
  54.                           |   <tr>
  55.                           |     <td>
  56.                           |       <code>${name}</code>
  57.                           |     </td>
  58.                           |     <td>
  59.                           |       <output style="color: ${wasSuccessful ? 'darkgreen' : 'darkred'};">
  60.                           |         ${wasSuccessful ? 'PASSED' : 'FAILED'}
  61.                           |       </output>
  62.                           |     </td>
  63.                           |   </tr>
  64.                           """.stripMargin()
  65.         }
  66.         output.println '</tbody></table>'
  67.        
  68.         output.println '<h3>Failures</h3>'
  69.         res.failures.each() { Failure fail ->
  70.             output.println """\
  71.                           | <li class='apm-testresult-failure'>
  72.                           |   <details>
  73.                           |     <summary>
  74.                           |       <code>${fail.getTestHeader()}</code>
  75.                           |     </summary>
  76.                           |     <pre><samp>${escapeXml(fail.getTrace())}</samp></pre>
  77.                           |   </details>
  78.                           | </li>
  79.                           """.stripMargin()
  80.         }
  81.        
  82.         output.println '<h3>Summary</h3>'
  83.         def passCount = res.getRunCount() - res.getFailureCount()
  84.         output.println "<p>Test results: ${passCount} passed, ${res.getFailureCount()} failed.</p>"
  85.     }
  86.  
  87.     // Called when an atomic test is about to be started.
  88.     void testStarted(Description desc) {
  89.         String methodName = desc.getMethodName()
  90.         allTests[methodName] = true
  91.     }
  92.    
  93.     // Called when an atomic test has finished, whether the test succeeds or fails.
  94.     void testFinished(Description desc) {
  95.         // output.println "<p>Test ${desc.getMethodName()} done</p>"
  96.     }
  97.  
  98.     // Called when an atomic test fails, or when a listener throws an exception.
  99.     void testFailure(Failure fail) {
  100.         String methodName = fail.getDescription().getMethodName()
  101.         allTests[methodName] = false
  102.         //output.println """${fail.getDescription().getMethodName()} <code style="color: darkred;">FAILED</code></li>"""
  103.     }
  104.  
  105.     // Called when an atomic test flags that it assumes a condition that is false.
  106.     // Such tests should not really be considered “failed” (for example, checking for Windows paths while tests run on Linux)
  107.     void testAssumptionFailure(Failure fail) {
  108.         // output.print "Failed: ${fail.getDescription().getMethodName()}"
  109.     }
  110.  
  111.     // Called when a test will not be run, generally because a test method is annotated with @org.junit.Ignore
  112.     void testIgnored(Description desc) {
  113.         //output.println "Ignored: ${desc.getMethodName()}"
  114.     }
  115.    
  116.     // Called when a test suite is about to be started.
  117.     void testSuiteStarted(Description desc) {
  118.         // TODO
  119.     }
  120.    
  121.     // Called when a test suite has finished, whether the test suite succeeds or fails.
  122.     void testSuiteFinished(Description desc) {
  123.         // TODO
  124.     }
  125. }
  126.  
  127. /*
  128. ** @author Dimitri Torterat
  129. */
  130. def classToLoad
  131. long testScriptId
  132.  
  133. try {
  134.     testScriptId = Long.parseLong(params.dataId)
  135. } catch (Exception e) {
  136.     /*
  137.     gui.contentType = 'application/problem+json'
  138.     def jsonOutput = JsonOutput.toJson(// See RFC 7807
  139.         [
  140.             title: 'Bad request – Invalid document identifier',
  141.             status: 403,
  142.             detail: "params.dataId (${params.dataId}) is invalid. Your URL should contain dataId=123456",
  143.             instance: "${params?.myurl}",
  144.         ])
  145.     response.success(jsonOutput, 403)
  146.     return
  147.     */
  148.    
  149.     out << "<p><var>params.dataId</var> (<out>${params.dataId}</out>) is invalid. Your URL should contain <var>dataId=123456</var>.</p>"
  150.     return
  151. }
  152.  
  153. try {
  154.     classToLoad = docman.runContentScript(testScriptId, binding)
  155. } catch (com.answer.modules.cscript.api.exception.InvalidParamException e) {
  156.     out << "<p>Invalid node ID ${testScriptId}.<p>"
  157.     out << '<pre>'
  158.     printError e
  159.     out << '</pre>'
  160.     return
  161. }
  162.  
  163. out.withPrintWriter { writer ->
  164.     if (classToLoad && classToLoad in ApmTestCase) {
  165.         def apmTestsListener = new ApmTestsListenerHtml(writer)
  166.         def runner = new JUnitCore()
  167.        
  168.         runner.addListener(apmTestsListener)
  169.        
  170.         runner.run(classToLoad)
  171.         writer.flush()
  172.     } else {
  173.         writer.println '''<p>No test classes have been found.</p>
  174.                          <p>Make sure your script contains the word “test” in its name (case-insensitive)
  175.                             and returns a class extending <code>ApmTestCase</code>.</p>'''
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement