下面列出了org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler#setMethodProvider ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testRawMultiParamsParsing_04() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<List<String>>() {}.getType(),
new TypeToken<List<Integer>>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"foo\",\n"
+ "\"params\": [[\"foo\", \"bar\"], [1, 2]]\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);
List<?> parameters = (List<?>) message.getParams();
Assert.assertEquals(3, parameters.size());
Assert.assertEquals("[foo, bar]", parameters.get(0).toString());
Assert.assertEquals("[1, 2]", parameters.get(1).toString());
Assert.assertNull(parameters.get(2));
}
@Test
public void testRequest_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {
}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"jsonrpc\":\"2.0\"",
"\"id\":2",
"\"method\":\"foo\"",
"\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}"
};
testAllPermutations(properties, json -> {
RequestMessage message = (RequestMessage) handler.parseMessage(json);
Object params = message.getParams();
Class<? extends Object> class1 = params.getClass();
Assert.assertEquals(Location.class, class1);
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri);
});
}
@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"));
}
}
@Test
public void testMultiParamsParsing_04() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<List<String>>() {}.getType(),
new TypeToken<List<Integer>>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"params\": [[\"foo\", \"bar\"], [1, 2]],\n"
+ "\"method\":\"foo\"\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);
List<?> parameters = (List<?>) message.getParams();
Assert.assertEquals(3, parameters.size());
Assert.assertEquals("[foo, bar]", parameters.get(0).toString());
Assert.assertEquals("[1, 2]", parameters.get(1).toString());
Assert.assertNull(parameters.get(2));
}
@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 testNotification_AllOrders() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {
}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
String[] properties = new String[] {
"\"jsonrpc\":\"2.0\"",
"\"method\":\"foo\"",
"\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}"
};
testAllPermutations(properties, json -> {
NotificationMessage message = (NotificationMessage) handler.parseMessage(json);
Object params = message.getParams();
Class<? extends Object> class1 = params.getClass();
Assert.assertEquals(Location.class, class1);
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri);
});
}
@Test
public void testEnumParamNull() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<List<MyEnum>>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"params\": [1, 2, null],\n"
+ "\"method\":\"foo\"\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);
List<?> parameters = (List<?>) message.getParams();
Assert.assertEquals(Arrays.asList(MyEnum.A, MyEnum.B, null),
parameters);
}
@Test
public void testParamsParsing_01() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"},\n"
+ "\"method\":\"foo\"\n"
+ "}");
Assert.assertEquals(Location.class, message.getParams().getClass());
}
@Test
public void testParamsParsing_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"foo\",\n"
+ "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}\n"
+ "}");
Assert.assertEquals(Location.class, message.getParams().getClass());
}
@Test
public void testParamsParsing_03() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"bar\",\n"
+ "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}\n"
+ "}");
Assert.assertEquals(JsonObject.class, message.getParams().getClass());
}
@Test
public void testParamsParsing_04() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"bar\",\n"
+ "\"params\": null\n"
+ "}");
Assert.assertEquals(null, message.getParams());
}
@Test
public void testMultiParamsParsing_03() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<List<String>>() {}.getType(),
new TypeToken<List<Integer>>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"params\": [[\"foo\", \"bar\"], [1, 2], {\"uri\": \"dummy://mymodel.mydsl\"}],\n"
+ "\"method\":\"foo\"\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);
List<?> parameters = (List<?>) message.getParams();
Assert.assertEquals(3, parameters.size());
Assert.assertEquals("[foo, bar]", parameters.get(0).toString());
Assert.assertEquals("[1, 2]", parameters.get(1).toString());
Assert.assertTrue("" + parameters.get(2).getClass(), parameters.get(2) instanceof Location);
}
@Test
public void testRawMultiParamsParsing_02() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<String>() {}.getType(),
new TypeToken<Integer>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"bar\",\n"
+ "\"params\": [\"foo\", 2]\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof JsonArray);
}
@Test
public void testRawMultiParamsParsing_03() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Void>() {}.getType(),
new TypeToken<List<String>>() {}.getType(),
new TypeToken<List<Integer>>() {}.getType(),
new TypeToken<Location>() {}.getType()));
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
handler.setMethodProvider((id) -> "foo");
RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"method\":\"foo\",\n"
+ "\"params\": [[\"foo\", \"bar\"], [1, 2], {\"uri\": \"dummy://mymodel.mydsl\"}]\n"
+ "}");
Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);
List<?> parameters = (List<?>) message.getParams();
Assert.assertEquals(3, parameters.size());
Assert.assertEquals("[foo, bar]", parameters.get(0).toString());
Assert.assertEquals("[1, 2]", parameters.get(1).toString());
Assert.assertTrue("" + parameters.get(2).getClass(), parameters.get(2) instanceof Location);
}
@Override
protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) {
MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler);
outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream);
Endpoint localEndpoint = ServiceEndpoints.toEndpoint(localServices);
RemoteEndpoint remoteEndpoint;
if (exceptionHandler == null)
remoteEndpoint = new PatchedRemoteEndpoint(outgoingMessageStream, localEndpoint);
else
remoteEndpoint = new PatchedRemoteEndpoint(outgoingMessageStream, localEndpoint, exceptionHandler);
jsonHandler.setMethodProvider(remoteEndpoint);
return remoteEndpoint;
}
@Override
protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) {
MessageConsumer outgoingMessageStream = new WebSocketMessageConsumer(session, jsonHandler);
outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream);
Endpoint localEndpoint = ServiceEndpoints.toEndpoint(localServices);
RemoteEndpoint remoteEndpoint;
if (exceptionHandler == null)
remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, localEndpoint);
else
remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, localEndpoint, exceptionHandler);
jsonHandler.setMethodProvider(remoteEndpoint);
return remoteEndpoint;
}
@Test
public void testParseNullList() {
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\": null}");
Assert.assertNull(((ResponseMessage)message).getResult());
}
@SuppressWarnings({ "unchecked" })
@Test
public void testParseEmptyList() {
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\": []}");
List<Entry> result = (List<Entry>) ((ResponseMessage)message).getResult();
Assert.assertEquals(0, result.size());
}
@SuppressWarnings({ "unchecked" })
@Test
public void testEither_05() {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo",
new TypeToken<Either<List<MyClass>, MyClassList>>() {}.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\": [{\n"
+ " value:\"foo\"\n"
+ "}]}");
Either<List<MyClass>, MyClassList> result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isLeft());
Assert.assertEquals("foo", result.getLeft().get(0).getValue());
message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
+ "\"id\":\"2\",\n"
+ "\"result\": {\n"
+ " items: [{\n"
+ " value:\"bar\"\n"
+ "}]}}");
result = (Either<List<MyClass>, MyClassList>) ((ResponseMessage)message).getResult();
Assert.assertTrue(result.isRight());
Assert.assertEquals("bar", result.getRight().getItems().get(0).getValue());
}