|
6 | 6 | import java.util.List;
|
7 | 7 |
|
8 | 8 | import com.fasterxml.jackson.databind.ObjectWriter;
|
| 9 | +import com.fasterxml.jackson.databind.PropertyName; |
9 | 10 | import com.fasterxml.jackson.dataformat.xml.*;
|
10 | 11 | import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
11 | 12 |
|
@@ -79,27 +80,34 @@ public void testDynamicRootName() throws IOException
|
79 | 80 | {
|
80 | 81 | String xml;
|
81 | 82 |
|
82 |
| - ObjectWriter w = _xmlMapper.writer().withRootName("rudy"); |
| 83 | + ObjectWriter w = _xmlMapper.writer().withRootName(PropertyName.construct("rudy", "localhost")); |
83 | 84 |
|
84 | 85 | xml = w.writeValueAsString(new StringBean("foo"));
|
85 |
| - assertEquals("<rudy><text>foo</text></rudy>", xml); |
| 86 | + assertEquals("<rudy xmlns=\"localhost\"><text xmlns=\"\">foo</text></rudy>", xml); |
86 | 87 |
|
87 | 88 | xml = w.writeValueAsString(new StringBean(null));
|
88 |
| - assertEquals("<rudy><text/></rudy>", xml); |
| 89 | + assertEquals("<rudy xmlns=\"localhost\"><text xmlns=\"\"/></rudy>", xml); |
89 | 90 |
|
90 | 91 | // and even with null will respect configured root name
|
91 | 92 | xml = w.writeValueAsString(null);
|
92 |
| - assertEquals("<rudy/>", xml); |
| 93 | + assertEquals("<rudy xmlns=\"localhost\"/>", xml); |
93 | 94 | }
|
94 | 95 |
|
95 | 96 | public void testDynamicRootNameForList() throws IOException
|
96 | 97 | {
|
97 | 98 | String xml;
|
98 | 99 |
|
| 100 | + // First: using explicit root name override, simple String |
99 | 101 | xml = _xmlMapper.writer().withRootName("Listy")
|
100 | 102 | .writeValueAsString(Arrays.asList("abc", "def"));
|
101 | 103 | assertEquals("<Listy><item>abc</item><item>def</item></Listy>", xml);
|
102 | 104 |
|
| 105 | + // Second: using explicit root name override, with namespace |
| 106 | + xml = _xmlMapper.writer().withRootName(PropertyName.construct("Spaced", "http://foo")) |
| 107 | + .writeValueAsString(Arrays.asList("foo", "bar")); |
| 108 | + assertEquals("<Spaced xmlns=\"http://foo\"><item>foo</item><item>bar</item></Spaced>", xml); |
| 109 | + |
| 110 | + // Third: root name annotation |
103 | 111 | xml = _xmlMapper.writer()
|
104 | 112 | .writeValueAsString(new StringList("a", "b"));
|
105 | 113 | assertEquals("<TheStrings><item>a</item><item>b</item></TheStrings>", xml);
|
|
0 commit comments