From 2ed1eac4daa9c7d736cac7bd632a51bac5c88c19 Mon Sep 17 00:00:00 2001 From: "carlos.borja" Date: Mon, 15 May 2023 16:44:32 +0200 Subject: [PATCH 1/2] Fix record list generation. Moved record factory declaration to class level --- .../java/org/jeasy/random/EasyRandom.java | 5 +- .../java/org/jeasy/random/RecordFactory.java | 18 ++--- .../java/org/jeasy/random/EasyRandomTest.java | 73 +++++++++++-------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java b/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java index e92e74c3..c3a887fe 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java +++ b/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java @@ -55,6 +55,8 @@ public class EasyRandom extends Random { private final ExclusionPolicy exclusionPolicy; + private final RecordFactory recordFactory; + /** * Create a new {@link EasyRandom} instance with default parameters. */ @@ -92,6 +94,7 @@ public EasyRandom(final EasyRandomParameters easyRandomParameters) { ); exclusionPolicy = easyRandomParameters.getExclusionPolicy(); parameters = easyRandomParameters; + recordFactory=new RecordFactory(); } /** @@ -143,7 +146,7 @@ T doPopulateBean(final Class type, final RandomizationContext context) { } if (isRecord(type)) { - return new RecordFactory(context).createInstance(type, context); + return recordFactory.createInstance(type, context); } // Collection types are randomized without introspection for internal fields diff --git a/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java b/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java index 877f1adc..15342458 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java +++ b/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java @@ -23,15 +23,13 @@ */ package org.jeasy.random; -import static org.jeasy.random.util.ReflectionUtils.isArrayType; -import static org.jeasy.random.util.ReflectionUtils.isCollectionType; -import static org.jeasy.random.util.ReflectionUtils.isMapType; -import static org.jeasy.random.util.ReflectionUtils.isOptionalType; +import org.jeasy.random.api.RandomizerContext; import java.lang.reflect.Constructor; import java.lang.reflect.RecordComponent; import java.lang.reflect.Type; -import org.jeasy.random.api.RandomizerContext; + +import static org.jeasy.random.util.ReflectionUtils.*; /** * What is the justification for extending ObjenesisObjectFactory? @@ -43,18 +41,14 @@ public class RecordFactory extends ObjenesisObjectFactory { private EasyRandom easyRandom; - private final RandomizationContext contextImpl; - - public RecordFactory(RandomizationContext contextImpl) { - this.contextImpl = contextImpl; - } @Override public T createInstance(Class type, RandomizerContext context) { if (easyRandom == null) { - easyRandom = new EasyRandom(contextImpl.getParameters()); + easyRandom = new EasyRandom(context.getParameters()); } - return createRandomRecord(type, contextImpl); + + return createRandomRecord(type, (RandomizationContext) context); } private T createRandomRecord(Class recordType, RandomizationContext context) { diff --git a/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java b/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java index c230a845..5b3019bb 100644 --- a/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java +++ b/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java @@ -23,18 +23,6 @@ */ package org.jeasy.random; -import static java.sql.Timestamp.valueOf; -import static java.time.LocalDateTime.of; -import static java.util.Arrays.asList; -import static org.assertj.core.api.Assertions.*; -import static org.assertj.core.api.BDDAssertions.then; -import static org.jeasy.random.FieldPredicates.*; -import static org.mockito.Mockito.when; - -import java.lang.reflect.Modifier; -import java.util.HashSet; -import java.util.List; -import java.util.stream.Stream; import org.jeasy.random.api.Randomizer; import org.jeasy.random.beans.*; import org.jeasy.random.util.ReflectionUtils; @@ -45,6 +33,19 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.lang.reflect.Modifier; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Stream; + +import static java.sql.Timestamp.valueOf; +import static java.time.LocalDateTime.of; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.BDDAssertions.then; +import static org.jeasy.random.FieldPredicates.*; +import static org.mockito.Mockito.when; + @ExtendWith(MockitoExtension.class) class EasyRandomTest { @@ -76,18 +77,18 @@ void shouldFailIfSetterInvocationFails() { Throwable thrown = catchThrowable(() -> easyRandom.nextObject(Salary.class)); assertThat(thrown) - .isInstanceOf(ObjectCreationException.class) - .hasMessageContaining("Unable to create a random instance of type class org.jeasy.random.beans.Salary"); + .isInstanceOf(ObjectCreationException.class) + .hasMessageContaining("Unable to create a random instance of type class org.jeasy.random.beans.Salary"); Throwable cause = thrown.getCause(); assertThat(cause) - .isInstanceOf(ObjectCreationException.class) - .hasMessageContaining("Unable to invoke setter for field amount of class org.jeasy.random.beans.Salary"); + .isInstanceOf(ObjectCreationException.class) + .hasMessageContaining("Unable to invoke setter for field amount of class org.jeasy.random.beans.Salary"); Throwable rootCause = cause.getCause(); assertThat(rootCause) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Amount must be positive"); + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Amount must be positive"); } @Test @@ -126,7 +127,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateObjects() { when(randomizer.getRandomValue()).thenReturn(FOO); EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(named("name").and(ofType(String.class)).and(inClass(Human.class)), randomizer); + .randomize(named("name").and(ofType(String.class)).and(inClass(Human.class)), randomizer); easyRandom = new EasyRandom(parameters); Person person = easyRandom.nextObject(Person.class); @@ -141,7 +142,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateFieldsWithOneModifier() { // Given EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(hasModifiers(Modifier.TRANSIENT).and(ofType(String.class)), randomizer); + .randomize(hasModifiers(Modifier.TRANSIENT).and(ofType(String.class)), randomizer); easyRandom = new EasyRandom(parameters); // When @@ -158,7 +159,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateFieldsWithMultipleModifier() when(randomizer.getRandomValue()).thenReturn(FOO); int modifiers = Modifier.TRANSIENT | Modifier.PROTECTED; EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(hasModifiers(modifiers).and(ofType(String.class)), randomizer); + .randomize(hasModifiers(modifiers).and(ofType(String.class)), randomizer); easyRandom = new EasyRandom(parameters); // When @@ -279,7 +280,7 @@ void customRandomzierForTypeRecordShouldBeUsedToPopulateObjects() { when(nestedRecordRandomizer.getRandomValue()).thenReturn(TEST_NESTED_RECORD); EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(TestNestedRecord.class, nestedRecordRandomizer); + .randomize(TestNestedRecord.class, nestedRecordRandomizer); easyRandom = new EasyRandom(parameters); try { @@ -290,6 +291,17 @@ void customRandomzierForTypeRecordShouldBeUsedToPopulateObjects() { } } + @Test + void typeRecordListShouldBePopulatedWithDifferentValues() { + List recordList = easyRandom.objects(TestRecord.class, 2).toList(); + + TestRecord rnd1 = recordList.get(0); + TestRecord rnd2 = recordList.get(1); + + assertThat(rnd1).isNotEqualTo(rnd2); + + } + @Test void differentCollectionsShouldBeRandomizedWithDifferentSizes() { // given @@ -329,7 +341,8 @@ class Base { T t; } - class Concrete extends Base {} + class Concrete extends Base { + } // when Concrete concrete = easyRandom.nextObject(Concrete.class); @@ -347,7 +360,8 @@ class Base { T t; S s; } - class Concrete extends Base {} + class Concrete extends Base { + } // when Concrete concrete = easyRandom.nextObject(Concrete.class); @@ -486,13 +500,14 @@ class Base { T t; } - class Concrete extends Base> {} + class Concrete extends Base> { + } assertThatThrownBy(() -> easyRandom.nextObject(Concrete.class) // when - ) - // then - .isInstanceOf(ObjectCreationException.class) - .hasMessage("Unable to create a random instance of type class org.jeasy.random.EasyRandomTest$7Concrete"); + ) + // then + .isInstanceOf(ObjectCreationException.class) + .hasMessage("Unable to create a random instance of type class org.jeasy.random.EasyRandomTest$7Concrete"); } @Test From 7df4e34bc8b4975972148e864780978e25480adc Mon Sep 17 00:00:00 2001 From: dvgaba <3621249+dvgaba@users.noreply.github.com> Date: Mon, 22 May 2023 09:19:00 -0400 Subject: [PATCH 2/2] Applied Spotless --- .../java/org/jeasy/random/EasyRandom.java | 2 +- .../java/org/jeasy/random/RecordFactory.java | 5 +- .../java/org/jeasy/random/EasyRandomTest.java | 63 +++++++++---------- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java b/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java index c3a887fe..378db866 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java +++ b/easy-random-core/src/main/java/org/jeasy/random/EasyRandom.java @@ -94,7 +94,7 @@ public EasyRandom(final EasyRandomParameters easyRandomParameters) { ); exclusionPolicy = easyRandomParameters.getExclusionPolicy(); parameters = easyRandomParameters; - recordFactory=new RecordFactory(); + recordFactory = new RecordFactory(); } /** diff --git a/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java b/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java index 15342458..15726e43 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java +++ b/easy-random-core/src/main/java/org/jeasy/random/RecordFactory.java @@ -23,13 +23,12 @@ */ package org.jeasy.random; -import org.jeasy.random.api.RandomizerContext; +import static org.jeasy.random.util.ReflectionUtils.*; import java.lang.reflect.Constructor; import java.lang.reflect.RecordComponent; import java.lang.reflect.Type; - -import static org.jeasy.random.util.ReflectionUtils.*; +import org.jeasy.random.api.RandomizerContext; /** * What is the justification for extending ObjenesisObjectFactory? diff --git a/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java b/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java index 5b3019bb..24a0c98e 100644 --- a/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java +++ b/easy-random-core/src/test/java/org/jeasy/random/EasyRandomTest.java @@ -23,6 +23,18 @@ */ package org.jeasy.random; +import static java.sql.Timestamp.valueOf; +import static java.time.LocalDateTime.of; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.BDDAssertions.then; +import static org.jeasy.random.FieldPredicates.*; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Modifier; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Stream; import org.jeasy.random.api.Randomizer; import org.jeasy.random.beans.*; import org.jeasy.random.util.ReflectionUtils; @@ -33,19 +45,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.lang.reflect.Modifier; -import java.util.HashSet; -import java.util.List; -import java.util.stream.Stream; - -import static java.sql.Timestamp.valueOf; -import static java.time.LocalDateTime.of; -import static java.util.Arrays.asList; -import static org.assertj.core.api.Assertions.*; -import static org.assertj.core.api.BDDAssertions.then; -import static org.jeasy.random.FieldPredicates.*; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class EasyRandomTest { @@ -77,18 +76,18 @@ void shouldFailIfSetterInvocationFails() { Throwable thrown = catchThrowable(() -> easyRandom.nextObject(Salary.class)); assertThat(thrown) - .isInstanceOf(ObjectCreationException.class) - .hasMessageContaining("Unable to create a random instance of type class org.jeasy.random.beans.Salary"); + .isInstanceOf(ObjectCreationException.class) + .hasMessageContaining("Unable to create a random instance of type class org.jeasy.random.beans.Salary"); Throwable cause = thrown.getCause(); assertThat(cause) - .isInstanceOf(ObjectCreationException.class) - .hasMessageContaining("Unable to invoke setter for field amount of class org.jeasy.random.beans.Salary"); + .isInstanceOf(ObjectCreationException.class) + .hasMessageContaining("Unable to invoke setter for field amount of class org.jeasy.random.beans.Salary"); Throwable rootCause = cause.getCause(); assertThat(rootCause) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Amount must be positive"); + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Amount must be positive"); } @Test @@ -127,7 +126,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateObjects() { when(randomizer.getRandomValue()).thenReturn(FOO); EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(named("name").and(ofType(String.class)).and(inClass(Human.class)), randomizer); + .randomize(named("name").and(ofType(String.class)).and(inClass(Human.class)), randomizer); easyRandom = new EasyRandom(parameters); Person person = easyRandom.nextObject(Person.class); @@ -142,7 +141,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateFieldsWithOneModifier() { // Given EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(hasModifiers(Modifier.TRANSIENT).and(ofType(String.class)), randomizer); + .randomize(hasModifiers(Modifier.TRANSIENT).and(ofType(String.class)), randomizer); easyRandom = new EasyRandom(parameters); // When @@ -159,7 +158,7 @@ void customRandomzierForFieldsShouldBeUsedToPopulateFieldsWithMultipleModifier() when(randomizer.getRandomValue()).thenReturn(FOO); int modifiers = Modifier.TRANSIENT | Modifier.PROTECTED; EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(hasModifiers(modifiers).and(ofType(String.class)), randomizer); + .randomize(hasModifiers(modifiers).and(ofType(String.class)), randomizer); easyRandom = new EasyRandom(parameters); // When @@ -280,7 +279,7 @@ void customRandomzierForTypeRecordShouldBeUsedToPopulateObjects() { when(nestedRecordRandomizer.getRandomValue()).thenReturn(TEST_NESTED_RECORD); EasyRandomParameters parameters = new EasyRandomParameters() - .randomize(TestNestedRecord.class, nestedRecordRandomizer); + .randomize(TestNestedRecord.class, nestedRecordRandomizer); easyRandom = new EasyRandom(parameters); try { @@ -299,7 +298,6 @@ void typeRecordListShouldBePopulatedWithDifferentValues() { TestRecord rnd2 = recordList.get(1); assertThat(rnd1).isNotEqualTo(rnd2); - } @Test @@ -341,8 +339,7 @@ class Base { T t; } - class Concrete extends Base { - } + class Concrete extends Base {} // when Concrete concrete = easyRandom.nextObject(Concrete.class); @@ -360,8 +357,7 @@ class Base { T t; S s; } - class Concrete extends Base { - } + class Concrete extends Base {} // when Concrete concrete = easyRandom.nextObject(Concrete.class); @@ -500,14 +496,13 @@ class Base { T t; } - class Concrete extends Base> { - } + class Concrete extends Base> {} assertThatThrownBy(() -> easyRandom.nextObject(Concrete.class) // when - ) - // then - .isInstanceOf(ObjectCreationException.class) - .hasMessage("Unable to create a random instance of type class org.jeasy.random.EasyRandomTest$7Concrete"); + ) + // then + .isInstanceOf(ObjectCreationException.class) + .hasMessage("Unable to create a random instance of type class org.jeasy.random.EasyRandomTest$7Concrete"); } @Test