Skip to content

Commit 79d696c

Browse files
author
dsaff
committed
- @Before and @After methods are run before and after each set of attempted parameters
on a Theory - Refactoring removed duplication that used to exist in classes MethodRoadie and ClassRoadie
1 parent 85c570e commit 79d696c

File tree

14 files changed

+246
-225
lines changed

14 files changed

+246
-225
lines changed

build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<property file="${user.home}/.junit.properties" />
55
<property name="src" value="src" />
66
<property name="bin" value="bin" />
7-
<property name="version" value="4.4" />
7+
<property name="version" value="4.5-SNAPSHOT-${timestamp}" />
88
<property name="dist" value="junit${version}" />
99
<property name="versionfile" value="${src}/junit/runner/Version.java" />
1010
<property name="zipfile" value="${dist}.zip" />

doc/ReleaseNotes4.4.txt

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Summary of Changes in version 4.5 ##
2+
3+
- `@Before` and `@After` methods are run before and after each set of attempted parameters
4+
on a Theory
5+
6+
- Refactoring removed duplication that used to exist in classes MethodRoadie and ClassRoadie
7+
18
## Summary of Changes in version 4.4 ##
29

310
JUnit is designed to efficiently capture developers' intentions about

src/org/junit/experimental/test/theories/TheoriesTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
1212
import static org.junit.matchers.StringContains.containsString;
1313
import org.hamcrest.Matcher;
14+
import org.junit.Before;
1415
import org.junit.Test;
1516
import org.junit.experimental.results.ResultMatchers;
1617
import org.junit.experimental.theories.DataPoint;
1718
import org.junit.experimental.theories.Theories;
1819
import org.junit.experimental.theories.Theory;
1920
import org.junit.experimental.theories.suppliers.TestedOn;
2021
import org.junit.internal.runners.TestClass;
22+
import org.junit.runner.JUnitCore;
2123
import org.junit.runner.RunWith;
2224

2325
@SuppressWarnings("restriction")
@@ -211,4 +213,28 @@ public void allStringsAreNonNull(String s) {
211213
public void shouldFilterNull() {
212214
assertThat(testResult(ShouldFilterNull.class), isSuccessful());
213215
}
216+
217+
// TODO: (Jul 20, 2007 1:58:18 PM) too complex
218+
219+
@RunWith(Theories.class)
220+
public static class BeforeAndAfterEachTime {
221+
public static int befores = 0;
222+
223+
@DataPoint public static String A = "A";
224+
@DataPoint public static String B = "B";
225+
226+
@Before public void incrementBefore() {
227+
befores++;
228+
}
229+
230+
@Theory public void stringsAreOK(String string) {
231+
}
232+
}
233+
234+
@Test public void beforeIsCalledOnEachParameterSet() {
235+
BeforeAndAfterEachTime.befores = 0;
236+
JUnitCore.runClasses(BeforeAndAfterEachTime.class);
237+
assertThat(BeforeAndAfterEachTime.befores, is(2));
238+
}
239+
214240
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public Theories(Class<?> klass) throws InitializationError {
2323

2424
@Override
2525
protected List<Method> getTestMethods() {
26+
// TODO: (Jul 20, 2007 2:02:44 PM) Only get methods once
27+
2628
List<Method> testMethods= super.getTestMethods();
2729
testMethods.addAll(getTestClass().getAnnotatedMethods(Theory.class));
2830
return testMethods;

src/org/junit/experimental/theories/internal/TheoryMethod.java

+40-27
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.junit.experimental.theories.PotentialParameterValue;
1515
import org.junit.experimental.theories.Theory;
1616
import org.junit.experimental.theories.PotentialParameterValue.CouldNotGenerateValueException;
17+
import org.junit.internal.runners.Roadie;
1718
import org.junit.internal.runners.TestClass;
1819
import org.junit.internal.runners.TestMethod;
1920

@@ -29,7 +30,8 @@ public PotentialMethodValues(List<PotentialParameterValue> concat) {
2930
fSources= concat;
3031
}
3132

32-
Object[] getValues(boolean nullsOk) throws CouldNotGenerateValueException {
33+
Object[] getValues(boolean nullsOk)
34+
throws CouldNotGenerateValueException {
3335
Object[] values= new Object[fSources.size()];
3436
for (int i= 0; i < values.length; i++) {
3537
values[i]= fSources.get(i).getValue();
@@ -51,23 +53,31 @@ PotentialMethodValues concat(PotentialParameterValue source) {
5153

5254
private List<AssumptionViolatedException> fInvalidParameters= new ArrayList<AssumptionViolatedException>();
5355

56+
private int successes= 0;
57+
58+
protected Throwable thrown = null;
59+
5460
public TheoryMethod(Method method, TestClass testClass) {
5561
super(method, testClass);
5662
fMethod= method;
5763
}
5864

5965
@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 {
6374
try {
64-
runCount+= runWithDiscoveredParameterValues(test,
65-
new PotentialMethodValues(), ParameterSignature
66-
.signatures(fMethod));
75+
runWithDiscoveredParameterValues(context, new PotentialMethodValues(),
76+
ParameterSignature.signatures(fMethod));
6777
} catch (Throwable e) {
6878
throw new InvocationTargetException(e);
6979
}
70-
if (runCount == 0)
80+
if (successes == 0)
7181
Assert
7282
.fail("Never found parameters that satisfied method. Violated assumptions: "
7383
+ fInvalidParameters);
@@ -80,45 +90,48 @@ public boolean nullsOk() {
8090
return annotation.nullsAccepted();
8191
}
8292

83-
int invokeWithActualParameters(Object target, Object[] params)
93+
void invokeWithActualParameters(Object target, Object[] params)
8494
throws Throwable {
8595
try {
8696
try {
8797
fMethod.invoke(target, params);
98+
successes++;
8899
} catch (InvocationTargetException e) {
89100
throw e.getTargetException();
90101
}
91102
} catch (AssumptionViolatedException e) {
92103
fInvalidParameters.add(e);
93-
return 0;
94104
} catch (Throwable e) {
95105
if (params.length == 0)
96106
throw e;
97107
throw new ParameterizedAssertionError(e, fMethod.getName(), params);
98108
}
99-
return 1;
100109
}
101110

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 {
105113
if (sigs.size() == 0) {
106114
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;
109127
} 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()));
111134
}
112135
}
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;
123136
}
124137
}

src/org/junit/internal/runners/JUnit4ClassRunner.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ protected void validate() throws InitializationError {
3939

4040
@Override
4141
public void run(final RunNotifier notifier) {
42-
new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() {
42+
fTestClass.runProtected(notifier, getDescription(), new Runnable() {
4343
public void run() {
4444
runMethods(notifier);
4545
}
46-
}).runProtected();
46+
});
4747
}
4848

4949
protected void runMethods(final RunNotifier notifier) {
@@ -85,7 +85,7 @@ protected void invokeTestMethod(Method method, RunNotifier notifier) {
8585
return;
8686
}
8787
TestMethod testMethod= wrapMethod(method);
88-
new MethodRoadie(test, testMethod, notifier, description).run();
88+
testMethod.run(new Roadie(notifier, description, test));
8989
}
9090

9191
protected TestMethod wrapMethod(Method method) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.junit.internal.runners;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.List;
5+
6+
7+
public abstract class JavaElement {
8+
protected abstract List<Method> getAfters();
9+
10+
protected abstract List<Method> getBefores();
11+
}

src/org/junit/internal/runners/MethodRoadie.java

-150
This file was deleted.

0 commit comments

Comments
 (0)