Skip to content

Commit d1e6bd8

Browse files
committed
Merge branch 'master' into Timeout-rule-builder
2 parents dd5140b + 7d07b8d commit d1e6bd8

23 files changed

+1056
-17
lines changed

doc/ReleaseNotes4.12.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ This allows for validation to be added to annotations. Validators should extend
9595
`ExpectedException` didn't handle `AssertionError`s and `AssumptionViolatedException` well. This has been fixed. The new documentation explains the usage of `ExpectedException` for testing these exceptions. The two methods `handleAssertionErrors()` and `handleAssumptionViolatedExceptions()` are not needed anymore. If you have used them, just remove it and read `ExpectedException`'s documentation.
9696

9797

98+
### [Pull request #818:](https://github.com/junit-team/junit/pull/818) External version of AssumptionViolatedException
99+
100+
In JUnit 4.11 and earlier, if you wanted to write a custom runner that handled
101+
`AssumptionViolatedException` or you needed to create an instance of `AssumptionViolatedException`
102+
directly, you needed to import an internal class (`org.junit.internal.AssumptionViolatedException`).
103+
Now you can import `org.junit.AssumptionViolatedException` (which extends
104+
`org.junit.internal.AssumptionViolatedException`).
105+
106+
The classes in `Assume` have been modified to throw `org.junit.AssumptionViolatedException`.
107+
108+
109+
### [Pull request #985:](https://github.com/junit-team/junit/pull/985) Change AssumptionViolatedException to not set the cause to null; fixes issue #494
110+
111+
Previously, the `AssumptionViolatedException` constructors would explicitly set the cause to `null`
112+
(unless you use a constructor where you provide a `Throwable`, in which case it would set that as
113+
the cause). This prevented code directly creating the exception from setting a cause.
114+
115+
With this change, the cause is only set if you pass in a `Throwable`.
116+
117+
It's recommended that you create `AssumptionViolatedException` via the methods in `Assume`.
118+
98119

99120
### [Pull request #542:](https://github.com/junit-team/junit/pull/542) Customized failure message for `ExpectedException`
100121

