diff --git a/libraries/kotlin.test/Module.md b/libraries/kotlin.test/Module.md index ac9fa4e09ee..63da336867a 100644 --- a/libraries/kotlin.test/Module.md +++ b/libraries/kotlin.test/Module.md @@ -17,6 +17,8 @@ The library consists of the modules: - `kotlin-test` – a JVM implementation of assertions from `kotlin-test-common`; - `kotlin-test-junit` – provides an implementation of [Asserter] on top of JUnit and maps the test annotations from `kotlin-test-annotations-common` to JUnit test annotations; +- `kotlin-test-junit5` – provides an implementation of [Asserter] on top of JUnit 5 + and maps the test annotations from `kotlin-test-annotations-common` to JUnit 5 test annotations; - `kotlin-test-testng` – provides an implementation of [Asserter] on top of TestNG and maps the test annotations from `kotlin-test-annotations-common` to TestNG test annotations; - `kotlin-test-js` – a JS implementation of common test assertions and annotations diff --git a/libraries/kotlin.test/junit5/build.gradle b/libraries/kotlin.test/junit5/build.gradle new file mode 100644 index 00000000000..97f222247f9 --- /dev/null +++ b/libraries/kotlin.test/junit5/build.gradle @@ -0,0 +1,44 @@ +description = 'Kotlin Test JUnit 5' + +apply plugin: 'kotlin-platform-jvm' + +test { + useJUnitPlatform() +} + +configureJvm6Project(project) +configureDist(project) +configurePublishing(project) + +project.ext["jpsLibraryPath"] = rootProject.distLibDir + +def junit5Version = "5.2.0" + +dependencies { + expectedBy project(':kotlin-test:kotlin-test-annotations-common') + compile project(':kotlin-test:kotlin-test-jvm') + + compile("org.junit.jupiter:junit-jupiter-api:$junit5Version") + testRuntime("org.junit.jupiter:junit-jupiter-engine:$junit5Version") +} + +jar { + manifestAttributes(manifest, project, 'Test') +} + +artifacts { + archives sourcesJar + archives javadocJar +} + +dist { + from (jar, sourcesJar) +} + +compileKotlin { + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-module-name", project.name] +} + +compileTestKotlin { + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package"] +} diff --git a/libraries/kotlin.test/junit5/src/main/kotlin/Annotations.kt b/libraries/kotlin.test/junit5/src/main/kotlin/Annotations.kt new file mode 100644 index 00000000000..1bc9ac6eca4 --- /dev/null +++ b/libraries/kotlin.test/junit5/src/main/kotlin/Annotations.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + + +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +@file:JvmPackageName("kotlin.test.junit5.annotations") +package kotlin.test + +public actual typealias Test = org.junit.jupiter.api.Test +public actual typealias Ignore = org.junit.jupiter.api.Disabled +public actual typealias BeforeTest = org.junit.jupiter.api.BeforeEach +public actual typealias AfterTest = org.junit.jupiter.api.AfterEach diff --git a/libraries/kotlin.test/junit5/src/main/kotlin/JUnitSupport.kt b/libraries/kotlin.test/junit5/src/main/kotlin/JUnitSupport.kt new file mode 100644 index 00000000000..3783f11a0df --- /dev/null +++ b/libraries/kotlin.test/junit5/src/main/kotlin/JUnitSupport.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package kotlin.test.junit5 + +import org.junit.jupiter.api.Assertions +import kotlin.test.* + +/** + * Provides [JUnitAsserter] if `org.junit.Assert` is found in the classpath. + */ +class JUnit5Contributor : AsserterContributor { + override fun contribute(): Asserter? { + return if (hasJUnitInClassPath) JUnit5Asserter else null + } + + private val hasJUnitInClassPath = try { + Class.forName("org.junit.jupiter.api.Assertions") + true + } catch (_: ClassNotFoundException) { + false + } +} + +/** + * Implements `kotlin.test` assertions by delegating them to `org.junit.jupiter.api.Assertions` class. + */ +object JUnit5Asserter : Asserter { + override fun assertEquals(message: String?, expected: Any?, actual: Any?) { + Assertions.assertEquals(expected, actual, message) + } + + override fun assertNotEquals(message: String?, illegal: Any?, actual: Any?) { + Assertions.assertNotEquals(illegal, actual, message) + } + + override fun assertSame(message: String?, expected: Any?, actual: Any?) { + Assertions.assertSame(expected, actual, message) + } + + override fun assertNotSame(message: String?, illegal: Any?, actual: Any?) { + Assertions.assertNotSame(illegal, actual, message) + } + + override fun assertNotNull(message: String?, actual: Any?) { + Assertions.assertNotNull(actual, message ?: "actual value is null") + } + + override fun assertNull(message: String?, actual: Any?) { + Assertions.assertNull(actual, message ?: "actual value is not null") + } + + override fun fail(message: String?): Nothing { + Assertions.fail(message) + // should not get here + throw AssertionError(message) + } +} diff --git a/libraries/kotlin.test/junit5/src/main/resources/META-INF/services/kotlin.test.AsserterContributor b/libraries/kotlin.test/junit5/src/main/resources/META-INF/services/kotlin.test.AsserterContributor new file mode 100644 index 00000000000..df9ef044ddd --- /dev/null +++ b/libraries/kotlin.test/junit5/src/main/resources/META-INF/services/kotlin.test.AsserterContributor @@ -0,0 +1 @@ +kotlin.test.junit5.JUnit5Contributor \ No newline at end of file diff --git a/libraries/kotlin.test/junit5/src/test/kotlin/JUnitContributorTest.kt b/libraries/kotlin.test/junit5/src/test/kotlin/JUnitContributorTest.kt new file mode 100644 index 00000000000..0b51439a40c --- /dev/null +++ b/libraries/kotlin.test/junit5/src/test/kotlin/JUnitContributorTest.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package kotlin.test.junit5.tests + +import org.junit.jupiter.api.Assertions +import kotlin.test.* +import java.util.concurrent.* +import kotlin.test.junit5.JUnit5Asserter + +class JUnitContributorTest { + @Test + fun smokeTest() { + assertSame(JUnit5Asserter, kotlin.test.asserter) + Assertions.assertEquals(JUnit5Asserter::class.java.simpleName, kotlin.test.asserter.javaClass.simpleName) + } + + @Test + fun parallelThreadGetsTheSameAsserter() { + val q = ArrayBlockingQueue(1) + + Thread { + q.put(asserter) + }.start() + + assertSame(kotlin.test.asserter, q.take()) + } + +} diff --git a/settings.gradle b/settings.gradle index 1476b1a8b81..d3be70ab375 100644 --- a/settings.gradle +++ b/settings.gradle @@ -100,6 +100,7 @@ include ":kotlin-build-common", ":kotlin-test:kotlin-test-annotations-common", ":kotlin-test:kotlin-test-jvm", ":kotlin-test:kotlin-test-junit", + ":kotlin-test:kotlin-test-junit5", ":kotlin-test:kotlin-test-testng", ":kotlin-test:kotlin-test-js", ":kotlin-test:kotlin-test-js:kotlin-test-js-it", @@ -188,6 +189,7 @@ project(':kotlin-test:kotlin-test-common').projectDir = "$rootDir/libraries/kotl project(':kotlin-test:kotlin-test-annotations-common').projectDir = "$rootDir/libraries/kotlin.test/annotations-common" as File project(':kotlin-test:kotlin-test-jvm').projectDir = "$rootDir/libraries/kotlin.test/jvm" as File project(':kotlin-test:kotlin-test-junit').projectDir = "$rootDir/libraries/kotlin.test/junit" as File +project(':kotlin-test:kotlin-test-junit5').projectDir = "$rootDir/libraries/kotlin.test/junit5" as File project(':kotlin-test:kotlin-test-testng').projectDir = "$rootDir/libraries/kotlin.test/testng" as File project(':kotlin-test:kotlin-test-js').projectDir = "$rootDir/libraries/kotlin.test/js" as File project(':kotlin-test:kotlin-test-js:kotlin-test-js-it').projectDir = "$rootDir/libraries/kotlin.test/js/it" as File