Skip to content

Commit 9b4d122

Browse files
committed
feat: add BigInteger to SpecialSpecimen
By limiting the number of bits to max. 1024, we save some time. Creating a BigInteger with Integer.MAX_VALUE bits can easily take one second or more. Refs: JF-78
1 parent 2120a76 commit 9b4d122

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public boolean isTimeType() {
162162
}
163163

164164
public boolean isSpecialType() {
165+
if (asClass().equals(java.math.BigInteger.class)) {
166+
return true;
167+
}
165168
if (asClass().equals(java.io.File.class)) {
166169
return true;
167170
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
import java.io.File;
99
import java.lang.annotation.Annotation;
10+
import java.math.BigInteger;
1011
import java.net.URI;
1112
import java.net.URISyntaxException;
13+
import java.util.Random;
1214
import java.util.UUID;
1315

1416
public class SpecialSpecimen<T> implements ISpecimen<T> {
17+
private static final int MAXIMUM_BITLENGTH_FOR_RANDOM_BIGINT = 1024;
1518
private final SpecimenType<T> type;
1619
private final Context context;
1720

@@ -38,10 +41,24 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
3841
if (type.asClass().equals(File.class)) {
3942
return (T) new File(UUID.randomUUID().toString());
4043
}
44+
if (type.asClass().equals(BigInteger.class)) {
45+
return (T) createBigInteger();
46+
}
4147
try {
4248
return (T) new URI("https://localhost/" + UUID.randomUUID());
4349
} catch (URISyntaxException e) {
4450
return null;
4551
}
4652
}
53+
54+
private BigInteger createBigInteger() {
55+
var rnd = new Random();
56+
var result = new BigInteger(rnd.nextInt(MAXIMUM_BITLENGTH_FOR_RANDOM_BIGINT), new Random());
57+
if (!context.getConfiguration().usePositiveNumbersOnly()) {
58+
if (rnd.nextBoolean()) { // negate randomly
59+
return result.negate();
60+
}
61+
}
62+
return result;
63+
}
4764
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.File;
1717
import java.lang.reflect.ParameterizedType;
1818
import java.lang.reflect.Type;
19+
import java.math.BigInteger;
1920
import java.net.URI;
2021
import java.time.Duration;
2122
import java.time.Instant;
@@ -258,14 +259,16 @@ void isTimeTypeFromClass(Class<?> value, boolean expected) {
258259
@Test
259260
void isSpecialType() {
260261
assertThat(new SpecimenType<String>() {}.isSpecialType()).isFalse(); // false
261-
assertThat(new SpecimenType<URI>() {}.isSpecialType()).isTrue();
262262
assertThat(new SpecimenType<File>() {}.isSpecialType()).isTrue();
263+
assertThat(new SpecimenType<URI>() {}.isSpecialType()).isTrue();
264+
assertThat(new SpecimenType<BigInteger>() {}.isSpecialType()).isTrue();
263265
}
264266

265267
@TestWithCases
266268
@TestCase(class1 = String.class, bool2 = false)
267269
@TestCase(class1 = File.class, bool2 = true)
268270
@TestCase(class1 = URI.class, bool2 = true)
271+
@TestCase(class1 = BigInteger.class, bool2 = true)
269272
void isSpecialTypeFromClass(Class<?> value, boolean expected) {
270273
assertThat(SpecimenType.fromClass(value).isSpecialType()).isEqualTo(expected);
271274
}

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.File;
1313
import java.lang.annotation.Annotation;
14+
import java.math.BigInteger;
1415
import java.net.URI;
1516
import java.util.Map;
1617

@@ -54,12 +55,13 @@ void contextIsRequired() {
5455
}
5556

5657
}
58+
5759
@Test
5860
@DisplayName("creating two files creates two different files")
5961
void createFile() {
6062
var sut = new SpecialSpecimen<>(SpecimenType.fromClass(File.class), context);
61-
var first = (File)sut.create(noContext(), new Annotation[0]);
62-
var second = (File)sut.create(noContext(), new Annotation[0]);
63+
var first = (File) sut.create(noContext(), new Annotation[0]);
64+
var second = (File) sut.create(noContext(), new Annotation[0]);
6365

6466
assertThat(first.getAbsolutePath()).isNotEqualTo(second.getAbsolutePath());
6567
}
@@ -69,10 +71,10 @@ void createFile() {
6971
void createFileWithoutContext() {
7072
var sut = new SpecialSpecimen<>(SpecimenType.fromClass(File.class), context);
7173

72-
var actual = (File)sut.create(noContext(), new Annotation[0]);
74+
var actual = (File) sut.create(noContext(), new Annotation[0]);
7375

74-
assertThat( actual ).isNotNull();
75-
assertThat( actual.getAbsolutePath() ).isNotEmpty();
76+
assertThat(actual).isNotNull();
77+
assertThat(actual.getAbsolutePath()).isNotEmpty();
7678
}
7779

7880

@@ -83,9 +85,30 @@ void craeteURI() {
8385
var actual = sut.create(noContext(), new Annotation[0]);
8486

8587
assertThat(actual).isInstanceOf(URI.class);
86-
assertThat( ((URI)actual).getScheme() ).isEqualTo("https");
87-
assertThat( ((URI)actual).getHost() ).isNotNull();
88-
assertThat( ((URI)actual).getPath() ).isNotEmpty();
88+
assertThat(((URI) actual).getScheme()).isEqualTo("https");
89+
assertThat(((URI) actual).getHost()).isNotNull();
90+
assertThat(((URI) actual).getPath()).isNotEmpty();
91+
}
92+
93+
@Test
94+
@DisplayName("create BigInteger creates random number with a maximum bit length of 1024")
95+
void createBigInteger() {
96+
97+
var sut = new SpecialSpecimen<>(new SpecimenType<BigInteger>() {}, context);
98+
var actual = sut.create(noContext(), new Annotation[0]);
99+
100+
assertThat(actual).isInstanceOf(BigInteger.class);
101+
assertThat(actual.bitLength()).isLessThanOrEqualTo(1024);
102+
}
103+
104+
@Test
105+
@DisplayName("create BigInteger creates non-negative random number when context demands it")
106+
void createNonNegativeBigInteger() {
107+
var context = new Context(Configuration.configure().usePositiveNumbersOnly(true));
108+
var sut = new SpecialSpecimen<>(new SpecimenType<BigInteger>() {}, context);
109+
var actual = sut.create(noContext(), new Annotation[0]);
110+
111+
assertThat(actual).isNotNegative();
89112
}
90113

91114
}

0 commit comments

Comments
 (0)