1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.qualitytest.blueprint.strategy.creation;
17
18 import java.util.Random;
19
20 import net.sf.qualitycheck.Check;
21 import net.sf.qualitycheck.Throws;
22 import net.sf.qualitycheck.exception.IllegalNullArgumentException;
23 import net.sf.qualitycheck.exception.IllegalStateOfArgumentException;
24
25
26
27
28
29
30 public class RandomEnumCreationStrategy extends ValueCreationStrategy<Enum<?>> {
31
32 private final Random random = new Random();
33
34
35
36
37
38
39
40
41
42
43 @Override
44 @Throws({ IllegalNullArgumentException.class, IllegalStateOfArgumentException.class })
45 public Enum<?> createValue(final Class<?> expectedClazz) {
46 Check.notNull(expectedClazz, "expectedClazz");
47 Check.stateIsTrue(expectedClazz.isEnum(), "Argument 'expectedClazz' must be an enum.");
48 @SuppressWarnings("unchecked")
49 final Class<? extends Enum<?>> enumClazz = (Class<? extends Enum<?>>) expectedClazz;
50 final Enum<?>[] enumConstants = enumClazz.getEnumConstants();
51 final int index = enumConstants.length > 0 ? random.nextInt(enumConstants.length) : -1;
52 return enumConstants.length > 0 ? enumConstants[index] : null;
53 }
54
55 }