Skip to content

Set projectType in CodeGenerator properly everywhere #1997

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 3 commits into from
Mar 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.utbot.framework.codegen.CodeGenerator
import org.utbot.framework.codegen.domain.ForceStaticMocking
import org.utbot.framework.codegen.domain.MockitoStaticMocking
import org.utbot.framework.codegen.domain.NoStaticMocking
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.StaticsMocking
import org.utbot.framework.codegen.domain.testFrameworkByName
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
Expand Down Expand Up @@ -211,6 +212,8 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
return CodeGenerator(
testFramework = testFrameworkByName(testFramework),
classUnderTest = classUnderTest,
//TODO: Support Spring projects in utbot-cli if requested
projectType = ProjectType.PureJvm,
codegenLanguage = codegenLanguage,
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
staticsMocking = staticsMocking,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.utbot.framework.codegen.domain.ForceStaticMocking;
import org.utbot.framework.codegen.domain.Junit4;
import org.utbot.framework.codegen.domain.MockitoStaticMocking;
import org.utbot.framework.codegen.domain.ProjectType;
import org.utbot.framework.plugin.api.*;
import org.utbot.framework.plugin.api.util.UtContext;
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider;
Expand Down Expand Up @@ -148,6 +149,7 @@ public void testMultiMethodClass() {
classpath,
dependencyClassPath,
MultiMethodExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -222,6 +224,7 @@ public void testCustomPackage() {
classpath,
dependencyClassPath,
ClassRefExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -245,6 +248,7 @@ public void testCustomPackage() {
classpath,
dependencyClassPath,
ClassRefExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -321,6 +325,7 @@ public void testOnObjectWithAssignedArrayField() {
classpath,
dependencyClassPath,
AssignedArrayExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -343,6 +348,7 @@ public void testOnObjectWithAssignedArrayField() {
classpath,
dependencyClassPath,
AssignedArrayExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -405,6 +411,7 @@ public void testClassRef() {
classpath,
dependencyClassPath,
ClassRefExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -428,6 +435,7 @@ public void testClassRef() {
classpath,
dependencyClassPath,
ClassRefExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -510,6 +518,7 @@ public void testObjectWithPublicFields() {
classpath,
dependencyClassPath,
DirectAccessExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -532,6 +541,7 @@ public void testObjectWithPublicFields() {
classpath,
dependencyClassPath,
DirectAccessExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -613,6 +623,7 @@ public void testObjectWithPublicFieldsWithAssembleModel() {
classpath,
dependencyClassPath,
DirectAccessExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -702,6 +713,7 @@ public void testOnObjectWithArrayOfPrimitiveArrays() {
classpath,
dependencyClassPath,
ArrayOfPrimitiveArraysExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -724,6 +736,7 @@ public void testOnObjectWithArrayOfPrimitiveArrays() {
classpath,
dependencyClassPath,
ArrayOfPrimitiveArraysExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -798,6 +811,7 @@ public void testProvided3() {
classpath,
dependencyClassPath,
Demo9.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -870,6 +884,7 @@ public void testCustomAssertion() {
classpath,
dependencyClassPath,
Trivial.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand All @@ -893,6 +908,7 @@ public void testCustomAssertion() {
classpath,
dependencyClassPath,
Trivial.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -982,6 +998,7 @@ public void testProvided3Reused() {
classpath,
dependencyClassPath,
compiledClass,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -1055,6 +1072,7 @@ public void testProvided3Reused() {
classpath,
dependencyClassPath,
recompiledClass,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -1120,6 +1138,7 @@ public void testProvided1() {
classpath,
dependencyClassPath,
ProvidedExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down Expand Up @@ -1196,6 +1215,7 @@ public void testOnObjectWithArrayOfComplexArrays() {
classpath,
dependencyClassPath,
ArrayOfComplexArraysExample.class,
ProjectType.PureJvm,
Junit4.INSTANCE,
MOCKITO,
CodegenLanguage.JAVA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.utbot.framework.codegen.CodeGenerator
import org.utbot.framework.codegen.domain.ForceStaticMocking
import org.utbot.framework.codegen.domain.Junit5
import org.utbot.framework.codegen.domain.NoStaticMocking
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.StaticsMocking
import org.utbot.framework.codegen.domain.TestFramework
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
Expand Down Expand Up @@ -55,6 +56,7 @@ object UtBotJavaApi {
classpath: String,
dependencyClassPath: String,
classUnderTest: Class<*>,
projectType: ProjectType = ProjectType.PureJvm,
testFramework: TestFramework = Junit5,
mockFramework: MockFramework = MockFramework.MOCKITO,
codegenLanguage: CodegenLanguage = CodegenLanguage.JAVA,
Expand Down Expand Up @@ -82,6 +84,7 @@ object UtBotJavaApi {
return withUtContext(utContext) {
val codeGenerator = CodeGenerator(
classUnderTest = classUnderTest.id,
projectType = projectType,
testFramework = testFramework,
mockFramework = mockFramework,
codegenLanguage = codegenLanguage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import java.time.format.DateTimeFormatter

open class CodeGenerator(
val classUnderTest: ClassId,
//TODO: support setting `projectType` in Sarif plugins, UtBotJava api, etc.
val projectType: ProjectType = PureJvm,
val projectType: ProjectType,
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
generateUtilClassFile: Boolean = false,
testFramework: TestFramework = TestFramework.defaultItem,
Expand Down Expand Up @@ -82,10 +81,8 @@ open class CodeGenerator(
return withCustomContext(testClassCustomName) {
context.withTestClassFileScope {
when (context.projectType) {
PureJvm,
Python,
JavaScript -> generateForSimpleClass(cgTestSets)
Spring -> generateForSpringClass(cgTestSets)
else -> generateForSimpleClass(cgTestSets)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class GenerateTestsAndSarifReportFacade(

return CodeGenerator(
classUnderTest = targetClass.classUnderTest.id,
projectType = sarifProperties.projectType,
testFramework = sarifProperties.testFramework,
mockFramework = sarifProperties.mockFramework,
staticsMocking = sarifProperties.staticsMocking,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.utbot.framework.codegen.domain.Junit4
import org.utbot.framework.codegen.domain.Junit5
import org.utbot.framework.codegen.domain.MockitoStaticMocking
import org.utbot.framework.codegen.domain.NoStaticMocking
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.ProjectType.*
import org.utbot.framework.codegen.domain.StaticsMocking
import org.utbot.framework.codegen.domain.TestFramework
import org.utbot.framework.codegen.domain.TestNg
Expand Down Expand Up @@ -51,6 +53,8 @@ interface SarifExtensionProvider {
*/
val testPrivateMethods: Boolean

val projectType: ProjectType

val testFramework: TestFramework

val mockFramework: MockFramework
Expand All @@ -76,6 +80,15 @@ interface SarifExtensionProvider {

// transform functions

fun projectTypeParse(projectType: String): ProjectType =
when (projectType.toLowerCase()) {
"purejvm" -> PureJvm
"spring" -> Spring
"python" -> Python
"javascript" -> JavaScript
else -> error("Parameter projectType == '$projectType', but it can take only 'pureJvm', 'spring', 'python' or 'javascript'")
}

fun testFrameworkParse(testFramework: String): TestFramework =
when (testFramework.toLowerCase()) {
"junit4" -> Junit4
Expand Down
11 changes: 11 additions & 0 deletions utbot-gradle/docs/utbot-gradle.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ __Groovy:__
sarifReportsRelativeRoot = 'build/generated/sarif'
markGeneratedTestsDirectoryAsTestSourcesRoot = true
testPrivateMethods = false
projectType = 'purejvm'
testFramework = 'junit5'
mockFramework = 'mockito'
generationTimeout = 60000L
Expand All @@ -60,6 +61,7 @@ __Kotlin DSL:__
sarifReportsRelativeRoot.set("build/generated/sarif")
markGeneratedTestsDirectoryAsTestSourcesRoot.set(true)
testPrivateMethods.set(false)
projectType.set("purejvm")
testFramework.set("junit5")
mockFramework.set("mockito")
generationTimeout.set(60000L)
Expand All @@ -81,6 +83,7 @@ generateTestsAndSarifReport
-PgeneratedTestsRelativeRoot='build/generated/test'
-PsarifReportsRelativeRoot='build/generated/sarif'
-PtestPrivateMethods='false'
-PtestProjectType=purejvm
-PtestFramework=junit5
-PmockFramework=mockito
-PgenerationTimeout=60000
Expand Down Expand Up @@ -119,6 +122,14 @@ generateTestsAndSarifReport
- Generate tests for private methods or not.
- By default, `false` is used.

- `projectType` &ndash;
- The type of project being analyzed.
- Can be one of:
- `'purejvm'` _(by default)_
- `'spring'`
- `'python'`
- `'javascript'`

- `testFramework` &ndash;
- The name of the test framework to be used.
- Can be one of:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ abstract class SarifGradleExtension {
@get:Input
abstract val testPrivateMethods: Property<Boolean>

/**
* Can be one of: 'purejvm', 'spring', 'python', 'javascript`.
*/
@get:Input
abstract val projectType: Property<String>

/**
* Can be one of: 'junit4', 'junit5', 'testng'.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.utbot.gradle.plugin.extension
import org.gradle.api.Project
import org.utbot.common.PathUtil.toPath
import org.utbot.framework.codegen.domain.ForceStaticMocking
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.StaticsMocking
import org.utbot.framework.codegen.domain.TestFramework
import org.utbot.framework.plugin.api.ClassId
Expand Down Expand Up @@ -57,6 +58,11 @@ class SarifGradleExtensionProvider(
?: extension.testPrivateMethods.orNull
?: false

override val projectType: ProjectType
get() = (taskParameters["projectType"] ?: extension.projectType.orNull)
?.let(::projectTypeParse)
?: ProjectType.PureJvm

override val testFramework: TestFramework
get() = (taskParameters["testFramework"] ?: extension.testFramework.orNull)
?.let(::testFrameworkParse)
Expand Down
Loading