Skip to content

Commit

Permalink
Updated tests to use lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
trinopoty committed Jan 9, 2019
1 parent 080ec80 commit 95bca58
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 837 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public void testSetAllowedCorsOrigins() {
};
options.setAllowedCorsOrigins(origins);

Arrays.sort(origins, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
Arrays.sort(origins, String::compareTo);

Assert.assertArrayEquals(origins, options.getAllowedCorsOrigins());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +24,7 @@ private static final class WebSocketConnectionStub extends EngineIoWebSocket {
private final Map<String, String> mQuery;

WebSocketConnectionStub() {
this(new HashMap<String, String>());
this(new HashMap<>());
}

WebSocketConnectionStub(Map<String, String> query) {
Expand Down Expand Up @@ -56,23 +54,20 @@ 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
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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "invalid");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "invalid");

return ParseQS.encode(queryMap);
}).when(request).getQueryString();

final HttpServletResponseImpl response = new HttpServletResponseImpl();
Expand All @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> 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();

Expand All @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");
queryMap.put("sid", ServerYeast.yeast());

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");
queryMap.put("sid", ServerYeast.yeast());

return ParseQS.encode(queryMap);
}).when(request).getQueryString();

final HttpServletResponseImpl response = new HttpServletResponseImpl();
Expand Down Expand Up @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> 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());

Expand All @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> 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());

Expand All @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> 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());

Expand All @@ -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<String, String> queryMap = new HashMap<>();
queryMap.put("transport", "polling");

return ParseQS.encode(queryMap);
}
Mockito.doAnswer(invocationOnMock -> {
HashMap<String, String> 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());

Expand All @@ -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<String, String> query) {
final HashMap<String, Object> 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;
}
}
Loading

0 comments on commit 95bca58

Please sign in to comment.