summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java
blob: 1e3f502762947e157d52975855686662c21b226c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package au.id.zancanaro.javacheck.junit;

import au.id.zancanaro.javacheck.Generator;
import au.id.zancanaro.javacheck.RoseTree;
import au.id.zancanaro.javacheck.ShrinkResult;
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;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;

import java.lang.reflect.*;
import java.util.*;

@SuppressWarnings("WeakerAccess")
public class Properties extends BlockJUnit4ClassRunner {
    private final Map<Type, Generator<?>> generators = new HashMap<>();

    public Properties(Class<?> classObject) throws InitializationError {
        super(classObject);
    }

    @Override
    protected void collectInitializationErrors(List<Throwable> errors) {
        super.collectInitializationErrors(errors);
        Set<Type> generated = validateGeneratorFields(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()) {
                if (!generated.contains(type)) {
                    errors.add(new Error("No @DataSource for type: " + type));
                    generated.add(type); // ignore future errors on this type
                }
            }
        }
    }

    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) {
        validateOnlyOneConstructor(errors);
    }

    @Override
    protected void validateTestMethods(List<Throwable> errors) {
        for (FrameworkMethod each : computeTestMethods()) {
            if (each.getAnnotation(Property.class) != null) {
                each.validatePublicVoid(false, errors);
                each.validateNoTypeParametersOnArgs(errors);
            } else {
                each.validatePublicVoidNoArg(false, errors);
            }
        }
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> testMethods = new ArrayList<>(super.computeTestMethods());
        List<FrameworkMethod> theoryMethods = getTestClass().getAnnotatedMethods(Property.class);
        testMethods.removeAll(theoryMethods);
        testMethods.addAll(theoryMethods);
        return testMethods;
    }

    @Override
    public Statement methodBlock(final FrameworkMethod method) {
        return new GenerativeTester(method, getTestClass(), computeGenerators());
    }

    public static class GenerativeTester extends Statement {
        private final FrameworkMethod testMethod;
        private final TestClass testClass;
        private final Map<Type, Generator<?>> generators;

        public GenerativeTester(FrameworkMethod testMethod, TestClass testClass, Map<Type, Generator<?>> generators) {
            this.testMethod = testMethod;
            this.testClass = testClass;
            this.generators = generators;
        }

        private long getSeed(Method method) {
            Seed seed = method.getAnnotation(Seed.class);
            if (seed == null) {
                return System.currentTimeMillis();
            } else {
                return seed.value();
            }
        }

        @Override
        public void evaluate() throws Throwable {
            Method method = testMethod.getMethod();
            if (method.getParameterCount() == 0) {
                runTest(new Object[0]);
            } else {
                @SuppressWarnings("unchecked")
                Generator<?>[] generators = (Generator<?>[]) new Generator[method.getParameterCount()];
                int index = 0;
                for (Type type : method.getGenericParameterTypes()) {
                    generators[index++] = this.generators.get(type);
                }
                @SuppressWarnings("unchecked")
                Generator<Object[]> generator = Generator.tuple((Generator<Object>[]) generators).map(List::toArray);

                long seed = getSeed(method);
                Random random = new Random(seed);

                Property property = testMethod.getAnnotation(Property.class);
                int assumptionsViolated = 0;
                int maxSize = property.maxSize();
                int numTests = property.runs();
                for (int i = 0; i < numTests; ++i) {
                    int size = Math.min(i + 1, maxSize);
                    RoseTree<Object[]> tree = generator.generate(random, size);
                    try {
                        runTest(tree.getValue());
                        assumptionsViolated = 0;
                    } catch (AssumptionViolatedException ex) {
                        numTests++;
                        if (assumptionsViolated++ == 50) {
                            throw new Error("Violated 50 assumptions in a row: failing test");
                        }
                    } catch (Throwable ex) {
//                        tree.print(new OutputStreamWriter(System.out), Arrays::toString);
                        throw new PropertyError(method.getName(), seed, shrink(tree, ex));
                    }
                }
            }
        }

        private ShrinkResult shrink(RoseTree<Object[]> failed, Throwable originalEx) {
            // this array is a mutable container so the shutdown handler can see the new version
            ShrinkResult[] smallest = new ShrinkResult[]{
                    new ShrinkResult(failed.getValue(), originalEx)};

            Thread shutdownHandler = makeShutdownHandler(smallest, originalEx);
            Runtime.getRuntime().addShutdownHook(shutdownHandler);

            Iterator<RoseTree<Object[]>> trees = failed.getChildren();
            Set<List<Object>> seenArgs = new HashSet<>();
            while (trees.hasNext()) {
                RoseTree<Object[]> tree = trees.next();
                if (seenArgs.add(Arrays.asList(tree.getValue()))) {
                    try {
                        runTest(tree.getValue());
                    } catch (AssumptionViolatedException ex) {
                        // ignore, because it's not useful
                    } catch (Throwable ex) {
                        smallest[0] = new ShrinkResult(tree.getValue(), ex);
                        Iterator<RoseTree<Object[]>> children = tree.getChildren();
                        if (children.hasNext()) {
                            trees = children;
                        } else {
                            break;
                        }
                    }
                }
            }

            Runtime.getRuntime().removeShutdownHook(shutdownHandler);
            return smallest[0];
        }

        private Thread makeShutdownHandler(ShrinkResult[] smallest, Throwable originalException) {
            return new Thread(() -> {
                System.err.println("Signal received while shrinking.\n" +
                        "Current best shrink is: " + Arrays.toString(smallest[0].args) + "\n" +
                        "Shrinking exception: " + smallest[0].thrown + "\n" +
                        "Originally was: " + originalException);
            });
        }

        public void runTest(final Object[] args) throws Throwable {
            new BlockJUnit4ClassRunner(testClass.getJavaClass()) {
                @Override
                protected void collectInitializationErrors(
                        List<Throwable> errors) {
                    // do nothing
                }

                @Override
                public Statement methodBlock(FrameworkMethod method) {
                    return super.methodBlock(method);
                }

                @Override
                protected Statement methodInvoker(FrameworkMethod method, Object test) {
                    return new Statement() {
                        @Override
                        public void evaluate() throws Throwable {
                            method.invokeExplosively(test, args);
                        }
                    };
                }

                @Override
                public Object createTest() throws Exception {
                    return getTestClass().getOnlyConstructor().newInstance();
                }
            }.methodBlock(testMethod).evaluate();
        }
    }

}