Skip to content

Commit 829c711

Browse files
committed
=tck remove not used assertion method which could lead to NPE
1 parent 91120b8 commit 829c711

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

tck/src/main/java/org/reactivestreams/tck/TestEnvironment.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.concurrent.CountDownLatch;
1313
import java.util.concurrent.TimeUnit;
1414

15-
import static org.reactivestreams.tck.support.NonFatal.isNonFatal;
1615
import static org.testng.Assert.assertNotNull;
1716
import static org.testng.Assert.assertTrue;
1817
import static org.testng.Assert.fail;
@@ -77,31 +76,25 @@ public void flop(Throwable thr, String msg) {
7776
}
7877

7978

80-
public <T extends Throwable> Throwable expectThrowingOf(Class<T> clazz, String errorMsg, Runnable block) throws Throwable {
79+
public <T extends Throwable> void expectThrowingOfWithMessage(Class<T> clazz, String requiredMessagePart, Runnable block) throws Throwable {
80+
String errorMsg = String.format("Expected [%s] to be thrown", clazz);
81+
8182
try {
8283
block.run();
83-
flop(errorMsg);
84+
throw new AssertionError("Expected " + clazz.getCanonicalName() + ", yet no exception was thrown!");
8485
} catch (Throwable e) {
8586
if (clazz.isInstance(e)) {
8687
// ok
87-
return e;
88-
} else if (isNonFatal(e)) {
89-
flop(errorMsg + " but was: " + e);
88+
String message = e.getMessage();
89+
assertTrue(message.contains(requiredMessagePart),
90+
String.format("Got expected exception [%s] but missing message part [%s], was: %s", e.getClass(), requiredMessagePart, message));
9091
} else {
91-
throw e;
92+
String msg = errorMsg + " but was: " + e;
93+
flop(e, msg);
94+
throw new AssertionError(msg); // would love to include the `e` cause, but that constructor is Java 7+
9295
}
9396
}
9497

95-
// make compiler happy
96-
return null;
97-
}
98-
99-
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
100-
public <T extends Throwable> void expectThrowingOfWithMessage(Class<T> clazz, String requiredMessagePart, Runnable block) throws Throwable {
101-
Throwable err = expectThrowingOf(clazz, String.format("Expected [%s] to be thrown", clazz), block);
102-
String message = err.getMessage();
103-
assertTrue(message.contains(requiredMessagePart),
104-
String.format("Got expected exception [%s] but missing message part [%s], was: %s", err.getClass(), requiredMessagePart, message));
10598
}
10699

107100
public <T extends Throwable> void assertAsyncErrorWithMessage(Class<T> clazz, String requiredMessagePart) throws Throwable {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.reactivestreams.tck;
2+
3+
import org.testng.annotations.Test;
4+
5+
public class TestEnvironmentTest {
6+
7+
final TestEnvironment env = new TestEnvironment(100);
8+
9+
@Test
10+
public void assertAsyncErrorWithMessage_shouldNotThrowWhenExpectedException() throws Throwable {
11+
env.flop(new ExampleException("3.17"), "boom");
12+
13+
env.assertAsyncErrorWithMessage(ExampleException.class, "3.17");
14+
}
15+
16+
@Test(expectedExceptions = AssertionError.class)
17+
public void assertAsyncErrorWithMessage_shouldThrowWhenExceptionHasMissingText() throws Throwable {
18+
env.flop(new ExampleException("3.17"), "boom");
19+
20+
env.assertAsyncErrorWithMessage(ExampleException.class, "wat");
21+
}
22+
23+
@Test
24+
public void expectThrowingOfWithMessage_shouldNotThrowWhenExpectedExceptionThrown() throws Throwable {
25+
env.expectThrowingOfWithMessage(ExampleException.class, "3.17", new Runnable() {
26+
@Override public void run() {
27+
throw new ExampleException("3.17");
28+
}
29+
});
30+
}
31+
32+
@Test(expectedExceptions = AssertionError.class)
33+
public void expectThrowingOfWithMessage_shouldThrowWhenExpectedExceptionHasMissingText() throws Throwable {
34+
env.expectThrowingOfWithMessage(ExampleException.class, "3.17", new Runnable() {
35+
@Override public void run() {
36+
throw new ExampleException("3");
37+
}
38+
});
39+
}
40+
41+
@Test(expectedExceptions = AssertionError.class)
42+
public void expectThrowingOfWithMessage_shouldThrowWhenNoExceptionWasThrown() throws Throwable {
43+
env.expectThrowingOfWithMessage(ExampleException.class, "3.17", new Runnable() {
44+
@Override public void run() {
45+
}
46+
});
47+
}
48+
}
49+
50+
final class ExampleException extends RuntimeException {
51+
public ExampleException(String message) {
52+
super(message);
53+
}
54+
}

tck/src/test/resources/testng.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ tests:
55
- name: TCK
66
classes:
77
- org.reactivestreams.tck.IdentityProcessorVerificationDelegationTest
8+
- org.reactivestreams.tck.TestEnvironmentTest

0 commit comments

Comments
 (0)