Skip to content

Commit

Permalink
Allow registration of a Supplier of Randomizers #28
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSchumacher committed Apr 1, 2016
1 parent df17980 commit e952c53
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.github.benas.randombeans.randomizers.registry.CustomRandomizerRegistry;

import java.util.*;
import java.util.function.Supplier;

import static io.github.benas.randombeans.util.Constants.DEFAULT_SEED;

Expand Down Expand Up @@ -77,6 +78,18 @@ public <T, F, R> EnhancedRandomBuilder randomize(FieldDefinition<T, F> fieldDefi
return this;
}

/**
* Register a {@code Supplier} of custom {@link Randomizer}s for a given field.
*
* @param fieldDefinition definition of the field to randomize
* @param randomizerSupplier the custom Supplier of {@link Randomizer}s to use
* @return a pre configured {@link EnhancedRandomBuilder} instance
*/
public <T, F, R> EnhancedRandomBuilder randomize(FieldDefinition<T, F> fieldDefinition, Supplier<Randomizer<R>> randomizerSupplier) {
customRandomizerRegistry.registerRandomizerSupplier(fieldDefinition.getName(), fieldDefinition.getType(), fieldDefinition.getClazz(), randomizerSupplier);
return this;
}

/**
* Exclude a field from being populated.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
* Registry of user defined randomizers.
Expand All @@ -42,6 +43,7 @@
public class CustomRandomizerRegistry implements RandomizerRegistry {

private Map<FieldDefinition<?, ?>, Randomizer<?>> customRandomizersRegistry = new HashMap<>();
private Map<FieldDefinition<?, ?>, Supplier<?>> customRandomizersSupplierRegistry = new HashMap<>();

/**
* Set the initial seed for all randomizers of the registry
Expand All @@ -55,7 +57,13 @@ public void setSeed(long seed) {

@Override
public Randomizer<?> getRandomizer(Field field) {
return customRandomizersRegistry.get(new FieldDefinition<>(field.getName(), field.getType(), field.getDeclaringClass()));
FieldDefinition<?, ?> fieldDefinition = new FieldDefinition<>(field.getName(), field.getType(), field.getDeclaringClass());
@SuppressWarnings("unchecked")
Supplier<Randomizer<?>> randomizerSupplier = (Supplier<Randomizer<?>>) customRandomizersSupplierRegistry.get(fieldDefinition);
if (randomizerSupplier != null) {
return randomizerSupplier.get();
}
return customRandomizersRegistry.get(fieldDefinition);
}

@Override
Expand All @@ -67,4 +75,8 @@ public <T, F, R> void registerRandomizer(final String fieldName, final Class<F>
customRandomizersRegistry.put(new FieldDefinition<>(fieldName, fieldType, type), randomizer);
}

public <T, F, R> void registerRandomizerSupplier(final String fieldName, final Class<F> fieldType, final Class<T> type, final Supplier<Randomizer<R>> randomizerSupplier) {
customRandomizersSupplierRegistry.put(new FieldDefinition<>(fieldName, fieldType, type), randomizerSupplier);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import io.github.benas.randombeans.api.ObjectGenerationException;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.beans.*;
import io.github.benas.randombeans.randomizers.ConstantRandomizer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -36,6 +38,7 @@

import java.util.Date;
import java.util.List;
import java.util.function.Supplier;

import static io.github.benas.randombeans.EnhancedRandomBuilder.aNewEnhancedRandomBuilder;
import static io.github.benas.randombeans.FieldDefinitionBuilder.field;
Expand Down Expand Up @@ -132,6 +135,49 @@ public void generatedBeansWithCustomRandomizersShouldBeCorrectlyPopulated() {
assertThat(person.getName()).isEqualTo(NAME);
}

@Test
public void generatedBeansWithCustomRandomizerSupplierShouldBeCorrectlyPopulated() {
FieldDefinition<?, ?> nameFieldDefinition = field().named("name").ofType(String.class).inClass(Human.class).get();
enhancedRandom = aNewEnhancedRandomBuilder().randomize(nameFieldDefinition, new Supplier<Randomizer<String>>() {
private boolean firstCall = true;

public Randomizer<String> get() {
if (firstCall) {
firstCall = false;
return new ConstantRandomizer<String>(NAME);
}
return new ConstantRandomizer<String>(NAME + NAME);
}
}).build();

Human firstHuman = enhancedRandom.nextObject(Human.class);

assertThat(firstHuman).isNotNull();
assertThat(firstHuman.getName()).isEqualTo(NAME);

Person secondHuman = enhancedRandom.nextObject(Person.class);

assertThat(secondHuman).isNotNull();
assertThat(secondHuman.getName()).isEqualTo(NAME + NAME);
}

@Test
public void customRandomizerSupplierShouldHavePrecedenceOverCustomRandomizer() {
FieldDefinition<?, ?> nameFieldDefinition = field().named("name").ofType(String.class).inClass(Human.class).get();
enhancedRandom = aNewEnhancedRandomBuilder()
.randomize(nameFieldDefinition, new Supplier<Randomizer<String>>() {
public Randomizer<String> get() {
return new ConstantRandomizer<String>(NAME);
}
})
.randomize(nameFieldDefinition, new ConstantRandomizer<String>("abc")).build();

Human firstHuman = enhancedRandom.nextObject(Human.class);

assertThat(firstHuman).isNotNull();
assertThat(firstHuman.getName()).isEqualTo(NAME);
}

@Test
public void javaNetTypesShouldBePopulated() {

Expand Down

0 comments on commit e952c53

Please sign in to comment.