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;
} catch (IllegalArgumentException e) {
assertEquals( assertEquals(
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
e.getMessage()); exception.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 =
Assert.assertThrows(
"Should have detected this is not a real static class, and thrown",
IllegalArgumentException.class,
() -> {
Props.create(new C() {}); // has implicit reference to outer class Props.create(new C() {}); // has implicit reference to outer class
org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); });
} catch (IllegalArgumentException e) {
assertEquals( assertEquals(
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
e.getMessage()); 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;
} catch (IllegalArgumentException e) {
assertEquals( assertEquals(
String.format("Actor class [%s] must not be abstract", H.class.getName()), String.format("Actor class [%s] must not be abstract", H.class.getName()),
e.getMessage()); exception.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 =
Assert.assertThrows(
"Should have detected this is not a real static class, and thrown",
IllegalArgumentException.class,
() -> {
final Creator<AbstractActor> anonymousCreatorFromStaticMethod = new P("hello") { final Creator<AbstractActor> anonymousCreatorFromStaticMethod = new P("hello") {
// captures enclosing class // captures enclosing class
}; };
Props.create(anonymousCreatorFromStaticMethod); Props.create(anonymousCreatorFromStaticMethod);
org.junit.Assert.fail("Should have detected this is not a real static class, and thrown"); });
} catch (IllegalArgumentException e) {
assertEquals( assertEquals(
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level", "cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
e.getMessage()); 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",
IllegalArgumentException.class,
() -> TestActor.propsUsingLamdaWithoutClass(17));
assertEquals( assertEquals(
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", "erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
e.getMessage()); 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.");
} }
} }