Skip to content
Open
Prev Previous commit
Next Next commit
Use null for partially unresolvable classes in fuzzed Spring tests
  • Loading branch information
IlyaMuravjov committed Sep 12, 2023
commit bbb102bf9ddf5839756fc35874451e3bfba311b8
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.utbot.fuzzing.spring

import mu.KotlinLogging
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.util.allDeclaredFieldIds
import org.utbot.fuzzer.FuzzedType
import org.utbot.fuzzer.FuzzedValue
import org.utbot.fuzzing.FuzzedDescription
import org.utbot.fuzzing.JavaValueProvider
import org.utbot.fuzzing.Seed
import org.utbot.fuzzing.providers.nullFuzzedValue

/**
* Returns [JavaValueProvider] that only uses `null` value for classes that mention (in their
* constructor, method, or field signatures) classes that are not present on the classpath.
*/
fun JavaValueProvider.useNullForPartiallyUnresolvableClasses() =
PartiallyUnresolvableClassValueProvider().withFallback(this)

private class PartiallyUnresolvableClassValueProvider : JavaValueProvider {
companion object {
private val logger = KotlinLogging.logger {}
}

private val classResolvabilityCache = mutableMapOf<ClassId, Boolean>()

override fun accept(type: FuzzedType): Boolean = !classResolvabilityCache.getOrPut(type.classId) {
runCatching {
type.classId.allConstructors.toList()
type.classId.allMethods.toList()
type.classId.allDeclaredFieldIds.toList()
}.onFailure { e ->
logger.warn { "Failed to resolve ${type.classId} dependencies, using `null` value, cause: $e" }
}.isSuccess
}

override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence<Seed<FuzzedType, FuzzedValue>> =
sequenceOf(Seed.Simple(nullFuzzedValue(type.classId)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.utbot.framework.context.TypeReplacer
import org.utbot.framework.context.custom.CoverageFilteringConcreteExecutionContext
import org.utbot.framework.context.custom.mockAllTypesWithoutSpecificValueProvider
import org.utbot.framework.context.utils.transformJavaFuzzingContext
import org.utbot.framework.context.utils.transformValueProvider
import org.utbot.framework.context.utils.withValueProvider
import org.utbot.framework.plugin.api.BeanDefinitionData
import org.utbot.framework.plugin.api.ClassId
Expand All @@ -25,6 +26,7 @@ import org.utbot.framework.plugin.api.util.id
import org.utbot.framework.plugin.api.util.jClass
import org.utbot.framework.plugin.api.util.utContext
import org.utbot.fuzzing.spring.unit.InjectMockValueProvider
import org.utbot.fuzzing.spring.useNullForPartiallyUnresolvableClasses

class SpringApplicationContextImpl(
private val delegateContext: ApplicationContext,
Expand Down Expand Up @@ -77,7 +79,7 @@ class SpringApplicationContextImpl(
classpathWithoutDependencies,
this
)
}
}.transformValueProvider { it.useNullForPartiallyUnresolvableClasses() }
}

override fun createCodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator =
Expand Down