Skip to content

Commit 7584d18

Browse files
committed
refactor: always pass customization context
For a recursive use of constructors we will need to know if use random constructor was configured. As a bonus, we don't need to implement two methods in the ISpecimen anymore. Refs: JF-63
1 parent 37c8bc3 commit 7584d18

26 files changed

+131
-189
lines changed

src/main/java/com/github/nylle/javafixture/ISpecimen.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
public interface ISpecimen<T> {
66

7-
T create(Annotation[] annotations);
8-
97
T create(final CustomizationContext customizationContext, Annotation[] annotations);
108

119
}

src/main/java/com/github/nylle/javafixture/InstanceFactory.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,26 @@ public InstanceFactory(SpecimenFactory specimenFactory) {
4949
}
5050

5151
public <T> T construct(final SpecimenType<T> type) {
52+
return construct(type, new CustomizationContext(true));
53+
}
54+
public <T> T construct(final SpecimenType<T> type, CustomizationContext customizationContext) {
5255
var constructors = type.getDeclaredConstructors()
5356
.stream()
5457
.filter(x -> Modifier.isPublic(x.getModifiers()))
5558
.collect(toList());
5659

5760
if (constructors.isEmpty()) {
58-
return manufacture(type);
61+
return manufacture(type, customizationContext);
5962
}
6063

61-
return construct(type, constructors.get(random.nextInt(constructors.size())));
64+
return construct(type, constructors.get(random.nextInt(constructors.size())), customizationContext);
6265
}
6366

