下面列出了怎么用org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void handleMessage(Message message, LanguageServer languageServer, URI rootUri) {
if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage) message;
if (responseMessage.getResult() instanceof InitializeResult) {
Map<String, Object> htmlOptions = new HashMap<>();
Map<String, Object> validateOptions = new HashMap<>();
validateOptions.put("scripts", true);
validateOptions.put("styles", true);
htmlOptions.put("validate", validateOptions);
htmlOptions.put("format", Collections.singletonMap("enable", Boolean.TRUE));
Map<String, Object> html = new HashMap<>();
html.put("html", htmlOptions);
DidChangeConfigurationParams params = new DidChangeConfigurationParams(html);
languageServer.getWorkspaceService().didChangeConfiguration(params);
}
}
}
@Test public void testRequest() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp);
endpoint.consume(new RequestMessage() {{
setId("1");
setMethod("foo");
setParams("myparam");
}});
Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next();
entry.getValue().complete("success");
assertEquals("foo", entry.getKey().getMethod());
assertEquals("myparam", entry.getKey().getParams());
assertEquals("success", ((ResponseMessage)consumer.messages.get(0)).getResult());
}
@Test public void testCancellation() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp);
endpoint.consume(new RequestMessage() {{
setId("1");
setMethod("foo");
setParams("myparam");
}});
Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next();
entry.getValue().cancel(true);
ResponseMessage message = (ResponseMessage) consumer.messages.get(0);
assertNotNull(message);
ResponseError error = message.getError();
assertNotNull(error);
assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue());
assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled");
}
@SuppressWarnings({ "unchecked" })
@Test
public void testParseList() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<List<? extends Entry>>() {}.getType(),
new TypeToken<List<? extends Entry>>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id)->"foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ " \"body\": [\n"
+ " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n"
+ " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n"
+ " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n"
+ " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n"
+ " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n"
+ "]}");
List<? extends Entry> result = (List<? extends Entry>) ((ResponseMessage)message).getResult();
Assert.assertEquals(5, result.size());
for (Entry e : result) {
Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria"));
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testParseList_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Set<Entry>>() {}.getType(),
new TypeToken<Set<Entry>>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id)->"foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ " \"body\": [\n"
+ " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n"
+ " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n"
+ " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n"
+ " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n"
+ " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n"
+ "]}");
Set<Entry> result = (Set<Entry>) ((ResponseMessage)message).getResult();
Assert.assertEquals(5, result.size());
for (Entry e : result) {
Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria"));
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<Integer, Map<String,String>>>() {}.getType(),
new TypeToken<Object>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ "\"body\": 2\n"
+ "}");
Either<Integer, List<Map<String, String>>> result = (Either<Integer, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isLeft());
Assert.assertEquals(Integer.valueOf(2), result.getLeft());
}
@Test
public void testNormalResponse_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Location>() {}.getType(),
new TypeToken<Void>() {
}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"seq\":2",
"\"type\":\"response\"",
"\"request_seq\":5",
"\"success\":true",
"\"body\": {\"uri\": \"dummy://mymodel.mydsl\"}"
};
testAllPermutations(properties, json -> {
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
Object result = message.getResult();
Class<? extends Object> class1 = result.getClass();
Assert.assertEquals(Location.class, class1);
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri);
Assert.assertNull(message.getError());
});
}
@Test
public void testNormalResponseExtraFields_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Location>() {}.getType(),
new TypeToken<Void>() {
}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"seq\":2",
"\"type\":\"response\"",
"\"request_seq\":5",
"\"success\":true",
"\"body\": {\"uri\": \"dummy://mymodel.mydsl\"}",
"\"message\": null"
};
testAllPermutations(properties, json -> {
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
Object result = message.getResult();
Class<? extends Object> class1 = result.getClass();
Assert.assertEquals(Location.class, class1);
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri);
Assert.assertNull(message.getError());
});
}
@Test
public void testMissingSuccessResponse_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Location>() {}.getType(),
new TypeToken<Void>() {
}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"seq\":2",
"\"type\":\"response\"",
"\"request_seq\":5",
"\"message\": \"failed\"",
"\"body\": {\"uri\": \"failed\"}"
};
testAllPermutations(properties, json -> {
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
Assert.assertEquals("failed", message.getError().getMessage());
Object data = message.getError().getData();
Map<String, String> expected = new HashMap<>();
expected.put("uri", "failed");
Assert.assertEquals(expected, data);
Assert.assertNull(message.getResult());
});
}
protected void handleResponse(ResponseMessage responseMessage) {
PendingRequestInfo requestInfo;
synchronized (sentRequestMap) {
requestInfo = sentRequestMap.remove(responseMessage.getId());
}
if (requestInfo == null) {
// We have no pending request information that matches the id given in the response
LOG.log(Level.WARNING, "Unmatched response message: " + responseMessage);
} else if (responseMessage.getError() != null) {
// The remote service has replied with an error
requestInfo.future.completeExceptionally(new ResponseErrorException(responseMessage.getError()));
} else {
// The remote service has replied with a result object
requestInfo.future.complete(responseMessage.getResult());
}
}
@Override
public void handle(Message message, List<MessageIssue> issues) {
if (issues.isEmpty()) {
throw new IllegalArgumentException("The list of issues must not be empty.");
}
if (message instanceof RequestMessage) {
RequestMessage requestMessage = (RequestMessage) message;
handleRequestIssues(requestMessage, issues);
} else if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage) message;
handleResponseIssues(responseMessage, issues);
} else {
logIssues(message, issues);
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testParseList_01() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<List<? extends Entry>>() {}.getType(),
new TypeToken<List<? extends Entry>>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id)->"foo");
Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ " \"result\": [\n"
+ " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n"
+ " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n"
+ " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n"
+ " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n"
+ " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n"
+ "]}");
List<? extends Entry> result = (List<? extends Entry>) ((ResponseMessage) message).getResult();
Assert.assertEquals(5, result.size());
for (Entry e : result) {
Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria"));
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testParseList_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Set<Entry>>() {}.getType(),
new TypeToken<Set<Entry>>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id)->"foo");
Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ " \"result\": [\n"
+ " {\"name\":\"$schema\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":1,\"character\":3},\"end\":{\"line\":1,\"character\":55}}}},\n"
+ " {\"name\":\"type\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":2,\"character\":3},\"end\":{\"line\":2,\"character\":19}}}},\n"
+ " {\"name\":\"title\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":3,\"character\":50}}}},\n"
+ " {\"name\":\"additionalProperties\",\"kind\":17,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":4,\"character\":4},\"end\":{\"line\":4,\"character\":32}}}},\n"
+ " {\"name\":\"properties\",\"kind\":15,\"location\":{\"uri\":\"file:/home/mistria/runtime-EclipseApplication-with-patch/EclipseConEurope/something.json\",\"range\":{\"start\":{\"line\":5,\"character\":3},\"end\":{\"line\":5,\"character\":20}}}}\n"
+ "]}");
Set<Entry> result = (Set<Entry>) ((ResponseMessage)message).getResult();
Assert.assertEquals(5, result.size());
for (Entry e : result) {
Assert.assertTrue(e.location.uri, e.location.uri.startsWith("file:/home/mistria"));
}
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<MyEnum, Map<String,String>>>() {}.getType(),
new TypeToken<Object>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"result\": 2\n"
+ "}");
Either<MyEnum, List<Map<String, String>>> result = (Either<MyEnum, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isLeft());
Assert.assertEquals(MyEnum.B, result.getLeft());
}
@Test
public void testResponseErrorData() {
MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap());
ResponseMessage message = (ResponseMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"error\": { \"code\": -32001, \"message\": \"foo\",\n"
+ " \"data\": { \"uri\": \"file:/foo\", \"version\": 5, \"list\": [\"a\", \"b\", \"c\"] }\n"
+ " }\n"
+ "}");
ResponseError error = message.getError();
Assert.assertTrue("Expected a JsonObject in error.data", error.getData() instanceof JsonObject);
JsonObject data = (JsonObject) error.getData();
Assert.assertEquals("file:/foo", data.get("uri").getAsString());
Assert.assertEquals(5, data.get("version").getAsInt());
JsonArray list = data.get("list").getAsJsonArray();
Assert.assertEquals("a", list.get(0).getAsString());
Assert.assertEquals("b", list.get(1).getAsString());
Assert.assertEquals("c", list.get(2).getAsString());
}
@Test
public void testNormalResponse_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Location>() {}.getType(),
new TypeToken<Void>() {
}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"jsonrpc\":\"2.0\"",
"\"id\":2",
"\"result\": {\"uri\": \"dummy://mymodel.mydsl\"}"
};
testAllPermutations(properties, json -> {
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
Object result = message.getResult();
Class<? extends Object> class1 = result.getClass();
Assert.assertEquals(Location.class, class1);
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri);
Assert.assertNull(message.getError());
});
}
@Test
public void testErrorResponse_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Location>() {}.getType(),
new TypeToken<Void>() {
}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"jsonrpc\":\"2.0\"",
"\"id\":2",
"\"message\": \"failed\"",
"\"error\": {\"code\": 123456, \"message\": \"failed\", \"data\": {\"uri\": \"failed\"}}"
};
testAllPermutations(properties, json -> {
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
Assert.assertEquals("failed", message.getError().getMessage());
Object data = message.getError().getData();
JsonObject expected = new JsonObject();
expected.addProperty("uri", "failed");
Assert.assertEquals(expected, data);
Assert.assertNull(message.getResult());
});
}
@Test
public void testRequest1() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);
endpoint.consume(init(new RequestMessage(), it -> {
it.setId("1");
it.setMethod("foo");
it.setParams("myparam");
}));
Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next();
entry.getValue().complete("success");
assertEquals("foo", entry.getKey().getMethod());
assertEquals("myparam", entry.getKey().getParams());
ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0);
assertEquals("success", responseMessage.getResult());
assertEquals(Either.forLeft("1"), responseMessage.getRawId());
}
@Test
public void testRequest2() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);
endpoint.consume(init(new RequestMessage(), it -> {
it.setId(1);
it.setMethod("foo");
it.setParams("myparam");
}));
Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next();
entry.getValue().complete("success");
assertEquals("foo", entry.getKey().getMethod());
assertEquals("myparam", entry.getKey().getParams());
ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0);
assertEquals("success", responseMessage.getResult());
assertEquals(Either.forRight(1), responseMessage.getRawId());
}
@Test
public void testHandleRequestIssues() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);
endpoint.handle(init(new RequestMessage(), it -> {
it.setId("1");
it.setMethod("foo");
it.setParams("myparam");
}), Collections.singletonList(new MessageIssue("bar")));
ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0);
assertNotNull(responseMessage.getError());
assertEquals("bar", responseMessage.getError().getMessage());
}
@Test
public void testCancellation() {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);
endpoint.consume(init(new RequestMessage(), it -> {
it.setId("1");
it.setMethod("foo");
it.setParams("myparam");
}));
Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next();
entry.getValue().cancel(true);
ResponseMessage message = (ResponseMessage) consumer.messages.get(0);
assertNotNull(message);
ResponseError error = message.getError();
assertNotNull(error);
assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue());
assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled");
}
@Test
public void testExceptionInCompletableFuture() throws Exception {
TestEndpoint endp = new TestEndpoint();
TestMessageConsumer consumer = new TestMessageConsumer();
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);
CompletableFuture<Object> future = endpoint.request("foo", "myparam");
CompletableFuture<Void> chained = future.thenAccept(result -> {
throw new RuntimeException("BAAZ");
});
endpoint.consume(init(new ResponseMessage(), it -> {
it.setId("1");
it.setResult("Bar");
}));
try {
chained.get(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.fail("Expected an ExecutionException.");
} catch (ExecutionException exception) {
assertEquals("java.lang.RuntimeException: BAAZ", exception.getMessage());
}
}
private void handleMessage(Message message) {
if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage) message;
if (responseMessage.getResult() instanceof InitializeResult) {
listener.initialize(languageServer, (InitializeResult) responseMessage.getResult());
}
}
}
public void logMessage(Message message) {
if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage) message;
if (responseMessage.getError() != null && (responseMessage.getId()
.equals(Integer.toString(ResponseErrorCode.RequestCancelled.getValue())))) {
LOG.error(new ResponseErrorException(responseMessage.getError()));
}
}
}
private void logMessage(Message message) {
if (message instanceof ResponseMessage && ((ResponseMessage) message).getError() != null
&& ((ResponseMessage) message).getId()
.equals(Integer.toString(ResponseErrorCode.RequestCancelled.getValue()))) {
ResponseMessage responseMessage = (ResponseMessage) message;
LOGGER.warn("", new ResponseErrorException(responseMessage.getError()));
} else if (LOGGER.isDebugEnabled()) {
LOGGER.info(message.getClass().getSimpleName() + '\n' + message.toString());
}
}
@Override
public void handleMessage(Message message, LanguageServer languageServer, URI rootUri) {
if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage)message;
if (responseMessage.getResult() instanceof InitializeResult) {
// enable validation: so far, no better way found than changing conf after init.
DidChangeConfigurationParams params = new DidChangeConfigurationParams(getInitializationOptions(rootUri));
languageServer.getWorkspaceService().didChangeConfiguration(params);
}
}
}
@Override
public void handleMessage(Message message, LanguageServer languageServer, URI rootUri) {
if (message instanceof ResponseMessage) {
ResponseMessage responseMessage = (ResponseMessage) message;
if (responseMessage.getResult() instanceof InitializeResult) {
// Send json/schemaAssociations notification to register JSON Schema on JSON
// Language server side.
JSonLanguageServerInterface server = (JSonLanguageServerInterface) languageServer;
Map<String, List<String>> schemaAssociations = getSchemaAssociations();
server.sendJSonchemaAssociations(schemaAssociations);
}
}
}
@Test
public void testInvalidCodeLens() {
ResponseMessage message = new ResponseMessage();
message.setId("1");
CodeLens codeLens = new CodeLens(new Range(new Position(3, 32), new Position(3, 35)), null, null);
// forbidden self reference!
codeLens.setData(codeLens);
message.setResult(codeLens);
assertIssues(message, "An element of the message has a direct or indirect reference to itself.");
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_01() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<String, List<Map<String,String>>>>() {}.getType(),
new TypeToken<Either<String, Integer>>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ " \"body\": [\n"
+ " {\"name\":\"foo\"},\n"
+ " {\"name\":\"bar\"}\n"
+ "]}");
Either<String, List<Map<String, String>>> result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isRight());
for (Map<String, String> e : result.getRight()) {
Assert.assertNotNull(e.get("name"));
}
message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ "\"body\": \"name\"\n"
+ "}");
result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
Assert.assertFalse(result.isRight());
Assert.assertEquals("name",result.getLeft());
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_04() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<MyClass, List<? extends MyClass>>>() {}.getType(),
new TypeToken<Object>() {}.getType()));
DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
Message message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ "\"body\": {\n"
+ " value:\"foo\"\n"
+ "}}");
Either<MyClass, List<? extends MyClass>> result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isLeft());
Assert.assertEquals("foo", result.getLeft().getValue());
message = handler.parseMessage("{"
+ "\"seq\":2,\n"
+ "\"type\":\"response\",\n"
+ "\"success\":true,\n"
+ "\"body\": [{\n"
+ " value:\"bar\"\n"
+ "}]}");
result = (Either<MyClass, List<? extends MyClass>>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isRight());
Assert.assertEquals("bar", result.getRight().get(0).getValue());
}