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

Commit

Permalink
updated test cases related to issue: FasterXML/jackson-dataformat-xml…
Browse files Browse the repository at this point in the history
  • Loading branch information
mrserverless committed Feb 12, 2014
1 parent f3e0bcc commit 24ee0bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Annotate your model POJO with either Jackson bindings:
@JacksonXmlRootElement
@JacksonXmlProperty
@JacksonXmlElementWrapper
@JacksonXmlText

or JAXB:

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<UnwrappedElement> 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 = "<root><unwrapped id=\"1\" type=\"string\"/><unwrapped id=\"2\" type=\"string\"/><name>text</name></root>";
protected final static JacksonJaxbXMLProvider provider = new JacksonJaxbXMLProvider(xmlMapper, JacksonJaxbXMLProvider.DEFAULT_ANNOTATIONS);
protected final String rootXml = "<root>" + System.lineSeparator() +
" <unwrapped id=\"1\" type=\"string\"/>" + System.lineSeparator() +
" <unwrapped id=\"2\" type=\"string\"/>" + System.lineSeparator() +
" <name>text</name>" + System.lineSeparator() +
"</root>";
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 {
Expand All @@ -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;

Expand All @@ -83,7 +106,7 @@ public void serializeTest () throws Exception {
new StringKeyObjectValueIgnoreCaseMultivaluedMap(),
output);

assertThat(root).isEqualsToByComparingFields(rootObject);
assertThat(output.toString()).isEqualTo(rootXml);
}

}
Binary file not shown.

This file was deleted.

0 comments on commit 24ee0bf

Please sign in to comment.