Skip to content

Commit d5079f4

Browse files
committed
j-easy#441 test suggested in the issue and a fix
1 parent 0426dcd commit d5079f4

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class FieldPopulator {
7676
}
7777

7878
void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException {
79-
Randomizer<?> randomizer = getRandomizer(field, context);
79+
Randomizer<?> randomizer = getRandomizer(field, context, target.getClass());
8080
if (randomizer instanceof SkipRandomizer) {
8181
return;
8282
}
@@ -113,14 +113,14 @@ void populateField(final Object target, final Field field, final RandomizationCo
113113
context.popStackItem();
114114
}
115115

116-
private Randomizer<?> getRandomizer(Field field, RandomizationContext context) {
116+
private Randomizer<?> getRandomizer(Field field, RandomizationContext context, Class<?> fieldTargetType) {
117117
// issue 241: if there is no custom randomizer by field, then check by type
118118
Randomizer<?> randomizer = randomizerProvider.getRandomizerByField(field, context);
119119
if (randomizer == null) {
120120
Type genericType = field.getGenericType();
121121
if (isTypeVariable(genericType)) {
122122
// if generic type, retrieve actual type from declaring class
123-
Class<?> type = getParametrizedType(field, context);
123+
Class<?> type = getParametrizedType(field, fieldTargetType);
124124
randomizer = randomizerProvider.getRandomizerByType(type, context);
125125
} else {
126126
randomizer = randomizerProvider.getRandomizerByType(field.getType(), context);
@@ -154,18 +154,18 @@ private Object generateRandomValue(final Field field, final RandomizationContext
154154
Type genericType = field.getGenericType();
155155
if (isTypeVariable(genericType)) {
156156
// if generic type, try to retrieve actual type from hierarchy
157-
Class<?> type = getParametrizedType(field, context);
157+
Class<?> type = getParametrizedType(field, context.getTargetType());
158158
return easyRandom.doPopulateBean(type, context);
159159
}
160160
return easyRandom.doPopulateBean(fieldType, context);
161161
}
162162
}
163163
}
164164

165-
private Class<?> getParametrizedType(Field field, RandomizationContext context) {
165+
private Class<?> getParametrizedType(Field field, Class<?> fieldTargetType) {
166166
Class<?> declaringClass = field.getDeclaringClass();
167167
TypeVariable<? extends Class<?>>[] typeParameters = declaringClass.getTypeParameters();
168-
Type genericSuperclass = getGenericSuperClass(context);
168+
Type genericSuperclass = getGenericSuperClass(fieldTargetType);
169169
ParameterizedType parameterizedGenericSuperType = (ParameterizedType) genericSuperclass;
170170
Type[] actualTypeArguments = parameterizedGenericSuperType.getActualTypeArguments();
171171
Type actualTypeArgument = null;
@@ -192,8 +192,7 @@ private Class<?> getParametrizedType(Field field, RandomizationContext context)
192192
}
193193

194194
// find the generic base class in the hierarchy (which might not be the first super type)
195-
private Type getGenericSuperClass(RandomizationContext context) {
196-
Class<?> targetType = context.getTargetType();
195+
private Type getGenericSuperClass(Class<?> targetType) {
197196
Type genericSuperclass = targetType.getGenericSuperclass();
198197
while (targetType != null && !(genericSuperclass instanceof ParameterizedType)) {
199198
targetType = targetType.getSuperclass();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2020, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.jeasy.random;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
import java.io.Serializable;
29+
import org.junit.jupiter.api.Test;
30+
31+
/** Test suggested in https://github.com/j-easy/easy-random/issues/441 by @seregamorph */
32+
public class Generic2Test {
33+
34+
@Test
35+
void genericComposedShouldBeCorrectlyPopulated() {
36+
// given
37+
EasyRandom easyRandom = new EasyRandom();
38+
39+
// when
40+
CompositeResource composite = easyRandom.nextObject(CompositeResource.class);
41+
42+
// then
43+
assertThat(composite.longResource.getId())
44+
.isInstanceOf(Long.class)
45+
.isNotNull();
46+
}
47+
48+
static abstract class IdResource<K extends Serializable, T extends IdResource<K, ?>> {
49+
50+
private K id;
51+
52+
@SuppressWarnings("unchecked")
53+
public T setId(K id) {
54+
this.id = id;
55+
return (T) this;
56+
}
57+
58+
public K getId() {
59+
return id;
60+
}
61+
}
62+
63+
static class LongResource extends IdResource<Long, LongResource> {
64+
}
65+
66+
static class CompositeResource {
67+
private LongResource longResource;
68+
}
69+
}

0 commit comments

Comments
 (0)