Debian Codegraph @

src/test/java/com/fasterxml/jackson/module/jaxb/misc/TestJaxbNullProperties.java

raw
1 package com.fasterxml.jackson.module.jaxb.misc;
2
3 import javax.xml.bind.annotation.XmlElement;
4 import javax.xml.bind.annotation.XmlRootElement;
5
6 import com.fasterxml.jackson.annotation.JsonInclude;
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
9 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
10 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
11
12 /**
13 * Unit tests to ensure that handling of writing of null properties (or not)
14 * works when using JAXB annotation introspector.
15 * Mostly to test out [JACKSON-309].
16 */
17 public class TestJaxbNullProperties
18 extends BaseJaxbTest
19 {
20 /*
21 /**********************************************************
22 /* Helper beans
23 /**********************************************************
24 */
25
26 public static class Bean
27 {
28 public String empty;
29
30 public String x = "y";
31 }
32
33 @XmlRootElement
34 static class BeanWithNillable {
35 public Nillable X;
36 }
37
38 @XmlRootElement
39 static class Nillable {
40 @XmlElement (name="Z", nillable=true)
41 Integer Z;
42
43 public Nillable() { }
44 public Nillable(int i) {
45 Z = Integer.valueOf(i);
46 }
47 }
48
49 @XmlRootElement
50 static class NonNillableZ {
51 @XmlElement(name="z", nillable=false)
52 public Integer z;
53
54 public NonNillableZ() { }
55 public NonNillableZ(int i) {
56 z = Integer.valueOf(i);
57 }
58 }
59
60 /*
61 /**********************************************************
62 /* Unit tests
63 /**********************************************************
64 */
65
66 private final ObjectMapper MAPPER = getJaxbMapper();
67
68 public void testWriteNulls() throws Exception
69 {
70 BeanWithNillable bean = new BeanWithNillable();
71 bean.X = new Nillable();
72 assertEquals("{\"X\":{\"Z\":null}}", MAPPER.writeValueAsString(bean));
73 }
74
75 public void testNullProps() throws Exception
76 {
77 ObjectMapper mapper = getJaxbMapper();
78 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
79 assertEquals("{\"x\":\"y\"}", mapper.writeValueAsString(new Bean()));
80 }
81
82 public void testNillability() throws Exception
83 {
84 ObjectMapper mapper = getJaxbMapper();
85 // by default, something not marked as nillable will still be written if null
86 assertEquals("{\"z\":null}", mapper.writeValueAsString(new NonNillableZ()));
87 assertEquals("{\"z\":3}", mapper.writeValueAsString(new NonNillableZ(3)));
88
89 // but we can change that...
90 mapper = new ObjectMapper()
91 .registerModule(new JaxbAnnotationModule()
92 .setNonNillableInclusion(JsonInclude.Include.NON_NULL)
93 );
94 mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory())
95 .setNonNillableInclusion(JsonInclude.Include.NON_NULL)
96 );
97 assertEquals("{}", mapper.writeValueAsString(new NonNillableZ()));
98 assertEquals("{\"z\":3}", mapper.writeValueAsString(new NonNillableZ(3)));
99 }
100 }