11
11
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
12
12
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlCData ;
13
13
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlProperty ;
14
- import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlRootElement ;
15
14
16
15
@ SuppressWarnings ("serial" )
17
16
public class TestSerialization extends XmlTestBase
18
17
{
19
- /*
20
- /**********************************************************
21
- /* Helper types
22
- /**********************************************************
23
- */
24
-
25
18
static class StringBean2
26
19
{
27
20
public String text = "foobar" ;
@@ -56,25 +49,13 @@ static class MapBean
56
49
public MapBean () { }
57
50
public MapBean (Map <String ,Integer > v ) { map = v ; }
58
51
}
59
-
52
+
60
53
static class NsElemBean
61
54
{
62
55
@ JacksonXmlProperty (namespace ="http://foo" )
63
56
public String text = "blah" ;
64
57
}
65
58
66
- @ JacksonXmlRootElement (localName ="root" )
67
- static class RootBean
68
- {
69
- public String value = "123" ;
70
- }
71
-
72
- @ JacksonXmlRootElement (localName ="nsRoot" , namespace ="http://foo" )
73
- static class NsRootBean
74
- {
75
- public String value = "abc" ;
76
- }
77
-
78
59
static class CDataStringBean
79
60
{
80
61
@ JacksonXmlCData
@@ -99,75 +80,36 @@ public void serialize(String value, JsonGenerator jgen,
99
80
}
100
81
101
82
static class CustomMap extends LinkedHashMap <String , Integer > { }
102
-
103
- /*
104
- /**********************************************************
105
- /* Set up
106
- /**********************************************************
107
- */
108
-
109
- protected XmlMapper _xmlMapper ;
110
83
111
- // let's actually reuse XmlMapper to make things bit faster
112
- @ Override
113
- public void setUp () throws Exception {
114
- super .setUp ();
115
- _xmlMapper = new XmlMapper ();
116
- }
117
-
118
84
/*
119
85
/**********************************************************
120
86
/* Unit tests
121
87
/**********************************************************
122
88
*/
123
89
124
- // Unit test to verify that root name is properly set
125
- public void testRootName () throws IOException
126
- {
127
- String xml = _xmlMapper .writeValueAsString (new StringBean ());
128
-
129
- // Hmmh. Looks like JDK Stax may adds bogus ns declaration. As such,
130
- // let's just check that name starts ok...
131
- if (!xml .startsWith ("<StringBean" )) {
132
- fail ("Expected root name of 'StringBean'; but XML document is [" +xml +"]" );
133
- }
134
-
135
- // and then see that basic non-namespace root is ok
136
- xml = _xmlMapper .writeValueAsString (new RootBean ());
137
- assertEquals ("<root><value>123</value></root>" , xml );
90
+ protected XmlMapper _xmlMapper = new XmlMapper ();
138
91
139
- // and namespace one too
140
- xml = _xmlMapper .writeValueAsString (new NsRootBean ());
141
- if (xml .indexOf ("nsRoot" ) < 0 ) { // verify localName
142
- fail ("Expected root name of 'nsRoot'; but XML document is [" +xml +"]" );
143
- }
144
- // and NS declaration
145
- if (xml .indexOf ("http://foo" ) < 0 ) {
146
- fail ("Expected NS declaration for 'http://foo', not found, XML document is [" +xml +"]" );
147
- }
148
- }
149
-
150
92
public void testSimpleAttribute () throws IOException
151
93
{
152
94
String xml = _xmlMapper .writeValueAsString (new AttributeBean ());
153
95
xml = removeSjsxpNamespace (xml );
154
96
assertEquals ("<AttributeBean attr=\" something\" />" , xml );
155
97
}
156
98
157
- public void testSimpleAttrAndElem () throws IOException
158
- {
159
- String xml = _xmlMapper .writeValueAsString (new AttrAndElem ());
160
- xml = removeSjsxpNamespace (xml );
161
- assertEquals ("<AttrAndElem id=\" 42\" ><elem>whatever</elem></AttrAndElem>" , xml );
162
- }
163
-
164
99
public void testSimpleNsElem () throws IOException
165
100
{
166
101
String xml = _xmlMapper .writeValueAsString (new NsElemBean ());
167
102
xml = removeSjsxpNamespace (xml );
168
103
// here we assume woodstox automatic prefixes, not very robust but:
169
104
assertEquals ("<NsElemBean><wstxns1:text xmlns:wstxns1=\" http://foo\" >blah</wstxns1:text></NsElemBean>" , xml );
170
105
}
106
+
107
+ public void testSimpleAttrAndElem () throws IOException
108
+ {
109
+ String xml = _xmlMapper .writeValueAsString (new AttrAndElem ());
110
+ xml = removeSjsxpNamespace (xml );
111
+ assertEquals ("<AttrAndElem id=\" 42\" ><elem>whatever</elem></AttrAndElem>" , xml );
112
+ }
171
113
172
114
@ SuppressWarnings ("boxing" )
173
115
public void testMap () throws IOException
@@ -225,7 +167,7 @@ public void testCDataStringArray() throws IOException
225
167
assertEquals ("<CDataStringArrayBean><value><value><![CDATA[<some<data\" ]]></value><value><![CDATA[abc]]></value></value></CDataStringArrayBean>" , xml );
226
168
}
227
169
228
- // for [Issue #41]
170
+ // for [dataformat-xml #41]
229
171
public void testCustomSerializer () throws Exception
230
172
{
231
173
JacksonXmlModule module = new JacksonXmlModule ();
0 commit comments