From 02fd91ad35810cfe06d8b2e28a740957a8437ad5 Mon Sep 17 00:00:00 2001 From: Ganesh Pagade Date: Sat, 9 Mar 2019 14:59:08 +0530 Subject: [PATCH] review comments --- .../org/baeldung/gson/entities/MyClass.java | 36 +++++++++++++---- .../gson/advance/GsonAdvanceUnitTest.java | 40 +++++++++++-------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/gson/src/main/java/org/baeldung/gson/entities/MyClass.java b/gson/src/main/java/org/baeldung/gson/entities/MyClass.java index 827ca10cb0..997f4c3f26 100644 --- a/gson/src/main/java/org/baeldung/gson/entities/MyClass.java +++ b/gson/src/main/java/org/baeldung/gson/entities/MyClass.java @@ -1,14 +1,18 @@ package org.baeldung.gson.entities; +import java.util.Objects; + public class MyClass { private int id; - private String[] strings; + private String name; - public MyClass() { - id = 1; - strings = new String[] { "a", "b" }; + public MyClass(int id, String name) { + this.id = id; + this.name = name; } + public MyClass() { } + public int getId() { return id; } @@ -17,11 +21,27 @@ public class MyClass { this.id = id; } - public String[] getStrings() { - return strings; + public String getName() { + return name; } - public void setStrings(String[] strings) { - this.strings = strings; + public void setName(String name) { + this.name = name; + } + + @Override public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + MyClass myClass = (MyClass) o; + return id == myClass.id && Objects.equals(name, myClass.name); + } + + @Override public int hashCode() { + + return Objects.hash(id, name); } } diff --git a/gson/src/test/java/org/baeldung/gson/advance/GsonAdvanceUnitTest.java b/gson/src/test/java/org/baeldung/gson/advance/GsonAdvanceUnitTest.java index 020118aa2b..3bf2afbe4a 100644 --- a/gson/src/test/java/org/baeldung/gson/advance/GsonAdvanceUnitTest.java +++ b/gson/src/test/java/org/baeldung/gson/advance/GsonAdvanceUnitTest.java @@ -8,6 +8,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.baeldung.gson.entities.Animal; import org.baeldung.gson.entities.Cow; @@ -18,40 +19,42 @@ import org.junit.Test; public class GsonAdvanceUnitTest { - @Test public void givenListOfMyClass_whenSerializing_thenCorrect() { - List list = new ArrayList<>(); - list.add(new MyClass()); - list.add(new MyClass()); + @Test + public void givenListOfMyClass_whenSerializing_thenCorrect() { + List list = Arrays.asList(new MyClass(1, "name1"), new MyClass(2, "name2")); Gson gson = new Gson(); String jsonString = gson.toJson(list); - String expectedString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]"; + String expectedString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]"; assertEquals(expectedString, jsonString); } @Test(expected = ClassCastException.class) public void givenJsonString_whenIncorrectDeserializing_thenThrowClassCastException() { - String inputString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]"; + String inputString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]"; Gson gson = new Gson(); - List list = gson.fromJson(inputString, ArrayList.class); + List outputList = gson.fromJson(inputString, ArrayList.class); - assertEquals(1, list.get(0).getId()); + assertEquals(1, outputList.get(0).getId()); } - @Test public void givenJsonString_whenDeserializing_thenReturnListOfMyClass() { - String inputString = "[{\"id\":1,\"strings\":[\"a\",\"b\"]},{\"id\":1,\"strings\":[\"a\",\"b\"]}]"; + @Test + public void givenJsonString_whenDeserializing_thenReturnListOfMyClass() { + String inputString = "[{\"id\":1,\"name\":\"name1\"},{\"id\":2,\"name\":\"name2\"}]"; + List inputList = Arrays.asList(new MyClass(1, "name1"), new MyClass(2, "name2")); + Type listOfMyClassObject = new TypeToken>() {}.getType(); Gson gson = new Gson(); - List list = gson.fromJson(inputString, listOfMyClassObject); + List outputList = gson.fromJson(inputString, listOfMyClassObject); - assertEquals(2, list.size()); - assertEquals(1, list.get(0).getId()); + assertEquals(inputList, outputList); } - @Test public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() { + @Test + public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect() { String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]"; List inList = new ArrayList<>(); @@ -63,7 +66,8 @@ public class GsonAdvanceUnitTest { assertEquals(expectedString, jsonString); } - @Test public void givenPolymorphicList_whenDeserializeWithTypeAdapter_thenCorrect() { + @Test + public void givenPolymorphicList_whenDeserializeWithTypeAdapter_thenCorrect() { String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]"; AnimalDeserializer deserializer = new AnimalDeserializer("type"); @@ -79,7 +83,8 @@ public class GsonAdvanceUnitTest { assertTrue(outList.get(0) instanceof Dog); } - @Test public void givenPolymorphicList_whenSerializeWithRuntimeTypeAdapter_thenCorrect() { + @Test + public void givenPolymorphicList_whenSerializeWithRuntimeTypeAdapter_thenCorrect() { String expectedString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]"; List inList = new ArrayList<>(); @@ -90,7 +95,8 @@ public class GsonAdvanceUnitTest { assertEquals(expectedString, jsonString); } - @Test public void givenPolymorphicList_whenDeserializeWithRuntimeTypeAdapter_thenCorrect() { + @Test + public void givenPolymorphicList_whenDeserializeWithRuntimeTypeAdapter_thenCorrect() { String inputString = "[{\"petName\":\"Milo\",\"type\":\"Dog\"},{\"breed\":\"Jersey\",\"type\":\"Cow\"}]"; Type listOfAnimals = new TypeToken>() {}.getType();