Added checks to ensure that we do not serialize NaN or postiive or negative infinity for BigDecimal.

This commit is contained in:
Inderjeet Singh 2008-12-18 23:56:38 +00:00
parent fce34ea057
commit f7e4d5e4bb

View File

@ -351,10 +351,8 @@ public class PrimitiveTest extends TestCase {
public void testDoubleNaNSerializationNotSupported() { public void testDoubleNaNSerializationNotSupported() {
try { try {
double d = Double.NaN; gson.toJson((double)Double.NaN);
gson.toJson(d); gson.toJson(Double.NaN);
Double dw = Double.NaN;
gson.toJson(dw);
fail("Gson should not accept NaN for serialization"); fail("Gson should not accept NaN for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -362,20 +360,16 @@ public class PrimitiveTest extends TestCase {
public void testDoubleNaNDeserializationNotSupported() { public void testDoubleNaNDeserializationNotSupported() {
try { try {
String json = "NaN"; gson.fromJson("NaN", Double.class);
assertEquals(Double.NaN, gson.fromJson(json, Double.class)); gson.fromJson("NaN", double.class);
assertEquals(Double.NaN, gson.fromJson(json, double.class));
fail("Gson should not accept NaN for deserialization"); fail("Gson should not accept NaN for deserialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
} }
public void testFloatNaNSerializationNotSupported() { public void testFloatNaNSerializationNotSupported() {
try { try {
float f = Float.NaN; gson.toJson((float)Float.NaN);
gson.toJson(f); gson.toJson(Float.NaN);
Float fw = Float.NaN;
gson.toJson(fw);
fail("Gson should not accept NaN for serialization"); fail("Gson should not accept NaN for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -383,9 +377,25 @@ public class PrimitiveTest extends TestCase {
public void testFloatNaNDeserializationNotSupported() { public void testFloatNaNDeserializationNotSupported() {
try { try {
String json = "NaN"; gson.fromJson("NaN", Float.class);
assertEquals(Float.NaN, gson.fromJson(json, Float.class)); gson.fromJson("NaN", float.class);
assertEquals(Float.NaN, gson.fromJson(json, float.class)); fail("Gson should not accept NaN for deserialization");
} catch (JsonParseException expected) {
}
}
public void testBigDecimalNaNSerializationNotSupported() {
try {
gson.toJson(new BigDecimal(Double.NaN));
fail("Gson should not accept NaN for serialization");
} catch (IllegalArgumentException expected) {
}
}
public void testBigDecimalNaNDeserializationNotSupported() {
try {
gson.fromJson("NaN", BigDecimal.class);
fail("Gson should not accept NaN for deserialization"); fail("Gson should not accept NaN for deserialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
@ -393,10 +403,8 @@ public class PrimitiveTest extends TestCase {
public void testDoubleInfinitySerializationNotSupported() { public void testDoubleInfinitySerializationNotSupported() {
try { try {
double d = Double.POSITIVE_INFINITY; gson.toJson((double)Double.POSITIVE_INFINITY);
gson.toJson(d); gson.toJson(Double.POSITIVE_INFINITY);
Double dw = Double.POSITIVE_INFINITY;
gson.toJson(dw);
fail("Gson should not accept positive infinity for serialization"); fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -404,9 +412,8 @@ public class PrimitiveTest extends TestCase {
public void testDoubleInfinityDeserializationNotSupported() { public void testDoubleInfinityDeserializationNotSupported() {
try { try {
String json = "Infinity"; gson.fromJson("Infinity", Double.class);
assertEquals(Double.POSITIVE_INFINITY, gson.fromJson(json, Double.class)); gson.fromJson("Infinity", double.class);
assertEquals(Double.POSITIVE_INFINITY, gson.fromJson(json, double.class));
fail("Gson should not accept positive infinity for deserialization"); fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
@ -414,10 +421,8 @@ public class PrimitiveTest extends TestCase {
public void testFloatInfinitySerializationNotSupported() { public void testFloatInfinitySerializationNotSupported() {
try { try {
float f = Float.POSITIVE_INFINITY; gson.toJson((float)Float.POSITIVE_INFINITY);
gson.toJson(f); gson.toJson(Float.POSITIVE_INFINITY);
Float fw = Float.POSITIVE_INFINITY;
gson.toJson(fw);
fail("Gson should not accept positive infinity for serialization"); fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -425,9 +430,24 @@ public class PrimitiveTest extends TestCase {
public void testFloatInfinityDeserializationNotSupported() { public void testFloatInfinityDeserializationNotSupported() {
try { try {
String json = "Infinity"; gson.fromJson("Infinity", Float.class);
assertEquals(Float.POSITIVE_INFINITY, gson.fromJson(json, Float.class)); gson.fromJson("Infinity", float.class);
assertEquals(Float.POSITIVE_INFINITY, gson.fromJson(json, float.class)); fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
}
public void testBigDecimalInfinitySerializationNotSupported() {
try {
gson.toJson(new BigDecimal(Double.POSITIVE_INFINITY));
fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) {
}
}
public void testBigDecimalInfinityDeserializationNotSupported() {
try {
gson.fromJson("Infinity", BigDecimal.class);
fail("Gson should not accept positive infinity for deserialization"); fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
@ -435,10 +455,8 @@ public class PrimitiveTest extends TestCase {
public void testNegativeInfinitySerializationNotSupported() { public void testNegativeInfinitySerializationNotSupported() {
try { try {
double d = Double.NEGATIVE_INFINITY; gson.toJson((double)Double.NEGATIVE_INFINITY);
gson.toJson(d); gson.toJson(Double.NEGATIVE_INFINITY);
Double dw = Double.NEGATIVE_INFINITY;
gson.toJson(dw);
fail("Gson should not accept positive infinity for serialization"); fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -446,20 +464,17 @@ public class PrimitiveTest extends TestCase {
public void testNegativeInfinityDeserializationNotSupported() { public void testNegativeInfinityDeserializationNotSupported() {
try { try {
String json = "-Infinity"; gson.fromJson("-Infinity", double.class);
assertEquals(Double.NEGATIVE_INFINITY, gson.fromJson(json, double.class)); gson.fromJson("-Infinity", Double.class);
assertEquals(Double.NEGATIVE_INFINITY, gson.fromJson(json, Double.class)); fail("Gson should not accept positive infinity for deserialization");
fail("Gson should not accept positive infinity for serialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
} }
public void testNegativeInfinityFloatSerializationNotSupported() { public void testNegativeInfinityFloatSerializationNotSupported() {
try { try {
float f = Float.NEGATIVE_INFINITY; gson.toJson((float)Float.NEGATIVE_INFINITY);
gson.toJson(f); gson.toJson(Float.NEGATIVE_INFINITY);
Float fw = Float.NEGATIVE_INFINITY;
gson.toJson(fw);
fail("Gson should not accept positive infinity for serialization"); fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) { } catch (IllegalArgumentException expected) {
} }
@ -467,10 +482,25 @@ public class PrimitiveTest extends TestCase {
public void testNegativeInfinityFloatDeserializationNotSupported() { public void testNegativeInfinityFloatDeserializationNotSupported() {
try { try {
String json = "-Infinity"; gson.fromJson("-Infinity", float.class);
assertEquals(Float.NEGATIVE_INFINITY, gson.fromJson(json, float.class)); gson.fromJson("-Infinity", Float.class);
assertEquals(Float.NEGATIVE_INFINITY, gson.fromJson(json, Float.class)); fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
}
public void testNegativeInfinityBigDecimalSerializationNotSupported() {
try {
gson.toJson(new BigDecimal(Double.NEGATIVE_INFINITY));
fail("Gson should not accept positive infinity for serialization"); fail("Gson should not accept positive infinity for serialization");
} catch (IllegalArgumentException expected) {
}
}
public void testNegativeInfinityBigDecimalDeserializationNotSupported() {
try {
gson.fromJson("-Infinity", BigDecimal.class);
fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) { } catch (JsonParseException expected) {
} }
} }