org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler Java Examples

The following examples show how to use org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageProducerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expected=JsonRpcException.class)
public void testIOException() throws Throwable {
	try {
		executorService.submit(() -> {
			InputStream input = new InputStream() {
				@Override
				public int read() throws IOException {
					throw new SocketException("Permission denied: connect");
				}
			};
			MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap());
			StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler);
			messageProducer.listen(message -> {});
			messageProducer.close();
		}).get(TIMEOUT, TimeUnit.MILLISECONDS);
	} catch (ExecutionException e) {
		throw e.getCause();
	}
}
 
Example #2
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testEnumParam() {
	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, 3],\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, MyEnum.C),
			parameters);
}
 
Example #3
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMultiParamsParsing_01() {
	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\":\"foo\"\n"
			+ "}");
	Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);

	List<?> parameters = (List<?>) message.getParams();
	Assert.assertEquals(2, parameters.size());
	Assert.assertEquals("foo", parameters.get(0));
	Assert.assertEquals(2, parameters.get(1));
}
 
Example #7
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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));
}
 
Example #9
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
	});
}
 
Example #10
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
	});
}
 
Example #11
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
	});
}
 
Example #12
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
	});
}
 
Example #13
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #14
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #15
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #16
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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"));
	}
}
 
Example #17
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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"));
	}
}
 
Example #18
Source File: RemoteEndpoint.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Cancellation is handled inside this class and not forwarded to the local endpoint.
 * 
 * @return {@code true} if the given message is a cancellation notification,
 *         {@code false} if it can be handled by the local endpoint
 */
protected boolean handleCancellation(NotificationMessage notificationMessage) {
	if (MessageJsonHandler.CANCEL_METHOD.getMethodName().equals(notificationMessage.getMethod())) {
		Object cancelParams = notificationMessage.getParams();
		if (cancelParams != null) {
			if (cancelParams instanceof CancelParams) {
				synchronized (receivedRequestMap) {
					String id = ((CancelParams) cancelParams).getId();
					CompletableFuture<?> future = receivedRequestMap.get(id);
					if (future != null)
						future.cancel(true);
					else
						LOG.warning("Unmatched cancel notification for request id " + id);
				}
				return true;
			} else {
				LOG.warning("Cancellation support is disabled, since the '" + MessageJsonHandler.CANCEL_METHOD.getMethodName() + "' method has been registered explicitly.");
			}
		} else {
			LOG.warning("Missing 'params' attribute of cancel notification.");
		}
	}
	return false;
}
 
Example #19
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #20
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #21
Source File: Launcher.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
public Launcher<T> create() {
	// Validate input
	if (input == null)
		throw new IllegalStateException("Input stream must be configured.");
	if (output == null)
		throw new IllegalStateException("Output stream must be configured.");
	if (localServices == null)
		throw new IllegalStateException("Local service must be configured.");
	if (remoteInterfaces == null)
		throw new IllegalStateException("Remote interface must be configured.");
	
	// Create the JSON handler, remote endpoint and remote proxy
	MessageJsonHandler jsonHandler = createJsonHandler();
	RemoteEndpoint remoteEndpoint = createRemoteEndpoint(jsonHandler);
	T remoteProxy = createProxy(remoteEndpoint);
	
	// Create the message processor
	StreamMessageProducer reader = new StreamMessageProducer(input, jsonHandler, remoteEndpoint);
	MessageConsumer messageConsumer = wrapMessageConsumer(remoteEndpoint);
	ConcurrentMessageProcessor msgProcessor = createMessageProcessor(reader, messageConsumer, remoteProxy);
	ExecutorService execService = executorService != null ? executorService : Executors.newCachedThreadPool();
	return createLauncher(execService, remoteProxy, remoteEndpoint, msgProcessor);
}
 
Example #22
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #23
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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());
}
 
Example #24
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRawMultiParamsParsing_01() {
	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\":\"foo\",\n"
			+ "\"params\": [\"foo\", 2]\n"
			+ "}");
	Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);

	List<?> parameters = (List<?>) message.getParams();
	Assert.assertEquals(2, parameters.size());
	Assert.assertEquals("foo", parameters.get(0));
	Assert.assertEquals(2, parameters.get(1));
}
 
Example #25
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #26
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: MessageProducerTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #28
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #29
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@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()));
	MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");
	Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\","
			+ "\"id\":\"2\",\n"
			+ " \"result\": [\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("{\"jsonrpc\":\"2.0\","
			+ "\"id\":\"2\",\n" 
			+ "\"result\": \"name\"\n"
			+ "}");
	result = (Either<String, List<Map<String,String>>>) ((ResponseMessage)message).getResult();
	Assert.assertFalse(result.isRight());
	Assert.assertEquals("name", result.getLeft());
}
 
Example #30
Source File: MessageJsonHandlerTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}