KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > SerializerTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.tests;
23
24 import nu.xom.Node;
25 import nu.xom.Serializer;
26 import nu.xom.Element;
27 import nu.xom.DocType;
28 import nu.xom.Document;
29 import nu.xom.Builder;
30 import nu.xom.Comment;
31 import nu.xom.ParsingException;
32 import nu.xom.ProcessingInstruction;
33 import nu.xom.Attribute;
34 import nu.xom.UnavailableCharacterException;
35 import nu.xom.XMLException;
36
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.BufferedReader JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.StringReader JavaDoc;
45 import java.io.UnsupportedEncodingException JavaDoc;
46
47 /**
48  * <p>
49  * Tests for <code>Serializer</code> functionality.
50  * </p>
51  *
52  * @author Elliotte Rusty Harold
53  * @version 1.0
54  *
55  */

56 public class SerializerTest extends XOMTestCase {
57
58     private Builder parser;
59     private final static double version = Double.parseDouble(
60       System.getProperty("java.version").substring(0,3)
61     );
62     Element root = new Element("root");
63     Document doc = new Document(root);
64     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
65     
66     
67     public SerializerTest(String JavaDoc name) {
68         super(name);
69     }
70
71     
72     protected void setUp() {
73        parser = new Builder();
74     }
75     
76     
77     public void testCDATASectionEndDelimiter() throws IOException JavaDoc {
78
79         root.appendChild("]]>");
80         Serializer serializer = new Serializer(out, "UTF-8");
81         serializer.setMaxLength(20);
82         serializer.write(doc);
83         String JavaDoc result = out.toString("UTF-8");
84         assertTrue(result.indexOf("]]&gt;") > 0);
85         
86     }
87
88     
89     public void testXMLSpacePreserve() throws IOException JavaDoc {
90
91         root.addAttribute(
92           new Attribute(
93             "xml:space",
94             "http://www.w3.org/XML/1998/namespace",
95             "preserve"));
96         String JavaDoc value =
97           "This is a long sentence with plenty of opportunities for " +
98           "breaking from beginning to end.";
99         root.appendChild(value);
100         Serializer serializer = new Serializer(out, "UTF-8");
101         serializer.setMaxLength(20);
102         serializer.write(doc);
103         String JavaDoc result = out.toString("UTF-8");
104         assertTrue(result.indexOf(value) > 0);
105         
106     }
107
108     
109     /**
110      * <p>
111      * Check that the UTF-16LE encoding omits the byte-order mark.
112      * </p>
113      *
114      * @throws IOException
115      */

116     public void testUTF16LEBOM() throws IOException JavaDoc {
117         
118         if (version >= 1.3) {
119             // UTF-16LE only supported in 1.3 and later
120
Serializer serializer = new Serializer(out, "UTF-16LE");
121             serializer.write(doc);
122             serializer.flush();
123             out.flush();
124             out.close();
125             byte[] data = out.toByteArray();
126             assertEquals('<', (char) data[0]);
127             assertEquals((byte) 0, data[1]);
128         }
129         
130     }
131     
132     
133     /**
134      * <p>
135      * Check that the UTF-16 encoding outputs a byte-order mark.
136      * </p>
137      *
138      * @throws IOException
139      */

140     public void testUTF16BOM() throws IOException JavaDoc {
141
142         Serializer serializer = new Serializer(out, "UTF-16");
143         serializer.write(doc);
144         serializer.flush();
145         out.flush();
146         out.close();
147         byte[] data = out.toByteArray();
148         assertEquals((byte) 0xFE, data[0]);
149         assertEquals((byte) 0xFF, data[1]);
150         assertEquals((byte) 0, data[2]);
151         assertEquals('<', (char) data[3]);
152
153     }
154     
155     
156     /**
157      * <p>
158      * Check that the UTF-16BE encoding omits the byte-order mark.
159      * </p>
160      *
161      * @throws IOException
162      */

163     public void testUTF16BEBOM() throws IOException JavaDoc {
164
165         if (version >= 1.3) {
166             // UTF-16LE only supported in 1.3 and later
167
Serializer serializer = new Serializer(out, "UTF-16BE");
168             serializer.write(doc);
169             serializer.flush();
170             out.flush();
171             out.close();
172             byte[] data = out.toByteArray();
173             assertEquals((byte) 0, data[0]);
174             assertEquals('<', (char) data[1]);
175         }
176         
177     }
178
179     
180     public void testXMLSpaceDefault() throws IOException JavaDoc {
181
182         root.addAttribute(
183           new Attribute(
184             "xml:space",
185             "http://www.w3.org/XML/1998/namespace",
186             "preserve"));
187         Element child1 = new Element("preserve");
188         String JavaDoc value =
189           "This is a long sentence with plenty of opportunities for " +
190           "breaking from beginning to end.";
191         child1.appendChild(value);
192         Element child2 = new Element("default");
193         root.appendChild(child1);
194         root.appendChild(child2);
195         child2.addAttribute(
196           new Attribute(
197             "xml:space",
198             "http://www.w3.org/XML/1998/namespace",
199             "default"));
200         String JavaDoc value2 =
201             "This is another very long sentence with plenty" +
202             " of opportunities for breaking from beginning to end.";
203         child2.appendChild(value2);
204
205         String JavaDoc value3 =
206           "This is still another very long sentence with plenty of " +
207           "opportunities for breaking from beginning to end.";
208         Element preserveAgain = new Element("test");
209         preserveAgain.appendChild(value3);
210         child2.appendChild(preserveAgain);
211         preserveAgain.addAttribute(
212           new Attribute(
213             "xml:space",
214             "http://www.w3.org/XML/1998/namespace",
215             "preserve"));
216
217
218         Serializer serializer = new Serializer(out, "UTF-8");
219         serializer.setMaxLength(20);
220         serializer.write(doc);
221         String JavaDoc result = out.toString("UTF-8");
222         assertTrue(result.indexOf(value) > 0);
223         assertTrue(result.indexOf(value3) > 0);
224         assertEquals(-1, result.indexOf(value2));
225         
226     }
227
228
229     public void testXMLSpacePreserveWithIndenting()
230       throws IOException JavaDoc {
231
232         root.addAttribute(
233           new Attribute(
234             "xml:space",
235             "http://www.w3.org/XML/1998/namespace",
236             "preserve"));
237         root.appendChild(new Element("sameline"));
238         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
239         Serializer serializer = new Serializer(out, "UTF-8");
240         serializer.setIndent(4);
241         serializer.write(doc);
242         String JavaDoc result = out.toString("UTF-8");
243         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
244           + "<root xml:space=\"preserve\"><sameline/></root>\r\n",
245            result);
246         
247     }
248
249     
250     public void testXMLSpaceUnspecifiedValueWithIndenting()
251       throws IOException JavaDoc {
252         
253         root.addAttribute(
254           new Attribute(
255             "xml:space",
256             "http://www.w3.org/XML/1998/namespace",
257             "undefined"));
258         root.appendChild(new Element("sameline"));
259         Serializer serializer = new Serializer(out, "UTF-8");
260         serializer.setIndent(4);
261         serializer.write(doc);
262         String JavaDoc result = out.toString("UTF-8");
263         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
264           + "<root xml:space=\"undefined\">\r\n <sameline/>\r\n</root>\r\n",
265            result);
266         
267     }
268
269     
270     public void testXMLSpaceDefaultWithIndenting() throws IOException JavaDoc {
271         
272         root.addAttribute(
273           new Attribute(
274             "xml:space",
275             "http://www.w3.org/XML/1998/namespace",
276             "preserve"));
277         Element child = new Element("child");
278         child.addAttribute(
279           new Attribute(
280             "xml:space",
281             "http://www.w3.org/XML/1998/namespace",
282             "default"));
283         root.appendChild(child);
284         Serializer serializer = new Serializer(out, "UTF-8");
285         serializer.setIndent(4);
286         serializer.write(doc);
287         String JavaDoc result = out.toString("UTF-8");
288         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
289           + "<root xml:space=\"preserve\">" +
290             "<child xml:space=\"default\"/></root>\r\n",
291            result);
292         
293     }
294
295     
296     public void testXMLSpaceDefaultWithIndentingAndGrandchildren()
297       throws IOException JavaDoc {
298         
299         root.addAttribute(
300           new Attribute(
301             "xml:space",
302             "http://www.w3.org/XML/1998/namespace",
303             "preserve"));
304         Element child = new Element("child");
305         child.addAttribute(
306           new Attribute(
307             "xml:space",
308             "http://www.w3.org/XML/1998/namespace",
309             "default"));
310         root.appendChild(child);
311         child.appendChild(new Element("differentLine"));
312         Serializer serializer = new Serializer(out, "UTF-8");
313         serializer.setIndent(2);
314         serializer.write(doc);
315         String JavaDoc result = out.toString("UTF-8");
316         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
317           + "<root xml:space=\"preserve\">" +
318             "<child xml:space=\"default\">\r\n <differentLine/>\r\n" +
319             " </child></root>\r\n",
320            result);
321         
322     }
323
324
325     public void testDontSerializeXMLNamespace() throws IOException JavaDoc {
326         
327         Element root
328           = new Element("html", "http://www.w3.org/1999/xhtml");
329         root.addAttribute(
330           new Attribute(
331             "xml:lang", "http://www.w3.org/XML/1998/namespace", "en"));
332         Document doc = new Document(root);
333         Serializer serializer = new Serializer(out, "UTF-8");
334         serializer.write(doc);
335         String JavaDoc result = out.toString("UTF-8");
336         assertEquals(-1, result.indexOf("xmlns:xml"));
337         assertTrue(result.indexOf("xml:lang=") > 1);
338         
339     }
340     
341     public void testDontSerializeNoNamespace() throws IOException JavaDoc {
342         
343         Serializer serializer = new Serializer(out, "UTF-8");
344         serializer.write(doc);
345         String JavaDoc result = out.toString("UTF-8");
346         assertEquals(-1, result.indexOf("xmlns="));
347         
348     }
349     
350     
351     public void testDefaultNamespace() throws IOException JavaDoc {
352         
353         Element root = new Element("root", "http://www.example.com");
354         Document doc = new Document(root);
355         Serializer serializer = new Serializer(out, "UTF-8");
356         serializer.write(doc);
357         String JavaDoc result = out.toString("UTF-8");
358         assertTrue(result.indexOf("xmlns=") > 1);
359         assertTrue(result.indexOf("http://www.example.com") > 1);
360         
361     }
362     
363     
364     public void testEmptyElement() throws IOException JavaDoc {
365         
366         Serializer serializer = new Serializer(out, "UTF-8");
367         serializer.write(doc);
368         String JavaDoc result = out.toString("UTF-8");
369         assertEquals(
370           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root/>\r\n",
371           result
372         );
373         
374     }
375     
376     
377     public void testElementWithText() throws IOException JavaDoc {
378         
379         String JavaDoc data = " test \n\n \n \n hello again";
380         root.appendChild(data);
381         Serializer serializer = new Serializer(out, "UTF-8");
382         serializer.write(doc);
383         String JavaDoc result = out.toString("UTF-8");
384         
385         assertEquals(
386           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
387           + data + "</root>\r\n",
388           result);
389         
390     }
391     
392     
393     public void testStaticElementWithText()
394       throws IOException JavaDoc {
395         
396         String JavaDoc data = " test \n\n \n \n hello again";
397         root.appendChild(data);
398         Serializer serializer = new Serializer(out);
399         serializer.write(doc);
400         String JavaDoc result = out.toString("UTF-8");
401         
402         assertEquals(
403           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
404           + data + "</root>\r\n",
405           result);
406         
407     }
408     
409     
410     public void testElementWithTextAndCarriageReturns()
411       throws IOException JavaDoc {
412         
413         String JavaDoc data = " test \r\n \n \r hello again";
414         root.appendChild(data);
415         Serializer serializer = new Serializer(out, "UTF-8");
416         serializer.write(doc);
417         String JavaDoc result = out.toString("UTF-8");
418         
419         assertEquals(
420           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
421           + " test &#x0D;\n \n &#x0D; hello again"
422           + "</root>\r\n",
423           result);
424         
425     }
426     
427     
428     private void serializeParseAndCompare(Document doc)
429       throws IOException JavaDoc, ParsingException {
430         
431         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
432         Serializer serializer = new Serializer(out, "UTF-8");
433         serializer.write(doc);
434         String JavaDoc result = out.toString("UTF-8");
435         
436         Document resultDoc = parser.build(result, null);
437         XOMTestCase.assertEquals(doc, resultDoc);
438            
439         setOutputStreamSerializeParseAndCompare(doc);
440         
441     }
442
443     
444     private void setOutputStreamSerializeParseAndCompare(Document doc)
445       throws IOException JavaDoc, ParsingException {
446         
447         Serializer serializer = new Serializer(out);
448         serializer.write(doc);
449         ByteArrayOutputStream JavaDoc out2 = new ByteArrayOutputStream JavaDoc();
450         serializer.setOutputStream(out2);
451         serializer.write(doc);
452         String JavaDoc result = out2.toString("UTF-8");
453         
454         Document resultDoc = parser.build(result, null);
455         XOMTestCase.assertEquals(doc, resultDoc);
456         
457     }
458     
459     
460     public void testComment()
461       throws IOException JavaDoc, ParsingException {
462         
463         String JavaDoc data = " <>&amp;&entity; test \n hello again";
464         root.appendChild(new Comment(data));
465         serializeParseAndCompare(doc);
466         
467     }
468     
469     
470     public void testProcessingInstruction()
471       throws IOException JavaDoc, ParsingException {
472         
473         String JavaDoc data = "<>&amp;&entity; test \n hello again";
474         root.appendChild(new ProcessingInstruction("target", data));
475         serializeParseAndCompare(doc);
476         
477     }
478     
479     public void testBasicElementWithText()
480       throws IOException JavaDoc, ParsingException {
481         
482         String JavaDoc data = " test \n hello again";
483         root.appendChild(data);
484         serializeParseAndCompare(doc);
485         
486     }
487     
488     
489     public void testAttributes()
490       throws IOException JavaDoc, ParsingException {
491         
492         root.addAttribute(new Attribute("test", "sadlkhasdk"));
493         String JavaDoc data = " test \n hello again";
494         root.appendChild(data);
495         serializeParseAndCompare(doc);
496         
497         root.addAttribute(new Attribute("test2", "sadlkhasdk"));
498         serializeParseAndCompare(doc);
499
500         root.addAttribute(new Attribute("test3", " s adl khasdk "));
501         serializeParseAndCompare(doc);
502
503         root.addAttribute(new Attribute("xlink:type",
504           "http://www.w3.org/2001/xlink", " s adl khasdk "));
505         serializeParseAndCompare(doc);
506
507     }
508
509     
510     public void testChildElements()
511       throws IOException JavaDoc, ParsingException {
512      
513         serializeParseAndCompare(doc);
514
515         Element child1 = new Element("child");
516         Element child2 = new Element("child");
517         Element child3 = new Element("child");
518         serializeParseAndCompare(doc);
519         root.appendChild(child1);
520         serializeParseAndCompare(doc);
521         child1.appendChild(child2);
522         serializeParseAndCompare(doc);
523         root.appendChild(child3);
524         serializeParseAndCompare(doc);
525         child3.appendChild("some data");
526         serializeParseAndCompare(doc);
527         child2.appendChild("\nsome data with \n line breaks\n");
528         serializeParseAndCompare(doc);
529         root.insertChild("now let's have some mixed content", 0);
530         serializeParseAndCompare(doc);
531
532         root.setNamespaceURI("http://www.example.org/");
533         serializeParseAndCompare(doc);
534         child1.setNamespaceURI("http://www.example.org/");
535         serializeParseAndCompare(doc);
536         child2.setNamespaceURI("http://www.example.org/");
537         serializeParseAndCompare(doc);
538         child3.setNamespaceURI("http://www.example.org/");
539         serializeParseAndCompare(doc);
540         child1.setNamespacePrefix("example");
541         serializeParseAndCompare(doc);
542         child2.setNamespacePrefix("perverse");
543         serializeParseAndCompare(doc);
544         
545     }
546
547     
548     public void testPrologAndEpilog()
549       throws IOException JavaDoc, ParsingException {
550         
551         serializeParseAndCompare(doc);
552
553         doc.insertChild(new Comment("Hello"), 0);
554         serializeParseAndCompare(doc);
555         doc.insertChild(new DocType("root"), 0);
556         serializeParseAndCompare(doc);
557         doc.insertChild(new ProcessingInstruction("test", "some data"),
558           0);
559         serializeParseAndCompare(doc);
560         doc.insertChild(new Comment("Goodbye"), 0);
561         serializeParseAndCompare(doc);
562         doc.insertChild(
563           new ProcessingInstruction("goodbye", "some data"), 0);
564         serializeParseAndCompare(doc);
565         doc.appendChild(new Comment("Hello"));
566         serializeParseAndCompare(doc);
567         doc.appendChild(
568           new ProcessingInstruction("test", "some data"));
569         serializeParseAndCompare(doc);
570         
571     }
572     
573     
574     public void testChangeLineSeparator() throws IOException JavaDoc {
575         
576         String JavaDoc breaks =
577           "This\nstring\rcontains\r\nseveral\r\rweird line breaks.";
578         root.appendChild(breaks);
579         root.addAttribute(new Attribute("test", breaks));
580             
581         Serializer serializer = new Serializer(out, "UTF-8");
582         serializer.setLineSeparator("\n");
583         serializer.write(doc);
584         String JavaDoc result = out.toString("UTF-8");
585         assertTrue(result.indexOf('\n') > 0);
586         assertTrue(result + "**\n" + result.indexOf('\r'),
587           result.indexOf('\r') == -1);
588
589         out = new ByteArrayOutputStream JavaDoc();
590         serializer = new Serializer(out, "UTF-8");
591         serializer.setLineSeparator("\r");
592         serializer.write(doc);
593         result = out.toString("UTF-8");
594         assertTrue(result.indexOf('\r') > 0);
595         assertTrue(result.indexOf('\n') == -1);
596         
597         out = new ByteArrayOutputStream JavaDoc();
598         serializer = new Serializer(out, "UTF-8");
599         serializer.setLineSeparator("\r\n");
600         serializer.write(doc);
601         result = out.toString("UTF-8");
602         assertTrue(result.indexOf("\r\n") > 0);
603         for (int i = 0; i < result.length(); i++) {
604           int c = result.charAt(i);
605           if (c == '\r') assertTrue(result.charAt(i+1) == '\n');
606         }
607         
608     }
609     
610     
611     public void testDontChangeLineSeparator() throws IOException JavaDoc {
612         
613         String JavaDoc breaks
614           = "This\nstring\rcontains\r\rseveral\n\nweird line breaks.";
615         String JavaDoc breaksHalfEscaped
616           = "This\nstring&#x0D;contains&#x0D;&#x0D;several" +
617             "\n\nweird line breaks.";
618         String JavaDoc breaksEscaped
619           = "This&#x0A;string&#x0D;contains&#x0D;&#x0D;several" +
620             "&#x0A;&#x0A;weird line breaks.";
621         root.appendChild(breaks);
622             
623         Serializer serializer = new Serializer(out, "UTF-8");
624         serializer.write(doc);
625         String JavaDoc result = out.toString("UTF-8");
626         assertTrue(result.indexOf(breaksHalfEscaped) > 0);
627
628         root = new Element("root");
629         doc = new Document(root);
630         root.addAttribute(new Attribute("test", breaks));
631             
632         out = new ByteArrayOutputStream JavaDoc();
633         serializer = new Serializer(out, "UTF-8");
634         serializer.write(doc);
635         result = out.toString("UTF-8");
636         assertTrue(result.indexOf(breaksEscaped) > 0);
637         
638     }
639  
640     
641     public void testPreserveBaseURI() throws IOException JavaDoc {
642
643         Serializer serializer = new Serializer(out, "UTF-8");
644         serializer.setPreserveBaseURI(true);
645         serializer.write(doc);
646         String JavaDoc result = out.toString("UTF-8");
647         assertTrue(result.indexOf("<root") > 1);
648         doc.setBaseURI("http://www.example.com/index.xml");
649         serializer.write(doc);
650         result = out.toString("UTF-8");
651         assertTrue(result.indexOf("<root ") > 1);
652         assertTrue(result.indexOf("xml:base=") > 1);
653         assertTrue(
654           result.indexOf("http://www.example.com/index.xml") > 1
655         );
656         
657     }
658
659     
660     public void testPreserveBaseURIWithChildren() throws IOException JavaDoc {
661
662         String JavaDoc base = "http://www.example.com/index.xml";
663         root.setBaseURI(base);
664         Element child = new Element("child");
665         child.setBaseURI(base);
666         root.appendChild(child);
667         Serializer serializer = new Serializer(out, "UTF-8");
668         serializer.setPreserveBaseURI(true);
669         serializer.write(doc);
670         String JavaDoc result = out.toString("UTF-8");
671         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
672           + "<root xml:base=\"" + base + "\"><child/></root>\r\n", result);
673         
674     }
675
676     
677     public void testPreserveBaseURIDoesntOverrideXMLBase()
678       throws IOException JavaDoc {
679         
680         root.addAttribute(new Attribute("xml:base",
681           "http://www.w3.org/XML/1998/namespace",
682           "http://www.cafeconleche.org/"));
683         Serializer serializer = new Serializer(out, "UTF-8");
684         serializer.setPreserveBaseURI(true);
685         serializer.write(doc);
686         String JavaDoc result = out.toString("UTF-8");
687         assertTrue(result.indexOf("<root") > 1);
688         doc.setBaseURI("http://www.example.com/index.xml");
689         serializer.write(doc);
690         result = out.toString("UTF-8");
691         assertTrue(result.indexOf("<root ") > 1);
692         assertTrue(result.indexOf("xml:base=") > 1);
693         assertTrue(result.indexOf("http://www.cafeconleche.org/") > 1);
694         assertEquals(-1, result.indexOf("http://www.example.com/index.xml"));
695         
696     }
697     
698     
699     public void testSetLineSeparator() {
700         
701         Serializer serializer = new Serializer(System.out);
702         
703         serializer.setLineSeparator("\r");
704         assertEquals("\r", serializer.getLineSeparator());
705         serializer.setLineSeparator("\n");
706         assertEquals("\n", serializer.getLineSeparator());
707         serializer.setLineSeparator("\r\n");
708         assertEquals("\r\n", serializer.getLineSeparator());
709         
710         try {
711             serializer.setLineSeparator("r");
712             fail("Allowed illegal separator character");
713         }
714         catch (IllegalArgumentException JavaDoc success) {
715             assertNotNull(success.getMessage());
716         }
717         
718         try {
719             serializer.setLineSeparator("n");
720             fail("Allowed illegal separator character");
721         }
722         catch (IllegalArgumentException JavaDoc success) {
723             assertNotNull(success.getMessage());
724         }
725         
726         try {
727             serializer.setLineSeparator(" ");
728             fail("Allowed illegal separator character");
729         }
730         catch (IllegalArgumentException JavaDoc success) {
731             assertNotNull(success.getMessage());
732         }
733         
734         try {
735             serializer.setLineSeparator("rn");
736             fail("Allowed illegal separator character");
737         }
738         catch (IllegalArgumentException JavaDoc success) {
739             assertNotNull(success.getMessage());
740         }
741         
742         try {
743             serializer.setLineSeparator("<");
744             fail("Allowed illegal separator character");
745         }
746         catch (IllegalArgumentException JavaDoc success) {
747             assertNotNull(success.getMessage());
748         }
749         
750         try {
751             serializer.setLineSeparator("\u0085");
752             fail("Allowed NEL separator character");
753         }
754         catch (IllegalArgumentException JavaDoc success) {
755             assertNotNull(success.getMessage());
756         }
757         
758     }
759
760       
761     public void testLowerLimitOfUnicodeInCharacterData()
762       throws IOException JavaDoc {
763         
764         root.appendChild("\uD800\uDC00");
765         Serializer serializer = new Serializer(out, "ISO-8859-1");
766         serializer.write(doc);
767         String JavaDoc result = out.toString("ISO-8859-1");
768         assertTrue(result, result.indexOf("&#x10000;") > 12);
769         
770     }
771     
772     
773     public void testUpperLimitOfUnicodeInCharacterData()
774       throws IOException JavaDoc {
775         
776         root.appendChild("\uDBFF\uDFFD");
777         Serializer serializer = new Serializer(out, "ISO-8859-1");
778         serializer.write(doc);
779         String JavaDoc result = out.toString("ISO-8859-1");
780         assertTrue(result, result.indexOf("&#x10FFFD;") > 12);
781         
782     }
783       
784     
785     public void testSerializePlane1CharacterInAttributeValue()
786       throws IOException JavaDoc {
787         
788         root.addAttribute(new Attribute("test", "\uD834\uDD1E"));
789         Serializer serializer = new Serializer(out, "ISO-8859-1");
790         serializer.write(doc);
791         String JavaDoc result = out.toString("ISO-8859-1");
792         assertTrue(result, result.indexOf("&#x1D11E;") > 12);
793         
794     }
795     
796     public void testSerializePlane1CharacterInCharacterData()
797       throws IOException JavaDoc {
798         
799         root.appendChild("\uD834\uDD1E");
800         Serializer serializer = new Serializer(out, "ISO-8859-1");
801         serializer.write(doc);
802         String JavaDoc result = out.toString("ISO-8859-1");
803         assertTrue(result, result.indexOf("&#x1D11E;") > 12);
804         
805     }
806     
807     
808     public void testSurrogatePairCountsAsOneCharacterForColumnCount()
809       throws IOException JavaDoc {
810         
811         Element root = new Element("r");
812         root.appendChild("\uD834\uDD1E");
813         Document doc = new Document(root);
814         Serializer serializer = new ColumnSerializer(out);
815         serializer.write(doc);
816         
817     }
818     
819     
820     private static class ColumnSerializer extends Serializer {
821      
822         
823         ColumnSerializer(OutputStream JavaDoc out) {
824             super(out);
825         }
826         
827         
828         public void write(Document doc) throws IOException JavaDoc {
829             
830             for (int i = 0; i < doc.getChildCount(); i++) {
831                 writeChild(doc.getChild(i));
832             }
833             super.flush();
834             assertEquals(8, super.getColumnNumber());
835             
836         }
837
838         
839     }
840     
841     
842     public void testEscapeAttributeValue() throws IOException JavaDoc {
843         
844         root.addAttribute(new Attribute("test", "\u0110"));
845         Serializer serializer = new Serializer(out, "ISO-8859-1");
846         serializer.write(doc);
847         String JavaDoc result = out.toString("ISO-8859-1");
848         assertTrue(result, result.indexOf("&#x110;") > 5);
849         
850     }
851
852     
853     public void testLineFeedInAttributeValueWithDefaultOptions()
854       throws IOException JavaDoc, ParsingException {
855         
856         root.addAttribute(new Attribute("test", "\n"));
857         Serializer serializer = new Serializer(out, "ISO-8859-1");
858         serializer.write(doc);
859         out.close();
860         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
861         Document reparsed = parser.build(in);
862         assertEquals(doc, reparsed);
863         
864     }
865     
866     
867     public void testCarriageReturnInAttributeValueWithDefaultOptions()
868       throws IOException JavaDoc, ParsingException {
869         
870         root.addAttribute(new Attribute("test", "\r"));
871         Serializer serializer = new Serializer(out, "ISO-8859-1");
872         serializer.write(doc);
873         out.close();
874         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
875         Document reparsed = parser.build(in);
876         assertEquals(doc, reparsed);
877         
878     }
879     
880     
881     public void testCarriageReturnInTextWithDefaultOptions()
882       throws IOException JavaDoc, ParsingException {
883         
884         root.appendChild("\r");
885         Serializer serializer = new Serializer(out, "ISO-8859-1");
886         serializer.write(doc);
887         out.close();
888         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
889         Document reparsed = parser.build(in);
890         assertEquals(doc, reparsed);
891         
892     }
893     
894     
895     public void testTabInAttributeValueWithDefaultOptions()
896       throws IOException JavaDoc, ParsingException {
897         
898         root.addAttribute(new Attribute("test", "\t"));
899         Serializer serializer = new Serializer(out, "ISO-8859-1");
900         serializer.write(doc);
901         out.close();
902         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
903         Document reparsed = parser.build(in);
904         assertEquals(doc, reparsed);
905         
906     }
907     
908     
909     /**
910      * <p>
911      * Test that tabs in attribute values are escaped even when
912      * a line separator is set.
913      * </p>
914      */

