Skip to content

Commit ccf0c0a

Browse files
committed
Changed single-valued datapoints back to deferred execution
1 parent 3209ce6 commit ccf0c0a

File tree

3 files changed

+55
-32
lines changed

3 files changed

+55
-32
lines changed

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,34 @@
1919
* Supplies Theory parameters based on all public members of the target class.
2020
*/
2121
public class AllMembersSupplier extends ParameterSupplier {
22+
static class MethodParameterValue extends PotentialAssignment {
23+
private final FrameworkMethod fMethod;
24+
25+
private MethodParameterValue(FrameworkMethod dataPointMethod) {
26+
fMethod = dataPointMethod;
27+
}
28+
29+
@Override
30+
public Object getValue() throws CouldNotGenerateValueException {
31+
try {
32+
return fMethod.invokeExplosively(null);
33+
} catch (IllegalArgumentException e) {
34+
throw new RuntimeException(
35+
"unexpected: argument length is checked");
36+
} catch (IllegalAccessException e) {
37+
throw new RuntimeException(
38+
"unexpected: getMethods returned an inaccessible method");
39+
} catch (Throwable e) {
40+
throw new CouldNotGenerateValueException();
41+
// do nothing, just look for more values
42+
}
43+
}
2244

45+
@Override
46+
public String getDescription() throws CouldNotGenerateValueException {
47+
return fMethod.getName();
48+
}
49+
}
2350
private final TestClass fClass;
2451

2552
/**
@@ -57,19 +84,8 @@ private void addMultiPointMethods(ParameterSignature sig, List<PotentialAssignme
5784

5885
private void addSinglePointMethods(ParameterSignature sig, List<PotentialAssignment> list) {
5986
for (FrameworkMethod dataPointMethod : getSingleDataPointMethods(sig)) {
60-
if (sig.canPotentiallyAcceptType(dataPointMethod.getReturnType())) {
61-
Object value;
62-
63-
try {
64-
value = dataPointMethod.invokeExplosively(null);
65-
} catch (Throwable e) {
66-
// ignore and move on
67-
continue;
68-
}
69-
70-
if (sig.canAcceptValue(value)) {
71-
list.add(PotentialAssignment.forValue(dataPointMethod.getName(), value));
72-
}
87+
if (sig.canAcceptType(dataPointMethod.getType())) {
88+
list.add(new MethodParameterValue(dataPointMethod));
7389
}
7490
}
7591
}

src/test/java/org/junit/tests/experimental/theories/internal/AllMembersSupplierTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,6 @@ public void dataPointsArrayShouldBeRecognizedOnValueTypeNotFieldType() throws Ex
5353
assertEquals(2, assignments.size());
5454
}
5555

56-
public static class HasDataPointMethodWithMatchingButInaccurateTypes {
57-
@DataPoint
58-
public static Object object() {
59-
return 1;
60-
}
61-
62-
@Theory
63-
public void theory(Integer param) {
64-
}
65-
}
66-
67-
@Test
68-
public void dataPointMethodShouldBeRecognizedOnValueTypeNotFieldType() throws Exception {
69-
List<PotentialAssignment> assignments = potentialAssignments(
70-
HasDataPointMethodWithMatchingButInaccurateTypes.class.getMethod("theory", Integer.class));
71-
72-
assertEquals(1, assignments.size());
73-
}
74-
7556
public static class HasDataPointMethodWithOverlyGeneralTypes {
7657
@DataPoint
7758
public static Integer object() {

src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
1111
import static org.junit.tests.experimental.theories.TheoryTestUtils.potentialAssignments;
1212

13+
import java.util.ArrayList;
1314
import java.util.Date;
1415
import java.util.List;
1516

@@ -64,6 +65,31 @@ public void ignoreExceptionsFromDataPointMethods() {
6465
assertThat(testResult(HasUglyDataPointMethod.class), isSuccessful());
6566
}
6667

68+
@RunWith(Theories.class)
69+
public static class DataPointMethodReturnsMutableObject {
70+
@DataPoint
71+
public static List<Object> empty() {
72+
return new ArrayList<Object>();
73+
}
74+
75+
@DataPoint
76+
public static int ONE = 1;
77+
78+
@DataPoint
79+
public static int TWO = 2;
80+
81+
@Theory
82+
public void everythingsEmpty(List<Object> first, int number) {
83+
assertThat(first.size(), is(0));
84+
first.add("a");
85+
}
86+
}
87+
88+
@Test
89+
public void mutableObjectsAreCreatedAfresh() {
90+
assertThat(failures(DataPointMethodReturnsMutableObject.class), empty());
91+
}
92+
6793
@RunWith(Theories.class)
6894
public static class HasDateMethod {
6995
@DataPoint

0 commit comments

Comments
 (0)