Skip to content

Commit

Permalink
add a failing test for #170
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 3, 2016
1 parent 764c97a commit 0bf4017
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ public XmlMapper() {
this(new XmlFactory());
}

/** @since 2.4 */
/**
* @since 2.4
*/
public XmlMapper(XMLInputFactory inputF, XMLOutputFactory outF) {
this(new XmlFactory(inputF, outF));
}

/** @since 2.4 */
/**
* @since 2.4
*/
public XmlMapper(XMLInputFactory inputF) {
this(new XmlFactory(inputF));
}
Expand Down Expand Up @@ -111,6 +115,7 @@ public Version version() {
protected void setXMLTextElementName(String name) {
((XmlFactory) _jsonFactory).setXMLTextElementName(name);
}

/**
* Since 2.7
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.fasterxml.jackson.dataformat.xml.failing;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

public class DeserializePolyList178Test extends XmlTestBase
{
static class Company {
public List<Computer> computers;

public Company() {
computers = new ArrayList<Computer>();
}

public Company add(Computer computer) {
if (computers == null) {
computers = new ArrayList<Computer>();
}
computers.add(computer);
return this;
}
}

// 02-Jan-2015, tatu: Does not seem to matter; was included in the original reproduction
// @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = DesktopComputer.class, name = "desktop"),
@JsonSubTypes.Type(value = LaptopComputer.class, name = "laptop")
})
static class Computer {
public String id;
}

@JsonTypeName("desktop")
static class DesktopComputer extends Computer {
public String location;

protected DesktopComputer() { }
public DesktopComputer with(String id0, String l) {
id = id0;
location = l;
return this;
}
}

@JsonTypeName("laptop")
static class LaptopComputer extends Computer {
public String vendor;
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

private final XmlMapper MAPPER = new XmlMapper();

// for [dataformat-xml#178]
public void testPolyIdList178() throws Exception
{
Company input = new Company();
input.add(new DesktopComputer().with("1", "http://foo.com"));
input.add(new DesktopComputer().with("2", "http://bar.com"));
String xml = MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(input);
//System.out.println("XML:\n"+xml);

Company result = MAPPER.readValue(xml, Company.class);
assertNotNull(result.computers);
assertEquals(2, result.computers.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.fasterxml.jackson.dataformat.xml.failing;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.*;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.dataformat.xml.XmlAnnotationIntrospector;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.jaxb.XmlJaxbAnnotationIntrospector;

public class JAXBObjectId170Test extends XmlTestBase
{
static class Company {
public List<Computer> computers;
public List<Employee> employees;

public Company() {
computers = new ArrayList<Computer>();
}

public Company add(Computer computer) {
if (computers == null) {
computers = new ArrayList<Computer>();
}
computers.add(computer);
return this;
}
}

@XmlType(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
static class Employee {
@XmlAttribute
@XmlID
public String id;

@XmlAttribute
public String name;

@XmlIDREF
public Computer computer;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = DesktopComputer.class, name = "desktop"),
@JsonSubTypes.Type(value = LaptopComputer.class, name = "laptop")
})
static class Computer {
public String id;
}

static class DesktopComputer extends Computer {
public String location;

protected DesktopComputer() { }
public DesktopComputer with(String id0, String l) {
id = id0;
location = l;
return this;
}
}

static class LaptopComputer extends Computer {
public String vendor;
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

// for [dataformat-xml#178]
public void testPolyIdList178() throws Exception
{
final String XML =
"<company>\n"+
"<computers>\n"+
" <computers>\n"+
" <desktop id='computer-1'>\n"+
" <location>Bangkok</location>\n"+
" </desktop>\n"+
" </computers>\n"+
" <computers>\n"+
" <desktop id='computer-2'>\n"+
" <location>Pattaya</location>\n"+
" </desktop>\n"+
" </computers>\n"+
" <computers>\n"+
" <laptop id='computer-3'>\n"+
" <vendor>Apple</vendor>\n"+
" </laptop>\n"+
" </computers>\n"+
" </computers>\n"+
" <employees>\n"+
" <employee id='emp-1' name='Robert Patrick'>\n"+
" <computer>computer-3</computer>\n"+
" </employee>\n"+
" <employee id='emp-2' name='Michael Smith'>\n"+
" <computer>computer-2</computer>\n"+
" </employee>\n"+
" </employees>\n"+
"</company>\n"
;

XmlMapper mapper = new XmlMapper();
AnnotationIntrospector intr = XmlAnnotationIntrospector.Pair.instance
(new XmlJaxbAnnotationIntrospector(mapper.getTypeFactory()), new JacksonAnnotationIntrospector());
mapper.setAnnotationIntrospector(intr);
// should be default but doesn't seem to be?
mapper.setDefaultUseWrapper(true);

Company result = mapper.readValue(XML, Company.class);
assertNotNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void testWrappedListBeanDeser() throws Exception
assertEquals(Integer.valueOf(3), bean.values.get(2));
}

// for [Issue#33]
// for [dataformat-xml#33]
public void testWrappedListWithAttribute() throws Exception
{
ListBeanWrapped bean = MAPPER.readValue(
Expand Down

0 comments on commit 0bf4017

Please sign in to comment.