Kapt: Launch integration tests also under Java 9

This commit is contained in:
Yan Zhulanow
2017-11-16 13:31:21 +09:00
parent 6ccf361942
commit 182c2e2eeb
4 changed files with 98 additions and 46 deletions
@@ -65,7 +65,7 @@ abstract class AbstractKotlinKapt3IntegrationTest : CodegenTestCase() {
super.tearDown()
}
protected fun test(
protected open fun test(
name: String,
vararg supportedAnnotations: String,
options: Map<String, String> = emptyMap(),
@@ -37,7 +37,6 @@ import org.jetbrains.kotlin.kapt3.javac.KaptJavaLog
import org.jetbrains.kotlin.kapt3.parseJavaFiles
import org.jetbrains.kotlin.kapt3.stubs.ClassFileToSourceStubConverter
import org.jetbrains.kotlin.kapt3.util.KaptLogger
import org.jetbrains.kotlin.kapt3.util.isJava9OrLater
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
import org.jetbrains.kotlin.resolve.jvm.extensions.PartialAnalysisHandlerExtension
import org.jetbrains.kotlin.test.ConfigurationKind
@@ -46,11 +45,8 @@ import org.jetbrains.kotlin.test.util.trimTrailingWhitespacesAndAddNewlineAtEOF
import org.jetbrains.kotlin.utils.PathUtil
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.nio.file.Files
import java.util.*
import java.util.concurrent.TimeUnit
import com.sun.tools.javac.util.List as JavacList
abstract class AbstractKotlinKapt3Test : CodegenTestCase() {
@@ -159,7 +155,7 @@ abstract class AbstractKotlinKapt3Test : CodegenTestCase() {
wholeFile: File)
}
open class AbstractClassFileToSourceStubConverterTest : AbstractKotlinKapt3Test() {
open class AbstractClassFileToSourceStubConverterTest : AbstractKotlinKapt3Test(), Java9TestLauncher {
companion object {
private val KOTLIN_METADATA_GROUP = "[a-z0-9]+ = (\\{.+?\\}|[0-9]+)"
private val KOTLIN_METADATA_REGEX = "@kotlin\\.Metadata\\(($KOTLIN_METADATA_GROUP)(, $KOTLIN_METADATA_GROUP)*\\)".toRegex()
@@ -184,45 +180,7 @@ open class AbstractClassFileToSourceStubConverterTest : AbstractKotlinKapt3Test(
override fun doTest(filePath: String) {
super.doTest(filePath)
if (!isJava9OrLater) {
doTestWithJdk9(filePath)
}
}
private fun doTestWithJdk9(filePath: String) {
val jdk9Home = KotlinTestUtils.getJdk9HomeIfPossible() ?: run {
println("JDK9 not found, the test was skipped")
return
}
val javaExe = File(jdk9Home, "bin/java.exe").takeIf { it.exists() } ?: File(jdk9Home, "bin/java")
assert(javaExe.exists()) { "Can't find 'java' executable in $jdk9Home" }
val currentJavaHome = System.getProperty("java.home")
val classpath = collectClasspath(AbstractClassFileToSourceStubConverterTest::class.java.classLoader)
.filter { !it.path.startsWith(currentJavaHome) }
val process = ProcessBuilder(
javaExe.absolutePath,
"--illegal-access=warn",
"-ea",
"-classpath",
classpath.joinToString(File.pathSeparator),
AbstractClassFileToSourceStubConverterTest::class.java.name,
filePath
).inheritIO().start()
process.waitFor(3, TimeUnit.MINUTES)
if (process.exitValue() != 0) {
throw AssertionError("Java 9 test process exited with exit code ${process.exitValue()} \n")
}
}
private fun collectClasspath(classLoader: ClassLoader?): List<URL> = when (classLoader) {
is URLClassLoader -> classLoader.urLs.asList() + collectClasspath(classLoader.parent)
is ClassLoader -> collectClasspath(classLoader.parent)
else -> emptyList()
doTestWithJdk9(AbstractClassFileToSourceStubConverterTest::class.java, filePath)
}
override fun check(kaptContext: KaptContext<GenerationState>, javaFiles: List<File>, txtFile: File, wholeFile: File) {
@@ -20,11 +20,28 @@ import org.jetbrains.kotlin.kapt3.javac.KaptJavaFileObject
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import org.junit.Test
import org.junit.runner.JUnitCore
import org.junit.runner.Request
import java.io.File
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.element.ElementKind
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.TypeElement
class KotlinKapt3IntegrationTests : AbstractKotlinKapt3IntegrationTest(), Java9TestLauncher {
override fun test(
name: String,
vararg supportedAnnotations: String,
options: Map<String, String>,
process: (Set<TypeElement>, RoundEnvironment, ProcessingEnvironment) -> Unit
) {
super.test(name, *supportedAnnotations, options = options, process = process)
doTestWithJdk9(SingleJUnitTestRunner::class.java,
KotlinKapt3IntegrationTests::class.java.name + "#test" + getTestName(false))
}
class KotlinKapt3IntegrationTests : AbstractKotlinKapt3IntegrationTest() {
@Test
fun testSimple() = test("Simple", "test.MyAnnotation") { set, roundEnv, _ ->
assertEquals(1, set.size)
@@ -119,4 +136,16 @@ class KotlinKapt3IntegrationTests : AbstractKotlinKapt3IntegrationTest() {
assertEquals("someLong", constructors[1].parameters[1].simpleName.toString())
assertEquals("someString", constructors[1].parameters[2].simpleName.toString())
}
}
internal class SingleJUnitTestRunner {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val (className, methodName) = args.single().split('#')
val request = Request.method(Class.forName(className), methodName)
val result = JUnitCore().run(request)
System.exit(if (result.wasSuccessful()) 0 else 1)
}
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.kapt3.test
import org.jetbrains.kotlin.kapt3.util.isJava9OrLater
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.util.concurrent.TimeUnit
interface Java9TestLauncher {
fun doTestWithJdk9(mainClass: Class<*>, arg: String) {
// Already under Java 9
if (isJava9OrLater) return
val jdk9Home = KotlinTestUtils.getJdk9HomeIfPossible() ?: run {
println("JDK9 not found, the test was skipped")
return
}
val javaExe = File(jdk9Home, "bin/java.exe").takeIf { it.exists() } ?: File(jdk9Home, "bin/java")
assert(javaExe.exists()) { "Can't find 'java' executable in $jdk9Home" }
val currentJavaHome = System.getProperty("java.home")
val classpath = collectClasspath(AbstractClassFileToSourceStubConverterTest::class.java.classLoader)
.filter { !it.path.startsWith(currentJavaHome) }
val process = ProcessBuilder(
javaExe.absolutePath,
"--illegal-access=warn",
"-ea",
"-classpath",
classpath.joinToString(File.pathSeparator),
mainClass.name,
arg
).inheritIO().start()
process.waitFor(3, TimeUnit.MINUTES)
if (process.exitValue() != 0) {
throw AssertionError("Java 9 test process exited with exit code ${process.exitValue()} \n")
}
}
private fun collectClasspath(classLoader: ClassLoader?): List<URL> = when (classLoader) {
is URLClassLoader -> classLoader.urLs.asList() + collectClasspath(classLoader.parent)
is ClassLoader -> collectClasspath(classLoader.parent)
else -> emptyList()
}
}