Skip to content

Commit 7ce1d09

Browse files
akutscherajk-idealo
authored andcommitted
fix: use customization in top-level object only
Refs: JF-75
1 parent c16ffba commit 7ce1d09

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.nylle.javafixture.SpecimenType;
1111

1212
import java.lang.annotation.Annotation;
13+
import java.util.List;
1314
import java.util.Map;
1415
import java.util.stream.IntStream;
1516

@@ -90,7 +91,7 @@ private T populate(CustomizationContext customizationContext) {
9091
field.getName(),
9192
specimens.getOrDefault(
9293
field.getGenericType().getTypeName(),
93-
specimenFactory.build(SpecimenType.fromClass(field.getType()))).create(customizationContext, new Annotation[0]))));
94+
specimenFactory.build(SpecimenType.fromClass(field.getType()))).create(new CustomizationContext(List.of(), Map.of()), new Annotation[0]))));
9495
} catch (SpecimenException ex ) {
9596
return context.overwrite(type, instanceFactory.construct(type));
9697
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.nylle.javafixture.SpecimenType;
1111

1212
import java.lang.annotation.Annotation;
13+
import java.util.List;
1314
import java.util.Map;
1415

1516
public class ObjectSpecimen<T> implements ISpecimen<T> {
@@ -68,7 +69,7 @@ private T populate(CustomizationContext customizationContext) {
6869
field.getName(),
6970
Map.<String, ISpecimen<?>>of().getOrDefault(
7071
field.getGenericType().getTypeName(),
71-
specimenFactory.build(SpecimenType.fromClass(field.getGenericType()))).create(customizationContext, field.getAnnotations()))));
72+
specimenFactory.build(SpecimenType.fromClass(field.getGenericType()))).create(new CustomizationContext(List.of(), Map.of()), field.getAnnotations()))));
7273
} catch (SpecimenException ex ) {
7374
return context.overwrite(type, instanceFactory.construct(type));
7475
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.nylle.javafixture.SpecimenException;
77
import com.github.nylle.javafixture.SpecimenFactory;
88
import com.github.nylle.javafixture.SpecimenType;
9+
import com.github.nylle.javafixture.testobjects.TestObject;
910
import com.github.nylle.javafixture.testobjects.TestObjectGeneric;
1011
import com.github.nylle.javafixture.testobjects.inheritance.GenericChild;
1112
import org.junit.jupiter.api.BeforeEach;
@@ -131,6 +132,21 @@ void cannotOmitNonExistingField() {
131132
.withNoCause();
132133
}
133134

135+
@Test
136+
void customFieldIsOnlyUsedInTopLevelObject() {
137+
var sut = new GenericSpecimen<>(new SpecimenType<WithTestObject<Integer>>() {}, context, specimenFactory);
138+
139+
var customizationContext = new CustomizationContext(List.of(), Map.of("topLevelValue", 42));
140+
141+
var actual = sut.create(customizationContext, new Annotation[0]);
142+
143+
assertThat(actual.getTopLevelValue()).isEqualTo(42);
144+
assertThat( actual.getTestObject() ).isNotNull();
145+
assertThat( actual.getTestObject().getValue() ).isNotNull();
146+
assertThat( actual.getTestObject().getStrings() ).isNotEmpty();
147+
assertThat( actual.getTestObject().getIntegers() ).isNotEmpty();
148+
}
149+
134150
@Nested
135151
@DisplayName("when specimen has superclass")
136152
class WhenInheritance {
@@ -200,4 +216,17 @@ void firstFieldPerNameIsOmitted() {
200216
.isThrownBy(() -> sut.create(new CustomizationContext(omitting, Map.of()), new Annotation[0]));
201217
}
202218
}
219+
220+
public static class WithTestObject<T> {
221+
private T topLevelValue;
222+
private TestObject testObject;
223+
224+
public T getTopLevelValue() {
225+
return topLevelValue;
226+
}
227+
228+
public TestObject getTestObject() {
229+
return testObject;
230+
}
231+
}
203232
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ void cannotOmitNonExistingField() {
138138
.withNoCause();
139139
}
140140

141+
@Test
142+
void customFieldIsOnlyUsedInTopLevelObject() {
143+
var sut = new ObjectSpecimen<WithTestObject>(SpecimenType.fromClass(WithTestObject.class), context, specimenFactory);
144+
var customizationContext = new CustomizationContext(List.of(), Map.of("topLevelValue", 42));
145+
var actual = sut.create(customizationContext, new Annotation[0]);
146+
assertThat(actual.getTopLevelValue()).isEqualTo(42);
147+
assertThat( actual.getTestObject() ).isNotNull();
148+
assertThat( actual.getTestObject().getValue() ).isNotNull();
149+
assertThat( actual.getTestObject().getStrings() ).isNotEmpty();
150+
assertThat( actual.getTestObject().getIntegers() ).isNotEmpty();
151+
}
152+
141153
@Nested
142154
@DisplayName("when specimen has superclass")
143155
class WhenInheritance {
@@ -207,5 +219,18 @@ void firstFieldPerNameIsOmitted() {
207219
.isThrownBy(() -> sut.create(new CustomizationContext(omitting, Map.of()), new Annotation[0]));
208220
}
209221
}
222+
223+
public static class WithTestObject {
224+
private int topLevelValue;
225+
private TestObject testObject;
226+
227+
public int getTopLevelValue() {
228+
return topLevelValue;
229+
}
230+
231+
public TestObject getTestObject() {
232+
return testObject;
233+
}
234+
}
210235
}
211236

0 commit comments

Comments
 (0)