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

Master #4

Merged
merged 4 commits into from
Aug 3, 2022
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
6 changes: 3 additions & 3 deletions easy-random-bean-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>5.0.1-SNAPSHOT</version>
<groupId>io.github.dvgaba</groupId>
<version>6.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -50,7 +50,7 @@

<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<groupId>io.github.dvgaba</groupId>
<artifactId>easy-random-randomizers</artifactId>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions easy-random-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>5.0.1-SNAPSHOT</version>
<groupId>io.github.dvgaba</groupId>
<version>6.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
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 (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<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;
}
}
6 changes: 3 additions & 3 deletions easy-random-randomizers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easy-random</artifactId>
<groupId>org.jeasy</groupId>
<version>5.0.1-SNAPSHOT</version>
<groupId>io.github.dvgaba</groupId>
<version>6.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -50,7 +50,7 @@

<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<groupId>io.github.dvgaba</groupId>
<artifactId>easy-random-core</artifactId>
</dependency>
<dependency>
Expand Down
103 changes: 99 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<version>9</version>
</parent>

<groupId>org.jeasy</groupId>
<groupId>io.github.dvgaba</groupId>
<artifactId>easy-random</artifactId>
<version>5.0.1-SNAPSHOT</version>
<version>6.0.1</version>
<packaging>pom</packaging>

<name>Easy Random</name>
Expand Down Expand Up @@ -83,17 +83,24 @@
<role>Core developer</role>
</roles>
</developer>
<developer>
<id>dvgaba</id>
<url>https://github.com/dvgaba</url>
<roles>
<role>Core developer</role>
</roles>
</developer>
</developers>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<groupId>io.github.dvgaba</groupId>
<artifactId>easy-random-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<groupId>io.github.dvgaba</groupId>
<artifactId>easy-random-randomizers</artifactId>
<version>${project.version}</version>
</dependency>
Expand Down Expand Up @@ -151,6 +158,14 @@
</dependencies>
</dependencyManagement>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/dvgaba/easy-random</url>
</repository>
</distributionManagement>

<build>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -215,5 +230,85 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<id>repo1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.12</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</profile>

<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
</build>

</profile>
</profiles>
</project>