Java源码示例:org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod

示例1
@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"));
	}
}
 
示例2
@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"));
	}
}
 
示例3
@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());
}
 
示例4
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");
	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": {\"uri\": \"dummy://mymodel.mydsl\"},\n"
			+ "\"command\":\"foo\"\n"
			+ "}");
	Object params = message.getParams();
	Class<? extends Object> class1 = params.getClass();
	Assert.assertEquals(Location.class, class1);
}
 
示例5
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"foo\",\n"
			+ "\"arguments\": {\"uri\": \"dummy://mymodel.mydsl\"}\n"
			+ "}");
	Assert.assertEquals(Location.class, message.getParams().getClass());
}
 
示例6
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"bar\",\n"
			+ "\"arguments\": {\"uri\": \"dummy://mymodel.mydsl\"}\n"
			+ "}");
	Assert.assertEquals(JsonObject.class, message.getParams().getClass());
}
 
示例7
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"bar\",\n"
			+ "\"arguments\": null\n"
			+ "}");
	Assert.assertEquals(null, message.getParams());
}
 
示例8
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"foo\",\n"
			+ "\"arguments\": [\"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));
}
 
示例9
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"bar\",\n"
			+ "\"arguments\": [\"foo\", 2]\n"
			+ "}");
	Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof JsonArray);
}
 
示例10
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"foo\",\n"
			+ "\"arguments\": [[\"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);
}
 
示例11
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"command\":\"foo\",\n"
			+ "\"arguments\": [[\"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));
}
 
示例12
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": [\"foo\", 2],\n"
			+ "\"command\":\"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));
}
 
示例13
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": [\"foo\", 2],\n"
			+ "\"command\":\"bar\"\n"
			+ "}");
	Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof JsonArray);
}
 
示例14
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": [[\"foo\", \"bar\"], [1, 2], {\"uri\": \"dummy://mymodel.mydsl\"}],\n"
			+ "\"command\":\"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);
}
 
示例15
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");

	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": [[\"foo\", \"bar\"], [1, 2]],\n"
			+ "\"command\":\"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));
}
 
示例16
@Test
public void testEnumParam() {
	Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
	supportedMethods.put("foo", JsonRpcMethod.request("foo",
			new TypeToken<Void>() {}.getType(),
			new TypeToken<List<MyDebugEnum>>() {}.getType()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");
	RequestMessage message = (RequestMessage) handler.parseMessage("{"
			+ "\"seq\":2,\n"
			+ "\"type\":\"request\",\n"
			+ "\"arguments\": [\"enum1\", \"anotherEnum\", \"aStrangeEnumUTC\"],\n"
			+ "\"command\":\"foo\"\n"
			+ "}");
	Assert.assertTrue("" + message.getParams().getClass(), message.getParams() instanceof List);

	List<?> parameters = (List<?>) message.getParams();
	Assert.assertEquals(3, parameters.size());
	Assert.assertEquals(Arrays.asList(MyDebugEnum.ENUM1, MyDebugEnum.ANOTHER_ENUM, MyDebugEnum.A_STRANGE_ENUM_UTC),
			parameters);
}
 
示例17
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");
	String[] properties = new String[] {
			"\"seq\":2",
			"\"type\":\"request\"",
			"\"command\":\"foo\"",
			"\"arguments\": {\"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);
	});
}
 
示例18
@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());
	});
}
 
示例19
@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());
    });
}
 
示例20
@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()));
	DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods);
	handler.setMethodProvider((id) -> "foo");
	String[] properties = new String[] {
			"\"seq\":2",
			"\"type\":\"event\"",
			"\"event\":\"foo\"",
			"\"body\": {\"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);
	});
}
 
示例21
@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());
	});
}
 
示例22
/**
 * Gather all JSON-RPC methods from the local and remote services.
 */
protected Map<String, JsonRpcMethod> getSupportedMethods() {
	Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
	// Gather the supported methods of remote interfaces
	for (Class<?> interface_ : remoteInterfaces) {
		supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(interface_));
	}
	
	// Gather the supported methods of local services
	for (Object localService : localServices) {
		if (localService instanceof JsonRpcMethodProvider) {
			JsonRpcMethodProvider rpcMethodProvider = (JsonRpcMethodProvider) localService;
			supportedMethods.putAll(rpcMethodProvider.supportedMethods());
		} else {
			supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(localService.getClass()));
		}
	}
	
	return supportedMethods;
}
 
示例23
/**
 * Convert the json input into the result object corresponding to the call made
 * by id.
 *
 * If the id is not known until after parsing, call
 * {@link #parseResult(Object, String)} on the return value of this call for a
 * second chance conversion.
 *
 * @param in
 *            json input to read from
 * @param id
 *            id of request message this is in response to
 * @return correctly typed object if the correct expected type can be
 *         determined, or a JsonElement representing the result
 */
protected Object parseResult(JsonReader in, String id) throws JsonIOException, JsonSyntaxException {
	Type type = null;
	MethodProvider methodProvider = handler.getMethodProvider();
	if (methodProvider != null && id != null) {
		String resolvedMethod = methodProvider.resolveMethod(id);
		if (resolvedMethod != null) {
			JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod);
			if (jsonRpcMethod != null) {
				type = jsonRpcMethod.getReturnType();
				if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) {
					TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(type));
					try {
						if (typeAdapter != null)
							return typeAdapter.read(in);
					} catch (IOException exception) {
						throw new JsonIOException(exception);
					}
				}
			}
		}
	}
	return fromJson(in, type);
}
 
示例24
/**
 * Convert the JsonElement into the result object corresponding to the call made
 * by id. If the result is already converted, does nothing.
 *
 * @param result
 *            json element to read from
 * @param id
 *            id of request message this is in response to
 * @return correctly typed object if the correct expected type can be
 *         determined, or result unmodified if no conversion can be done.
 */
protected Object parseResult(Object result, String id) throws JsonSyntaxException {
	if (result instanceof JsonElement) {
		// Type of result could not be resolved - try again with the parsed JSON tree
		Type type = null;
		MethodProvider methodProvider = handler.getMethodProvider();
		if (methodProvider != null) {
			String resolvedMethod = methodProvider.resolveMethod(id);
			if (resolvedMethod != null) {
				JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod);
				if (jsonRpcMethod != null) {
					type = jsonRpcMethod.getReturnType();
					if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) {
						TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(type));
						if (typeAdapter != null)
							return typeAdapter.fromJsonTree((JsonElement) result);
					}
				}
			}
		}
		return fromJson((JsonElement) result, type);
	}
	return result;
}
 
示例25
@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"));
	}
}
 
示例26
@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"));
	}
}
 
示例27
@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());
}
 
示例28
@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());
}
 
示例29
@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());
}
 
示例30
@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());
}