1 package com.atlassian.plugins.rest.common.json;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.codehaus.jackson.annotate.JsonProperty;
6 import org.codehaus.jackson.map.annotate.JsonSerialize;
7 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
8 import org.junit.Before;
9 import org.junit.Test;
10
11 import java.util.Arrays;
12 import java.util.Collections;
13
14 import javax.xml.bind.JAXBException;
15 import javax.xml.bind.annotation.XmlElement;
16 import javax.xml.bind.annotation.XmlRootElement;
17
18 public class DefaultJaxbJsonMarshallerTest
19 {
20 private JaxbJsonMarshaller marshaller;
21
22 @Before
23 public void setUp()
24 {
25 marshaller = new DefaultJaxbJsonMarshaller();
26 }
27
28 @Test
29 public void testMarshalBoolean() throws JAXBException
30 {
31 assertEquals("true", marshaller.marshal(Boolean.TRUE));
32 }
33
34 @Test
35 public void testMarshalInteger() throws JAXBException
36 {
37 assertEquals("2", marshaller.marshal(Integer.valueOf(2)));
38 }
39
40 @Test
41 public void testMarshalString() throws JAXBException
42 {
43 assertEquals("\"foobar\"", marshaller.marshal("foobar"));
44 }
45
46 @Test
47 public void testMarshalMap() throws JAXBException
48 {
49 assertEquals("{\"foo\":\"bar\"}", marshaller.marshal(Collections.singletonMap("foo", "bar")));
50 }
51
52 @Test
53 public void testMarshalList() throws JAXBException
54 {
55 assertEquals("[\"foo\",\"bar\"]", marshaller.marshal(Arrays.asList("foo", "bar")));
56 }
57
58 @Test
59 public void testMarshalObjectWithMember() throws Exception
60 {
61 assertEquals("{\"string\":\"foo\"}", marshaller.marshal(new ObjectWithMember("foo")));
62 }
63
64 @Test
65 public void testMarshalObjectWithNullMember() throws Exception
66 {
67 assertEquals("{}", marshaller.marshal(new ObjectWithMember(null)));
68 }
69
70 @Test
71 public void testMarshalObjectWithMemberMissingAnnotation() throws Exception
72 {
73 assertEquals("{}", marshaller.marshal(new ObjectWithMemberMissingAnnotation("foo")));
74 }
75
76 @Test
77 public void testMarshalObjectWithMemberWithRenaming() throws Exception
78 {
79 assertEquals("{\"str\":\"foo\"}", marshaller.marshal(new ObjectWithMemberWithRenaming("foo")));
80 }
81
82 @Test
83 public void marshalCanPrettyPrint() throws Exception
84 {
85 marshaller = new DefaultJaxbJsonMarshaller(true);
86 assertEquals("{\n \"str\" : \"foo\"\n}", marshaller.marshal(new ObjectWithMemberWithRenaming("foo")));
87 }
88
89 @Test
90 public void testMarshalObjectWithJsonAnnotatedPropertyNull() throws Exception
91 {
92 assertEquals("Default behaviour with @JsonProperty annotations should exclude null members",
93 "{}", marshaller.marshal(new JsonBeanInclusionDefault(null)));
94 }
95
96 @Test
97 public void testMarshalObjectWithJsonSerializeAnnotationButNoExplicitInclusionMeansAlways() throws Exception
98 {
99 assertEquals("Behaviour with @JsonSerialize includes non-null members",
100 "{\"name\":null}", marshaller.marshal(new JsonBeanWithAnnotationButNoExplicitInclusion(null)));
101 }
102
103 @Test
104 public void testMarshalObjectWithJsonAnnotatedPropertyNullAndAlwaysInclusion() throws Exception
105 {
106 assertEquals("If we ask for null members to always be included they should be in the result",
107 "{\"name\":null}", marshaller.marshal(new JsonBeanInclusionAlways(null)));
108 }
109
110 @Test
111 public void testMarshalObjectWithJsonAnnotatedPropertyNullAndNonNullInclusion() throws Exception
112 {
113 assertEquals("Non-NULL inclusion means null members should not be in the output",
114 "{}", marshaller.marshal(new JsonBeanInclusionNonNull(null)));
115 }
116
117 @XmlRootElement
118 private static class ObjectWithMember
119 {
120 @SuppressWarnings("unused")
121 @XmlElement
122 private final String string;
123
124 public ObjectWithMember(String string)
125 {
126 this.string = string;
127 }
128 }
129
130 @XmlRootElement
131 private static class ObjectWithMemberMissingAnnotation
132 {
133 @SuppressWarnings("unused")
134 private final String string;
135
136 public ObjectWithMemberMissingAnnotation(String string)
137 {
138 this.string = string;
139 }
140 }
141
142 @XmlRootElement
143 private static class ObjectWithMemberWithRenaming
144 {
145 @SuppressWarnings("unused")
146 @XmlElement(name = "str")
147 private final String string;
148
149 public ObjectWithMemberWithRenaming(String string)
150 {
151 this.string = string;
152 }
153 }
154
155 static class JsonBeanInclusionDefault
156 {
157 @JsonProperty
158 public final String name;
159
160 JsonBeanInclusionDefault(String name)
161 {
162 this.name = name;
163 }
164 }
165
166 @JsonSerialize
167 static class JsonBeanWithAnnotationButNoExplicitInclusion
168 {
169 @JsonProperty
170 public final String name;
171
172 JsonBeanWithAnnotationButNoExplicitInclusion(String name)
173 {
174 this.name = name;
175 }
176 }
177
178 @JsonSerialize(include = Inclusion.ALWAYS)
179 static class JsonBeanInclusionAlways
180 {
181 @JsonProperty
182 public final String name;
183
184 JsonBeanInclusionAlways(String name)
185 {
186 this.name = name;
187 }
188 }
189
190 @JsonSerialize(include = Inclusion.NON_NULL)
191 static class JsonBeanInclusionNonNull
192 {
193 @JsonProperty
194 public final String name;
195
196 JsonBeanInclusionNonNull(String name)
197 {
198 this.name = name;
199 }
200 }
201 }
202