summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java
blob: c428edc1c27aa8eb751e506cfab0a2e5e274b552 (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
package au.id.zancanaro.javacheck.junit;

import au.id.zancanaro.javacheck.Generator;
import au.id.zancanaro.javacheck.ShrinkResult;
import au.id.zancanaro.javacheck.ShrinkTree;
import au.id.zancanaro.javacheck.annotations.Property;
import au.id.zancanaro.javacheck.annotations.Seed;
import au.id.zancanaro.javacheck.object.GeneratorProvider;
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.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

@SuppressWarnings("WeakerAccess")
public class Properties extends BlockJUnit4ClassRunner {
    private final GeneratorProvider provider;

    public Properties(Class<?> classObject) throws InitializationError {
        super(classObject);
        provider = GeneratorProvider.DEFAULT_PROVIDER.withDataSources(classObject);
    }

    @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(), provider);
    }

    public static class GenerativeTester extends Statement {
        private final FrameworkMethod testMethod;
        private final TestClass testClass;
        private final GeneratorProvider provider;

        public GenerativeTester(FrameworkMethod testMethod, TestClass testClass, GeneratorProvider provider) {
            this.testMethod = testMethod;
            this.testClass = testClass;
            this.provider = provider;
        }

        private static 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 {
                Generator<?>[] generators = Arrays.stream(method.getGenericParameterTypes())
                        .map(param -> provider.getGenerator(param, new Annotation[0], provider))
                        .toArray(Generator[]::new);
                Generator<List<Object>> generator = Generator.tuple(generators);

                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);
                    ShrinkTree<List<Object>> tree = generator.generate(random, size);
                    try {
                        runTest(tree.getValue().toArray());
                        assumptionsViolated = 0;
                    } catch (AssumptionViolatedException ex) {
                        numTests++;
                        if (assumptionsViolated++ == 50) {
                            throw new Error("Violated 50 assumptions in a row: failing test", ex);
                        }
                    } catch (Throwable ex) {
                        throw new PropertyError(method.getName(), seed, shrink(tree, ex));
                    }
                }
            }
        }

        private ShrinkResult shrink(ShrinkTree<List<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);

            Set<List<Object>> seenArgs = new HashSet<>();
            Iterator<ShrinkTree<List<Object>>> trees = failed.getChildren().iterator();
            while (trees.hasNext()) {
                ShrinkTree<List<Object>> tree = trees.next();
                List<Object> value = tree.getValue();
                boolean haveSeen = seenArgs.add(Arrays.asList(value));
                if (haveSeen) {
                    try {
                        runTest(value.toArray());
                    } catch (AssumptionViolatedException ex) {
                        // ignore, because it's not useful
                    } catch (Throwable ex) {
                        smallest[0] = new ShrinkResult(tree.getValue(), ex);
                        Iterator<ShrinkTree<List<Object>>> children = tree.getChildren().iterator();
                        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: " + 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();
        }
    }

}