Skip to content

Commit

Permalink
Test refactoring/reorg to group related JDK type deser tests together…
Browse files Browse the repository at this point in the history
… more closely
  • Loading branch information
cowtowncoder committed Aug 31, 2016
1 parent c22d5c5 commit d00e63c
Show file tree
Hide file tree
Showing 7 changed files with 1,258 additions and 1,263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class TestJDKAtomicTypes
public class JDKAtomicTypesTest
extends com.fasterxml.jackson.databind.BaseMapTest
{
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.fasterxml.jackson.databind.deser;

import java.io.IOException;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

public class NumberDeserTest extends BaseMapTest
public class JDKNumberDeserTest extends BaseMapTest
{
/*
/**********************************************************************
Expand Down Expand Up @@ -78,7 +81,6 @@ public void testDoubleInf() throws Exception
assertEquals(Double.valueOf(Double.NEGATIVE_INFINITY), result);
}

// [JACKSON-349]
public void testEmptyAsNumber() throws Exception
{
assertNull(MAPPER.readValue(quote(""), Integer.class));
Expand All @@ -89,8 +91,6 @@ public void testEmptyAsNumber() throws Exception
assertNull(MAPPER.readValue(quote(""), BigDecimal.class));
}

// // Tests for [JACKSON-668]

public void testDeserializeDecimalHappyPath() throws Exception {
String json = "{\"defaultValue\": { \"value\": 123 } }";
MyBeanHolder result = MAPPER.readValue(json, MyBeanHolder.class);
Expand Down Expand Up @@ -118,7 +118,7 @@ public void testDeserializeDecimalProperExceptionWhenIdSet() throws Exception {
}

// And then [databind#852]
public void testScientificNotationForString() throws Exception
public void testScientificNotationAsStringForNumber() throws Exception
{
Object ob = MAPPER.readValue("\"3E-8\"", Number.class);
assertEquals(Double.class, ob.getClass());
Expand All @@ -129,4 +129,123 @@ public void testScientificNotationForString() throws Exception
ob = MAPPER.readValue("\"123456789012\"", Number.class);
assertEquals(Long.class, ob.getClass());
}

public void testIntAsNumber() throws Exception
{
/* Even if declared as 'generic' type, should return using most
* efficient type... here, Integer
*/
Number result = MAPPER.readValue(" 123 ", Number.class);
assertEquals(Integer.valueOf(123), result);
}

public void testLongAsNumber() throws Exception
{
// And beyond int range, should get long
long exp = 1234567890123L;
Number result = MAPPER.readValue(String.valueOf(exp), Number.class);
assertEquals(Long.valueOf(exp), result);
}

public void testBigIntAsNumber() throws Exception
{
// and after long, BigInteger
BigInteger biggie = new BigInteger("1234567890123456789012345678901234567890");
Number result = MAPPER.readValue(biggie.toString(), Number.class);
assertEquals(BigInteger.class, biggie.getClass());
assertEquals(biggie, result);
}

public void testIntTypeOverride() throws Exception
{
/* Slight twist; as per [JACKSON-100], can also request binding
* to BigInteger even if value would fit in Integer
*/
ObjectReader r = MAPPER.reader(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);

BigInteger exp = BigInteger.valueOf(123L);

// first test as any Number
Number result = r.forType(Number.class).readValue(" 123 ");
assertEquals(BigInteger.class, result.getClass());
assertEquals(exp, result);

// then as any Object
/*Object value =*/ r.forType(Object.class).readValue("123");
assertEquals(BigInteger.class, result.getClass());
assertEquals(exp, result);

// and as JsonNode
JsonNode node = r.readTree(" 123");
assertTrue(node.isBigInteger());
assertEquals(123, node.asInt());
}

public void testDoubleAsNumber() throws Exception
{
Number result = MAPPER.readValue(new StringReader(" 1.0 "), Number.class);
assertEquals(Double.valueOf(1.0), result);
}

public void testFpTypeOverrideSimple() throws Exception
{
ObjectReader r = MAPPER.reader(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
BigDecimal dec = new BigDecimal("0.1");

// First test generic stand-alone Number
Number result = r.forType(Number.class).readValue(dec.toString());
assertEquals(BigDecimal.class, result.getClass());
assertEquals(dec, result);

// Then plain old Object
Object value = r.forType(Object.class).readValue(dec.toString());
assertEquals(BigDecimal.class, result.getClass());
assertEquals(dec, value);

JsonNode node = r.readTree(dec.toString());
assertTrue(node.isBigDecimal());
assertEquals(dec.doubleValue(), node.asDouble());
}

public void testFpTypeOverrideStructured() throws Exception
{
ObjectReader r = MAPPER.reader(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

BigDecimal dec = new BigDecimal("-19.37");
// List element types
@SuppressWarnings("unchecked")
List<Object> list = (List<Object>) r.forType(List.class).readValue("[ "+dec.toString()+" ]");
assertEquals(1, list.size());
Object val = list.get(0);
assertEquals(BigDecimal.class, val.getClass());
assertEquals(dec, val);

// and a map
Map<?,?> map = r.forType(Map.class).readValue("{ \"a\" : "+dec.toString()+" }");
assertEquals(1, map.size());
val = map.get("a");
assertEquals(BigDecimal.class, val.getClass());
assertEquals(dec, val);
}

// [databind#504]
public void testForceIntsToLongs() throws Exception
{
ObjectReader r = MAPPER.reader(DeserializationFeature.USE_LONG_FOR_INTS);

Object ob = r.forType(Object.class).readValue("42");
assertEquals(Long.class, ob.getClass());
assertEquals(Long.valueOf(42L), ob);

Number n = r.forType(Number.class).readValue("42");
assertEquals(Long.class, n.getClass());
assertEquals(Long.valueOf(42L), n);

// and one more: should get proper node as well
JsonNode node = r.readTree("42");
if (!node.isLong()) {
fail("Expected LongNode, got: "+node.getClass().getName());
}
assertEquals(42, node.asInt());
}
}
Loading

0 comments on commit d00e63c

Please sign in to comment.