Use assertThrows in classes ActorCreationTest and OutputStreamSinkTest (#30165)
This commit is contained in:
parent
73d74dfc69
commit
1a36631dbb
2 changed files with 56 additions and 57 deletions
|
|
@ -10,6 +10,7 @@ import static java.util.stream.Collectors.toCollection;
|
|||
import java.util.ArrayList;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.japi.Creator;
|
||||
|
|
@ -196,14 +197,11 @@ public class ActorCreationTest extends JUnitSuite {
|
|||
@SuppressWarnings("unchecked")
|
||||
@Deprecated
|
||||
public void testWrongErasedStaticCreator() {
|
||||
try {
|
||||
Props.create(new G());
|
||||
assert false;
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals(
|
||||
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
|
||||
e.getMessage());
|
||||
}
|
||||
IllegalArgumentException exception =
|
||||
Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(new G()));
|
||||
assertEquals(
|
||||
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
|
||||
exception.getMessage());
|
||||
Props.create(AbstractActor.class, new G());
|
||||
}
|
||||
|
||||
|
|
@ -217,14 +215,16 @@ public class ActorCreationTest extends JUnitSuite {
|
|||
@Test
|
||||
@Deprecated
|
||||
public void testWrongAnonymousClassStaticCreator() {
|
||||
try {
|
||||
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(
|
||||
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
|
||||
e.getMessage());
|
||||
}
|
||||
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
|
||||
});
|
||||
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
|
||||
|
|
@ -257,14 +257,11 @@ public class ActorCreationTest extends JUnitSuite {
|
|||
|
||||
@Test
|
||||
public void testWrongAbstractActorClass() {
|
||||
try {
|
||||
Props.create(H.class, "a");
|
||||
assert false;
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals(
|
||||
String.format("Actor class [%s] must not be abstract", H.class.getName()),
|
||||
e.getMessage());
|
||||
}
|
||||
IllegalArgumentException exception =
|
||||
Assert.assertThrows(IllegalArgumentException.class, () -> Props.create(H.class, "a"));
|
||||
assertEquals(
|
||||
String.format("Actor class [%s] must not be abstract", H.class.getName()),
|
||||
exception.getMessage());
|
||||
}
|
||||
|
||||
private static Creator<AbstractActor> createAnonymousCreatorInStaticMethod() {
|
||||
|
|
@ -294,17 +291,19 @@ public class ActorCreationTest extends JUnitSuite {
|
|||
@Test
|
||||
@Deprecated
|
||||
public void testAnonymousClassCreatorWithArguments() {
|
||||
try {
|
||||
final Creator<AbstractActor> anonymousCreatorFromStaticMethod = new P("hello") {
|
||||
// captures enclosing class
|
||||
};
|
||||
Props.create(anonymousCreatorFromStaticMethod);
|
||||
org.junit.Assert.fail("Should have detected this is not a real static class, and thrown");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals(
|
||||
"cannot use non-static local Creator to create actors; make it static (e.g. local to a static method) or top-level",
|
||||
e.getMessage());
|
||||
}
|
||||
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") {
|
||||
// captures enclosing class
|
||||
};
|
||||
Props.create(anonymousCreatorFromStaticMethod);
|
||||
});
|
||||
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
|
||||
|
|
@ -318,14 +317,15 @@ public class ActorCreationTest extends JUnitSuite {
|
|||
public void testWrongPropsUsingLambdaWithoutClass() {
|
||||
final Props p = TestActor.propsUsingLamda(17);
|
||||
assertEquals(TestActor.class, p.actorClass());
|
||||
try {
|
||||
TestActor.propsUsingLamdaWithoutClass(17);
|
||||
org.junit.Assert.fail("Should have detected lambda erasure, and thrown");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals(
|
||||
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
IllegalArgumentException exception =
|
||||
Assert.assertThrows(
|
||||
"Should have detected lambda erasure, and thrown",
|
||||
IllegalArgumentException.class,
|
||||
() -> TestActor.propsUsingLamdaWithoutClass(17));
|
||||
assertEquals(
|
||||
"erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
|
||||
exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -15,18 +15,13 @@ import akka.util.ByteString;
|
|||
import org.junit.Assert;
|
||||
import org.junit.ClassRule;
|
||||
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.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class OutputStreamSinkTest extends StreamTest {
|
||||
public OutputStreamSinkTest() {
|
||||
|
|
@ -38,7 +33,7 @@ public class OutputStreamSinkTest extends StreamTest {
|
|||
new AkkaJUnitActorSystemResource("OutputStreamSinkTest", Utils.UnboundedMailboxConfig());
|
||||
|
||||
@Test
|
||||
public void mustSignalFailureViaFailingFuture() throws Exception {
|
||||
public void mustSignalFailureViaFailingFuture() {
|
||||
|
||||
final OutputStream os =
|
||||
new OutputStream() {
|
||||
|
|
@ -54,12 +49,16 @@ public class OutputStreamSinkTest extends StreamTest {
|
|||
final CompletionStage<IOResult> resultFuture =
|
||||
Source.single(ByteString.fromString("123456"))
|
||||
.runWith(StreamConverters.fromOutputStream(() -> os), system);
|
||||
try {
|
||||
resultFuture.toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
Assert.fail("expected IOIncompleteException");
|
||||
} catch (ExecutionException e) {
|
||||
Assert.assertEquals(e.getCause().getClass(), IOOperationIncompleteException.class);
|
||||
Assert.assertEquals(e.getCause().getCause().getMessage(), "Can't accept more data.");
|
||||
}
|
||||
|
||||
ExecutionException exception =
|
||||
Assert.assertThrows(
|
||||
"CompletableFuture.get() should throw ExecutionException",
|
||||
ExecutionException.class,
|
||||
() -> 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.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue