|
22 | 22 | import com.google.gson.JsonArray;
|
23 | 23 | import com.google.gson.JsonObject;
|
24 | 24 | import com.google.gson.JsonParser;
|
| 25 | +import com.google.gson.JsonSyntaxException; |
25 | 26 |
|
26 | 27 | import org.junit.Test;
|
27 | 28 | import org.junit.runner.RunWith;
|
@@ -180,7 +181,7 @@ public void testShouldBeAbleToConvertASessionId() {
|
180 | 181 | assertEquals("some id", converted.get("value").getAsString());
|
181 | 182 | }
|
182 | 183 |
|
183 |
| - //@Test |
| 184 | + @Test |
184 | 185 | public void testShouldBeAbleToConvertAJsonObject() {
|
185 | 186 | JsonObject obj = new JsonObject();
|
186 | 187 | obj.addProperty("key", "value");
|
@@ -258,10 +259,27 @@ public void testShouldConvertAProxyCorrectly() {
|
258 | 259 | @Test
|
259 | 260 | public void testShouldCallToJsonMethodIfPresent() {
|
260 | 261 | String json = new BeanToJsonConverter().convert(new JsonAware("converted"));
|
261 |
| - |
262 | 262 | assertEquals("\"converted\"", json);
|
263 | 263 | }
|
264 | 264 |
|
| 265 | + @Test |
| 266 | + public void testConvertsToJsonMethodResultToPrimitiveIfItIsNotJson() { |
| 267 | + // We want this parsed as a string primitive, but JsonParser will reject it |
| 268 | + // as malformed because of the slash. |
| 269 | + String raw = "gnu/linux"; |
| 270 | + |
| 271 | + try { |
| 272 | + // Make sure that the parser does actually reject this so the test is |
| 273 | + // meaningful. If this stops failing, choose a different malformed JSON |
| 274 | + // string. |
| 275 | + new JsonParser().parse(raw).toString(); |
| 276 | + fail("Expected a parser exception when parsing: " + raw); |
| 277 | + } catch (JsonSyntaxException expected) { |
| 278 | + } |
| 279 | + |
| 280 | + String json = new BeanToJsonConverter().convert(new JsonAware(raw)); |
| 281 | + assertEquals("\"gnu/linux\"", json); |
| 282 | + } |
265 | 283 |
|
266 | 284 | private void verifyStackTraceInJson(String json, StackTraceElement[] stackTrace) {
|
267 | 285 | int posOfLastStackTraceElement = 0;
|
|
0 commit comments