Search in sources :

Example 1 with TagVO2

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

the class JAXBElementProviderTest method testOutAppendElementsSameNs.

@Test
public void testOutAppendElementsSameNs() throws Exception {
    JAXBElementProvider<TagVO2> provider = new JAXBElementProvider<TagVO2>();
    Map<String, String> prefixes = new HashMap<>();
    prefixes.put("http://tags", "ns2");
    provider.setNamespacePrefixes(prefixes);
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}thetag", "{http://tags}t");
    provider.setOutAppendElements(map);
    TagVO2 tag = new TagVO2("A", "B");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(tag, TagVO2.class, TagVO2.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version='1.0' encoding='UTF-8'?>" + "<ns2:t xmlns:ns2=\"http://tags\"><ns2:thetag><group>B</group><name>A</name></ns2:thetag>" + "</ns2:t>";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with TagVO2

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

the class JAXBElementProviderTest method testInNsElementsFromLocalsWildcard2.

@Test
public void testInNsElementsFromLocalsWildcard2() throws Exception {
    String data = "<?xml version='1.0' encoding='UTF-8'?>" + "<ns2:tagholder xmlns:ns2=\"http://tags2\" attr=\"attribute\"><ns2:thetag><group>B</group>" + "<name>A</name></ns2:thetag></ns2:tagholder>";
    JAXBElementProvider<TagVO2Holder> provider = new JAXBElementProvider<TagVO2Holder>();
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("group", "group");
    map.put("name", "name");
    map.put("{http://tags2}*", "{http://tags}*");
    provider.setInTransformElements(map);
    ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
    Object o = provider.readFrom(TagVO2Holder.class, TagVO2Holder.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
    TagVO2Holder holder = (TagVO2Holder) o;
    TagVO2 tag2 = holder.getTagValue();
    assertEquals("A", tag2.getName());
    assertEquals("B", tag2.getGroup());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) ByteArrayInputStream(java.io.ByteArrayInputStream) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with TagVO2

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

the class JAXBElementProviderTest method testOutAttributesAsElementsForList.

@Test
public void testOutAttributesAsElementsForList() throws Exception {
    // Provider
    JAXBElementProvider<List<?>> provider = new JAXBElementProvider<List<?>>();
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    provider.setCollectionWrapperName("tagholders");
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}*", "*");
    provider.setOutTransformElements(map);
    provider.setAttributesToElements(true);
    // data setup
    TagVO2 tag = new TagVO2("A", "B");
    TagVO2Holder holder = new TagVO2Holder();
    holder.setTag(tag);
    List<TagVO2Holder> list = new ArrayList<JAXBElementProviderTest.TagVO2Holder>();
    list.add(holder);
    // ParameterizedType required for Lists of Objects
    ParameterizedType type = new ParameterizedType() {

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return null;
        }

        public Type[] getActualTypeArguments() {
            return new Type[] { TagVO2Holder.class };
        }
    };
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(list, ArrayList.class, type, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<tagholders><tagholder><attr>attribute</attr>" + "<thetag><group>B</group><name>A</name></thetag></tagholder></tagholders>";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ParamType(org.apache.cxf.jaxrs.fortest.jaxb.jaxbelement.ParamType) ParameterizedCollectionType(org.apache.cxf.jaxrs.utils.ParameterizedCollectionType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

Example 4 with TagVO2

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

the class JAXBElementProviderTest method testOutElementsMapLocalNsToLocalWildcard.

@Test
public void testOutElementsMapLocalNsToLocalWildcard() throws Exception {
    JAXBElementProvider<TagVO2Holder> provider = new JAXBElementProvider<TagVO2Holder>();
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}*", "*");
    provider.setOutTransformElements(map);
    TagVO2 tag = new TagVO2("A", "B");
    TagVO2Holder holder = new TagVO2Holder();
    holder.setTag(tag);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(holder, TagVO2Holder.class, TagVO2Holder.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version='1.0' encoding='UTF-8'?>" + "<tagholder attr=\"attribute\"><thetag><group>B</group><name>A</name></thetag></tagholder>";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with TagVO2

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

the class JAXBElementProviderTest method testDropQualifiedElements.

@Test
public void testDropQualifiedElements() throws Exception {
    JAXBElementProvider<TagVO2> provider = new JAXBElementProvider<TagVO2>();
    List<String> list = new ArrayList<>();
    list.add("{http://tags}thetag");
    provider.setOutDropElements(list);
    Map<String, String> map = new HashMap<>();
    map.put("name", "");
    provider.setOutTransformElements(map);
    TagVO2 tag = new TagVO2("A", "B");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(tag, TagVO2.class, TagVO2.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "<?xml version='1.0' encoding='UTF-8'?>" + "<group>B</group>";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

TagVO2 (org.apache.cxf.jaxrs.resources.TagVO2)38 Test (org.junit.Test)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 HashMap (java.util.HashMap)27 LinkedHashMap (java.util.LinkedHashMap)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Method (java.lang.reflect.Method)4 LinkedList (java.util.LinkedList)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 MediaType (javax.ws.rs.core.MediaType)2 XmlType (javax.xml.bind.annotation.XmlType)2 CollectionsResource (org.apache.cxf.jaxrs.resources.CollectionsResource)2 ParameterizedCollectionType (org.apache.cxf.jaxrs.utils.ParameterizedCollectionType)2 OutputStream (java.io.OutputStream)1 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)1 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1