summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/Properties.java
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-01 10:38:08 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-01 10:38:08 +1000
commitedfce37bc21699042baf14ad6d172d3187fe530c (patch)
tree7e3379e7a0f2a1c5ba203bea72f468941ca13dad /src/main/java/au/id/zancanaro/Properties.java
parent7e1182355d54f0dc8461ce7df7c4aca8c40d2a92 (diff)
Add @DataSource, allow printing of RoseTrees, other small changes to generators
Diffstat (limited to 'src/main/java/au/id/zancanaro/Properties.java')
-rw-r--r--src/main/java/au/id/zancanaro/Properties.java46
1 files changed, 38 insertions, 8 deletions
diff --git a/src/main/java/au/id/zancanaro/Properties.java b/src/main/java/au/id/zancanaro/Properties.java
index fab48f4..b5a0649 100644
--- a/src/main/java/au/id/zancanaro/Properties.java
+++ b/src/main/java/au/id/zancanaro/Properties.java
@@ -1,5 +1,6 @@
package au.id.zancanaro;
+import au.id.zancanaro.annotations.DataSource;
import au.id.zancanaro.annotations.Property;
import au.id.zancanaro.annotations.Seed;
import org.junit.AssumptionViolatedException;
@@ -14,7 +15,7 @@ import java.util.*;
public class Properties extends BlockJUnit4ClassRunner {
- private final Map<Type, Generator<Object>> generators = new HashMap<>();
+ private final Map<Type, Generator<?>> generators = new HashMap<>();
public Properties(Class<?> klass) throws InitializationError {
super(klass);
@@ -23,13 +24,18 @@ public class Properties extends BlockJUnit4ClassRunner {
@Override
protected void collectInitializationErrors(List<Throwable> errors) {
super.collectInitializationErrors(errors);
- validateGeneratorFields(errors);
+ Set<Type> generated = validateGeneratorFields(errors);
+ validateTestMethodParameters(errors, generated);
}
- private void validateGeneratorFields(List<Throwable> errors) {
+ private Set<Type> validateGeneratorFields(List<Throwable> errors) {
+ Set<Type> result = new HashSet<>();
Field[] fields = getTestClass().getJavaClass().getDeclaredFields();
for (Field field : fields) {
+ if (!field.isAnnotationPresent(DataSource.class)) {
+ continue;
+ }
Type type = field.getGenericType();
if (!(type instanceof ParameterizedType)) {
continue;
@@ -42,11 +48,29 @@ public class Properties extends BlockJUnit4ClassRunner {
if (c != Generator.class) {
continue;
}
+ boolean error = false;
if (!Modifier.isStatic(field.getModifiers())) {
errors.add(new Error("Generator field " + field.getName() + " must be static"));
+ error = true;
}
if (!Modifier.isPublic(field.getModifiers())) {
errors.add(new Error("Generator field " + field.getName() + " must be public"));
+ error = true;
+ }
+ if (!error) {
+ result.add(ptype.getActualTypeArguments()[0]);
+ }
+ }
+ return result;
+ }
+
+ private void validateTestMethodParameters(List<Throwable> errors, Set<Type> generated) {
+ for (FrameworkMethod each : computeTestMethods()) {
+ for (Type type : each.getMethod().getGenericParameterTypes()) {
+ if (!generated.contains(type)) {
+ errors.add(new Error("No @DataSource for type: " + type));
+ generated.add(type); // ignore future errors on this type
+ }
}
}
}
@@ -66,11 +90,14 @@ public class Properties extends BlockJUnit4ClassRunner {
rawTypes = Collections.unmodifiableMap(types);
}
- private Map<Type, Generator<Object>> computeGenerators() {
+ private Map<Type, Generator<?>> computeGenerators() {
if (generators.isEmpty()) {
Field[] fields = getTestClass().getJavaClass().getDeclaredFields();
for (Field field : fields) {
+ if (!field.isAnnotationPresent(DataSource.class)) {
+ continue;
+ }
Type type = field.getGenericType();
if (!(type instanceof ParameterizedType)) {
continue;
@@ -133,9 +160,9 @@ public class Properties extends BlockJUnit4ClassRunner {
public static class GenerativeTester extends Statement {
private final FrameworkMethod testMethod;
private final TestClass testClass;
- private final Map<Type, Generator<Object>> generators;
+ private final Map<Type, Generator<?>> generators;
- public GenerativeTester(FrameworkMethod testMethod, TestClass testClass, Map<Type, Generator<Object>> generators) {
+ public GenerativeTester(FrameworkMethod testMethod, TestClass testClass, Map<Type, Generator<?>> generators) {
this.testMethod = testMethod;
this.testClass = testClass;
this.generators = generators;
@@ -157,12 +184,14 @@ public class Properties extends BlockJUnit4ClassRunner {
runTest(new Object[0]);
} else {
@SuppressWarnings("unchecked")
- Generator<Object>[] generators = (Generator<Object>[]) new Generator[method.getParameterCount()];
+ Generator<?>[] generators = (Generator<?>[]) new Generator[method.getParameterCount()];
int index = 0;
for (Type type : method.getGenericParameterTypes()) {
+ // TODO: validate ahead of time that this generator will exist (ideally in the constructor validation time)
generators[index++] = this.generators.get(type);
}
- Generator<Object[]> generator = Generator.tuple(generators);
+ @SuppressWarnings("unchecked")
+ Generator<Object[]> generator = Generator.tuple((Generator<Object>[]) generators);
long seed = getSeed(method);
Random random = new Random(seed);
@@ -184,6 +213,7 @@ public class Properties extends BlockJUnit4ClassRunner {
}
;
} catch (Throwable ex) {
+// tree.print(new OutputStreamWriter(System.out), Arrays::toString);
throw new PropertyError(method.getName(), seed, shrink(tree, ex));
}
}