Search in sources :

Example 6 with SuperBook

use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.

the class JSONProviderTest method testWriteListOfDerivedTypes.

@Test
public void testWriteListOfDerivedTypes() throws Exception {
    JSONProvider<Books2> p = new JSONProvider<Books2>();
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
    p.setNamespaceMap(namespaceMap);
    Books2 books2 = new Books2();
    books2.setBooks(Collections.singletonList(new SuperBook("CXF Rocks", 123L, 124L)));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    p.writeTo(books2, Books2.class, Books2.class, Books2.class.getAnnotations(), MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
    String s = os.toString();
    String data = "{\"books2\":{\"books\":{\"@xsins.type\":\"superBook\",\"id\":123," + "\"name\":\"CXF Rocks\",\"state\":\"\",\"superId\":124}}}";
    assertEquals(data, s);
}
Also used : HashMap(java.util.HashMap) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 7 with SuperBook

use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.

the class JAXBElementProviderTest method testExtraClass.

@Test
public void testExtraClass() throws Exception {
    ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    JAXBElementProvider<SuperBook> provider = new JAXBElementProvider<SuperBook>();
    provider.setSingleJaxbContext(true);
    provider.setExtraClass(new Class[] { SuperBook.class });
    provider.init(Collections.singletonList(cri));
    JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);
    assertNotNull(bookContext);
    JAXBContext superBookContext = provider.getJAXBContext(SuperBook.class, SuperBook.class);
    assertSame(bookContext, superBookContext);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 8 with SuperBook

use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.

the class JAXBElementProviderTest method testWriteDerivedType2.

@Test
public void testWriteDerivedType2() throws Exception {
    JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
    Book b = new SuperBook("CXF in Action", 123L, 124L);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, Book.class, Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    readSuperBook(bos.toString(), false);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 9 with SuperBook

use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.

the class JAXBElementProviderTest method readSuperBook2.

private void readSuperBook2(String data, boolean unmarshalAsJaxbElement) throws Exception {
    JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook> provider = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook>();
    if (!unmarshalAsJaxbElement) {
        provider.setJaxbElementClassMap(Collections.singletonMap(org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), "SuperBook"));
    } else {
        provider.setUnmarshallAsJaxbElement(true);
    }
    ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
    org.apache.cxf.jaxrs.fortest.jaxb.SuperBook book = provider.readFrom(org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class, org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
    assertEquals(124L, book.getSuperId());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook)

Example 10 with SuperBook

use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.

the class JAXBElementProviderTest method testWriteCollectionWithoutXmlRootElement.

@Test
public void testWriteCollectionWithoutXmlRootElement() throws Exception {
    JAXBElementProvider<List<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook>> provider = new JAXBElementProvider<List<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook>>();
    Map<String, String> prefixes = new HashMap<>();
    prefixes.put("http://superbooks", "ns1");
    prefixes.put("http://books", "ns2");
    provider.setNamespacePrefixes(prefixes);
    provider.setCollectionWrapperName("{http://superbooks}SuperBooks");
    provider.setJaxbElementClassMap(Collections.singletonMap(org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), "{http://superbooks}SuperBook"));
    org.apache.cxf.jaxrs.fortest.jaxb.SuperBook b = new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
    List<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook> books = Collections.singletonList(b);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(books, List.class, org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ns1:SuperBooks xmlns:ns1=\"http://superbooks\">" + "<ns1:SuperBook xmlns:ns2=\"http://books\" xmlns:ns1=\"http://superbooks\"><id>123</id>" + "<name>CXF in Action</name><superId>124</superId></ns1:SuperBook></ns1:SuperBooks>";
    assertEquals(expected, bos.toString());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SuperBook (org.apache.cxf.jaxrs.resources.SuperBook)11 Test (org.junit.Test)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JAXBContext (javax.xml.bind.JAXBContext)2 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)2 Book (org.apache.cxf.jaxrs.resources.Book)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1