Skip to content

Commit

Permalink
issue-299: split argument value when argument type is array
Browse files Browse the repository at this point in the history
Jackson is able to convert an array of String values to an
array of other Java types. However, it does not parse a raw
String and split it if it is intended to be mapped to an array.

This commit prepares the data for Jackson by splitting the
value of `RandomizerArgument` when its type is an array.

NB: The goal is to cover simple use cases of comma separated
values like reported in #299. There is no plan to make this
feature support custom separator, date format, number format, etc.
  • Loading branch information
fmbenhassine committed Jan 15, 2019
1 parent e790939 commit cda1de5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,14 @@ private static Object[] convertArguments(final RandomizerArgument[] declaredArgu
int numberOfArguments = declaredArguments.length;
Object[] arguments = new Object[numberOfArguments];
for (int i = 0; i < numberOfArguments; i++) {
arguments[i] = objectMapper.convertValue(declaredArguments[i].value(), declaredArguments[i].type());
Class<?> type = declaredArguments[i].type();
String argument = declaredArguments[i].value();
Object value = argument;
// issue 299: if argument type is array, split values before conversion
if (type.isArray()) {
value = Stream.of(argument.split(",")).map(String::trim).toArray();
}
arguments[i] = objectMapper.convertValue(value, type);
}
return arguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import io.github.benas.randombeans.annotation.RandomizerArgument;
import io.github.benas.randombeans.api.EnhancedRandom;
import io.github.benas.randombeans.randomizers.AbstractRandomizer;
import lombok.Data;
import org.junit.jupiter.api.Test;

import io.github.benas.randombeans.api.ObjectGenerationException;
Expand All @@ -46,6 +50,59 @@ public void shouldThrowObjectGenerationExceptionWhenRandomizerUsedInRandomizerAn
assertThatThrownBy(() -> aNewEnhancedRandom().nextObject(Bar.class)).isInstanceOf(ObjectGenerationException.class);
}


@Test
void testRandomizerArgumentAsArray() {
Person person = EnhancedRandom.random(Person.class);

assertThat(person.getName()).isIn("foo", "bar");
assertThat(person.getAge()).isIn(1, 2, 3);
}

@Data
static class Person {

@io.github.benas.randombeans.annotation.Randomizer(value = MyStringRandomizer.class, args = {
@RandomizerArgument(value = "foo, bar", type = String[].class)
})
private String name;

@io.github.benas.randombeans.annotation.Randomizer(value = MyNumbersRandomizer.class, args = {
@RandomizerArgument(value = "1, 2, 3", type = Integer[].class)
})
private int age;
}

public static class MyStringRandomizer extends AbstractRandomizer<String> {

private String[] words;

public MyStringRandomizer(String[] words) {
this.words = words;
}

@Override
public String getRandomValue() {
int randomIndex = random.nextInt(words.length);
return words[randomIndex];
}
}

public static class MyNumbersRandomizer extends AbstractRandomizer<Integer> {

private Integer[] numbers;

public MyNumbersRandomizer(Integer[] numbers) {
this.numbers = numbers;
}

@Override
public Integer getRandomValue() {
int randomIndex = random.nextInt(numbers.length);
return numbers[randomIndex];
}
}

private class Bar {
@io.github.benas.randombeans.annotation.Randomizer(RandomizerWithoutDefaultConstrcutor.class)
private String name;
Expand Down

0 comments on commit cda1de5

Please sign in to comment.