4
4
5
5
import com .fasterxml .jackson .annotation .JsonInclude ;
6
6
import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
7
+ import com .fasterxml .jackson .core .PrettyPrinter ;
7
8
import com .fasterxml .jackson .databind .SerializationFeature ;
8
9
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
9
10
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
10
11
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlElementWrapper ;
11
12
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlProperty ;
13
+ import com .fasterxml .jackson .dataformat .xml .util .DefaultXmlPrettyPrinter ;
12
14
13
15
public class XmlPrettyPrinterTest extends XmlTestBase
14
16
{
15
17
static class StringWrapperBean {
16
18
public StringWrapper string ;
17
-
19
+
18
20
public StringWrapperBean () { }
19
21
public StringWrapperBean (String s ) { string = new StringWrapper (s ); }
20
22
}
21
23
22
24
static class IntWrapperBean {
23
25
public IntWrapper wrapped ;
24
-
26
+
25
27
public IntWrapperBean () { }
26
28
public IntWrapperBean (int i ) { wrapped = new IntWrapper (i ); }
27
29
}
@@ -44,11 +46,11 @@ public class PojoFor123
44
46
@ JacksonXmlProperty (isAttribute = true )
45
47
public String name ;
46
48
47
- @ JsonInclude (JsonInclude .Include .NON_EMPTY )
49
+ @ JsonInclude (JsonInclude .Include .NON_EMPTY )
48
50
public String property ;
49
51
50
52
public PojoFor123 (String name ) {
51
- this .name = name ;
53
+ this .name = name ;
52
54
}
53
55
}
54
56
@@ -89,6 +91,8 @@ public void setUp() throws Exception {
89
91
_xmlMapper .configure (SerializationFeature .INDENT_OUTPUT , true );
90
92
}
91
93
94
+ private static final String SYSTEM_DEFAULT_NEW_LINE = System .getProperty ("line.separator" );
95
+
92
96
/*
93
97
/**********************************************************
94
98
/* Unit tests
@@ -176,16 +180,90 @@ public void testMultiLevel172() throws Exception
176
180
.writeValueAsString (root );
177
181
// unify possible apostrophes to quotes
178
182
xml = a2q (xml );
179
- // with indentation, should get linefeeds in prolog/epilog too
180
- assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n "
181
- +"<Company>\n "
182
- +" <e>\n "
183
- +" <employee>\n "
184
- +" <id>abc</id>\n "
185
- +" <type>FULL_TIME</type>\n "
186
- +" </employee>\n "
187
- +" </e>\n "
188
- +"</Company>\n " ,
183
+
184
+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + SYSTEM_DEFAULT_NEW_LINE
185
+ +"<Company>" + SYSTEM_DEFAULT_NEW_LINE
186
+ +" <e>" + SYSTEM_DEFAULT_NEW_LINE
187
+ +" <employee>" + SYSTEM_DEFAULT_NEW_LINE
188
+ +" <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
189
+ +" <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
190
+ +" </employee>" + SYSTEM_DEFAULT_NEW_LINE
191
+ +" </e>" + SYSTEM_DEFAULT_NEW_LINE
192
+ +"</Company>" + SYSTEM_DEFAULT_NEW_LINE ,
189
193
xml );
190
194
}
195
+
196
+ public void testNewLine_withCustomNewLine () throws Exception {
197
+ String customNewLine = "\n \r LF\n \r " ;
198
+ PrettyPrinter customXmlPrettyPrinter = new DefaultXmlPrettyPrinter ().withCustomNewLine (customNewLine );
199
+
200
+ Company root = new Company ();
201
+ root .employee .add (new Employee ("abc" ));
202
+
203
+ String xml = _xmlMapper .writer ()
204
+ .with (customXmlPrettyPrinter )
205
+ .with (ToXmlGenerator .Feature .WRITE_XML_DECLARATION )
206
+ .writeValueAsString (root );
207
+ // unify possible apostrophes to quotes
208
+ xml = a2q (xml );
209
+
210
+ // with indentation, should get newLines in prolog/epilog too
211
+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + customNewLine
212
+ + "<Company>" + customNewLine
213
+ + " <e>" + customNewLine
214
+ + " <employee>" + customNewLine
215
+ + " <id>abc</id>" + customNewLine
216
+ + " <type>FULL_TIME</type>" + customNewLine
217
+ + " </employee>" + customNewLine
218
+ + " </e>" + customNewLine
219
+ + "</Company>" + customNewLine ,
220
+ xml );
221
+ }
222
+
223
+ public void testNewLine_systemDefault () throws Exception {
224
+ Company root = new Company ();
225
+ root .employee .add (new Employee ("abc" ));
226
+
227
+ String xml = _xmlMapper .writer ()
228
+ .with (new DefaultXmlPrettyPrinter ())
229
+ .with (ToXmlGenerator .Feature .WRITE_XML_DECLARATION )
230
+ .writeValueAsString (root );
231
+ // unify possible apostrophes to quotes
232
+ xml = a2q (xml );
233
+
234
+ // with indentation, should get newLines in prolog/epilog too
235
+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + SYSTEM_DEFAULT_NEW_LINE
236
+ + "<Company>" + SYSTEM_DEFAULT_NEW_LINE
237
+ + " <e>" + SYSTEM_DEFAULT_NEW_LINE
238
+ + " <employee>" + SYSTEM_DEFAULT_NEW_LINE
239
+ + " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
240
+ + " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
241
+ + " </employee>" + SYSTEM_DEFAULT_NEW_LINE
242
+ + " </e>" + SYSTEM_DEFAULT_NEW_LINE
243
+ + "</Company>" + SYSTEM_DEFAULT_NEW_LINE ,
244
+ xml );
245
+ }
246
+
247
+ public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine () throws Exception {
248
+ Company root = new Company ();
249
+ root .employee .add (new Employee ("abc" ));
250
+
251
+ String xml = _xmlMapper .writer ()
252
+ .with (new DefaultXmlPrettyPrinter ().withCustomNewLine (null ))
253
+ .with (ToXmlGenerator .Feature .WRITE_XML_DECLARATION )
254
+ .writeValueAsString (root );
255
+ // unify possible apostrophes to quotes
256
+ xml = a2q (xml );
257
+
258
+ assertEquals ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" + SYSTEM_DEFAULT_NEW_LINE
259
+ + "<Company>" + SYSTEM_DEFAULT_NEW_LINE
260
+ + " <e>" + SYSTEM_DEFAULT_NEW_LINE
261
+ + " <employee>" + SYSTEM_DEFAULT_NEW_LINE
262
+ + " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
263
+ + " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
264
+ + " </employee>" + SYSTEM_DEFAULT_NEW_LINE
265
+ + " </e>" + SYSTEM_DEFAULT_NEW_LINE
266
+ + "</Company>" + SYSTEM_DEFAULT_NEW_LINE ,
267
+ xml );
268
+ }
191
269
}
0 commit comments