Skip to content

Commit

Permalink
#441 test suggested in the issue and a fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Sep 6, 2021
1 parent 0426dcd commit d5079f4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class FieldPopulator {
}

void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException {
Randomizer<?> randomizer = getRandomizer(field, context);
Randomizer<?> randomizer = getRandomizer(field, context, target.getClass());
if (randomizer instanceof SkipRandomizer) {
return;
}
Expand Down Expand Up @@ -113,14 +113,14 @@ void populateField(final Object target, final Field field, final RandomizationCo
context.popStackItem();
}

private Randomizer<?> getRandomizer(Field field, RandomizationContext context) {
private Randomizer<?> getRandomizer(Field field, RandomizationContext context, Class<?> fieldTargetType) {
// issue 241: if there is no custom randomizer by field, then check by type
Randomizer<?> randomizer = randomizerProvider.getRandomizerByField(field, context);
if (randomizer == null) {
Type genericType = field.getGenericType();
if (isTypeVariable(genericType)) {
// if generic type, retrieve actual type from declaring class
Class<?> type = getParametrizedType(field, context);
Class<?> type = getParametrizedType(field, fieldTargetType);
randomizer = randomizerProvider.getRandomizerByType(type, context);
} else {
randomizer = randomizerProvider.getRandomizerByType(field.getType(), context);
Expand Down Expand Up @@ -154,18 +154,18 @@ private Object generateRandomValue(final Field field, final RandomizationContext
Type genericType = field.getGenericType();
if (isTypeVariable(genericType)) {
// if generic type, try to retrieve actual type from hierarchy
Class<?> type = getParametrizedType(field, context);
Class<?> type = getParametrizedType(field, context.getTargetType());
return easyRandom.doPopulateBean(type, context);
}
return easyRandom.doPopulateBean(fieldType, context);
}
}
}

private Class<?> getParametrizedType(Field field, RandomizationContext context) {
private Class<?> getParametrizedType(Field field, Class<?> fieldTargetType) {
Class<?> declaringClass = field.getDeclaringClass();
TypeVariable<? extends Class<?>>[] typeParameters = declaringClass.getTypeParameters();
Type genericSuperclass = getGenericSuperClass(context);
Type genericSuperclass = getGenericSuperClass(fieldTargetType);
ParameterizedType parameterizedGenericSuperType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = parameterizedGenericSuperType.getActualTypeArguments();
Type actualTypeArgument = null;
Expand All @@ -192,8 +192,7 @@ private Class<?> getParametrizedType(Field field, RandomizationContext context)
}

// find the generic base class in the hierarchy (which might not be the first super type)
private Type getGenericSuperClass(RandomizationContext context) {
Class<?> targetType = context.getTargetType();
private Type getGenericSuperClass(Class<?> targetType) {
Type genericSuperclass = targetType.getGenericSuperclass();
while (targetType != null && !(genericSuperclass instanceof ParameterizedType)) {
targetType = targetType.getSuperclass();
Expand Down
69 changes: 69 additions & 0 deletions easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License
*
* Copyright (c) 2020, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.random;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.Serializable;
import org.junit.jupiter.api.Test;

/** Test suggested in https://github.com/j-easy/easy-random/issues/441 by @seregamorph */
public class Generic2Test {

@Test
void genericComposedShouldBeCorrectlyPopulated() {
// given
EasyRandom easyRandom = new EasyRandom();

// when
CompositeResource composite = easyRandom.nextObject(CompositeResource.class);

// then
assertThat(composite.longResource.getId())
.isInstanceOf(Long.class)
.isNotNull();
}

static abstract class IdResource<K extends Serializable, T extends IdResource<K, ?>> {

private K id;

@SuppressWarnings("unchecked")
public T setId(K id) {
this.id = id;
return (T) this;
}

public K getId() {
return id;
}
}

static class LongResource extends IdResource<Long, LongResource> {
}

static class CompositeResource {
private LongResource longResource;
}
}

0 comments on commit d5079f4

Please sign in to comment.