Skip to content

Commit fd174b1

Browse files
authored
Add ability to use whitelist conditions
1 parent 2923e4a commit fd174b1

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

src/main/java/net/datafaker/Faker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.util.Locale;
1515
import java.util.Random;
16+
import java.util.function.Predicate;
1617

1718
public class Faker extends BaseFaker implements BaseProviders, SportProviders, FoodProviders, EntertainmentProviders, VideoGameProviders, HealthcareProviders {
1819
public Faker() {
@@ -37,4 +38,8 @@ public Faker(Locale locale, RandomService randomService) {
3738
public Faker(FakeValuesService fakeValuesService, FakerContext context) {
3839
super(fakeValuesService, context);
3940
}
41+
42+
public Faker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
43+
super(fakeValuesService, context, whiteListPredicate);
44+
}
4045
}

src/main/java/net/datafaker/providers/base/BaseFaker.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Random;
2020
import java.util.concurrent.Callable;
2121
import java.util.function.Function;
22+
import java.util.function.Predicate;
2223
import java.util.function.Supplier;
2324

2425
/**
@@ -28,9 +29,11 @@
2829
* @author ren
2930
*/
3031
public class BaseFaker implements BaseProviders {
32+
private static final Predicate<Class<?>> EVERY_PROVIDER_ALLOWED = t -> true;
3133
private final FakerContext context;
3234
private final FakeValuesService fakeValuesService;
3335
private final Map<Class<?>, AbstractProvider<?>> providersCache = new IdentityHashMap<>();
36+
private final Predicate<Class<?>> whiteListPredicate;
3437

3538
public BaseFaker() {
3639
this(Locale.ENGLISH);
@@ -53,8 +56,13 @@ public BaseFaker(Locale locale, RandomService randomService) {
5356
}
5457

5558
public BaseFaker(FakeValuesService fakeValuesService, FakerContext context) {
59+
this(fakeValuesService, context, EVERY_PROVIDER_ALLOWED);
60+
}
61+
62+
public BaseFaker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
5663
this.fakeValuesService = fakeValuesService;
5764
this.context = context;
65+
this.whiteListPredicate = whiteListPredicate == null ? EVERY_PROVIDER_ALLOWED : whiteListPredicate;
5866
fakeValuesService.updateFakeValuesInterfaceMap(context.getLocaleChain());
5967
}
6068

@@ -335,8 +343,13 @@ public static <T> T populate(Class<T> clazz, Schema<Object, ?> schema) {
335343
@SuppressWarnings("unchecked")
336344
public <PR extends ProviderRegistration, AP extends AbstractProvider<PR>> AP getProvider(
337345
Class<AP> clazz, Function<PR, AP> valueSupplier) {
338-
return (AP) providersCache.computeIfAbsent(clazz, (klass) -> valueSupplier.apply(getFaker()));
339-
}
346+
return (AP) providersCache.computeIfAbsent(clazz, (klass) -> {
347+
if (whiteListPredicate.test(klass)) {
348+
return valueSupplier.apply(getFaker());
349+
}
350+
throw new RuntimeException("Provider '" + klass.getName() + "' is not in white list");
351+
});
352+
}
340353

341354
/**
342355
* This method is not needed anymore, don't use it.

src/main/java/net/datafaker/providers/food/FoodFaker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Locale;
99
import java.util.Random;
10+
import java.util.function.Predicate;
1011

1112
public class FoodFaker extends BaseFaker implements FoodProviders {
1213
public FoodFaker() {
@@ -32,4 +33,8 @@ public FoodFaker(Locale locale, RandomService randomService) {
3233
public FoodFaker(FakeValuesService fakeValuesService, FakerContext context) {
3334
super(fakeValuesService, context);
3435
}
36+
37+
public FoodFaker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
38+
super(fakeValuesService, context, whiteListPredicate);
39+
}
3540
}

src/main/java/net/datafaker/providers/healthcare/HealthcareFaker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Locale;
99
import java.util.Random;
10+
import java.util.function.Predicate;
1011

1112
/**
1213
* @since 2.3.0
@@ -31,4 +32,8 @@ public HealthcareFaker(Locale locale, RandomService randomService) {
3132
public HealthcareFaker(FakeValuesService fakeValuesService, FakerContext context) {
3233
super(fakeValuesService, context);
3334
}
35+
36+
public HealthcareFaker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
37+
super(fakeValuesService, context, whiteListPredicate);
38+
}
3439
}

src/main/java/net/datafaker/providers/sport/SportFaker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Locale;
99
import java.util.Random;
10+
import java.util.function.Predicate;
1011

1112
public class SportFaker extends BaseFaker implements SportProviders {
1213
public SportFaker() {
@@ -32,4 +33,8 @@ public SportFaker(Locale locale, RandomService randomService) {
3233
public SportFaker(FakeValuesService fakeValuesService, FakerContext context) {
3334
super(fakeValuesService, context);
3435
}
36+
37+
public SportFaker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
38+
super(fakeValuesService, context, whiteListPredicate);
39+
}
3540
}

src/main/java/net/datafaker/providers/videogame/VideoGameFaker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Locale;
99
import java.util.Random;
10+
import java.util.function.Predicate;
1011

1112
public class VideoGameFaker extends BaseFaker implements VideoGameProviders {
1213
public VideoGameFaker() {
@@ -32,4 +33,8 @@ public VideoGameFaker(Locale locale, RandomService randomService) {
3233
public VideoGameFaker(FakeValuesService fakeValuesService, FakerContext context) {
3334
super(fakeValuesService, context);
3435
}
36+
37+
public VideoGameFaker(FakeValuesService fakeValuesService, FakerContext context, Predicate<Class<?>> whiteListPredicate) {
38+
super(fakeValuesService, context, whiteListPredicate);
39+
}
3540
}

src/test/java/net/datafaker/FakerTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import net.datafaker.annotations.Deterministic;
44
import net.datafaker.providers.base.AbstractProvider;
55
import net.datafaker.providers.base.BaseFaker;
6+
import net.datafaker.providers.base.Name;
7+
import net.datafaker.service.FakeValuesService;
8+
import net.datafaker.service.FakerContext;
9+
import net.datafaker.service.RandomService;
610
import org.junit.jupiter.api.RepeatedTest;
711
import org.junit.jupiter.api.Test;
812
import org.junit.jupiter.api.Timeout;
@@ -27,6 +31,7 @@
2731
import java.util.stream.Collectors;
2832

2933
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.assertThatNoException;
3035
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3136
import static org.reflections.scanners.Scanners.SubTypes;
3237

@@ -393,4 +398,29 @@ void testDeterministicAndNonDeterministicProvidersReturnValues() {
393398
}
394399
}
395400
}
401+
402+
@Test
403+
void whitelistTest() {
404+
final Faker fakerWithoutWhiteList = new Faker();
405+
final Faker fakerWithEmptyWhiteList = new Faker(new FakeValuesService(), new FakerContext(Locale.ENGLISH, new RandomService()), c -> false);
406+
final Faker fakerWithFullWhiteList = new Faker(new FakeValuesService(), new FakerContext(Locale.ENGLISH, new RandomService()), c -> true);
407+
final Faker fakerWithWithoutNameInWhiteList = new Faker(new FakeValuesService(), new FakerContext(Locale.ENGLISH, new RandomService()), c -> c != Name.class);
408+
409+
assertThatThrownBy(() -> fakerWithEmptyWhiteList.expression("#{Address.country}"))
410+
.hasRootCauseMessage("Provider 'net.datafaker.providers.base.Address' is not in white list");
411+
412+
assertThatNoException()
413+
.isThrownBy(() -> fakerWithoutWhiteList.expression("#{Address.country}"));
414+
415+
assertThatNoException()
416+
.isThrownBy(() -> fakerWithFullWhiteList.expression("#{Address.country}"));
417+
418+
assertThatNoException()
419+
.isThrownBy(() -> fakerWithWithoutNameInWhiteList.expression("#{Address.country}"));
420+
421+
422+
assertThatThrownBy(() -> fakerWithEmptyWhiteList.expression("#{Name.fullName}"))
423+
.hasRootCauseMessage("Provider 'net.datafaker.providers.base.Name' is not in white list");
424+
425+
}
396426
}

0 commit comments

Comments
 (0)