Refactor TestCaseGenerator and CodeGenerator and their usages #508 by EgorkaKulikov · Pull Request #540 · UnitTestBot/UTBotJava · GitHub
Skip to content
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 @@ -154,12 +154,13 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
classLoader.loadClass(classFqn).kotlin

protected fun generateTestSets(
testCaseGenerator: TestCaseGenerator,
targetMethods: List<UtMethod<*>>,
sourceCodeFile: Path? = null,
searchDirectory: Path,
chosenClassesToMockAlways: Set<ClassId>
): List<UtMethodTestSet> =
TestCaseGenerator.generate(
testCaseGenerator.generate(
targetMethods,
mockStrategy,
chosenClassesToMockAlways,
Expand Down Expand Up @@ -190,30 +191,26 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
classUnderTest
).generateAsString(testSets, testClassname)

protected fun initializeEngine(workingDirectory: Path) {
protected fun initializeGenerator(workingDirectory: Path): TestCaseGenerator {
val classPathNormalized =
classLoader.urLs.joinToString(separator = File.pathSeparator) { it.toPath().absolutePath }

// TODO: SAT-1566
// Set UtSettings parameters.
// TODO: SAT-1566 Set UtSettings parameters.
UtSettings.treatOverflowAsError = treatOverflowAsError == TreatOverflowAsError.AS_ERROR

TestCaseGenerator.init(workingDirectory, classPathNormalized, System.getProperty("java.class.path"))
return TestCaseGenerator(workingDirectory, classPathNormalized, System.getProperty("java.class.path"))
}

private fun initializeCodeGenerator(testFramework: String, classUnderTest: KClass<*>): CodeGenerator {
val generateWarningsForStaticMocking =
forceStaticMocking == ForceStaticMocking.FORCE && staticsMocking is NoStaticMocking
return CodeGenerator().apply {
init(
testFramework = testFrameworkByName(testFramework),
classUnderTest = classUnderTest.java,
codegenLanguage = codegenLanguage,
staticsMocking = staticsMocking,
forceStaticMocking = forceStaticMocking,
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
)
}
return CodeGenerator(
testFramework = testFrameworkByName(testFramework),
classUnderTest = classUnderTest.java,
codegenLanguage = codegenLanguage,
staticsMocking = staticsMocking,
forceStaticMocking = forceStaticMocking,
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
)
}

protected fun KClass<*>.targetMethods() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class GenerateTestsCommand :
val classUnderTest: KClass<*> = loadClassBySpecifiedFqn(targetClassFqn)
val targetMethods = classUnderTest.targetMethods()
.filterWhen(UtSettings.skipTestGenerationForSyntheticMethods) { !isKnownSyntheticMethod(it) }
initializeEngine(workingDirectory)
val testCaseGenerator = initializeGenerator(workingDirectory)

if (targetMethods.isEmpty()) {
throw Exception("Nothing to process. No methods were provided")
Expand All @@ -107,6 +107,7 @@ class GenerateTestsCommand :
val testClassName = output?.toPath()?.toFile()?.nameWithoutExtension
?: "${classUnderTest.simpleName}Test"
val testSets = generateTestSets(
testCaseGenerator,
targetMethods,
Paths.get(sourceCodeFile),
searchDirectory = workingDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ object UtBotJavaApi {
}

return withUtContext(utContext) {
val testGenerator = CodeGenerator().apply {
init(
val codeGenerator = CodeGenerator(
classUnderTest = classUnderTest,
testFramework = testFramework,
mockFramework = mockFramework,
Expand All @@ -93,12 +92,8 @@ object UtBotJavaApi {
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
testClassPackageName = testClassPackageName
)
}

testGenerator.generateAsString(
testSets,
destinationClassName
)
codeGenerator.generateAsString(testSets, destinationClassName)
}
}

Expand All @@ -122,12 +117,8 @@ object UtBotJavaApi {
val testSets: MutableList<UtMethodTestSet> = mutableListOf()

testSets.addAll(withUtContext(utContext) {
TestCaseGenerator
.apply {
init(
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
)
}
val buildPath = FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath()
TestCaseGenerator(buildPath, classpath, dependencyClassPath)
.generate(
methodsForAutomaticGeneration.map {
toUtMethod(
Expand Down Expand Up @@ -189,12 +180,9 @@ object UtBotJavaApi {
}

return withUtContext(UtContext(classUnderTest.classLoader)) {
TestCaseGenerator
.apply {
init(
FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath(), classpath, dependencyClassPath
)
}.generate(
val buildPath = FileUtil.isolateClassFiles(classUnderTest.kotlin).toPath()
TestCaseGenerator(buildPath, classpath, dependencyClassPath)
.generate(
methodsForAutomaticGeneration.map {
toUtMethod(
it.methodToBeTestedFromUserInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,36 @@ import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.UtMethodTestSet
import org.utbot.framework.plugin.api.util.id

class CodeGenerator {
private lateinit var context: CgContext

fun init(
classUnderTest: Class<*>,
params: MutableMap<UtMethod<*>, List<String>> = mutableMapOf(),
testFramework: TestFramework = TestFramework.defaultItem,
mockFramework: MockFramework? = MockFramework.defaultItem,
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
generateWarningsForStaticMocking: Boolean = true,
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
enableTestsTimeout: Boolean = true,
testClassPackageName: String = classUnderTest.packageName,
) {
context = CgContext(
classUnderTest = classUnderTest.id,
// TODO: remove existingNames parameter completely
existingMethodNames = mutableSetOf(),
paramNames = params,
testFramework = testFramework,
mockFramework = mockFramework ?: MockFramework.MOCKITO,
codegenLanguage = codegenLanguage,
parameterizedTestSource = parameterizedTestSource,
staticsMocking = staticsMocking,
forceStaticMocking = forceStaticMocking,
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
hangingTestsTimeout = hangingTestsTimeout,
enableTestsTimeout = enableTestsTimeout,
testClassPackageName = testClassPackageName
)
}
class CodeGenerator(
private val classUnderTest: Class<*>,
params: MutableMap<UtMethod<*>, List<String>> = mutableMapOf(),
testFramework: TestFramework = TestFramework.defaultItem,
mockFramework: MockFramework? = MockFramework.defaultItem,
staticsMocking: StaticsMocking = StaticsMocking.defaultItem,
forceStaticMocking: ForceStaticMocking = ForceStaticMocking.defaultItem,
generateWarningsForStaticMocking: Boolean = true,
codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
parameterizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
runtimeExceptionTestsBehaviour: RuntimeExceptionTestsBehaviour = RuntimeExceptionTestsBehaviour.defaultItem,
hangingTestsTimeout: HangingTestsTimeout = HangingTestsTimeout(),
enableTestsTimeout: Boolean = true,
testClassPackageName: String = classUnderTest.packageName,
) {
private var context: CgContext = CgContext(
classUnderTest = classUnderTest.id,
paramNames = params,
testFramework = testFramework,
mockFramework = mockFramework ?: MockFramework.MOCKITO,
codegenLanguage = codegenLanguage,
parameterizedTestSource = parameterizedTestSource,
staticsMocking = staticsMocking,
forceStaticMocking = forceStaticMocking,
generateWarningsForStaticMocking = generateWarningsForStaticMocking,
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
hangingTestsTimeout = hangingTestsTimeout,
enableTestsTimeout = enableTestsTimeout,
testClassPackageName = testClassPackageName
)

//TODO: we support custom test class name only in utbot-online, probably support them in plugin as well
fun generateAsString(testSets: Collection<UtMethodTestSet>, testClassCustomName: String? = null): String =
Expand Down
Loading