diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 17:33:56 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 17:33:56 +1000 |
commit | dd9f72b94eb7b2c37061c80457e74e8d7ac3e18f (patch) | |
tree | 17ac650c0c4a5045b1cbf0ef5c194b0ea7f7acd3 /src/main/java/au/id/zancanaro/javacheck/junit | |
parent | 813e523e9e57dc38f81afc53340e216b948d87cf (diff) |
Add an ObjectGenerator<>, and related machinery (also a mapOf generator)
Diffstat (limited to 'src/main/java/au/id/zancanaro/javacheck/junit')
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/Properties.java | 102 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java | 5 |
2 files changed, 10 insertions, 97 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index 9d50af0..94032bc 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -1,9 +1,9 @@ package au.id.zancanaro.javacheck.junit; +import au.id.zancanaro.javacheck.DataSourceHelper; import au.id.zancanaro.javacheck.Generator; import au.id.zancanaro.javacheck.ShrinkResult; import au.id.zancanaro.javacheck.ShrinkTree; -import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; import au.id.zancanaro.javacheck.annotations.Seed; import org.junit.AssumptionViolatedException; @@ -13,64 +13,26 @@ import org.junit.runners.model.InitializationError; import org.junit.runners.model.Statement; import org.junit.runners.model.TestClass; -import java.lang.reflect.*; +import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.util.*; @SuppressWarnings("WeakerAccess") public class Properties extends BlockJUnit4ClassRunner { - private final Map<Type, Generator<?>> generators = new HashMap<>(); + private final DataSourceHelper helper; public Properties(Class<?> classObject) throws InitializationError { super(classObject); + helper = new DataSourceHelper(classObject); } @Override protected void collectInitializationErrors(List<Throwable> errors) { super.collectInitializationErrors(errors); - Set<Type> generated = validateGeneratorFields(errors); + Set<Type> generated = DataSourceHelper.validateGeneratorFields(getTestClass().getJavaClass(), errors); validateTestMethodParameters(errors, generated); } - 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)) { - boolean error = false; - if (!Modifier.isStatic(field.getModifiers())) { - errors.add(new Error("@DataSource field " + field.getName() + " must be static")); - error = true; - } - if (!Modifier.isPublic(field.getModifiers())) { - errors.add(new Error("@DataSource field " + field.getName() + " must be public")); - error = true; - } - - Type type = field.getGenericType(); - ParameterizedType parameterizedType; - if (type instanceof ParameterizedType) { - parameterizedType = (ParameterizedType) type; - if (parameterizedType.getRawType() instanceof Class) { - Class<?> c = (Class) parameterizedType.getRawType(); - if (c == Generator.class) { - if (!error) { - result.add(parameterizedType.getActualTypeArguments()[0]); - } - } else { - errors.add(new Error("@DataSource fields must be of type Generator<T>")); - } - } else { - errors.add(new Error("@DataSource fields must be of type Generator<T>")); - } - } else { - errors.add(new Error("@DataSource fields must be of type Generator<T>")); - } - } - } - return result; - } - private void validateTestMethodParameters(List<Throwable> errors, Set<Type> generated) { for (FrameworkMethod each : computeTestMethods()) { for (Type type : each.getMethod().getGenericParameterTypes()) { @@ -82,56 +44,6 @@ public class Properties extends BlockJUnit4ClassRunner { } } - private static final Map<Type, Type> rawTypes; - - static { - Map<Type, Type> types = new HashMap<>(); - types.put(Double.class, Double.TYPE); - types.put(Float.class, Float.TYPE); - types.put(Long.class, Long.TYPE); - types.put(Integer.class, Integer.TYPE); - types.put(Short.class, Short.TYPE); - types.put(Byte.class, Byte.TYPE); - types.put(Character.class, Character.TYPE); - types.put(Boolean.class, Boolean.TYPE); - rawTypes = Collections.unmodifiableMap(types); - } - - 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; - } - ParameterizedType parameterizedType = (ParameterizedType) type; - if (!(parameterizedType.getRawType() instanceof Class)) { - continue; - } - Class<?> c = (Class) parameterizedType.getRawType(); - if (c != Generator.class) { - continue; - } - try { - Type target = parameterizedType.getActualTypeArguments()[0]; - @SuppressWarnings("unchecked") - Generator<Object> generator = (Generator<Object>) field.get(null); - generators.put(target, generator); - if (rawTypes.containsKey(target)) { - generators.put(rawTypes.get(target), generator); - } - } catch (IllegalAccessException ex) { - throw new RuntimeException(ex); - } - } - } - return generators; - } @Override protected void validateConstructor(List<Throwable> errors) { @@ -161,7 +73,7 @@ public class Properties extends BlockJUnit4ClassRunner { @Override public Statement methodBlock(final FrameworkMethod method) { - return new GenerativeTester(method, getTestClass(), computeGenerators()); + return new GenerativeTester(method, getTestClass(), helper.computeGenerators()); } public static class GenerativeTester extends Statement { diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java index 44c071b..d3747b9 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java @@ -4,6 +4,7 @@ import au.id.zancanaro.javacheck.ShrinkResult; import java.util.Arrays; import java.util.Iterator; +import java.util.List; @SuppressWarnings("WeakerAccess") public class PropertyError extends AssertionError { @@ -19,9 +20,9 @@ public class PropertyError extends AssertionError { initCause(shrunk.thrown); } - private static String joinArgs(Object... params) { + private static String joinArgs(List<Object> params) { StringBuilder sb = new StringBuilder(); - Iterator<Object> iterator = Arrays.asList(params).iterator(); + Iterator<Object> iterator = params.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); sb.append(stringValueOf(next)); |