diff --git a/README.md b/README.md index 3e8e633..7fd62d1 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Annotate your model POJO with either Jackson bindings: @JacksonXmlRootElement @JacksonXmlProperty @JacksonXmlElementWrapper + @JacksonXmlText or JAXB: diff --git a/dropwizard-jackson-xml/src/test/java/com/yunspace/dropwizard/jersey/jackson/xml/UnwrappedAttributeTest.java b/dropwizard-jackson-xml/src/test/java/com/yunspace/dropwizard/jersey/jackson/xml/UnwrappedAttributeTest.java index 7252031..f8c1a86 100644 --- a/dropwizard-jackson-xml/src/test/java/com/yunspace/dropwizard/jersey/jackson/xml/UnwrappedAttributeTest.java +++ b/dropwizard-jackson-xml/src/test/java/com/yunspace/dropwizard/jersey/jackson/xml/UnwrappedAttributeTest.java @@ -1,19 +1,22 @@ package com.yunspace.dropwizard.jersey.jackson.xml; +import com.fasterxml.jackson.databind.SerializationFeature; 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.fasterxml.jackson.jaxrs.xml.JacksonJaxbXMLProvider; import com.sun.jersey.core.util.MultivaluedMapImpl; import com.sun.jersey.core.util.StringKeyObjectValueIgnoreCaseMultivaluedMap; import com.yunspace.dropwizard.jackson.xml.JacksonXML; +import org.junit.Before; import org.junit.Test; -import javax.validation.Validation; import javax.ws.rs.core.MediaType; +import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.lang.annotation.Annotation; +import java.util.Arrays; import java.util.List; import static org.fest.assertions.api.Assertions.assertThat; @@ -28,33 +31,50 @@ public class UnwrappedAttributeTest { private static final Annotation[] NONE = new Annotation[0]; - //@XmlAccessorType(XmlAccessType.FIELD) - @JacksonXmlRootElement(localName = "root") - //@XmlRootElement(name = "root") + @XmlRootElement(name = "root") static class Root { -// @JacksonXmlProperty(localName = "unwrapped") -// @JacksonXmlElementWrapper(useWrapping = false) @XmlElement(name = "unwrapped") public List unwrapped; - @JacksonXmlProperty(localName = "name") + @XmlElement(name = "name") public String name; public static class UnwrappedElement { - @JacksonXmlProperty(isAttribute = true) + public UnwrappedElement () {} + + public UnwrappedElement (String id, String type) { + this.id = id; + this.type = type; + } + + @XmlAttribute(name = "id") public String id; - @JacksonXmlProperty(isAttribute = true) + @XmlAttribute(name = "type") 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 = "text"; + protected final static JacksonJaxbXMLProvider provider = new JacksonJaxbXMLProvider(xmlMapper, JacksonJaxbXMLProvider.DEFAULT_ANNOTATIONS); + protected final String rootXml = "" + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " text" + System.lineSeparator() + + ""; protected final Root rootObject = new Root(); + @Before + public void setUp() { + xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); + rootObject.unwrapped = Arrays.asList( + new Root.UnwrappedElement("1", "string"), + new Root.UnwrappedElement("2", "string") + ); + rootObject.name = "text"; + } + @Test public void deserializeTest () throws Exception { @@ -67,11 +87,14 @@ public void deserializeTest () throws Exception { new MultivaluedMapImpl(), requestList); + assertThat(obj).isInstanceOf(Root.class); + Root root = (Root) obj; + assertThat(root).isEqualsToByComparingFields(rootObject); } @Test public void serializeTest () throws Exception { - Root root = xmlMapper.readValue(rootXml, Root.class); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); final Class klass = Root.class; @@ -83,7 +106,7 @@ public void serializeTest () throws Exception { new StringKeyObjectValueIgnoreCaseMultivaluedMap(), output); - assertThat(root).isEqualsToByComparingFields(rootObject); + assertThat(output.toString()).isEqualTo(rootXml); } } \ No newline at end of file diff --git a/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.jar b/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 5838598..0000000 Binary files a/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.properties b/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index e171eb4..0000000 --- a/dropwizard-xml-example/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Wed Jan 08 01:13:19 EST 2014 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-bin.zip