Debian Codegraph @

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

raw
1 package com.fasterxml.jackson.module.jaxb.introspect;
2
3 import java.util.*;
4
5 import javax.xml.bind.annotation.*;
6 import javax.xml.bind.annotation.adapters.XmlAdapter;
7 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
8 import javax.xml.namespace.QName;
9
10 import com.fasterxml.jackson.databind.*;
11 import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
12 import com.fasterxml.jackson.databind.introspect.AnnotatedField;
13 import com.fasterxml.jackson.databind.type.TypeFactory;
14
15 import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
16 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
17
18 /**
19 * Tests for verifying that JAXB annotation based introspector
20 * implementation works as expected
21 *
22 * @author Ryan Heaton
23 * @author Tatu Saloranta
24 */
25 public class TestJaxbAnnotationIntrospector
26 extends BaseJaxbTest
27 {
28 /*
29 /****************************************************
30 /* Helper beans
31 /****************************************************
32 */
33
34 public static enum EnumExample {
35
36 @XmlEnumValue("Value One")
37 VALUE1
38 }
39
40 public static class JaxbExample
41 {
42 protected String attributeProperty;
43 protected String elementProperty;
44 protected List<String> wrappedElementProperty;
45 protected EnumExample enumProperty;
46 protected QName qname;
47 protected QName qname1;
48 protected String propertyToIgnore;
49
50 @XmlJavaTypeAdapter(QNameAdapter.class)
51 public QName getQname()
52 {
53 return qname;
54 }
55
56 public void setQname(QName qname)
57 {
58 this.qname = qname;
59 }
60
61 public QName getQname1()
62 {
63 return qname1;
64 }
65
66 public void setQname1(QName qname1)
67 {
68 this.qname1 = qname1;
69 }
70
71 @XmlAttribute(name="myattribute")
72 public String getAttributeProperty()
73 {
74 return attributeProperty;
75 }
76
77 public void setAttributeProperty(String attributeProperty)
78 {
79 this.attributeProperty = attributeProperty;
80 }
81
82 @XmlElement(name="myelement")
83 public String getElementProperty()
84 {
85 return elementProperty;
86 }
87
88 public void setElementProperty(String elementProperty)
89 {
90 this.elementProperty = elementProperty;
91 }
92
93 @XmlElementWrapper(name="mywrapped")
94 public List<String> getWrappedElementProperty()
95 {
96 return wrappedElementProperty;
97 }
98
99 public void setWrappedElementProperty(List<String> wrappedElementProperty)
100 {
101 this.wrappedElementProperty = wrappedElementProperty;
102 }
103
104 public EnumExample getEnumProperty()
105 {
106 return enumProperty;
107 }
108
109 public void setEnumProperty(EnumExample enumProperty)
110 {
111 this.enumProperty = enumProperty;
112 }
113
114 @XmlTransient
115 public String getPropertyToIgnore()
116 {
117 return propertyToIgnore;
118 }
119
120 public void setPropertyToIgnore(String propertyToIgnore)
121 {
122 this.propertyToIgnore = propertyToIgnore;
123 }
124 }
125
126 public static class QNameAdapter extends XmlAdapter<String, QName> {
127
128 @Override
129 public QName unmarshal(String v) throws Exception
130 {
131 return QName.valueOf(v);
132 }
133
134 @Override
135 public String marshal(QName v) throws Exception
136 {
137 return (v == null) ? null : v.toString();
138 }
139 }
140
141 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
142 public static class SimpleBean
143 {
144 @XmlElement
145 protected String jaxb = "1";
146
147 @XmlElement
148 protected String jaxb2 = "2";
149
150 @XmlElement(name="jaxb3")
151 private String oddName = "3";
152
153 public String notAGetter() { return "xyz"; }
154
155 @XmlTransient
156 public int foobar = 3;
157 }
158
159 @SuppressWarnings("unused")
160 @XmlAccessorType(XmlAccessType.FIELD)
161 public static class SimpleBean2 {
162
163 protected String jaxb = "1";
164 private String jaxb2 = "2";
165 @XmlElement(name="jaxb3")
166 private String oddName = "3";
167
168 }
169
170 @XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
171 @XmlRootElement(namespace="urn:class")
172 static class NamespaceBean
173 {
174 @XmlElement(namespace="urn:method")
175 public String string;
176 }
177
178 @XmlRootElement(name="test")
179 static class RootNameBean { }
180
181 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
182 public static class AlphaBean
183 {
184 public int c = 3;
185 public int a = 1;
186 public int b = 2;
187 }
188
189 public static class KeyValuePair {
190 private String key;
191 private String value;
192 public KeyValuePair() {}
193 public String getKey() {
194 return key;
195 }
196 public void setKey(String key) {
197 this.key = key;
198 }
199 public String getValue() {
200 return value;
201 }
202 public void setValue(String value) {
203 this.value = value;
204 }
205 }
206
207 /*
208 /****************************************************
209 /* Unit tests
210 /****************************************************
211 */
212
213 private final ObjectMapper MAPPER = getJaxbMapper();
214
215 public void testDetection() throws Exception
216 {
217 Map<String,Object> result = writeAndMap(MAPPER, new SimpleBean());
218 assertEquals(3, result.size());
219 assertEquals("1", result.get("jaxb"));
220 assertEquals("2", result.get("jaxb2"));
221 assertEquals("3", result.get("jaxb3"));
222
223 result = writeAndMap(MAPPER, new SimpleBean2());
224 assertEquals(3, result.size());
225 assertEquals("1", result.get("jaxb"));
226 assertEquals("2", result.get("jaxb2"));
227 assertEquals("3", result.get("jaxb3"));
228 }
229
230 /**
231 * tests getting serializer/deserializer instances.
232 */
233 public void testSerializeDeserializeWithJaxbAnnotations() throws Exception
234 {
235 ObjectMapper mapper = getJaxbMapper();
236 // test expects that wrapper name be used...
237 mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
238
239 mapper.enable(SerializationFeature.INDENT_OUTPUT);
240 JaxbExample ex = new JaxbExample();
241 QName qname = new QName("urn:hi", "hello");
242 ex.setQname(qname);
243 QName qname1 = new QName("urn:hi", "hello1");
244 ex.setQname1(qname1);
245 ex.setAttributeProperty("attributeValue");
246 ex.setElementProperty("elementValue");
247 ex.setWrappedElementProperty(Arrays.asList("wrappedElementValue"));
248 ex.setEnumProperty(EnumExample.VALUE1);
249 ex.setPropertyToIgnore("ignored");
250 String json = mapper.writeValueAsString(ex);
251
252 // uncomment to see what the JSON looks like.
253 // System.out.println(json);
254
255 //make sure the json is written out correctly.
256 JsonNode node = mapper.readValue(json, JsonNode.class);
257 assertEquals(qname.toString(), node.get("qname").asText());
258 JsonNode attr = node.get("myattribute");
259 assertNotNull(attr);
260 assertEquals("attributeValue", attr.asText());
261 assertEquals("elementValue", node.get("myelement").asText());
262 assertTrue(node.has("mywrapped"));
263 assertEquals(1, node.get("mywrapped").size());
264 assertEquals("wrappedElementValue", node.get("mywrapped").get(0).asText());
265 assertEquals("Value One", node.get("enumProperty").asText());
266 assertNull(node.get("propertyToIgnore"));
267
268 //now make sure it gets deserialized correctly.
269 JaxbExample readEx = mapper.readValue(json, JaxbExample.class);
270 assertEquals(ex.qname, readEx.qname);
271 assertEquals(ex.qname1, readEx.qname1);
272 assertEquals(ex.attributeProperty, readEx.attributeProperty);
273 assertEquals(ex.elementProperty, readEx.elementProperty);
274 assertEquals(ex.wrappedElementProperty, readEx.wrappedElementProperty);
275 assertEquals(ex.enumProperty, readEx.enumProperty);
276 assertNull(readEx.propertyToIgnore);
277 }
278
279 public void testRootNameAccess() throws Exception
280 {
281 final TypeFactory tf = TypeFactory.defaultInstance();
282 AnnotationIntrospector ai = new JaxbAnnotationIntrospector(tf);
283 // If no @XmlRootElement, should get null (unless pkg has etc)
284 assertNull(ai.findRootName(AnnotatedClass.construct(tf.constructType(SimpleBean.class),
285 MAPPER.getSerializationConfig(), null)));
286 // With @XmlRootElement, but no name, empty String
287 PropertyName rootName = ai.findRootName(AnnotatedClass.construct(tf.constructType(NamespaceBean.class),
288 MAPPER.getSerializationConfig(), null));
289 assertNotNull(rootName);
290 assertEquals("", rootName.getSimpleName());
291 assertEquals("urn:class", rootName.getNamespace());
292
293 // and otherwise explicit name
294 rootName = ai.findRootName(AnnotatedClass.construct(tf.constructType(RootNameBean.class),
295 MAPPER.getSerializationConfig(), null));
296 assertNotNull(rootName);
297 assertEquals("test", rootName.getSimpleName());
298 assertNull(rootName.getNamespace());
299 }
300
301 // JAXB can specify that properties are to be written in alphabetic order...
302 public void testSerializationAlphaOrdering() throws Exception
303 {
304 assertEquals("{\"a\":1,\"b\":2,\"c\":3}", MAPPER.writeValueAsString(new AlphaBean()));
305 }
306
307 /**
308 * Additional simple tests to ensure we will retain basic namespace information
309 * now that it can be included
310 *
311 * @since 2.1
312 */
313 public void testNamespaces() throws Exception
314 {
315 final TypeFactory tf = TypeFactory.defaultInstance();
316 JaxbAnnotationIntrospector ai = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
317 AnnotatedClass ac = AnnotatedClass.construct(tf.constructType(NamespaceBean.class),
318 MAPPER.getSerializationConfig(), null);
319 AnnotatedField af = _findField(ac, "string");
320 assertNotNull(af);
321 PropertyName pn = ai.findNameForDeserialization(af);
322 assertNotNull(pn);
323
324 // JAXB seems to assert field name instead of giving "use default"...
325 assertEquals("", pn.getSimpleName());
326 assertEquals("urn:method", pn.getNamespace());
327 }
328
329 private AnnotatedField _findField(AnnotatedClass ac, String name)
330 {
331 for (AnnotatedField af : ac.fields()) {
332 if (name.equals(af.getName())) {
333 return af;
334 }
335 }
336 return null;
337 }
338 }