Skip to content

Commit b099bba

Browse files
committed
fix: use pre-defined specimen when available
Refs: #98
1 parent 124bef5 commit b099bba

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.nylle.javafixture.specimen.InterfaceSpecimen;
99
import com.github.nylle.javafixture.specimen.MapSpecimen;
1010
import com.github.nylle.javafixture.specimen.ObjectSpecimen;
11+
import com.github.nylle.javafixture.specimen.PredefinedSpecimen;
1112
import com.github.nylle.javafixture.specimen.PrimitiveSpecimen;
1213
import com.github.nylle.javafixture.specimen.SpecialSpecimen;
1314
import com.github.nylle.javafixture.specimen.TimeSpecimen;
@@ -22,6 +23,10 @@ public SpecimenFactory(Context context) {
2223

2324
public <T> ISpecimen<T> build(final SpecimenType<T> type) {
2425

26+
if ( context.isCached(type) ) {
27+
return new PredefinedSpecimen<>( type, context );
28+
}
29+
2530
if (type.isPrimitive() || type.isBoxed() || type.asClass() == String.class) {
2631
return new PrimitiveSpecimen<>(type, context);
2732
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.nylle.javafixture.specimen;
2+
3+
import com.github.nylle.javafixture.Context;
4+
import com.github.nylle.javafixture.CustomizationContext;
5+
import com.github.nylle.javafixture.ISpecimen;
6+
import com.github.nylle.javafixture.SpecimenType;
7+
8+
import java.lang.annotation.Annotation;
9+
10+
public class PredefinedSpecimen<T> implements ISpecimen<T> {
11+
12+
private final Context context;
13+
private final SpecimenType<T> type;
14+
15+
public PredefinedSpecimen( SpecimenType<T> type, Context context ) {
16+
if (type == null) {
17+
throw new IllegalArgumentException("type: null");
18+
}
19+
20+
if (context == null) {
21+
throw new IllegalArgumentException("context: null");
22+
}
23+
24+
this.context = context;
25+
this.type = type;
26+
}
27+
28+
@Override
29+
public T create( CustomizationContext customizationContext, Annotation[] annotations ) {
30+
return context.cached( type );
31+
}
32+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.nylle.javafixture.testobjects.example.ContractPosition;
2020
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithGenericConstructor;
2121
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithoutDefaultConstructor;
22+
import org.assertj.core.api.SoftAssertions;
2223
import org.junit.jupiter.api.Disabled;
2324
import org.junit.jupiter.api.DisplayName;
2425
import org.junit.jupiter.api.Nested;
@@ -242,13 +243,18 @@ void canOmitPrivatePrimitiveFieldAndInitializesDefaultValue() {
242243
@Test
243244
void objectCanBeCustomizedWithType() {
244245
var expected = Period.ofDays(42);
246+
var expectedFile = new File( "expected-file-name" );
245247
var result = fixture().build(Contract.class)
246248
.with(Period.class, expected)
249+
.with(File.class, expectedFile)
247250
.create();
248251

249252
var actual = result.getBaseContractPosition().getRemainingPeriod();
250253

251-
assertThat(actual).isSameAs(expected);
254+
var softly = new SoftAssertions();
255+
softly.assertThat(actual).isSameAs(expected);
256+
softly.assertThat(result.getBaseContractPosition().getFile()).isEqualTo( expectedFile );
257+
softly.assertAll();
252258
}
253259

254260
@Test

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
import com.github.nylle.javafixture.specimen.InterfaceSpecimen;
1010
import com.github.nylle.javafixture.specimen.MapSpecimen;
1111
import com.github.nylle.javafixture.specimen.ObjectSpecimen;
12+
import com.github.nylle.javafixture.specimen.PredefinedSpecimen;
1213
import com.github.nylle.javafixture.specimen.PrimitiveSpecimen;
1314
import com.github.nylle.javafixture.specimen.SpecialSpecimen;
1415
import com.github.nylle.javafixture.specimen.TimeSpecimen;
1516
import com.github.nylle.javafixture.testobjects.TestEnum;
1617
import com.github.nylle.javafixture.testobjects.TestObjectGeneric;
18+
import com.github.nylle.javafixture.testobjects.TestPrimitive;
1719
import com.github.nylle.javafixture.testobjects.example.IContract;
20+
import org.junit.jupiter.api.DisplayName;
1821
import org.junit.jupiter.api.Test;
1922

2023
import java.io.File;
@@ -66,7 +69,18 @@ void buildGeneric() {
6669
assertThat(sut.build(new SpecimenType<Map<String, Integer>>(){})).isExactlyInstanceOf(MapSpecimen.class);
6770
assertThat(sut.build(new SpecimenType<Class<String>>(){})).isExactlyInstanceOf(GenericSpecimen.class);
6871
assertThat(sut.build(new SpecimenType<TestObjectGeneric<String, List<Integer>>>(){})).isExactlyInstanceOf(GenericSpecimen.class);
72+
}
73+
74+
@Test
75+
@DisplayName( "when cache contains a predefined value, return this" )
76+
void buildReturnsCacnedValue() {
77+
var context = new Context( new Configuration() );
78+
var cachedValue = new TestPrimitive();
79+
var type = SpecimenType.fromClass( TestPrimitive.class );
80+
context.overwrite( type, cachedValue );
81+
var sut = new SpecimenFactory( context );
6982

83+
assertThat( sut.build( type ) ).isExactlyInstanceOf( PredefinedSpecimen.class );
7084
}
7185

7286

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.nylle.javafixture.specimen;
2+
3+
import com.github.nylle.javafixture.Context;
4+
import com.github.nylle.javafixture.SpecimenType;
5+
import com.github.nylle.javafixture.testobjects.TestEnum;
6+
import com.github.nylle.javafixture.testobjects.TestObject;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.github.nylle.javafixture.Configuration.configure;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
13+
public class PredefinedSpecimenTest {
14+
@Test
15+
void typeIsRequired() {
16+
assertThatThrownBy(() -> new PredefinedSpecimen<>(null, new Context( configure())))
17+
.isInstanceOf(IllegalArgumentException.class)
18+
.hasMessageContaining("type: null");
19+
}
20+
21+
@Test
22+
void contextIsRequired() {
23+
assertThatThrownBy(() -> new PredefinedSpecimen<>( SpecimenType.fromClass( TestEnum.class), null))
24+
.isInstanceOf(IllegalArgumentException.class)
25+
.hasMessageContaining("context: null");
26+
}
27+
28+
@Test
29+
void createReturnsCachedValue() {
30+
var type = SpecimenType.fromClass( TestObject.class );
31+
var testObject = new TestObject( null, null, null );
32+
var context = new Context( configure() );
33+
context.overwrite( type, testObject);
34+
var sut = new PredefinedSpecimen<>( type, context );
35+
36+
assertThat( sut.create( null, null ) ).isEqualTo( testObject );
37+
}
38+
39+
@Test
40+
void createReturnsNullIfNoCachedValueIsFound() {
41+
var type = SpecimenType.fromClass( TestObject.class );
42+
var context = new Context( configure() );
43+
var sut = new PredefinedSpecimen<>( type, context );
44+
45+
assertThat( sut.create( null, null ) ).isNull();
46+
}
47+
}

0 commit comments

Comments
 (0)