Skip to content

Commit 6b13e5d

Browse files
author
Pascal Gélinas
committed
Added unit test for issue FasterXML#97.
1 parent eceee04 commit 6b13e5d

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2002-2014 Nu Echo Inc. All rights reserved.
3+
*/
4+
5+
package com.fasterxml.jackson.dataformat.xml.unwrapped;
6+
7+
import static org.junit.Assert.*;
8+
import static org.hamcrest.CoreMatchers.*;
9+
10+
import java.util.*;
11+
12+
import org.junit.*;
13+
import org.junit.runner.*;
14+
import org.junit.runners.*;
15+
16+
import com.fasterxml.jackson.annotation.*;
17+
import com.fasterxml.jackson.annotation.JsonTypeInfo.*;
18+
import com.fasterxml.jackson.dataformat.xml.*;
19+
import com.fasterxml.jackson.dataformat.xml.annotation.*;
20+
21+
/**
22+
* @author Nu Echo Inc.
23+
*/
24+
@RunWith(JUnit4.class)
25+
public class TestIssue97 {
26+
27+
@Test
28+
public void testGood() throws Exception {
29+
XmlMapper mapper = new XmlMapper();
30+
mapper.registerSubtypes(FooGood.class);
31+
32+
String xml = "<Foo type=\"good\" data=\"dummy\"><bar>FOOBAR</bar></Foo>";
33+
Foo fooRead = mapper.readValue(xml, Foo.class);
34+
assertThat(fooRead, instanceOf(FooGood.class));
35+
36+
xml = "<Foo data=\"dummy\" type=\"good\" ><bar>FOOBAR</bar></Foo>";
37+
fooRead = mapper.readValue(xml, Foo.class);
38+
assertThat(fooRead, instanceOf(FooGood.class));
39+
}
40+
41+
@Test
42+
public void testBad() throws Exception {
43+
XmlMapper mapper = new XmlMapper();
44+
mapper.registerSubtypes(FooBad.class);
45+
46+
String xml = "<Foo type=\"bad\" data=\"dummy\"><bar><bar>FOOBAR</bar></bar></Foo>";
47+
Foo fooRead = mapper.readValue(xml, Foo.class);
48+
assertThat(fooRead, instanceOf(FooBad.class));
49+
50+
// BOOM
51+
xml = "<Foo data=\"dummy\" type=\"bad\"><bar><bar>FOOBAR</bar></bar></Foo>";
52+
fooRead = mapper.readValue(xml, Foo.class);
53+
assertThat(fooRead, instanceOf(FooBad.class));
54+
}
55+
56+
@JsonTypeInfo(property = "type", use = Id.NAME)
57+
public static abstract class Foo {
58+
private String mData;
59+
60+
public void setData(String data) {
61+
mData = data;
62+
}
63+
64+
@JacksonXmlProperty(isAttribute = true)
65+
public String getData() {
66+
return mData;
67+
}
68+
}
69+
70+
@JsonTypeName("good")
71+
public static class FooGood extends Foo {
72+
private String mBar;
73+
74+
public String getBar() {
75+
return mBar;
76+
}
77+
78+
public void setBar(String bar) {
79+
mBar = bar;
80+
}
81+
}
82+
83+
@JsonTypeName("bad")
84+
public static class FooBad extends Foo {
85+
private List<String> mBar;
86+
87+
@JacksonXmlElementWrapper(useWrapping = false)
88+
public List<String> getBar() {
89+
return mBar;
90+
}
91+
92+
public void setBar(List<String> bar) {
93+
mBar = bar;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)