From d5079f4fceb29ef8174906c9de43936116f4c400 Mon Sep 17 00:00:00 2001 From: Marian Jureczko Date: Mon, 6 Sep 2021 11:33:51 +0200 Subject: [PATCH 1/3] #441 test suggested in the issue and a fix --- .../java/org/jeasy/random/FieldPopulator.java | 15 ++-- .../java/org/jeasy/random/Generic2Test.java | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java diff --git a/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java b/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java index 482c7583..733b21fd 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java +++ b/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java @@ -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; } @@ -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); @@ -154,7 +154,7 @@ 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); @@ -162,10 +162,10 @@ private Object generateRandomValue(final Field field, final RandomizationContext } } - private Class getParametrizedType(Field field, RandomizationContext context) { + private Class getParametrizedType(Field field, Class fieldTargetType) { Class declaringClass = field.getDeclaringClass(); TypeVariable>[] typeParameters = declaringClass.getTypeParameters(); - Type genericSuperclass = getGenericSuperClass(context); + Type genericSuperclass = getGenericSuperClass(fieldTargetType); ParameterizedType parameterizedGenericSuperType = (ParameterizedType) genericSuperclass; Type[] actualTypeArguments = parameterizedGenericSuperType.getActualTypeArguments(); Type actualTypeArgument = null; @@ -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(); diff --git a/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java b/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java new file mode 100644 index 00000000..c9260f26 --- /dev/null +++ b/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java @@ -0,0 +1,69 @@ +/* + * The MIT License + * + * Copyright (c) 2020, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * 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> { + + 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 { + } + + static class CompositeResource { + private LongResource longResource; + } +} From 831a4fe8d0039dbca050a79366cb52597cf8b347 Mon Sep 17 00:00:00 2001 From: dvgaba Date: Fri, 29 Jul 2022 08:40:10 -0400 Subject: [PATCH 2/3] Update pom.xml --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index a585e38f..79153c9a 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,13 @@ Core developer + + dvgaba + https://github.com/dvgaba + + Core developer + + From 254fa38c92e0c9ce5089882f9d92f582eefae6be Mon Sep 17 00:00:00 2001 From: dvgaba Date: Wed, 3 Aug 2022 19:08:23 -0400 Subject: [PATCH 3/3] Update group id and version --- easy-random-bean-validation/pom.xml | 6 +- easy-random-core/pom.xml | 4 +- easy-random-randomizers/pom.xml | 6 +- pom.xml | 96 +++++++++++++++++++++++++++-- 4 files changed, 100 insertions(+), 12 deletions(-) diff --git a/easy-random-bean-validation/pom.xml b/easy-random-bean-validation/pom.xml index 8a9ba4cb..d7afe689 100644 --- a/easy-random-bean-validation/pom.xml +++ b/easy-random-bean-validation/pom.xml @@ -2,8 +2,8 @@ easy-random - org.jeasy - 5.0.1-SNAPSHOT + io.github.dvgaba + 6.0.1 4.0.0 @@ -50,7 +50,7 @@ - org.jeasy + io.github.dvgaba easy-random-randomizers diff --git a/easy-random-core/pom.xml b/easy-random-core/pom.xml index b1bcdc5d..c41d7812 100644 --- a/easy-random-core/pom.xml +++ b/easy-random-core/pom.xml @@ -2,8 +2,8 @@ easy-random - org.jeasy - 5.0.1-SNAPSHOT + io.github.dvgaba + 6.0.1 4.0.0 diff --git a/easy-random-randomizers/pom.xml b/easy-random-randomizers/pom.xml index 9ec89554..b6a3baf1 100644 --- a/easy-random-randomizers/pom.xml +++ b/easy-random-randomizers/pom.xml @@ -2,8 +2,8 @@ easy-random - org.jeasy - 5.0.1-SNAPSHOT + io.github.dvgaba + 6.0.1 4.0.0 @@ -50,7 +50,7 @@ - org.jeasy + io.github.dvgaba easy-random-core diff --git a/pom.xml b/pom.xml index 79153c9a..4853d17c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,9 +8,9 @@ 9 - org.jeasy + io.github.dvgaba easy-random - 5.0.1-SNAPSHOT + 6.0.1 pom Easy Random @@ -95,12 +95,12 @@ - org.jeasy + io.github.dvgaba easy-random-core ${project.version} - org.jeasy + io.github.dvgaba easy-random-randomizers ${project.version} @@ -158,6 +158,14 @@ + + + github + GitHub Packages + https://maven.pkg.github.com/dvgaba/easy-random + + + @@ -222,5 +230,85 @@ + + + + + ossrh + https://s01.oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + repo1 + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.12 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + + + + + + default + + true + + + + +