Skip to content

Commit 5b4d643

Browse files
authored
Make fuzzer use generic field types #2430 (#2431)
1 parent 1de5cf9 commit 5b4d643

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal fun findAccessibleModifiableFields(description: FuzzedDescription?, cla
206206
FieldDescription(
207207
name = field.name,
208208
type = if (description != null) toFuzzerType(
209-
field.type,
209+
field.genericType,
210210
description.typeCache
211211
) else FuzzedType(field.type.id),
212212
canBeSetDirectly = isAccessible(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.utbot.fuzzing.samples;
2+
3+
import java.util.List;
4+
5+
public class StringListHolder {
6+
private List<String> strings;
7+
8+
9+
public List<String> getStrings() {
10+
return strings;
11+
}
12+
13+
14+
public void setStrings(List<String> strings) {
15+
this.strings = strings;
16+
}
17+
18+
19+
public void methodUnderTest() {}
20+
}

utbot-java-fuzzing/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.utbot.framework.plugin.api.UtAssembleModel
99
import org.utbot.framework.plugin.api.UtNullModel
1010
import org.utbot.framework.plugin.api.UtPrimitiveModel
1111
import org.utbot.framework.plugin.api.util.*
12+
import org.utbot.framework.plugin.api.util.constructor.ValueConstructor
1213
import org.utbot.fuzzer.FuzzedConcreteValue
1314
import org.utbot.fuzzing.samples.DeepNested
1415
import org.utbot.fuzzer.FuzzedType
@@ -17,13 +18,15 @@ import org.utbot.fuzzer.IdentityPreservingIdGenerator
1718
import org.utbot.fuzzing.providers.NullValueProvider
1819
import org.utbot.fuzzing.samples.AccessibleObjects
1920
import org.utbot.fuzzing.samples.FailToGenerateListGeneric
21+
import org.utbot.fuzzing.samples.StringListHolder
2022
import org.utbot.fuzzing.samples.Stubs
2123
import org.utbot.fuzzing.utils.Trie
2224
import java.lang.reflect.GenericArrayType
2325
import java.lang.reflect.ParameterizedType
2426
import java.lang.reflect.Type
2527
import java.util.IdentityHashMap
2628
import java.util.concurrent.atomic.AtomicInteger
29+
import kotlin.reflect.jvm.javaMethod
2730

2831
internal object TestIdentityPreservingIdGenerator : IdentityPreservingIdGenerator<Int> {
2932
private val cache = mutableMapOf<Any, Int>()
@@ -273,6 +276,31 @@ class JavaFuzzingTest {
273276
}
274277
}
275278

279+
@Test
280+
fun `fuzzer correctly works with settable field that has a parameterized type`() {
281+
val seenStringListHolders = mutableListOf<StringListHolder>()
282+
var remainingRuns = 100
283+
runBlockingWithContext {
284+
runJavaFuzzing(
285+
TestIdentityPreservingIdGenerator,
286+
methodUnderTest = StringListHolder::methodUnderTest.javaMethod!!.executableId,
287+
constants = emptyList(),
288+
names = emptyList(),
289+
) { thisInstance, _, _ ->
290+
thisInstance?.let {
291+
seenStringListHolders.add(
292+
ValueConstructor().construct(listOf(it.model)).single().value as StringListHolder
293+
)
294+
}
295+
remainingRuns--
296+
BaseFeedback(Trie.emptyNode(), if (remainingRuns > 0) Control.CONTINUE else Control.STOP)
297+
}
298+
}
299+
val seenStrings = seenStringListHolders.flatMap { it.strings.orEmpty().filterNotNull() }
300+
assertNotEquals(emptyList<String>(), seenStrings)
301+
seenStrings.forEach { assertInstanceOf(String::class.java, it) }
302+
}
303+
276304
@Test
277305
fun `value providers override every function of fuzzing in simple case`() {
278306
val provided = MarkerValueProvider<FuzzedType, FuzzedValue, FuzzedDescription>("p")

0 commit comments

Comments
 (0)