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

#461 populate already initialized Collections #462

Closed
Closed
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
4 changes: 2 additions & 2 deletions easy-random-bean-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>4.3.0</version>
<version>4.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -79,4 +79,4 @@
</dependency>
</dependencies>

</project>
</project>
4 changes: 2 additions & 2 deletions easy-random-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>4.3.0</version>
<version>4.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -96,4 +96,4 @@
</plugins>
</build>

</project>
</project>
15 changes: 13 additions & 2 deletions easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,23 @@ private <T> void populateField(final Field field, final T result, final Randomiz
if (exclusionPolicy.shouldBeExcluded(field, context)) {
return;
}
if (!parameters.isOverrideDefaultInitialization() && getFieldValue(result, field) != null && !isPrimitiveFieldWithDefaultValue(result, field)) {
if (!parameters.isOverrideDefaultInitialization() && !fieldValueIsNullOrEmpty(result, field) && !isPrimitiveFieldWithDefaultValue(result, field)) {
return;
}
fieldPopulator.populateField(result, field, context);
}

private <T> boolean fieldValueIsNullOrEmpty(T result, Field field) throws IllegalAccessException {
T value = (T) getFieldValue(result, field);
if (null == value) {
return true;
}
if (value instanceof Collection) {
return ((Collection) value).isEmpty();
}
return false;
}

private LinkedHashSet<RandomizerRegistry> setupRandomizerRegistries(EasyRandomParameters parameters) {
LinkedHashSet<RandomizerRegistry> registries = new LinkedHashSet<>();
registries.add(parameters.getCustomRandomizerRegistry());
Expand All @@ -225,4 +236,4 @@ private Collection<RandomizerRegistry> loadRegistries() {
return registries;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,51 @@ private void assertContainsOnlyNonEmptyPersons(Collection<Person> persons) {
assertThat(Person.getName()).isNotEmpty();
}
}
}

@Test
void typedCollectionsShouldNotBeEmpty() {
EasyRandom easyRandom = new EasyRandom(
new EasyRandomParameters()
.collectionSizeRange(1, 1)
);

final CollectionBean collectionsBean = easyRandom.nextObject(CollectionBean.class);

assertThat(collectionsBean).isNotNull();

assertThat(collectionsBean.getTypedCollection()).isNotEmpty();
assertThat(collectionsBean.getTypedSet()).isNotEmpty();
assertThat(collectionsBean.getTypedSortedSet()).isNotEmpty();
assertThat(collectionsBean.getTypedNavigableSet()).isNotEmpty();
assertThat(collectionsBean.getTypedList()).isNotEmpty();
assertThat(collectionsBean.getTypedQueue()).isNotEmpty();
assertThat(collectionsBean.getPreInitedTypedList()).isNotEmpty();
assertThat(collectionsBean.getTypedBlockingQueue()).isNotEmpty();
assertThat(collectionsBean.getTypedTransferQueue()).isNotEmpty();
assertThat(collectionsBean.getTypedDeque()).isNotEmpty();
assertThat(collectionsBean.getTypedBlockingDeque()).isNotEmpty();
}

@Test
void untypedCollectionsShouldBeEmpty() {
EasyRandom easyRandom = new EasyRandom(
new EasyRandomParameters()
.collectionSizeRange(1, 1)
);

final CollectionBean collectionsBean = easyRandom.nextObject(CollectionBean.class);

assertThat(collectionsBean).isNotNull();
assertThat(collectionsBean.getCollection()).isEmpty();
assertThat(collectionsBean.getSet()).isEmpty();
assertThat(collectionsBean.getSortedSet()).isEmpty();
assertThat(collectionsBean.getNavigableSet()).isEmpty();
assertThat(collectionsBean.getList()).isEmpty();
assertThat(collectionsBean.getQueue()).isEmpty();
assertThat(collectionsBean.getPreInitedList()).isEmpty();
assertThat(collectionsBean.getBlockingQueue()).isEmpty();
assertThat(collectionsBean.getTransferQueue()).isEmpty();
assertThat(collectionsBean.getDeque()).isEmpty();
assertThat(collectionsBean.getBlockingDeque()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public class CollectionBean {
private NavigableSet<Person> typedNavigableSet;

private List list;
private List preInitedList = new ArrayList();
private List<Person> typedList;
private List<Person> preInitedTypedList = new ArrayList<>();

private Queue queue;
private Queue<Person> typedQueue;
Expand Down Expand Up @@ -162,6 +164,14 @@ public List<Person> getTypedList() {
return this.typedList;
}

public List getPreInitedList() {
return preInitedList;
}

public List<Person> getPreInitedTypedList() {
return preInitedTypedList;
}

public Queue getQueue() {
return this.queue;
}
Expand Down Expand Up @@ -374,10 +384,18 @@ public void setList(List list) {
this.list = list;
}

public void setPreInitedList(List preInitedList) {
this.preInitedList = preInitedList;
}

public void setTypedList(List<Person> typedList) {
this.typedList = typedList;
}

public void setPreInitedTypedList(List<Person> preInitedTypedList) {
this.preInitedTypedList = preInitedTypedList;
}

public void setQueue(Queue queue) {
this.queue = queue;
}
Expand Down Expand Up @@ -553,4 +571,4 @@ public void setConcurrentLinkedDeque(ConcurrentLinkedDeque concurrentLinkedDeque
public void setTypedConcurrentLinkedDeque(ConcurrentLinkedDeque<Person> typedConcurrentLinkedDeque) {
this.typedConcurrentLinkedDeque = typedConcurrentLinkedDeque;
}
}
}
4 changes: 2 additions & 2 deletions easy-random-randomizers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>4.3.0</version>
<version>4.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -69,4 +69,4 @@
</dependency>
</dependencies>

</project>
</project>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.jeasy</groupId>
<artifactId>easy-random</artifactId>
<version>4.3.0</version>
<version>4.3.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Easy Random</name>
Expand Down Expand Up @@ -247,4 +247,4 @@
</profile>
</profiles>

</project>
</project>