Skip to content

Commit ea7ba2a

Browse files
committed
make a public generate method that takes oneTimeConfig and remove methods that take sizes
1 parent ab453cf commit ea7ba2a

File tree

9 files changed

+109
-107
lines changed

9 files changed

+109
-107
lines changed
Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package io.javarig;
22

3-
import io.javarig.config.JavaRIGConfig;
3+
import io.javarig.config.Configuration;
44
import io.javarig.exception.InstanceGenerationException;
55
import io.javarig.exception.NestedObjectRecursionException;
66
import io.javarig.generator.TypeGenerator;
7-
import lombok.AllArgsConstructor;
87
import lombok.Getter;
98
import lombok.NonNull;
109
import lombok.RequiredArgsConstructor;
1110

12-
import org.apache.commons.lang3.Validate;
1311

1412
import java.lang.reflect.Type;
1513
import java.util.Stack;
@@ -20,19 +18,23 @@ public class RandomInstanceGenerator {
2018

2119
private final Stack<Type> objectStack = new Stack<>();
2220
private final TypeGeneratorFactory typeGeneratorFactory = new TypeGeneratorFactory();
23-
private final JavaRIGConfig generalConfig;
24-
private JavaRIGConfig oneTimeConfig = null;
21+
private final Configuration generalConfig;
22+
private Configuration oneTimeConfig = null;
2523

2624
public RandomInstanceGenerator() {
27-
this.generalConfig = JavaRIGConfig.builder().build();
25+
this.generalConfig = Configuration.builder().build();
2826
}
2927

3028
/**
3129
* generate a random instance of the given type
32-
*
30+
*
31+
* @param objectType type of the object
3332
* @return the generated object
34-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
35-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
33+
* @throws InstanceGenerationException if the instance cannot be generated for
34+
* some reason (class doesn't have a default
35+
* constructor , class have a non-public
36+
* default constructor , setter cannot be
37+
* invoked ... )
3638
*/
3739
@SuppressWarnings({"unchecked"})
3840
public <T> T generate(@NonNull Type objectType) throws InstanceGenerationException {
@@ -45,52 +47,36 @@ public <T> T generate(@NonNull Type objectType) throws InstanceGenerationExcepti
4547
}
4648

4749
/**
48-
* generate a random instance for a collection, with a fixed size
49-
*
50-
* @param collectionSize the size of the collection to generate
51-
* @return the generated object
52-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
53-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
54-
*/
55-
public <T> T generate(
56-
@NonNull Type type,
57-
int collectionSize
58-
) throws InstanceGenerationException {
59-
validateSize(collectionSize);
60-
JavaRIGConfig oneTimeConfig = JavaRIGConfig.builder()
61-
.maxSizeExclusive(collectionSize + 1)
62-
.minSizeInclusive(collectionSize)
63-
.build();
64-
return generateWithOneTimeConfig(type, oneTimeConfig);
65-
}
66-
67-
/**
68-
* generate a random instance for a collection, with size between a range
50+
* generate a random instance for a collection, with a one time configuration
6951
*
70-
* @param <T> the generic type of the object to generate
52+
* @param objectType type of the object
53+
* @param oneTimeConfig a configuration for the generator that gets applied one
54+
* time and doesn't overide the general configuration
55+
* provided at the creation
7156
* @return the generated object
72-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
73-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
57+
* @throws InstanceGenerationException if the instance cannot be generated for
58+
* some reason (class doesn't have a default
59+
* constructor , class have a non-public
60+
* default constructor , setter cannot be
61+
* invoked ... )
7462
*/
7563
public <T> T generate(@NonNull Type objectType,
76-
int minSizeInclusive,
77-
int maxSizeExclusive
64+
Configuration oneTimeConfig
7865
) throws InstanceGenerationException {
79-
validateSize(minSizeInclusive, maxSizeExclusive);
80-
JavaRIGConfig oneTimeConfig = JavaRIGConfig.builder()
81-
.maxSizeExclusive(maxSizeExclusive)
82-
.minSizeInclusive(minSizeInclusive)
83-
.build();
8466
return generateWithOneTimeConfig(objectType, oneTimeConfig);
8567
}
8668

8769
/**
8870
* generate a random instance of a generic type
8971
*
72+
* @param objectType type of the object
9073
* @param genericTypes types of generic parameters
9174
* @return the generated object
92-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
93-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
75+
* @throws InstanceGenerationException if the instance cannot be generated for
76+
* some reason (class doesn't have a default
77+
* constructor , class have a non-public
78+
* default constructor , setter cannot be
79+
* invoked ... )
9480
*/
9581
public <T> T generate(
9682
@NonNull Type objectType,
@@ -103,41 +89,30 @@ public <T> T generate(
10389
/**
10490
* generate a random instance of a generic collection with a fixed size
10591
*
106-
* @param genericTypes types of generic parameters
107-
* @return the generated object
108-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
109-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
110-
*/
111-
public <T> T generate(
112-
@NonNull Type type,
113-
int collectionSize,
114-
@NonNull Type... genericTypes
115-
) throws InstanceGenerationException {
116-
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) type);
117-
return generate(parameterizedType, collectionSize);
118-
}
119-
120-
/**
121-
* generate a random instance of a generic collection with a size between a range
122-
*
123-
* @param genericTypes types of generic parameters
92+
* @param objectType type of the object
93+
* @param oneTimeConfig a configuration for the generator that gets applied one
94+
* time and doesn't overide the general configuration
95+
* provided at the creation
96+
* @param genericTypes types of generic parameters
12497
* @return the generated object
125-
* @throws InstanceGenerationException if the instance cannot be generated for some reason (class doesn't have
126-
* a default constructor , class have a non-public default constructor , setter cannot be invoked ... )
98+
* @throws InstanceGenerationException if the instance cannot be generated for
99+
* some reason (class doesn't have
100+
* a default constructor , class have a
101+
* non-public default constructor , setter
102+
* cannot be invoked ... )
127103
*/
128104
public <T> T generate(
129-
@NonNull Type type,
130-
int minSizeInclusive,
131-
int maxSizeExclusive,
105+
@NonNull Type objectType,
106+
Configuration oneTimeConfig,
132107
@NonNull Type... genericTypes
133108
) throws InstanceGenerationException {
134-
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) type);
135-
return generate(parameterizedType, minSizeInclusive, maxSizeExclusive);
109+
Type parameterizedType = new ParameterizedTypeImpl(genericTypes, (Class<?>) objectType);
110+
return generate(parameterizedType, oneTimeConfig);
136111
}
137112

