Debian Codegraph @

src/test/java/com/fasterxml/jackson/module/jaxb/introspect/TestIntrospectorPair.java

raw
1 package com.fasterxml.jackson.module.jaxb.introspect;
2
3 import java.util.*;
4
5 import javax.xml.bind.annotation.*;
6
7 import com.fasterxml.jackson.annotation.*;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
10 import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
11 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
12 import com.fasterxml.jackson.databind.type.TypeFactory;
13
14 import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
15 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
16
17 /**
18 * Simple testing that <code>AnnotationIntrospector.Pair</code> works as
19 * expected, when used with Jackson and JAXB-based introspector.
20 *
21 * @author Tatu Saloranta
22 */
23 public class TestIntrospectorPair
24 extends BaseJaxbTest
25 {
26 final static AnnotationIntrospector _jacksonAI = new JacksonAnnotationIntrospector();
27 final static AnnotationIntrospector _jaxbAI = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
28
29 /*
30 /**********************************************************
31 /* Helper beans
32 /**********************************************************
33 */
34
35 /**
36 * Simple test bean for verifying basic field detection and property
37 * naming annotation handling
38 */
39 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
40 static class NamedBean
41 {
42 @JsonProperty
43 private String jackson = "1";
44
45 @XmlElement(name="jaxb")
46 protected String jaxb = "2";
47
48 @JsonProperty("bothJackson")
49 @XmlElement(name="bothJaxb")
50 private String bothString = "3";
51
52 public String notAGetter() { return "xyz"; }
53 }
54
55 /**
56 * Another bean for verifying details of property naming
57 */
58 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
59 static class NamedBean2
60 {
61 @JsonProperty("")
62 @XmlElement(name="jaxb")
63 public String foo = "abc";
64
65 @JsonProperty("jackson")
66 @XmlElement()
67 public String getBar() { return "123"; }
68
69 // JAXB, alas, requires setters for all properties too
70 public void setBar(String v) { }
71 }
72
73 /**
74 * And a bean to check how "ignore" annotations work with
75 * various combinations of annotation introspectors
76 */
77 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
78 static class IgnoreBean
79 {
80 @JsonIgnore
81 public int getNumber() { return 13; }
82
83 @XmlTransient
84 public String getText() { return "abc"; }
85
86 public boolean getAny() { return true; }
87 }
88
89 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
90 static class IgnoreFieldBean
91 {
92 @JsonIgnore public int number = 7;
93 @XmlTransient public String text = "123";
94 public boolean any = true;
95 }
96
97 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
98 @XmlRootElement(name="test", namespace="urn:whatever")
99 static class NamespaceBean
100 {
101 public String string;
102 }
103
104 // Testing [JACKSON-495]
105 static class CreatorBean {
106 @JsonCreator
107 public CreatorBean(@JsonProperty("name") String name) {
108 ;
109 }
110 }
111
112 /*
113 /**********************************************************
114 /* Unit tests
115 /**********************************************************
116 */
117
118 public void testSimple() throws Exception
119 {
120 ObjectMapper mapper;
121 AnnotationIntrospector pair;
122 Map<String,Object> result;
123
124 mapper = new ObjectMapper();
125 // first: test with Jackson/Jaxb pair (jackson having precedence)
126 pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
127 mapper.setAnnotationIntrospector(pair);
128
129 result = writeAndMap(mapper, new NamedBean());
130 assertEquals(3, result.size());
131 assertEquals("1", result.get("jackson"));
132 assertEquals("2", result.get("jaxb"));
133 // jackson one should have priority
134 assertEquals("3", result.get("bothJackson"));
135
136 mapper = new ObjectMapper();
137 pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
138 mapper.setAnnotationIntrospector(pair);
139
140 result = writeAndMap(mapper, new NamedBean());
141 assertEquals(3, result.size());
142 assertEquals("1", result.get("jackson"));
143 assertEquals("2", result.get("jaxb"));
144 // JAXB one should have priority
145 assertEquals("3", result.get("bothJaxb"));
146 }
147
148 public void testNaming() throws Exception
149 {
150 ObjectMapper mapper;
151 AnnotationIntrospector pair;
152 Map<String,Object> result;
153
154 mapper = new ObjectMapper();
155 // first: test with Jackson/Jaxb pair (jackson having precedence)
156 pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
157 mapper.setAnnotationIntrospector(pair);
158
159 result = writeAndMap(mapper, new NamedBean2());
160 assertEquals(2, result.size());
161 // order shouldn't really matter here...
162 assertEquals("123", result.get("jackson"));
163 assertEquals("abc", result.get("jaxb"));
164
165 mapper = new ObjectMapper();
166 pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
167 mapper.setAnnotationIntrospector(pair);
168
169 result = writeAndMap(mapper, new NamedBean2());
170 /* Hmmh. Not 100% sure what JAXB would dictate.... thus...
171 */
172 assertEquals(2, result.size());
173 assertEquals("abc", result.get("jaxb"));
174 //assertEquals("123", result.get("jackson"));
175 }
176
177 public void testSimpleIgnore() throws Exception
178 {
179 // first: only Jackson introspector (default)
180 ObjectMapper mapper = new ObjectMapper();
181 Map<String,Object> result = writeAndMap(mapper, new IgnoreBean());
182 assertEquals(2, result.size());
183 assertEquals("abc", result.get("text"));
184 assertEquals(Boolean.TRUE, result.get("any"));
185
186 // Then JAXB only
187 mapper = new ObjectMapper();
188 mapper.setAnnotationIntrospector(_jaxbAI);
189
190 // jackson one should have priority
191 result = writeAndMap(mapper, new IgnoreBean());
192 assertEquals(2, result.size());
193 assertEquals(Integer.valueOf(13), result.get("number"));
194 assertEquals(Boolean.TRUE, result.get("any"));
195
196 // then both, Jackson first
197 mapper = new ObjectMapper();
198 mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
199
200 result = writeAndMap(mapper, new IgnoreBean());
201 assertEquals(1, result.size());
202 assertEquals(Boolean.TRUE, result.get("any"));
203
204 // then both, JAXB first
205 mapper = new ObjectMapper();
206 mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI));
207
208 result = writeAndMap(mapper, new IgnoreBean());
209 assertEquals(1, result.size());
210 assertEquals(Boolean.TRUE, result.get("any"));
211 }
212
213 public void testSimpleFieldIgnore() throws Exception
214 {
215 ObjectMapper mapper;
216
217 // first: only Jackson introspector (default)
218 mapper = new ObjectMapper();
219 Map<String,Object> result = writeAndMap(mapper, new IgnoreFieldBean());
220 assertEquals(2, result.size());
221 assertEquals("123", result.get("text"));
222 assertEquals(Boolean.TRUE, result.get("any"));
223
224 // Then JAXB only
225 mapper = new ObjectMapper();
226 mapper.setAnnotationIntrospector(_jaxbAI);
227
228 // jackson one should have priority
229 result = writeAndMap(mapper, new IgnoreFieldBean());
230 assertEquals(2, result.size());
231 assertEquals(Integer.valueOf(7), result.get("number"));
232 assertEquals(Boolean.TRUE, result.get("any"));
233
234 // then both, Jackson first
235 mapper = new ObjectMapper();
236 mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
237
238 result = writeAndMap(mapper, new IgnoreFieldBean());
239 assertEquals(1, result.size());
240 assertEquals(Boolean.TRUE, result.get("any"));
241
242 // then both, JAXB first
243 mapper = new ObjectMapper();
244 mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI));
245
246 result = writeAndMap(mapper, new IgnoreFieldBean());
247 assertEquals(1, result.size());
248 assertEquals(Boolean.TRUE, result.get("any"));
249 }
250
251 public void testRootName() throws Exception
252 {
253 // first: test with Jackson/Jaxb pair (jackson having precedence)
254 AnnotationIntrospector pair = new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI);
255 ObjectMapper mapper = new ObjectMapper()
256 .setAnnotationIntrospector(pair);
257 TypeFactory tf = mapper.getTypeFactory();
258
259 assertNull(pair.findRootName(AnnotatedClass.construct(tf.constructType(NamedBean.class),
260 mapper.getSerializationConfig(), null)));
261 PropertyName name = pair.findRootName(AnnotatedClass.construct(tf.constructType(NamespaceBean.class),
262 mapper.getSerializationConfig(), null));
263 assertNotNull(name);
264 assertEquals("test", name.getSimpleName());
265 assertEquals("urn:whatever", name.getNamespace());
266
267 // then reverse; should make no difference
268 pair = new AnnotationIntrospectorPair(_jaxbAI, _jacksonAI);
269 name = pair.findRootName(AnnotatedClass.construct(tf.constructType(NamedBean.class),
270 mapper.getSerializationConfig(), null));
271 assertNull(name);
272
273 name = pair.findRootName(AnnotatedClass.construct(tf.constructType(NamespaceBean.class),
274 mapper.getSerializationConfig(), null));
275 assertEquals("test", name.getSimpleName());
276 assertEquals("urn:whatever", name.getNamespace());
277 }
278
279 /**
280 * Test that will just use Jackson annotations, but did trigger [JACKSON-495] due to a bug
281 * in JAXB annotation introspector.
282 */
283 public void testIssue495() throws Exception
284 {
285 ObjectMapper mapper = new ObjectMapper();
286 mapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(_jacksonAI, _jaxbAI));
287 CreatorBean bean = mapper.readValue("{\"name\":\"foo\"}", CreatorBean.class);
288 assertNotNull(bean);
289 }
290 }