915     public void testTabInAttributeValueWithLineSeparator()
916       throws IOException JavaDoc, ParsingException {
917         
918         root.addAttribute(new Attribute("test", "\t"));
919         Serializer serializer = new Serializer(out, "ISO-8859-1");
920         serializer.setLineSeparator("\r");
921         serializer.write(doc);
922         out.close();
923         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
924         Document reparsed = parser.build(in);
925         assertEquals(doc, reparsed);
926         
927     }
928     
929     
930     
931     /**
932      * <p>
933      * Test that tabs in attribute values are not escaped when
934      * indenting.
935      * </p>
936      */

937     public void testTabInAttributeValueWithIndenting()
938       throws IOException JavaDoc, ParsingException {
939       
940         root.addAttribute(new Attribute("test", "\t"));
941         Serializer serializer = new Serializer(out, "ISO-8859-1");
942         serializer.setIndent(2);
943         serializer.write(doc);
944         out.close();
945         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
946         Document reparsed = parser.build(in);
947         String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
948         assertEquals("Tab not normalized to space", " ", result);
949         
950     }
951
952     
953     public void testCRLFInAttributeValueWithLineSeparatorCR()
954       throws IOException JavaDoc, ParsingException {
955         
956         root.addAttribute(new Attribute("test", "\r\n"));
957         Serializer serializer = new Serializer(out, "ISO-8859-1");
958         serializer.setLineSeparator("\r");
959         serializer.write(doc);
960         out.close();
961         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
962         Document reparsed = parser.build(in);
963         String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
964         assertEquals("\r", result);
965         
966     }
967     
968     
969     public void testCRLFInAttributeValueWithLineSeparatorLF()
970       throws IOException JavaDoc, ParsingException {
971         
972         root.addAttribute(new Attribute("test", "\r\n"));
973         Serializer serializer = new Serializer(out, "ISO-8859-1");
974         serializer.setLineSeparator("\n");
975         serializer.write(doc);
976         out.close();
977         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
978         Document reparsed = parser.build(in);
979         String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
980         assertEquals("\n", result);
981         
982     }
983     
984     public void testLFInAttributeValueWithLineSeparatorCRLF()
985       throws IOException JavaDoc, ParsingException {
986         
987         root.addAttribute(new Attribute("test", "\n"));
988         Serializer serializer = new Serializer(out, "ISO-8859-1");
989         serializer.setLineSeparator("\r\n");
990         serializer.write(doc);
991         out.close();
992         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
993         Document reparsed = parser.build(in);
994         String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
995         assertEquals("\r\n", result);
996         
997     }
998
999     
1000    public void testNotEscapeLinefeedInTextContent()
1001      throws IOException JavaDoc {
1002
1003        root.appendChild("\r\n");
1004        Serializer serializer = new Serializer(out, "ISO-8859-1");
1005        serializer.write(doc);
1006        out.close();
1007        String JavaDoc result = new String JavaDoc(out.toByteArray(), "ISO-8859-1");
1008        assertEquals(
1009          "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>&#x0D;\n</root>\r\n",
1010          result
1011        );
1012        
1013    }
1014    
1015    
1016    public void testCRLFInAttributeValueWithIndenting()
1017      throws IOException JavaDoc, ParsingException {
1018
1019        root.addAttribute(new Attribute("test", "\r\n"));
1020        Serializer serializer = new Serializer(out, "ISO-8859-1");
1021        serializer.setIndent(2);
1022        serializer.write(doc);
1023        out.close();
1024        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1025        Document reparsed = parser.build(in);
1026        String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
1027        assertEquals("CRLF unnecessarily escaped", -1, result.indexOf('\r'));
1028        // Need to figure out the serializer should indent this
1029
// and write a unit test for that too.
1030

1031    }
1032    
1033    
1034    public void testCRLFInAttributeValueWithMaxLength()
1035      throws IOException JavaDoc, ParsingException {
1036
1037        root.addAttribute(new Attribute("test", "\r\n"));
1038
1039        Serializer serializer = new Serializer(out, "ISO-8859-1");
1040        serializer.setMaxLength(64);
1041        serializer.write(doc);
1042        out.close();
1043        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1044        Document reparsed = parser.build(in);
1045        String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
1046        assertEquals("CRLF unnecessarily escaped", " ", result);
1047        
1048    }
1049
1050    
1051    public void testCRInTextValueWithLineSeparator()
1052      throws IOException JavaDoc, ParsingException {
1053
1054        root.appendChild("\r");
1055        Serializer serializer = new Serializer(out, "ISO-8859-1");
1056        serializer.setLineSeparator("\n");
1057        serializer.write(doc);
1058        out.close();
1059        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1060        Document reparsed = parser.build(in);
1061        String JavaDoc result = reparsed.getValue();
1062        assertEquals("\n", result);
1063        
1064    }
1065    
1066    
1067    public void testCRLFInTextValueWithLineSeparator()
1068      throws IOException JavaDoc, ParsingException {
1069        
1070        root.appendChild("test \r\n test");
1071        Serializer serializer = new Serializer(out, "ISO-8859-1");
1072        serializer.setLineSeparator("\n");
1073        serializer.write(doc);
1074        out.close();
1075        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1076        Document reparsed = parser.build(in);
1077        String JavaDoc result = reparsed.getValue();
1078        assertEquals("test \n test", result);
1079        
1080    }
1081    
1082    
1083    public void testCRInTextWithIndenting()
1084      throws IOException JavaDoc, ParsingException {
1085        
1086        root.appendChild("\r");
1087        Serializer serializer = new Serializer(out, "ISO-8859-1");
1088        serializer.setIndent(2);
1089        serializer.write(doc);
1090        out.close();
1091        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1092        Document reparsed = parser.build(in);
1093        String JavaDoc result = reparsed.getValue();
1094        assertEquals("Carriage return unnecessarily escaped",
1095          -1, result.indexOf('\r'));
1096        
1097        // really need to think about what the serializer should output here
1098
// and write a test case for that; this is not ideal output
1099

1100    }
1101    
1102    
1103    public void testCRInTextWithMaxLength()
1104      throws IOException JavaDoc, ParsingException {
1105
1106        root.appendChild("\r");
1107        Serializer serializer = new Serializer(out, "ISO-8859-1");
1108        serializer.setMaxLength(64);
1109        serializer.write(doc);
1110        out.close();
1111        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1112        Document reparsed = parser.build(in);
1113        String JavaDoc result = reparsed.getValue();
1114        assertEquals("Carriage return unnecessarily escaped", "\n", result);
1115        
1116    }
1117    
1118    
1119    public void testTabInAttributeValueWithMaxLength()
1120      throws IOException JavaDoc, ParsingException {
1121        
1122        root.addAttribute(new Attribute("test", "\t"));
1123        Serializer serializer = new Serializer(out, "ISO-8859-1");
1124        serializer.setMaxLength(64);
1125        serializer.write(doc);
1126        out.close();
1127        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1128        Document reparsed = parser.build(in);
1129        String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
1130        assertEquals("Tab not normalized to space", " ", result);
1131        
1132    }
1133
1134    
1135    /**
1136     * <p>
1137     * Test that tabs in attribute values are escaped when
1138     * max length is set to 0
1139     * </p>
1140     */