138-
private <T> T generateWithOneTimeConfig(@NonNull Type type, JavaRIGConfig oneTimeConfig) {
113+
private <T> T generateWithOneTimeConfig(@NonNull Type objectType, Configuration oneTimeConfig) {
139114
this.oneTimeConfig = oneTimeConfig;
140-
T generatedObject = generate(type);
115+
T generatedObject = generate(objectType);
141116
this.oneTimeConfig = null;
142117
return generatedObject;
143118
}
@@ -149,19 +124,12 @@ private <T> T generateWithOneTimeConfig(@NonNull Type type, JavaRIGConfig oneTim
149124
*
150125
* @param type - a type to search for in the stack
151126
*/
152-
private void checkForRecursion(Type type) {
153-
if (!objectStack.isEmpty() && objectStack.contains(type)) {
154-
throw new NestedObjectRecursionException(type);
127+
private void checkForRecursion(Type objectType) {
128+
if (!objectStack.isEmpty() && objectStack.contains(objectType)) {
129+
throw new NestedObjectRecursionException(objectType);
155130
}
156131
}
157132

158-
private void validateSize(int minSizeInclusive, int maxSizeExclusive) {
159-
Validate.isTrue(maxSizeExclusive > minSizeInclusive, "Start value must be smaller than end value.");
160-
Validate.isTrue(minSizeInclusive >= 0, "Both range values must be non-negative.");
161-
}
162133

163-
private void validateSize(int size) {
164-
Validate.isTrue(size >= 0, "Size must be non-negative.");
165-
}
166134

167135
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.javarig.config;
2+
3+
import org.apache.commons.lang3.Validate;
4+
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
8+
@Getter
9+
@Builder
10+
public class Configuration {
11+
12+
@Builder.Default
13+
private int maxSizeExclusive = DefaultConfigValues.DEFAULT_MAX_SIZE_EXCLUSIVE;
14+
@Builder.Default
15+
private int minSizeInclusive = DefaultConfigValues.DEFAULT_MIN_SIZE_INCLUSIVE;
16+
17+
public static Configuration withSize(int size) {
18+
validateSize(size);
19+
return Configuration.builder()
20+
.maxSizeExclusive(size + 1)
21+
.minSizeInclusive(size)
22+
.build();
23+
}
24+
25+
public static Configuration withSize(int minSizeInclusive, int maxSizeExclusive) {
26+
validateSize(minSizeInclusive, maxSizeExclusive);
27+
return Configuration.builder()
28+
.maxSizeExclusive(maxSizeExclusive + 1)
29+
.minSizeInclusive(minSizeInclusive)
30+
.build();
31+
}
32+
33+
private static void validateSize(int minSizeInclusive, int maxSizeExclusive) {
34+
Validate.isTrue(maxSizeExclusive > minSizeInclusive, "Start value must be smaller than end value.");
35+
Validate.isTrue(minSizeInclusive >= 0, "Both range values must be non-negative.");
36+
}
37+
38+
private static void validateSize(int size) {
39+
Validate.isTrue(size >= 0, "Size must be non-negative.");
40+
}
41+
}

src/main/java/io/javarig/config/JavaRIGConfig.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/io/javarig/generator/ArrayGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private Object generatePrimitiveArray(Class<?> primitiveType) {
3535

3636
private Object[] generateArray(Class<?> arrayParameterType) {
3737
List<Object> objectList = getRandomInstanceGenerator()
38-
.generate(List.class, getConfig().getMinSizeInclusive(), getConfig().getMaxSizeExclusive(), arrayParameterType);
38+
.generate(List.class, arrayParameterType);
3939
Object[] arrayInstance = (Object[]) Array.newInstance(arrayParameterType, 0);
4040
arrayInstance = objectList.toArray(arrayInstance);
4141
return arrayInstance;

src/main/java/io/javarig/generator/TypeGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.javarig.generator;
22

33
import io.javarig.RandomInstanceGenerator;
4-
import io.javarig.config.JavaRIGConfig;
4+
import io.javarig.config.Configuration;
55
import io.javarig.exception.InstanceGenerationException;
66
import lombok.Getter;
77
import lombok.RequiredArgsConstructor;
@@ -21,7 +21,7 @@ public abstract class TypeGenerator {
2121
private final Type type;
2222
private final RandomInstanceGenerator randomInstanceGenerator;
2323

24-
protected JavaRIGConfig getConfig(){
24+
protected Configuration getConfig(){
2525
if(getRandomInstanceGenerator().getOneTimeConfig() != null) {
2626
return getRandomInstanceGenerator().getOneTimeConfig();
2727
}

src/test/java/io/javarig/generation/javapredefinedtypes/ArrayGenerationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javarig.generation.javapredefinedtypes;
22

33
import io.javarig.RandomInstanceGenerator;
4+
import io.javarig.config.Configuration;
45
import io.javarig.testclasses.TestClass;
56
import lombok.extern.slf4j.Slf4j;
67
import org.junit.jupiter.api.BeforeEach;
@@ -33,7 +34,7 @@ public void shouldReturnIntegerArray(Class<?> testClass) {
3334
@Test
3435
public void shouldReturnWrapperArrayWithExactSize() {
3536
int size = 20;
36-
Object generated = randomInstanceGenerator.generate(Double[].class, size);
37+
Object generated = randomInstanceGenerator.generate(Double[].class, Configuration.withSize(size));
3738
log.info("shouldReturnArrayWithExactSize : {}", generated);
3839
assertThat(generated).isNotNull();
3940
assertThat(generated).isInstanceOf(Double[].class);
@@ -43,7 +44,7 @@ public void shouldReturnWrapperArrayWithExactSize() {
4344
@Test
4445
public void shouldReturnPrimitiveArrayWithExactSize() {
4546
int size = 20;
46-
Object generated = randomInstanceGenerator.generate(double[].class, size);
47+
Object generated = randomInstanceGenerator.generate(double[].class, Configuration.withSize(size));
4748
log.info("shouldReturnArrayWithExactSize : {}", generated);
4849
assertThat(generated).isNotNull();
4950
assertThat(generated).isInstanceOf(double[].class);
@@ -54,7 +55,7 @@ public void shouldReturnPrimitiveArrayWithExactSize() {
5455
public void shouldReturnArrayWithSizeBetween() {
5556
int minSize = 20;
5657
int maxSize = 40;
57-
Object generated = randomInstanceGenerator.generate(String[].class, minSize, maxSize);
58+
Object generated = randomInstanceGenerator.generate(String[].class, Configuration.withSize(minSize, maxSize));
5859
log.info("shouldReturnArrayWithSizeBetween : {}", generated);
5960
assertThat(generated).isNotNull();
6061
assertThat(generated).isInstanceOf(String[].class);
@@ -72,7 +73,7 @@ public void shouldReturn2DArray() {
7273
@Test
7374
public void shouldReturnTestClassArrayWithExactSize() {
7475
int size = 22;
75-
Object generated = randomInstanceGenerator.generate(TestClass[].class, size);
76+
Object generated = randomInstanceGenerator.generate(TestClass[].class, Configuration.withSize(size));
7677
log.info("shouldReturnTestClassArrayWithExactSize {} : {}", size, generated);
7778
assertThat(generated).isNotNull();
7879
assertThat(generated).isInstanceOf(TestClass[].class);

src/test/java/io/javarig/generation/javapredefinedtypes/StringGenerationTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javarig.generation.javapredefinedtypes;
22

33
import io.javarig.RandomInstanceGenerator;
4+
import io.javarig.config.Configuration;
45
import lombok.extern.slf4j.Slf4j;
56
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.api.Test;
@@ -29,7 +30,7 @@ public void shouldGenerateString() {
2930
@Test
3031
public void shouldGenerateStringWithExactSize() {
3132
int size = 20;
32-
Object generated = randomInstanceGenerator.generate(String.class, size);
33+
Object generated = randomInstanceGenerator.generate(String.class, Configuration.withSize(size));
3334
log.info("shouldGenerateString : {}", generated);
3435
assertThat(generated).isNotNull();
3536
assertThat(generated).isInstanceOf(String.class);
@@ -40,7 +41,7 @@ public void shouldGenerateStringWithExactSize() {
4041
public void shouldGenerateStringWithSizeBetween() {
4142
int minSize = 20;
4243
int maxSize = 40;
43-
Object generated = randomInstanceGenerator.generate(String.class, minSize, maxSize);
44+
Object generated = randomInstanceGenerator.generate(String.class, Configuration.withSize(minSize, maxSize));
4445
log.info("shouldGenerateString : {}", generated);
4546
assertThat(generated).isNotNull();
4647
assertThat(generated).isInstanceOf(String.class);
@@ -51,7 +52,7 @@ public void shouldThrowIllegalArgumentExceptionGivenStringHavingMinSizeGreaterTh
5152
int minSize = 40;
5253
int maxSize = 20;
5354
assertThatThrownBy(() -> {//when
54-
randomInstanceGenerator.generate(String.class, minSize, maxSize);
55+
randomInstanceGenerator.generate(String.class, Configuration.withSize(minSize, maxSize));
5556
}).isInstanceOf(IllegalArgumentException.class)
5657
.hasMessage("Start value must be smaller than end value.");
5758
}
@@ -60,15 +61,15 @@ public void shouldThrowIllegalArgumentExceptionGivenStringHavingMinSizeGreaterTh
6061
public void shouldThrowIllegalArgumentExceptionGivenStringHavingMinSizeSameASMaxSize() {
6162
int size = 30;
6263
assertThatThrownBy(() -> {//when
63-
randomInstanceGenerator.generate(String.class, size, size);
64+
randomInstanceGenerator.generate(String.class, Configuration.withSize(size, size));
6465
}).isInstanceOf(IllegalArgumentException.class)
6566
.hasMessage("Start value must be smaller than end value.");
6667
}
6768
@Test
6869
public void shouldThrowIllegalArgumentExceptionWhenGivenNegativeSize() {
6970
int size = -20;
7071
assertThatThrownBy(() -> {//when
71-
randomInstanceGenerator.generate(String.class, size);
72+
randomInstanceGenerator.generate(String.class, Configuration.withSize(size));
7273
}).isInstanceOf(IllegalArgumentException.class)
7374
.hasMessage("Size must be non-negative.");
7475
}

0 commit comments

Comments
 (0)