Functional tests for Json serialization and deserialization of regular classes. @author Inderjeet Singh @author Joel Leitch
| 63 | * @author Joel Leitch |
| 64 | */ |
| 65 | public class ObjectTest { |
| 66 | private Gson gson; |
| 67 | private TimeZone oldTimeZone; |
| 68 | private Locale oldLocale; |
| 69 | |
| 70 | @Before |
| 71 | public void setUp() throws Exception { |
| 72 | gson = new Gson(); |
| 73 | |
| 74 | oldTimeZone = TimeZone.getDefault(); |
| 75 | TimeZone.setDefault(TimeZone.getTimeZone(class="st">"America/Los_Angeles")); |
| 76 | oldLocale = Locale.getDefault(); |
| 77 | Locale.setDefault(Locale.US); |
| 78 | } |
| 79 | |
| 80 | @After |
| 81 | public void tearDown() { |
| 82 | TimeZone.setDefault(oldTimeZone); |
| 83 | Locale.setDefault(oldLocale); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testJsonInSingleQuotesDeserialization() { |
| 88 | String json = class="st">"{'stringValue':'no message','intValue':10,'longValue':20}"; |
| 89 | BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class); |
| 90 | assertThat(target.stringValue).isEqualTo(class="st">"no message"); |
| 91 | assertThat(target.intValue).isEqualTo(10); |
| 92 | assertThat(target.longValue).isEqualTo(20); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testJsonInMixedQuotesDeserialization() { |
| 97 | String json = class="st">"{\"stringValue\class="st">":'no message','intValue':10,'longValue':20}"; |
| 98 | BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class); |
| 99 | assertThat(target.stringValue).isEqualTo(class="st">"no message"); |
| 100 | assertThat(target.intValue).isEqualTo(10); |
| 101 | assertThat(target.longValue).isEqualTo(20); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testBagOfPrimitivesSerialization() { |
| 106 | BagOfPrimitives target = new BagOfPrimitives(10, 20, false, class="st">"stringValue"); |
| 107 | assertThat(gson.toJson(target)).isEqualTo(target.getExpectedJson()); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testBagOfPrimitivesDeserialization() { |
| 112 | BagOfPrimitives src = new BagOfPrimitives(10, 20, false, class="st">"stringValue"); |
| 113 | String json = src.getExpectedJson(); |
| 114 | BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class); |
| 115 | assertThat(target.getExpectedJson()).isEqualTo(json); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testBagOfPrimitiveWrappersSerialization() { |
| 120 | BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers(10L, 20, false); |
| 121 | assertThat(gson.toJson(target)).isEqualTo(target.getExpectedJson()); |
| 122 | } |
nothing calls this directly
no outgoing calls
no test coverage detected