use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method doTestWriteJAXBCollection.
private void doTestWriteJAXBCollection(String mName) throws Exception {
JAXBElementProvider<List<?>> provider = new JAXBElementProvider<List<?>>();
List<JAXBElement<Book>> books = new ArrayList<JAXBElement<Book>>();
books.add(new JAXBElement<Book>(new QName("Books"), Book.class, null, new Book("CXF in Action", 123L)));
books.add(new JAXBElement<Book>(new QName("Books"), Book.class, null, new Book("CXF Rocks", 124L)));
Method m = CollectionsResource.class.getMethod(mName, new Class[0]);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(books, m.getReturnType(), m.getGenericReturnType(), new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
doReadUnqualifiedCollection(bos.toString(), "setBooks", List.class);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method testReadFromISO.
@Test
public void testReadFromISO() throws Exception {
String eWithAcute = "\u00E9";
String nameStringUTF16 = "F" + eWithAcute + "lix";
String bookStringUTF16 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<Book><name>" + nameStringUTF16 + "</name></Book>";
byte[] iso88591bytes = bookStringUTF16.getBytes("ISO-8859-1");
JAXBElementProvider<Book> p = new JAXBElementProvider<Book>();
Book book = p.readFrom(Book.class, null, new Annotation[] {}, MediaType.valueOf(MediaType.APPLICATION_XML), null, new ByteArrayInputStream(iso88591bytes));
assertEquals(book.getName(), nameStringUTF16);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method doReadUnqualifiedCollection.
@SuppressWarnings("unchecked")
private <T> void doReadUnqualifiedCollection(String data, String mName, Class<T> type) throws Exception {
JAXBElementProvider<T> provider = new JAXBElementProvider<T>();
Method m = CollectionsResource.class.getMethod(mName, new Class[] { type });
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
Object o = provider.readFrom(type, m.getGenericParameterTypes()[0], new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
assertNotNull(o);
Book b1 = null;
Book b2 = null;
if (type.isArray()) {
assertEquals(2, ((Book[]) o).length);
b1 = ((Book[]) o)[0];
b2 = ((Book[]) o)[1];
} else if (type == Set.class) {
Set<Book> set = CastUtils.cast((Set<?>) o);
List<Book> books = new ArrayList<>(new TreeSet<Book>(set));
b1 = books.get(0);
b2 = books.get(1);
} else {
List<Book> books = (List<Book>) o;
b1 = books.get(0);
b2 = books.get(1);
}
assertEquals(123, b1.getId());
assertEquals("CXF in Action", b1.getName());
assertEquals(124, b2.getId());
assertEquals("CXF Rocks", b2.getName());
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method testWriteWithoutXmlRootElementDerived.
@Test
public void testWriteWithoutXmlRootElementDerived() throws Exception {
JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
provider.setJaxbElementClassMap(Collections.singletonMap(org.apache.cxf.jaxrs.fortest.jaxb.Book.class.getName(), "Book"));
org.apache.cxf.jaxrs.fortest.jaxb.Book b = new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("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);
readSuperBook2(bos.toString(), false);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JAXBElementProviderTest method testReadMalformedXML.
@Test
public void testReadMalformedXML() throws Exception {
JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
try {
provider.readFrom(Book.class, Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream("<Book>".getBytes()));
fail("404 is expected");
} catch (WebApplicationException ex) {
assertEquals(400, ex.getResponse().getStatus());
}
}
Aggregations