636
|
1 |
/**
|
|
2 |
*
|
|
3 |
*/
|
|
4 |
package org.junit.experimental.theories;
|
|
5 |
|
|
6 |
import java.lang.reflect.Field;
|
|
7 |
import java.lang.reflect.InvocationTargetException;
|
|
8 |
import java.lang.reflect.Modifier;
|
|
9 |
import java.util.ArrayList;
|
|
10 |
import java.util.List;
|
|
11 |
|
|
12 |
import org.junit.Assert;
|
|
13 |
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
|
|
14 |
import org.junit.experimental.theories.internal.Assignments;
|
|
15 |
import org.junit.experimental.theories.internal.ParameterizedAssertionError;
|
|
16 |
import org.junit.internal.AssumptionViolatedException;
|
|
17 |
import org.junit.runners.BlockJUnit4ClassRunner;
|
|
18 |
import org.junit.runners.model.FrameworkMethod;
|
|
19 |
import org.junit.runners.model.InitializationError;
|
|
20 |
import org.junit.runners.model.Statement;
|
|
21 |
|
|
22 |
public class Theories extends BlockJUnit4ClassRunner {
|
|
23 |
public Theories(Class<?> klass) throws InitializationError {
|
|
24 |
super(klass);
|
|
25 |
}
|
|
26 |
|
|
27 |
@Override
|
|
28 |
protected void collectInitializationErrors(List<Throwable> errors) {
|
|
29 |
super.collectInitializationErrors(errors);
|
|
30 |
validateDataPointFields(errors);
|
|
31 |
}
|
|
32 |
|
|
33 |
private void validateDataPointFields(List<Throwable> errors) {
|
|
34 |
Field[] fields= getTestClass().getJavaClass().getDeclaredFields();
|
|
35 |
|
|
36 |
for (Field each : fields)
|
|
37 |
if (each.getAnnotation(DataPoint.class) != null && !Modifier.isStatic(each.getModifiers()))
|
|
38 |
errors.add(new Error("DataPoint field " + each.getName() + " must be static"));
|
|
39 |
}
|
|
40 |
|
|
41 |
@Override
|
|
42 |
protected void validateZeroArgConstructor(List<Throwable> errors) {
|
|
43 |
// constructor can have args
|
|
44 |
}
|
|
45 |
|
|
46 |
@Override
|
|
47 |
protected void validateTestMethods(List<Throwable> errors) {
|
|
48 |
for (FrameworkMethod each : computeTestMethods())
|
|
49 |
each.validatePublicVoid(false, errors);
|
|
50 |
}
|
|
51 |
|
|
52 |
@Override
|
|
53 |
protected List<FrameworkMethod> computeTestMethods() {
|
|
54 |
List<FrameworkMethod> testMethods= super.computeTestMethods();
|
|
55 |
List<FrameworkMethod> theoryMethods= getTestClass().getAnnotatedMethods(Theory.class);
|
|
56 |
testMethods.removeAll(theoryMethods);
|
|
57 |
testMethods.addAll(theoryMethods);
|
|
58 |
return testMethods;
|
|
59 |
}
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public Statement methodBlock(final FrameworkMethod method) {
|
|
63 |
return new TheoryAnchor(method);
|
|
64 |
}
|
|
65 |
|
|
66 |
public class TheoryAnchor extends Statement {
|
|
67 |
private int successes= 0;
|
|
68 |
|
|
69 |
private FrameworkMethod fTestMethod;
|
|
70 |
|
|
71 |
private List<AssumptionViolatedException> fInvalidParameters= new ArrayList<AssumptionViolatedException>();
|
|
72 |
|
|
73 |
public TheoryAnchor(FrameworkMethod method) {
|
|
74 |
fTestMethod= method;
|
|
75 |
}
|
|
76 |
|
|
77 |
@Override
|
|
78 |
public void evaluate() throws Throwable {
|
|
79 |
runWithAssignment(Assignments.allUnassigned(
|
|
80 |
fTestMethod.getMethod(), getTestClass()));
|
|
81 |
|
|
82 |
if (successes == 0)
|
|
83 |
Assert
|
|
84 |
.fail("Never found parameters that satisfied method assumptions. Violated assumptions: "
|
|
85 |
+ fInvalidParameters);
|
|
86 |
}
|
|
87 |
|
|
88 |
protected void runWithAssignment(Assignments parameterAssignment)
|
|
89 |
throws Throwable {
|
|
90 |
if (!parameterAssignment.isComplete()) {
|
|
91 |
runWithIncompleteAssignment(parameterAssignment);
|
|
92 |
} else {
|
|
93 |
runWithCompleteAssignment(parameterAssignment);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
protected void runWithIncompleteAssignment(Assignments incomplete)
|
|
98 |
throws InstantiationException, IllegalAccessException,
|
|
99 |
Throwable {
|
|
100 |
for (PotentialAssignment source : incomplete
|
|
101 |
.potentialsForNextUnassigned()) {
|
|
102 |
runWithAssignment(incomplete.assignNext(source));
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
protected void runWithCompleteAssignment(final Assignments complete)
|
|
107 |
throws InstantiationException, IllegalAccessException,
|
|
108 |
InvocationTargetException, NoSuchMethodException, Throwable {
|
|
109 |
new BlockJUnit4ClassRunner(getTestClass().getJavaClass()) {
|
|
110 |
@Override
|
|
111 |
protected void collectInitializationErrors(
|
|
112 |
List<Throwable> errors) {
|
|
113 |
// do nothing
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public Statement methodBlock(FrameworkMethod method) {
|
|
118 |
final Statement statement= super.methodBlock(method);
|
|
119 |
return new Statement() {
|
|
120 |
@Override
|
|
121 |
public void evaluate() throws Throwable {
|
|
122 |
try {
|
|
123 |
statement.evaluate();
|
|
124 |
handleDataPointSuccess();
|
|
125 |
} catch (AssumptionViolatedException e) {
|
|
126 |
handleAssumptionViolation(e);
|
|
127 |
} catch (Throwable e) {
|
|
128 |
reportParameterizedError(e, complete
|
|
129 |
.getArgumentStrings(nullsOk()));
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
};
|
|
134 |
}
|
|
135 |
|
|
136 |
@Override
|
|
137 |
protected Statement methodInvoker(FrameworkMethod method, Object test) {
|
|
138 |
return methodCompletesWithParameters(method, complete, test);
|
|
139 |
}
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public Object createTest() throws Exception {
|
|
143 |
return getTestClass().getOnlyConstructor().newInstance(
|
|
144 |
complete.getConstructorArguments(nullsOk()));
|
|
145 |
}
|
|
146 |
}.methodBlock(fTestMethod).evaluate();
|
|
147 |
}
|
|
148 |
|
|
149 |
private Statement methodCompletesWithParameters(
|
|
150 |
final FrameworkMethod method, final Assignments complete, final Object freshInstance) {
|
|
151 |
return new Statement() {
|
|
152 |
@Override
|
|
153 |
public void evaluate() throws Throwable {
|
|
154 |
try {
|
|
155 |
final Object[] values= complete.getMethodArguments(
|
|
156 |
nullsOk());
|
|
157 |
method.invokeExplosively(freshInstance, values);
|
|
158 |
} catch (CouldNotGenerateValueException e) {
|
|
159 |
// ignore
|
|
160 |
}
|
|
161 |
}
|
|
162 |
};
|
|
163 |
}
|
|
164 |
|
|
165 |
protected void handleAssumptionViolation(AssumptionViolatedException e) {
|
|
166 |
fInvalidParameters.add(e);
|
|
167 |
}
|
|
168 |
|
|
169 |
protected void reportParameterizedError(Throwable e, Object... params)
|
|
170 |
throws Throwable {
|
|
171 |
if (params.length == 0)
|
|
172 |
throw e;
|
|
173 |
throw new ParameterizedAssertionError(e, fTestMethod.getName(),
|
|
174 |
params);
|
|
175 |
}
|
|
176 |
|
|
177 |
private boolean nullsOk() {
|
|
178 |
Theory annotation= fTestMethod.getMethod().getAnnotation(
|
|
179 |
Theory.class);
|
|
180 |
if (annotation == null)
|
|
181 |
return false;
|
|
182 |
return annotation.nullsAccepted();
|
|
183 |
}
|
|
184 |
|
|
185 |
protected void handleDataPointSuccess() {
|
|
186 |
successes++;
|
|
187 |
}
|
|
188 |
}
|
|
189 |
} |