|
| 1 | +package com.fasterxml.jackson.databind; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonFactory; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext; |
| 10 | +import com.fasterxml.jackson.databind.node.*; |
| 11 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 12 | + |
| 13 | +public class TestObjectMapper extends BaseMapTest |
| 14 | +{ |
| 15 | + static class Bean { |
| 16 | + int value = 3; |
| 17 | + |
| 18 | + public void setX(int v) { value = v; } |
| 19 | + } |
| 20 | + |
| 21 | + // for [Issue#206] |
| 22 | + @SuppressWarnings("serial") |
| 23 | + static class CustomMapper extends ObjectMapper { |
| 24 | + @Override |
| 25 | + protected DefaultDeserializationContext createDeserializationContext(JsonParser jp, |
| 26 | + DeserializationConfig cfg) { |
| 27 | + return super.createDeserializationContext(jp, cfg); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /* |
| 32 | + /********************************************************** |
| 33 | + /* Test methods |
| 34 | + /********************************************************** |
| 35 | + */ |
| 36 | + |
| 37 | + final static ObjectMapper MAPPER = new ObjectMapper(); |
| 38 | + |
| 39 | + public void testProps() |
| 40 | + { |
| 41 | + ObjectMapper m = new ObjectMapper(); |
| 42 | + // should have default factory |
| 43 | + assertNotNull(m.getNodeFactory()); |
| 44 | + JsonNodeFactory nf = JsonNodeFactory.instance; |
| 45 | + m.setNodeFactory(nf); |
| 46 | + assertSame(nf, m.getNodeFactory()); |
| 47 | + } |
| 48 | + |
| 49 | + public void testSupport() |
| 50 | + { |
| 51 | + assertTrue(MAPPER.canSerialize(String.class)); |
| 52 | + assertTrue(MAPPER.canDeserialize(TypeFactory.defaultInstance().constructType(String.class))); |
| 53 | + } |
| 54 | + |
| 55 | + public void testTreeRead() throws Exception |
| 56 | + { |
| 57 | + String JSON = "{ }"; |
| 58 | + JsonNode n = MAPPER.readTree(JSON); |
| 59 | + assertTrue(n instanceof ObjectNode); |
| 60 | + |
| 61 | + n = MAPPER.readTree(new StringReader(JSON)); |
| 62 | + assertTrue(n instanceof ObjectNode); |
| 63 | + |
| 64 | + n = MAPPER.readTree(new ByteArrayInputStream(JSON.getBytes("UTF-8"))); |
| 65 | + assertTrue(n instanceof ObjectNode); |
| 66 | + } |
| 67 | + |
| 68 | + // Test to ensure that we can check property ordering defaults... |
| 69 | + public void testConfigForPropertySorting() throws Exception |
| 70 | + { |
| 71 | + ObjectMapper m = new ObjectMapper(); |
| 72 | + |
| 73 | + // sort-alphabetically is disabled by default: |
| 74 | + assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 75 | + SerializationConfig sc = m.getSerializationConfig(); |
| 76 | + assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 77 | + assertFalse(sc.shouldSortPropertiesAlphabetically()); |
| 78 | + DeserializationConfig dc = m.getDeserializationConfig(); |
| 79 | + assertFalse(dc.shouldSortPropertiesAlphabetically()); |
| 80 | + |
| 81 | + // but when enabled, should be visible: |
| 82 | + m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); |
| 83 | + sc = m.getSerializationConfig(); |
| 84 | + assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 85 | + assertTrue(sc.shouldSortPropertiesAlphabetically()); |
| 86 | + dc = m.getDeserializationConfig(); |
| 87 | + // and not just via SerializationConfig, but also via DeserializationConfig |
| 88 | + assertTrue(dc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); |
| 89 | + assertTrue(dc.shouldSortPropertiesAlphabetically()); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + public void testJsonFactoryLinkage() |
| 94 | + { |
| 95 | + // first, implicit factory, giving implicit linkage |
| 96 | + assertSame(MAPPER, MAPPER.getFactory().getCodec()); |
| 97 | + |
| 98 | + // and then explicit factory, which should also be implicitly linked |
| 99 | + JsonFactory f = new JsonFactory(); |
| 100 | + ObjectMapper m = new ObjectMapper(f); |
| 101 | + assertSame(f, m.getFactory()); |
| 102 | + assertSame(m, f.getCodec()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test for verifying working of [JACKSON-191] |
| 107 | + */ |
| 108 | + public void testProviderConfig() throws Exception |
| 109 | + { |
| 110 | + ObjectMapper m = new ObjectMapper(); |
| 111 | + |
| 112 | + assertEquals(0, m._deserializationContext._cache.cachedDeserializersCount()); |
| 113 | + // and then should get one constructed for: |
| 114 | + Bean bean = m.readValue("{ \"x\" : 3 }", Bean.class); |
| 115 | + assertNotNull(bean); |
| 116 | + assertEquals(1, m._deserializationContext._cache.cachedDeserializersCount()); |
| 117 | + m._deserializationContext._cache.flushCachedDeserializers(); |
| 118 | + assertEquals(0, m._deserializationContext._cache.cachedDeserializersCount()); |
| 119 | + } |
| 120 | + |
| 121 | + // [Issue#28]: ObjectMapper.copy() |
| 122 | + public void testCopy() throws Exception |
| 123 | + { |
| 124 | + ObjectMapper m = new ObjectMapper(); |
| 125 | + assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 126 | + m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 127 | + assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 128 | + |
| 129 | + // // First: verify that handling of features is decoupled: |
| 130 | + |
| 131 | + ObjectMapper m2 = m.copy(); |
| 132 | + assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 133 | + m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 134 | + assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 135 | + // but should NOT change the original |
| 136 | + assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); |
| 137 | + |
| 138 | + // nor vice versa: |
| 139 | + assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 140 | + assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 141 | + m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); |
| 142 | + assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 143 | + assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); |
| 144 | + |
| 145 | + // // Also, underlying JsonFactory instances should be distinct |
| 146 | + |
| 147 | + assertNotSame(m.getFactory(), m2.getFactory()); |
| 148 | + |
| 149 | + // [Issue#122]: Need to ensure mix-ins are not shared |
| 150 | + assertEquals(0, m.getSerializationConfig().mixInCount()); |
| 151 | + assertEquals(0, m2.getSerializationConfig().mixInCount()); |
| 152 | + assertEquals(0, m.getDeserializationConfig().mixInCount()); |
| 153 | + assertEquals(0, m2.getDeserializationConfig().mixInCount()); |
| 154 | + |
| 155 | + m.addMixInAnnotations(String.class, Integer.class); |
| 156 | + assertEquals(1, m.getSerializationConfig().mixInCount()); |
| 157 | + assertEquals(0, m2.getSerializationConfig().mixInCount()); |
| 158 | + assertEquals(1, m.getDeserializationConfig().mixInCount()); |
| 159 | + assertEquals(0, m2.getDeserializationConfig().mixInCount()); |
| 160 | + } |
| 161 | +} |
0 commit comments