64-
public <T> T manufacture(final SpecimenType<T> type) {
67+
public <T> T manufacture(final SpecimenType<T> type, CustomizationContext customizationContext) {
6568
var results = type.getFactoryMethods()
6669
.stream()
6770
.filter(x -> Modifier.isPublic(x.getModifiers()))
68-
.map(x -> manufactureOrNull(x, type))
71+
.map(x -> manufactureOrNull(x, type, customizationContext))
6972
.filter(x -> x != null)
7073
.map(x -> (T) x)
7174
.collect(toList());
@@ -104,15 +107,15 @@ public <G, T extends Collection<G>> T createCollection(final SpecimenType<T> typ
104107
return type.isInterface() ? createCollectionFromInterfaceType(type.asClass()) : createCollectionFromConcreteType(type);
105108
}
106109

107-
private <T> T construct(final SpecimenType<T> type, final Constructor<?> constructor) {
110+
private <T> T construct(final SpecimenType<T> type, final Constructor<?> constructor, CustomizationContext customizationContext) {
108111
try {
109112
constructor.setAccessible(true);
110113
return (T) constructor.newInstance(stream(constructor.getGenericParameterTypes())
111114
.map(t -> specimenFactory.build(SpecimenType.fromClass(t)))
112-
.map(s -> s.create(new Annotation[0]))
115+
.map(s -> s.create(customizationContext, new Annotation[0]))
113116
.toArray());
114117
} catch (Exception e) {
115-
return manufacture(type);
118+
return manufacture(type, customizationContext);
116119
}
117120
}
118121

@@ -126,15 +129,15 @@ private <T> T createProxyForAbstract(final SpecimenType<T> type, final Map<Strin
126129
}
127130
}
128131

129-
private <T> T manufactureOrNull(final Method method, SpecimenType<T> type) {
132+
private <T> T manufactureOrNull(final Method method, SpecimenType<T> type, CustomizationContext customizationContext) {
130133
try {
131134
List<Object> arguments = new ArrayList<>();
132135
for (int i = 0; i < method.getParameterCount(); i++) {
133136
var genericParameterType = method.getGenericParameterTypes()[i];
134137
var specimen = specimenFactory.build(type.isParameterized()
135138
? SpecimenType.fromClass(type.getGenericTypeArgument(i))
136139
: SpecimenType.fromClass(genericParameterType));
137-
var o = specimen.create(new Annotation[0]);
140+
var o = specimen.create(customizationContext, new Annotation[0]);
138141
arguments.add(o);
139142
}
140143
return (T) method.invoke(null, arguments.toArray());

src/main/java/com/github/nylle/javafixture/ProxyInvocationHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.HashMap;
1111
import java.util.Map;
1212

13+
import static com.github.nylle.javafixture.CustomizationContext.noContext;
1314
import static java.util.Arrays.stream;
1415

1516
public class ProxyInvocationHandler implements InvocationHandler, MethodHandler {
@@ -31,7 +32,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
3132

3233
return methodResults.computeIfAbsent(
3334
method.toString(),
34-
key -> specimens.getOrDefault(method.getGenericReturnType().getTypeName(), resolveSpecimen(method)).create(new Annotation[0]));
35+
key -> specimens.getOrDefault(method.getGenericReturnType().getTypeName(), resolveSpecimen(method)).create(noContext(), new Annotation[0]));
3536
}
3637

3738
@Override
@@ -42,7 +43,7 @@ public Object invoke(final Object self, final Method thisMethod, final Method pr
4243

4344
return methodResults.computeIfAbsent(
4445
thisMethod.toString(),
45-
key -> specimens.getOrDefault(thisMethod.getGenericReturnType().getTypeName(), resolveSpecimen(thisMethod)).create(new Annotation[0]));
46+
key -> specimens.getOrDefault(thisMethod.getGenericReturnType().getTypeName(), resolveSpecimen(thisMethod)).create(noContext(), new Annotation[0]));
4647
}
4748

4849
private ISpecimen<?> resolveSpecimen(final Method method) {
@@ -60,7 +61,7 @@ private ISpecimen<?> resolveSpecimen(final Method method) {
6061

6162
private Type resolveType(Type type) {
6263
if (specimens.containsKey(type.getTypeName())) {
63-
return specimens.get(type.getTypeName()).create(new Annotation[0]).getClass();
64+
return specimens.get(type.getTypeName()).create(noContext(), new Annotation[0]).getClass();
6465
}
6566

6667
return type;

src/main/java/com/github/nylle/javafixture/specimen/AbstractSpecimen.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
import java.lang.annotation.Annotation;
1212

13-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
14-
1513
public class AbstractSpecimen<T> implements ISpecimen<T> {
1614

1715
private final SpecimenType<T> type;
@@ -41,11 +39,6 @@ public AbstractSpecimen(final SpecimenType<T> type, final Context context, final
4139
this.instanceFactory = new InstanceFactory(specimenFactory);
4240
}
4341

44-
@Override
45-
public T create(Annotation[] annotations) {
46-
return create(noContext(), annotations);
47-
}
48-
4942
@Override
5043
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
5144
if (context.isCached(type)) {
@@ -55,7 +48,7 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
5548
try {
5649
return (T) context.cached(type, instanceFactory.proxy(type));
5750
} catch(SpecimenException ignored) {
58-
return context.cached(type, instanceFactory.manufacture(type));
51+
return context.cached(type, instanceFactory.manufacture(type, customizationContext));
5952
}
6053
}
6154
}

src/main/java/com/github/nylle/javafixture/specimen/ArraySpecimen.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.lang.reflect.Array;
1111
import java.util.stream.IntStream;
1212

13-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
14-
1513
public class ArraySpecimen<T> implements ISpecimen<T> {
1614
private final SpecimenType<T> type;
1715
private final Context context;
@@ -39,11 +37,6 @@ public ArraySpecimen(final SpecimenType<T> type, Context context, SpecimenFactor
3937
this.specimenFactory = specimenFactory;
4038
}
4139

42-
@Override
43-
public T create(Annotation[] annotations) {
44-
return create(noContext(), annotations);
45-
}
46-
4740
@Override
4841
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
4942
if (context.isCached(type)) {
@@ -54,7 +47,7 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
5447

5548
T result = (T) context.cached(type, Array.newInstance(type.getComponentType(), length));
5649

57-
IntStream.range(0, length).boxed().forEach(i -> Array.set(result, i, specimenFactory.build(SpecimenType.fromClass(type.getComponentType())).create(new Annotation[0])));
50+
IntStream.range(0, length).boxed().forEach(i -> Array.set(result, i, specimenFactory.build(SpecimenType.fromClass(type.getComponentType())).create(customizationContext, new Annotation[0])));
5851

5952
return result;
6053
}

src/main/java/com/github/nylle/javafixture/specimen/CollectionSpecimen.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.List;
1414
import java.util.stream.IntStream;
1515

16-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
1716
import static java.util.stream.Collectors.toList;
1817

1918
public class CollectionSpecimen<T, G> implements ISpecimen<T> {
@@ -49,36 +48,31 @@ public CollectionSpecimen(final SpecimenType<T> type, final Context context, fin
4948
this.instanceFactory = new InstanceFactory(specimenFactory);
5049
}
5150

52-
@Override
53-
public T create(Annotation[] annotations) {
54-
return create(noContext(), annotations);
55-
}
56-
5751
@Override
5852
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
5953
if (context.isCached(type)) {
6054
return context.cached(type);
6155
}
6256

6357
if (type.asClass().equals(EnumSet.class)) {
64-
return context.cached(type, createEnumSet());
58+
return context.cached(type, createEnumSet(customizationContext));
6559
}
6660

6761
var collection = context.cached(type, instanceFactory.createCollection((SpecimenType<Collection<G>>) type));
6862

6963
IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
7064
.boxed()
7165
.filter(x -> specimen != null)
72-
.forEach(x -> collection.add(specimen.create(new Annotation[0])));
66+
.forEach(x -> collection.add(specimen.create(customizationContext, new Annotation[0])));
7367

7468
return (T) collection;
7569
}
7670

77-
private <G extends Enum> T createEnumSet() {
71+
private <G extends Enum> T createEnumSet(CustomizationContext customizationContext) {
7872
final List<G> elements = IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
7973
.boxed()
8074
.filter(x -> specimen != null)
81-
.map(x -> (G) specimen.create(new Annotation[0]))
75+
.map(x -> (G) specimen.create(customizationContext, new Annotation[0]))
8276
.collect(toList());
8377

8478
return (T) EnumSet.of(elements.get(0), (G[]) elements.stream().skip(1).toArray(size -> new Enum[size]));

src/main/java/com/github/nylle/javafixture/specimen/EnumSpecimen.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.lang.annotation.Annotation;
99
import java.util.Random;
1010

11-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
12-
1311
public class EnumSpecimen<T> implements ISpecimen<T> {
1412

1513
private final SpecimenType<T> type;
@@ -35,11 +33,6 @@ public EnumSpecimen(final SpecimenType<T> type, final Context context) {
3533
this.context = context;
3634
}
3735

38-
@Override
39-
public T create(Annotation[] annotations) {
40-
return create(noContext(), annotations);
41-
}
42-
4336
@Override
4437
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
4538
return context.preDefined(type, type.getEnumConstants()[random.nextInt(type.getEnumConstants().length)]);

src/main/java/com/github/nylle/javafixture/specimen/GenericSpecimen.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.Map;
1414
import java.util.stream.IntStream;
1515

16-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
1716
import static java.util.stream.Collectors.toMap;
1817

1918
public class GenericSpecimen<T> implements ISpecimen<T> {
@@ -58,15 +57,10 @@ public GenericSpecimen(SpecimenType<T> type, Context context, SpecimenFactory sp
5857
i -> specimenFactory.build(SpecimenType.fromClass(type.getGenericTypeArgument(i)))));
5958
}
6059

61-
@Override
62-
public T create(Annotation[] annotations) {
63-
return create(noContext(), annotations);
64-
}
65-
6660
@Override
6761
public T create(CustomizationContext customizationContext, Annotation[] annotations) {
6862
if (type.asClass().equals(Class.class)) {
69-
return (T) specimens.entrySet().stream().findFirst().get().getValue().create(new Annotation[0]).getClass();
63+
return (T) specimens.entrySet().stream().findFirst().get().getValue().create(customizationContext, new Annotation[0]).getClass();
7064
}
7165

7266
if (context.isCached(type)) {
@@ -96,7 +90,7 @@ private T populate(CustomizationContext customizationContext) {
9690
field.getName(),
9791
specimens.getOrDefault(
9892
field.getGenericType().getTypeName(),
99-
specimenFactory.build(SpecimenType.fromClass(field.getType()))).create(new Annotation[0]))));
93+
specimenFactory.build(SpecimenType.fromClass(field.getType()))).create(customizationContext, new Annotation[0]))));
10094
} catch (SpecimenException ex ) {
10195
return context.overwrite(type, instanceFactory.construct(type));
10296
}

src/main/java/com/github/nylle/javafixture/specimen/InterfaceSpecimen.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import java.lang.annotation.Annotation;
1111

12-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
13-
1412
public class InterfaceSpecimen<T> implements ISpecimen<T> {
1513

1614
private final SpecimenType<T> type;
@@ -40,11 +38,6 @@ public InterfaceSpecimen(final SpecimenType<T> type, final Context context, fina
4038
this.instanceFactory = new InstanceFactory(specimenFactory);
4139
}
4240

43-
@Override
44-
public T create(Annotation[] annotations) {
45-
return create(noContext(), annotations);
46-
}
47-
4841
@Override
4942
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
5043
if (context.isCached(type)) {

src/main/java/com/github/nylle/javafixture/specimen/MapSpecimen.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.concurrent.ConcurrentSkipListMap;
2222
import java.util.stream.IntStream;
2323

24-
import static com.github.nylle.javafixture.CustomizationContext.noContext;
25-
2624
public class MapSpecimen<T, K, V> implements ISpecimen<T> {
2725
private final SpecimenType<T> type;
2826
private final Context context;
@@ -56,11 +54,6 @@ public MapSpecimen(final SpecimenType<T> type, final Context context, final Spec
5654
}
5755
}
5856

59-
@Override
60-
public T create(Annotation[] annotations) {
61-
return create(noContext(), annotations);
62-
}
63-
6457
@Override
6558
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
6659
if (context.isCached(type)) {
@@ -72,7 +65,7 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
7265
IntStream.range(0, context.getConfiguration().getRandomCollectionSize())
7366
.boxed()
7467
.filter(x -> keySpecimen != null && valueSpecimen != null)
75-
.forEach(x -> map.put(keySpecimen.create(new Annotation[0]), valueSpecimen.create(new Annotation[0])));
68+
.forEach(x -> map.put(keySpecimen.create(customizationContext, new Annotation[0]), valueSpecimen.create(customizationContext, new Annotation[0])));
7669

7770
return (T) map;
7871
}

0 commit comments

Comments
 (0)