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 testMarshalObjectWithJsonAnnotatedPropertyNull() throws Exception
84 {
85 assertEquals("Default behaviour with @JsonProperty annotations should include null members",
86 "{\"name\":null}", marshaller.marshal(new JsonBeanInclusionDefault(null)));
87 }
88
89 @Test
90 public void testMarshalObjectWithJsonAnnotatedPropertyNullAndAlwaysInclusion() throws Exception
91 {
92 assertEquals("If we ask for null members to always be included they should be in the result",
93 "{\"name\":null}", marshaller.marshal(new JsonBeanInclusionAlways(null)));
94 }
95
96 @Test
97 public void testMarshalObjectWithJsonAnnotatedPropertyNullAndNonNullInclusion() throws Exception
98 {
99 assertEquals("Non-NULL inclusion means null members should not be in the output",
100 "{}", marshaller.marshal(new JsonBeanInclusionNonNull(null)));
101 }
102
103 @XmlRootElement
104 private static class ObjectWithMember
105 {
106 @SuppressWarnings("unused")
107 @XmlElement
108 private final String string;
109
110 public ObjectWithMember(String string)
111 {
112 this.string = string;
113 }
114 }
115
116 @XmlRootElement
117 private static class ObjectWithMemberMissingAnnotation
118 {
119 @SuppressWarnings("unused")
120 private final String string;
121
122 public ObjectWithMemberMissingAnnotation(String string)
123 {
124 this.string = string;
125 }
126 }
127
128 @XmlRootElement
129 private static class ObjectWithMemberWithRenaming
130 {
131 @SuppressWarnings("unused")
132 @XmlElement(name = "str")
133 private final String string;
134
135 public ObjectWithMemberWithRenaming(String string)
136 {
137 this.string = string;
138 }
139 }
140
141 static class JsonBeanInclusionDefault
142 {
143 @JsonProperty
144 public final String name;
145
146 JsonBeanInclusionDefault(String name)
147 {
148 this.name = name;
149 }
150 }
151
152 @JsonSerialize(include = Inclusion.ALWAYS)
153 static class JsonBeanInclusionAlways
154 {
155 @JsonProperty
156 public final String name;
157
158 JsonBeanInclusionAlways(String name)
159 {
160 this.name = name;
161 }
162 }
163
164 @JsonSerialize(include = Inclusion.NON_NULL)
165 static class JsonBeanInclusionNonNull
166 {
167 @JsonProperty
168 public final String name;
169
170 JsonBeanInclusionNonNull(String name)
171 {
172 this.name = name;
173 }
174 }
175 }
176