From 6fc1d9b2294524fc4032b7b77553d358bea6cc49 Mon Sep 17 00:00:00 2001 From: Dmitrii Timofeev Date: Fri, 15 Jul 2022 16:09:21 +0300 Subject: [PATCH] Enable Java assertions in framework integration tests Added "-ea" command line argument to the java invocation used to run integration tests in `utbot-framework`. --- utbot-framework/build.gradle | 6 +++ .../org/utbot/framework/codegen/Domain.kt | 41 ++++++++++++++----- .../utbot/examples/codegen/JavaAssertTest.kt | 18 ++++++++ .../codegen/CompilationAndRunUtils.kt | 11 ++++- .../utbot/examples/codegen/JavaAssert.java | 8 ++++ 5 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 utbot-framework/src/test/kotlin/org/utbot/examples/codegen/JavaAssertTest.kt create mode 100644 utbot-sample/src/main/java/org/utbot/examples/codegen/JavaAssert.java diff --git a/utbot-framework/build.gradle b/utbot-framework/build.gradle index 09aa3da335..526110da95 100644 --- a/utbot-framework/build.gradle +++ b/utbot-framework/build.gradle @@ -65,3 +65,9 @@ test { jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009' } } + +compileKotlin { + kotlinOptions { + freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" + } +} diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt index c96347f4c7..4c875293bb 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt @@ -223,7 +223,8 @@ sealed class TestFramework( executionInvoke: String, classPath: String, classesNames: List, - buildDirectory: String + buildDirectory: String, + additionalArguments: List ): List override fun toString() = displayName @@ -298,16 +299,23 @@ object TestNg : TestFramework(displayName = "TestNG") { simpleName = "DataProvider" ) + @OptIn(ExperimentalStdlibApi::class) override fun getRunTestsCommand( executionInvoke: String, classPath: String, classesNames: List, - buildDirectory: String + buildDirectory: String, + additionalArguments: List ): List { // TestNg requires a specific xml to run with writeXmlFileForTestSuite(buildDirectory, classesNames) - return listOf(executionInvoke, "$mainPackage.TestNG", "$buildDirectory${File.separator}$testXmlName") + return buildList { + add(executionInvoke) + addAll(additionalArguments) + add("$mainPackage.TestNG") + add("$buildDirectory${File.separator}$testXmlName") + } } private fun writeXmlFileForTestSuite(buildDirectory: String, testsNames: List) { @@ -375,12 +383,19 @@ object Junit4 : TestFramework("JUnit4") { ) } + @OptIn(ExperimentalStdlibApi::class) override fun getRunTestsCommand( executionInvoke: String, classPath: String, classesNames: List, - buildDirectory: String - ): List = listOf(executionInvoke, "$mainPackage.runner.JUnitCore") + classesNames + buildDirectory: String, + additionalArguments: List + ): List = buildList { + add(executionInvoke) + addAll(additionalArguments) + add("$mainPackage.runner.JUnitCore") + addAll(classesNames) + } } object Junit5 : TestFramework("JUnit5") { @@ -464,16 +479,20 @@ object Junit5 : TestFramework("JUnit5") { private const val junitVersion = "1.7.1" // TODO read it from gradle.properties private const val platformJarName: String = "junit-platform-console-standalone-$junitVersion.jar" + @OptIn(ExperimentalStdlibApi::class) override fun getRunTestsCommand( executionInvoke: String, classPath: String, classesNames: List, - buildDirectory: String - ): List = - listOf( - executionInvoke, - "-jar", classPath.split(File.pathSeparator).single { platformJarName in it }, - ) + isolateCommandLineArgumentsToArgumentFile(listOf("-cp", classPath).plus(classesNames.map { "-c=$it" })) + buildDirectory: String, + additionalArguments: List + ): List = buildList { + add(executionInvoke) + addAll(additionalArguments) + add("-jar") + add(classPath.split(File.pathSeparator).single { platformJarName in it }) + add(isolateCommandLineArgumentsToArgumentFile(listOf("-cp", classPath).plus(classesNames.map { "-c=$it" }))) + } } enum class RuntimeExceptionTestsBehaviour( diff --git a/utbot-framework/src/test/kotlin/org/utbot/examples/codegen/JavaAssertTest.kt b/utbot-framework/src/test/kotlin/org/utbot/examples/codegen/JavaAssertTest.kt new file mode 100644 index 0000000000..ed0060bd53 --- /dev/null +++ b/utbot-framework/src/test/kotlin/org/utbot/examples/codegen/JavaAssertTest.kt @@ -0,0 +1,18 @@ +package org.utbot.examples.codegen + +import org.junit.jupiter.api.Test +import org.utbot.examples.UtValueTestCaseChecker +import org.utbot.examples.eq +import org.utbot.examples.isException + +class JavaAssertTest : UtValueTestCaseChecker(testClass = JavaAssert::class){ + @Test + fun testAssertPositive() { + checkWithException( + JavaAssert::assertPositive, + eq(2), + { value, result -> value > 0 && result.isSuccess && result.getOrNull() == value }, + { value, result -> value <= 0 && result.isException() } + ) + } +} \ No newline at end of file diff --git a/utbot-framework/src/test/kotlin/org/utbot/framework/codegen/CompilationAndRunUtils.kt b/utbot-framework/src/test/kotlin/org/utbot/framework/codegen/CompilationAndRunUtils.kt index 72c3cb2f88..abe6b7805d 100644 --- a/utbot-framework/src/test/kotlin/org/utbot/framework/codegen/CompilationAndRunUtils.kt +++ b/utbot-framework/src/test/kotlin/org/utbot/framework/codegen/CompilationAndRunUtils.kt @@ -56,8 +56,17 @@ fun runTests( ) { val classpath = System.getProperty("java.class.path") + File.pathSeparator + buildDirectory val executionInvoke = generatedLanguage.executorInvokeCommand + val additionalArguments = listOf( + "-ea", // Enable assertions + ) - val command = testFramework.getRunTestsCommand(executionInvoke, classpath, testsNames, buildDirectory) + val command = testFramework.getRunTestsCommand( + executionInvoke, + classpath, + testsNames, + buildDirectory, + additionalArguments + ) logger.trace { "Command to run test: [${command.joinToString(" ")}]" } diff --git a/utbot-sample/src/main/java/org/utbot/examples/codegen/JavaAssert.java b/utbot-sample/src/main/java/org/utbot/examples/codegen/JavaAssert.java new file mode 100644 index 0000000000..fb5dc8b622 --- /dev/null +++ b/utbot-sample/src/main/java/org/utbot/examples/codegen/JavaAssert.java @@ -0,0 +1,8 @@ +package org.utbot.examples.codegen; + +public class JavaAssert { + public int assertPositive(int value) { + assert value > 0; + return value; + } +}