• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.deser;
2 
3 import java.util.*;
4 
5 import com.fasterxml.jackson.annotation.*;
6 
7 import com.fasterxml.jackson.databind.*;
8 
9 /**
10  * Unit tests for verifying that feature requested
11  * via [JACKSON-88] ("setterless collections") work as
12  * expected, similar to how Collections and Maps work
13  * with JAXB.
14  */
15 public class TestSetterlessProperties
16     extends BaseMapTest
17 {
18     static class CollectionBean
19     {
20         List<String> _values = new ArrayList<String>();
21 
getValues()22         public List<String> getValues() { return _values; }
23     }
24 
25     static class MapBean
26     {
27         Map<String,Integer> _values = new HashMap<String,Integer>();
28 
getValues()29         public Map<String,Integer> getValues() { return _values; }
30     }
31 
32     // testing to verify that field has precedence over getter, for lists
33     static class Dual
34     {
35         @JsonProperty("list") protected List<Integer> values = new ArrayList<Integer>();
36 
Dual()37         public Dual() { }
38 
getList()39         public List<Integer> getList() {
40             throw new IllegalStateException("Should not get called");
41         }
42     }
43 
44     /*
45     /**********************************************************
46     /* Unit tests
47     /**********************************************************
48      */
49 
testSimpleSetterlessCollectionOk()50     public void testSimpleSetterlessCollectionOk()
51         throws Exception
52     {
53         CollectionBean result = new ObjectMapper().readValue
54             ("{\"values\":[ \"abc\", \"def\" ]}", CollectionBean.class);
55         List<String> l = result._values;
56         assertEquals(2, l.size());
57         assertEquals("abc", l.get(0));
58         assertEquals("def", l.get(1));
59     }
60 
61     /**
62      * Let's also verify that disabling the feature makes
63      * deserialization fail for setterless bean
64      */
testSimpleSetterlessCollectionFailure()65     public void testSimpleSetterlessCollectionFailure()
66         throws Exception
67     {
68         ObjectMapper m = new ObjectMapper();
69         // by default, it should be enabled
70         assertTrue(m.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
71         m = jsonMapperBuilder()
72                 .configure(MapperFeature.USE_GETTERS_AS_SETTERS, false)
73                 .build();
74         assertFalse(m.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
75 
76         // and now this should fail
77         try {
78             m.readValue
79                 ("{\"values\":[ \"abc\", \"def\" ]}", CollectionBean.class);
80             fail("Expected an exception");
81         } catch (JsonMappingException e) {
82             /* Not a good exception, ideally could suggest a need for
83              * a setter...?
84              */
85             verifyException(e, "Unrecognized field");
86         }
87     }
88 
testSimpleSetterlessMapOk()89     public void testSimpleSetterlessMapOk()
90         throws Exception
91     {
92         MapBean result = new ObjectMapper().readValue
93             ("{\"values\":{ \"a\": 15, \"b\" : -3 }}", MapBean.class);
94         Map<String,Integer> m = result._values;
95         assertEquals(2, m.size());
96         assertEquals(Integer.valueOf(15), m.get("a"));
97         assertEquals(Integer.valueOf(-3), m.get("b"));
98     }
99 
testSimpleSetterlessMapFailure()100     public void testSimpleSetterlessMapFailure()
101         throws Exception
102     {
103         ObjectMapper m = jsonMapperBuilder()
104                 .configure(MapperFeature.USE_GETTERS_AS_SETTERS, false)
105                 .build();
106         // so this should fail now without a setter
107         try {
108             m.readValue
109                 ("{\"values\":{ \"a\":3 }}", MapBean.class);
110             fail("Expected an exception");
111         } catch (JsonMappingException e) {
112             verifyException(e, "Unrecognized field");
113         }
114     }
115 
116     /* Test precedence of "getter-as-setter" (for Lists) versus
117      * field for same property.
118      */
testSetterlessPrecedence()119     public void testSetterlessPrecedence() throws Exception
120     {
121         ObjectMapper m = jsonMapperBuilder()
122                 .configure(MapperFeature.USE_GETTERS_AS_SETTERS, true)
123                 .build();
124         Dual value = m.readValue("{\"list\":[1,2,3]}, valueType)", Dual.class);
125         assertNotNull(value);
126         assertEquals(3, value.values.size());
127     }
128 }
129