Skip to content

Commit

Permalink
Use assertThrows in classes ActorCreationTest and OutputStreamSinkTest (
Browse files Browse the repository at this point in the history
  • Loading branch information
Captain1653 committed May 5, 2021
1 parent 73d74df commit 1a36631
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
86 changes: 43 additions & 43 deletions akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.stream.IntStream;

import org.junit.Assert;
import org.junit.Test;

import akka.japi.Creator;
Expand Down Expand Up @@ -196,14 +197,11 @@ public Actor create() throws Exception {
@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());
}

Expand All @@ -217,14 +215,16 @@ public void testRightStaticCreator() {
@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
Expand Down Expand Up @@ -257,14 +257,11 @@ public void testRightStaticSuperinterface() {

@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() {
Expand Down Expand Up @@ -294,17 +291,19 @@ public void testClassCreatorWithArguments() {
@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
Expand All @@ -318,14 +317,15 @@ public void testRightPropsUsingLambda() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
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() {
Expand All @@ -38,7 +33,7 @@ public OutputStreamSinkTest() {
new AkkaJUnitActorSystemResource("OutputStreamSinkTest", Utils.UnboundedMailboxConfig());

@Test
public void mustSignalFailureViaFailingFuture() throws Exception {
public void mustSignalFailureViaFailingFuture() {

final OutputStream os =
new OutputStream() {
Expand All @@ -54,12 +49,16 @@ public void write(int data) {
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.");
}
}

0 comments on commit 1a36631

Please sign in to comment.