Use assertThrows in classes ActorCreationTest and OutputStreamSinkTest (#30165)

This commit is contained in:
Andrei Arlou 2021-05-05 11:45:37 +03:00 committed by GitHub
parent 73d74dfc69
commit 1a36631dbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 57 deletions

View file

@ -10,6 +10,7 @@ import static java.util.stream.Collectors.toCollection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import akka.japi.Creator; import akka.japi.Creator;
@ -196,14 +197,11 @@ public class ActorCreationTest extends JUnitSuite {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Deprecated @Deprecated
public void testWrongErasedStaticCreator() { public void testWrongErasedStaticCreator() {
try { IllegalArgumentException exception =
Props.create(new G()); Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(new G()));
assert false; assertEquals(
} catch (IllegalArgumentException e) { "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
assertEquals( exception.getMessage());
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
e.getMessage());
}
Props.create(AbstractActor.class, new G()); Props.create(AbstractActor.class, new G());
} }
@ -217,14 +215,16 @@ public class ActorCreationTest extends JUnitSuite {
@Test @Test
@Deprecated @Deprecated
public void testWrongAnonymousClassStaticCreator() { public void testWrongAnonymousClassStaticCreator() {
try { IllegalArgumentException exception =
Props.create(new C() {}); // has implicit reference to outer class Assert.assertThrows(
org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); "Should have detected this is not a real static class, and thrown",
} catch (IllegalArgumentException e) { IllegalArgumentException.class,
assertEquals( () -> {
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", Props.create(new C() {}); // has implicit reference to outer class
e.getMessage()); });
} assertEquals(
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
exception.getMessage());
} }
@Test @Test
@ -257,14 +257,11 @@ public class ActorCreationTest extends JUnitSuite {
@Test @Test
public void testWrongAbstractActorClass() { public void testWrongAbstractActorClass() {
try { IllegalArgumentException exception =
Props.create(H.class, "a"); Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(H.class, "a"));
assert false; assertEquals(
} catch (IllegalArgumentException e) { String.format("Actor class [%s] must not be abstract", H.class.getName()),
assertEquals( exception.getMessage());
String.format("Actor class [%s] must not be abstract", H.class.getName()),
e.getMessage());
}
} }
private static Creator<AbstractActor> createAnonymousCreatorInStaticMethod() { private static Creator<AbstractActor> createAnonymousCreatorInStaticMethod() {
@ -294,17 +291,19 @@ public class ActorCreationTest extends JUnitSuite {
@Test @Test
@Deprecated @Deprecated
public void testAnonymousClassCreatorWithArguments() { public void testAnonymousClassCreatorWithArguments() {
try { IllegalArgumentException exception =
final Creator<AbstractActor> anonymousCreatorFromStaticMethod = new P("hello") { Assert.assertThrows(
// captures enclosing class "Should have detected this is not a real static class, and thrown",
}; IllegalArgumentException.class,
Props.create(anonymousCreatorFromStaticMethod); () -> {
org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); final Creator<AbstractActor> anonymousCreatorFromStaticMethod = new P("hello") {
} catch (IllegalArgumentException e) { // captures enclosing class
assertEquals( };
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", Props.create(anonymousCreatorFromStaticMethod);
e.getMessage()); });
} assertEquals(
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
exception.getMessage());
} }
@Test @Test
@ -318,14 +317,15 @@ public class ActorCreationTest extends JUnitSuite {
public void testWrongPropsUsingLambdaWithoutClass() { public void testWrongPropsUsingLambdaWithoutClass() {
final Props p = TestActor.propsUsingLamda(17); final Props p = TestActor.propsUsingLamda(17);
assertEquals(TestActor.class, p.actorClass()); assertEquals(TestActor.class, p.actorClass());
try {
TestActor.propsUsingLamdaWithoutClass(17); IllegalArgumentException exception =
org.junit.Assert.fail("Should have detected lambda erasure, and thrown"); Assert.assertThrows(
} catch (IllegalArgumentException e) { "Should have detected lambda erasure, and thrown",
assertEquals( IllegalArgumentException.class,
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", () -> TestActor.propsUsingLamdaWithoutClass(17));
e.getMessage()); assertEquals(
} "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
exception.getMessage());
} }
@Test @Test

View file

@ -15,18 +15,13 @@ import akka.util.ByteString;
import org.junit.Assert; import org.junit.Assert;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.FiniteDuration;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class OutputStreamSinkTest extends StreamTest { public class OutputStreamSinkTest extends StreamTest {
public OutputStreamSinkTest() { public OutputStreamSinkTest() {
@ -38,7 +33,7 @@ public class OutputStreamSinkTest extends StreamTest {
new AkkaJUnitActorSystemResource("OutputStreamSinkTest", Utils.UnboundedMailboxConfig()); new AkkaJUnitActorSystemResource("OutputStreamSinkTest", Utils.UnboundedMailboxConfig());
@Test @Test
public void mustSignalFailureViaFailingFuture() throws Exception { public void mustSignalFailureViaFailingFuture() {
final OutputStream os = final OutputStream os =
new OutputStream() { new OutputStream() {
@ -54,12 +49,16 @@ public class OutputStreamSinkTest extends StreamTest {
final CompletionStage<IOResult> resultFuture = final CompletionStage<IOResult> resultFuture =
Source.single(ByteString.fromString("123456")) Source.single(ByteString.fromString("123456"))
.runWith(StreamConverters.fromOutputStream(() -> os), system); .runWith(StreamConverters.fromOutputStream(() -> os), system);
try {
resultFuture.toCompletableFuture().get(3, TimeUnit.SECONDS); ExecutionException exception =
Assert.fail("expected IOIncompleteException"); Assert.assertThrows(
} catch (ExecutionException e) { "CompletableFuture.get() should throw ExecutionException",
Assert.assertEquals(e.getCause().getClass(), IOOperationIncompleteException.class); ExecutionException.class,
Assert.assertEquals(e.getCause().getCause().getMessage(), "Can't accept more data."); () -> resultFuture.toCompletableFuture().get(3, TimeUnit.SECONDS));
} assertEquals(
"The cause of ExecutionException should be IOOperationIncompleteException",
exception.getCause().getClass(),
IOOperationIncompleteException.class);
assertEquals(exception.getCause().getCause().getMessage(), "Can't accept more data.");
} }
} }