636
|
1 |
/**
|
|
2 |
*
|
|
3 |
*/
|
|
4 |
package org.junit.experimental.theories.internal;
|
|
5 |
|
|
6 |
import java.lang.reflect.Method;
|
|
7 |
import java.util.ArrayList;
|
|
8 |
import java.util.List;
|
|
9 |
|
|
10 |
import org.junit.experimental.theories.ParameterSignature;
|
|
11 |
import org.junit.experimental.theories.ParameterSupplier;
|
|
12 |
import org.junit.experimental.theories.ParametersSuppliedBy;
|
|
13 |
import org.junit.experimental.theories.PotentialAssignment;
|
|
14 |
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
|
|
15 |
import org.junit.runners.model.TestClass;
|
|
16 |
|
|
17 |
public class Assignments {
|
|
18 |
private List<PotentialAssignment> fAssigned;
|
|
19 |
|
|
20 |
private final List<ParameterSignature> fUnassigned;
|
|
21 |
|
|
22 |
private final TestClass fClass;
|
|
23 |
|
|
24 |
public Assignments(List<PotentialAssignment> assigned,
|
|
25 |
List<ParameterSignature> unassigned, TestClass testClass) {
|
|
26 |
fUnassigned= unassigned;
|
|
27 |
fAssigned= assigned;
|
|
28 |
fClass= testClass;
|
|
29 |
}
|
|
30 |
|
|
31 |
public static Assignments allUnassigned(Method testMethod,
|
|
32 |
TestClass testClass) throws Exception {
|
|
33 |
List<ParameterSignature> signatures;
|
|
34 |
signatures= ParameterSignature.signatures(testClass.getOnlyConstructor());
|
|
35 |
signatures.addAll(ParameterSignature.signatures(testMethod));
|
|
36 |
return new Assignments(new ArrayList<PotentialAssignment>(),
|
|
37 |
signatures, testClass);
|
|
38 |
}
|
|
39 |
|
|
40 |
public boolean isComplete() {
|
|
41 |
return fUnassigned.size() == 0;
|
|
42 |
}
|
|
43 |
|
|
44 |
public ParameterSignature nextUnassigned() {
|
|
45 |
return fUnassigned.get(0);
|
|
46 |
}
|
|
47 |
|
|
48 |
public Assignments assignNext(PotentialAssignment source) {
|
|
49 |
List<PotentialAssignment> assigned= new ArrayList<PotentialAssignment>(
|
|
50 |
fAssigned);
|
|
51 |
assigned.add(source);
|
|
52 |
|
|
53 |
return new Assignments(assigned, fUnassigned.subList(1, fUnassigned
|
|
54 |
.size()), fClass);
|
|
55 |
}
|
|
56 |
|
|
57 |
public Object[] getActualValues(int start, int stop, boolean nullsOk)
|
|
58 |
throws CouldNotGenerateValueException {
|
|
59 |
Object[] values= new Object[stop - start];
|
|
60 |
for (int i= start; i < stop; i++) {
|
|
61 |
Object value= fAssigned.get(i).getValue();
|
|
62 |
if (value == null && !nullsOk)
|
|
63 |
throw new CouldNotGenerateValueException();
|
|
64 |
values[i - start]= value;
|
|
65 |
}
|
|
66 |
return values;
|
|
67 |
}
|
|
68 |
|
|
69 |
public List<PotentialAssignment> potentialsForNextUnassigned()
|
|
70 |
throws InstantiationException, IllegalAccessException {
|
|
71 |
ParameterSignature unassigned= nextUnassigned();
|
|
72 |
return getSupplier(unassigned).getValueSources(unassigned);
|
|
73 |
}
|
|
74 |
|
|
75 |
public ParameterSupplier getSupplier(ParameterSignature unassigned)
|
|
76 |
throws InstantiationException, IllegalAccessException {
|
|
77 |
ParameterSupplier supplier= getAnnotatedSupplier(unassigned);
|
|
78 |
if (supplier != null)
|
|
79 |
return supplier;
|
|
80 |
|
|
81 |
return new AllMembersSupplier(fClass);
|
|
82 |
}
|
|
83 |
|
|
84 |
public ParameterSupplier getAnnotatedSupplier(ParameterSignature unassigned)
|
|
85 |
throws InstantiationException, IllegalAccessException {
|
|
86 |
ParametersSuppliedBy annotation= unassigned
|
|
87 |
.findDeepAnnotation(ParametersSuppliedBy.class);
|
|
88 |
if (annotation == null)
|
|
89 |
return null;
|
|
90 |
return annotation.value().newInstance();
|
|
91 |
}
|
|
92 |
|
|
93 |
public Object[] getConstructorArguments(boolean nullsOk)
|
|
94 |
throws CouldNotGenerateValueException {
|
|
95 |
return getActualValues(0, getConstructorParameterCount(), nullsOk);
|
|
96 |
}
|
|
97 |
|
|
98 |
public Object[] getMethodArguments(boolean nullsOk)
|
|
99 |
throws CouldNotGenerateValueException {
|
|
100 |
return getActualValues(getConstructorParameterCount(),
|
|
101 |
fAssigned.size(), nullsOk);
|
|
102 |
}
|
|
103 |
|
|
104 |
public Object[] getAllArguments(boolean nullsOk)
|
|
105 |
throws CouldNotGenerateValueException {
|
|
106 |
return getActualValues(0, fAssigned.size(), nullsOk);
|
|
107 |
}
|
|
108 |
|
|
109 |
private int getConstructorParameterCount() {
|
|
110 |
List<ParameterSignature> signatures= ParameterSignature
|
|
111 |
.signatures(fClass.getOnlyConstructor());
|
|
112 |
int constructorParameterCount= signatures.size();
|
|
113 |
return constructorParameterCount;
|
|
114 |
}
|
|
115 |
|
|
116 |
public Object[] getArgumentStrings(boolean nullsOk) throws CouldNotGenerateValueException {
|
|
117 |
Object[] values= new Object[fAssigned.size()];
|
|
118 |
for (int i= 0; i < values.length; i++) {
|
|
119 |
values[i]= fAssigned.get(i).getDescription();
|
|
120 |
}
|
|
121 |
return values;
|
|
122 |
}
|
|
123 |
} |