Skip to content

Commit e8ab10c

Browse files
committed
Add a test for fallback behavior in Message.toString()
1 parent 594caa4 commit e8ab10c

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

org.eclipse.lsp4j.jsonrpc/src/test/java/org/eclipse/lsp4j/jsonrpc/test/json/MessageJsonHandlerTest.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.Test;
3737

3838
import com.google.gson.JsonArray;
39+
import com.google.gson.JsonIOException;
3940
import com.google.gson.JsonObject;
4041
import com.google.gson.reflect.TypeToken;
4142

@@ -153,7 +154,41 @@ public void testSerializeImmutableList() {
153154
String json = handler.serialize(message);
154155
Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[\"a\",\"b\"]}", json);
155156
}
156-
157+
158+
@Test
159+
public void testMessageToString() {
160+
NotificationMessage message = new NotificationMessage();
161+
message.setMethod("foo");
162+
List<Object> list = new ArrayList<>();
163+
list.add("a");
164+
list.add("b");
165+
message.setParams(list);
166+
Assert.assertEquals("{\n"
167+
+ " \"jsonrpc\": \"2.0\",\n"
168+
+ " \"method\": \"foo\",\n"
169+
+ " \"params\": [\n"
170+
+ " \"a\",\n"
171+
+ " \"b\"\n"
172+
+ " ]\n"
173+
+ "}", message.toString());
174+
175+
MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()) {
176+
@Override
177+
public String format(Object object) {
178+
throw new JsonIOException("TEST");
179+
};
180+
};
181+
message.setJsonHandler(handler);
182+
Assert.assertEquals("NotificationMessage [\n"
183+
+ " method = \"foo\"\n"
184+
+ " params = ArrayList (\n"
185+
+ " \"a\",\n"
186+
+ " \"b\"\n"
187+
+ " )\n"
188+
+ " jsonrpc = \"2.0\"\n"
189+
+ "]", message.toString());
190+
}
191+
157192
@SuppressWarnings({ "unchecked" })
158193
@Test
159194
public void testEither_01() {

0 commit comments

Comments
 (0)