1   package com.atlassian.plugins.rest.common.json;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   import org.junit.Test;
6   
7   import javax.xml.bind.JAXBException;
8   import javax.xml.bind.annotation.XmlElement;
9   import javax.xml.bind.annotation.XmlRootElement;
10  
11  public class DefaultJaxbJsonMarshallerTest
12  {
13  
14      @Test
15      public void testMarshal() throws JAXBException
16      {
17          final DefaultJaxbJsonMarshaller marshaller = new DefaultJaxbJsonMarshaller();
18          final TestRepresentation rep = new TestRepresentation("theDude", Boolean.TRUE);
19  
20          final String json = marshaller.marshal(rep, TestRepresentation.class);
21          assertEquals("{\"name\":\"theDude\",\"check\":true}", json);
22      }
23  
24      @Test
25      public void testMarshalWithoutClasses()
26      {
27          final DefaultJaxbJsonMarshaller marshaller = new DefaultJaxbJsonMarshaller();
28          final TestRepresentation rep = new TestRepresentation("theDude", Boolean.TRUE);
29  
30          final String json;
31          try
32          {
33              json = marshaller.marshal(rep);
34              fail("Should have thrown exception!!!");
35          }
36          catch (JAXBException e)
37          {
38              //yay
39              assertEquals("class com.atlassian.plugins.rest.common.json.DefaultJaxbJsonMarshallerTest$TestRepresentation nor any of its super class is known to this context.",
40                      e.getMessage());
41          }
42      }
43  
44  
45      @XmlRootElement
46      public static class TestRepresentation
47      {
48          @XmlElement
49          private final String name;
50          @XmlElement
51          private final Boolean check;
52  
53          private TestRepresentation()
54          {
55              name = null;
56              check = null;
57          }
58  
59          public TestRepresentation(final String name, final Boolean check)
60          {
61              this.name = name;
62              this.check = check;
63          }
64  
65          public String getName()
66          {
67              return name;
68          }
69  
70          public Boolean getCheck()
71          {
72              return check;
73          }
74      }
75  }
76