1 package com.fasterxml.jackson.databind.convert; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.core.type.TypeReference; 6 import com.fasterxml.jackson.databind.*; 7 8 import com.fasterxml.jackson.databind.cfg.CoercionAction; 9 import com.fasterxml.jackson.databind.cfg.CoercionInputShape; 10 11 public class CoerceContainersTest extends BaseMapTest 12 { 13 private final String JSON_EMPTY = quote(""); 14 15 private final ObjectMapper VANILLA_MAPPER = sharedMapper(); 16 17 private final ObjectMapper COERCING_MAPPER = newJsonMapper(); 18 { 19 COERCING_MAPPER.coercionConfigDefaults().setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); 20 } 21 22 /* 23 /******************************************************** 24 /* Tests for collections 25 /******************************************************** 26 */ 27 testScalarCollections()28 public void testScalarCollections() throws Exception 29 { 30 final JavaType listType = VANILLA_MAPPER.getTypeFactory() 31 .constructType(new TypeReference<List<Double>>() { }); 32 _verifyNoCoercion(listType); 33 List<Double> result = _readWithCoercion(listType); 34 assertNotNull(result); 35 assertEquals(0, result.size()); 36 } 37 testStringCollections()38 public void testStringCollections() throws Exception 39 { 40 final JavaType listType = VANILLA_MAPPER.getTypeFactory() 41 .constructType(new TypeReference<List<String>>() { }); 42 _verifyNoCoercion(listType); 43 List<String> result = _readWithCoercion(listType); 44 assertNotNull(result); 45 assertEquals(0, result.size()); 46 } 47 48 /* 49 /******************************************************** 50 /* Tests for Maps 51 /******************************************************** 52 */ 53 testScalarMap()54 public void testScalarMap() throws Exception 55 { 56 final JavaType mapType = VANILLA_MAPPER.getTypeFactory() 57 .constructType(new TypeReference<Map<Long, Boolean>>() { }); 58 _verifyNoCoercion(mapType); 59 Map<?,?> result = _readWithCoercion(mapType); 60 assertNotNull(result); 61 assertEquals(0, result.size()); 62 } 63 testEnumMap()64 public void testEnumMap() throws Exception 65 { 66 final JavaType mapType = VANILLA_MAPPER.getTypeFactory() 67 .constructType(new TypeReference<EnumMap<ABC, Boolean>>() { }); 68 _verifyNoCoercion(mapType); 69 Map<?,?> result = _readWithCoercion(mapType); 70 assertNotNull(result); 71 assertEquals(0, result.size()); 72 } 73 74 /* 75 /******************************************************** 76 /* Tests for arrays 77 /******************************************************** 78 */ 79 testObjectArray()80 public void testObjectArray() throws Exception 81 { 82 final JavaType arrayType = VANILLA_MAPPER.getTypeFactory() 83 .constructType(new TypeReference<Object[]>() { }); 84 _verifyNoCoercion(arrayType); 85 Object[] result = _readWithCoercion(arrayType); 86 assertNotNull(result); 87 assertEquals(0, result.length); 88 } 89 testStringArray()90 public void testStringArray() throws Exception 91 { 92 final JavaType arrayType = VANILLA_MAPPER.getTypeFactory() 93 .constructType(new TypeReference<String[]>() { }); 94 _verifyNoCoercion(arrayType); 95 String[] result = _readWithCoercion(arrayType); 96 assertNotNull(result); 97 assertEquals(0, result.length); 98 } 99 testBooleanArray()100 public void testBooleanArray() throws Exception 101 { 102 _verifyNoCoercion(boolean[].class); 103 boolean[] result = _readWithCoercion(boolean[].class); 104 assertNotNull(result); 105 assertEquals(0, result.length); 106 } 107 testIntArray()108 public void testIntArray() throws Exception 109 { 110 _verifyNoCoercion(int[].class); 111 int[] result = _readWithCoercion(int[].class); 112 assertNotNull(result); 113 assertEquals(0, result.length); 114 } 115 testLongArray()116 public void testLongArray() throws Exception 117 { 118 _verifyNoCoercion(long[].class); 119 long[] result = _readWithCoercion(long[].class); 120 assertNotNull(result); 121 assertEquals(0, result.length); 122 } 123 testFloatArray()124 public void testFloatArray() throws Exception 125 { 126 _verifyNoCoercion(float[].class); 127 float[] result = _readWithCoercion(float[].class); 128 assertNotNull(result); 129 assertEquals(0, result.length); 130 } 131 testDoubleArray()132 public void testDoubleArray() throws Exception 133 { 134 _verifyNoCoercion(double[].class); 135 double[] result = _readWithCoercion(double[].class); 136 assertNotNull(result); 137 assertEquals(0, result.length); 138 } 139 testPOJOArray()140 public void testPOJOArray() throws Exception 141 { 142 _verifyNoCoercion(StringWrapper[].class); 143 StringWrapper[] result = _readWithCoercion(StringWrapper[].class); 144 assertNotNull(result); 145 assertEquals(0, result.length); 146 } 147 148 /* 149 /******************************************************** 150 /* Helper methods 151 /******************************************************** 152 */ 153 _verifyNoCoercion(Class<?> targetType)154 private void _verifyNoCoercion(Class<?> targetType) throws Exception { 155 _verifyNoCoercion(VANILLA_MAPPER.constructType(targetType)); 156 } 157 _verifyNoCoercion(JavaType targetType)158 private void _verifyNoCoercion(JavaType targetType) throws Exception { 159 try { 160 VANILLA_MAPPER.readerFor(targetType).readValue(JSON_EMPTY); 161 fail("Should not pass"); 162 } catch (Exception e) { 163 verifyException(e, "Cannot deserialize value of type"); 164 verifyException(e, "from empty String"); 165 } 166 } 167 _readWithCoercion(Class<?> targetType)168 private <T> T _readWithCoercion(Class<?> targetType) throws Exception { 169 return COERCING_MAPPER.readerFor(targetType).readValue(JSON_EMPTY); 170 } 171 _readWithCoercion(JavaType targetType)172 private <T> T _readWithCoercion(JavaType targetType) throws Exception { 173 return COERCING_MAPPER.readerFor(targetType).readValue(JSON_EMPTY); 174 } 175 } 176