1 package com.atlassian.plugins.rest.common.json;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.junit.Before;
6 import org.junit.Test;
7
8 import java.util.Arrays;
9 import java.util.Collections;
10
11 import javax.xml.bind.JAXBException;
12 import javax.xml.bind.annotation.XmlElement;
13 import javax.xml.bind.annotation.XmlRootElement;
14
15 public class DefaultJaxbJsonMarshallerTest
16 {
17 private JaxbJsonMarshaller marshaller;
18
19 @Before
20 public void setUp()
21 {
22 marshaller = new DefaultJaxbJsonMarshaller();
23 }
24
25 @Test
26 public void testMarshalBoolean() throws JAXBException
27 {
28 assertEquals("true", marshaller.marshal(Boolean.TRUE));
29 }
30
31 @Test
32 public void testMarshalInteger() throws JAXBException
33 {
34 assertEquals("2", marshaller.marshal(Integer.valueOf(2)));
35 }
36
37 @Test
38 public void testMarshalString() throws JAXBException
39 {
40 assertEquals("\"foobar\"", marshaller.marshal("foobar"));
41 }
42
43 @Test
44 public void testMarshalMap() throws JAXBException
45 {
46 assertEquals("{\"foo\":\"bar\"}", marshaller.marshal(Collections.singletonMap("foo", "bar")));
47 }
48
49 @Test
50 public void testMarshalList() throws JAXBException
51 {
52 assertEquals("[\"foo\",\"bar\"]", marshaller.marshal(Arrays.asList("foo", "bar")));
53 }
54
55 @Test
56 public void testMarshalObjectWithMember() throws Exception
57 {
58 assertEquals("{\"string\":\"foo\"}", marshaller.marshal(new ObjectWithMember("foo")));
59 }
60
61 @Test
62 public void testMarshalObjectWithNullMember() throws Exception
63 {
64 assertEquals("{}", marshaller.marshal(new ObjectWithMember(null)));
65 }
66
67 @Test
68 public void testMarshalObjectWithMemberMissingAnnotation() throws Exception
69 {
70 assertEquals("{}", marshaller.marshal(new ObjectWithMemberMissingAnnotation("foo")));
71 }
72
73 @Test
74 public void testMarshalObjectWithMemberWithRenaming() throws Exception
75 {
76 assertEquals("{\"str\":\"foo\"}", marshaller.marshal(new ObjectWithMemberWithRenaming("foo")));
77 }
78
79 @XmlRootElement
80 private static class ObjectWithMember
81 {
82 @SuppressWarnings("unused")
83 @XmlElement
84 private final String string;
85
86 public ObjectWithMember(String string)
87 {
88 this.string = string;
89 }
90 }
91
92 @XmlRootElement
93 private static class ObjectWithMemberMissingAnnotation
94 {
95 @SuppressWarnings("unused")
96 private final String string;
97
98 public ObjectWithMemberMissingAnnotation(String string)
99 {
100 this.string = string;
101 }
102 }
103
104 @XmlRootElement
105 private static class ObjectWithMemberWithRenaming
106 {
107 @SuppressWarnings("unused")
108 @XmlElement(name = "str")
109 private final String string;
110
111 public ObjectWithMemberWithRenaming(String string)
112 {
113 this.string = string;
114 }
115 }
116 }
117