aboutsummaryrefslogtreecommitdiff
path: root/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java')
-rw-r--r--gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java37
1 files changed, 28 insertions, 9 deletions
diff --git a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java
index a9b77f1d..793ad569 100644
--- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java
+++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java
@@ -16,22 +16,26 @@
package com.google.gson.functional;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
-import com.google.gson.JsonDeserializationContext;
-import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.common.TestTypes.ClassWithObjects;
-
-import junit.framework.TestCase;
-
import java.lang.reflect.Type;
import java.util.Collection;
+import org.junit.Before;
+import org.junit.Test;
/**
* Functional tests for the different cases for serializing (or ignoring) null fields and object.
@@ -39,15 +43,15 @@ import java.util.Collection;
* @author Inderjeet Singh
* @author Joel Leitch
*/
-public class NullObjectAndFieldTest extends TestCase {
+public class NullObjectAndFieldTest {
private GsonBuilder gsonBuilder;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
+ @Before
+ public void setUp() throws Exception {
gsonBuilder = new GsonBuilder().serializeNulls();
}
+ @Test
public void testTopLevelNullObjectSerialization() {
Gson gson = gsonBuilder.create();
String actual = gson.toJson(null);
@@ -57,12 +61,14 @@ public class NullObjectAndFieldTest extends TestCase {
assertEquals("null", actual);
}
+ @Test
public void testTopLevelNullObjectDeserialization() throws Exception {
Gson gson = gsonBuilder.create();
String actual = gson.fromJson("null", String.class);
assertNull(actual);
}
+ @Test
public void testExplicitSerializationOfNulls() {
Gson gson = gsonBuilder.create();
ClassWithObjects target = new ClassWithObjects(null);
@@ -71,12 +77,14 @@ public class NullObjectAndFieldTest extends TestCase {
assertEquals(expected, actual);
}
+ @Test
public void testExplicitDeserializationOfNulls() throws Exception {
Gson gson = gsonBuilder.create();
ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class);
assertNull(target.bag);
}
+ @Test
public void testExplicitSerializationOfNullArrayMembers() {
Gson gson = gsonBuilder.create();
ClassWithMembers target = new ClassWithMembers();
@@ -87,6 +95,7 @@ public class NullObjectAndFieldTest extends TestCase {
/**
* Added to verify http://code.google.com/p/google-gson/issues/detail?id=68
*/
+ @Test
public void testNullWrappedPrimitiveMemberSerialization() {
Gson gson = gsonBuilder.serializeNulls().create();
ClassWithNullWrappedPrimitive target = new ClassWithNullWrappedPrimitive();
@@ -97,6 +106,7 @@ public class NullObjectAndFieldTest extends TestCase {
/**
* Added to verify http://code.google.com/p/google-gson/issues/detail?id=68
*/
+ @Test
public void testNullWrappedPrimitiveMemberDeserialization() {
Gson gson = gsonBuilder.create();
String json = "{'value':null}";
@@ -104,6 +114,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertNull(target.value);
}
+ @Test
public void testExplicitSerializationOfNullCollectionMembers() {
Gson gson = gsonBuilder.create();
ClassWithMembers target = new ClassWithMembers();
@@ -111,6 +122,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertTrue(json.contains("\"col\":null"));
}
+ @Test
public void testExplicitSerializationOfNullStringMembers() {
Gson gson = gsonBuilder.create();
ClassWithMembers target = new ClassWithMembers();
@@ -118,6 +130,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertTrue(json.contains("\"str\":null"));
}
+ @Test
public void testCustomSerializationOfNulls() {
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
Gson gson = gsonBuilder.create();
@@ -127,6 +140,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertEquals(expected, actual);
}
+ @Test
public void testPrintPrintingObjectWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
@@ -138,6 +152,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertTrue(result.contains("\"str\":null"));
}
+ @Test
public void testPrintPrintingArraysWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
@@ -150,6 +165,7 @@ public class NullObjectAndFieldTest extends TestCase {
}
// test for issue 389
+ @Test
public void testAbsentJsonElementsAreSetToNull() {
Gson gson = new Gson();
ClassWithInitializedMembers target =
@@ -200,6 +216,7 @@ public class NullObjectAndFieldTest extends TestCase {
}
}
+ @Test
public void testExplicitNullSetsFieldToNullDuringDeserialization() {
Gson gson = new Gson();
String json = "{value:null}";
@@ -207,6 +224,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertNull(obj.value);
}
+ @Test
public void testCustomTypeAdapterPassesNullSerialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonSerializer<ObjectWithField>() {
@@ -221,6 +239,7 @@ public class NullObjectAndFieldTest extends TestCase {
assertFalse(json.contains("value1"));
}
+ @Test
public void testCustomTypeAdapterPassesNullDesrialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonDeserializer<ObjectWithField>() {