@@ -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,36 @@ 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
+ * For test generation, take a look at the first too arguments.
46
+ * If you want to execute some methods concretely,
47
+ * use the first argument. The first argument must contain
48
+ * all information required for the execution, including arguments values.
49
+ * The second one is ready to use {@code UtMethodTestSet}s.
50
+ * Test for them will be generated without concrete execution.
51
+ */
56
52
@JvmStatic
57
53
@JvmOverloads
58
- fun generate (
54
+ fun generateTestCode (
59
55
methodsForGeneration : List <TestMethodInfo >,
60
56
generatedTestCases : List <UtMethodTestSet > = mutableListOf(),
61
57
destinationClassName : String ,
@@ -69,16 +65,18 @@ object UtBotJavaApi {
69
65
staticsMocking : StaticsMocking = NoStaticMocking ,
70
66
generateWarningsForStaticMocking : Boolean = false,
71
67
forceStaticMocking : ForceStaticMocking = ForceStaticMocking .DO_NOT_FORCE ,
72
- testClassPackageName : String = classUnderTest.nameOfPackage
68
+ testClassPackageName : String = classUnderTest.nameOfPackage,
69
+ applicationContext : ApplicationContext
73
70
): String {
74
71
75
- val utContext = UtContext (classUnderTest.classLoader)
76
-
77
72
val testSets: MutableList <UtMethodTestSet > = generatedTestCases.toMutableList()
78
73
79
74
val concreteExecutor = ConcreteExecutor (
80
- SimpleUtExecutionInstrumentation .Factory (pathsToUserClasses = classpath.split(File .pathSeparator).toSet()),
81
- classpath,
75
+ applicationContext.createConcreteExecutionContext(
76
+ fullClasspath = dependencyClassPath,
77
+ classpathWithoutDependencies = classpath
78
+ ).instrumentationFactory,
79
+ classpath
82
80
)
83
81
84
82
testSets.addAll(generateUnitTests(concreteExecutor, methodsForGeneration, classUnderTest))
@@ -87,8 +85,8 @@ object UtBotJavaApi {
87
85
concreteExecutor.close()
88
86
}
89
87
90
- return withUtContext(utContext ) {
91
- val codeGenerator = CodeGenerator (
88
+ return withUtContext(UtContext (classUnderTest.classLoader) ) {
89
+ applicationContext.createCodeGenerator (
92
90
CodeGeneratorParams (
93
91
classUnderTest = classUnderTest.id,
94
92
projectType = projectType,
@@ -99,11 +97,9 @@ object UtBotJavaApi {
99
97
staticsMocking = staticsMocking,
100
98
forceStaticMocking = forceStaticMocking,
101
99
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
102
- testClassPackageName = testClassPackageName
100
+ testClassPackageName = testClassPackageName,
103
101
)
104
- )
105
-
106
- codeGenerator.generateAsString(testSets, destinationClassName)
102
+ ).generateAsString(testSets, destinationClassName)
107
103
}
108
104
}
109
105
@@ -114,29 +110,33 @@ object UtBotJavaApi {
114
110
*/
115
111
@JvmStatic
116
112
@JvmOverloads
117
- fun generateTestSets (
118
- methodsForAutomaticGeneration : List <TestMethodInfo >,
113
+ fun generateTestSetsForMethods (
114
+ methodsToAnalyze : List <Method >,
119
115
classUnderTest : Class <* >,
120
116
classpath : String ,
121
117
dependencyClassPath : String ,
122
118
mockStrategyApi : MockStrategyApi = MockStrategyApi .OTHER_PACKAGES ,
123
- generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis
119
+ generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis,
120
+ applicationContext : ApplicationContext
124
121
): MutableList <UtMethodTestSet > {
125
122
126
123
val utContext = UtContext (classUnderTest.classLoader)
127
124
val testSets: MutableList <UtMethodTestSet > = mutableListOf ()
128
125
129
126
testSets.addAll(withUtContext(utContext) {
130
127
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
- )
128
+ TestCaseGenerator (
129
+ listOf (buildPath),
130
+ classpath,
131
+ dependencyClassPath,
132
+ jdkInfo = JdkInfoDefaultProvider ().info,
133
+ applicationContext = applicationContext
134
+ ).generate(
135
+ methodsToAnalyze.map { it.executableId },
136
+ mockStrategyApi,
137
+ chosenClassesToMockAlways = emptySet(),
138
+ generationTimeoutInMillis
139
+ )
140
140
})
141
141
142
142
return testSets
@@ -150,13 +150,14 @@ object UtBotJavaApi {
150
150
@JvmStatic
151
151
@JvmOverloads
152
152
fun fuzzingTestSets (
153
- methodsForAutomaticGeneration : List <TestMethodInfo >,
153
+ methodsToAnalyze : List <Method >,
154
154
classUnderTest : Class <* >,
155
155
classpath : String ,
156
156
dependencyClassPath : String ,
157
157
mockStrategyApi : MockStrategyApi = MockStrategyApi .OTHER_PACKAGES ,
158
158
generationTimeoutInMillis : Long = UtSettings .utBotGenerationTimeoutInMillis,
159
- primitiveValuesSupplier : CustomFuzzerValueSupplier = CustomFuzzerValueSupplier { null }
159
+ primitiveValuesSupplier : CustomFuzzerValueSupplier = CustomFuzzerValueSupplier { null },
160
+ applicationContext : ApplicationContext
160
161
): MutableList <UtMethodTestSet > {
161
162
fun createPrimitiveModels (supplier : CustomFuzzerValueSupplier , classId : ClassId ): Sequence <UtPrimitiveModel > =
162
163
supplier
@@ -189,14 +190,12 @@ object UtBotJavaApi {
189
190
classpath,
190
191
dependencyClassPath,
191
192
jdkInfo = JdkInfoDefaultProvider ().info,
192
- applicationContext = SimpleApplicationContext () .transformValueProvider { defaultModelProvider ->
193
+ applicationContext = applicationContext .transformValueProvider { defaultModelProvider ->
193
194
customModelProvider.withFallback(defaultModelProvider)
194
195
}
195
196
)
196
197
.generate(
197
- methodsForAutomaticGeneration.map {
198
- it.methodToBeTestedFromUserInput.executableId
199
- },
198
+ methodsToAnalyze.map{ it.executableId },
200
199
mockStrategyApi,
201
200
chosenClassesToMockAlways = emptySet(),
202
201
generationTimeoutInMillis,
0 commit comments