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

Pass properly updated context to a ContextAwareRandomizer #432

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ void populateField(final Object target, final Field field, final RandomizationCo
if (randomizer instanceof SkipRandomizer) {
return;
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if(!context.hasExceededRandomizationDepth()) {
Object value;
if (randomizer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
Expand All @@ -35,6 +36,7 @@

import javax.xml.bind.JAXBElement;

import org.jeasy.random.api.ContextAwareRandomizer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -64,6 +66,8 @@ class FieldPopulatorTest {
@Mock
private Randomizer randomizer;
@Mock
private ContextAwareRandomizer contextAwareRandomizer;
@Mock
private ArrayPopulator arrayPopulator;
@Mock
private CollectionPopulator collectionPopulator;
Expand Down Expand Up @@ -109,6 +113,27 @@ void whenCustomRandomizerIsRegisteredForTheField_thenTheFieldShouldBePopulatedWi
assertThat(human.getName()).isEqualTo(NAME);
}

@Test
void whenContextAwareRandomizerIsRegisteredForTheField_thenTheFieldShouldBeOnTopOfTheSuppliedContextStack() throws Exception {
// Given
Field name = Human.class.getDeclaredField("name");
Human human = new Human();
RandomizationContext context = new RandomizationContext(Human.class, new EasyRandomParameters());
final Human[] currentObjectFromContext = new Human[1];
when(randomizerProvider.getRandomizerByField(name, context)).thenReturn(contextAwareRandomizer);
when(contextAwareRandomizer.getRandomValue()).thenReturn(NAME);
doAnswer(invocationOnMock -> {
currentObjectFromContext[0] = (Human)invocationOnMock.getArgument(0, RandomizationContext.class).getCurrentObject();
return null;
}).when(contextAwareRandomizer).setRandomizerContext(context);

// When
fieldPopulator.populateField(human, name, context);

// Then
assertThat(currentObjectFromContext[0]).isEqualTo(human);
}

@Test
void whenTheFieldIsOfTypeArray_thenShouldDelegatePopulationToArrayPopulator() throws Exception {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public CityRandomizer(final long seed, final Locale locale) {
* Create a new {@link CityRandomizer}.
*
* @return a new {@link CityRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static CityRandomizer aNewCityRandomizer() {
return new CityRandomizer();
}
Expand All @@ -73,7 +75,9 @@ public static CityRandomizer aNewCityRandomizer() {
*
* @param seed the initial seed
* @return a new {@link CityRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static CityRandomizer aNewCityRandomizer(final long seed) {
return new CityRandomizer(seed);
}
Expand All @@ -84,7 +88,9 @@ public static CityRandomizer aNewCityRandomizer(final long seed) {
* @param seed the initial seed
* @param locale the locale to use
* @return a new {@link CityRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static CityRandomizer aNewCityRandomizer(final long seed, final Locale locale) {
return new CityRandomizer(seed, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public IsbnRandomizer(final long seed, final Locale locale) {
* Create a new {@link IsbnRandomizer}.
*
* @return a new {@link IsbnRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static IsbnRandomizer aNewIsbnRandomizer() {
return new IsbnRandomizer();
}
Expand All @@ -73,7 +75,9 @@ public static IsbnRandomizer aNewIsbnRandomizer() {
*
* @param seed the initial seed
* @return a new {@link IsbnRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static IsbnRandomizer aNewIsbnRandomizer(final long seed) {
return new IsbnRandomizer(seed);
}
Expand All @@ -84,7 +88,9 @@ public static IsbnRandomizer aNewIsbnRandomizer(final long seed) {
* @param seed the initial seed
* @param locale the locale to use
* @return a new {@link IsbnRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static IsbnRandomizer aNewIsbnRandomizer(final long seed, final Locale locale) {
return new IsbnRandomizer(seed, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public LastNameRandomizer(final long seed, final Locale locale) {
* Create a new {@link LastNameRandomizer}.
*
* @return a new {@link LastNameRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static LastNameRandomizer aNewLastNameRandomizer() {
return new LastNameRandomizer();
}
Expand All @@ -73,7 +75,9 @@ public static LastNameRandomizer aNewLastNameRandomizer() {
*
* @param seed the initial seed
* @return a new {@link LastNameRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static LastNameRandomizer aNewLastNameRandomizer(final long seed) {
return new LastNameRandomizer(seed);
}
Expand All @@ -84,7 +88,9 @@ public static LastNameRandomizer aNewLastNameRandomizer(final long seed) {
* @param seed the initial seed
* @param locale the locale to use
* @return a new {@link LastNameRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static LastNameRandomizer aNewLastNameRandomizer(final long seed, final Locale locale) {
return new LastNameRandomizer(seed, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public static MacAddressRandomizer aNewMacAddressRandomizer() {
* @param seed
* the initial seed
* @return a new {@link MacAddressRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static MacAddressRandomizer aNewMacAddressRandomizer(final long seed) {
return new MacAddressRandomizer(seed);
}
Expand All @@ -90,7 +92,9 @@ public static MacAddressRandomizer aNewMacAddressRandomizer(final long seed) {
* @param locale
* the locale to use
* @return a new {@link MacAddressRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static MacAddressRandomizer aNewMacAddressRandomizer(final long seed, final Locale locale) {
return new MacAddressRandomizer(seed, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public WordRandomizer(final long seed, final Locale locale) {
* Create a new {@link WordRandomizer}.
*
* @return a new {@link WordRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static WordRandomizer aNewWordRandomizer() {
return new WordRandomizer();
}
Expand All @@ -73,7 +75,9 @@ public static WordRandomizer aNewWordRandomizer() {
*
* @param seed the initial seed
* @return a new {@link WordRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static WordRandomizer aNewWordRandomizer(final long seed) {
return new WordRandomizer(seed);
}
Expand All @@ -84,7 +88,9 @@ public static WordRandomizer aNewWordRandomizer(final long seed) {
* @param seed the initial seed
* @param locale the locale to use
* @return a new {@link WordRandomizer}
* @deprecated in favor of the equivalent constructor
*/
@Deprecated
public static WordRandomizer aNewWordRandomizer(final long seed, final Locale locale) {
return new WordRandomizer(seed, locale);
}
Expand Down