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.
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)
}
}