All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yag.punner.suite.Executor.kt Maven / Gradle / Ivy

Go to download

Punner is a parallel JUnit test runner and maven plugin which can speed up your unit test.

There is a newer version: 0.9.17
Show newest version
package com.github.yag.punner.suite

import com.github.yag.punner.core.getPooledRunner
import com.github.yag.punner.report.TestResults
import org.junit.runner.Computer
import org.junit.runner.Result
import org.junit.runner.manipulation.Filter
import org.junit.runner.manipulation.Filterable
import org.junit.runner.notification.RunNotifier
import java.util.regex.Pattern

private val coreBasedPattern = Pattern.compile("([\\d]+)[cC]")

fun getParallel(value: String) : Int {
    val matcher = coreBasedPattern.matcher(value)

    return if (matcher.matches()) {
        matcher.group(1).toInt() * Runtime.getRuntime().availableProcessors()
    } else {
        value.toInt()
    }
}

internal class Executor(
        val overrideDefault: Boolean = false,
        val classes: Collection> = emptyList(),
        val punnerListener: PunnerListener = PunnerListener(),
        val filter: Collection = emptyList()
        ) {

    fun exec() : TestResults {
        val notifier = RunNotifier()
        val result = Result()

        val builder = PSuiteBuilder(true, overrideDefault)
        val runner = Computer().getSuite(builder, classes.toTypedArray())

        if (runner is Filterable) {
            filter.forEach {
                runner.filter(it)
            }
        }

        notifier.addFirstListener(result.createListener())
        notifier.addListener(punnerListener)

        val pool = getPooledRunner()

        notifier.fireTestRunStarted(runner.description)
        runner.run(notifier)
        pool.close()

        notifier.fireTestRunFinished(result)
        return TestResults(punnerListener.results)
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy