Skip to content

Commit

Permalink
Merge pull request #15 from eXparity/issue_13
Browse files Browse the repository at this point in the history
Add support for bean properties with maps containing arrays
  • Loading branch information
stewbis committed Oct 9, 2018
2 parents d466178 + ed98e84 commit cc03f15
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/exparity/stub/bean/BeanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ private <K, V> Map<K, V> createMap(final Class<K> keyType,
for (int i = 0; i < length; ++i) {
K key = populate(createValue(keyType), path.appendIndex(i), stack.append(keyType).append(valueType));
if (key != null) {
map.put(key, createValue(valueType));
map.put(key, RandomBuilder.aRandomInstanceOf(valueType));
}
}
return map;
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/exparity/stub/core/ArrayFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
*/
package org.exparity.stub.core;

import org.exparity.stub.bean.BeanBuilder;

/**
* Interface to be implemented by classes which can provide arrays of values to a {@link BeanBuilder}
*
*
* @author Stewart Bissett
*
* @deprecated No longer supported
*/
@FunctionalInterface
@Deprecated
public interface ArrayFactory<T> {

/**
* Create an array of type T.
* @param type the scalar type the array will be composed of
* @param size the size of the array to create
* @return an array of type T
*/
public T[] createValue(final Class<T> type, final int size);
/**
* Create an array of type T.
* @param type the scalar type the array will be composed of
* @param size the size of the array to create
* @return an array of type T
*/
public T[] createValue(final Class<T> type, final int size);
}
19 changes: 12 additions & 7 deletions src/main/java/org/exparity/stub/core/ValueFactories.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,21 @@ public static <E> ValueFactory<E> aRandomEnum(final Class<E> enumType) {
/**
* Creates an {@link ArrayFactory} which returns a random array of the given type.
* @param typeFactory the {@link ValueFactory} to use to create each instance in the random array
* @param size the size of the array
* @return an {@link ArrayFactory} which returns a random array of the given type.
*/
@SuppressWarnings("unchecked")
public static <A> ArrayFactory<A> aRandomArrayOf(final ValueFactory<A> typeFactory) {
return (type,size) -> {
Object array = Array.newInstance(type, size);
for (int i = 0; i < size; ++i) {
Array.set(array, i, typeFactory.createValue());
}
return (A[]) array;
public static <A> ValueFactory<A[]> aRandomArrayOf(final ValueFactory<A> typeFactory, final int size) {
return () -> {
A instance = typeFactory.createValue();
Object array = Array.newInstance(instance.getClass(), size);
if ( size > 0 ) {
Array.set(array, 0, instance);
for (int i = 1; i < size; ++i) {
Array.set(array, i, typeFactory.createValue());
}
}
return (A[]) array;
};
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/exparity/stub/random/RandomBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ public static <E extends Enum<E>> E[] aRandomArrayOfEnum(final Class<E> enumType
* @return an array of random values from the supplied enumeration.
*/
public static <E extends Enum<E>> E[] aRandomArrayOfEnum(final Class<E> enumType, final int min, final int max) {
return ValueFactories.aRandomArrayOf(ValueFactories.aRandomEnum(enumType)).createValue(enumType,
aRandomInteger(min, max));
return ValueFactories.aRandomArrayOf(ValueFactories.aRandomEnum(enumType), aRandomInteger(min, max))
.createValue();
}

/**
Expand Down Expand Up @@ -574,7 +574,7 @@ public static <T> T[] aRandomArrayOf(final Class<T> type) {
* @return an array of randomly populated instances of the supplied type
*/
public static <T> T[] aRandomArrayOf(final Class<T> type, final int min, final int max) {
return ValueFactories.aRandomArrayOf(instanceFactoryFor(type)).createValue(type, aRandomInteger(min, max));
return ValueFactories.aRandomArrayOf(instanceFactoryFor(type), aRandomInteger(min, max)).createValue();
}

/**
Expand Down Expand Up @@ -937,6 +937,10 @@ private static <T> ValueFactory<T> instanceFactoryFor(final Class<T> type,
return factory;
} else if (type.isEnum()) {
return ValueFactories.aRandomEnum(type);
} else if (type.isArray()) {
Class<?> componentType = type.getComponentType();
ValueFactory<?> componentTypeValueFactory = instanceFactoryFor(componentType, restrictions);
return (ValueFactory<T>) ValueFactories.aRandomArrayOf(componentTypeValueFactory, 1);
} else if (isBean(type)) {
BeanBuilder<T> builder = BeanBuilder.aRandomInstanceOf(type);
for (RandomRestriction restriction : restrictions) {
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/org/exparity/stub/core/ValueFactoriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ public void canCreateANullValue() {

@Test
public void canCreateARandomArrayOf() {
Boolean[] array = aRandomArrayOf(aRandomBoolean()).createValue(Boolean.class, 5);
Boolean[] array = aRandomArrayOf(aRandomBoolean(), 5).createValue();
assertThat(array, arrayWithSize(5));
assertThat(array, hasItemInArray(any(Boolean.class)));
}

@Test
public void canCreateARandomEmptyArrayOf() {
Boolean[] array = aRandomArrayOf(aRandomBoolean(), 0).createValue();
assertThat(array, arrayWithSize(0));
}

@Test
public void canCreateARandomBoolean() {
checkResult(aRandomBoolean(), any(Boolean.class));
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/exparity/stub/random/RandomBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ public void canBuildARandomListOfObjectsOfExactSize() {
assertThat(aRandomListOf(AllTypes.class, 3, 3), hasSize(3));
}

@Test
public void canBuildARandomInstanceOfAnArray() {
assertThat(aRandomInstanceOf(String[].class), any(String[].class));
}

@Test
public void canBuildARandomInstanceOfAnArrayOfObjects() {
assertThat(aRandomInstanceOf(AllTypes[].class), any(AllTypes[].class));
}

@Test
public void canBuildARandomInstanceOf() {
assertThat(aRandomInstanceOf(AllTypes.class), any(AllTypes.class));
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/org/exparity/stub/testutils/type/AllTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static enum EnumValues {
};

private AllTypes.EnumValues enumValue;
private boolean booleanValue;
private Boolean booleanObjectValue;
private String stringValue;
private Integer integerObjectValue;
private int integerValue;
Expand Down Expand Up @@ -46,7 +48,16 @@ public static enum EnumValues {
private List<String> list;
private Set<String> set;
private Map<Long, String> map;
private Map<String, String[]> arrayMap;

public Map<String, String[]> getArrayMap() {
return arrayMap;
}

public void setArrayMap(Map<String, String[]> arrayMap) {
this.arrayMap = arrayMap;
}

public AllTypes.EnumValues getEnumValue() {
return this.enumValue;
}
Expand Down Expand Up @@ -111,9 +122,6 @@ public void setBooleanObjectValue(final Boolean booleanObjectValue) {
this.booleanObjectValue = booleanObjectValue;
}

private boolean booleanValue;
private Boolean booleanObjectValue;

public String getStringValue() {
return this.stringValue;
}
Expand Down Expand Up @@ -290,4 +298,4 @@ public void setZonedlDateTimeValue(final ZonedDateTime zonedlDateTimeValue) {
this.zonedlDateTimeValue = zonedlDateTimeValue;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ public class ConstructorOnlyCollectionTypes {
private final List<String> list;
private final Set<String> set;
private final Map<Long, String> map;
private final Map<String, String[]> arrayMap;

public ConstructorOnlyCollectionTypes(final int[] array,
final String[] stringArray,
final Collection<String> collection,
final List<String> list,
final Set<String> set,
final Map<Long, String> map) {
final Map<Long, String> map,
final Map<String, String[]> arrayMap) {
this.array = array;
this.stringArray = stringArray;
this.collection = collection;
this.list = list;
this.set = set;
this.map = map;
this.arrayMap = arrayMap;
}

public int[] getArray() {
Expand All @@ -51,5 +54,9 @@ public Set<String> getSet() {
public Map<Long, String> getMap() {
return map;
}

public Map<String, String[]> getArrayMap() {
return arrayMap;
}

}
}

0 comments on commit cc03f15

Please sign in to comment.