Skip to content

Commit bfc2de9

Browse files
Set projectType in CodeGenerator properly everywhere #1987 (#1997)
1 parent 3dac352 commit bfc2de9

File tree

17 files changed

+160
-8
lines changed

17 files changed

+160
-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-test/src/test/java/org/utbot/examples/manual/UtBotJavaApiTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.utbot.framework.codegen.domain.ForceStaticMocking;
2424
import org.utbot.framework.codegen.domain.Junit4;
2525
import org.utbot.framework.codegen.domain.MockitoStaticMocking;
26+
import org.utbot.framework.codegen.domain.ProjectType;
2627
import org.utbot.framework.plugin.api.*;
2728
import org.utbot.framework.plugin.api.util.UtContext;
2829
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider;
@@ -148,6 +149,7 @@ public void testMultiMethodClass() {
148149
classpath,
149150
dependencyClassPath,
150151
MultiMethodExample.class,
152+
ProjectType.PureJvm,
151153
Junit4.INSTANCE,
152154
MOCKITO,
153155
CodegenLanguage.JAVA,
@@ -222,6 +224,7 @@ public void testCustomPackage() {
222224
classpath,
223225
dependencyClassPath,
224226
ClassRefExample.class,
227+
ProjectType.PureJvm,
225228
Junit4.INSTANCE,
226229
MOCKITO,
227230
CodegenLanguage.JAVA,
@@ -245,6 +248,7 @@ public void testCustomPackage() {
245248
classpath,
246249
dependencyClassPath,
247250
ClassRefExample.class,
251+
ProjectType.PureJvm,
248252
Junit4.INSTANCE,
249253
MOCKITO,
250254
CodegenLanguage.JAVA,
@@ -321,6 +325,7 @@ public void testOnObjectWithAssignedArrayField() {
321325
classpath,
322326
dependencyClassPath,
323327
AssignedArrayExample.class,
328+
ProjectType.PureJvm,
324329
Junit4.INSTANCE,
325330
MOCKITO,
326331
CodegenLanguage.JAVA,
@@ -343,6 +348,7 @@ public void testOnObjectWithAssignedArrayField() {
343348
classpath,
344349
dependencyClassPath,
345350
AssignedArrayExample.class,
351+
ProjectType.PureJvm,
346352
Junit4.INSTANCE,
347353
MOCKITO,
348354
CodegenLanguage.JAVA,
@@ -405,6 +411,7 @@ public void testClassRef() {
405411
classpath,
406412
dependencyClassPath,
407413
ClassRefExample.class,
414+
ProjectType.PureJvm,
408415
Junit4.INSTANCE,
409416
MOCKITO,
410417
CodegenLanguage.JAVA,
@@ -428,6 +435,7 @@ public void testClassRef() {
428435
classpath,
429436
dependencyClassPath,
430437
ClassRefExample.class,
438+
ProjectType.PureJvm,
431439
Junit4.INSTANCE,
432440
MOCKITO,
433441
CodegenLanguage.JAVA,
@@ -510,6 +518,7 @@ public void testObjectWithPublicFields() {
510518
classpath,
511519
dependencyClassPath,
512520
DirectAccessExample.class,
521+
ProjectType.PureJvm,
513522
Junit4.INSTANCE,
514523
MOCKITO,
515524
CodegenLanguage.JAVA,
@@ -532,6 +541,7 @@ public void testObjectWithPublicFields() {
532541
classpath,
533542
dependencyClassPath,
534543
DirectAccessExample.class,
544+
ProjectType.PureJvm,
535545
Junit4.INSTANCE,
536546
MOCKITO,
537547
CodegenLanguage.JAVA,
@@ -613,6 +623,7 @@ public void testObjectWithPublicFieldsWithAssembleModel() {
613623
classpath,
614624
dependencyClassPath,
615625
DirectAccessExample.class,
626+
ProjectType.PureJvm,
616627
Junit4.INSTANCE,
617628
MOCKITO,
618629
CodegenLanguage.JAVA,
@@ -702,6 +713,7 @@ public void testOnObjectWithArrayOfPrimitiveArrays() {
702713
classpath,
703714
dependencyClassPath,
704715
ArrayOfPrimitiveArraysExample.class,
716+
ProjectType.PureJvm,
705717
Junit4.INSTANCE,
706718
MOCKITO,
707719
CodegenLanguage.JAVA,
@@ -724,6 +736,7 @@ public void testOnObjectWithArrayOfPrimitiveArrays() {
724736
classpath,
725737
dependencyClassPath,
726738
ArrayOfPrimitiveArraysExample.class,
739+
ProjectType.PureJvm,
727740
Junit4.INSTANCE,
728741
MOCKITO,
729742
CodegenLanguage.JAVA,
@@ -798,6 +811,7 @@ public void testProvided3() {
798811
classpath,
799812
dependencyClassPath,
800813
Demo9.class,
814+
ProjectType.PureJvm,
801815
Junit4.INSTANCE,
802816
MOCKITO,
803817
CodegenLanguage.JAVA,
@@ -870,6 +884,7 @@ public void testCustomAssertion() {
870884
classpath,
871885
dependencyClassPath,
872886
Trivial.class,
887+
ProjectType.PureJvm,
873888
Junit4.INSTANCE,
874889
MOCKITO,
875890
CodegenLanguage.JAVA,
@@ -893,6 +908,7 @@ public void testCustomAssertion() {
893908
classpath,
894909
dependencyClassPath,
895910
Trivial.class,
911+
ProjectType.PureJvm,
896912
Junit4.INSTANCE,
897913
MOCKITO,
898914
CodegenLanguage.JAVA,
@@ -982,6 +998,7 @@ public void testProvided3Reused() {
982998
classpath,
983999
dependencyClassPath,
9841000
compiledClass,
1001+
ProjectType.PureJvm,
9851002
Junit4.INSTANCE,
9861003
MOCKITO,
9871004
CodegenLanguage.JAVA,
@@ -1055,6 +1072,7 @@ public void testProvided3Reused() {
10551072
classpath,
10561073
dependencyClassPath,
10571074
recompiledClass,
1075+
ProjectType.PureJvm,
10581076
Junit4.INSTANCE,
10591077
MOCKITO,
10601078
CodegenLanguage.JAVA,
@@ -1120,6 +1138,7 @@ public void testProvided1() {
11201138
classpath,
11211139
dependencyClassPath,
11221140
ProvidedExample.class,
1141+
ProjectType.PureJvm,
11231142
Junit4.INSTANCE,
11241143
MOCKITO,
11251144
CodegenLanguage.JAVA,
@@ -1196,6 +1215,7 @@ public void testOnObjectWithArrayOfComplexArrays() {
11961215
classpath,
11971216
dependencyClassPath,
11981217
ArrayOfComplexArraysExample.class,
1218+
ProjectType.PureJvm,
11991219
Junit4.INSTANCE,
12001220
MOCKITO,
12011221
CodegenLanguage.JAVA,

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/docs/utbot-gradle.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ __Groovy:__
4141
sarifReportsRelativeRoot = 'build/generated/sarif'
4242
markGeneratedTestsDirectoryAsTestSourcesRoot = true
4343
testPrivateMethods = false
44+
projectType = 'purejvm'
4445
testFramework = 'junit5'
4546
mockFramework = 'mockito'
4647
generationTimeout = 60000L
@@ -60,6 +61,7 @@ __Kotlin DSL:__
6061
sarifReportsRelativeRoot.set("build/generated/sarif")
6162
markGeneratedTestsDirectoryAsTestSourcesRoot.set(true)
6263
testPrivateMethods.set(false)
64+
projectType.set("purejvm")
6365
testFramework.set("junit5")
6466
mockFramework.set("mockito")
6567
generationTimeout.set(60000L)
@@ -81,6 +83,7 @@ generateTestsAndSarifReport
8183
-PgeneratedTestsRelativeRoot='build/generated/test'
8284
-PsarifReportsRelativeRoot='build/generated/sarif'
8385
-PtestPrivateMethods='false'
86+
-PtestProjectType=purejvm
8487
-PtestFramework=junit5
8588
-PmockFramework=mockito
8689
-PgenerationTimeout=60000
@@ -119,6 +122,14 @@ generateTestsAndSarifReport
119122
- Generate tests for private methods or not.
120123
- By default, `false` is used.
121124

125+
- `projectType` &ndash;
126+
- The type of project being analyzed.
127+
- Can be one of:
128+
- `'purejvm'` _(by default)_
129+
- `'spring'`
130+
- `'python'`
131+
- `'javascript'`
132+
122133
- `testFramework` &ndash;
123134
- The name of the test framework to be used.
124135
- Can be one of:

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)

0 commit comments

Comments
 (0)