|
1 | 1 | /** |
2 | | - * (C) Copyright IBM Corp. 2015, 2019. |
| 2 | + * (C) Copyright IBM Corp. 2015, 2020. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
5 | 5 | * the License. You may obtain a copy of the License at |
@@ -54,15 +54,18 @@ private <T> T deserialize(String json, Class<T> clazz) { |
54 | 54 | return GsonSingleton.getGson().fromJson(json, clazz); |
55 | 55 | } |
56 | 56 |
|
57 | | - private <T> void testSerDeser(DynamicModel<?> model, Class<T> clazz) { |
58 | | - String jsonString = serialize(model); |
| 57 | + private void display(String msg) { |
59 | 58 | if (displayOutput) { |
60 | | - System.out.println("serialized " + model.getClass().getSimpleName() + ": " + jsonString); |
| 59 | + System.out.println(msg); |
61 | 60 | } |
| 61 | + } |
| 62 | + |
| 63 | + private <T> void testSerDeser(DynamicModel<?> model, Class<T> clazz) { |
| 64 | + String jsonString = serialize(model); |
| 65 | + |
| 66 | + display("serialized " + model.getClass().getSimpleName() + ": " + jsonString); |
62 | 67 | T newModel = deserialize(jsonString, clazz); |
63 | | - if (displayOutput) { |
64 | | - System.out.println("de-serialized " + model.getClass().getSimpleName() + ": " + newModel.toString()); |
65 | | - } |
| 68 | + display("de-serialized " + model.getClass().getSimpleName() + ": " + newModel.toString()); |
66 | 69 | assertEquals(newModel, model); |
67 | 70 | } |
68 | 71 |
|
@@ -203,19 +206,31 @@ public void testNoCtor() { |
203 | 206 | public void testNullValues() { |
204 | 207 | ModelAPFoo model = createModelAPFoo(); |
205 | 208 | model.setProp1(null); |
206 | | - // model.put("basketball", "foo"); |
207 | 209 | testSerDeser(model, ModelAPFoo.class); |
208 | 210 | } |
209 | 211 |
|
| 212 | + @Test |
| 213 | + public void testAddlPropsNull() { |
| 214 | + ModelAPString model = createModelAPString(); |
| 215 | + model.put("basketball", null); |
| 216 | + |
| 217 | + String json = serialize(model); |
| 218 | + display("Serialized: " + json); |
| 219 | + assertTrue(json.contains("\"basketball\": null")); |
| 220 | + |
| 221 | + ModelAPString newModel = deserialize(json, ModelAPString.class); |
| 222 | + assertEquals(newModel, model); |
| 223 | + } |
| 224 | + |
210 | 225 | @Test(expectedExceptions = {JsonSyntaxException.class}) |
211 | 226 | public void testBadDeser() { |
212 | 227 |
|
213 | 228 | // Obtain the json string and then render it incorrect to trigger a deserialization error. |
214 | 229 | ModelAPFoo model = createModelAPFoo(); |
215 | 230 | String goodJson = serialize(model); |
216 | | - if (displayOutput) System.out.println("Serialized ModelAPFoo: " + goodJson); |
| 231 | + display("Serialized ModelAPFoo: " + goodJson); |
217 | 232 | String badJson = goodJson.replaceAll("foo", "FOO").replaceAll("prop2", "var2"); |
218 | | - if (displayOutput) System.out.println("Incorrect JSON: " + badJson); |
| 233 | + display("Incorrect JSON: " + badJson); |
219 | 234 |
|
220 | 235 | // We just need to try to deserialize the bad JSON string to trigger the exception. |
221 | 236 | deserialize(badJson, ModelAPFoo.class); |
|
0 commit comments