PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/canned/admin/RunUnitTests.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 126 lines | 106 code | 19 blank | 1 comment | 6 complexity | 5a5eda23b5c26670194f9907b9648111 MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.admin
  2. import com.onresolve.jira.groovy.canned.CannedScript
  3. import com.atlassian.jira.util.ErrorCollection
  4. import org.apache.log4j.Category
  5. import com.atlassian.jira.util.SimpleErrorCollection
  6. import com.onresolve.jira.groovy.ScriptManager
  7. import com.atlassian.jira.ComponentManager
  8. import org.junit.runner.Result
  9. import org.junit.runner.notification.Failure
  10. import groovy.xml.MarkupBuilder
  11. import org.junit.runner.JUnitCore
  12. import org.junit.runner.Request
  13. import com.atlassian.jira.component.ComponentAccessor
  14. class RunUnitTests implements CannedScript {
  15. Category log = Category.getInstance(RunUnitTests.class)
  16. public static FIELD_CLASS_NAMES = "FIELD_CLASS_NAMES"
  17. def scriptManager
  18. String getName() {
  19. "Run unit tests"
  20. }
  21. String getDescription() {
  22. "Run unit tests in classes on the server"
  23. }
  24. List getCategories() {
  25. ["ADMIN"]
  26. }
  27. List getParameters(Map params) {
  28. [
  29. [
  30. Name:FIELD_CLASS_NAMES,
  31. Label:"Class names",
  32. Description:"Names of classes separated by a space"
  33. ]
  34. ]
  35. }
  36. ErrorCollection doValidate(Map params, boolean forPreview) {
  37. new SimpleErrorCollection();
  38. }
  39. Map doScript(Map params) {
  40. List<Class> clazzes = []
  41. String methodName = null
  42. (params[FIELD_CLASS_NAMES] as String).split(/\s+/).each {String s ->
  43. if (s.contains("#")) {
  44. List bits = s.split("#") as List
  45. s = bits[0]
  46. methodName = bits[1]
  47. }
  48. def gcl = this.class.classLoader.parent
  49. clazzes.add(gcl.loadClass(s, true, false))
  50. }
  51. Result result
  52. if (clazzes.size() == 1 && methodName) {
  53. log.warn ("Execute ${clazzes[0].getName()} method $methodName")
  54. JUnitCore jUnitCore = new JUnitCore()
  55. result = jUnitCore.run(Request.method(clazzes[0], methodName))
  56. }
  57. else {
  58. result = org.junit.runner.JUnitCore.runClasses(clazzes as Class[])
  59. }
  60. StringWriter writer = new StringWriter()
  61. MarkupBuilder builder = new MarkupBuilder(writer)
  62. builder.table {
  63. tr{
  64. td("Run")
  65. td(result.getRunCount())
  66. }
  67. tr{
  68. td("Failures")
  69. td(result.getFailureCount())
  70. }
  71. }
  72. builder.table {
  73. result.getFailures().each {Failure failure ->
  74. tr{
  75. td{
  76. img(src:"../../../images/icons/emoticons/error.png", class:"testsFailed")
  77. }
  78. td (failure.getDescription())
  79. }
  80. tr {
  81. td (colspan:"2") {
  82. pre(failure.getException().class.name + ":\n" + failure.getMessage())
  83. }
  84. }
  85. log.warn failure.getTrace()
  86. }
  87. }
  88. if (! result.getFailureCount()) {
  89. builder.p(class: "testsPassed", "Tests passed")
  90. }
  91. params["output"] = writer.toString()
  92. result.getFailures().each {Failure failure ->
  93. log.debug (failure.getDescription())
  94. }
  95. // org.junit.runner.JUnitCore.main(params[FIELD_CLASS_NAMES] as String)
  96. params
  97. }
  98. String getDescription(Map params, boolean forPreview) {
  99. "Run tests in..."
  100. }
  101. public Boolean isFinalParamsPage(Map params) {
  102. true
  103. }
  104. public void setScriptManager(def scriptManager) {
  105. this.scriptManager = scriptManager
  106. }
  107. }