diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDslTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDslTest.java index 570171a9cc..b620eb88d7 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDslTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDslTest.java @@ -10,19 +10,19 @@ import akka.japi.Pair; import akka.stream.*; import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.AkkaSpec; +import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; -import scala.collection.Seq; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; import java.util.List; import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; public class GraphDslTest extends StreamTest { @@ -78,29 +78,33 @@ public class GraphDslTest extends StreamTest { @Test @SuppressWarnings("unused") public void demonstrateConnectErrors() { - try { - // #simple-graph - final RunnableGraph g = - RunnableGraph.fromGraph( - GraphDSL.create( - (b) -> { - final SourceShape source1 = - b.add(Source.from(Arrays.asList(1, 2, 3, 4, 5))); - final SourceShape source2 = - b.add(Source.from(Arrays.asList(1, 2, 3, 4, 5))); - final FanInShape2> zip = - b.add(Zip.create()); - b.from(source1).toInlet(zip.in0()); - b.from(source2).toInlet(zip.in1()); - return ClosedShape.getInstance(); - })); - // unconnected zip.out (!) => "The inlets [] and outlets [] must correspond to the inlets [] - // and outlets [ZipWith2.out]" - // #simple-graph - org.junit.Assert.fail("expected IllegalArgumentException"); - } catch (IllegalStateException e) { - assertTrue(e != null && e.getMessage() != null && e.getMessage().contains("ZipWith2.out")); - } + IllegalStateException exception = + Assert.assertThrows( + "expected IllegalStateException", + IllegalStateException.class, + () -> { + // #simple-graph + final RunnableGraph g = + RunnableGraph.fromGraph( + GraphDSL.create( + (b) -> { + final SourceShape source1 = + b.add(Source.from(Arrays.asList(1, 2, 3, 4, 5))); + final SourceShape source2 = + b.add(Source.from(Arrays.asList(1, 2, 3, 4, 5))); + final FanInShape2> zip = + b.add(Zip.create()); + b.from(source1).toInlet(zip.in0()); + b.from(source2).toInlet(zip.in1()); + return ClosedShape.getInstance(); + })); + // unconnected zip.out (!) => "The inlets [] and outlets [] must correspond to the + // inlets [] + // and outlets [ZipWith2.out]" + // #simple-graph + }); + assertNotNull(exception.getMessage()); + assertTrue(exception.getMessage().contains("ZipWith2.out")); } @Test @@ -112,7 +116,7 @@ public class GraphDslTest extends StreamTest { Flow.of(Integer.class).map(elem -> elem * 2); final RunnableGraph, CompletionStage>> g = - RunnableGraph., CompletionStage>>fromGraph( + RunnableGraph.fromGraph( GraphDSL.create( topHeadSink, // import this sink into the graph bottomHeadSink, // and this as well @@ -137,7 +141,7 @@ public class GraphDslTest extends StreamTest { public void demonstrateMatValue() throws Exception { // #graph-dsl-matvalue final Sink> foldSink = - Sink.fold( + Sink.fold( 0, (a, b) -> { return a + b; @@ -222,10 +226,9 @@ public class GraphDslTest extends StreamTest { public void canUseMapMaterializedValueOnGraphs() { Graph, NotUsed> srcGraph = Source.empty(); Graph, Pair> mappedMatValueSrcGraph = - Graph.mapMaterializedValue( - srcGraph, notUsed -> new Pair(notUsed, notUsed)); + Graph.mapMaterializedValue(srcGraph, notUsed -> new Pair<>(notUsed, notUsed)); Sink> snk = Sink.ignore(); Pair pair = Source.fromGraph(mappedMatValueSrcGraph).to(snk).run(system); - assertEquals(pair, new Pair(NotUsed.getInstance(), NotUsed.getInstance())); + assertEquals(pair, new Pair<>(NotUsed.getInstance(), NotUsed.getInstance())); } } diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java index db345ba5f8..5d9e412922 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java @@ -20,6 +20,8 @@ import akka.testkit.javadsl.EventFilter; import akka.testkit.javadsl.TestKit; import akka.util.ByteString; import static akka.util.ByteString.emptyByteString; + +import org.junit.Assert; import org.junit.ClassRule; import org.junit.Test; @@ -69,7 +71,7 @@ public class TcpTest extends StreamTest { } }); - final List testInput = new ArrayList(); + final List testInput = new ArrayList<>(); { for (char c = 'a'; c <= 'z'; c++) { @@ -127,22 +129,20 @@ public class TcpTest extends StreamTest { .occurrences(1) .intercept( () -> { - try { - binding - .to(echoHandler) - .run(system) - .toCompletableFuture() - .get(5, TimeUnit.SECONDS); - assertTrue("Expected BindFailedException, but nothing was reported", false); - } catch (ExecutionException e) { - if (e.getCause() instanceof BindFailedException) { - } // all good - else throw new AssertionError("failed", e); - // expected - b.unbind(); - } catch (Exception e) { - throw new AssertionError("failed", e); - } + ExecutionException executionException = + Assert.assertThrows( + "CompletableFuture.get() should throw ExecutionException", + ExecutionException.class, + () -> + binding + .to(echoHandler) + .run(system) + .toCompletableFuture() + .get(5, TimeUnit.SECONDS)); + assertTrue( + "The cause of ExecutionException should be instanceof BindFailedException", + executionException.getCause() instanceof BindFailedException); + b.unbind(); return null; }); } @@ -150,26 +150,27 @@ public class TcpTest extends StreamTest { } @Test - public void mustReportClientConnectFailure() throws Throwable { + public void mustReportClientConnectFailure() { final InetSocketAddress serverAddress = SocketUtil.notBoundServerAddress(); - try { - try { - Source.from(testInput) - .viaMat( - Tcp.get(system) - .outgoingConnection(serverAddress.getHostString(), serverAddress.getPort()), - Keep.right()) - .to(Sink.ignore()) - .run(system) - .toCompletableFuture() - .get(5, TimeUnit.SECONDS); - assertTrue("Expected StreamTcpException, but nothing was reported", false); - } catch (ExecutionException e) { - throw e.getCause(); - } - } catch (StreamTcpException e) { - // expected - } + ExecutionException executionException = + Assert.assertThrows( + "CompletableFuture.get() should throw ExecutionException", + ExecutionException.class, + () -> + Source.from(testInput) + .viaMat( + Tcp.get(system) + .outgoingConnection( + serverAddress.getHostString(), serverAddress.getPort()), + Keep.right()) + .to(Sink.ignore()) + .run(system) + .toCompletableFuture() + .get(5, TimeUnit.SECONDS)); + assertEquals( + "The cause of ExecutionException should be StreamTcpException", + StreamTcpException.class, + executionException.getCause().getClass()); } // compile only sample