• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.cfg;
2 
3 import java.util.Collections;
4 
5 import com.fasterxml.jackson.annotation.JsonInclude;
6 import com.fasterxml.jackson.core.json.JsonReadFeature;
7 import com.fasterxml.jackson.databind.*;
8 import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
9 
10 public class DeserializationConfigTest extends BaseMapTest
11 {
12     private final ObjectMapper MAPPER = new ObjectMapper();
13 
testFeatureDefaults()14     public void testFeatureDefaults()
15     {
16         ObjectMapper m = new ObjectMapper();
17         DeserializationConfig cfg = m.getDeserializationConfig();
18 
19         // Expected defaults:
20         assertTrue(cfg.isEnabled(MapperFeature.USE_ANNOTATIONS));
21         assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
22         assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_CREATORS));
23         assertTrue(cfg.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
24         assertTrue(cfg.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS));
25 
26         assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS));
27         assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS));
28 
29         assertTrue(cfg.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
30     }
31 
testBasicFeatures()32     public void testBasicFeatures() throws Exception
33     {
34         DeserializationConfig config = MAPPER.getDeserializationConfig();
35         assertTrue(config.hasDeserializationFeatures(DeserializationFeature.EAGER_DESERIALIZER_FETCH.getMask()));
36         assertFalse(config.hasDeserializationFeatures(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
37         assertTrue(config.hasSomeOfFeatures(DeserializationFeature.EAGER_DESERIALIZER_FETCH.getMask()
38                 + DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
39         assertFalse(config.hasSomeOfFeatures(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
40 
41         // if no changes then same config object
42         assertSame(config, config.without());
43         assertSame(config, config.with());
44         assertSame(config, config.with(MAPPER.getSubtypeResolver()));
45 
46         // and then change
47         DeserializationConfig newConfig = config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
48         assertNotSame(config, newConfig);
49         config = newConfig;
50 
51         // but another attempt with no real change returns same
52         assertSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
53         assertNotSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, false));
54 
55         assertNotSame(config, config.with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,
56                 DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES));
57     }
58 
testParserFeatures()59     public void testParserFeatures() throws Exception
60     {
61         DeserializationConfig config = MAPPER.getDeserializationConfig();
62         assertNotSame(config, config.with(JsonReadFeature.ALLOW_JAVA_COMMENTS));
63         assertNotSame(config, config.withFeatures(JsonReadFeature.ALLOW_JAVA_COMMENTS,
64                 JsonReadFeature.ALLOW_MISSING_VALUES));
65 
66         assertNotSame(config, config.without(JsonReadFeature.ALLOW_JAVA_COMMENTS));
67         assertNotSame(config, config.withoutFeatures(JsonReadFeature.ALLOW_JAVA_COMMENTS,
68                 JsonReadFeature.ALLOW_MISSING_VALUES));
69     }
70 
testFormatFeatures()71     public void testFormatFeatures() throws Exception
72     {
73         DeserializationConfig config = MAPPER.getDeserializationConfig();
74         assertNotSame(config, config.with(BogusFormatFeature.FF_DISABLED_BY_DEFAULT));
75         assertNotSame(config, config.withFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
76                 BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
77         assertNotSame(config, config.without(BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
78         assertNotSame(config, config.withoutFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
79                 BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
80     }
81 
82     /* Test to verify that we don't overflow number of features; if we
83      * hit the limit, need to change implementation -- this test just
84      * gives low-water mark
85      */
testEnumIndexes()86     public void testEnumIndexes()
87     {
88         int max = 0;
89 
90         for (DeserializationFeature f : DeserializationFeature.values()) {
91             max = Math.max(max, f.ordinal());
92         }
93         if (max >= 31) { // 31 is actually ok; 32 not
94             fail("Max number of DeserializationFeature enums reached: "+max);
95         }
96     }
97 
testOverrideIntrospectors()98     public void testOverrideIntrospectors()
99     {
100         ObjectMapper m = new ObjectMapper();
101         DeserializationConfig cfg = m.getDeserializationConfig();
102         // and finally, ensure we could override introspectors
103         cfg = cfg.with((ClassIntrospector) null); // no way to verify tho
104         cfg = cfg.with((AnnotationIntrospector) null);
105         assertNull(cfg.getAnnotationIntrospector());
106     }
107 
testMisc()108     public void testMisc() throws Exception
109     {
110         DeserializationConfig config = MAPPER.getDeserializationConfig();
111         assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion());
112         assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion(String.class));
113 
114         assertSame(config, config.withRootName((PropertyName) null)); // defaults to 'none'
115 
116         DeserializationConfig newConfig = config.withRootName(PropertyName.construct("foobar"));
117         assertNotSame(config, newConfig);
118         config = newConfig;
119         assertSame(config, config.withRootName(PropertyName.construct("foobar")));
120 
121         assertSame(config, config.with(config.getAttributes()));
122         assertNotSame(config, config.with(new ContextAttributes.Impl(Collections.singletonMap("a", "b"))));
123 
124         // should also be able to introspect:
125         assertNotNull(config.introspectDirectClassAnnotations(getClass()));
126     }
127 }
128