@@ -3,28 +3,14 @@ package org.utbot.external.api
3
3
import org.utbot.common.FileUtil
4
4
import org.utbot.common.nameOfPackage
5
5
import org.utbot.framework.UtSettings
6
- import org.utbot.framework.codegen.domain.ForceStaticMocking
7
- import org.utbot.framework.codegen.domain.Junit5
8
- import org.utbot.framework.codegen.domain.NoStaticMocking
9
- import org.utbot.framework.codegen.domain.ProjectType
10
- import org.utbot.framework.codegen.domain.StaticsMocking
11
- import org.utbot.framework.codegen.domain.TestFramework
12
- import org.utbot.framework.codegen.generator.CodeGenerator
6
+ import org.utbot.framework.codegen.domain.*
13
7
import org.utbot.framework.codegen.generator.CodeGeneratorParams
14
8
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
15
- import org.utbot.framework.context.simple.SimpleApplicationContext
9
+ import org.utbot.framework.context.ApplicationContext
16
10
import org.utbot.framework.context.utils.transformValueProvider
17
11
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionData
18
12
import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult
19
13
import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrumentation
20
- import org.utbot.framework.plugin.api.ClassId
21
- import org.utbot.framework.plugin.api.CodegenLanguage
22
- import org.utbot.framework.plugin.api.MockFramework
23
- import org.utbot.framework.plugin.api.MockStrategyApi
24
- import org.utbot.framework.plugin.api.TestCaseGenerator
25
- import org.utbot.framework.plugin.api.UtMethodTestSet
26
- import org.utbot.framework.plugin.api.UtPrimitiveModel
27
- import org.utbot.framework.plugin.api.UtSymbolicExecution
28
14
import org.utbot.framework.plugin.api.util.UtContext
29
15
import org.utbot.framework.plugin.api.util.executableId
30
16
import org.utbot.framework.plugin.api.util.id
@@ -36,26 +22,54 @@ import org.utbot.framework.plugin.api.util.stringClassId
36
22
import org.utbot.framework.plugin.api.util.withUtContext
37
23
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
38
24
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider
39
- import org.utbot.fuzzer.FuzzedType
40
25
import org.utbot.fuzzer.FuzzedValue
41
- import org.utbot.fuzzing.FuzzedDescription
42
26
import org.utbot.fuzzing.JavaValueProvider
43
27
import org.utbot.fuzzing.Seed
44
- import org.utbot.fuzzing.ValueProvider
45
28
import org.utbot.instrumentation.ConcreteExecutor
46
29
import org.utbot.instrumentation.execute
47
- import org.utbot.instrumentation.instrumentation.execution.SimpleUtExecutionInstrumentation
48
- import java.io.File
49
30
import kotlin.reflect.jvm.kotlinFunction
31
+ import org.utbot.framework.codegen.domain.StaticsMocking
32
+ import org.utbot.framework.plugin.api.*
33
+ import java.lang.reflect.Method
50
34
51
35
object UtBotJavaApi {
52
36
37
+ /* *
38
+ * For running tests it could be reasonable to reuse the same concrete executor
39
+ */
53
40
@JvmStatic
54
41
var stopConcreteExecutorOnExit: Boolean = true
55
42
43
+
44
+ /* *
45
+ * Generates test code
46
+ * @param methodsForGeneration specify methods that are supposed to be executed concretely.
47
+ * In order to execute method you are supposed to provide some
48
+ * values to pass in it this is why we use [TestMethodInfo] here.
49
+ * @param generatedTestCases specify [UtMethodTestSet]s that are used for test code
50
+ * generation. By comparison with the first parameter,
51
+ * {@code UtMethodTestSet} contains more information about
52
+ * test, including result of the executions. Note, that
53
+ * you can get the object with any sort of analysis,
54
+ * for instance, symbolic or fuzz execution.
55
+ * @param destinationClassName the name of containing class for the generated tests
56
+ * @param classpath classpath that are used to build the class under test
57
+ * @param dependencyClassPath class path including dependencies required for the code generation
58
+ * @param classUnderTest for this class test should be generated
59
+ * @param projectType JVM, Spring, Python, or other type of project
60
+ * @param testFramework test framework that is going to be used for running generated tests
61
+ * @param mockFramework framework that will be used in the generated tests
62
+ * @param codegenLanguage the target language of the test generation. It can be different from the source language.
63
+ * @param staticsMocking the approach to the statics mocking
64
+ * @param generateWarningsForStaticMocking enable generation of warning about forced static mocking in comments
65
+ * of generated tests.
66
+ * @param forceStaticMocking enables static mocking
67
+ * @param testClassPackageName package name for the generated class with the tests
68
+ * @param applicationContext specify application context here
69
+ */
56
70
@JvmStatic
57
71
@JvmOverloads
58
- fun generate (
72
+ fun generateTestCode (
59
73
methodsForGeneration : List <TestMethodInfo >,
60
74
generatedTestCases : List <UtMethodTestSet > = mutableListOf(),
61
75
destinationClassName : String ,
@@ -69,16 +83,18 @@ object UtBotJavaApi {
69
83
staticsMocking : StaticsMocking = NoStaticMocking ,
70
84
generateWarningsForStaticMocking : Boolean = false,
71
85
forceStaticMocking : ForceStaticMocking = ForceStaticMocking .DO_NOT_FORCE ,
72
- testClassPackageName : String = classUnderTest.nameOfPackage
86
+ testClassPackageName : String = classUnderTest.nameOfPackage,
87
+ applicationContext : ApplicationContext
73
88
): String {
74
89
75
- val utContext = UtContext (classUnderTest.classLoader)
76
-
77
90
val testSets: MutableList <UtMethodTestSet > = generatedTestCases.toMutableList()
78
91
79
92
val concreteExecutor = ConcreteExecutor (
80
- SimpleUtExecutionInstrumentation .Factory (pathsToUserClasses = classpath.split(File .pathSeparator).toSet()),
81
- classpath,
93
+ applicationContext.createConcreteExecutionContext(
94
+ fullClasspath = dependencyClassPath,
95
+ classpathWithoutDependencies = classpath
96
+ ).instrumentationFactory,
97
+ classpath
82
98
)
83
99
84
100
testSets.addAll(generateUnitTests(concreteExecutor, methodsForGeneration, classUnderTest))
@@ -87,8 +103,8 @@ object UtBotJavaApi {
87
103
concreteExecutor.close()
88
104
}
89
105
90
- return withUtContext(utContext ) {
91
- val codeGenerator = CodeGenerator (
106
+ return withUtContext(UtContext (classUnderTest.classLoader) ) {
107
+ applicationContext.createCodeGenerator (
92
108
CodeGeneratorParams (
93
109
classUnderTest = classUnderTest.id,
94
110
projectType = projectType,
@@ -99,11 +115,9 @@ object UtBotJavaApi {
99
115
staticsMocking = staticsMocking,
100
116
forceStaticMocking = forceStaticMocking,
101
117
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
102
- testClassPackageName = testClassPackageName
118
+ testClassPackageName = testClassPackageName,
103
119
)
104
- )
105
-
106
- codeGenerator.generateAsString(testSets, destinationClassName)
120
+ ).generateAsString(testSets, destinationClassName)
107
121
}
108
122
}
109
123
@@ -114,29 +128,33 @@ object UtBotJavaApi {
114
128
*/
115
129
@JvmStatic
116
130
@JvmOverloads
117
- fun generateTestSets (
118
- methodsForAutomaticGeneration : List <TestMethodInfo >,
131
+ fun generateTestSetsForMethods (
132
+ methodsToAnalyze : List <Method >,
119
133
classUnderTest : Class <* >,
120
134
classpath : String ,
121
135
dependencyClassPath : String ,
122
136
mockStrategyApi : MockStrategyApi = MockStrategyApi .OTHER_PACKAGES ,
123
- generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis
137
+ generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis,
138
+ applicationContext : ApplicationContext
124
139
): MutableList <UtMethodTestSet > {
125
140
126
141
val utContext = UtContext (classUnderTest.classLoader)
127
142
val testSets: MutableList <UtMethodTestSet > = mutableListOf ()
128
143
129
144
testSets.addAll(withUtContext(utContext) {
130
145
val buildPath = FileUtil .isolateClassFiles(classUnderTest).toPath()
131
- TestCaseGenerator (listOf (buildPath), classpath, dependencyClassPath, jdkInfo = JdkInfoDefaultProvider ().info)
132
- .generate(
133
- methodsForAutomaticGeneration.map {
134
- it.methodToBeTestedFromUserInput.executableId
135
- },
136
- mockStrategyApi,
137
- chosenClassesToMockAlways = emptySet(),
138
- generationTimeoutInMillis
139
- )
146
+ TestCaseGenerator (
147
+ listOf (buildPath),
148
+ classpath,
149
+ dependencyClassPath,
150
+ jdkInfo = JdkInfoDefaultProvider ().info,
151
+ applicationContext = applicationContext
152
+ ).generate(
153
+ methodsToAnalyze.map { it.executableId },
154
+ mockStrategyApi,
155
+ chosenClassesToMockAlways = emptySet(),
156
+ generationTimeoutInMillis
157
+ )
140
158
})
141
159
142
160
return testSets
@@ -145,18 +163,19 @@ object UtBotJavaApi {
145
163
/* *
146
164
* Generates test cases using only fuzzing workflow.
147
165
*
148
- * @see [generateTestSets ]
166
+ * @see [generateTestSetsForMethods ]
149
167
*/
150
168
@JvmStatic
151
169
@JvmOverloads
152
170
fun fuzzingTestSets (
153
- methodsForAutomaticGeneration : List <TestMethodInfo >,
171
+ methodsToAnalyze : List <Method >,
154
172
classUnderTest : Class <* >,
155
173
classpath : String ,
156
174
dependencyClassPath : String ,
157
175
mockStrategyApi : MockStrategyApi = MockStrategyApi .OTHER_PACKAGES ,
158
176
generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis,
159
- primitiveValuesSupplier : CustomFuzzerValueSupplier = CustomFuzzerValueSupplier { null }
177
+ primitiveValuesSupplier : CustomFuzzerValueSupplier = CustomFuzzerValueSupplier { null },
178
+ applicationContext : ApplicationContext
160
179
): MutableList <UtMethodTestSet > {
161
180
fun createPrimitiveModels (supplier : CustomFuzzerValueSupplier , classId : ClassId ): Sequence <UtPrimitiveModel > =
162
181
supplier
@@ -189,14 +208,12 @@ object UtBotJavaApi {
189
208
classpath,
190
209
dependencyClassPath,
191
210
jdkInfo = JdkInfoDefaultProvider ().info,
192
- applicationContext = SimpleApplicationContext () .transformValueProvider { defaultModelProvider ->
211
+ applicationContext = applicationContext .transformValueProvider { defaultModelProvider ->
193
212
customModelProvider.withFallback(defaultModelProvider)
194
213
}
195
214
)
196
215
.generate(
197
- methodsForAutomaticGeneration.map {
198
- it.methodToBeTestedFromUserInput.executableId
199
- },
216
+ methodsToAnalyze.map{ it.executableId },
200
217
mockStrategyApi,
201
218
chosenClassesToMockAlways = emptySet(),
202
219
generationTimeoutInMillis,
0 commit comments