Skip to content

Enable Java assertions in framework integration tests #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions utbot-framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ test {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
}
}

compileKotlin {
kotlinOptions {
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ sealed class TestFramework(
executionInvoke: String,
classPath: String,
classesNames: List<String>,
buildDirectory: String
buildDirectory: String,
additionalArguments: List<String>
): List<String>

override fun toString() = displayName
Expand Down Expand Up @@ -298,16 +299,23 @@ object TestNg : TestFramework(displayName = "TestNG") {
simpleName = "DataProvider"
)

@OptIn(ExperimentalStdlibApi::class)
override fun getRunTestsCommand(
executionInvoke: String,
classPath: String,
classesNames: List<String>,
buildDirectory: String
buildDirectory: String,
additionalArguments: List<String>
): List<String> {
// 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<String>) {
Expand Down Expand Up @@ -375,12 +383,19 @@ object Junit4 : TestFramework("JUnit4") {
)
}

@OptIn(ExperimentalStdlibApi::class)
override fun getRunTestsCommand(
executionInvoke: String,
classPath: String,
classesNames: List<String>,
buildDirectory: String
): List<String> = listOf(executionInvoke, "$mainPackage.runner.JUnitCore") + classesNames
buildDirectory: String,
additionalArguments: List<String>
): List<String> = buildList {
add(executionInvoke)
addAll(additionalArguments)
add("$mainPackage.runner.JUnitCore")
addAll(classesNames)
}
}

object Junit5 : TestFramework("JUnit5") {
Expand Down Expand Up @@ -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<String>,
buildDirectory: String
): List<String> =
listOf(
executionInvoke,
"-jar", classPath.split(File.pathSeparator).single { platformJarName in it },
) + isolateCommandLineArgumentsToArgumentFile(listOf("-cp", classPath).plus(classesNames.map { "-c=$it" }))
buildDirectory: String,
additionalArguments: List<String>
): List<String> = 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<java.lang.AssertionError>() }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")}]" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.utbot.examples.codegen;

public class JavaAssert {
public int assertPositive(int value) {
assert value > 0;
return value;
}
}