Skip to content

Commit

Permalink
issue #241 : fix usage of custom randomizers when field type is primi…
Browse files Browse the repository at this point in the history
…tive
  • Loading branch information
fmbenhassine committed Feb 23, 2017
1 parent e1ddb24 commit 75318b0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FieldPopulator {
}

void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException {
Randomizer<?> randomizer = randomizerProvider.getRandomizerByField(field);
Randomizer<?> randomizer = getRandomizer(field);
if (randomizer instanceof SkipRandomizer) {
return;
}
Expand All @@ -90,6 +90,15 @@ void populateField(final Object target, final Field field, final RandomizationCo
context.popStackItem();
}

private Randomizer<?> getRandomizer(Field field) {
// issue 241: if there is no custom randomizer by field, then check by type
Randomizer<?> randomizer = randomizerProvider.getRandomizerByField(field);
if (randomizer == null) {
randomizer = randomizerProvider.getRandomizerByType(field.getType());
}
return randomizer;
}

private Object generateRandomValue(final Field field, final RandomizationContext context) {
Class<?> fieldType = field.getType();
Type fieldGenericType = field.getGenericType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.github.benas.randombeans.api.EnhancedRandomParameters;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.api.RandomizerRegistry;
import io.github.benas.randombeans.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
Expand Down Expand Up @@ -60,12 +61,18 @@ public Randomizer<?> getRandomizer(Field field) {
return customFieldRandomizersRegistry.get(fieldDefinition);
}
}
return customTypeRandomizersRegistry.get(field.getType());
return getRandomizer(field.getType());
}

@Override
public Randomizer<?> getRandomizer(Class<?> type) {
return customTypeRandomizersRegistry.get(type);
// issue 241: primitive type were ignored: try to get randomizer by primtive type, if not, then try by wrapper type
Randomizer<?> randomizer = customTypeRandomizersRegistry.get(type);
if( randomizer == null) {
Class<?> wrapperType = type.isPrimitive() ? ReflectionUtils.getWrapperType(type) : type;
randomizer = customTypeRandomizersRegistry.get(wrapperType);
}
return randomizer;
}

public <T, F, R> void registerRandomizer(final FieldDefinition<T,F> fieldDefinition, final Randomizer<R> randomizer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,41 @@ public static Object getFieldValue(final Object object, final Field field) throw
return value;
}

/**
* Get wrapper type of a primitive type.
*
* @param primitiveType to get its wrapper type
* @return the wrapper type of the given primitive type
*/
public Class<?> getWrapperType(Class<?> primitiveType) {
// FIXME is there a better way to do this?
if (Byte.TYPE.equals(primitiveType)) {
return Byte.class;
}
if (Short.TYPE.equals(primitiveType)) {
return Short.class;
}
if (Integer.TYPE.equals(primitiveType)) {
return Integer.class;
}
if (Long.TYPE.equals(primitiveType)) {
return Long.class;
}
if (Double.TYPE.equals(primitiveType)) {
return Double.class;
}
if (Float.TYPE.equals(primitiveType)) {
return Float.class;
}
if (Boolean.TYPE.equals(primitiveType)) {
return Boolean.class;
}
if (Character.TYPE.equals(primitiveType)) {
return Character.class;
}
return primitiveType; // if not primitive, return it as is
}

/**
* Check if a field has a primitive type and matching default value which is set by the compiler.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ public void shouldAlwaysGenerateTheSameValueForTheSameSeed() {
*/

@Test
public void generatedValueShouldBeWithinSpecifiedRange_whenUsedToRandomizeIntegerType() throws Exception {
public void generatedValueShouldBeWithinSpecifiedRange_whenUsedToRandomizePrimitiveIntegerType() throws Exception {
EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
.randomize(int.class, new IntegerRangeRandomizer(min, max))
.build();

int integer = enhancedRandom.nextObject(int.class);
assertThat(integer).isBetween(min, max);
}

@Test
public void generatedValueShouldBeWithinSpecifiedRange_whenUsedToRandomizeWrapperIntegerType() throws Exception {
EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandomBuilder()
.randomize(Integer.class, new IntegerRangeRandomizer(min, max))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public void testIsJdkBuiltIn() throws Exception {
assertThat(ReflectionUtils.isJdkBuiltIn(CustomList.class)).isFalse();
}

@Test
public void getWrapperTypeTest() throws Exception {
assertThat(ReflectionUtils.getWrapperType(Byte.TYPE)).isEqualTo(Byte.class);
assertThat(ReflectionUtils.getWrapperType(Short.TYPE)).isEqualTo(Short.class);
assertThat(ReflectionUtils.getWrapperType(Integer.TYPE)).isEqualTo(Integer.class);
assertThat(ReflectionUtils.getWrapperType(Long.TYPE)).isEqualTo(Long.class);
assertThat(ReflectionUtils.getWrapperType(Double.TYPE)).isEqualTo(Double.class);
assertThat(ReflectionUtils.getWrapperType(Float.TYPE)).isEqualTo(Float.class);
assertThat(ReflectionUtils.getWrapperType(Boolean.TYPE)).isEqualTo(Boolean.class);
assertThat(ReflectionUtils.getWrapperType(Character.TYPE)).isEqualTo(Character.class);
assertThat(ReflectionUtils.getWrapperType(String.class)).isEqualTo(String.class);
}

@Test
public void testIsPrimitiveFieldWithDefaultValue() throws Exception {
Class<PrimitiveFieldsWithDefaultValuesBean> defaultValueClass = PrimitiveFieldsWithDefaultValuesBean.class;
Expand Down

0 comments on commit 75318b0

Please sign in to comment.