@@ -565,11 +586,6 @@ Follow this link for _IntelliJ IDEA_: [http://www.jetbrains.com/idea/webhelp/act
565586
# Miscellaneous
566587

567588

568-
### [Pull request #862:] (https://github.com/junit-team/junit/pull/862) Delete classes that are deprecated for six years
569-
570-
`JUnit4ClassRunner` was deprecated in JUnit 4.3 (six years and nine major releases ago) with a comment saying "This may disappear as soon as 1 April 2009". We started having some problems with running the tests in JDK 7, and we decided to delete the class and its support classes. Although we try very hard to maintain backwards compatibility, `JUnit4ClassRunner` didn't support `Rule`s, it wasn't designed to be extensible, and it was in an internal package. Please use `BlockJUnit4ClassRunner` instead.
571-
572-
573589
### [Pull request #776:](https://github.com/junit-team/junit/pull/776) Add support for [Travis CI](http://travis-ci.org)
574590

575591
Travis CI is a free CI server for public Github repositories. Every pull request is run by Travis CI and Github's web interface shows the CI result for each pull request. Every user can use Travis CI for testing her branches, too.

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

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22

33
import java.util.List;
44

5+
/**
6+
* Abstract parent class for suppliers of input data points for theories. Extend this class to customize how {@link
7+
* org.junit.experimental.theories.Theories Theories} runner
8+
* finds accepted data points. Then use your class together with <b>&#064;ParametersSuppliedBy</b> on input
9+
* parameters for theories.
10+
*
11+
* <p>
12+
* For example, here is a supplier for values between two integers, and an annotation that references it:
13+
*
14+
* <pre>
15+
* &#064;Retention(RetentionPolicy.RUNTIME)
16+
* <b>&#064;ParametersSuppliedBy</b>(BetweenSupplier.class)
17+
* public @interface Between {
18+
* int first();
19+
*
20+
* int last();
21+
* }
22+
*
23+
* public static class BetweenSupplier extends <b>ParameterSupplier</b> {
24+
* &#064;Override
25+
* public List&lt;<b>PotentialAssignment</b>&gt; getValueSources(<b>ParameterSignature</b> sig) {
26+
* List&lt;<b>PotentialAssignment</b>&gt; list = new ArrayList&lt;PotentialAssignment&gt;();
27+
* Between annotation = (Between) sig.getSupplierAnnotation();
28+
*
29+
* for (int i = annotation.first(); i &lt;= annotation.last(); i++)
30+
* list.add(<b>PotentialAssignment</b>.forValue("ints", i));
31+
* return list;
32+
* }
33+
* }
34+
* </pre>
35+
* </p>
36+
*
37+
* @see org.junit.experimental.theories.ParametersSuppliedBy
38+
* @see org.junit.experimental.theories.Theories
39+
* @see org.junit.experimental.theories.FromDataPoints
40+
*/
541
public abstract class ParameterSupplier {
642
public abstract List<PotentialAssignment> getValueSources(ParameterSignature sig) throws Throwable;
743
}

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

+50
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,56 @@
1818
import org.junit.runners.model.Statement;
1919
import org.junit.runners.model.TestClass;
2020

21+
/**
22+
* The Theories runner allows to test a certain functionality against a subset of an infinite set of data points.
23+
* <p>
24+
* A Theory is a piece of functionality (a method) that is executed against several data inputs called data points.
25+
* To make a test method a theory you mark it with <b>&#064;Theory</b>. To create a data point you create a public
26+
* field in your test class and mark it with <b>&#064;DataPoint</b>. The Theories runner then executes your test
27+
* method as many times as the number of data points declared, providing a different data point as
28+
* the input argument on each invocation.
29+
* </p>
30+
* <p>
31+
* A Theory differs from standard test method in that it captures some aspect of the intended behavior in possibly
32+
* infinite numbers of scenarios which corresponds to the number of data points declared. Using assumptions and
33+
* assertions properly together with covering multiple scenarios with different data points can make your tests more
34+
* flexible and bring them closer to scientific theories (hence the name).
35+
* </p>
36+
* <p>
37+
* For example:
38+
* <pre>
39+
*
40+
* &#064;RunWith(<b>Theories.class</b>)
41+
* public class UserTest {
42+
* <b>&#064;DataPoint</b>
43+
* public static String GOOD_USERNAME = "optimus";
44+
* <b>&#064;DataPoint</b>
45+
* public static String USERNAME_WITH_SLASH = "optimus/prime";
46+
*
47+
* <b>&#064;Theory</b>
48+
* public void filenameIncludesUsername(String username) {
49+
* assumeThat(username, not(containsString("/")));
50+
* assertThat(new User(username).configFileName(), containsString(username));
51+
* }
52+
* }
53+
* </pre>
54+
* This makes it clear that the user's filename should be included in the config file name,
55+
* only if it doesn't contain a slash. Another test or theory might define what happens when a username does contain
56+
* a slash. <code>UserTest</code> will attempt to run <code>filenameIncludesUsername</code> on every compatible data
57+
* point defined in the class. If any of the assumptions fail, the data point is silently ignored. If all of the
58+
* assumptions pass, but an assertion fails, the test fails.
59+
* <p>
60+
* Defining general statements as theories allows data point reuse across a bunch of functionality tests and also
61+
* allows automated tools to search for new, unexpected data points that expose bugs.
62+
* </p>
63+
* <p>
64+
* The support for Theories has been absorbed from the Popper project, and more complete documentation can be found
65+
* from that projects archived documentation.
66+
* </p>
67+
*
68+
* @see <a href="http://web.archive.org/web/20071012143326/popper.tigris.org/tutorial.html">Archived Popper project documentation</a>
69+
* @see <a href="http://web.archive.org/web/20110608210825/http://shareandenjoy.saff.net/tdd-specifications.pdf">Paper on Theories</a>
70+
*/
2171
public class Theories extends BlockJUnit4ClassRunner {
2272
public Theories(Class<?> klass) throws InitializationError {
2373
super(klass);

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

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
88

9+
/**
10+
* Marks test methods that should be read as theories by the {@link org.junit.experimental.theories.Theories Theories} runner.
11+
*
12+
* @see org.junit.experimental.theories.Theories
13+
*/
914
@Retention(RetentionPolicy.RUNTIME)
1015
@Target(METHOD)
1116
public @interface Theory {

src/main/java/org/junit/experimental/theories/suppliers/TestedOnSupplier.java

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.junit.experimental.theories.ParameterSupplier;
88
import org.junit.experimental.theories.PotentialAssignment;
99

10+
/**
11+
* @see org.junit.experimental.theories.suppliers.TestedOn
12+
* @see org.junit.experimental.theories.ParameterSupplier
13+
*/
1014
public class TestedOnSupplier extends ParameterSupplier {
1115
@Override
1216
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {

src/main/java/org/junit/internal/AssumptionViolatedException.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ public class AssumptionViolatedException extends RuntimeException implements Sel
2626
private final Matcher<?> matcher;
2727

2828
public AssumptionViolatedException(String assumption, boolean valueMatcher, Object value, Matcher<?> matcher) {
29-
super(value instanceof Throwable ? (Throwable) value : null);
3029
this.assumption = assumption;
3130
this.value = value;
3231
this.matcher = matcher;
3332
this.valueMatcher = valueMatcher;
33+
34+
if (value instanceof Throwable) {
35+
initCause((Throwable) value);
36+
}
3437
}
3538

3639
/**
@@ -87,4 +90,4 @@ public void describeTo(Description description) {
8790
}
8891
}
8992
}
90-
}
93+
}

src/main/java/org/junit/internal/ComparisonCriteria.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.junit.internal;
22

33
import java.lang.reflect.Array;
4+
import java.util.Arrays;
45

56
import org.junit.Assert;
67

@@ -24,7 +25,11 @@ public abstract class ComparisonCriteria {
2425
*/
2526
public void arrayEquals(String message, Object expecteds, Object actuals)
2627
throws ArrayComparisonFailure {
27-
if (expecteds == actuals) {
28+
if (expecteds == actuals
29+
|| Arrays.deepEquals(new Object[] {expecteds}, new Object[] {actuals})) {
30+
// The reflection-based loop below is potentially very slow, especially for primitive
31+
// arrays. The deepEquals check allows us to circumvent it in the usual case where
32+
// the arrays are exactly equal.
2833
return;
2934
}
3035
String header = message == null ? "" : message + ": ";

src/main/java/org/junit/internal/JUnitSystem.java

+7
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
import java.io.PrintStream;
44

55
public interface JUnitSystem {
6+
7+
/**
8+
* Will be removed in the next major release
9+
*/
10+
@Deprecated
11+
void exit(int code);
12+
613
PrintStream out();
714
}

src/main/java/org/junit/internal/RealSystem.java

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
import java.io.PrintStream;
44

55
public class RealSystem implements JUnitSystem {
6+
7+
/**
8+
* Will be removed in the next major release
9+
*/
10+
@Deprecated
11+
public void exit(int code) {
12+
System.exit(code);
13+
}
14+
615
public PrintStream out() {
716
return System.out;
817
}

src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,46 @@
55
import org.hamcrest.Matcher;
66
import org.hamcrest.TypeSafeMatcher;
77

8+
/**
9+
* A matcher that applies a delegate matcher to the cause of the current Throwable, returning the result of that
10+
* match.
11+
*
12+
* @param <T> the type of the throwable being matched
13+
*/
814
public class ThrowableCauseMatcher<T extends Throwable> extends
915
TypeSafeMatcher<T> {
1016

11-
private final Matcher<T> matcher;
17+
private final Matcher<? extends Throwable> causeMatcher;
1218

13-
public ThrowableCauseMatcher(Matcher<T> matcher) {
14-
this.matcher = matcher;
19+
public ThrowableCauseMatcher(Matcher<? extends Throwable> causeMatcher) {
20+
this.causeMatcher = causeMatcher;
1521
}
1622

1723
public void describeTo(Description description) {
1824
description.appendText("exception with cause ");
19-
description.appendDescriptionOf(matcher);
25+
description.appendDescriptionOf(causeMatcher);
2026
}
2127

2228
@Override
2329
protected boolean matchesSafely(T item) {
24-
return matcher.matches(item.getCause());
30+
return causeMatcher.matches(item.getCause());
2531
}
2632

2733
@Override
2834
protected void describeMismatchSafely(T item, Description description) {
2935
description.appendText("cause ");
30-
matcher.describeMismatch(item.getCause(), description);
36+
causeMatcher.describeMismatch(item.getCause(), description);
3137
}
3238

39+
/**
40+
* Returns a matcher that verifies that the outer exception has a cause for which the supplied matcher
41+
* evaluates to true.
42+
*
43+
* @param matcher to apply to the cause of the outer exception
44+
* @param <T> type of the outer exception
45+
*/
3346
@Factory
34-
public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) {
47+
public static <T extends Throwable> Matcher<T> hasCause(final Matcher<? extends Throwable> matcher) {
3548
return new ThrowableCauseMatcher<T>(matcher);
3649
}
3750
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.junit.internal.runners;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.util.List;
6+
7+
import org.junit.internal.AssumptionViolatedException;
8+
import org.junit.runner.Description;
9+
import org.junit.runner.notification.Failure;
10+
import org.junit.runner.notification.RunNotifier;
11+
import org.junit.runners.BlockJUnit4ClassRunner;
12+
13+
/**
14+
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
15+
* removed in the next major release. Please use
16+
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
17+
*/
18+
@Deprecated
19+
public class ClassRoadie {
20+
private RunNotifier notifier;
21+
private TestClass testClass;
22+
private Description description;
23+
private final Runnable runnable;
24+
25+
public ClassRoadie(RunNotifier notifier, TestClass testClass,
26+
Description description, Runnable runnable) {
27+
this.notifier = notifier;
28+
this.testClass = testClass;
29+
this.description = description;
30+
this.runnable = runnable;
31+
}
32+
33+
protected void runUnprotected() {
34+
runnable.run();
35+
}
36+
37+
protected void addFailure(Throwable targetException) {
38+
notifier.fireTestFailure(new Failure(description, targetException));
39+
}
40+
41+
public void runProtected() {
42+
try {
43+
runBefores();
44+
runUnprotected();
45+
} catch (FailedBefore e) {
46+
} finally {
47+
runAfters();
48+
}
49+
}
50+
51+
private void runBefores() throws FailedBefore {
52+
try {
53+
try {
54+
List<Method> befores = testClass.getBefores();
55+
for (Method before : befores) {
56+
before.invoke(null);
57+
}
58+
} catch (InvocationTargetException e) {
59+
throw e.getTargetException();
60+
}
61+
} catch (AssumptionViolatedException e) {
62+
throw new FailedBefore();
63+
} catch (Throwable e) {
64+
addFailure(e);
65+
throw new FailedBefore();
66+
}
67+
}
68+
69+
private void runAfters() {
70+
List<Method> afters = testClass.getAfters();
71+
for (Method after : afters) {
72+
try {
73+
after.invoke(null);
74+
} catch (InvocationTargetException e) {
75+
addFailure(e.getTargetException());
76+
} catch (Throwable e) {
77+
addFailure(e); // Untested, but seems impossible
78+
}
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.junit.internal.runners;
2+
3+
import org.junit.runners.BlockJUnit4ClassRunner;
4+
5+
/**
6+
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
7+
* removed in the next major release. Please use
8+
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
9+
*/
10+
@Deprecated
11+
class FailedBefore extends Exception {
12+
private static final long serialVersionUID = 1L;
13+
}

0 commit comments

Comments
 (0)