/** * Create the JSON handler for messages between the local and remote services. */ protected MessageJsonHandler createJsonHandler() { Map<String, JsonRpcMethod> supportedMethods = getSupportedMethods(); if (configureGson != null) return new MessageJsonHandler(supportedMethods, configureGson); else return new MessageJsonHandler(supportedMethods); }
/** * Create the JSON handler for messages between the local and remote services. */ protected MessageJsonHandler createJsonHandler() { Map<String, JsonRpcMethod> supportedMethods = getSupportedMethods(); if (configureGson != null) return new MessageJsonHandler(supportedMethods, configureGson); else return new MessageJsonHandler(supportedMethods); }
/** * Perform JSON serialization of the given object using the default configuration of JSON-RPC messages * enhanced with the pretty printing option. */ public static String toString(Object object) { if (toStringInstance == null) { toStringInstance = new MessageJsonHandler(Collections.emptyMap(), gsonBuilder -> { gsonBuilder.setPrettyPrinting(); }); } return toStringInstance.gson.toJson(object); }
/** * Perform JSON serialization of the given object using the default configuration of JSON-RPC messages * enhanced with the pretty printing option. */ public static String toString(Object object) { if (toStringInstance == null) { toStringInstance = new MessageJsonHandler(Collections.emptyMap(), gsonBuilder -> { gsonBuilder.setPrettyPrinting(); }); } return toStringInstance.gson.toJson(object); }
@Before public void setup() { final Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); final Consumer<GsonBuilder> _function = (GsonBuilder it) -> { it.setPrettyPrinting(); }; MessageJsonHandler _messageJsonHandler = new MessageJsonHandler(methods, _function); this.jsonHandler = _messageJsonHandler; }
@Before public void setup() { final Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); final Map<String, JsonRpcMethod> clientMethods = ServiceEndpoints.getSupportedMethods(LanguageClient.class); final HashMap<String, JsonRpcMethod> all = new HashMap<String, JsonRpcMethod>(); all.putAll(methods); all.putAll(clientMethods); MessageJsonHandler _messageJsonHandler = new MessageJsonHandler(all); this.jsonHandler = _messageJsonHandler; }
@Test public void testSerializeImmutableList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); List<Object> list = new ArrayList<>(); list.add("a"); list.add("b"); message.setParams(Collections.unmodifiableList(list)); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[\"a\",\"b\"]}", json); }
@Test public void testStopOnInterrrupt() throws Exception { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new InterruptedIOException(); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
@Test public void testStopOnChannelClosed() throws Exception { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new ClosedChannelException(); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
@Test public void testStopOnSocketClosed() throws Throwable { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new SocketException("Socket closed"); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
@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()); }
@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()); }
@Test public void testSerializeEmptyList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); message.setParams(Collections.EMPTY_LIST); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[]}", json); }
@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_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 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()); }
@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 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 testMultiParamsParsing_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" + "\"params\": [\"foo\", 2],\n" + "\"method\":\"bar\"\n" + "}"); Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof JsonArray); }
@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()); }