View Javadoc

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