From b130123c4096e196bac3294cd6ac02e22490f7a9 Mon Sep 17 00:00:00 2001 From: IlyaMuravjov Date: Mon, 11 Sep 2023 19:34:27 +0300 Subject: [PATCH] Avoid using `anyObjectValueProvider` that is removed in #2583 --- .../custom/MockingJavaFuzzingContext.kt | 33 ++++++++++--------- .../org/utbot/fuzzing/providers/Objects.kt | 15 ++++++--- .../decorators/SeedFilteringValueProvider.kt | 19 +++++++++++ ...vider.kt => TypeFilteringValueProvider.kt} | 4 +-- 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/SeedFilteringValueProvider.kt rename utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/{FilteredValueProvider.kt => TypeFilteringValueProvider.kt} (82%) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt index 820fb7ac52..46949a8760 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/context/custom/MockingJavaFuzzingContext.kt @@ -1,15 +1,14 @@ package org.utbot.framework.context.custom import org.utbot.framework.context.JavaFuzzingContext +import org.utbot.framework.plugin.api.UtNullModel import org.utbot.fuzzer.FuzzedType import org.utbot.fuzzing.JavaValueProvider +import org.utbot.fuzzing.Seed import org.utbot.fuzzing.providers.AnyDepthNullValueProvider -import org.utbot.fuzzing.providers.MapValueProvider +import org.utbot.fuzzing.providers.AnyObjectValueProvider import org.utbot.fuzzing.spring.unit.MockValueProvider -import org.utbot.fuzzing.providers.NullValueProvider -import org.utbot.fuzzing.providers.ObjectValueProvider -import org.utbot.fuzzing.providers.StringValueProvider -import org.utbot.fuzzing.providers.anyObjectValueProvider +import org.utbot.fuzzing.spring.decorators.filterSeeds import org.utbot.fuzzing.spring.decorators.filterTypes import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionResult @@ -17,9 +16,10 @@ import org.utbot.instrumentation.instrumentation.execution.UtConcreteExecutionRe * Makes fuzzer to use mocks in accordance with [mockPredicate]. * * NOTE: - * - fuzzer won't mock types, that have *specific* value providers (e.g. [MapValueProvider] and [StringValueProvider]) - * - [ObjectValueProvider] and [NullValueProvider] do not count as *specific* value providers - * - fuzzer may still resort to mocks despite [mockPredicate] if it can't create other non-null values or at runtime + * - fuzzer won't mock types, that have *specific* value providers + * (i.e. ones that do not implement [AnyObjectValueProvider]) + * - fuzzer may still resort to mocks despite [mockPredicate] and *specific* + * value providers if it can't create other non-null values or at runtime */ fun JavaFuzzingContext.useMocks(mockPredicate: (FuzzedType) -> Boolean) = MockingJavaFuzzingContext(delegateContext = this, mockPredicate) @@ -31,17 +31,20 @@ class MockingJavaFuzzingContext( private val mockValueProvider = MockValueProvider(delegateContext.idGenerator) override val valueProvider: JavaValueProvider = - // NOTE: we first remove `NullValueProvider` and `ObjectValueProvider` from `delegateContext.valueProvider` - // and then add them back as a part of our `withFallback` so they have the same priority as - // `mockValueProvider`, otherwise mocks will never be used where `null` or new object can be used. + delegateContext.valueProvider - .except { it is NullValueProvider } - .except { it is ObjectValueProvider } + // NOTE: we first remove `AnyObjectValueProvider` and `NullValueProvider` from `delegateContext.valueProvider` + // and then add them back as a part of our `withFallback` so they have the same priority as + // `mockValueProvider`, otherwise mocks will never be used where `null` or new object can be used. + .except { it is AnyObjectValueProvider } .withFallback( mockValueProvider.filterTypes(mockPredicate) - .with(anyObjectValueProvider(idGenerator).filterTypes { !mockPredicate(it) }) + .with( + delegateContext.valueProvider + .filterTypes { !mockPredicate(it) } + .filterSeeds { (it as? Seed.Simple)?.value?.model !is UtNullModel } + ) .withFallback(mockValueProvider.with(AnyDepthNullValueProvider)) - .with(NullValueProvider) ) override fun handleFuzzedConcreteExecutionResult(concreteExecutionResult: UtConcreteExecutionResult) = diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt index bb14e0ecfd..02f485594c 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt @@ -70,9 +70,16 @@ fun anyObjectValueProvider(idGenerator: IdentityPreservingIdGenerator) = ovp.withFallback(AbstractsObjectValueProvider(idGenerator)) } +/** + * Marker interface that shows that this [JavaValueProvider] can potentially provide values of + * arbitrary types, unlike type-specific value providers that were designed to provide values of + * few specific popular types (e.g. `List`, `String`, etc.). + */ +interface AnyObjectValueProvider : JavaValueProvider + class ObjectValueProvider( val idGenerator: IdGenerator, -) : JavaValueProvider { +) : AnyObjectValueProvider { override fun accept(type: FuzzedType) = !isIgnored(type.classId) @@ -140,7 +147,7 @@ class ObjectValueProvider( } @Suppress("unused") -object NullValueProvider : JavaValueProvider { +object NullValueProvider : AnyObjectValueProvider { override fun enrich(description: FuzzedDescription, type: FuzzedType, scope: Scope) { // any value in static function is ok to fuzz @@ -170,7 +177,7 @@ object NullValueProvider : JavaValueProvider { * * Intended to be used as a last fallback. */ -object AnyDepthNullValueProvider : JavaValueProvider { +object AnyDepthNullValueProvider : AnyObjectValueProvider { override fun accept(type: FuzzedType) = type.classId.isRefType @@ -185,7 +192,7 @@ object AnyDepthNullValueProvider : JavaValueProvider { */ class AbstractsObjectValueProvider( val idGenerator: IdGenerator, -) : JavaValueProvider { +) : AnyObjectValueProvider { override fun accept(type: FuzzedType) = type.classId.isRefType && !isKnownTypes(type.classId) diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/SeedFilteringValueProvider.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/SeedFilteringValueProvider.kt new file mode 100644 index 0000000000..62b9dbf8f0 --- /dev/null +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/SeedFilteringValueProvider.kt @@ -0,0 +1,19 @@ +package org.utbot.fuzzing.spring.decorators + +import org.utbot.fuzzing.Description +import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.ValueProvider + +fun > ValueProvider.filterSeeds(predicate: (Seed) -> Boolean) = + SeedFilteringValueProvider(delegate = this, predicate) + +class SeedFilteringValueProvider>( + delegate: ValueProvider, + private val predicate: (Seed) -> Boolean +) : ValueProviderDecorator(delegate) { + override fun wrap(provider: ValueProvider): ValueProvider = + provider.filterSeeds(predicate) + + override fun generate(description: D, type: T): Sequence> = + delegate.generate(description, type).filter(predicate) +} \ No newline at end of file diff --git a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeFilteringValueProvider.kt similarity index 82% rename from utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt rename to utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeFilteringValueProvider.kt index faf2178f03..7c46dfa46b 100644 --- a/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/FilteredValueProvider.kt +++ b/utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/decorators/TypeFilteringValueProvider.kt @@ -4,9 +4,9 @@ import org.utbot.fuzzing.Description import org.utbot.fuzzing.ValueProvider fun > ValueProvider.filterTypes(predicate: (T) -> Boolean) = - FilteredValueProvider(delegate = this, predicate) + TypeFilteringValueProvider(delegate = this, predicate) -class FilteredValueProvider>( +class TypeFilteringValueProvider>( delegate: ValueProvider, private val predicate: (T) -> Boolean ) : ValueProviderDecorator(delegate) {