Skip to content

Commit dbe7711

Browse files
committed
Refactored out ignorable exception type-matching to minimise duplicating
of that logic
1 parent 93d7738 commit dbe7711

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.junit.experimental.theories.internal;
22

3-
import static org.hamcrest.CoreMatchers.not;
4-
import static org.hamcrest.core.IsInstanceOf.instanceOf;
5-
63
import java.lang.reflect.Array;
74
import java.lang.reflect.Field;
85
import java.util.ArrayList;
@@ -40,23 +37,19 @@ public Object getValue() throws CouldNotGenerateValueException {
4037
} catch (IllegalAccessException e) {
4138
throw new RuntimeException(
4239
"unexpected: getMethods returned an inaccessible method");
43-
} catch (Throwable t) {
44-
DataPoint annotation = fMethod.getAnnotation(DataPoint.class);
45-
if (annotation != null) {
46-
for (Class<? extends Throwable> ignorable : annotation.ignoredExceptions()) {
47-
Assume.assumeThat(t, not(instanceOf(ignorable)));
48-
}
49-
}
40+
} catch (Throwable throwable) {
41+
DataPoint annotation = fMethod.getAnnotation(DataPoint.class);
42+
Assume.assumeTrue(annotation == null || !isAssignableToAnyOf(annotation.ignoredExceptions(), throwable));
5043

51-
throw new CouldNotGenerateValueException(t);
44+
throw new CouldNotGenerateValueException(throwable);
5245
}
5346
}
5447

5548
@Override
5649
public String getDescription() throws CouldNotGenerateValueException {
5750
return fMethod.getName();
5851
}
59-
}
52+
}
6053

6154
private final TestClass fClass;
6255

@@ -86,16 +79,13 @@ private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignme
8679
if (returnType.isArray() && sig.canPotentiallyAcceptType(returnType.getComponentType())) {
8780
try {
8881
addArrayValues(sig, dataPointsMethod.getName(), list, dataPointsMethod.invokeExplosively(null));
89-
} catch (Throwable t) {
82+
} catch (Throwable throwable) {
9083
DataPoints annotation = dataPointsMethod.getAnnotation(DataPoints.class);
91-
if (annotation != null) {
92-
for (Class<? extends Throwable> ignored : annotation.ignoredExceptions()) {
93-
if (ignored.isAssignableFrom(t.getClass())) {
94-
return;
95-
}
96-
}
84+
if (annotation != null && isAssignableToAnyOf(annotation.ignoredExceptions(), throwable)) {
85+
return;
86+
} else {
87+
throw throwable;
9788
}
98-
throw t;
9989
}
10090
}
10191
}
@@ -145,6 +135,15 @@ private Object getStaticFieldValue(final Field field) {
145135
"unexpected: getFields returned an inaccessible field");
146136
}
147137
}
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+
}
148147

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

0 commit comments

Comments
 (0)