14
14
import org .junit .experimental .theories .PotentialParameterValue ;
15
15
import org .junit .experimental .theories .Theory ;
16
16
import org .junit .experimental .theories .PotentialParameterValue .CouldNotGenerateValueException ;
17
+ import org .junit .internal .runners .Roadie ;
17
18
import org .junit .internal .runners .TestClass ;
18
19
import org .junit .internal .runners .TestMethod ;
19
20
@@ -29,7 +30,8 @@ public PotentialMethodValues(List<PotentialParameterValue> concat) {
29
30
fSources = concat ;
30
31
}
31
32
32
- Object [] getValues (boolean nullsOk ) throws CouldNotGenerateValueException {
33
+ Object [] getValues (boolean nullsOk )
34
+ throws CouldNotGenerateValueException {
33
35
Object [] values = new Object [fSources .size ()];
34
36
for (int i = 0 ; i < values .length ; i ++) {
35
37
values [i ]= fSources .get (i ).getValue ();
@@ -51,23 +53,31 @@ PotentialMethodValues concat(PotentialParameterValue source) {
51
53
52
54
private List <AssumptionViolatedException > fInvalidParameters = new ArrayList <AssumptionViolatedException >();
53
55
56
+ private int successes = 0 ;
57
+
58
+ protected Throwable thrown = null ;
59
+
54
60
public TheoryMethod (Method method , TestClass testClass ) {
55
61
super (method , testClass );
56
62
fMethod = method ;
57
63
}
58
64
59
65
@ Override
60
- public void invoke (Object test ) throws IllegalArgumentException ,
61
- IllegalAccessException , InvocationTargetException {
62
- int runCount = 0 ;
66
+ protected void runTestProtected (Roadie context ) {
67
+ runTestUnprotected (context );
68
+ }
69
+
70
+ @ Override
71
+ public void invoke (Roadie context )
72
+ throws IllegalArgumentException , IllegalAccessException ,
73
+ InvocationTargetException {
63
74
try {
64
- runCount += runWithDiscoveredParameterValues (test ,
65
- new PotentialMethodValues (), ParameterSignature
66
- .signatures (fMethod ));
75
+ runWithDiscoveredParameterValues (context , new PotentialMethodValues (),
76
+ ParameterSignature .signatures (fMethod ));
67
77
} catch (Throwable e ) {
68
78
throw new InvocationTargetException (e );
69
79
}
70
- if (runCount == 0 )
80
+ if (successes == 0 )
71
81
Assert
72
82
.fail ("Never found parameters that satisfied method. Violated assumptions: "
73
83
+ fInvalidParameters );
@@ -80,45 +90,48 @@ public boolean nullsOk() {
80
90
return annotation .nullsAccepted ();
81
91
}
82
92
83
- int invokeWithActualParameters (Object target , Object [] params )
93
+ void invokeWithActualParameters (Object target , Object [] params )
84
94
throws Throwable {
85
95
try {
86
96
try {
87
97
fMethod .invoke (target , params );
98
+ successes ++;
88
99
} catch (InvocationTargetException e ) {
89
100
throw e .getTargetException ();
90
101
}
91
102
} catch (AssumptionViolatedException e ) {
92
103
fInvalidParameters .add (e );
93
- return 0 ;
94
104
} catch (Throwable e ) {
95
105
if (params .length == 0 )
96
106
throw e ;
97
107
throw new ParameterizedAssertionError (e , fMethod .getName (), params );
98
108
}
99
- return 1 ;
100
109
}
101
110
102
- int runWithDiscoveredParameterValues (Object target ,
103
- PotentialMethodValues valueSources , List <ParameterSignature > sigs )
104
- throws Throwable {
111
+ void runWithDiscoveredParameterValues (final Roadie context ,
112
+ PotentialMethodValues valueSources , List <ParameterSignature > sigs ) throws Throwable {
105
113
if (sigs .size () == 0 ) {
106
114
try {
107
- return invokeWithActualParameters (target , valueSources
108
- .getValues (nullsOk ()));
115
+ final Object [] values = valueSources .getValues (nullsOk ());
116
+ context .runProtected (this , new Runnable () {
117
+ public void run () {
118
+ try {
119
+ invokeWithActualParameters (context .getTarget (), values );
120
+ } catch (Throwable e ) {
121
+ thrown = e ;
122
+ }
123
+ }
124
+ });
125
+ if (thrown != null )
126
+ throw thrown ;
109
127
} catch (CouldNotGenerateValueException e ) {
110
- return 0 ;
128
+ }
129
+ } else {
130
+ for (PotentialParameterValue source : sigs .get (0 )
131
+ .getPotentialValues (context .getTarget ())) {
132
+ runWithDiscoveredParameterValues (context , valueSources
133
+ .concat (source ), sigs .subList (1 , sigs .size ()));
111
134
}
112
135
}
113
-
114
- int count = 0 ;
115
-
116
- for (PotentialParameterValue source : sigs .get (0 ).getPotentialValues (
117
- target )) {
118
- count += runWithDiscoveredParameterValues (target , valueSources
119
- .concat (source ), sigs .subList (1 , sigs .size ()));
120
- }
121
-
122
- return count ;
123
136
}
124
137
}
0 commit comments