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);
}
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);
}
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);
}
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());
}
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());
}
Aggregations