Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix record list generation. Moved record factory declaration to class… #27

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class EasyRandom extends Random {

private final ExclusionPolicy exclusionPolicy;

private final RecordFactory recordFactory;

/**
* Create a new {@link EasyRandom} instance with default parameters.
*/
Expand Down Expand Up @@ -92,6 +94,7 @@ public EasyRandom(final EasyRandomParameters easyRandomParameters) {
);
exclusionPolicy = easyRandomParameters.getExclusionPolicy();
parameters = easyRandomParameters;
recordFactory = new RecordFactory();
}

/**
Expand Down Expand Up @@ -143,7 +146,7 @@ <T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {
}

if (isRecord(type)) {
return new RecordFactory(context).createInstance(type, context);
return recordFactory.createInstance(type, context);
}

// Collection types are randomized without introspection for internal fields
Expand Down
15 changes: 4 additions & 11 deletions easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
*/
package org.jeasy.random;

import static org.jeasy.random.util.ReflectionUtils.isArrayType;
import static org.jeasy.random.util.ReflectionUtils.isCollectionType;
import static org.jeasy.random.util.ReflectionUtils.isMapType;
import static org.jeasy.random.util.ReflectionUtils.isOptionalType;
import static org.jeasy.random.util.ReflectionUtils.*;

import java.lang.reflect.Constructor;
import java.lang.reflect.RecordComponent;
Expand All @@ -43,18 +40,14 @@
public class RecordFactory extends ObjenesisObjectFactory {

private EasyRandom easyRandom;
private final RandomizationContext contextImpl;

public RecordFactory(RandomizationContext contextImpl) {
this.contextImpl = contextImpl;
}

@Override
public <T> T createInstance(Class<T> type, RandomizerContext context) {
if (easyRandom == null) {
easyRandom = new EasyRandom(contextImpl.getParameters());
easyRandom = new EasyRandom(context.getParameters());
}
return createRandomRecord(type, contextImpl);

return createRandomRecord(type, (RandomizationContext) context);
}

private <T> T createRandomRecord(Class<T> recordType, RandomizationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ void customRandomzierForTypeRecordShouldBeUsedToPopulateObjects() {
}
}

@Test
void typeRecordListShouldBePopulatedWithDifferentValues() {
List<TestRecord> recordList = easyRandom.objects(TestRecord.class, 2).toList();

TestRecord rnd1 = recordList.get(0);
TestRecord rnd2 = recordList.get(1);

assertThat(rnd1).isNotEqualTo(rnd2);
}

@Test
void differentCollectionsShouldBeRandomizedWithDifferentSizes() {
// given
Expand Down