Skip to content

Commit 2cc7ee9

Browse files
committed
Set projectType in CodeGenerator properly everywhere.
1 parent 9375d32 commit 2cc7ee9

File tree

12 files changed

+49
-8
lines changed

12 files changed

+49
-8
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.utbot.framework.codegen.CodeGenerator
2020
import org.utbot.framework.codegen.domain.ForceStaticMocking
2121
import org.utbot.framework.codegen.domain.MockitoStaticMocking
2222
import org.utbot.framework.codegen.domain.NoStaticMocking
23+
import org.utbot.framework.codegen.domain.ProjectType
2324
import org.utbot.framework.codegen.domain.StaticsMocking
2425
import org.utbot.framework.codegen.domain.testFrameworkByName
2526
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
@@ -211,6 +212,8 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
211212
return CodeGenerator(
212213
testFramework = testFrameworkByName(testFramework),
213214
classUnderTest = classUnderTest,
215+
//TODO: Support Spring projects in utbot-cli if requested
216+
projectType = ProjectType.PureJvm,
214217
codegenLanguage = codegenLanguage,
215218
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
216219
staticsMocking = staticsMocking,

utbot-framework/src/main/kotlin/org/utbot/external/api/UtBotJavaApi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.utbot.framework.codegen.CodeGenerator
77
import org.utbot.framework.codegen.domain.ForceStaticMocking
88
import org.utbot.framework.codegen.domain.Junit5
99
import org.utbot.framework.codegen.domain.NoStaticMocking
10+
import org.utbot.framework.codegen.domain.ProjectType
1011
import org.utbot.framework.codegen.domain.StaticsMocking
1112
import org.utbot.framework.codegen.domain.TestFramework
1213
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
@@ -55,6 +56,7 @@ object UtBotJavaApi {
5556
classpath: String,
5657
dependencyClassPath: String,
5758
classUnderTest: Class<*>,
59+
projectType: ProjectType = ProjectType.PureJvm,
5860
testFramework: TestFramework = Junit5,
5961
mockFramework: MockFramework = MockFramework.MOCKITO,
6062
codegenLanguage: CodegenLanguage = CodegenLanguage.JAVA,
@@ -82,6 +84,7 @@ object UtBotJavaApi {
8284
return withUtContext(utContext) {
8385
val codeGenerator = CodeGenerator(
8486
classUnderTest = classUnderTest.id,
87+
projectType = projectType,
8588
testFramework = testFramework,
8689
mockFramework = mockFramework,
8790
codegenLanguage = codegenLanguage,

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/CodeGenerator.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import java.time.format.DateTimeFormatter
3030

3131
open class CodeGenerator(
3232
val classUnderTest: ClassId,
33-
//TODO: support setting `projectType` in Sarif plugins, UtBotJava api, etc.
34-
val projectType: ProjectType = PureJvm,
33+
val projectType: ProjectType,
3534
paramNames: MutableMap<ExecutableId, List<String>> = mutableMapOf(),
3635
generateUtilClassFile: Boolean = false,
3736
testFramework: TestFramework = TestFramework.defaultItem,
@@ -82,10 +81,8 @@ open class CodeGenerator(
8281
return withCustomContext(testClassCustomName) {
8382
context.withTestClassFileScope {
8483
when (context.projectType) {
85-
PureJvm,
86-
Python,
87-
JavaScript -> generateForSimpleClass(cgTestSets)
8884
Spring -> generateForSpringClass(cgTestSets)
85+
else -> generateForSimpleClass(cgTestSets)
8986
}
9087
}
9188
}

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/sarif/GenerateTestsAndSarifReportFacade.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class GenerateTestsAndSarifReportFacade(
7171

7272
return CodeGenerator(
7373
classUnderTest = targetClass.classUnderTest.id,
74+
projectType = sarifProperties.projectType,
7475
testFramework = sarifProperties.testFramework,
7576
mockFramework = sarifProperties.mockFramework,
7677
staticsMocking = sarifProperties.staticsMocking,

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/sarif/SarifExtensionProvider.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import org.utbot.framework.codegen.domain.Junit4
66
import org.utbot.framework.codegen.domain.Junit5
77
import org.utbot.framework.codegen.domain.MockitoStaticMocking
88
import org.utbot.framework.codegen.domain.NoStaticMocking
9+
import org.utbot.framework.codegen.domain.ProjectType
10+
import org.utbot.framework.codegen.domain.ProjectType.*
911
import org.utbot.framework.codegen.domain.StaticsMocking
1012
import org.utbot.framework.codegen.domain.TestFramework
1113
import org.utbot.framework.codegen.domain.TestNg
@@ -51,6 +53,8 @@ interface SarifExtensionProvider {
5153
*/
5254
val testPrivateMethods: Boolean
5355

56+
val projectType: ProjectType
57+
5458
val testFramework: TestFramework
5559

5660
val mockFramework: MockFramework
@@ -76,6 +80,15 @@ interface SarifExtensionProvider {
7680

7781
// transform functions
7882

83+
fun projectTypeParse(projectType: String): ProjectType =
84+
when (projectType.toLowerCase()) {
85+
"purejvm" -> PureJvm
86+
"spring" -> Spring
87+
"python" -> Python
88+
"javascript" -> JavaScript
89+
else -> error("Parameter projectType == '$projectType', but it can take only 'pureJvm', 'spring', 'python' or 'javascript'")
90+
}
91+
7992
fun testFrameworkParse(testFramework: String): TestFramework =
8093
when (testFramework.toLowerCase()) {
8194
"junit4" -> Junit4

utbot-gradle/src/main/kotlin/org/utbot/gradle/plugin/extension/SarifGradleExtension.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ abstract class SarifGradleExtension {
4949
@get:Input
5050
abstract val testPrivateMethods: Property<Boolean>
5151

52+
/**
53+
* Can be one of: 'purejvm', 'spring', 'python', 'javascript`.
54+
*/
55+
@get:Input
56+
abstract val projectType: Property<String>
57+
5258
/**
5359
* Can be one of: 'junit4', 'junit5', 'testng'.
5460
*/

utbot-gradle/src/main/kotlin/org/utbot/gradle/plugin/extension/SarifGradleExtensionProvider.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.utbot.gradle.plugin.extension
33
import org.gradle.api.Project
44
import org.utbot.common.PathUtil.toPath
55
import org.utbot.framework.codegen.domain.ForceStaticMocking
6+
import org.utbot.framework.codegen.domain.ProjectType
67
import org.utbot.framework.codegen.domain.StaticsMocking
78
import org.utbot.framework.codegen.domain.TestFramework
89
import org.utbot.framework.plugin.api.ClassId
@@ -57,6 +58,11 @@ class SarifGradleExtensionProvider(
5758
?: extension.testPrivateMethods.orNull
5859
?: false
5960

61+
override val projectType: ProjectType
62+
get() = (taskParameters["projectType"] ?: extension.projectType.orNull)
63+
?.let(::projectTypeParse)
64+
?: ProjectType.PureJvm
65+
6066
override val testFramework: TestFramework
6167
get() = (taskParameters["testFramework"] ?: extension.testFramework.orNull)
6268
?.let(::testFrameworkParse)

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ object UtTestsDialogProcessor {
178178
val staticMockingConfigured = model.staticsMocking.isConfigured
179179

180180
val applicationContext = when (model.projectType) {
181-
PureJvm,
182-
Python,
183-
JavaScript -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
184181
Spring -> {
185182
val shouldUseImplementors = when (model.typeReplacementApproach) {
186183
TypeReplacementApproach.DO_NOT_REPLACE -> false
@@ -195,6 +192,7 @@ object UtTestsDialogProcessor {
195192
shouldUseImplementors = shouldUseImplementors,
196193
)
197194
}
195+
else -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
198196
}
199197

200198
val process = EngineProcess.createBlocking(project, classNameToPath)

utbot-junit-contest/src/main/kotlin/org/utbot/contest/Contest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import kotlinx.coroutines.newSingleThreadContext
5252
import kotlinx.coroutines.runBlocking
5353
import kotlinx.coroutines.withTimeoutOrNull
5454
import org.utbot.framework.SummariesGenerationType
55+
import org.utbot.framework.codegen.domain.ProjectType
5556
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
5657
import org.utbot.framework.minimization.minimizeExecutions
5758
import org.utbot.framework.plugin.api.*
@@ -221,6 +222,7 @@ fun runGeneration(
221222

222223
val codeGenerator = CodeGenerator(
223224
cut.classId,
225+
projectType = ProjectType.PureJvm,
224226
testFramework = junitByVersion(junitVersion),
225227
staticsMocking = staticsMocking,
226228
forceStaticMocking = forceStaticMocking,

utbot-maven/src/main/kotlin/org/utbot/maven/plugin/GenerateTestsAndSarifReportMojo.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class GenerateTestsAndSarifReportMojo : AbstractMojo() {
8080
@Parameter(defaultValue = "false")
8181
internal var testPrivateMethods: Boolean = false
8282

83+
/**
84+
* Can be one of: 'purejvm', 'spring', 'python', 'javascript'.
85+
*/
86+
@Parameter(defaultValue = "purejvm")
87+
internal lateinit var projectType: String
88+
8389
/**
8490
* Can be one of: 'junit4', 'junit5', 'testng'.
8591
*/

utbot-maven/src/main/kotlin/org/utbot/maven/plugin/extension/SarifMavenConfigurationProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.maven.plugin.extension
22

33
import org.utbot.framework.codegen.domain.ForceStaticMocking
4+
import org.utbot.framework.codegen.domain.ProjectType
45
import org.utbot.framework.codegen.domain.StaticsMocking
56
import org.utbot.framework.codegen.domain.TestFramework
67
import org.utbot.framework.plugin.api.ClassId
@@ -36,6 +37,9 @@ class SarifMavenConfigurationProvider(
3637
override val testPrivateMethods: Boolean
3738
get() = generateTestsAndSarifReportMojo.testPrivateMethods
3839

40+
override val projectType: ProjectType
41+
get() = projectTypeParse(generateTestsAndSarifReportMojo.projectType)
42+
3943
override val testFramework: TestFramework
4044
get() = testFrameworkParse(generateTestsAndSarifReportMojo.testFramework)
4145

utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.utbot.framework.codegen.CodeGenerator
99
import org.utbot.framework.codegen.CodeGeneratorResult
1010
import org.utbot.framework.codegen.domain.ForceStaticMocking
1111
import org.utbot.framework.codegen.domain.ParametrizedTestSource
12+
import org.utbot.framework.codegen.domain.ProjectType
1213
import org.utbot.framework.codegen.domain.StaticsMocking
1314
import org.utbot.framework.codegen.domain.TestFramework
1415
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
@@ -265,6 +266,7 @@ class TestCodeGeneratorPipeline(private val testFrameworkConfiguration: TestFram
265266
val codeGenerator = with(testFrameworkConfiguration) {
266267
CodeGenerator(
267268
classUnderTest.id,
269+
projectType = ProjectType.PureJvm,
268270
generateUtilClassFile = generateUtilClassFile,
269271
paramNames = params,
270272
testFramework = testFramework,

0 commit comments

Comments
 (0)