Merge branch 'topicusoverheid-feature/random-collection-element'
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.github.javafaker.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomService {
|
||||
@@ -50,4 +51,37 @@ public class RandomService {
|
||||
public Boolean nextBoolean() {
|
||||
return random.nextBoolean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from an array.
|
||||
*
|
||||
* @param array The array to take a random element fom.
|
||||
* @param <E> The type of the elements in the array.
|
||||
* @return A randomly selected element from the array.
|
||||
*/
|
||||
public <E> E nextElement(E[] array) {
|
||||
return array[this.nextInt(array.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from a list.
|
||||
*
|
||||
* @param list The list to take a random element fom.
|
||||
* @param <E> The type of the elements in the list.
|
||||
* @return A randomly selected element from the list.
|
||||
*/
|
||||
public <E> E nextElement(List<E> list) {
|
||||
return list.get(this.nextInt(list.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random enumeration value
|
||||
*
|
||||
* @param enumeration The enumeration to take a random value from
|
||||
* @param <E> The type of the enumeration
|
||||
* @return A randomly selected enumeration value
|
||||
*/
|
||||
public <E extends Enum<E>> E nextEnumValue(Class<E> enumeration) {
|
||||
return nextElement(enumeration.getEnumConstants());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package com.github.javafaker.service;
|
||||
|
||||
import com.github.javafaker.AbstractFakerTest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.isIn;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@@ -55,4 +56,35 @@ public class RandomServiceTest extends AbstractFakerTest {
|
||||
assertThat(randomService.nextLong(Long.MAX_VALUE), greaterThan(0L));
|
||||
assertThat(randomService.nextLong(Long.MAX_VALUE), lessThan(Long.MAX_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextArrayElement() {
|
||||
Integer[] array = new Integer[] { 1, 2, 3, 5, 8, 13, 21 };
|
||||
|
||||
for (int i = 1; i < 10; i++) {
|
||||
assertThat(randomService.nextElement(array), isIn(array));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextListElement() {
|
||||
List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 5, 8, 13, 21 });
|
||||
|
||||
for (int i = 1; i < 10; i++) {
|
||||
assertThat(randomService.nextElement(list), isIn(list));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextEnumValue() {
|
||||
for (int i = 1; i < 10; i++) {
|
||||
assertThat(randomService.nextEnumValue(TestEnum.class), isIn(TestEnum.values()));
|
||||
}
|
||||
}
|
||||
|
||||
private enum TestEnum {
|
||||
ONE,
|
||||
TWO,
|
||||
THREE
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user