From 52e4d404bc451502cb9050a927bb45501ccac24a Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 8 May 2016 23:23:37 -0800 Subject: [PATCH] Tests for ITestJvmFlag{Contributor,Interceptor}. --- .../beust/kobalt/internal/GenericRunner.kt | 68 ++++++++++------- .../beust/kobalt/internal/KobaltPluginXml.kt | 19 ++++- .../kobalt/internal/GenericRunnerTest.kt | 75 +++++++++++++++++++ 3 files changed, 130 insertions(+), 32 deletions(-) create mode 100644 src/test/kotlin/com/beust/kobalt/internal/GenericRunnerTest.kt diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt index 7da58b79..90266dab 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt @@ -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, 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().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().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, pluginInfo: IPluginInfo) : List { + // Default JVM args + val jvmFlags = arrayListOf().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 + } + } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt index ab0c374b..b062dc62 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt @@ -50,12 +50,22 @@ class ClassNameXml { var className: List = arrayListOf() } +interface IPluginInfo { + val testJvmFlagContributors : List + val testJvmFlagInterceptors : List +} + +open class BasePluginInfo : IPluginInfo { + override val testJvmFlagContributors = arrayListOf() + override val testJvmFlagInterceptors = arrayListOf() +} /** * 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() val projectContributors = arrayListOf() val classpathContributors = arrayListOf() @@ -80,8 +90,11 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?, // Not documented yet val buildConfigContributors = arrayListOf() val mavenIdInterceptors = arrayListOf() - val testJvmFlagContributors = arrayListOf() - val testJvmFlagInterceptors = arrayListOf() + + // 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() + override val testJvmFlagInterceptors = arrayListOf() companion object { /** diff --git a/src/test/kotlin/com/beust/kobalt/internal/GenericRunnerTest.kt b/src/test/kotlin/com/beust/kobalt/internal/GenericRunnerTest.kt new file mode 100644 index 00000000..b23de295 --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/internal/GenericRunnerTest.kt @@ -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): List { + 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): List { + return currentFlags.map { if (it == A_JAR) B_JAR else it } + } + } + + @BeforeClass + fun beforeClass() { + Kobalt.init(TestModule()) + } + + private fun runTest(pluginInfo: IPluginInfo, expected: List) { + 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")) + } +} +