|
| 1 | +package com.fasterxml.jackson.databind; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.JsonFactory; |
| 7 | +import com.fasterxml.jackson.core.JsonParser; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext; |
| 10 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 11 | +import com.fasterxml.jackson.databind.node.*; |
| 12 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 13 | + |
| 14 | +public class ObjectMapperTest extends BaseMapTest |
| 15 | +{ |
| 16 | + static class Bean { |
| 17 | + int value = 3; |
| 18 | + |
| 19 | + public void setX(int v) { value = v; } |
| 20 | + } |
| 21 | + |
| 22 | + // for [Issue#206] |
| 23 | + @SuppressWarnings("serial") |
| 24 | + static class CustomMapper extends ObjectMapper { |
| 25 | + @Override |
| 26 | + protected DefaultDeserializationContext createDeserializationContext(JsonParser jp, |
| 27 | + DeserializationConfig cfg) { |
| 28 | + return super.createDeserializationContext(jp, cfg); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("serial") |
| 33 | + static class MyAnnotationIntrospector extends JacksonAnnotationIntrospector { } |
| 34 | + |
| 35 | + /* |
| 36 | + /********************************************************** |
| 37 | + /* Test methods |
| 38 | + /********************************************************** |
| 39 | + */ |
| 40 | + |
| 41 | + final static ObjectMapper MAPPER = new ObjectMapper(); |
| 42 | + |
| 43 | + public void testProps() |
| 44 | + { |
| 45 | + ObjectMapper m = new ObjectMapper(); |
| 46 | + // should have default factory |
| 47 | + assertNotNull(m.getNodeFactory()); |
| 48 | + JsonNodeFactory nf = JsonNodeFactory.instance; |
| 49 | + m.setNodeFactory(nf); |
| 50 | + assertNull(m.getInjectableValues()); |
| 51 | + assertSame(nf, m.getNodeFactory()); |
| 52 | + } |
| 53 | + |
| 54 | + public void testSupport() |
| 55 | + { |
| 56 | + assertTrue(MAPPER.canSerialize(String.class)); |
| 57 | + assertTrue(MAPPER.canDeserialize(TypeFactory.defaultInstance().constructType(String.class))); |
| 58 | + } |
| 59 | + |
| 60 | + public void testTreeRead() throws Exception |
| 61 | + { |
| 62 | + String JSON = "{ }"; |
| 63 | + JsonNode n = MAPPER.readTree(JSON); |
| 64 | + assertTrue(n instanceof ObjectNode); |
| 65 | + |
| 66 | + n = MAPPER.readTree(new StringReader(JSON)); |
| 67 | + assertTrue(n instanceof ObjectNode); |
| 68 | + |
| 69 | + n = MAPPER.readTree(new ByteArrayInputStream(JSON.getBytes("UTF-8"))); |
| 70 | + assertTrue(n instanceof ObjectNode); |
| 71 | + } |
| 72 | + |
| 73 | + // Test to ensure that we can check property ordering defaults... |
| 74 | + public void testConfigForPropertySorting() throws Exception |
| 75 | + { |
| 76 | + ObjectMapper m = new ObjectMapper(); |
| 77 | + |
| 78 | + // sort-alphabetically is disabled by default: |
| 79 | + assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 80 | + SerializationConfig sc = m.getSerializationConfig(); |
| 81 | + assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 82 | + assertFalse(sc.shouldSortPropertiesAlphabetically()); |
| 83 | + DeserializationConfig dc = m.getDeserializationConfig(); |
| 84 | + assertFalse(dc.shouldSortPropertiesAlphabetically()); |
| 85 | + |
| 86 | + // but when enabled, should be visible: |
| 87 | + m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); |
| 88 | + sc = m.getSerializationConfig(); |
| 89 | + assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 90 | + assertTrue(sc.shouldSortPropertiesAlphabetically()); |
| 91 | + dc = m.getDeserializationConfig(); |
| 92 | + // and not just via SerializationConfig, but also via DeserializationConfig |
| 93 | + assertTrue(dc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 94 | + assertTrue(dc.shouldSortPropertiesAlphabetically()); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public void testJsonFactoryLinkage() |
| 99 | + { |
| 100 | + // first, implicit factory, giving implicit linkage |
| 101 | + assertSame(MAPPER, MAPPER.getFactory().getCodec()); |
| 102 | + |
| 103 | + // and then explicit factory, which should also be implicitly linked |
| 104 | + JsonFactory f = new JsonFactory(); |
| 105 | + ObjectMapper m = new ObjectMapper(f); |
| 106 | + assertSame(f, m.getFactory()); |
| 107 | + assertSame(m, f.getCodec()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test for verifying working of [JACKSON-191] |
| 112 | + */ |
| 113 | + public void testProviderConfig() throws Exception |
| 114 | + { |
| 115 | + ObjectMapper m = new ObjectMapper(); |
| 116 | + final String JSON = "{ \"x\" : 3 }"; |
| 117 | + |
| 118 | + assertEquals(0, m._deserializationContext._cache.cachedDeserializersCount()); |
| 119 | + // and then should get one constructed for: |
| 120 | + Bean bean = m.readValue(JSON, Bean.class); |
| 121 | + assertNotNull(bean); |
| 122 | + // Since 2.6, serializer for int also cached: |
| 123 | + assertEquals(2, m._deserializationContext._cache.cachedDeserializersCount()); |
| 124 | + m._deserializationContext._cache.flushCachedDeserializers(); |
| 125 | + assertEquals(0, m._deserializationContext._cache.cachedDeserializersCount()); |
| 126 | + |
| 127 | + // 07-Nov-2014, tatu: As per [databind#604] verify that Maps also get cached |
| 128 | + m = new ObjectMapper(); |
| 129 | + List<?> stuff = m.readValue("[ ]", List.class); |
| 130 | + assertNotNull(stuff); |
| 131 | + // may look odd, but due to "Untyped" deserializer thing, we actually have |
| 132 | + // 4 deserializers (int, List<?>, Map<?,?>, Object) |
| 133 | + assertEquals(4, m._deserializationContext._cache.cachedDeserializersCount()); |
| 134 | + } |
| 135 | + |
| 136 | + // [Issue#28]: ObjectMapper.copy() |
| 137 | + public void testCopy() throws Exception |
| 138 | + { |
| 139 | + ObjectMapper m = new ObjectMapper(); |
| 140 | + assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 141 | + m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 142 | + assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 143 | + InjectableValues inj = new InjectableValues.Std(); |
| 144 | + m.setInjectableValues(inj); |
| 145 | + |
| 146 | + // // First: verify that handling of features is decoupled: |
| 147 | + |
| 148 | + ObjectMapper m2 = m.copy(); |
| 149 | + assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 150 | + m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 151 | + assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 152 | + assertSame(inj, m2.getInjectableValues()); |
| 153 | + |
| 154 | + // but should NOT change the original |
| 155 | + assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 156 | + |
| 157 | + // nor vice versa: |
| 158 | + assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 159 | + assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 160 | + m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); |
| 161 | + assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 162 | + assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 163 | + |
| 164 | + // // Also, underlying JsonFactory instances should be distinct |
| 165 | + |
| 166 | + assertNotSame(m.getFactory(), m2.getFactory()); |
| 167 | + |
| 168 | + // [Issue#122]: Need to ensure mix-ins are not shared |
| 169 | + assertEquals(0, m.getSerializationConfig().mixInCount()); |
| 170 | + assertEquals(0, m2.getSerializationConfig().mixInCount()); |
| 171 | + assertEquals(0, m.getDeserializationConfig().mixInCount()); |
| 172 | + assertEquals(0, m2.getDeserializationConfig().mixInCount()); |
| 173 | + |
| 174 | + m.addMixIn(String.class, Integer.class); |
| 175 | + assertEquals(1, m.getSerializationConfig().mixInCount()); |
| 176 | + assertEquals(0, m2.getSerializationConfig().mixInCount()); |
| 177 | + assertEquals(1, m.getDeserializationConfig().mixInCount()); |
| 178 | + assertEquals(0, m2.getDeserializationConfig().mixInCount()); |
| 179 | + } |
| 180 | + |
| 181 | + public void testAnnotationIntrospectorCopyin() |
| 182 | + { |
| 183 | + ObjectMapper m = new ObjectMapper(); |
| 184 | + m.setAnnotationIntrospector(new MyAnnotationIntrospector()); |
| 185 | + assertEquals(MyAnnotationIntrospector.class, |
| 186 | + m.getDeserializationConfig().getAnnotationIntrospector().getClass()); |
| 187 | + ObjectMapper m2 = m.copy(); |
| 188 | + |
| 189 | + assertEquals(MyAnnotationIntrospector.class, |
| 190 | + m2.getDeserializationConfig().getAnnotationIntrospector().getClass()); |
| 191 | + assertEquals(MyAnnotationIntrospector.class, |
| 192 | + m2.getSerializationConfig().getAnnotationIntrospector().getClass()); |
| 193 | + } |
| 194 | + |
| 195 | + // For [databind#703] |
| 196 | + public void testNonSerializabilityOfObject() |
| 197 | + { |
| 198 | + ObjectMapper m = new ObjectMapper(); |
| 199 | + assertFalse(m.canSerialize(Object.class)); |
| 200 | + // but this used to pass, incorrectly |
| 201 | + assertFalse(m.canSerialize(Object.class)); |
| 202 | + } |
| 203 | +} |
0 commit comments