Skip to content

Commit 1991eb4

Browse files
akutscheraNylle
authored andcommitted
fix: do not cache finished specimen #102
This will allow us to create different objects so we can populate sets.
1 parent 735e15b commit 1991eb4

17 files changed

+126
-61
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
6161
Map.<String, ISpecimen<?>>of().getOrDefault(
6262
field.getGenericType().getTypeName(),
6363
specimenFactory.build(SpecimenType.fromClass(field.getGenericType()))).create(new CustomizationContext(List.of(), Map.of(), false), field.getAnnotations()))));
64-
return result;
64+
return context.remove(type);
6565
} catch(SpecimenException ignored) {
66-
return context.cached(type, instanceFactory.manufacture(type, customizationContext));
66+
return instanceFactory.manufacture(type, customizationContext);
6767
}
6868
}
6969
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
4949

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

52-
return result;
52+
return context.remove(type);
5353
}
5454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
6565
.filter(x -> specimen != null)
6666
.forEach(x -> collection.add(specimen.create(customizationContext, new Annotation[0])));
6767

68-
return (T) collection;
68+
return context.remove(type);
6969
}
7070

7171
private <G extends Enum> T createEnumSet(CustomizationContext customizationContext) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
5858
return context.cached(type);
5959
}
6060

61-
return context.cached(type, shuffledStream(derivedTypes)
61+
return shuffledStream(derivedTypes)
6262
.map(derivedType -> specimenFactory.build(derivedType))
6363
.flatMap(derivedSpecimen -> tryCreate(derivedSpecimen, customizationContext, annotations).stream())
6464
.findFirst()
65-
.orElseGet(() -> proxy(customizationContext)));
65+
.orElseGet(() -> proxy(customizationContext));
6666
}
6767

6868
private <R extends T> R proxy(CustomizationContext customizationContext) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
6161
}
6262

6363
if (type.isInterface()) {
64-
return (T) context.cached(type, instanceFactory.proxy(type, specimens));
64+
return (T) instanceFactory.proxy(type, specimens);
6565
}
6666

6767
if (customizationContext.useRandomConstructor()) {
68-
return context.cached(type, instanceFactory.construct(type, customizationContext));
68+
return instanceFactory.construct(type, customizationContext);
6969
}
7070

7171
return populate(customizationContext);
@@ -85,9 +85,10 @@ private T populate(CustomizationContext customizationContext) {
8585
field.getGenericType().getTypeName(),
8686
specimenFactory.build(SpecimenType.fromClass(field.getType()))).create(new CustomizationContext(List.of(), Map.of(), false), new Annotation[0]))));
8787
} catch (SpecimenException ex) {
88-
return context.overwrite(type, instanceFactory.construct(type, customizationContext));
88+
context.remove(type);
89+
return instanceFactory.construct(type, customizationContext);
8990
}
90-
return result;
91+
return context.remove(type);
9192
}
9293
}
9394

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public InterfaceSpecimen(final SpecimenType<T> type, final Context context, fina
4040

4141
@Override
4242
public T create(final CustomizationContext customizationContext, Annotation[] annotations) {
43-
if (context.isCached(type)) {
44-
return context.cached(type);
45-
}
46-
47-
return (T) context.cached(type, instanceFactory.proxy(type));
43+
return (T) instanceFactory.proxy(type);
4844
}
4945
}
5046

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
6767
.filter(x -> keySpecimen != null && valueSpecimen != null)
6868
.forEach(x -> map.put(keySpecimen.create(customizationContext, new Annotation[0]), valueSpecimen.create(customizationContext, new Annotation[0])));
6969

70-
return (T) map;
70+
return context.remove(type);
7171
}
7272

7373
private Map<K, V> createFromConcreteType(final SpecimenType<T> type) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
5151
}
5252

5353
if (customizationContext.useRandomConstructor()) {
54-
return context.cached(type, instanceFactory.construct(type, customizationContext));
54+
return instanceFactory.construct(type, customizationContext);
5555
}
5656

5757
return populate(customizationContext);
@@ -72,6 +72,6 @@ private T populate(CustomizationContext customizationContext) {
7272
} catch (SpecimenException ex) {
7373
return context.overwrite(type, instanceFactory.construct(type, customizationContext));
7474
}
75-
return result;
75+
return context.remove(type);
7676
}
7777
}

src/test/java/com/github/nylle/javafixture/InstanceFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ void argumentsCanBeCustomized() {
134134

135135
@Test
136136
@DisplayName("constructor arguments are cached")
137-
void constructorArgumentsAreCached() {
137+
void constructorArgumentsAreNotCached() {
138138
var sut = new InstanceFactory(new SpecimenFactory(new Context(Configuration.configure())));
139139

140140
var customizationContext = new CustomizationContext(List.of(), Map.of(), true);
141141
TestObject first = sut.construct(fromClass(TestObject.class), customizationContext);
142142
TestObject second = sut.construct(fromClass(TestObject.class), customizationContext);
143143

144-
assertThat(first.getIntegers()).usingRecursiveComparison().isEqualTo(second.getIntegers());
145-
assertThat(first.getStrings()).usingRecursiveComparison().isEqualTo(second.getStrings());
144+
assertThat(first.getIntegers()).usingRecursiveComparison().isNotEqualTo(second.getIntegers());
145+
assertThat(first.getStrings()).usingRecursiveComparison().isNotEqualTo(second.getStrings());
146146
assertThat(first.getValue()).as("primitives are never cached").isNotEqualTo(second.getValue());
147147
}
148148

src/test/java/com/github/nylle/javafixture/specimen/AbstractSpecimenTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,27 @@ void voidAbstractMethodsWillJustReturnVoid() {
108108

109109
@Test
110110
void createAbstractClassWithoutConstructor() {
111-
var sut = new AbstractSpecimen<Charset>(SpecimenType.fromClass(Charset.class), context, specimenFactory);
111+
SpecimenType<Charset> specimenType = SpecimenType.fromClass(Charset.class);
112+
var sut = new AbstractSpecimen<>(specimenType, context, specimenFactory);
113+
assertThat(context.isCached(specimenType)).isFalse();
112114

113115
var actual = sut.create(noContext(), new Annotation[0]);
114116

115117
assertThat(actual).isInstanceOf(Charset.class);
118+
assertThat(context.isCached(specimenType)).isFalse();
116119
}
117120

118121
@Test
119-
void resultIsCached() {
122+
void resultIsNotCached() {
120123

121124
var original = new AbstractSpecimen<TestAbstractClass>(SpecimenType.fromClass(TestAbstractClass.class), context, specimenFactory).create(noContext(), new Annotation[0]);
122-
var cached = new AbstractSpecimen<TestAbstractClass>(SpecimenType.fromClass(TestAbstractClass.class), context, specimenFactory).create(noContext(), new Annotation[0]);
125+
var second = new AbstractSpecimen<TestAbstractClass>(SpecimenType.fromClass(TestAbstractClass.class), context, specimenFactory).create(noContext(), new Annotation[0]);
123126

124127
assertThat(original).isInstanceOf(TestAbstractClass.class);
125-
assertThat(original).isSameAs(cached);
126-
assertThat(original.toString()).isEqualTo(cached.toString());
127-
assertThat(original.getString()).isSameAs(cached.getString());
128+
assertThat(original).isNotEqualTo(second);
129+
assertThat(original.hashCode()).isNotEqualTo(second.hashCode());
130+
assertThat(original.toString()).isNotEqualTo(second.toString());
131+
assertThat(original.getString()).isNotEqualTo(second.getString());
128132
}
129133
}
130134

0 commit comments

Comments
 (0)