1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Tests for ITestJvmFlag{Contributor,Interceptor}.

This commit is contained in:
Cedric Beust 2016-05-08 23:23:37 -08:00
parent 86af9256a2
commit 52e4d404bc
3 changed files with 130 additions and 32 deletions

View file

@ -4,6 +4,7 @@ import com.beust.kobalt.*
import com.beust.kobalt.api.*
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import com.google.common.annotations.VisibleForTesting
import java.io.File
import java.util.*
@ -48,8 +49,6 @@ abstract class GenericTestRunner: ITestRunnerContributor {
*/
fun runTests(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
configName: String) : Boolean {
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val java = jvm.javaExecutable
var result = false
val testConfig = project.testConfigs.firstOrNull { it.name == configName }
@ -58,35 +57,12 @@ abstract class GenericTestRunner: ITestRunnerContributor {
val args = args(project, classpath, testConfig)
if (args.size > 0) {
// Default JVM args
val jvmFlags = arrayListOf<String>().apply {
addAll(testConfig.jvmArgs)
add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
}
val pluginInfo = Kobalt.INJECTOR.getInstance(PluginInfo::class.java)
// JVM flags from the contributors
val jvmFlagsFromContributors = pluginInfo.testJvmFlagContributors.flatMap {
it.testJvmFlagsFor(project, context, jvmFlags)
}
// JVM flags from the interceptors (these overwrite flags instead of just adding to the list)
var interceptedJvmFlags = ArrayList(jvmFlags + jvmFlagsFromContributors)
pluginInfo.testJvmFlagInterceptors.forEach {
val newFlags = it.testJvmFlagsFor(project, context, interceptedJvmFlags)
interceptedJvmFlags.clear()
interceptedJvmFlags.addAll(newFlags)
}
if (interceptedJvmFlags.any()) {
log(2, "Final JVM test flags after running the contributors and interceptors: $interceptedJvmFlags")
}
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable
val jvmArgs = calculateAllJvmArgs(project, context, testConfig, classpath,
Kobalt.INJECTOR.getInstance (PluginInfo::class.java))
val allArgs = arrayListOf<String>().apply {
add(java!!.absolutePath)
addAll(interceptedJvmFlags)
addAll(jvmArgs)
add(mainClass)
addAll(args)
}
@ -113,5 +89,39 @@ abstract class GenericTestRunner: ITestRunnerContributor {
}
return result
}
/*
** @return all the JVM flags from contributors and interceptors.
*/
@VisibleForTesting
fun calculateAllJvmArgs(project: Project, context: KobaltContext,
testConfig: TestConfig, classpath: List<IClasspathDependency>, pluginInfo: IPluginInfo) : List<String> {
// Default JVM args
val jvmFlags = arrayListOf<String>().apply {
addAll(testConfig.jvmArgs)
add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
}
// JVM flags from the contributors
val jvmFlagsFromContributors = pluginInfo.testJvmFlagContributors.flatMap {
it.testJvmFlagsFor(project, context, jvmFlags)
}
// JVM flags from the interceptors (these overwrite flags instead of just adding to the list)
var result = ArrayList(jvmFlags + jvmFlagsFromContributors)
pluginInfo.testJvmFlagInterceptors.forEach {
val newFlags = it.testJvmFlagsFor(project, context, result)
result.clear()
result.addAll(newFlags)
}
if (result.any()) {
log(2, "Final JVM test flags after running the contributors and interceptors: $result")
}
return result
}
}

View file

@ -50,12 +50,22 @@ class ClassNameXml {
var className: List<String> = arrayListOf()
}
interface IPluginInfo {
val testJvmFlagContributors : List<ITestJvmFlagContributor>
val testJvmFlagInterceptors : List<ITestJvmFlagInterceptor>
}
open class BasePluginInfo : IPluginInfo {
override val testJvmFlagContributors = arrayListOf<ITestJvmFlagContributor>()
override val testJvmFlagInterceptors = arrayListOf<ITestJvmFlagInterceptor>()
}
/**
* Turn a KobaltPluginXml (the raw content of kobalt-plugin.xml mapped to POJO's) into a PluginInfo object, which
* contains all the contributors instantiated and other information that Kobalt can actually use. Kobalt code that
* needs to access plug-in info can then just inject a PluginInfo object.
*/
class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?, val classLoader: ClassLoader?) {
class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?, val classLoader: ClassLoader?)
: BasePluginInfo() {
val plugins = arrayListOf<IPlugin>()
val projectContributors = arrayListOf<IProjectContributor>()
val classpathContributors = arrayListOf<IClasspathContributor>()
@ -80,8 +90,11 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?,
// Not documented yet
val buildConfigContributors = arrayListOf<IBuildConfigContributor>()
val mavenIdInterceptors = arrayListOf<IMavenIdInterceptor>()
val testJvmFlagContributors = arrayListOf<ITestJvmFlagContributor>()
val testJvmFlagInterceptors = arrayListOf<ITestJvmFlagInterceptor>()
// Note: intentionally repeating them here even though they are defined by our base class so
// that this class always contains the full list of contributors and interceptors
override val testJvmFlagContributors = arrayListOf<ITestJvmFlagContributor>()
override val testJvmFlagInterceptors = arrayListOf<ITestJvmFlagInterceptor>()
companion object {
/**

View file

@ -0,0 +1,75 @@
package com.beust.kobalt.internal
import com.beust.kobalt.Args
import com.beust.kobalt.TestConfig
import com.beust.kobalt.TestModule
import com.beust.kobalt.api.*
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.project
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test
import javax.inject.Inject
/**
* Test ITestJvmFlagContributor and ITestJvmFlagInterceptor.
*/
class DependencyTest @Inject constructor() {
private val project : Project get() = project { name = "dummy" }
private val classpath = listOf(FileDependency("/tmp/a.jar"))
private val context = KobaltContext(Args())
private val contributor = object : ITestJvmFlagContributor {
override fun testJvmFlagsFor(project: Project, context: KobaltContext,
currentFlags: List<String>): List<String> {
return listOf("-agent", "foo")
}
}
private val A_JAR = "/tmp/a.jar"
private val B_JAR = "/tmp/b.jar"
private val interceptor = object : ITestJvmFlagInterceptor {
override fun testJvmFlagsFor(project: Project, context: KobaltContext,
currentFlags: List<String>): List<String> {
return currentFlags.map { if (it == A_JAR) B_JAR else it }
}
}
@BeforeClass
fun beforeClass() {
Kobalt.init(TestModule())
}
private fun runTest(pluginInfo: IPluginInfo, expected: List<String>) {
val result = TestNgRunner().calculateAllJvmArgs(project, context, TestConfig(project),
classpath, pluginInfo)
assertThat(result).isEqualTo(expected)
}
@Test
fun noContributorsNoInterceptors() {
runTest(BasePluginInfo(), listOf("-classpath", A_JAR))
}
@Test
fun contributorOnly() {
runTest(BasePluginInfo().apply { testJvmFlagContributors.add(contributor) },
listOf("-classpath", A_JAR, "-agent", "foo"))
}
@Test
fun interceptorOnly() {
runTest(BasePluginInfo().apply { testJvmFlagInterceptors.add(interceptor) },
listOf("-classpath", B_JAR))
}
@Test
fun contributorAndInterceptor() {
runTest(BasePluginInfo().apply {
testJvmFlagContributors.add(contributor)
testJvmFlagInterceptors.add(interceptor)
},
listOf("-classpath", B_JAR, "-agent", "foo"))
}
}