Skip to content

Commit

Permalink
Fix RandomizationContext#getCurrentObject using the stack
Browse files Browse the repository at this point in the history
The credit goes to https://github.com/nrenzoni for the fix!

Resolves #356
  • Loading branch information
fmbenhassine committed Nov 3, 2019
1 parent c97c354 commit 359d18c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ If you have any question, suggestion, or feedback, do not hesitate to use the [G
* [Lucas Andersson](https://github.com/LucasAndersson)
* [Michael Düsterhus](https://github.com/reitzmichnicht)
* [Nikola Milivojevic](https://github.com/dziga)
* [nrenzoni](https://github.com/nrenzoni)
* [Oleksandr Shcherbyna](https://github.com/sansherbina)
* [Petromir Dzhunev](https://github.com/petromir)
* [Rebecca McQuary](https://github.com/rmcquary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class RandomizationContext implements RandomizerContext {
private final Class<?> type;

private Object rootObject;
private Object randomizedObject;

RandomizationContext(final Class<?> type, final EasyRandomParameters parameters) {
this.type = type;
Expand Down Expand Up @@ -113,7 +112,6 @@ void setRandomizedObject(Object randomizedObject) {
if (this.rootObject == null) {
this.rootObject = randomizedObject;
}
this.randomizedObject = randomizedObject;
}

@Override
Expand All @@ -123,7 +121,12 @@ public Class<?> getTargetType() {

@Override
public Object getCurrentObject() {
return randomizedObject;
if (stack.empty()) {
return rootObject;
}
else {
return stack.lastElement().getObject();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
*/
package org.jeasy.random;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jeasy.random.FieldPredicates.named;
import static org.mockito.Mockito.when;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import org.jeasy.random.api.ContextAwareRandomizer;
Expand Down Expand Up @@ -165,6 +170,7 @@ void testRandomizerContext() {
MyRandomizer randomizer = new MyRandomizer();
EasyRandomParameters parameters = new EasyRandomParameters()
.randomize(D.class, randomizer)
.randomize(FieldPredicates.isAnnotatedWith(ExampleAnnotation.class), new ERandomizer())
.excludeField(named("excluded"));
EasyRandom easyRandom = new EasyRandom(parameters);

Expand All @@ -176,8 +182,10 @@ void testRandomizerContext() {
assertThat(a.excluded).isNull();
assertThat(a.b).isNotNull();
assertThat(a.b.c).isNotNull();
assertThat(a.b.e).isNotNull();
assertThat(a.b.c.d).isNotNull();
assertThat(a.b.c.d.name).isEqualTo("foo");
assertThat(a.b.e.name).isEqualTo("bar");
}

static class MyRandomizer implements ContextAwareRandomizer<D> {
Expand All @@ -204,6 +212,37 @@ public D getRandomValue() {
}
}

static class ERandomizer implements ContextAwareRandomizer<E> {

private RandomizerContext context;

@Override
public void setRandomizerContext(RandomizerContext context) {
this.context = context;
}

@Override
public E getRandomValue() {
// At this level, the context should be as follows:
assertThat(context.getCurrentField()).isEqualTo("b.e");
assertThat(context.getCurrentRandomizationDepth()).isEqualTo(2);
assertThat(context.getTargetType()).isEqualTo(A.class);
assertThat(context.getRootObject()).isInstanceOf(A.class);
assertThat(context.getCurrentObject()).isInstanceOf(B.class);

E e = new E();
String currentField = context.getCurrentField();
Object currentObject = context.getCurrentObject();
try {
String substring = currentField.substring(currentField.lastIndexOf(".") + 1);
e.name = currentObject.getClass().getDeclaredField(substring).getAnnotation(ExampleAnnotation.class).value();
} catch (NoSuchFieldException ex) {
e.name = "default";
}
return e;
}
}

@Data
static class A {
private B b;
Expand All @@ -213,6 +252,9 @@ static class A {
@Data
static class B {
private C c;

@ExampleAnnotation("bar")
private E e;
}

@Data
Expand All @@ -225,4 +267,16 @@ static class D {
private String name;
}

}
@Data
static class E {
private String name;
}

@Target({FIELD})
@Retention(RUNTIME)
@Documented
public @interface ExampleAnnotation {
String value();
}

}

0 comments on commit 359d18c

Please sign in to comment.