Skip to content

Commit

Permalink
Add a test for fallback behavior in Message.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
pisv committed Nov 16, 2023
1 parent 594caa4 commit e8ab10c
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.Test;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

Expand Down Expand Up @@ -153,7 +154,41 @@ public void testSerializeImmutableList() {
String json = handler.serialize(message);
Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[\"a\",\"b\"]}", json);
}


@Test
public void testMessageToString() {
NotificationMessage message = new NotificationMessage();
message.setMethod("foo");
List<Object> list = new ArrayList<>();
list.add("a");
list.add("b");
message.setParams(list);
Assert.assertEquals("{\n"
+ " \"jsonrpc\": \"2.0\",\n"
+ " \"method\": \"foo\",\n"
+ " \"params\": [\n"
+ " \"a\",\n"
+ " \"b\"\n"
+ " ]\n"
+ "}", message.toString());

MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()) {
@Override
public String format(Object object) {
throw new JsonIOException("TEST");
};
};
message.setJsonHandler(handler);
Assert.assertEquals("NotificationMessage [\n"
+ " method = \"foo\"\n"
+ " params = ArrayList (\n"
+ " \"a\",\n"
+ " \"b\"\n"
+ " )\n"
+ " jsonrpc = \"2.0\"\n"
+ "]", message.toString());
}

@SuppressWarnings({ "unchecked" })
@Test
public void testEither_01() {
Expand Down

0 comments on commit e8ab10c

Please sign in to comment.