@@ -19,8 +19,7 @@ public class XmlGeneratorInitializerTest extends XmlTestUtil
1919 @ Test
2020 public void testDTDWithOnlyRootElement () throws Exception
2121 {
22- ObjectWriter w = MAPPER .writer ().with (
23- new XmlGeneratorInitializer ()
22+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
2423 .addDTD ("StringBean" , null , null , null ));
2524 assertEquals (a2q ("<!DOCTYPE StringBean>\n "
2625 +"<StringBean><text>test</text></StringBean>" ),
@@ -30,8 +29,7 @@ public void testDTDWithOnlyRootElement() throws Exception
3029 @ Test
3130 public void testDTDWithPublicId () throws Exception
3231 {
33- ObjectWriter w = MAPPER .writer ().with (
34- new XmlGeneratorInitializer ()
32+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
3533 .addDTD ("StringBean" , "system" , "http://foo.bar" , "" ));
3634 assertEquals (a2q ("<!DOCTYPE StringBean PUBLIC 'http://foo.bar' 'system'>\n "
3735 +"<StringBean><text>test</text></StringBean>" ),
@@ -41,8 +39,7 @@ public void testDTDWithPublicId() throws Exception
4139 @ Test
4240 public void testDTDWithSystemIdOnly () throws Exception
4341 {
44- ObjectWriter w = MAPPER .writer ().with (
45- new XmlGeneratorInitializer ()
42+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
4643 .addDTD ("StringBean" , "system" , "" , null ));
4744 assertEquals (a2q ("<!DOCTYPE StringBean SYSTEM 'system'>\n "
4845 +"<StringBean><text>test</text></StringBean>" ),
@@ -52,8 +49,7 @@ public void testDTDWithSystemIdOnly() throws Exception
5249 @ Test
5350 public void testDTDWithInternalSubset () throws Exception
5451 {
55- ObjectWriter w = MAPPER .writer ().with (
56- new XmlGeneratorInitializer ()
52+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
5753 .addDTD ("StringBean" , "system" , "http://foo.bar" , "<!ELEMENT root (#PCDATA)>" ));
5854 assertEquals (a2q ("<!DOCTYPE StringBean PUBLIC 'http://foo.bar' 'system' "
5955 +"[<!ELEMENT root (#PCDATA)>]>\n "
@@ -68,8 +64,7 @@ public void testDTDWithXmlDeclaration() throws Exception
6864 XmlMapper mapper = XmlMapper .builder ()
6965 .configure (XmlWriteFeature .WRITE_XML_DECLARATION , true )
7066 .build ();
71- ObjectWriter w = mapper .writer ().with (
72- new XmlGeneratorInitializer ()
67+ ObjectWriter w = _writer (mapper , new XmlGeneratorInitializer ()
7368 .addDTD ("StringBean" , "system" , "http://foo.bar" , null ));
7469 // XML declaration is emitted with single quotes, DOCTYPE with double quotes,
7570 // so cannot use a2q() on the whole string here.
@@ -84,8 +79,7 @@ public void testDTDWithXmlDeclaration() throws Exception
8479 public void testDTDInvalidNoRoot () throws Exception
8580 {
8681 try {
87- /*ObjectWriter w =*/ MAPPER .writer ().with (
88- new XmlGeneratorInitializer ()
82+ /*ObjectWriter w =*/ _writer (new XmlGeneratorInitializer ()
8983 .addDTD ("" , null , null , null ));
9084 fail ("Should not pass" );
9185 } catch (IllegalArgumentException e ) {
@@ -98,8 +92,7 @@ public void testDTDInvalidNoRoot() throws Exception
9892 @ Test
9993 public void testSimpleComment () throws Exception
10094 {
101- ObjectWriter w = MAPPER .writer ().with (
102- new XmlGeneratorInitializer ()
95+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
10396 .addComment ("Comment content!" ));
10497 assertEquals (a2q ("<!--Comment content!-->\n "
10598 +"<StringBean><text>test</text></StringBean>" ),
@@ -113,8 +106,7 @@ public void testCommentWithXmlDeclaration() throws Exception
113106 XmlMapper mapper = XmlMapper .builder ()
114107 .configure (XmlWriteFeature .WRITE_XML_DECLARATION , true )
115108 .build ();
116- ObjectWriter w = mapper .writer ().with (
117- new XmlGeneratorInitializer ()
109+ ObjectWriter w = _writer (mapper , new XmlGeneratorInitializer ()
118110 .addComment ("Hello" ));
119111 // XML declaration is emitted with single quotes, so cannot use a2q() here.
120112 assertEquals ("<?xml version='1.0' encoding='UTF-8'?>\n "
@@ -127,8 +119,7 @@ public void testCommentWithXmlDeclaration() throws Exception
127119 @ Test
128120 public void testCommentBeforeDTD () throws Exception
129121 {
130- ObjectWriter w = MAPPER .writer ().with (
131- new XmlGeneratorInitializer ()
122+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
132123 .addComment ("before dtd" )
133124 .addDTD ("StringBean" , null , null , null ));
134125 assertEquals (a2q ("<!--before dtd-->\n "
@@ -141,8 +132,7 @@ public void testCommentBeforeDTD() throws Exception
141132 @ Test
142133 public void testDTDBeforeComment () throws Exception
143134 {
144- ObjectWriter w = MAPPER .writer ().with (
145- new XmlGeneratorInitializer ()
135+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
146136 .addDTD ("StringBean" , null , null , null )
147137 .addComment ("after dtd" ));
148138 assertEquals (a2q ("<!DOCTYPE StringBean>\n "
@@ -155,8 +145,7 @@ public void testDTDBeforeComment() throws Exception
155145 @ Test
156146 public void testMultipleComments () throws Exception
157147 {
158- ObjectWriter w = MAPPER .writer ().with (
159- new XmlGeneratorInitializer ()
148+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
160149 .addComment ("first" )
161150 .addComment ("second" )
162151 .addComment ("third" ));
@@ -171,8 +160,7 @@ public void testMultipleComments() throws Exception
171160 @ Test
172161 public void testEmptyComment () throws Exception
173162 {
174- ObjectWriter w = MAPPER .writer ().with (
175- new XmlGeneratorInitializer ()
163+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
176164 .addComment ("" ));
177165 assertEquals (a2q ("<!---->\n "
178166 +"<StringBean><text>test</text></StringBean>" ),
@@ -183,8 +171,7 @@ public void testEmptyComment() throws Exception
183171 @ Test
184172 public void testNullComment () throws Exception
185173 {
186- ObjectWriter w = MAPPER .writer ().with (
187- new XmlGeneratorInitializer ()
174+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
188175 .addComment (null ));
189176 assertEquals (a2q ("<!---->\n "
190177 +"<StringBean><text>test</text></StringBean>" ),
@@ -199,8 +186,7 @@ public void testLinefeedsBetweenPrologDirectivesDisabled() throws Exception
199186 XmlMapper mapper = XmlMapper .builder ()
200187 .configure (XmlWriteFeature .WRITE_XML_DECLARATION , true )
201188 .build ();
202- ObjectWriter w = mapper .writer ().with (
203- new XmlGeneratorInitializer ()
189+ ObjectWriter w = _writer (mapper , new XmlGeneratorInitializer ()
204190 .linefeedsBetweenPrologDirectives (false )
205191 .addDTD ("StringBean" , null , null , null )
206192 .addComment ("squished" ));
@@ -212,5 +198,120 @@ public void testLinefeedsBetweenPrologDirectivesDisabled() throws Exception
212198 w .writeValueAsString (new StringBean ("test" )));
213199 }
214200
201+ // // [dataformat-xml#452]: PI writing -- ok cases
202+
203+ @ Test
204+ public void testSimplePIs () throws Exception
205+ {
206+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
207+ .addPI ("target" , "data" ));
208+ assertEquals (a2q ("<?target data?>\n "
209+ +"<StringBean><text>test</text></StringBean>" ),
210+ w .writeValueAsString (new StringBean ("test" )));
211+
212+ // Then empty/null
213+ final String EXP_WITH_NO_DATA = a2q ("<?target?>\n "
214+ +"<StringBean><text>test</text></StringBean>" );
215+
216+ w = _writer (new XmlGeneratorInitializer ()
217+ .addPI ("target" , "" ));
218+ assertEquals (EXP_WITH_NO_DATA ,
219+ w .writeValueAsString (new StringBean ("test" )));
220+
221+ w = _writer (new XmlGeneratorInitializer ()
222+ .addPI ("target" , null ));
223+ assertEquals (EXP_WITH_NO_DATA ,
224+ w .writeValueAsString (new StringBean ("test" )));
225+ }
226+
227+ // Ensure multiple PIs are all written in insertion order
228+ @ Test
229+ public void testMultiplePIs () throws Exception
230+ {
231+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
232+ .addPI ("xml-stylesheet" , "type=\" text/xsl\" href=\" style.xsl\" " )
233+ .addPI ("target2" , "data2" ));
234+ assertEquals (a2q ("<?xml-stylesheet type=\" text/xsl\" href=\" style.xsl\" ?>\n "
235+ +"<?target2 data2?>\n "
236+ +"<StringBean><text>test</text></StringBean>" ),
237+ w .writeValueAsString (new StringBean ("test" )));
238+ }
239+
240+ // Verify ordering: XML declaration must come before PI
241+ @ Test
242+ public void testPIWithXmlDeclaration () throws Exception
243+ {
244+ XmlMapper mapper = XmlMapper .builder ()
245+ .configure (XmlWriteFeature .WRITE_XML_DECLARATION , true )
246+ .build ();
247+ ObjectWriter w = _writer (mapper , new XmlGeneratorInitializer ()
248+ .addPI ("target" , "data" ));
249+ // XML declaration is emitted with single quotes, so cannot use a2q() here.
250+ assertEquals ("<?xml version='1.0' encoding='UTF-8'?>\n "
251+ +"<?target data?>\n "
252+ +"<StringBean><text>test</text></StringBean>" ,
253+ w .writeValueAsString (new StringBean ("test" )));
254+ }
255+
256+ // Verify "position added" ordering contract: Comment before PI
257+ @ Test
258+ public void testCommentBeforePI () throws Exception
259+ {
260+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
261+ .addComment ("before pi" )
262+ .addPI ("target" , "data" ));
263+ assertEquals (a2q ("<!--before pi-->\n "
264+ +"<?target data?>\n "
265+ +"<StringBean><text>test</text></StringBean>" ),
266+ w .writeValueAsString (new StringBean ("test" )));
267+ }
268+
269+ // Verify "position added" ordering contract: PI before Comment
270+ @ Test
271+ public void testPIBeforeComment () throws Exception
272+ {
273+ ObjectWriter w = _writer (new XmlGeneratorInitializer ()
274+ .addPI ("target" , "data" )
275+ .addComment ("after pi" ));
276+ assertEquals (a2q ("<?target data?>\n "
277+ +"<!--after pi-->\n "
278+ +"<StringBean><text>test</text></StringBean>" ),
279+ w .writeValueAsString (new StringBean ("test" )));
280+ }
281+
282+ // // [dataformat-xml#452]: PI writing -- failing cases
283+
284+ @ Test
285+ public void testInvalidPINullTarget () throws Exception
286+ {
287+ try {
288+ /*ObjectWriter w =*/ _writer (new XmlGeneratorInitializer ()
289+ .addPI (null , "data" ));
290+ fail ("Should not pass" );
291+ } catch (IllegalArgumentException e ) {
292+ verifyException (e , "Illegal argument for 'target': must be" );
293+ }
294+ }
295+
296+ @ Test
297+ public void testInvalidPIEmptyTarget () throws Exception
298+ {
299+ try {
300+ /*ObjectWriter w =*/ _writer (new XmlGeneratorInitializer ()
301+ .addPI ("" , "data" ));
302+ fail ("Should not pass" );
303+ } catch (IllegalArgumentException e ) {
304+ verifyException (e , "Illegal argument for 'target': must be" );
305+ }
306+ }
307+
215308 // // Other tests
309+
310+ private ObjectWriter _writer (XmlGeneratorInitializer initializer ) {
311+ return _writer (MAPPER , initializer );
312+ }
313+
314+ private ObjectWriter _writer (XmlMapper mapper , XmlGeneratorInitializer initializer ) {
315+ return mapper .writer ().with (initializer );
316+ }
216317}
0 commit comments