From 95bca58e25e4fd28ff68eefc93fafa10519fcb5e Mon Sep 17 00:00:00 2001 From: Trinopoty Biswas Date: Wed, 9 Jan 2019 16:23:46 +0530 Subject: [PATCH] Updated tests to use lambda --- .../engineio/parser/ServerParserTest.java | 334 +++++++----------- .../server/EngineIoServerOptionsTest.java | 7 +- .../engineio/server/EngineIoServerTest.java | 200 +++-------- .../engineio/server/EngineIoSocketTest.java | 264 +++++--------- .../server/HttpServletResponseImpl.java | 35 +- .../socket/engineio/server/TransportTest.java | 23 +- .../server/transport/PollingTest.java | 319 ++++++----------- .../server/transport/WebSocketTest.java | 78 ++-- 8 files changed, 423 insertions(+), 837 deletions(-) diff --git a/engine.io-server/src/test/java/io/socket/engineio/parser/ServerParserTest.java b/engine.io-server/src/test/java/io/socket/engineio/parser/ServerParserTest.java index 60d10c4..6158366 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/parser/ServerParserTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/parser/ServerParserTest.java @@ -30,21 +30,15 @@ public void testEncodePacket_string() { final Packet packet = new Packet<>(Packet.MESSAGE); packet.data = "Hello World"; - ServerParser.encodePacket(packet, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_string.js", packet.data, String.class); - assertEquals(result, data); - } + ServerParser.encodePacket(packet, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_string.js", packet.data, String.class); + assertEquals(result, data); }); packet.data = "Engine.IO"; - ServerParser.encodePacket(packet, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_string.js", packet.data, String.class); - assertEquals(result, data); - } + ServerParser.encodePacket(packet, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_string.js", packet.data, String.class); + assertEquals(result, data); }); } @@ -53,12 +47,9 @@ public void testEncodePacket_binary() { final Packet packet = new Packet<>(Packet.MESSAGE); packet.data = new byte[] { 1, 2, 3, 4, 5 }; - ServerParser.encodePacket(packet, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePacket_binary.js", packet.data, byte[].class); - assertArrayEquals(result, (byte[]) data); - } + ServerParser.encodePacket(packet, true, data -> { + byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePacket_binary.js", packet.data, byte[].class); + assertArrayEquals(result, (byte[]) data); }); } @@ -67,12 +58,9 @@ public void testEncodePacket_base64() { final Packet packet = new Packet<>(Packet.MESSAGE); packet.data = new byte[] { 1, 2, 3, 4, 5 }; - ServerParser.encodePacket(packet, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_base64.js", packet.data, String.class); - assertEquals(result, data); - } + ServerParser.encodePacket(packet, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePacket_base64.js", packet.data, String.class); + assertEquals(result, data); }); } @@ -81,12 +69,9 @@ public void testEncodePayload_string_empty() { final Packet[] packets = new Packet[0]; final JSONArray jsonArray = new JSONArray(); - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_string.js", jsonArray.toString(), String.class); - assertEquals(result, data); - } + ServerParser.encodePayload(packets, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_string.js", jsonArray.toString(), String.class); + assertEquals(result, data); }); } @@ -106,15 +91,13 @@ public void testEncodePayload_string() { packets[i] = packet; } - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_string.js", jsonArray.toString(), String.class); - assertEquals(result, data); - } + ServerParser.encodePayload(packets, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_string.js", jsonArray.toString(), String.class); + assertEquals(result, data); }); } + @SuppressWarnings("ConstantConditions") @Test public void testEncodePayload_binary_empty() { final byte[][] messages = new byte[][] { @@ -129,12 +112,9 @@ public void testEncodePayload_binary_empty() { packets[i] = packet; } - ServerParser.encodePayload(packets, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePayload_binary.js", jsonArray.toString(), byte[].class); - assertArrayEquals(result, ((String) data).getBytes()); - } + ServerParser.encodePayload(packets, true, data -> { + byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePayload_binary.js", jsonArray.toString(), byte[].class); + assertArrayEquals(result, ((String) data).getBytes()); }); } @@ -153,12 +133,9 @@ public void testEncodePayload_binary() { packets[i] = packet; } - ServerParser.encodePayload(packets, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePayload_binary.js", jsonArray.toString(), byte[].class); - assertArrayEquals(result, (byte[]) data); - } + ServerParser.encodePayload(packets, true, data -> { + byte[] result = runScriptAndGetOutput("src/test/resources/testEncodePayload_binary.js", jsonArray.toString(), byte[].class); + assertArrayEquals(result, (byte[]) data); }); } @@ -177,23 +154,15 @@ public void testEncodePayload_base64() { packets[i] = packet; } - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_base64.js", jsonArray.toString(), String.class); - assertEquals(result, data); - } + ServerParser.encodePayload(packets, false, data -> { + String result = runScriptAndGetOutput("src/test/resources/testEncodePayload_base64.js", jsonArray.toString(), String.class); + assertEquals(result, data); }); } @Test public void testEncodePayloadAsBinary_empty() { - ServerParser.encodePayloadAsBinary(new Packet[0], new Parser.EncodeCallback() { - @Override - public void call(byte[] data) { - assertArrayEquals(data, new byte[0]); - } - }); + ServerParser.encodePayloadAsBinary(new Packet[0], data -> assertArrayEquals(data, new byte[0])); } @Test @@ -213,53 +182,39 @@ public void testDecodePacket_error() { @Test public void testDecodePacket_string() { final Packet packetOriginal = new Packet<>(Packet.MESSAGE, "Engine.IO"); - ServerParser.encodePacket(packetOriginal, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - Packet packetDecoded = ServerParser.decodePacket(data); - assertEquals(Packet.MESSAGE, packetDecoded.type); - assertEquals(String.class, packetDecoded.data.getClass()); - assertEquals(packetOriginal.data, packetDecoded.data); - } + ServerParser.encodePacket(packetOriginal, false, data -> { + Packet packetDecoded = ServerParser.decodePacket(data); + assertEquals(Packet.MESSAGE, packetDecoded.type); + assertEquals(String.class, packetDecoded.data.getClass()); + assertEquals(packetOriginal.data, packetDecoded.data); }); } @Test public void testDecodePacket_binary() { final Packet packetOriginal = new Packet<>(Packet.MESSAGE, "Engine.IO".getBytes(StandardCharsets.UTF_8)); - ServerParser.encodePacket(packetOriginal, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - Packet packetDecoded = ServerParser.decodePacket(data); - assertEquals(Packet.MESSAGE, packetDecoded.type); - assertEquals(byte[].class, packetDecoded.data.getClass()); - assertArrayEquals(packetOriginal.data, (byte[]) packetDecoded.data); - } + ServerParser.encodePacket(packetOriginal, true, data -> { + Packet packetDecoded = ServerParser.decodePacket(data); + assertEquals(Packet.MESSAGE, packetDecoded.type); + assertEquals(byte[].class, packetDecoded.data.getClass()); + assertArrayEquals(packetOriginal.data, (byte[]) packetDecoded.data); }); } @Test public void testDecodePacket_base64() { final Packet packetOriginal = new Packet<>(Packet.MESSAGE, "Engine.IO".getBytes(StandardCharsets.UTF_8)); - ServerParser.encodePacket(packetOriginal, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - Packet packetDecoded = ServerParser.decodePacket(data); - assertEquals(Packet.MESSAGE, packetDecoded.type); - assertEquals(byte[].class, packetDecoded.data.getClass()); - assertArrayEquals(packetOriginal.data, (byte[]) packetDecoded.data); - } + ServerParser.encodePacket(packetOriginal, false, data -> { + Packet packetDecoded = ServerParser.decodePacket(data); + assertEquals(Packet.MESSAGE, packetDecoded.type); + assertEquals(byte[].class, packetDecoded.data.getClass()); + assertArrayEquals(packetOriginal.data, (byte[]) packetDecoded.data); }); } @Test(expected = IllegalArgumentException.class) public void testDecodePayload_error() { - ServerParser.decodePayload("abcxyz", new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - return false; - } - }); + ServerParser.decodePayload("abcxyz", (packet, index, total) -> false); } @Test @@ -270,23 +225,17 @@ public void testDecodePayload_string() { }; packets[0].data = "Engine.IO"; packets[1].data = "Test.Data"; - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - assertEquals(String.class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - assertEquals(originalPacket.data, packet.data); - - return true; - } - }); - } + ServerParser.encodePayload(packets, false, data -> { + assertEquals(String.class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + assertEquals(originalPacket.data, packet.data); + + return true; + }); }); } @@ -298,24 +247,17 @@ public void testDecodePayload_binary() { }; packets[0].data = "Engine.IO".getBytes(StandardCharsets.UTF_8); packets[1].data = "Test.Data".getBytes(StandardCharsets.UTF_8); - ServerParser.encodePayload(packets, true, new Parser.EncodeCallback() { - @SuppressWarnings("Duplicates") - @Override - public void call(Object data) { - assertEquals(byte[].class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); - - return true; - } - }); - } + ServerParser.encodePayload(packets, true, data -> { + assertEquals(byte[].class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); + + return true; + }); }); } @@ -327,24 +269,17 @@ public void testDecodePayload_base64() { }; packets[0].data = "Engine.IO".getBytes(StandardCharsets.UTF_8); packets[1].data = "Test.Data".getBytes(StandardCharsets.UTF_8); - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @SuppressWarnings("Duplicates") - @Override - public void call(Object data) { - assertEquals(String.class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); - - return true; - } - }); - } + ServerParser.encodePayload(packets, false, data -> { + assertEquals(String.class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); + + return true; + }); }); } @@ -356,29 +291,22 @@ public void testDecodePayload_mixed_binary() { }; packets[0].data = "Engine.IO"; packets[1].data = "Test.Data".getBytes(StandardCharsets.UTF_8); - ServerParser.encodePayload(packets, true, new Parser.EncodeCallback() { - @SuppressWarnings("Duplicates") - @Override - public void call(Object data) { - assertEquals(byte[].class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - - if (originalPacket.data instanceof byte[]) { - assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); - } else { - assertEquals(originalPacket.data, packet.data); - } - - return true; - } - }); - } + ServerParser.encodePayload(packets, true, data -> { + assertEquals(byte[].class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + + if (originalPacket.data instanceof byte[]) { + assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); + } else { + assertEquals(originalPacket.data, packet.data); + } + + return true; + }); }); } @@ -390,29 +318,22 @@ public void testDecodePayload_mixed_base64() { }; packets[0].data = "Engine.IO"; packets[1].data = "Test.Data".getBytes(StandardCharsets.UTF_8); - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @SuppressWarnings("Duplicates") - @Override - public void call(Object data) { - assertEquals(String.class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - - if (originalPacket.data instanceof byte[]) { - assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); - } else { - assertEquals(originalPacket.data, packet.data); - } - - return true; - } - }); - } + ServerParser.encodePayload(packets, false, data -> { + assertEquals(String.class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + + if (originalPacket.data instanceof byte[]) { + assertArrayEquals((byte[]) originalPacket.data, (byte[]) packet.data); + } else { + assertEquals(originalPacket.data, packet.data); + } + + return true; + }); }); } @@ -424,29 +345,23 @@ public void testDecodePayload_exit() { }; packets[0].data = "Engine.IO"; packets[1].data = "Test.Data"; - ServerParser.encodePayload(packets, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - assertEquals(String.class, data.getClass()); - - ServerParser.decodePayload(data, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - assertEquals(0, index); - - Packet originalPacket = packets[index]; - assertEquals(originalPacket.data.getClass(), packet.data.getClass()); - assertEquals(originalPacket.type, packet.type); - assertEquals(originalPacket.data, packet.data); - - return false; - } - }); - } + ServerParser.encodePayload(packets, false, data -> { + assertEquals(String.class, data.getClass()); + + ServerParser.decodePayload(data, (packet, index, total) -> { + assertEquals(0, index); + + Packet originalPacket = packets[index]; + assertEquals(originalPacket.data.getClass(), packet.data.getClass()); + assertEquals(originalPacket.type, packet.type); + assertEquals(originalPacket.data, packet.data); + + return false; + }); }); } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "ConstantConditions", "ResultOfMethodCallIgnored"}) private T runScriptAndGetOutput(String script, Object input, Class outputClass) { byte[] nodeInputBytes; if (input instanceof String) { @@ -472,7 +387,6 @@ private T runScriptAndGetOutput(String script, Object input, Class output InputStream processInputStream = process.getInputStream(); byte[] result = new byte[processInputStream.available()]; - //noinspection ResultOfMethodCallIgnored processInputStream.read(result); processInputStream.close(); diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerOptionsTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerOptionsTest.java index 7a2194c..640c0f0 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerOptionsTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerOptionsTest.java @@ -41,12 +41,7 @@ public void testSetAllowedCorsOrigins() { }; options.setAllowedCorsOrigins(origins); - Arrays.sort(origins, new Comparator() { - @Override - public int compare(String o1, String o2) { - return o1.compareTo(o2); - } - }); + Arrays.sort(origins, String::compareTo); Assert.assertArrayEquals(origins, options.getAllowedCorsOrigins()); } diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerTest.java index 4d5f6bf..aff391a 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoServerTest.java @@ -7,8 +7,6 @@ import org.json.JSONObject; import org.junit.Test; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @@ -26,7 +24,7 @@ private static final class WebSocketConnectionStub extends EngineIoWebSocket { private final Map mQuery; WebSocketConnectionStub() { - this(new HashMap()); + this(new HashMap<>()); } WebSocketConnectionStub(Map query) { @@ -56,8 +54,8 @@ public void testOptions() { EngineIoServer server = new EngineIoServer(EngineIoServerOptions.newFromDefault() .setPingInterval(1500) .setPingTimeout(1500)); - assertEquals(1500, server.getPingInterval()); - assertEquals(1500, server.getPingTimeout()); + assertEquals(1500, server.getOptions().getPingInterval()); + assertEquals(1500, server.getOptions().getPingTimeout()); } @Test @@ -65,14 +63,11 @@ public void testHandleRequest_unknown_transport() throws IOException { final EngineIoServer server = new EngineIoServer(); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "invalid"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "invalid"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -93,21 +88,13 @@ public void testHandleRequest_bad_handshake_method() throws IOException { final EngineIoServer server = new EngineIoServer(); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -127,15 +114,12 @@ public void testHandleRequest_unknown_sid() throws IOException { final EngineIoServer server = new EngineIoServer(); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - queryMap.put("sid", ServerYeast.yeast()); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + queryMap.put("sid", ServerYeast.yeast()); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -191,27 +175,14 @@ public void testCors_all() throws IOException { .setAllowedCorsOrigins(EngineIoServerOptions.ALLOWED_CORS_ORIGIN_ALL)); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return origin; - } - }).when(request).getHeader(Mockito.eq("Origin")); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> origin).when(request).getHeader(Mockito.eq("Origin")); final HttpServletResponseImpl response = Mockito.spy(new HttpServletResponseImpl()); @@ -228,27 +199,14 @@ public void testCors_none() throws IOException { .setAllowedCorsOrigins(EngineIoServerOptions.ALLOWED_CORS_ORIGIN_NONE)); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return origin; - } - }).when(request).getHeader(Mockito.eq("Origin")); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> origin).when(request).getHeader(Mockito.eq("Origin")); final HttpServletResponseImpl response = Mockito.spy(new HttpServletResponseImpl()); @@ -267,27 +225,14 @@ public void testCors_some1() throws IOException { })); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return origin; - } - }).when(request).getHeader(Mockito.eq("Origin")); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> origin).when(request).getHeader(Mockito.eq("Origin")); final HttpServletResponseImpl response = Mockito.spy(new HttpServletResponseImpl()); @@ -306,27 +251,14 @@ public void testCors_some2() throws IOException { })); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - HashMap queryMap = new HashMap<>(); - queryMap.put("transport", "polling"); - - return ParseQS.encode(queryMap); - } + Mockito.doAnswer(invocationOnMock -> { + HashMap queryMap = new HashMap<>(); + queryMap.put("transport", "polling"); + + return ParseQS.encode(queryMap); }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "http://www.example.org"; - } - }).when(request).getHeader(Mockito.eq("Origin")); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> "http://www.example.org").when(request).getHeader(Mockito.eq("Origin")); final HttpServletResponseImpl response = Mockito.spy(new HttpServletResponseImpl()); @@ -336,37 +268,19 @@ public Object answer(InvocationOnMock invocationOnMock) { .addHeader(Mockito.eq("Access-Control-Allow-Origin"), Mockito.eq(origin)); } + @SuppressWarnings("SuspiciousMethodCalls") private HttpServletRequest getConnectRequest(final Map query) { final HashMap attributes = new HashMap<>(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return ParseQS.encode(query); - } - }).when(request).getQueryString(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "GET"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final String name = invocationOnMock.getArgument(0); - final Object value = invocationOnMock.getArgument(1); - attributes.put(name, value); - return null; - } + Mockito.doAnswer(invocationOnMock -> ParseQS.encode(query)).when(request).getQueryString(); + Mockito.doAnswer(invocationOnMock -> "GET").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final String name = invocationOnMock.getArgument(0); + final Object value = invocationOnMock.getArgument(1); + attributes.put(name, value); + return null; }).when(request).setAttribute(Mockito.anyString(), Mockito.any()); - Mockito.doAnswer(new Answer() { - @SuppressWarnings("SuspiciousMethodCalls") - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return attributes.get(invocationOnMock.getArgument(0)); - } - }).when(request).getAttribute(Mockito.anyString()); + Mockito.doAnswer(invocationOnMock -> attributes.get(invocationOnMock.getArgument(0))).when(request).getAttribute(Mockito.anyString()); return request; } } \ No newline at end of file diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoSocketTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoSocketTest.java index 73f09a8..1234e88 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoSocketTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/EngineIoSocketTest.java @@ -9,8 +9,6 @@ import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -50,20 +48,17 @@ public void testInit() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = Mockito.spy(new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer())); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final List packetList = invocationOnMock.getArgument(0); - for (Packet packet : packetList) { - if (packet.type.equals(Packet.OPEN)) { - final JSONObject jsonObject = new JSONObject((String) packet.data); - Assert.assertTrue(jsonObject.has("sid")); - Assert.assertEquals(socket.getId(), jsonObject.getString("sid")); - } + Mockito.doAnswer(invocationOnMock -> { + final List packetList = invocationOnMock.getArgument(0); + for (Packet packet : packetList) { + if (packet.type.equals(Packet.OPEN)) { + final JSONObject jsonObject = new JSONObject((String) packet.data); + Assert.assertTrue(jsonObject.has("sid")); + Assert.assertEquals(socket.getId(), jsonObject.getString("sid")); } - return null; } - }).when(transport).send(Mockito.anyList()); + return null; + }).when(transport).send(Mockito.anyList()); socket.init(transport, null); @@ -75,7 +70,7 @@ public Object answer(InvocationOnMock invocationOnMock) { Mockito.verify(socket, Mockito.times(1)) .emit(Mockito.eq("drain")); Mockito.verify(transport, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } @Test @@ -87,23 +82,20 @@ public void testInit_initialPacket() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = Mockito.spy(new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer(options))); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final List packetList = invocationOnMock.getArgument(0); - for (Packet packet : packetList) { - if (packet.type.equals(Packet.MESSAGE)) { - Assert.assertEquals(initialPacket.data, packet.data); - } + Mockito.doAnswer(invocationOnMock -> { + final List packetList = invocationOnMock.getArgument(0); + for (Packet packet : packetList) { + if (packet.type.equals(Packet.MESSAGE)) { + Assert.assertEquals(initialPacket.data, packet.data); } - return null; } - }).when(transport).send(Mockito.anyList()); + return null; + }).when(transport).send(Mockito.anyList()); socket.init(transport, null); Mockito.verify(transport, Mockito.times(2)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } @Test @@ -121,7 +113,7 @@ public void testOnRequest() throws IOException { .onRequest(Mockito.eq(request), Mockito.eq(response)); } - @SuppressWarnings("Duplicates") + @SuppressWarnings({"Duplicates", "unchecked"}) @Test public void testSend() { final Transport transport = Mockito.spy(new StubTransport()); @@ -130,37 +122,28 @@ public void testSend() { final Packet packet = new Packet<>(Packet.MESSAGE, "TestMessage"); - Mockito.doAnswer(new Answer() { - @SuppressWarnings("unchecked") - @Override - public Object answer(InvocationOnMock invocationOnMock) { - List packets = (List) invocationOnMock.getArguments()[0]; - Assert.assertEquals(1, packets.size()); - Assert.assertEquals(packet, packets.get(0)); - return null; - } - }).when(transport).send(Mockito.anyList()); + Mockito.doAnswer(invocationOnMock -> { + List packets = (List) invocationOnMock.getArguments()[0]; + Assert.assertEquals(1, packets.size()); + Assert.assertEquals(packet, packets.get(0)); + return null; + }).when(transport).send(Mockito.anyList()); socket.send(packet); // +1 for init() Mockito.verify(transport, Mockito.times(2)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } - @SuppressWarnings("Duplicates") + @SuppressWarnings({"Duplicates", "unchecked"}) @Test public void testSend_delayed() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport, null); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return false; - } - }).when(transport).isWritable(); + Mockito.doAnswer(invocationOnMock -> false).when(transport).isWritable(); final Packet packet = new Packet<>(Packet.MESSAGE, "TestMessage"); @@ -168,29 +151,20 @@ public Object answer(InvocationOnMock invocationOnMock) { // +1 for init() Mockito.verify(transport, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return true; - } - }).when(transport).isWritable(); - Mockito.doAnswer(new Answer() { - @SuppressWarnings("unchecked") - @Override - public Object answer(InvocationOnMock invocationOnMock) { - List packets = (List) invocationOnMock.getArguments()[0]; - Assert.assertEquals(1, packets.size()); - Assert.assertEquals(packet, packets.get(0)); - return null; - } - }).when(transport).send(Mockito.anyList()); + Mockito.doAnswer(invocationOnMock -> true).when(transport).isWritable(); + Mockito.doAnswer(invocationOnMock -> { + List packets = (List) invocationOnMock.getArguments()[0]; + Assert.assertEquals(1, packets.size()); + Assert.assertEquals(packet, packets.get(0)); + return null; + }).when(transport).send(Mockito.anyList()); transport.emit("drain"); Mockito.verify(transport, Mockito.times(2)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } @@ -216,12 +190,7 @@ public void testClose_withData() { final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport, null); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return false; - } - }).when(transport).isWritable(); + Mockito.doAnswer(invocationOnMock -> false).when(transport).isWritable(); final Packet packet = new Packet<>(Packet.MESSAGE, "TestMessage"); @@ -230,21 +199,16 @@ public Object answer(InvocationOnMock invocationOnMock) { Assert.assertEquals(ReadyState.CLOSING, socket.getReadyState()); Mockito.verify(transport, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return true; - } - }).when(transport).isWritable(); + Mockito.doAnswer(invocationOnMock -> true).when(transport).isWritable(); transport.emit("drain"); Assert.assertEquals(ReadyState.CLOSING, socket.getReadyState()); // init() + send() Mockito.verify(transport, Mockito.times(2)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); Mockito.verify(transport, Mockito.times(1)) .close(); @@ -259,12 +223,7 @@ public void testCanUpgrade() { final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), server); socket.init(transport, null); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport).getName(); Assert.assertFalse(socket.canUpgrade(Polling.NAME)); Assert.assertTrue(socket.canUpgrade(WebSocket.NAME)); } @@ -348,13 +307,14 @@ public void testTransportPacket_message() { final String packetData = "TestMessage"; final Packet packet = new Packet<>(Packet.MESSAGE, packetData); - final Emitter.Listener messageListener = Mockito.spy(new Emitter.Listener() { - @Override - public void call(Object... args) { - Assert.assertEquals(1, args.length); - Assert.assertEquals(packetData, args[0]); - } - }); + final Emitter.Listener messageListener = Mockito.spy(Emitter.Listener.class); + Mockito.doAnswer(invocation -> { + final Object[] args = invocation.getArguments(); + Assert.assertEquals(1, args.length); + Assert.assertEquals(packetData, args[0]); + return null; + }).when(messageListener).call(Mockito.any()); + socket.on("message", messageListener); socket.on("data", messageListener); @@ -364,6 +324,7 @@ public void call(Object... args) { .call(Mockito.eq(packetData)); } + @SuppressWarnings("unchecked") @Test public void testTransportPacket_ping() { final Transport transport = Mockito.spy(new StubTransport()); @@ -375,22 +336,18 @@ public void testTransportPacket_ping() { final Emitter.Listener heartbeatListener = Mockito.mock(Emitter.Listener.class); socket.on("heartbeat", heartbeatListener); - Mockito.doAnswer(new Answer() { - @SuppressWarnings("unchecked") - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final Packet argPacket = ((List) invocationOnMock.getArgument(0)).get(0); - Assert.assertEquals(Packet.PONG, argPacket.type); - return null; - } - }).when(transport).send(Mockito.anyList()); + Mockito.doAnswer(invocationOnMock -> { + final Packet argPacket = ((List) invocationOnMock.getArgument(0)).get(0); + Assert.assertEquals(Packet.PONG, argPacket.type); + return null; + }).when(transport).send(Mockito.anyList()); transport.emit("packet", packet); Mockito.verify(heartbeatListener, Mockito.times(1)) .call(); Mockito.verify(transport, Mockito.times(2)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } @Test @@ -409,43 +366,30 @@ public void testTransportPacket_error() { Mockito.verify(closeListener, Mockito.times(1)) .call(Mockito.eq("parse error"), Mockito.isNull()); Mockito.verify(transport, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); } @Test public void testUpgrade_probe() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final List packetList = invocationOnMock.getArgument(0); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> { + final List packetList = invocationOnMock.getArgument(0); - Assert.assertEquals(1, packetList.size()); + Assert.assertEquals(1, packetList.size()); - final Packet packet = packetList.get(0); - Assert.assertEquals(Packet.PONG, packet.type); - Assert.assertEquals("probe", packet.data); + final Packet packet = packetList.get(0); + Assert.assertEquals(Packet.PONG, packet.type); + Assert.assertEquals("probe", packet.data); - return null; - } - }).when(transport2).send(Mockito.anyList()); + return null; + }).when(transport2).send(Mockito.anyList()); final Emitter.Listener upgradingListener = Mockito.mock(Emitter.Listener.class); socket.on("upgrading", upgradingListener); @@ -456,7 +400,7 @@ public Object answer(InvocationOnMock invocationOnMock) { transport2.emit("packet", new Packet<>(Packet.PING, "probe")); Mockito.verify(transport2, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); Mockito.verify(upgradingListener, Mockito.times(1)) .call(Mockito.eq(transport2)); } @@ -464,23 +408,13 @@ public Object answer(InvocationOnMock invocationOnMock) { @Test public void testUpgrade_upgrade() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); final Emitter.Listener upgradingListener = Mockito.mock(Emitter.Listener.class); socket.on("upgrading", upgradingListener); @@ -508,23 +442,13 @@ public Object answer(InvocationOnMock invocationOnMock) { @Test public void testUpgrade_invalid_handshake() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); socket.upgrade(transport2); Mockito.verify(transport2, Mockito.times(1)) @@ -539,23 +463,13 @@ public Object answer(InvocationOnMock invocationOnMock) { @Test public void testUpgrade_close() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); socket.upgrade(transport2); Mockito.verify(transport2, Mockito.times(1)) @@ -569,23 +483,13 @@ public Object answer(InvocationOnMock invocationOnMock) { @Test public void testUpgrade_error() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); socket.upgrade(transport2); Mockito.verify(transport2, Mockito.times(1)) @@ -599,23 +503,13 @@ public Object answer(InvocationOnMock invocationOnMock) { @Test public void testUpgrade_socketClose() { final Transport transport1 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return Polling.NAME; - } - }).when(transport1).getName(); + Mockito.doAnswer(invocationOnMock -> Polling.NAME).when(transport1).getName(); final EngineIoSocket socket = new EngineIoSocket(ServerYeast.yeast(), new EngineIoServer()); socket.init(transport1, null); final Transport transport2 = Mockito.spy(new StubTransport()); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return WebSocket.NAME; - } - }).when(transport2).getName(); + Mockito.doAnswer(invocationOnMock -> WebSocket.NAME).when(transport2).getName(); socket.upgrade(transport2); Mockito.verify(transport2, Mockito.times(1)) diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/HttpServletResponseImpl.java b/engine.io-server/src/test/java/io/socket/engineio/server/HttpServletResponseImpl.java index 3374340..ef14a40 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/HttpServletResponseImpl.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/HttpServletResponseImpl.java @@ -5,22 +5,21 @@ import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.util.ArrayList; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Locale; public final class HttpServletResponseImpl implements HttpServletResponse { private final ServletOutputStreamWrapper mServletOutputStreamWrapper = new ServletOutputStreamWrapper(); - private final ArrayList mCookieList = new ArrayList<>(); private int mStatus = HttpServletResponse.SC_OK; private PrintWriter mPrintWriter = null; @Override public void addCookie(Cookie cookie) { - mCookieList.add(cookie); } @Override @@ -30,22 +29,38 @@ public boolean containsHeader(String s) { @Override public String encodeURL(String s) { - return URLEncoder.encode(s); + try { + return URLEncoder.encode(s, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ignore) { + return null; + } } @Override public String encodeRedirectURL(String s) { - return URLEncoder.encode(s); + try { + return URLEncoder.encode(s, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ignore) { + return null; + } } @Override public String encodeUrl(String s) { - return URLEncoder.encode(s); + try { + return URLEncoder.encode(s, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ignore) { + return null; + } } @Override public String encodeRedirectUrl(String s) { - return URLEncoder.encode(s); + try { + return URLEncoder.encode(s, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ignore) { + return null; + } } @Override @@ -192,15 +207,11 @@ public Locale getLocale() { return null; } - public ArrayList getCookieList() { - return mCookieList; - } - public ByteArrayOutputStream getByteOutputStream() { return mServletOutputStreamWrapper.getByteStream(); } - public synchronized void flushWriterIfNecessary() { + synchronized void flushWriterIfNecessary() { if (mPrintWriter != null) { mPrintWriter.flush(); } diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/TransportTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/TransportTest.java index 12e180a..ceacccb 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/TransportTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/TransportTest.java @@ -1,8 +1,6 @@ package io.socket.engineio.server; -import io.socket.emitter.Emitter; import io.socket.engineio.parser.Packet; -import io.socket.engineio.parser.Parser; import io.socket.engineio.parser.ServerParser; import org.junit.Test; import org.mockito.Mockito; @@ -85,7 +83,6 @@ public void testInitialState() { assertEquals(ReadyState.OPEN, transport.getState()); } - @SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testClose_open() { final TestTransport transport = Mockito.spy(new TestTransport()); @@ -95,7 +92,6 @@ public void testClose_open() { assertEquals(ReadyState.CLOSING, transport.getState()); } - @SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testClose_closed() { final TestTransport transport = Mockito.spy(new TestTransport()); @@ -110,11 +106,7 @@ public void testClose_closed() { public void testOnError() { final TestTransport transport = Mockito.spy(new TestTransport()); - transport.on("error", new Emitter.Listener() { - @Override - public void call(Object... args) { - } - }); + transport.on("error", args -> { }); transport.onError("test", null); Mockito.verify(transport, Mockito.times(1)).emit("error", "test", null); } @@ -135,14 +127,11 @@ public void testOnData() { final TestTransport transport = Mockito.spy(new TestTransport()); final Packet testPacket = new Packet(Packet.MESSAGE, "test"); - ServerParser.encodePacket(testPacket, false, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - String packetData = (String) data; - - transport.onData(packetData); - Mockito.verify(transport, Mockito.times(1)).onPacket(Mockito.any(Packet.class)); - } + ServerParser.encodePacket(testPacket, false, data -> { + String packetData = (String) data; + + transport.onData(packetData); + Mockito.verify(transport, Mockito.times(1)).onPacket(Mockito.any(Packet.class)); }); } diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/transport/PollingTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/transport/PollingTest.java index 6979708..ab8372c 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/transport/PollingTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/transport/PollingTest.java @@ -1,15 +1,11 @@ package io.socket.engineio.server.transport; -import io.socket.emitter.Emitter; import io.socket.engineio.parser.Packet; -import io.socket.engineio.parser.Parser; import io.socket.engineio.parser.ServerParser; import io.socket.engineio.server.HttpServletResponseImpl; import io.socket.engineio.server.ServletInputStreamWrapper; import org.junit.Test; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayInputStream; @@ -41,19 +37,11 @@ public void testOnRequest_error() throws IOException { final Polling polling = Mockito.spy(new Polling()); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "DELETE"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } + Mockito.doAnswer(invocationOnMock -> "DELETE").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; }).when(request).getAttribute("query"); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -68,19 +56,11 @@ public void testOnRequest_poll() throws IOException { final Polling polling = Mockito.spy(new Polling()); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "GET"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } + Mockito.doAnswer(invocationOnMock -> "GET").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; }).when(request).getAttribute("query"); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -91,13 +71,10 @@ public Object answer(InvocationOnMock invocationOnMock) { .emit(Mockito.eq("drain")); final String responseString = new String(response.getByteOutputStream().toByteArray(), StandardCharsets.UTF_8); - ServerParser.decodePayload(responseString, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - assertEquals(1, total); - assertEquals(Packet.NOOP, packet.type); - return true; - } + ServerParser.decodePayload(responseString, (packet, index, total) -> { + assertEquals(1, total); + assertEquals(Packet.NOOP, packet.type); + return true; }); } @@ -106,67 +83,38 @@ public void testOnRequest_data() { final String messageData = "Test Data"; final Polling polling = Mockito.spy(new Polling()); - polling.on("packet", new Emitter.Listener() { - @Override - public void call(Object... args) { - final Packet packet = (Packet) args[0]; - assertEquals(Packet.MESSAGE, packet.type); - assertEquals(messageData, packet.data); - } + polling.on("packet", args -> { + final Packet packet = (Packet) args[0]; + assertEquals(Packet.MESSAGE, packet.type); + assertEquals(messageData, packet.data); }); final Packet requestPacket = new Packet<>(Packet.MESSAGE, messageData); - ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, new Parser.EncodeCallback() { - @Override - public void call(final byte[] data) { - final ByteArrayInputStream requestInputStream = new ByteArrayInputStream(data); - final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } - }).when(request).getAttribute("query"); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "application/octet-stream"; - } - }).when(request).getContentType(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return data.length; - } - }).when(request).getContentLength(); - try { - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return new ServletInputStreamWrapper(requestInputStream); - } - }).when(request).getInputStream(); - } catch (IOException ignore) { - } - - final HttpServletResponseImpl response = new HttpServletResponseImpl(); - - try { - polling.onRequest(request, response); - } catch (IOException ignore) { - } - - Mockito.verify(polling, Mockito.times(1)) - .emit(Mockito.eq("packet"), Mockito.any(Packet.class)); + ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, data -> { + final ByteArrayInputStream requestInputStream = new ByteArrayInputStream(data); + final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; + }).when(request).getAttribute("query"); + Mockito.doAnswer(invocationOnMock -> "application/octet-stream").when(request).getContentType(); + Mockito.doAnswer(invocationOnMock -> data.length).when(request).getContentLength(); + try { + Mockito.doAnswer(invocationOnMock -> new ServletInputStreamWrapper(requestInputStream)).when(request).getInputStream(); + } catch (IOException ignore) { } + + final HttpServletResponseImpl response = new HttpServletResponseImpl(); + + try { + polling.onRequest(request, response); + } catch (IOException ignore) { + } + + Mockito.verify(polling, Mockito.times(1)) + .emit(Mockito.eq("packet"), Mockito.any(Packet.class)); }); } @@ -175,57 +123,31 @@ public void testClose_client() { final Polling polling = Mockito.spy(new Polling()); final Packet requestPacket = new Packet<>(Packet.CLOSE); - ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, new Parser.EncodeCallback() { - @Override - public void call(final byte[] data) { - final ByteArrayInputStream requestInputStream = new ByteArrayInputStream(data); - final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "POST"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } - }).when(request).getAttribute("query"); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "application/octet-stream"; - } - }).when(request).getContentType(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return data.length; - } - }).when(request).getContentLength(); - try { - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return new ServletInputStreamWrapper(requestInputStream); - } - }).when(request).getInputStream(); - } catch (IOException ignore) { - } - - final HttpServletResponseImpl response = new HttpServletResponseImpl(); - - try { - polling.onRequest(request, response); - } catch (IOException ignore) { - } - - Mockito.verify(polling, Mockito.times(1)) - .emit(Mockito.eq("close")); + ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, data -> { + final ByteArrayInputStream requestInputStream = new ByteArrayInputStream(data); + final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; + }).when(request).getAttribute("query"); + Mockito.doAnswer(invocationOnMock -> "application/octet-stream").when(request).getContentType(); + Mockito.doAnswer(invocationOnMock -> data.length).when(request).getContentLength(); + try { + Mockito.doAnswer(invocationOnMock -> new ServletInputStreamWrapper(requestInputStream)).when(request).getInputStream(); + } catch (IOException ignore) { + } + + final HttpServletResponseImpl response = new HttpServletResponseImpl(); + + try { + polling.onRequest(request, response); + } catch (IOException ignore) { } + + Mockito.verify(polling, Mockito.times(1)) + .emit(Mockito.eq("close")); }); } @@ -234,19 +156,11 @@ public void testClose_server1() throws IOException { final Polling polling = Mockito.spy(new Polling()); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "GET"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } + Mockito.doAnswer(invocationOnMock -> "GET").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; }).when(request).getAttribute("query"); final HttpServletResponseImpl response = new HttpServletResponseImpl(); @@ -260,17 +174,14 @@ public Object answer(InvocationOnMock invocationOnMock) { .emit(Mockito.eq("close")); final String responseString = new String(response.getByteOutputStream().toByteArray(), StandardCharsets.UTF_8); - ServerParser.decodePayload(responseString, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - assertEquals(2, total); - if (index == 0) { - assertEquals(Packet.NOOP, packet.type); - } else if (index == 1) { - assertEquals(Packet.CLOSE, packet.type); - } - return true; + ServerParser.decodePayload(responseString, (packet, index, total) -> { + assertEquals(2, total); + if (index == 0) { + assertEquals(Packet.NOOP, packet.type); + } else if (index == 1) { + assertEquals(Packet.CLOSE, packet.type); } + return true; }); } @@ -278,56 +189,36 @@ public boolean call(Packet packet, int index, int total) { public void testClose_server2() { final Polling polling = Mockito.spy(new Polling()); - polling.on("drain", new Emitter.Listener() { - @Override - public void call(Object... args) { - polling.close(); - } - }); + polling.on("drain", args -> polling.close()); final Packet requestPacket = new Packet<>(Packet.CLOSE); - ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, new Parser.EncodeCallback() { - @SuppressWarnings("Duplicates") - @Override - public void call(final byte[] data) { - final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - return "GET"; - } - }).when(request).getMethod(); - Mockito.doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocationOnMock) { - final HashMap queryMap = new HashMap<>(); - queryMap.put("transport", Polling.NAME); - return queryMap; - } - }).when(request).getAttribute("query"); - - final HttpServletResponseImpl response = new HttpServletResponseImpl(); - - try { - polling.onRequest(request, response); - } catch (IOException ignore) { - } - - Mockito.verify(polling, Mockito.times(1)) - .emit(Mockito.eq("drain")); - Mockito.verify(polling, Mockito.times(1)) - .emit(Mockito.eq("close")); - - final String responseString = new String(response.getByteOutputStream().toByteArray(), StandardCharsets.UTF_8); - ServerParser.decodePayload(responseString, new Parser.DecodePayloadCallback() { - @Override - public boolean call(Packet packet, int index, int total) { - assertEquals(1, total); - assertEquals(Packet.CLOSE, packet.type); - return true; - } - }); + ServerParser.encodePayloadAsBinary(new Packet[]{requestPacket}, data -> { + final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + Mockito.doAnswer(invocationOnMock -> "GET").when(request).getMethod(); + Mockito.doAnswer(invocationOnMock -> { + final HashMap queryMap = new HashMap<>(); + queryMap.put("transport", Polling.NAME); + return queryMap; + }).when(request).getAttribute("query"); + + final HttpServletResponseImpl response = new HttpServletResponseImpl(); + + try { + polling.onRequest(request, response); + } catch (IOException ignore) { } + + Mockito.verify(polling, Mockito.times(1)) + .emit(Mockito.eq("drain")); + Mockito.verify(polling, Mockito.times(1)) + .emit(Mockito.eq("close")); + + final String responseString = new String(response.getByteOutputStream().toByteArray(), StandardCharsets.UTF_8); + ServerParser.decodePayload(responseString, (packet, index, total) -> { + assertEquals(1, total); + assertEquals(Packet.CLOSE, packet.type); + return true; + }); }); } } \ No newline at end of file diff --git a/engine.io-server/src/test/java/io/socket/engineio/server/transport/WebSocketTest.java b/engine.io-server/src/test/java/io/socket/engineio/server/transport/WebSocketTest.java index c302b00..6681a20 100644 --- a/engine.io-server/src/test/java/io/socket/engineio/server/transport/WebSocketTest.java +++ b/engine.io-server/src/test/java/io/socket/engineio/server/transport/WebSocketTest.java @@ -2,7 +2,6 @@ import io.socket.emitter.Emitter; import io.socket.engineio.parser.Packet; -import io.socket.engineio.parser.Parser; import io.socket.engineio.parser.ServerParser; import io.socket.engineio.server.EngineIoWebSocket; import org.junit.Test; @@ -86,16 +85,13 @@ public void testSend_string() { add(packet); }}); - Mockito.verify(webSocket, Mockito.times(1)).send(Mockito.anyList()); + Mockito.verify(webSocket, Mockito.times(1)).send(Mockito.anyList()); - ServerParser.encodePacket(packet, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - try { - Mockito.verify(webSocketConnection, Mockito.times(1)) - .write(Mockito.eq((String) data)); - } catch (IOException ignore) { - } + ServerParser.encodePacket(packet, true, data -> { + try { + Mockito.verify(webSocketConnection, Mockito.times(1)) + .write(Mockito.eq((String) data)); + } catch (IOException ignore) { } }); } @@ -112,16 +108,13 @@ public void testSend_binary() { add(packet); }}); - Mockito.verify(webSocket, Mockito.times(1)).send(Mockito.anyList()); + Mockito.verify(webSocket, Mockito.times(1)).send(Mockito.anyList()); - ServerParser.encodePacket(packet, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - try { - Mockito.verify(webSocketConnection, Mockito.times(1)) - .write(Mockito.eq((byte[]) data)); - } catch (IOException ignore) { - } + ServerParser.encodePacket(packet, true, data -> { + try { + Mockito.verify(webSocketConnection, Mockito.times(1)) + .write(Mockito.eq((byte[]) data)); + } catch (IOException ignore) { } }); } @@ -133,11 +126,7 @@ public void testSend_error() throws IOException { final WebSocket webSocket = Mockito.spy(new WebSocket(webSocketConnection)); Mockito.doThrow(new IOException()).when(webSocketConnection).write(Mockito.anyString()); - webSocket.on("error", new Emitter.Listener() { - @Override - public void call(Object... args) { - } - }); + webSocket.on("error", args -> { }); final String stringData = "Test string"; final Packet packet = new Packet(Packet.MESSAGE, stringData); @@ -146,7 +135,7 @@ public void call(Object... args) { }}); Mockito.verify(webSocket, Mockito.times(1)) - .send(Mockito.anyList()); + .send(Mockito.anyList()); Mockito.verify(webSocket, Mockito.times(1)) .emit(Mockito.eq("error"), Mockito.eq("write error"), Mockito.isNull()); } @@ -156,11 +145,7 @@ public void testConnection_close() { final EngineIoWebSocket webSocketConnection = new EngineIoWebSocketStub(); final WebSocket webSocket = Mockito.spy(new WebSocket(webSocketConnection)); - final Emitter.Listener closeListener = Mockito.spy(new Emitter.Listener() { - @Override - public void call(Object... args) { - } - }); + final Emitter.Listener closeListener = Mockito.spy(Emitter.Listener.class); webSocket.on("close", closeListener); webSocketConnection.emit("close"); @@ -173,11 +158,7 @@ public void testConnection_error() { final EngineIoWebSocket webSocketConnection = Mockito.spy(new EngineIoWebSocketStub()); final WebSocket webSocket = Mockito.spy(new WebSocket(webSocketConnection)); - final Emitter.Listener errorListener = Mockito.spy(new Emitter.Listener() { - @Override - public void call(Object... args) { - } - }); + final Emitter.Listener errorListener = Mockito.spy(Emitter.Listener.class); webSocket.on("error", errorListener); webSocketConnection.emit("error", "test", null); @@ -191,24 +172,21 @@ public void testConnection_message() { final WebSocket webSocket = Mockito.spy(new WebSocket(webSocketConnection)); final Packet packet = new Packet<>(Packet.MESSAGE, "Test Message"); - final Emitter.Listener packetListener = Mockito.spy(new Emitter.Listener() { - @Override - public void call(Object... args) { - final Packet argPacket = (Packet) args[0]; - assertEquals(packet.type, argPacket.type); - assertEquals(packet.data, argPacket.data); - } - }); + final Emitter.Listener packetListener = Mockito.spy(Emitter.Listener.class); + Mockito.doAnswer(invocation -> { + Object[] args = invocation.getArguments(); + final Packet argPacket = (Packet) args[0]; + assertEquals(packet.type, argPacket.type); + assertEquals(packet.data, argPacket.data); + return null; + }).when(packetListener).call(Mockito.any()); webSocket.on("packet", packetListener); - ServerParser.encodePacket(packet, true, new Parser.EncodeCallback() { - @Override - public void call(Object data) { - webSocketConnection.emit("message", data); + ServerParser.encodePacket(packet, true, data -> { + webSocketConnection.emit("message", data); - Mockito.verify(packetListener, Mockito.times(1)) - .call(Mockito.any(Packet.class)); - } + Mockito.verify(packetListener, Mockito.times(1)) + .call(Mockito.any(Packet.class)); }); } } \ No newline at end of file