|
1 | 1 | package net.datafaker; |
2 | 2 |
|
| 3 | +import net.datafaker.annotations.Deterministic; |
| 4 | +import net.datafaker.providers.base.AbstractProvider; |
3 | 5 | import net.datafaker.providers.base.BaseFaker; |
4 | 6 | import org.junit.jupiter.api.RepeatedTest; |
5 | 7 | import org.junit.jupiter.api.Test; |
6 | 8 | import org.junit.jupiter.params.ParameterizedTest; |
7 | 9 | import org.junit.jupiter.params.provider.ValueSource; |
8 | | - |
| 10 | +import org.reflections.Reflections; |
| 11 | + |
| 12 | +import java.lang.reflect.Constructor; |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.lang.reflect.Modifier; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.HashSet; |
9 | 19 | import java.util.Locale; |
10 | 20 | import java.util.Map; |
11 | 21 | import java.util.Random; |
| 22 | +import java.util.Set; |
12 | 23 | import java.util.concurrent.Callable; |
| 24 | +import java.util.stream.Collectors; |
13 | 25 |
|
14 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
15 | 27 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | +import static org.reflections.scanners.Scanners.SubTypes; |
16 | 29 |
|
17 | 30 | class FakerTest extends AbstractFakerTest { |
18 | 31 |
|
@@ -348,4 +361,57 @@ void shouldNotApplyCachingToMethodsWithParameters() { |
348 | 361 | String flight2 = faker.expression("#{Aviation.flight 'ICAO'}"); |
349 | 362 | assertThat(flight2).matches("[A-z]{3}\\d{1,4}"); |
350 | 363 | } |
| 364 | + |
| 365 | + @Test |
| 366 | + void testDeterministicAndNonDeterministicProvidersReturnValues() { |
| 367 | + final Reflections reflections = new Reflections("net.datafaker.providers"); |
| 368 | + Set<Class<?>> classes = reflections.get(SubTypes.of(AbstractProvider.class).asClass()); |
| 369 | + for (var clazz: classes) { |
| 370 | + Collection<Method> methods = Arrays.stream(clazz.getDeclaredMethods()) |
| 371 | + .filter(m -> Modifier.isPublic(m.getModifiers()) && m.getParameterCount() == 0).collect(Collectors.toSet()); |
| 372 | + if (methods.isEmpty()) continue; |
| 373 | + Constructor<AbstractProvider<?>> constructor = null; |
| 374 | + final AbstractProvider<?> ap; |
| 375 | + try { |
| 376 | + Set<Constructor<AbstractProvider<?>>> constructorsWith1Arg = |
| 377 | + Arrays.stream(clazz.getDeclaredConstructors()) |
| 378 | + .filter(c -> c.getParameterCount() == 1).map(c -> (Constructor<AbstractProvider<?>>) c) |
| 379 | + .collect(Collectors.toSet()); |
| 380 | + for (var c: constructorsWith1Arg) { |
| 381 | + Class<?>[] types = c.getParameterTypes(); |
| 382 | + if (types[0].isAssignableFrom(Faker.class)) { |
| 383 | + constructor = c; |
| 384 | + break; |
| 385 | + } |
| 386 | + } |
| 387 | + assertThat(constructor).isNotNull(); |
| 388 | + constructor.setAccessible(true); |
| 389 | + ap = constructor.newInstance(faker); |
| 390 | + } catch (InvocationTargetException | InstantiationException | |
| 391 | + IllegalAccessException e) { |
| 392 | + throw new RuntimeException(e); |
| 393 | + } |
| 394 | + for (Method m: methods) { |
| 395 | + var set = new HashSet<>(); |
| 396 | + try { |
| 397 | + for (int i = 0; i < 10; i++) { |
| 398 | + set.add(m.invoke(ap)); |
| 399 | + } |
| 400 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 401 | + throw new RuntimeException(e); |
| 402 | + } |
| 403 | + if (m.isAnnotationPresent(Deterministic.class)) { |
| 404 | + assertThat(set) |
| 405 | + .as("Class: " + ap.getClass().getName() |
| 406 | + + ", method: " + m.getName() + " should have the same return value") |
| 407 | + .hasSize(1); |
| 408 | + } else { |
| 409 | + assertThat(set) |
| 410 | + .as("Class: " + ap.getClass().getName() |
| 411 | + + ", method: " + m.getName() + " should generate different return values") |
| 412 | + .hasSizeGreaterThan(1); |
| 413 | + } |
| 414 | + } |
| 415 | + } |
| 416 | + } |
351 | 417 | } |
0 commit comments