1141    public void testTabInAttributeValueWithZeroMaxLength()
1142      throws IOException JavaDoc, ParsingException {
1143
1144        root.addAttribute(new Attribute("test", "\t"));
1145        Serializer serializer = new Serializer(out, "ISO-8859-1");
1146        serializer.setMaxLength(0);
1147        serializer.write(doc);
1148        out.close();
1149        InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
1150        Document reparsed = parser.build(in);
1151        String JavaDoc result = reparsed.getRootElement().getAttributeValue("test");
1152        assertEquals("Tab not normalized to space", "\t", result);
1153        
1154    }
1155    
1156
1157    public void testSetMaxLength() {
1158        Serializer serializer = new Serializer(System.out);
1159        
1160        serializer.setMaxLength(72);
1161        assertEquals(72, serializer.getMaxLength());
1162        serializer.setMaxLength(720);
1163        assertEquals(720, serializer.getMaxLength());
1164        serializer.setMaxLength(1);
1165        assertEquals(1, serializer.getMaxLength());
1166        serializer.setMaxLength(0);
1167        assertEquals(0, serializer.getMaxLength());
1168        serializer.setMaxLength(-1);
1169        assertEquals(0, serializer.getMaxLength());
1170        
1171    }
1172
1173    
1174    public void testSetIndent() {
1175        
1176        Serializer serializer = new Serializer(System.out);
1177        serializer.setIndent(72);
1178        assertEquals(72, serializer.getIndent());
1179        serializer.setIndent(720);
1180        assertEquals(720, serializer.getIndent());
1181        serializer.setIndent(1);
1182        assertEquals(1, serializer.getIndent());
1183        serializer.setIndent(0);
1184        assertEquals(0, serializer.getIndent());
1185        try {
1186            serializer.setIndent(-1);
1187            fail("Allowed negative indent");
1188        }
1189        catch (IllegalArgumentException JavaDoc success) {
1190            assertNotNull(success.getMessage());
1191        }
1192        
1193    }
1194
1195    
1196    public void testLineLength() throws IOException JavaDoc {
1197          
1198        int length = 40;
1199        String JavaDoc data = "This is a really long string that does not "
1200         + "contain any line breaks. However, there is lots of "
1201         + "white space so there shouldn't be any trouble wrapping it"
1202         + " into 40 characters or less per line. ";
1203        root.appendChild(data);
1204        Serializer serializer = new Serializer(out, "UTF-8");
1205        serializer.setMaxLength(length);
1206        serializer.write(doc);
1207        String JavaDoc result = out.toString("UTF-8");
1208        
1209        BufferedReader JavaDoc reader
1210          = new BufferedReader JavaDoc(new StringReader JavaDoc(result));
1211        for (String JavaDoc line = reader.readLine();
1212             line != null;
1213             line = reader.readLine()) {
1214            assertTrue(line.length() + ": " + line,
1215              line.length() <= length);
1216        }
1217        
1218    }
1219
1220    
1221    public void testLineLengthWithSetOutputStream()
1222      throws IOException JavaDoc {
1223          
1224        int length = 40;
1225        String JavaDoc data = "This is a really long string that does not "
1226         + "contain any line breaks. However, there is lots of "
1227         + "white space so there shouldn't be any trouble wrapping it"
1228         + " into 40 characters or less per line. ";
1229        root.appendChild(data);
1230        Serializer serializer = new Serializer(new ByteArrayOutputStream JavaDoc(), "UTF-8");
1231        serializer.setMaxLength(length);
1232        serializer.write(doc);
1233        serializer.setOutputStream(out);
1234        serializer.write(doc);
1235        String JavaDoc result = out.toString("UTF-8");
1236        
1237        BufferedReader JavaDoc reader
1238          = new BufferedReader JavaDoc(new StringReader JavaDoc(result));
1239        for (String JavaDoc line = reader.readLine();
1240             line != null;
1241             line = reader.readLine()) {
1242            assertTrue(line.length() + ": " + line,
1243              line.length() <= length);
1244        }
1245        
1246    }
1247    
1248    
1249    public void testPrettyXML() throws IOException JavaDoc {
1250        
1251        Element items = new Element("itemSet");
1252        items.appendChild(new Element("item1"));
1253        items.appendChild(new Element("item2"));
1254        Document doc = new Document(items);
1255        Serializer serializer = new Serializer(out);
1256        serializer.setIndent(4);
1257        serializer.write(doc);
1258        serializer.flush();
1259        out.close();
1260        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1261        assertEquals(
1262          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1263          + "<itemSet>\r\n <item1/>\r\n <item2/>\r\n"
1264          + "</itemSet>\r\n",
1265          result
1266        );
1267        
1268    }
1269
1270    
1271    public void testIndentAndBreakBeforeComment() throws IOException JavaDoc {
1272        
1273        Element items = new Element("itemSet");
1274        items.appendChild(new Comment("item1"));
1275        Document doc = new Document(items);
1276        Serializer serializer = new Serializer(out);
1277        serializer.setIndent(4);
1278        serializer.write(doc);
1279        serializer.flush();
1280        out.close();
1281        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1282        assertEquals(
1283          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1284          + "<itemSet>\r\n <!--item1-->\r\n"
1285          + "</itemSet>\r\n",
1286          result
1287        );
1288        
1289    }
1290    
1291    
1292    public void testWhiteSpaceBetweenCommentsIsBoundaryWhiteSpace()
1293      throws IOException JavaDoc {
1294        
1295        Element items = new Element("itemSet");
1296        items.appendChild(new Comment("item1"));
1297        items.appendChild(" \r\n ");
1298        items.appendChild(new Comment("item2"));
1299        Document doc = new Document(items);
1300        Serializer serializer = new Serializer(out);
1301        serializer.setIndent(4);
1302        serializer.write(doc);
1303        serializer.flush();
1304        out.close();
1305        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1306        assertEquals(
1307          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1308          + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1309          + "</itemSet>\r\n",
1310          result
1311        );
1312        
1313    }
1314    
1315    
1316    public void testWhiteSpaceBeforeCommentIsBoundaryWhiteSpace()
1317      throws IOException JavaDoc {
1318        
1319        Element items = new Element("itemSet");
1320        items.appendChild(" \r\n ");
1321        items.appendChild(new Comment("item1"));
1322        items.appendChild(new Comment("item2"));
1323        Document doc = new Document(items);
1324        Serializer serializer = new Serializer(out);
1325        serializer.setIndent(4);
1326        serializer.write(doc);
1327        serializer.flush();
1328        out.close();
1329        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1330        assertEquals(
1331          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1332          + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1333          + "</itemSet>\r\n",
1334          result
1335        );
1336        
1337    }
1338    
1339    
1340    public void testWhiteSpaceAfterCommentsIsBoundaryWhiteSpace()
1341      throws IOException JavaDoc {
1342        
1343        Element items = new Element("itemSet");
1344        items.appendChild(new Comment("item1"));
1345        items.appendChild(new Comment("item2"));
1346        items.appendChild(" \r\n ");
1347        Document doc = new Document(items);
1348        Serializer serializer = new Serializer(out);
1349        serializer.setIndent(4);
1350        serializer.write(doc);
1351        serializer.flush();
1352        out.close();
1353        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1354        assertEquals(
1355          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1356          + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1357          + "</itemSet>\r\n",
1358          result
1359        );
1360        
1361    }
1362    
1363    
1364    public void testIndentAndBreakBeforeProcessingInstruction()
1365      throws IOException JavaDoc {
1366        
1367        Element items = new Element("itemSet");
1368        items.appendChild(new ProcessingInstruction("target", "value"));
1369        Document doc = new Document(items);
1370        Serializer serializer = new Serializer(out);
1371        serializer.setIndent(4);
1372        serializer.write(doc);
1373        serializer.flush();
1374        out.close();
1375        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1376        assertEquals(
1377          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1378          + "<itemSet>\r\n <?target value?>\r\n"
1379          + "</itemSet>\r\n",
1380          result
1381        );
1382        
1383    }
1384    
1385    
1386    public void testDontBreakLineInElementWithSimpleContent()
1387      throws IOException JavaDoc {
1388        
1389        Element items = new Element("itemSet");
1390        Element item1 = new Element("item1");
1391        items.appendChild(item1);
1392        item1.appendChild("content");
1393        Document doc = new Document(items);
1394        Serializer serializer = new Serializer(out);
1395        serializer.setIndent(4);
1396        serializer.write(doc);
1397        serializer.flush();
1398        out.close();
1399        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1400        assertEquals(
1401          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1402          + "<itemSet>\r\n <item1>content</item1>\r\n"
1403          + "</itemSet>\r\n",
1404          result
1405        );
1406        
1407    }
1408    
1409    
1410    public void testPrettyXMLWithSetOutputStream() throws IOException JavaDoc {
1411        
1412        Element items = new Element("itemSet");
1413        items.appendChild(new Element("item1"));
1414        items.appendChild(new Element("item2"));
1415        Document doc = new Document(items);
1416        Serializer serializer = new Serializer(new ByteArrayOutputStream JavaDoc());
1417        serializer.setIndent(4);
1418        serializer.setLineSeparator("\n");
1419        serializer.write(doc);
1420        serializer.setOutputStream(out);
1421        serializer.write(doc);
1422        serializer.flush();
1423        out.close();
1424        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1425        assertEquals(
1426          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1427          + "<itemSet>\n <item1/>\n <item2/>\n"
1428          + "</itemSet>\n",
1429          result
1430        );
1431        
1432    }
1433    
1434    
1435    public void testAmpersandAndLessThanInText() throws IOException JavaDoc {
1436        
1437        root.appendChild("data<data&data");
1438        Serializer serializer = new Serializer(out);
1439        serializer.write(doc);
1440        serializer.flush();
1441        out.close();
1442        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1443        assertEquals(
1444          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1445          + "<root>data&lt;data&amp;data"
1446          + "</root>\r\n",
1447          result
1448        );
1449        
1450    }
1451
1452    
1453    public void testAmpersandAndAngleBracketsInAttributeValue()
1454      throws IOException JavaDoc {
1455        
1456        root.addAttribute(new Attribute("b", "data<data>data&"));
1457        Serializer serializer = new Serializer(out);
1458        serializer.write(doc);
1459        serializer.flush();
1460        out.close();
1461        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1462        assertEquals(
1463          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1464          + "<root b=\"data&lt;data&gt;data&amp;\"/>\r\n",
1465          result
1466        );
1467        
1468    }
1469    
1470    
1471    public void testSetNFC() {
1472        
1473        Serializer serializer = new Serializer(System.out);
1474        assertFalse(serializer.getUnicodeNormalizationFormC());
1475        serializer.setUnicodeNormalizationFormC(true);
1476        assertTrue(serializer.getUnicodeNormalizationFormC());
1477        
1478    }
1479
1480    
1481    public void testNFCInElementContent() throws IOException JavaDoc {
1482        
1483        Element root = new Element("a");
1484        root.appendChild("c\u0327"); // c with combining cedilla
1485
Document doc = new Document(root);
1486        Serializer serializer = new Serializer(out);
1487        serializer.setUnicodeNormalizationFormC(true);
1488        serializer.write(doc);
1489        serializer.flush();
1490        out.close();
1491        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1492        assertEquals(
1493          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1494          + "<a>\u00E7</a>\r\n",
1495          result
1496        );
1497        
1498    }
1499
1500    
1501    public void testNoNFCByDefault() throws IOException JavaDoc {
1502        
1503        Element root = new Element("c\u0327");
1504        root.appendChild("c\u0327"); // c with combining cedilla
1505
Document doc = new Document(root);
1506        Serializer serializer = new Serializer(out);
1507        serializer.write(doc);
1508        serializer.flush();
1509        out.close();
1510        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1511        assertEquals(
1512          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1513          + "<c\u0327>c\u0327</c\u0327>\r\n",
1514          result
1515        );
1516        
1517    }
1518
1519
1520    public void testNFCInAttribute() throws IOException JavaDoc {
1521        
1522        root.addAttribute(new Attribute("c\u0327", "c\u0327")); // c with combining cedilla
1523
Serializer serializer = new Serializer(out);
1524        serializer.setUnicodeNormalizationFormC(true);
1525        serializer.write(doc);
1526        serializer.flush();
1527        out.close();
1528        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1529        assertEquals(
1530          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1531          + "<root \u00E7=\"\u00E7\"/>\r\n",
1532          result
1533        );
1534        
1535    }
1536
1537    
1538    public void testNFCInElementName() throws IOException JavaDoc {
1539        
1540        Element root = new Element("c\u0327"); // c with combining cedilla
1541
Document doc = new Document(root);
1542        Serializer serializer = new Serializer(out);
1543        serializer.setUnicodeNormalizationFormC(true);
1544        serializer.write(doc);
1545        serializer.flush();
1546        out.close();
1547        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1548        assertEquals(
1549          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1550          + "<\u00E7/>\r\n",
1551          result
1552        );
1553        
1554    }
1555
1556    
1557    public void testNFCInComment() throws IOException JavaDoc {
1558        
1559        Element root = new Element("a");
1560        Document doc = new Document(root);
1561        doc.insertChild(new Comment("c\u0327hat"), 0); // c with combining cedilla
1562
Serializer serializer = new Serializer(out);
1563        serializer.setUnicodeNormalizationFormC(true);
1564        serializer.write(doc);
1565        serializer.flush();
1566        out.close();
1567        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1568        assertEquals(
1569          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1570          + "<!--\u00E7hat-->\r\n"
1571          + "<a/>\r\n",
1572          result
1573        );
1574        
1575    }
1576
1577    
1578    public void testNFCInProcessingInstruction() throws IOException JavaDoc {
1579        
1580        doc.appendChild(new ProcessingInstruction("c\u0327hat", "c\u0327hat"));
1581        Serializer serializer = new Serializer(out);
1582        serializer.setUnicodeNormalizationFormC(true);
1583        serializer.write(doc);
1584        serializer.flush();
1585        out.close();
1586        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1587        assertEquals(
1588          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1589          + "<root/>\r\n"
1590          + "<?\u00E7hat \u00E7hat?>\r\n",
1591          result
1592        );
1593        
1594    }
1595
1596    
1597    public void testNFCInElementContentWithNonUnicodeEncoding()
1598      throws IOException JavaDoc {
1599        
1600        root.appendChild("c\u0327"); // c with combining cedilla
1601
Serializer serializer = new Serializer(out, "ISO-8859-5");
1602        serializer.setUnicodeNormalizationFormC(true);
1603        serializer.write(doc);
1604        serializer.flush();
1605        out.close();
1606        String JavaDoc result = new String JavaDoc(out.toByteArray(), "ISO-8859-5");
1607        assertEquals(
1608          "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
1609          + "<root>&#xE7;</root>\r\n",
1610          result
1611        );
1612        
1613    }
1614
1615    
1616    public void testNFCWithSetOutputStream()
1617      throws IOException JavaDoc {
1618        
1619        root.appendChild("c\u0327"); // c with combining cedilla
1620
Serializer serializer = new Serializer(new ByteArrayOutputStream JavaDoc(), "ISO-8859-5");
1621        serializer.setUnicodeNormalizationFormC(true);
1622        serializer.write(doc);
1623        serializer.setOutputStream(out);
1624        serializer.write(doc);
1625        serializer.flush();
1626        out.close();
1627        String JavaDoc result = new String JavaDoc(out.toByteArray(), "ISO-8859-5");
1628        assertEquals(
1629          "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
1630          + "<root>&#xE7;</root>\r\n",
1631          result
1632        );
1633        
1634    }
1635    
1636    
1637    public void testNullOutputStream() {
1638        
1639        try {
1640            new Serializer(null);
1641            fail("Allowed null output stream");
1642        }
1643        catch (NullPointerException JavaDoc success) {
1644            assertNotNull(success.getMessage());
1645        }
1646        
1647    }
1648
1649    
1650    public void testNullOutputStreamWithEncoding()
1651      throws UnsupportedEncodingException JavaDoc {
1652        
1653        try {
1654            new Serializer(null, "UTF-8");
1655            fail("Allowed null output stream");
1656        }
1657        catch (NullPointerException JavaDoc success) {
1658            assertNotNull(success.getMessage());
1659        }
1660        
1661    }
1662
1663    
1664    public void testNullEncoding()
1665      throws UnsupportedEncodingException JavaDoc {
1666        
1667        try {
1668            new Serializer(System.out, null);
1669            fail("Allowed null encoding");
1670        }
1671        catch (NullPointerException JavaDoc success) {
1672            assertNotNull(success.getMessage());
1673        }
1674        
1675    }
1676
1677    
1678    // make sure null pointer exception doesn't cause any output
1679
public void testNullDocument() throws IOException JavaDoc {
1680        
1681        Serializer serializer = new Serializer(out, "UTF-16");
1682        try {
1683            serializer.write(null);
1684            fail("Wrote null document");
1685        }
1686        catch (NullPointerException JavaDoc success) {
1687            // success
1688
}
1689        byte[] result = out.toByteArray();
1690        assertEquals(0, result.length);
1691        
1692    }
1693
1694    
1695    public void testGetEncoding()
1696      throws UnsupportedEncodingException JavaDoc {
1697        
1698        Serializer serializer = new Serializer(System.out, "ISO-8859-1");
1699        assertEquals("ISO-8859-1", serializer.getEncoding());
1700        
1701    }
1702
1703    
1704    public void testGetPreserveBaseURI()
1705      throws UnsupportedEncodingException JavaDoc {
1706        
1707        Serializer serializer = new Serializer(System.out, "ISO-8859-1");
1708        assertFalse(serializer.getPreserveBaseURI());
1709        serializer.setPreserveBaseURI(true);
1710        assertTrue(serializer.getPreserveBaseURI());
1711        serializer.setPreserveBaseURI(false);
1712        assertFalse(serializer.getPreserveBaseURI());
1713        
1714    }
1715
1716
1717    public void testSerializeDocTypeWithSystemID()
1718      throws IOException JavaDoc {
1719        
1720        Serializer serializer = new Serializer(out);
1721        Document doc = new Document(new Element("a"));
1722        doc.setDocType(new DocType("b", "example.dtd"));
1723        serializer.write(doc);
1724        String JavaDoc result = out.toString("UTF-8");
1725        assertTrue(result.endsWith("<a/>\r\n"));
1726        assertTrue(result.indexOf("<!DOCTYPE b SYSTEM \"example.dtd\">") > 0);
1727        
1728    }
1729
1730    
1731    public void testSerializeDocTypeWithPublicAndSystemID()
1732      throws IOException JavaDoc {
1733        
1734        Serializer serializer = new Serializer(out);
1735        Document doc = new Document(new Element("a"));
1736        doc.setDocType(new DocType("b",
1737          "-//W3C//DTD XHTML 1.0 Transitional//EN", "example.dtd"));
1738        serializer.write(doc);
1739        String JavaDoc result = out.toString("UTF-8");
1740        assertTrue(result.endsWith("<a/>\r\n"));
1741        assertTrue(result.indexOf(
1742          "<!DOCTYPE b PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"example.dtd\">") > 0);
1743        
1744    }
1745
1746    
1747    public void testSerializeDocTypeWithInternalDTDSubset()
1748      throws ParsingException, IOException JavaDoc {
1749        
1750        Serializer serializer = new Serializer(out);
1751        String JavaDoc data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> ]><test/>";
1752        Builder builder = new Builder();
1753        Document doc = builder.build(data, "http://www.example.com");
1754        serializer.write(doc);
1755        String JavaDoc result = out.toString("UTF-8");
1756        assertTrue(result.endsWith("<test/>\r\n"));
1757        assertTrue(result.indexOf("<!DOCTYPE root [\r\n") > 0);
1758        assertTrue(result.indexOf("\r\n]>\r\n") > 0);
1759        assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r\n") > 0);
1760        
1761    }
1762
1763    
1764    public void testSerializeInternalDTDSubsetContainingUnavailableCharacter()
1765      throws ParsingException, IOException JavaDoc {
1766        
1767        Serializer serializer = new Serializer(out, "US-ASCII");
1768        String JavaDoc data = "<!DOCTYPE root ["
1769          + "<!ELEMENT root EMPTY> "
1770          + "<!ATTLIST root attr CDATA 'café creme'> "
1771          + "]><test/>";
1772        Builder builder = new Builder();
1773        Document doc = builder.build(data, "http://www.example.com");
1774        try {
1775            serializer.write(doc);
1776            fail("How'd you serialize é in ASCII?");
1777        }
1778        catch (UnavailableCharacterException success) {
1779            assertTrue(success.getMessage().indexOf("é (&#xE9;)") > 1);
1780        }
1781        
1782    }
1783
1784    
1785    public void testLineBreaksInInternalDTDSubset()
1786      throws ParsingException, IOException JavaDoc {
1787        
1788        Serializer serializer = new Serializer(out);
1789        serializer.setLineSeparator("\r");
1790        String JavaDoc data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> <!ELEMENT data EMPTY> ]><test/>";
1791        Builder builder = new Builder();
1792        Document doc = builder.build(data, "http://www.example.com");
1793        serializer.write(doc);
1794        String JavaDoc result = out.toString("UTF-8");
1795        assertTrue(result.endsWith("<test/>\r"));
1796        assertTrue(result.indexOf("<!DOCTYPE root [\r") > 0);
1797        assertTrue(result.indexOf("\r]>\r") > 0);
1798        assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r") > 0);
1799        assertTrue(result.indexOf("<!ELEMENT data EMPTY>\r") > 0);
1800        assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\n"));
1801        assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\n"));
1802        assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\r"));
1803        assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\r"));
1804        assertEquals(-1, result.indexOf('\n'));
1805        
1806    }
1807
1808    
1809    public void testSerializeQuoteInAttributeValue()
1810      throws IOException JavaDoc {
1811        
1812        Serializer serializer = new Serializer(out);
1813        root.addAttribute(new Attribute("name", "\""));
1814        serializer.write(doc);
1815        String JavaDoc result = out.toString("UTF-8");
1816        assertTrue(result.endsWith("<root name=\"&quot;\"/>\r\n"));
1817        
1818    }
1819
1820    
1821    public void testSerializeUnavailableCharacterInMarkup()
1822      throws IOException JavaDoc {
1823        
1824        Serializer serializer = new Serializer(out, "ISO-8859-1");
1825        Element root = new Element("\u0419");
1826        Document doc = new Document(root);
1827        try {
1828            serializer.write(doc);
1829            fail("Wrote bad character: " + out.toString("ISO-8859-1"));
1830        }
1831        catch (UnavailableCharacterException success) {
1832            assertNotNull(success.getMessage());
1833            assertEquals('\u0419', success.getCharacter());
1834            assertEquals("ISO-8859-1", success.getEncoding());
1835        }
1836        
1837    }
1838
1839    
1840    public void testTurnLineFeedInAttributeValueIntoSpaceWhenIndenting()
1841      throws IOException JavaDoc {
1842        
1843        Element root = new Element("a");
1844        root.appendChild("c");
1845        root.addAttribute(new Attribute("name", "value1\nvalue2"));
1846        Document doc = new Document(root);
1847        Serializer serializer = new Serializer(out);
1848        serializer.setMaxLength(245);
1849        serializer.setIndent(4);
1850        serializer.write(doc);
1851        serializer.flush();
1852        out.close();
1853        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1854        assertEquals(
1855          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1856          + "<a name=\"value1 value2\">c</a>\r\n",
1857          result
1858        );
1859        
1860    }
1861
1862    
1863    public void testConflictBetweenMaxLengthAndIndent()
1864      throws IOException JavaDoc {
1865        
1866        Element root = new Element("a");
1867        Element b = new Element("b");
1868        Element c = new Element("c");
1869        Element d = new Element("d");
1870        b.appendChild(c);
1871        root.appendChild(b);
1872        c.appendChild(d);
1873        d.appendChild("data");
1874        Document doc = new Document(root);
1875        Serializer serializer = new Serializer(out);
1876        serializer.setMaxLength(16);
1877        serializer.setIndent(4);
1878        serializer.write(doc);
1879        serializer.flush();
1880        out.close();
1881        String JavaDoc result = new String JavaDoc(out.toByteArray(), "UTF-8");
1882        assertEquals(
1883          "<?xml version=\"1.0\"\r\nencoding=\"UTF-8\"?>\r\n"
1884          + "<a>\r\n <b>\r\n <c>\r\n <d>data</d>\r\n </c>\r\n </b>\r\n</a>\r\n",
1885          result
1886        );
1887        
1888    }
1889
1890    
1891    public void testWriteChild() throws IOException JavaDoc {
1892                  
1893        ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
1894        try {
1895            serializer.writeChild(doc);
1896            fail("writeChild wrote a document");
1897        }
1898        catch (XMLException success) {
1899            assertNotNull(success.getMessage());
1900        }
1901        try {
1902            serializer.writeChild(new Attribute("name", "value"));
1903            fail("writeChild wrote an attribute");
1904        }
1905        catch (XMLException success) {
1906            assertNotNull(success.getMessage());
1907        }
1908       
1909    }
1910    
1911    
1912    // just so we can test protected methods
1913
private static class ExposingSerializer extends Serializer {
1914        
1915        ExposingSerializer(OutputStream JavaDoc out, String JavaDoc encoding)
1916          throws UnsupportedEncodingException JavaDoc {
1917            super(out, encoding);
1918        }
1919        
1920        public void writeChild(Node node) throws IOException JavaDoc {
1921            super.writeChild(node);
1922        }
1923        
1924        public void exposedWriteRaw(String JavaDoc text) throws IOException JavaDoc {
1925            writeRaw(text);
1926        }
1927        
1928        public void exposedWriteEscaped(String JavaDoc text) throws IOException JavaDoc {
1929            writeEscaped(text);
1930        }
1931        
1932        public void exposedWriteAttributeValue(String JavaDoc text) throws IOException JavaDoc {
1933            writeAttributeValue(text);
1934        }
1935        
1936        public int exposeGetColumnNumber() {
1937            return super.getColumnNumber();
1938        }
1939        
1940    }
1941
1942    
1943    public void testWriteRaw() throws IOException JavaDoc {
1944
1945        ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
1946        serializer.exposedWriteRaw("<>&\"'");
1947        assertEquals(5, serializer.exposeGetColumnNumber());
1948        serializer.flush();
1949        String JavaDoc result = out.toString("UTF-8");
1950        assertEquals("<>&\"'", result);
1951        
1952    }
1953
1954    
1955    public void testWriteEscaped() throws IOException JavaDoc {
1956
1957        ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
1958        serializer.exposedWriteEscaped("<>&\"'");
1959        assertEquals(15, serializer.exposeGetColumnNumber());
1960        serializer.flush();
1961        byte[] data = out.toByteArray();
1962        String JavaDoc result = new String JavaDoc(data, "UTF-8");
1963        assertEquals("&lt;&gt;&amp;\"'", result);
1964        
1965    }
1966
1967    
1968    public void testWriteAttributeValue() throws IOException JavaDoc {
1969
1970        ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
1971        serializer.exposedWriteAttributeValue("<>&\"'");
1972        assertEquals(20, serializer.exposeGetColumnNumber());
1973        serializer.flush();
1974        String JavaDoc result = out.toString("UTF-8");
1975        assertEquals("&lt;&gt;&amp;&quot;'", result);
1976        
1977    }
1978    
1979    
1980    // This test case reproduces a bug that
1981
// showed up while working on SAX conformance testing.
1982
public void testElementsThatOnlyContainsAnEmptyTextNodeShouldBeOutputWithAnEmptyElementTag()
1983      throws IOException JavaDoc {
1984        
1985        Element child = new Element("b");
1986        child.appendChild("");
1987        root.appendChild(child);
1988        
1989        Serializer serializer = new Serializer(out);
1990        serializer.write(doc);
1991           
1992        serializer.flush();
1993        String JavaDoc result = out.toString("UTF-8");
1994        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
1995          "<root><b/></root>\r\n", result);
1996        
1997    }
1998    
1999    
2000    // This test case reproduces a bug that
2001
// showed up while working on SAX conformance testing.
2002
public void testElementsThatOnlyContainEmptyTextNodesShouldBeOutputWithAnEmptyElementTag()
2003      throws IOException JavaDoc {
2004        
2005        Element root = new Element("a");
2006        Element child = new Element("b");
2007        child.appendChild("");
2008        child.appendChild("");
2009        child.appendChild("");
2010        root.appendChild(child);
2011        
2012        Document doc = new Document(root);
2013        
2014        Serializer serializer = new Serializer(out);
2015        serializer.write(doc);
2016           
2017        serializer.flush();
2018        String JavaDoc result = out.toString("UTF-8");
2019        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2020          "<a><b/></a>\r\n", result);
2021        
2022    }
2023
2024    
2025    // This test case reproduces a bug that
2026
// showed up while working on SAX conformance testing.
2027
public void testElementsThatOnlyContainASingleSpaceShouldNotBeSplitWhileIndenting()
2028      throws IOException JavaDoc {
2029        
2030        Element root = new Element("a");
2031        Element child = new Element("b");
2032        child.appendChild(" ");
2033        root.appendChild(child);
2034        
2035        Document doc = new Document(root);
2036        
2037        Serializer serializer = new Serializer(out);
2038        serializer.setIndent(4);
2039        serializer.write(doc);
2040           
2041        serializer.flush();
2042        String JavaDoc result = out.toString("UTF-8");
2043        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2044          "<a>\r\n <b> </b>\r\n</a>\r\n", result);
2045
2046    }
2047    
2048    
2049    public void testElementsThatOnlyContainTextNodesWithBoundaryWhiteSpaceShouldNotBeSplitWhileIndenting()
2050      throws IOException JavaDoc {
2051        
2052        Element root = new Element("a");
2053        Element child = new Element("b");
2054        child.appendChild(" ");
2055        child.appendChild(" ");
2056        root.appendChild(child);
2057        
2058        Document doc = new Document(root);
2059        
2060        Serializer serializer = new Serializer(out);
2061        serializer.setIndent(4);
2062        serializer.write(doc);
2063           
2064        serializer.flush();
2065        String JavaDoc result = out.toString("UTF-8");
2066        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2067          "<a>\r\n <b> </b>\r\n</a>\r\n", result);
2068
2069    }
2070    
2071    
2072    public void testElementsThatOnlyContainASingleLinefeedShouldNotBeSplitWhileIndenting()
2073      throws IOException JavaDoc {
2074        
2075        Element root = new Element("a");
2076        Element child = new Element("b");
2077        child.appendChild("\n");
2078        root.appendChild(child);
2079        
2080        Document doc = new Document(root);
2081        
2082        Serializer serializer = new Serializer(out);
2083        serializer.setIndent(4);
2084        serializer.write(doc);
2085           
2086        serializer.flush();
2087        String JavaDoc result = out.toString("UTF-8");
2088        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2089          "<a>\r\n <b> </b>\r\n</a>\r\n", result);
2090
2091    }
2092    
2093    
2094    public void testEndTagsOfElementsWithContentGoOnSeparateLine()
2095      throws ParsingException, IOException JavaDoc {
2096      
2097        Serializer serializer = new Serializer(out, "ISO-8859-1");
2098        serializer.setIndent(4);
2099        serializer.setPreserveBaseURI(true);
2100        serializer.flush();
2101    
2102        File JavaDoc f = new File JavaDoc("data");
2103        f = new File JavaDoc(f, "prettyxml.xml");
2104        Builder builder = new Builder();
2105        Document doc = builder.build(f);
2106        serializer.write(doc);
2107        String JavaDoc result = out.toString("UTF-8");
2108        assertTrue(result.endsWith("\r\n</html>\r\n"));
2109      
2110    }
2111
2112    
2113    public void testDontDoubleBreak()
2114      throws ParsingException, IOException JavaDoc {
2115      
2116        Serializer serializer = new Serializer(out, "ISO-8859-1");
2117        serializer.setIndent(4);
2118        serializer.setMaxLength(64);
2119
2120        File JavaDoc f = new File JavaDoc("data");
2121        f = new File JavaDoc(f, "prettytest.xml");
2122        Builder builder = new Builder();
2123        Document doc = builder.build(f);
2124        serializer.write(doc);
2125        String JavaDoc result = out.toString("UTF-8");
2126        assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n" +
2127          "<html a=\"AReallyLongNameWithNoOpportunitiesToBreakToPutUsPastTheMaxLineLengthAndForceABreak\">\r\n" +
2128          " <head> </head>\r\n" +
2129          "</html>\r\n", result);
2130      
2131    }
2132
2133    
2134}
2135
Popular Tags