Skip to content
This repository has been archived by the owner on Nov 28, 2020. It is now read-only.

Commit

Permalink
added UnwrappedAttributeTest in response to jackson-dataformat-xml is…
Browse files Browse the repository at this point in the history
…sue #101
  • Loading branch information
mrserverless committed Feb 11, 2014
1 parent b434655 commit 26c7f0b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ Uses:
Dropwizard is fast, container-less and JSON is awesome. But understandably, XML files are deeply rooted into the IT ecosystem of many organisations. It would be great if the power and simplicity of Dropwizard to build XML RESTful web services, instead of relying on bloated frameworks and chunky application servers. That's what this project aims to do.

##Noteworthy
Dropwizard-xml is compatible with Dropwizard 0.7.0-SNAPSHOT and above only. Dropwizard 0.7 uses Jackson 2.2.3. The previous Dropwizard release uses Jackson 2.1.4 which contains a show stopper bug with XML unwrapped lists. See jackson-dataformat-xml
Dropwizard-xml is compatible with Dropwizard 0.7.0-rc1 and above only. Dropwizard 0.7 uses Jackson 2.3.0. The previous Dropwizard release uses Jackson 2.1.4 which contains a show stopper bug with XML unwrapped lists. See jackson-dataformat-xml
[ISSUE-58](https://github.com/FasterXML/jackson-dataformat-xml/issues/58)

For unwrapped lists containing elements with multiple attributes, it's easier to use JAXB annotation.
[ISSUE-101](https://github.com/FasterXML/jackson-dataformat-xml/issues/101)

Certain configurations such as Indentation are switched on by default. Will attempt to make these configurable in the
future.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.yunspace.dropwizard.jersey.jackson.xml;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.core.util.StringKeyObjectValueIgnoreCaseMultivaluedMap;
import com.yunspace.dropwizard.jackson.xml.JacksonXML;
import org.junit.Test;

import javax.validation.Validation;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.annotation.Annotation;
import java.util.List;

import static org.fest.assertions.api.Assertions.assertThat;

/**
* this test is created to show that it is easier to use JAXB to annotate
* unwrapped lists instead of Jackson due to issue:
* https://github.com/FasterXML/jackson-dataformat-xml/issues/101
*
*/

public class UnwrappedAttributeTest {
private static final Annotation[] NONE = new Annotation[0];

//@XmlAccessorType(XmlAccessType.FIELD)
@JacksonXmlRootElement(localName = "root")
//@XmlRootElement(name = "root")
static class Root {

// @JacksonXmlProperty(localName = "unwrapped")
// @JacksonXmlElementWrapper(useWrapping = false)
@XmlElement(name = "unwrapped")
public List<UnwrappedElement> unwrapped;

@JacksonXmlProperty(localName = "name")
public String name;

public static class UnwrappedElement {
@JacksonXmlProperty(isAttribute = true)
public String id;

@JacksonXmlProperty(isAttribute = true)
public String type;
}
}

protected final static XmlMapper xmlMapper = JacksonXML.newXMLMapper();
protected final static JacksonXMLMessageBodyProvider provider = new JacksonXMLMessageBodyProvider(xmlMapper, Validation.buildDefaultValidatorFactory().getValidator());
protected final String rootXml = "<root><unwrapped id=\"1\" type=\"string\"/><unwrapped id=\"2\" type=\"string\"/><name>text</name></root>";
protected final Root rootObject = new Root();


@Test
public void deserializeTest () throws Exception {
final ByteArrayInputStream requestList = new ByteArrayInputStream(rootXml.getBytes());
final Class<?> klass = Root.class;
final Object obj = provider.readFrom((Class<Object>) klass,
Root.class,
NONE,
MediaType.APPLICATION_XML_TYPE,
new MultivaluedMapImpl(),
requestList);

}

@Test
public void serializeTest () throws Exception {
Root root = xmlMapper.readValue(rootXml, Root.class);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final Class<?> klass = Root.class;

provider.writeTo(rootObject,
klass,
klass,
NONE,
MediaType.APPLICATION_XML_TYPE,
new StringKeyObjectValueIgnoreCaseMultivaluedMap(),
output);

assertThat(root).isEqualsToByComparingFields(rootObject);
}

}

0 comments on commit 26c7f0b

Please sign in to comment.