/src/main/resources/com/onresolve/jira/groovy/canned/admin/RunUnitTests.groovy
Groovy | 126 lines | 106 code | 19 blank | 1 comment | 6 complexity | 5a5eda23b5c26670194f9907b9648111 MD5 | raw file
- package com.onresolve.jira.groovy.canned.admin
-
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.atlassian.jira.util.ErrorCollection
- import org.apache.log4j.Category
- import com.atlassian.jira.util.SimpleErrorCollection
- import com.onresolve.jira.groovy.ScriptManager
- import com.atlassian.jira.ComponentManager
- import org.junit.runner.Result
- import org.junit.runner.notification.Failure
- import groovy.xml.MarkupBuilder
- import org.junit.runner.JUnitCore
- import org.junit.runner.Request
- import com.atlassian.jira.component.ComponentAccessor
-
- class RunUnitTests implements CannedScript {
-
- Category log = Category.getInstance(RunUnitTests.class)
- public static FIELD_CLASS_NAMES = "FIELD_CLASS_NAMES"
- def scriptManager
-
- String getName() {
- "Run unit tests"
- }
-
- String getDescription() {
- "Run unit tests in classes on the server"
- }
-
- List getCategories() {
- ["ADMIN"]
- }
-
- List getParameters(Map params) {
- [
- [
- Name:FIELD_CLASS_NAMES,
- Label:"Class names",
- Description:"Names of classes separated by a space"
- ]
- ]
- }
-
- ErrorCollection doValidate(Map params, boolean forPreview) {
- new SimpleErrorCollection();
- }
-
- Map doScript(Map params) {
-
- List<Class> clazzes = []
- String methodName = null
- (params[FIELD_CLASS_NAMES] as String).split(/\s+/).each {String s ->
- if (s.contains("#")) {
- List bits = s.split("#") as List
- s = bits[0]
- methodName = bits[1]
- }
-
- def gcl = this.class.classLoader.parent
- clazzes.add(gcl.loadClass(s, true, false))
- }
-
- Result result
- if (clazzes.size() == 1 && methodName) {
- log.warn ("Execute ${clazzes[0].getName()} method $methodName")
- JUnitCore jUnitCore = new JUnitCore()
- result = jUnitCore.run(Request.method(clazzes[0], methodName))
- }
- else {
- result = org.junit.runner.JUnitCore.runClasses(clazzes as Class[])
- }
-
- StringWriter writer = new StringWriter()
- MarkupBuilder builder = new MarkupBuilder(writer)
- builder.table {
- tr{
- td("Run")
- td(result.getRunCount())
- }
- tr{
- td("Failures")
- td(result.getFailureCount())
- }
- }
- builder.table {
- result.getFailures().each {Failure failure ->
- tr{
- td{
- img(src:"../../../images/icons/emoticons/error.png", class:"testsFailed")
- }
- td (failure.getDescription())
- }
- tr {
- td (colspan:"2") {
- pre(failure.getException().class.name + ":\n" + failure.getMessage())
- }
- }
- log.warn failure.getTrace()
- }
- }
-
- if (! result.getFailureCount()) {
- builder.p(class: "testsPassed", "Tests passed")
- }
-
- params["output"] = writer.toString()
-
- result.getFailures().each {Failure failure ->
- log.debug (failure.getDescription())
- }
- // org.junit.runner.JUnitCore.main(params[FIELD_CLASS_NAMES] as String)
- params
- }
-
- String getDescription(Map params, boolean forPreview) {
- "Run tests in..."
- }
-
- public Boolean isFinalParamsPage(Map params) {
- true
- }
-
- public void setScriptManager(def scriptManager) {
- this.scriptManager = scriptManager
- }
- }