Skip to content

Commit cbc3f7e

Browse files
add more tests
1 parent 5eb9e11 commit cbc3f7e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
*.class
55
out/
66
target/
7+
.DS_Store
78

src/test/b2SdkExamples/B2JsonTest.java

+44-4
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ public void testResponseUsingB2Json() throws B2JsonException, JSONException {
214214
System.out.println("obj is: " + obj);
215215
String expected = "{\n" +
216216
" \"categories\": [\n" +
217-
" \"test1\",\n" +
218-
" \"test2\"\n" +
217+
" \"test2\",\n" +
218+
" \"test1\"\n" +
219219
" ],\n" +
220220
" \"localDate\": \"20230331\",\n" +
221221
" \"localDateTime\": \"d20230331_m122100\",\n" +
@@ -310,11 +310,11 @@ public void testObjectUsingDefaultValue() throws B2JsonException {
310310
System.out.println("about to check the equality between Gson and B2Json results..");
311311
assertEquals(fromGson, fromB2Json);
312312
String fromB2JsonString = b2Json.toJson(fromB2Json);
313-
String fromGsonString = gson.toJson(fromB2Json);
313+
String fromGsonString = gson.toJson(fromGson);
314314
String expectedFromGson = "{\n" +
315315
" \"a\": 2023,\n" +
316316
" \"b\": \"hello\",\n" +
317-
" \"c\": 5,\n" +
317+
" \"c\": 0,\n" +
318318
" \"d\": 0\n" +
319319
"}";
320320
assertEquals(expectedFromGson, fromGsonString);
@@ -401,4 +401,44 @@ public TestRequest3(String zip) {
401401
}
402402
}
403403

404+
@Test
405+
public void testGsonFailure() {
406+
// there's an unknown field called e in this json string, in this case, GSON library will just ignore it
407+
String json = "{\n" +
408+
" \"a\": 2023,\n" +
409+
" \"b\": \"hello\",\n" +
410+
" \"e\": \"hello\"\n" +
411+
"}";
412+
System.out.println("json is: " + json);
413+
Container fromGson = gson.fromJson(json, Container.class);
414+
System.out.println("gson.fromJson(json) is: " + fromGson);
415+
System.out.println("about to check the equality between Gson and B2Json results..");
416+
String fromGsonString = gson.toJson(fromGson);
417+
String expectedFromGson = "{\n" +
418+
" \"a\": 2023,\n" +
419+
" \"b\": \"hello\",\n" +
420+
" \"c\": 0,\n" +
421+
" \"d\": 0\n" +
422+
"}";
423+
assertEquals(expectedFromGson, fromGsonString);
424+
425+
// in this json, the field named a is missing
426+
json = "{\n" +
427+
" \"b\": \"hello\",\n" +
428+
" \"e\": \"hello\"\n" +
429+
"}";
430+
System.out.println("2nd time, json is: " + json);
431+
fromGson = gson.fromJson(json, Container.class);
432+
System.out.println("2nd time, gson.fromJson(json) is: " + fromGson);
433+
System.out.println("about to check the equality between Gson and B2Json results..");
434+
fromGsonString = gson.toJson(fromGson);
435+
System.out.println("2nd time, fromGsonString is: " + fromGsonString);
436+
expectedFromGson = "{\n" +
437+
" \"a\": 0,\n" +
438+
" \"b\": \"hello\",\n" +
439+
" \"c\": 0,\n" +
440+
" \"d\": 0\n" +
441+
"}";
442+
assertEquals(expectedFromGson, fromGsonString);
443+
}
404444
}

0 commit comments

Comments
 (0)