Skip to content

Commit

Permalink
Add bit more testing, related to investigation of #2101 (although ult…
Browse files Browse the repository at this point in the history
…imately determined there is no bug)
  • Loading branch information
cowtowncoder committed Sep 7, 2018
1 parent 6abb1dd commit 5256bfc
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.io.*;
import java.lang.reflect.Array;
import java.util.List;

import org.junit.Assert;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.JsonMappingException.Reference;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

/**
* Unit tests for verifying handling of simple basic non-structured
Expand Down Expand Up @@ -102,6 +105,14 @@ static class WrappersBean
public Double doubleValue;
}

// [databind#2101]
static class PrimitiveCreatorBean
{
@JsonCreator
public PrimitiveCreatorBean(@JsonProperty(value="a",required=true) int a,
@JsonProperty(value="b",required=true) int b) { }
}

private final ObjectMapper MAPPER = new ObjectMapper();

/*
Expand Down Expand Up @@ -220,19 +231,18 @@ public void testCharacterWrapper() throws Exception
final CharacterWrapperBean wrapper = MAPPER.readValue("{\"v\":null}", CharacterWrapperBean.class);
assertNotNull(wrapper);
assertNull(wrapper.getV());

final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

try {
mapper.readValue("{\"v\":null}", CharacterBean.class);
MAPPER.readerFor(CharacterBean.class)
.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.readValue("{\"v\":null}");
fail("Attempting to deserialize a 'null' JSON reference into a 'char' property did not throw an exception");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "cannot map `null`");
//Exception thrown as required
}

mapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
final CharacterBean charBean = MAPPER.readValue("{\"v\":null}", CharacterBean.class);
final CharacterBean charBean = MAPPER.readerFor(CharacterBean.class)
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.readValue("{\"v\":null}");
assertNotNull(wrapper);
assertEquals('\u0000', charBean.getV());
}
Expand Down Expand Up @@ -272,7 +282,7 @@ public void testIntPrimitive() throws Exception
try {
mapper.readValue("{\"v\":[3]}", IntBean.class);
fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
} catch (JsonMappingException exp) {
} catch (MismatchedInputException exp) {
//Correctly threw exception
}

Expand All @@ -287,7 +297,7 @@ public void testIntPrimitive() throws Exception
try {
mapper.readValue("[{\"v\":[3,3]}]", IntBean.class);
fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
} catch (JsonMappingException exp) {
} catch (MismatchedInputException exp) {
//threw exception as required
}

Expand All @@ -300,7 +310,7 @@ public void testIntPrimitive() throws Exception
assertEquals(1, array.length);
assertEquals(0, array[0]);
}

public void testLongWrapper() throws Exception
{
Long result = MAPPER.readValue("12345678901", Long.class);
Expand Down Expand Up @@ -335,7 +345,7 @@ public void testLongPrimitive() throws Exception
try {
mapper.readValue("{\"v\":[3]}", LongBean.class);
fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
} catch (JsonMappingException exp) {
} catch (MismatchedInputException exp) {
//Correctly threw exception
}

Expand All @@ -350,7 +360,7 @@ public void testLongPrimitive() throws Exception
try {
mapper.readValue("[{\"v\":[3,3]}]", LongBean.class);
fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
} catch (JsonMappingException exp) {
} catch (MismatchedInputException exp) {
//threw exception as required
}

Expand Down Expand Up @@ -473,7 +483,7 @@ public void testDoubleAsArray() throws Exception
try {
mapper.readValue("[{\"v\":[" + value + "," + value + "]}]", DoubleBean.class);
fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
} catch (JsonMappingException exp) {
} catch (MismatchedInputException exp) {
//threw exception as required
}

Expand Down Expand Up @@ -541,7 +551,7 @@ private void _testEmptyToNullCoercion(Class<?> primType, Object emptyValue) thro
intR.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.readValue("\"\"");
fail("Should not have passed");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot coerce empty String");
}
}
Expand Down Expand Up @@ -599,7 +609,6 @@ public void testSequenceOfInts() throws Exception
jp.close();
}


/*
/**********************************************************
/* Empty String coercion, handling
Expand Down Expand Up @@ -712,56 +721,85 @@ public void testNullForPrimitives() throws IOException
try {
reader.readValue("{\"booleanValue\":null}");
fail("Expected failure for boolean + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type boolean");
verifyPath(e, "booleanValue");
}
// byte/char/short/int/long
try {
reader.readValue("{\"byteValue\":null}");
fail("Expected failure for byte + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type byte");
verifyPath(e, "byteValue");
}
try {
reader.readValue("{\"charValue\":null}");
fail("Expected failure for char + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type char");
verifyPath(e, "charValue");
}
try {
reader.readValue("{\"shortValue\":null}");
fail("Expected failure for short + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type short");
verifyPath(e, "shortValue");
}
try {
reader.readValue("{\"intValue\":null}");
fail("Expected failure for int + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type int");
verifyPath(e, "intValue");
}
try {
reader.readValue("{\"longValue\":null}");
fail("Expected failure for long + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type long");
verifyPath(e, "longValue");
}

// float/double
try {
reader.readValue("{\"floatValue\":null}");
fail("Expected failure for float + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type float");
verifyPath(e, "floatValue");
}
try {
reader.readValue("{\"doubleValue\":null}");
fail("Expected failure for double + null");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type double");
verifyPath(e, "doubleValue");
}
}

// [databind#2101]
public void testNullForPrimitivesViaCreator() throws IOException
{
try {
/*PrimitiveCreatorBean bean =*/ MAPPER
.readerFor(PrimitiveCreatorBean.class)
.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.readValue(aposToQuotes("{'a': null}"));
fail("Expected failure for `int` and `null`");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type int");
verifyPath(e, "a");
}
}

private void verifyPath(MismatchedInputException e, String propName) {
final List<Reference> path = e.getPath();
assertEquals(1, path.size());
assertEquals(propName, path.get(0).getFieldName());
}

public void testNullForPrimitiveArrays() throws IOException
{
_testNullForPrimitiveArrays(boolean[].class, Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.fasterxml.jackson.failing;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

public class EnumAsIndexMapKey1877Test extends BaseMapTest
{
public enum Type {
ANY,
OTHER
}

static class Container {
private Type simpleType;
private Map<Type, String> map;

public Type getSimpleType() {
return simpleType;
}

public void setSimpleType(Type simpleType) {
this.simpleType= simpleType;
}

public Map<Type, String> getMap() {
return map;
}

public void setMap(Map<Type, String> map) {
this.map = map;
}
}
// [databind#1877]
public void testEnumAsIndexMapKey() throws Exception
{
ObjectMapper mapper = newObjectMapper();

Map<Type, String> map = new HashMap<>();
map.put(Type.OTHER, "hello world");
Container container = new Container();
container.setSimpleType(Type.ANY);
container.setMap(map);

String json = mapper
.writer().with(SerializationFeature.WRITE_ENUMS_USING_INDEX)
.writeValueAsString(container);

Container cont;
try {
cont = mapper
.readerFor(Container.class)
.readValue(json);
} catch (JsonMappingException e) {
throw e;
}
assertNotNull(cont);
assertEquals(1, container.getMap().size());
InvalidFormatException foo = null;

assertSame(Type.OTHER, container.getMap().keySet().iterator().next());
}
}

0 comments on commit 5256bfc

Please sign in to comment.