Skip to content

Commit 34e6674

Browse files
author
David Saff
committed
Merge pull request #639 from pimterry/datapoints-exceptions-#449
Fixes #449, stopping AllMembersSupplier & Theories hiding DataPoints method exceptions
2 parents 8d7f5cc + dbe7711 commit 34e6674

18 files changed

+294
-105
lines changed

src/main/java/org/junit/experimental/theories/DataPoint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
@Target({FIELD, METHOD})
5353
public @interface DataPoint {
5454
String[] value() default {};
55-
}
55+
Class<? extends Throwable>[] ignoredExceptions() default {};
56+
}

src/main/java/org/junit/experimental/theories/DataPoints.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@
5151
@Target({FIELD, METHOD})
5252
public @interface DataPoints {
5353
String[] value() default {};
54+
Class<? extends Throwable>[] ignoredExceptions() default {};
5455
}

src/main/java/org/junit/experimental/theories/ParameterSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import java.util.List;
44

55
public abstract class ParameterSupplier {
6-
public abstract List<PotentialAssignment> getValueSources(ParameterSignature sig);
6+
public abstract List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable;
77
}

src/main/java/org/junit/experimental/theories/PotentialAssignment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
public abstract class PotentialAssignment {
66
public static class CouldNotGenerateValueException extends Exception {
77
private static final long serialVersionUID = 1L;
8+
9+
public CouldNotGenerateValueException() {
10+
}
11+
12+
public CouldNotGenerateValueException(Throwable t) {
13+
super(t);
14+
}
815
}
916

1017
public static PotentialAssignment forValue(final String name, final Object value) {

src/main/java/org/junit/experimental/theories/Theories.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010

1111
import org.junit.Assert;
12-
import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
12+
import org.junit.Assume;
1313
import org.junit.experimental.theories.internal.Assignments;
1414
import org.junit.experimental.theories.internal.ParameterizedAssertionError;
1515
import org.junit.internal.AssumptionViolatedException;
@@ -202,8 +202,13 @@ protected Statement methodInvoker(FrameworkMethod method, Object test) {
202202

203203
@Override
204204
public Object createTest() throws Exception {
205-
return getTestClass().getOnlyConstructor().newInstance(
206-
complete.getConstructorArguments(nullsOk()));
205+
Object[] params = complete.getConstructorArguments();
206+
207+
if (!nullsOk()) {
208+
Assume.assumeNotNull(params);
209+
}
210+
211+
return getTestClass().getOnlyConstructor().newInstance(params);
207212
}
208213
}.methodBlock(fTestMethod).evaluate();
209214
}
@@ -213,13 +218,13 @@ private Statement methodCompletesWithParameters(
213218
return new Statement() {
214219
@Override
215220
public void evaluate() throws Throwable {
216-
try {
217-
final Object[] values = complete.getMethodArguments(
218-
nullsOk());
219-
method.invokeExplosively(freshInstance, values);
220-
} catch (CouldNotGenerateValueException e) {
221-
// ignore
221+
final Object[] values = complete.getMethodArguments();
222+
223+
if (!nullsOk()) {
224+
Assume.assumeNotNull(values);
222225
}
226+
227+
method.invokeExplosively(freshInstance, values);
223228
}
224229
};
225230
}

src/main/java/org/junit/experimental/theories/internal/AllMembersSupplier.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Collection;
77
import java.util.List;
88

9+
import org.junit.Assume;
910
import org.junit.experimental.theories.DataPoint;
1011
import org.junit.experimental.theories.DataPoints;
1112
import org.junit.experimental.theories.ParameterSignature;
@@ -36,17 +37,20 @@ public Object getValue() throws CouldNotGenerateValueException {
3637
} catch (IllegalAccessException e) {
3738
throw new RuntimeException(
3839
"unexpected: getMethods returned an inaccessible method");
39-
} catch (Throwable e) {
40-
throw new CouldNotGenerateValueException();
41-
// do nothing, just look for more values
40+
} catch (Throwable throwable) {
41+
DataPoint annotation = fMethod.getAnnotation(DataPoint.class);
42+
Assume.assumeTrue(annotation == null || !isAssignableToAnyOf(annotation.ignoredExceptions(), throwable));
43+
44+
throw new CouldNotGenerateValueException(throwable);
4245
}
43-
}
44-
46+
}
47+
4548
@Override
4649
public String getDescription() throws CouldNotGenerateValueException {
4750
return fMethod.getName();
4851
}
49-
}
52+
}
53+
5054
private final TestClass fClass;
5155

