Minor test fixes. Explicitly fail test if the expected exception was not thrown.
This commit is contained in:
parent
de0f8da151
commit
3fc4bcc061
@ -31,7 +31,6 @@ import java.lang.reflect.Type;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functional tests for Json serialization and deserialization of arrays.
|
* Functional tests for Json serialization and deserialization of arrays.
|
||||||
*
|
*
|
||||||
@ -63,7 +62,7 @@ public class ArrayTest extends TestCase {
|
|||||||
try {
|
try {
|
||||||
gson.fromJson(json, int[].class);
|
gson.fromJson(json, int[].class);
|
||||||
fail("Gson should not deserialize array elements with missing ,");
|
fail("Gson should not deserialize array elements with missing ,");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +80,7 @@ public class ArrayTest extends TestCase {
|
|||||||
|
|
||||||
actualObject = gson.fromJson("[ ]", int[].class);
|
actualObject = gson.fromJson("[ ]", int[].class);
|
||||||
assertTrue(actualObject.length == 0);
|
assertTrue(actualObject.length == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNullsInArraySerialization() {
|
public void testNullsInArraySerialization() {
|
||||||
String[] array = {"foo", null, "bar"};
|
String[] array = {"foo", null, "bar"};
|
||||||
@ -98,19 +97,19 @@ public class ArrayTest extends TestCase {
|
|||||||
assertEquals(expected[i], target[i]);
|
assertEquals(expected[i], target[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSingleNullInArraySerialization() {
|
public void testSingleNullInArraySerialization() {
|
||||||
BagOfPrimitives[] array = new BagOfPrimitives[1];
|
BagOfPrimitives[] array = new BagOfPrimitives[1];
|
||||||
array[0] = null;
|
array[0] = null;
|
||||||
String json = gson.toJson(array);
|
String json = gson.toJson(array);
|
||||||
assertEquals("[null]", json);
|
assertEquals("[null]", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSingleNullInArrayDeserialization() {
|
public void testSingleNullInArrayDeserialization() {
|
||||||
BagOfPrimitives[] array = gson.fromJson("[null]", BagOfPrimitives[].class);
|
BagOfPrimitives[] array = gson.fromJson("[null]", BagOfPrimitives[].class);
|
||||||
assertNull(array[0]);
|
assertNull(array[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNullsInArrayWithSerializeNullPropertySetSerialization() {
|
public void testNullsInArrayWithSerializeNullPropertySetSerialization() {
|
||||||
gson = new GsonBuilder().serializeNulls().create();
|
gson = new GsonBuilder().serializeNulls().create();
|
||||||
String[] array = {"foo", null, "bar"};
|
String[] array = {"foo", null, "bar"};
|
||||||
@ -136,13 +135,13 @@ public class ArrayTest extends TestCase {
|
|||||||
String output = gson.toJson(s);
|
String output = gson.toJson(s);
|
||||||
assertEquals("[\"hello\"]", output);
|
assertEquals("[\"hello\"]", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSingleStringArrayDeserialization() throws Exception {
|
public void testSingleStringArrayDeserialization() throws Exception {
|
||||||
String json = "[\"hello\"]";
|
String json = "[\"hello\"]";
|
||||||
String[] arrayType = gson.fromJson(json, String[].class);
|
String[] arrayType = gson.fromJson(json, String[].class);
|
||||||
assertEquals(1, arrayType.length);
|
assertEquals(1, arrayType.length);
|
||||||
assertEquals("hello", arrayType[0]);
|
assertEquals("hello", arrayType[0]);
|
||||||
|
|
||||||
String type = gson.fromJson(json, String.class);
|
String type = gson.fromJson(json, String.class);
|
||||||
assertEquals("hello", type);
|
assertEquals("hello", type);
|
||||||
}
|
}
|
||||||
@ -181,7 +180,7 @@ public class ArrayTest extends TestCase {
|
|||||||
MoreAsserts.assertEquals(new Integer[] { 1, 2 }, target[0].toArray(new Integer[0]));
|
MoreAsserts.assertEquals(new Integer[] { 1, 2 }, target[0].toArray(new Integer[0]));
|
||||||
MoreAsserts.assertEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0]));
|
MoreAsserts.assertEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayOfPrimitivesWithCustomTypeAdapter() throws Exception {
|
public void testArrayOfPrimitivesWithCustomTypeAdapter() throws Exception {
|
||||||
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
||||||
gson = new GsonBuilder()
|
gson = new GsonBuilder()
|
||||||
@ -192,12 +191,12 @@ public class ArrayTest extends TestCase {
|
|||||||
String serializedValue = gson.toJson(value);
|
String serializedValue = gson.toJson(value);
|
||||||
String expected = "[" + String.valueOf(value[0] + CrazyLongTypeAdapter.DIFFERENCE) + "]";
|
String expected = "[" + String.valueOf(value[0] + CrazyLongTypeAdapter.DIFFERENCE) + "]";
|
||||||
assertEquals(expected, serializedValue);
|
assertEquals(expected, serializedValue);
|
||||||
|
|
||||||
long[] deserializedValue = gson.fromJson(serializedValue, long[].class);
|
long[] deserializedValue = gson.fromJson(serializedValue, long[].class);
|
||||||
assertEquals(1, deserializedValue.length);
|
assertEquals(1, deserializedValue.length);
|
||||||
assertEquals(value[0], deserializedValue[0]);
|
assertEquals(value[0], deserializedValue[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
|
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
|
||||||
Object[] objs = new Object[]{1, "abc", 0.3f, 5L};
|
Object[] objs = new Object[]{1, "abc", 0.3f, 5L};
|
||||||
String json = gson.toJson(objs);
|
String json = gson.toJson(objs);
|
||||||
@ -219,18 +218,20 @@ public class ArrayTest extends TestCase {
|
|||||||
String json = "[1,'abc',{a:1},5]";
|
String json = "[1,'abc',{a:1},5]";
|
||||||
try {
|
try {
|
||||||
gson.fromJson(json, Object[].class);
|
gson.fromJson(json, Object[].class);
|
||||||
|
fail("This is crazy....how did we deserialize it!!!");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayWithoutTypeInfoDeserialization() throws Exception {
|
public void testArrayWithoutTypeInfoDeserialization() throws Exception {
|
||||||
String json = "[1,'abc',[1,2],5]";
|
String json = "[1,'abc',[1,2],5]";
|
||||||
try {
|
try {
|
||||||
gson.fromJson(json, Object[].class);
|
gson.fromJson(json, Object[].class);
|
||||||
|
fail("This is crazy....how did we deserialize it!!!");
|
||||||
} catch (JsonParseException expected) {
|
} catch (JsonParseException expected) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
|
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
|
||||||
ClassWithObjects classWithObjects = new ClassWithObjects();
|
ClassWithObjects classWithObjects = new ClassWithObjects();
|
||||||
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
||||||
@ -239,7 +240,7 @@ public class ArrayTest extends TestCase {
|
|||||||
|
|
||||||
Object[] objects = new Object[] { classWithObjects, bagOfPrimitives };
|
Object[] objects = new Object[] { classWithObjects, bagOfPrimitives };
|
||||||
String json = gson.toJson(objects);
|
String json = gson.toJson(objects);
|
||||||
|
|
||||||
assertTrue(json.contains(classWithObjectsJson));
|
assertTrue(json.contains(classWithObjectsJson));
|
||||||
assertTrue(json.contains(bagOfPrimitivesJson));
|
assertTrue(json.contains(bagOfPrimitivesJson));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user