From ad7701922c9c083875b759a30a8522ced734178a Mon Sep 17 00:00:00 2001 From: Valeriy Zhirnov Date: Wed, 31 Jan 2018 20:29:56 +0300 Subject: [PATCH] TestNG support in kotlin.test #KT-22620 --- build.gradle.kts | 1 + libraries/kotlin.test/testng/build.gradle | 39 +++++++++++++ .../testng/src/main/kotlin/Annotations.kt | 14 +++++ .../testng/src/main/kotlin/TestNGSupport.kt | 55 +++++++++++++++++++ .../services/kotlin.test.AsserterContributor | 1 + .../src/test/kotlin/TestNGContributorTest.kt | 25 +++++++++ settings.gradle | 2 + 7 files changed, 137 insertions(+) create mode 100644 libraries/kotlin.test/testng/build.gradle create mode 100644 libraries/kotlin.test/testng/src/main/kotlin/Annotations.kt create mode 100644 libraries/kotlin.test/testng/src/main/kotlin/TestNGSupport.kt create mode 100644 libraries/kotlin.test/testng/src/main/resources/META-INF/services/kotlin.test.AsserterContributor create mode 100644 libraries/kotlin.test/testng/src/test/kotlin/TestNGContributorTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 70c628313e6..60b284eee8d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -223,6 +223,7 @@ val coreLibProjects = listOf( ":kotlin-test:kotlin-test-common", ":kotlin-test:kotlin-test-jvm", ":kotlin-test:kotlin-test-junit", + ":kotlin-test:kotlin-test-testng", ":kotlin-test:kotlin-test-js", ":kotlin-reflect" ) diff --git a/libraries/kotlin.test/testng/build.gradle b/libraries/kotlin.test/testng/build.gradle new file mode 100644 index 00000000000..2fe8ca0ab58 --- /dev/null +++ b/libraries/kotlin.test/testng/build.gradle @@ -0,0 +1,39 @@ +description = 'Kotlin Test TestNG' + +apply plugin: 'kotlin-platform-jvm' + +configureJvm6Project(project) +configurePublishing(project) + + +dependencies { + expectedBy project(':kotlin-test:kotlin-test-annotations-common') + compile project(':kotlin-test:kotlin-test-jvm') + compile('org.testng:testng:6.13.1') +} + + +jar { + manifestAttributes(manifest, project, 'Test') +} + +artifacts { + archives sourcesJar + archives javadocJar +} + +dist { + from (jar, sourcesJar) +} + +compileKotlin { + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-Xmulti-platform", "-module-name", project.name] +} + +compileTestKotlin { + kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package"] +} + +test { + useTestNG() +} diff --git a/libraries/kotlin.test/testng/src/main/kotlin/Annotations.kt b/libraries/kotlin.test/testng/src/main/kotlin/Annotations.kt new file mode 100644 index 00000000000..b7bd29a7526 --- /dev/null +++ b/libraries/kotlin.test/testng/src/main/kotlin/Annotations.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2000-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.annotations") +package kotlin.test + +public actual typealias Test = org.testng.annotations.Test +public actual typealias Ignore = org.testng.annotations.Ignore +public actual typealias BeforeTest = org.testng.annotations.BeforeClass +public actual typealias AfterTest = org.testng.annotations.AfterClass diff --git a/libraries/kotlin.test/testng/src/main/kotlin/TestNGSupport.kt b/libraries/kotlin.test/testng/src/main/kotlin/TestNGSupport.kt new file mode 100644 index 00000000000..f78f5b23fad --- /dev/null +++ b/libraries/kotlin.test/testng/src/main/kotlin/TestNGSupport.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2000-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.testng + +import org.testng.* +import kotlin.test.* + +class TestNGContributor : AsserterContributor { + override fun contribute(): Asserter? { + for (stackFrame in currentStackTrace()) { + val className = stackFrame.className + + if (className.startsWith("org.testng.") || className.startsWith("testng.")) { + return TestNGAsserter + } + } + + return null + } +} + +object TestNGAsserter : Asserter { + override fun assertEquals(message : String?, expected : Any?, actual : Any?) { + Assert.assertEquals(expected, actual, message) + } + + override fun assertNotEquals(message : String?, illegal : Any?, actual : Any?) { + Assert.assertNotEquals(illegal, actual, message) + } + + override fun assertSame(message : String?, expected : Any?, actual : Any?) { + Assert.assertSame(expected, actual, message) + } + + override fun assertNotSame(message : String?, illegal : Any?, actual : Any?) { + Assert.assertNotSame(illegal, actual, message) + } + + override fun assertNotNull(message : String?, actual : Any?) { + Assert.assertNotNull(actual, message ?: "actual value is null") + } + + override fun assertNull(message : String?, actual : Any?) { + Assert.assertNull(actual, message ?: "actual value is not null") + } + + override fun fail(message : String?): Nothing { + Assert.fail(message) + // should not get here + throw AssertionError(message) + } +} diff --git a/libraries/kotlin.test/testng/src/main/resources/META-INF/services/kotlin.test.AsserterContributor b/libraries/kotlin.test/testng/src/main/resources/META-INF/services/kotlin.test.AsserterContributor new file mode 100644 index 00000000000..49c03fa8fbe --- /dev/null +++ b/libraries/kotlin.test/testng/src/main/resources/META-INF/services/kotlin.test.AsserterContributor @@ -0,0 +1 @@ +kotlin.test.testng.TestNGContributor \ No newline at end of file diff --git a/libraries/kotlin.test/testng/src/test/kotlin/TestNGContributorTest.kt b/libraries/kotlin.test/testng/src/test/kotlin/TestNGContributorTest.kt new file mode 100644 index 00000000000..962bf2da472 --- /dev/null +++ b/libraries/kotlin.test/testng/src/test/kotlin/TestNGContributorTest.kt @@ -0,0 +1,25 @@ +package kotlinx.testing.tests + +import org.testng.annotations.* +import org.testng.* +import java.util.concurrent.* + +class TestNGContributorTest { + + @Test + fun smokeTest() { + Assert.assertEquals("TestNGAsserter", kotlin.test.asserter.javaClass.simpleName) + } + + @Test + fun `should fail to contribute if it was run outside of testng`() { + val q = ArrayBlockingQueue(1) + + Thread { + q.put(kotlin.test.asserter) + }.start() + + Assert.assertEquals("DefaultAsserter", q.take().javaClass.simpleName) + } + +} diff --git a/settings.gradle b/settings.gradle index 07d56cf4221..082c8027b7e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -97,6 +97,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-testng", ":kotlin-test:kotlin-test-js", ":kotlin-test:kotlin-test-js:kotlin-test-js-it", ":kotlin-stdlib-common", @@ -164,6 +165,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-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 project(':kotlin-stdlib-common').projectDir = "$rootDir/libraries/stdlib/common" as File