5256
/**
@@ -57,7 +61,7 @@ public AllMembersSupplier(TestClass type) {
5761
}
5862

5963
@Override
60-
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
64+
public List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable {
6165
List<PotentialAssignment> list = new ArrayList<PotentialAssignment>();
6266

6367
addSinglePointFields(sig, list);
@@ -68,15 +72,20 @@ public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
6872
return list;
6973
}
7074

71-
private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) {
75+
private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignment> list) throws Throwable {
7276
for (FrameworkMethod dataPointsMethod : getDataPointsMethods(sig)) {
7377
Class<?> returnType = dataPointsMethod.getReturnType();
7478

7579
if (returnType.isArray() && sig.canPotentiallyAcceptType(returnType.getComponentType())) {
7680
try {
7781
addArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
78-
} catch (Throwable e) {
79-
// ignore and move on
82+
} catch (Throwable throwable) {
83+
DataPoints annotation = dataPointsMethod.getAnnotation(DataPoints.class);
84+
if (annotation != null && isAssignableToAnyOf(annotation.ignoredExceptions(), throwable)) {
85+
return;
86+
} else {
87+
throw throwable;
88+
}
8089
}
8190
}
8291
}
@@ -94,7 +103,7 @@ private void addMultiPointFields(ParameterSignature sig, List<PotentialAssignmen
94103
for (final Field field : getDataPointsFields(sig)) {
95104
addArrayValues(sig, field.getName(), list, getStaticFieldValue(field));
96105
}
97-
}
106+
}
98107

99108
private void addSinglePointFields(ParameterSignature sig, List<PotentialAssignment> list) {
100109
for (final Field field : getSingleDataPointFields(sig)) {
@@ -126,6 +135,15 @@ private Object getStaticFieldValue(final Field field) {
126135
"unexpected: getFields returned an inaccessible field");
127136
}
128137
}
138+
139+
private static boolean isAssignableToAnyOf(Class<?>[] typeArray, Object target) {
140+
for (Class<?> type : typeArray) {
141+
if (type.isAssignableFrom(target.getClass())) {
142+
return true;
143+
}
144+
}
145+
return false;
146+
}
129147

130148
protected Collection<FrameworkMethod> getDataPointsMethods(ParameterSignature sig) {
131149
return fClass.getAnnotatedMethods(DataPoints.class);

src/main/java/org/junit/experimental/theories/internal/Assignments.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,17 @@ public Assignments assignNext(PotentialAssignment source) {
6363
fUnassigned.size()), fClass);
6464
}
6565

66-
public Object[] getActualValues(int start, int stop, boolean nullsOk)
66+
public Object[] getActualValues(int start, int stop)
6767
throws CouldNotGenerateValueException {
6868
Object[] values = new Object[stop - start];
6969
for (int i = start; i < stop; i++) {
70-
Object value = fAssigned.get(i).getValue();
71-
if (value == null && !nullsOk) {
72-
throw new CouldNotGenerateValueException();
73-
}
74-
values[i - start] = value;
70+
values[i - start] = fAssigned.get(i).getValue();
7571
}
7672
return values;
7773
}
7874

7975
public List<PotentialAssignment> potentialsForNextUnassigned()
80-
throws Exception {
76+
throws Throwable {
8177
ParameterSignature unassigned = nextUnassigned();
8278
List<PotentialAssignment> assignments = getSupplier(unassigned).getValueSources(unassigned);
8379

@@ -127,20 +123,17 @@ private ParameterSupplier buildParameterSupplierFromClass(
127123
return cls.newInstance();
128124
}
129125

130-
public Object[] getConstructorArguments(boolean nullsOk)
126+
public Object[] getConstructorArguments()
131127
throws CouldNotGenerateValueException {
132-
return getActualValues(0, getConstructorParameterCount(), nullsOk);
128+
return getActualValues(0, getConstructorParameterCount());
133129
}
134130

135-
public Object[] getMethodArguments(boolean nullsOk)
136-
throws CouldNotGenerateValueException {
137-
return getActualValues(getConstructorParameterCount(),
138-
fAssigned.size(), nullsOk);
131+
public Object[] getMethodArguments() throws CouldNotGenerateValueException {
132+
return getActualValues(getConstructorParameterCount(), fAssigned.size());
139133
}
140134

141-
public Object[] getAllArguments(boolean nullsOk)
142-
throws CouldNotGenerateValueException {
143-
return getActualValues(0, fAssigned.size(), nullsOk);
135+
public Object[] getAllArguments() throws CouldNotGenerateValueException {
136+
return getActualValues(0, fAssigned.size());
144137
}
145138

146139
private int getConstructorParameterCount() {

src/main/java/org/junit/experimental/theories/internal/SpecificDataPointsSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected Collection<Field> getSingleDataPointFields(ParameterSignature sig) {
3333
}
3434
}
3535

36-
return fieldsWithMatchingNames;
36+
return fieldsWithMatchingNames;
3737
}
3838

3939
@Override

src/test/java/org/junit/tests/experimental/theories/TheoryTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final class TheoryTestUtils {
1818
private TheoryTestUtils() { }
1919

2020
public static List<PotentialAssignment> potentialAssignments(Method method)
21-
throws Exception {
21+
throws Throwable {
2222
return Assignments.allUnassigned(method,
2323
new TestClass(method.getDeclaringClass()))
2424
.potentialsForNextUnassigned();

src/test/java/org/junit/tests/experimental/theories/extendingwithstubs/StubbedTheories.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected void runWithIncompleteAssignment(Assignments incomplete)
5050
}
5151

5252
private GuesserQueue createGuesserQueue(Assignments incomplete)
53-
throws Exception {
53+
throws Throwable {
5454
ParameterSignature nextUnassigned = incomplete.nextUnassigned();
5555

5656
if (nextUnassigned.hasAnnotation(Stub.class)) {

0 commit comments

Comments
 (0)