use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method testReadChineeseChars.
@Test
public void testReadChineeseChars() throws Exception {
String nameStringUTF16 = "中文";
String bookStringUTF16 = "<Book><name>" + nameStringUTF16 + "</name></Book>";
JAXBElementProvider<Book> p = new JAXBElementProvider<Book>();
Book book = p.readFrom(Book.class, null, new Annotation[] {}, MediaType.valueOf(MediaType.APPLICATION_XML + ";charset=UTF-8"), null, new ByteArrayInputStream(bookStringUTF16.getBytes(StandardCharsets.UTF_8)));
assertEquals(book.getName(), nameStringUTF16);
}
use of org.apache.cxf.jaxrs.resources.Book 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.Book in project cxf by apache.
the class JAXBElementProviderTest method doWriteUnqualifiedCollection.
public <T> void doWriteUnqualifiedCollection(boolean setName, String mName, String setterName, Class<T> type) throws Exception {
JAXBElementProvider<T> provider = new JAXBElementProvider<T>();
if (setName) {
provider.setCollectionWrapperName("Books");
}
List<Book> books = new ArrayList<>();
books.add(new Book("CXF in Action", 123L));
books.add(new Book("CXF Rocks", 124L));
@SuppressWarnings("unchecked") T o = (T) (type.isArray() ? books.toArray() : type == Set.class ? new HashSet<>(books) : books);
Method m = CollectionsResource.class.getMethod(mName, new Class[0]);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(o, m.getReturnType(), m.getGenericReturnType(), new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
doReadUnqualifiedCollection(bos.toString(), setterName, type);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method doTestObjectFactoryExtraClass.
private void doTestObjectFactoryExtraClass(JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider) throws Exception {
org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 b = new org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3("CXF in Action", 123L, 124L);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider2 = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
provider2.setExtraClass(new Class[] { org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class });
ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 book = (org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3) provider2.readFrom(org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
assertEquals(124L, book.getSuperId());
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method testWriteDerivedType.
@Test
public void testWriteDerivedType() throws Exception {
JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName()));
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(), true);
}
Aggregations