Skip to content

Commit

Permalink
Attempt to make the randomization context generic
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Feb 12, 2019
1 parent 58d27f2 commit 613659d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Object getRandomPrimitiveArray(final Class<?> primitiveType, RandomizationContex
final int randomSize = abs((byte) enhancedRandom.nextInt());
final Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(primitiveType);
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
((ContextAwareRandomizer<?, ?>) randomizer).setRandomizerContext(context);
}
final Object result = Array.newInstance(primitiveType, randomSize);
for (int index = 0; index < randomSize; index++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ <T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {
Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(type);
if (randomizer != null) {
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
((ContextAwareRandomizer<?, ?>) randomizer).setRandomizerContext(context);
}
return (T) randomizer.getRandomValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void populateField(final Object target, final Field field, final RandomizationCo
return;
}
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
((ContextAwareRandomizer<?, ?>) randomizer).setRandomizerContext(context);
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if(!context.hasExceededRandomizationDepth()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
/**
* Interface for a {@link Randomizer} that is aware of the {@link RandomizerContext randomization context} it is invoked in.
*
* @param <T> the type generated by the randomizer
* @param <RandomizedType> the type generated by the randomizer
* @param <ContextType> the type of object in the current {@link RandomizerContext}.
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public interface ContextAwareRandomizer<T> extends Randomizer<T> {
public interface ContextAwareRandomizer<ContextType, RandomizedType> extends Randomizer<RandomizedType> {

void setRandomizerContext(RandomizerContext context);

}
void setRandomizerContext(RandomizerContext<ContextType> context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public interface RandomizerContext {
public interface RandomizerContext<T> {

/**
* Return the target type (first parameter of {@link EnhancedRandom#nextObject(Class, String...)}).
* @return target type
*/
Class<?> getRandomizedType();
Class<? extends T> getRandomizedType();

/**
* Get the set of excluded fields (varargs of {@link EnhancedRandom#nextObject(Class, String...)}).
Expand All @@ -49,7 +49,7 @@ public interface RandomizerContext {
* Return the currently randomized object (instance of the {@link RandomizerContext#getRandomizedType()}).
* @return currently randomized object
*/
Object getRandomizedObject();
T getRandomizedObject();

/**
* Return the currently used parameters by the enclosing {@link EnhancedRandom}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,43 @@ void testContextAwareRandomization() {
}
assertThat(person.getNickname()).isNull();
}

@Test
void testContextAwareRandomizerWithMultipleTypes() {
// given
String[] names = {"james", "daniel"};
EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
.randomize(field().named("firstName").ofType(String.class).get(), new FirstNameRandomizer(names))
.randomize(field().named("lastName").ofType(String.class).get(), new LastNameRandomizer())
.build();

// when
Person person = enhancedRandom.nextObject(Person.class, "nickname");

// then
String firstName = person.getFirstName();
String lastName = person.getLastName();
assertThat(firstName).isIn(names);
assertThat(lastName).isNotNull();
if (firstName.equalsIgnoreCase("james")) {
assertThat(lastName.equalsIgnoreCase("bond"));
}
if (firstName.equalsIgnoreCase("daniel")) {
assertThat(lastName.equalsIgnoreCase("craig"));
}
assertThat(person.getNickname()).isNull();

Foo foo = person.getFoo();
firstName = foo.getFirstName();
lastName = foo.getLastName();
assertThat(firstName).isIn(names);
assertThat(lastName).isNotNull();
if (firstName.equalsIgnoreCase("james")) {
assertThat(lastName.equalsIgnoreCase("bond"));
}
if (firstName.equalsIgnoreCase("daniel")) {
assertThat(lastName.equalsIgnoreCase("craig"));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* The MIT License
*
* Copyright (c) 2019, 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 io.github.benas.randombeans.context;

import lombok.Data;

@Data
public class Foo {
private String firstName;
private String lastName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,26 @@

/**
* A last name randomizer that depends on the first name of the currently randomized object.
* The currently randomized object can be retreived from the randomization context.
* The currently randomized object can be retrieved from the randomization context.
*/
public class LastNameRandomizer implements ContextAwareRandomizer<String> {
public class LastNameRandomizer implements ContextAwareRandomizer<Person, String> {

private RandomizerContext context;
private RandomizerContext<Person> context;

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

@Override
public String getRandomValue() {
if (context.getRandomizedType().equals(Person.class)) {
Person randomizedObject = (Person) context.getRandomizedObject();
String firstName = randomizedObject.getFirstName();
if (firstName != null && firstName.equalsIgnoreCase("james")) {
return "bond";
}
if (firstName != null && firstName.equalsIgnoreCase("daniel")) {
return "craig";
}
Person randomizedObject = context.getRandomizedObject();
String firstName = randomizedObject.getFirstName();
if (firstName != null && firstName.equalsIgnoreCase("james")) {
return "bond";
}
if (firstName != null && firstName.equalsIgnoreCase("daniel")) {
return "craig";
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public class Person {
private String firstName;
private String lastName;
private String nickname;

private Foo foo;
}

0 comments on commit 613659d

Please sign in to comment.