Search in sources :

Example 1 with AegisTestBean

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

the class AegisElementProviderTest method testReadWriteComplexMap.

@Test
public void testReadWriteComplexMap() throws Exception {
    Map<AegisTestBean, AegisSuperBean> testMap = new HashMap<AegisTestBean, AegisSuperBean>();
    Class<InterfaceWithMap> iwithMapClass = InterfaceWithMap.class;
    Method method = iwithMapClass.getMethod("mapFunction");
    Type mapType = method.getGenericReturnType();
    AegisTestBean bean = new AegisTestBean();
    bean.setBoolValue(Boolean.TRUE);
    bean.setStrValue("hovercraft");
    AegisSuperBean bean2 = new AegisSuperBean();
    bean2.setBoolValue(Boolean.TRUE);
    bean2.setStrValue("hovercraft2");
    testMap.put(bean, bean2);
    MessageBodyWriter<Map<AegisTestBean, AegisSuperBean>> writer = new AegisElementProvider<Map<AegisTestBean, AegisSuperBean>>();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    writer.writeTo(testMap, testMap.getClass(), mapType, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), os);
    byte[] bytes = os.toByteArray();
    String xml = new String(bytes, "utf-8");
    MessageBodyReader<Map<AegisTestBean, AegisSuperBean>> reader = new AegisElementProvider<Map<AegisTestBean, AegisSuperBean>>();
    byte[] simpleBytes = xml.getBytes("utf-8");
    Map<AegisTestBean, AegisSuperBean> map2 = reader.readFrom(null, mapType, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(simpleBytes));
    assertEquals(1, map2.size());
    Map.Entry<AegisTestBean, AegisSuperBean> entry = map2.entrySet().iterator().next();
    AegisTestBean bean1 = entry.getKey();
    assertEquals("hovercraft", bean1.getStrValue());
    assertEquals(Boolean.TRUE, bean1.getBoolValue());
    AegisTestBean bean22 = entry.getValue();
    assertEquals("hovercraft2", bean22.getStrValue());
    assertEquals(Boolean.TRUE, bean22.getBoolValue());
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with AegisTestBean

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

the class AegisElementProviderTest method testWriteTo.

@Test
public void testWriteTo() throws Exception {
    MessageBodyWriter<Object> p = new AegisElementProvider<Object>();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    AegisTestBean bean = new AegisTestBean();
    bean.setBoolValue(Boolean.TRUE);
    bean.setStrValue("hovercraft");
    p.writeTo(bean, null, null, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), os);
    byte[] bytes = os.toByteArray();
    String xml = new String(bytes, "utf-8");
    assertEquals(simpleBeanXml, xml);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) Test(org.junit.Test)

Example 3 with AegisTestBean

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

the class AegisElementProviderTest method testNoNamespaceReadFrom.

@Test
public void testNoNamespaceReadFrom() throws Exception {
    MessageBodyReader<AegisTestBean> p = new NoNamespaceAegisElementProvider<AegisTestBean>();
    byte[] bytes = noNamespaceXml.getBytes("utf-8");
    AegisTestBean bean = p.readFrom(AegisTestBean.class, null, null, null, null, new ByteArrayInputStream(bytes));
    assertEquals("hovercraft", bean.getStrValue());
    assertEquals(Boolean.TRUE, bean.getBoolValue());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) Test(org.junit.Test)

Example 4 with AegisTestBean

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

the class AegisElementProviderTest method testReadFrom.

@Test
public void testReadFrom() throws Exception {
    MessageBodyReader<AegisTestBean> p = new AegisElementProvider<AegisTestBean>();
    byte[] simpleBytes = simpleBeanXml.getBytes("utf-8");
    AegisTestBean bean = p.readFrom(AegisTestBean.class, null, null, null, null, new ByteArrayInputStream(simpleBytes));
    assertEquals("hovercraft", bean.getStrValue());
    assertEquals(Boolean.TRUE, bean.getBoolValue());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) Test(org.junit.Test)

Example 5 with AegisTestBean

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

the class AegisElementProviderTest method testNoNamespaceWriteTo.

@Test
public void testNoNamespaceWriteTo() throws Exception {
    MessageBodyWriter<Object> p = new NoNamespaceAegisElementProvider<Object>();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    AegisTestBean bean = new AegisTestBean();
    bean.setBoolValue(Boolean.TRUE);
    bean.setStrValue("hovercraft");
    p.writeTo(bean, null, null, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), os);
    byte[] bytes = os.toByteArray();
    String xml = new String(bytes, "utf-8");
    assertEquals(noNamespaceXml, xml);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) Test(org.junit.Test)

Aggregations

AegisTestBean (org.apache.cxf.jaxrs.resources.AegisTestBean)5 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MediaType (javax.ws.rs.core.MediaType)1 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)1