|
8 | 8 | import com.carrotsearch.randomizedtesting.jupiter.SysProps; |
9 | 9 | import java.lang.reflect.Constructor; |
10 | 10 | import java.lang.reflect.Method; |
| 11 | +import java.lang.reflect.ParameterizedType; |
| 12 | +import java.lang.reflect.Type; |
11 | 13 | import java.util.ArrayList; |
12 | 14 | import java.util.Arrays; |
13 | 15 | import java.util.List; |
14 | 16 | import java.util.Objects; |
15 | 17 | import java.util.Optional; |
16 | 18 | import java.util.Random; |
17 | 19 | import java.util.function.LongFunction; |
| 20 | +import java.util.function.Supplier; |
18 | 21 | import org.jspecify.annotations.Nullable; |
19 | 22 | import org.junit.jupiter.api.extension.BeforeAllCallback; |
20 | 23 | import org.junit.jupiter.api.extension.DynamicTestInvocationContext; |
@@ -103,28 +106,44 @@ private static SeedChain parseRootSeed(Optional<String> rootSeedValue) { |
103 | 106 | // |
104 | 107 | // ParameterResolver: inject RandomizedContext and Random instances into test methods. |
105 | 108 | // |
| 109 | + private static final Type supplierOfRandom = getSupplierOfRandomType(); |
| 110 | + |
| 111 | + /** Helper method that returns parameterized type for {@code Supplier<Random>}. */ |
| 112 | + private static Type getSupplierOfRandomType() { |
| 113 | + abstract class TypeLiteral<T> { |
| 114 | + public Type getType() { |
| 115 | + return ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return (new TypeLiteral<Supplier<Random>>() {}).getType(); |
| 120 | + } |
106 | 121 |
|
107 | 122 | @Override |
108 | 123 | public boolean supportsParameter( |
109 | 124 | ParameterContext parameterContext, ExtensionContext extensionContext) |
110 | 125 | throws ParameterResolutionException { |
111 | | - Class<?> parameterType = parameterContext.getParameter().getType(); |
112 | | - return parameterType.equals(RandomizedContext.class) || parameterType.equals(Random.class); |
| 126 | + var type = parameterContext.getParameter().getParameterizedType(); |
| 127 | + return type.equals(RandomizedContext.class) |
| 128 | + || type.equals(Random.class) |
| 129 | + || type.equals(supplierOfRandom); |
113 | 130 | } |
114 | 131 |
|
115 | 132 | @Override |
116 | 133 | public Object resolveParameter( |
117 | 134 | ParameterContext parameterContext, ExtensionContext extensionContext) |
118 | 135 | throws ParameterResolutionException { |
119 | 136 | var ctx = getRandomizedContextFor(extensionContext); |
120 | | - Class<?> parameterType = parameterContext.getParameter().getType(); |
121 | | - if (parameterType.equals(RandomizedContext.class)) { |
| 137 | + var type = parameterContext.getParameter().getParameterizedType(); |
| 138 | + if (type.equals(RandomizedContext.class)) { |
122 | 139 | return ctx; |
123 | | - } else if (parameterType.equals(Random.class)) { |
| 140 | + } else if (type.equals(Random.class)) { |
124 | 141 | return ctx.getRandom(); |
| 142 | + } else if (type.equals(supplierOfRandom)) { |
| 143 | + return (Supplier<Random>) ctx::splitRandom; |
125 | 144 | } else { |
126 | 145 | throw new RuntimeException( |
127 | | - "Unexpected unsupported parameter type in resolveParameter: " + parameterType); |
| 146 | + "Unexpected unsupported parameter type in resolveParameter: " + type); |
128 | 147 | } |
129 | 148 | } |
130 | 149 |
|
|
0 commit comments