Skip to content

Commit

Permalink
Add ContextAwareRandomizer interface
Browse files Browse the repository at this point in the history
Resolves issue #6
  • Loading branch information
fmbenhassine committed Feb 27, 2019
1 parent e982b0f commit 7d31889
Show file tree
Hide file tree
Showing 17 changed files with 695 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.benas.randombeans;

import io.github.benas.randombeans.api.ContextAwareRandomizer;
import io.github.benas.randombeans.api.Randomizer;

import java.lang.reflect.Array;
Expand All @@ -49,7 +50,7 @@ class ArrayPopulator {
<T> Object getRandomArray(final Class<?> fieldType, final RandomizationContext context) {
Class<?> componentType = fieldType.getComponentType();
if (componentType.isPrimitive()) {
return getRandomPrimitiveArray(componentType);
return getRandomPrimitiveArray(componentType, context);
}
int randomSize = enhancedRandom.getRandomCollectionSize();
T[] itemsList = (T[]) Array.newInstance(componentType, randomSize);
Expand All @@ -59,9 +60,12 @@ <T> Object getRandomArray(final Class<?> fieldType, final RandomizationContext c
return itemsList;
}

Object getRandomPrimitiveArray(final Class<?> primitiveType) {
Object getRandomPrimitiveArray(final Class<?> primitiveType, RandomizationContext context) {
final int randomSize = abs((byte) enhancedRandom.nextInt());
final Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(primitiveType);
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
}
final Object result = Array.newInstance(primitiveType, randomSize);
for (int index = 0; index < randomSize; index++) {
Array.set(result, index, randomizer.getRandomValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EnhancedRandomImpl extends EnhancedRandom {

@Override
public <T> T nextObject(final Class<T> type, final String... excludedFields) {
return doPopulateBean(type, new RandomizationContext(parameters, excludedFields));
return doPopulateBean(type, new RandomizationContext(type, parameters, excludedFields));
}

@Override
Expand All @@ -88,11 +88,14 @@ public <T> Stream<T> objects(final Class<T> type, final int streamSize, final St
}

<T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {
T result;
T result = null;
try {

Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(type);
if (randomizer != null) {
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
}
return (T) randomizer.getRandomValue();
}

Expand All @@ -108,6 +111,7 @@ <T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {

// create a new instance of the target type
result = objectFactory.createInstance(type);
context.setRandomizedObject(result);

// cache instance in the population context
context.addPopulatedBean(type, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.benas.randombeans;

import io.github.benas.randombeans.api.ContextAwareRandomizer;
import io.github.benas.randombeans.api.ObjectGenerationException;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.randomizers.misc.SkipRandomizer;
Expand Down Expand Up @@ -71,6 +72,9 @@ void populateField(final Object target, final Field field, final RandomizationCo
if (randomizer instanceof SkipRandomizer) {
return;
}
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if(!context.hasExceededRandomizationDepth()) {
Object value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.api.EnhancedRandomParameters;
import io.github.benas.randombeans.api.RandomizerContext;

import java.lang.reflect.Field;
import java.util.*;
Expand All @@ -37,7 +38,7 @@
*
* @author Rémi Alvergnat (toilal.dev@gmail.com)
*/
class RandomizationContext {
class RandomizationContext implements RandomizerContext {

private final EnhancedRandomParameters parameters;

Expand All @@ -47,7 +48,13 @@ class RandomizationContext {

private final Stack<RandomizationContextStackItem> stack;

RandomizationContext(final EnhancedRandomParameters parameters, final String... excludedFields) {
private final Class<?> type;

private Object rootObject;
private Object randomizedObject;

RandomizationContext(final Class<?> type, final EnhancedRandomParameters parameters, final String... excludedFields) {
this.type = type;
populatedBeans = new IdentityHashMap<>();
stack = new Stack<>();
this.parameters = parameters;
Expand Down Expand Up @@ -76,7 +83,8 @@ boolean hasAlreadyRandomizedType(final Class<?> type) {
return populatedBeans.containsKey(type) && populatedBeans.get(type).size() == parameters.getObjectPoolSize();
}

Set<String> getExcludedFields() {
@Override
public Set<String> getExcludedFields() {
return excludedFields;
}

Expand Down Expand Up @@ -110,4 +118,41 @@ private List<String> toLowerCase(final List<String> strings) {
private int nextInt(int startInclusive, int endExclusive) {
return startInclusive + new Random().nextInt(endExclusive - startInclusive);
}

void setRandomizedObject(Object randomizedObject) {
if (this.rootObject == null) {
this.rootObject = randomizedObject;
}
this.randomizedObject = randomizedObject;
}

@Override
public Class<?> getType() {
return type;
}

@Override
public Object getCurrentObject() {
return randomizedObject;
}

@Override
public String getCurrentField() {
return String.join(".", getStackedFieldNames());
}

@Override
public int getRandomizationDepth() {
return stack.size();
}

@Override
public Object getRootObject() {
return this.rootObject;
}

@Override
public EnhancedRandomParameters getParameters() {
return parameters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.api;

/**
* 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
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public interface ContextAwareRandomizer<T> extends Randomizer<T> {

void setRandomizerContext(RandomizerContext context);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.api;

import java.util.Set;

/**
* A context object for a {@link Randomizer}.
* This interface provides information about the randomization context.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public interface RandomizerContext {

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

/**
* Get the set of excluded fields (varargs of {@link EnhancedRandom#nextObject(Class, String...)}).
* @return set of excluded fields
*/
Set<String> getExcludedFields();

/**
* Return the root object being randomized (instance of {@link RandomizerContext#getType()}.
* @return root object being randomized
*/
Object getRootObject();

/**
* Return the currently randomized object in the object graph.
* @return currently randomized object
*/
Object getCurrentObject();

/**
* Return the full path to the current field being randomized (starting from the first field in the root type).
* @return full path to the current field being randomized
*/
String getCurrentField();

/**
* Get the current level in the hierarchy of the object graph.
* @return current level in the hierarchy of the object graph.
*/
int getRandomizationDepth();

/**
* Return the currently used parameters by the enclosing {@link EnhancedRandom}.
* @return currently used parameters
*/
EnhancedRandomParameters getParameters();

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void getRandomPrimitiveArray() {
when(randomizerProvider.getRandomizerByType(Integer.TYPE)).thenReturn(integerRandomizer);
when(integerRandomizer.getRandomValue()).thenReturn(INT);

int[] ints = (int[]) arrayPopulator.getRandomPrimitiveArray(Integer.TYPE);
int[] ints = (int[]) arrayPopulator.getRandomPrimitiveArray(Integer.TYPE, context);

assertThat(ints).containsOnly(INT);
}
Expand Down
Loading

0 comments on commit 7d31889

Please sign in to comment.