Source Code Cross Referenced for SerializerTest.java in  » XML » xom » nu » xom » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xom » nu.xom.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* Copyright 2002-2005 Elliotte Rusty Harold
0002:           
0003:           This library is free software; you can redistribute it and/or modify
0004:           it under the terms of version 2.1 of the GNU Lesser General Public 
0005:           License as published by the Free Software Foundation.
0006:           
0007:           This library is distributed in the hope that it will be useful,
0008:           but WITHOUT ANY WARRANTY; without even the implied warranty of
0009:           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
0010:           GNU Lesser General Public License for more details.
0011:           
0012:           You should have received a copy of the GNU Lesser General Public
0013:           License along with this library; if not, write to the 
0014:           Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
0015:           Boston, MA 02111-1307  USA
0016:           
0017:           You can contact Elliotte Rusty Harold by sending e-mail to
0018:           elharo@metalab.unc.edu. Please include the word "XOM" in the
0019:           subject line. The XOM home page is located at http://www.xom.nu/
0020:         */
0021:
0022:        package nu.xom.tests;
0023:
0024:        import nu.xom.Elements;
0025:        import nu.xom.Namespace;
0026:        import nu.xom.Node;
0027:        import nu.xom.Serializer;
0028:        import nu.xom.Element;
0029:        import nu.xom.DocType;
0030:        import nu.xom.Document;
0031:        import nu.xom.Builder;
0032:        import nu.xom.Comment;
0033:        import nu.xom.ParsingException;
0034:        import nu.xom.ProcessingInstruction;
0035:        import nu.xom.Attribute;
0036:        import nu.xom.Text;
0037:        import nu.xom.UnavailableCharacterException;
0038:        import nu.xom.ValidityException;
0039:        import nu.xom.XMLException;
0040:
0041:        import java.io.ByteArrayInputStream;
0042:        import java.io.ByteArrayOutputStream;
0043:        import java.io.File;
0044:        import java.io.IOException;
0045:        import java.io.BufferedReader;
0046:        import java.io.InputStream;
0047:        import java.io.OutputStream;
0048:        import java.io.StringReader;
0049:        import java.io.UnsupportedEncodingException;
0050:
0051:        /**
0052:         * <p>
0053:         * Tests for <code>Serializer</code> functionality.
0054:         * </p>
0055:         * 
0056:         * @author Elliotte Rusty Harold
0057:         * @version 1.1b7
0058:         *
0059:         */
0060:        public class SerializerTest extends XOMTestCase {
0061:
0062:            private Builder parser;
0063:            private final static double version = Double.parseDouble(System
0064:                    .getProperty("java.version").substring(0, 3));
0065:            Element root = new Element("root");
0066:            Document doc = new Document(root);
0067:            ByteArrayOutputStream out = new ByteArrayOutputStream();
0068:
0069:            public SerializerTest(String name) {
0070:                super (name);
0071:            }
0072:
0073:            protected void setUp() {
0074:                parser = new Builder();
0075:            }
0076:
0077:            public void testNFC88592() throws IOException, ParsingException {
0078:
0079:                String data = " Ą˘Å?¤ĽŚ§¨ŠŞŤŹ­ŽŻ°ą˛ł´ľśˇ¸šşťźË?žżŔÃ?ÂĂÄĹĆÇČÉĘËĚÃ?ÃŽÄŽÄ?ŃŇÓÔÅ?Ö×ŘŮÚŰÜÃ?ŢßŕáâăäĺćçÄ?éęëěíîÄ?đńňóôőö÷řůúűüýţ˙";
0080:                Element root = new Element("a");
0081:                root.appendChild(data);
0082:                Document doc = new Document(root);
0083:
0084:                // try with 8859-2 encoding on serializer
0085:                Serializer serializer = new Serializer(out);
0086:                serializer.setUnicodeNormalizationFormC(true);
0087:                serializer.write(doc);
0088:                serializer.flush();
0089:                out.close();
0090:                byte[] temp = out.toByteArray();
0091:                Document roundTrip = parser
0092:                        .build(new ByteArrayInputStream(temp));
0093:
0094:                assertEquals(data, roundTrip.getValue());
0095:
0096:            }
0097:
0098:            public void testMultipleCombiningCharactersWithNFC()
0099:                    throws ParsingException, IOException {
0100:
0101:                // LATIN CAPITAL LETTER D WITH DOT BELOW, COMBINING DOT ABOVE
0102:                String input = "<a>&#x1E0A;&#x0323;</a>";
0103:                // LATIN CAPITAL LETTER D WITH DOT ABOVE, COMBINING DOT BELOW
0104:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
0105:                        + "<a>&#x1E0C;&#x307;</a>\r\n";
0106:
0107:                Document doc = parser.build(input, null);
0108:                Serializer serializer = new Serializer(out, "US-ASCII");
0109:                serializer.setUnicodeNormalizationFormC(true);
0110:                serializer.write(doc);
0111:                serializer.flush();
0112:                String result = out.toString("US-ASCII");
0113:                assertEquals(output, result);
0114:
0115:            }
0116:
0117:            public void testMultipleCombiningCharactersWithDifferentCombiningClassesNFC()
0118:                    throws ParsingException, IOException {
0119:
0120:                // LATIN SMALL LETTER A, COMBINING GRAVE ACCENT, HEBREW ACCENT ZINOR, 
0121:                // MUSICAL SYMBOL COMBINING AUGMENTATION DOT, HEBREW ACCENT ZINOR, 
0122:                // LATIN SMALL LETTER B
0123:                String input = "<a>&#x0061;&#x0300;&#x05AE;&#x1D16D;&#x05AE;&#x0062;</a>";
0124:                // There was a bug where 1D16D was not listed as a combining character
0125:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
0126:                        + "<a>&#xE0;&#x1D16D;&#x5AE;&#x5AE;b</a>\r\n";
0127:
0128:                Document doc = parser.build(input, null);
0129:                Serializer serializer = new Serializer(out, "US-ASCII");
0130:                serializer.setUnicodeNormalizationFormC(true);
0131:                serializer.write(doc);
0132:                serializer.flush();
0133:                String result = out.toString("US-ASCII");
0134:                assertEquals(output, result);
0135:
0136:            }
0137:
0138:            public void testEWithCombiningMacron() throws ParsingException,
0139:                    IOException {
0140:
0141:                // LATIN CAPITAL LETTER E WITH COMBINING MACRON 
0142:                String input = "<a>&#x45;&#x0304;</a>";
0143:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>&#x112;</a>\r\n";
0144:
0145:                Document doc = parser.build(input, null);
0146:                Serializer serializer = new Serializer(out, "US-ASCII");
0147:                serializer.setUnicodeNormalizationFormC(true);
0148:                serializer.write(doc);
0149:                serializer.flush();
0150:                String result = out.toString("US-ASCII");
0151:                assertEquals(output, result);
0152:
0153:            }
0154:
0155:            public void testEWithCombiningMacronAndGrave()
0156:                    throws ParsingException, IOException {
0157:
0158:                // LATIN CAPITAL LETTER E WITH COMBINING MACRON and grave
0159:                String input = "<a>&#x0045;&#x0304;&#x0300;</a>";
0160:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>&#x1E14;</a>\r\n";
0161:
0162:                Document doc = parser.build(input, null);
0163:                Serializer serializer = new Serializer(out, "US-ASCII");
0164:                serializer.setUnicodeNormalizationFormC(true);
0165:                serializer.write(doc);
0166:                serializer.flush();
0167:                String result = out.toString("US-ASCII");
0168:                assertEquals(output, result);
0169:
0170:            }
0171:
0172:            public void testParenthesizedOjeon() throws ParsingException,
0173:                    IOException {
0174:
0175:                // (Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant)
0176:                String input = "<a>&#x0028;&#x110B;&#x1169;&#x110C;&#x1165;&#x11AB;&#x0029;</a>";
0177:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>(&#xC624;&#xC804;)</a>\r\n";
0178:
0179:                Document doc = parser.build(input, null);
0180:                Serializer serializer = new Serializer(out, "US-ASCII");
0181:                serializer.setUnicodeNormalizationFormC(true);
0182:                serializer.write(doc);
0183:                serializer.flush();
0184:                String result = out.toString("US-ASCII");
0185:                assertEquals(output, result);
0186:
0187:            }
0188:
0189:            public void testNonParenthesizedOjeon() throws ParsingException,
0190:                    IOException {
0191:
0192:                // Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant
0193:                String input = "<a>&#x110B;&#x1169;&#x110C;&#x1165;&#x11AB;</a>";
0194:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>&#xC624;&#xC804;</a>\r\n";
0195:
0196:                Document doc = parser.build(input, null);
0197:                Serializer serializer = new Serializer(out, "US-ASCII");
0198:                serializer.setUnicodeNormalizationFormC(true);
0199:                serializer.write(doc);
0200:                serializer.flush();
0201:                String result = out.toString("US-ASCII");
0202:                assertEquals(output, result);
0203:
0204:            }
0205:
0206:            public void testOjeon() throws ParsingException, IOException {
0207:
0208:                // (Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant)
0209:                String input = "<a>&#x110C;&#x1165;&#x11AB;</a>";
0210:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>&#xC804;</a>\r\n";
0211:
0212:                Document doc = parser.build(input, null);
0213:                Serializer serializer = new Serializer(out, "US-ASCII");
0214:                serializer.setUnicodeNormalizationFormC(true);
0215:                serializer.write(doc);
0216:                serializer.flush();
0217:                String result = out.toString("US-ASCII");
0218:                assertEquals(output, result);
0219:
0220:            }
0221:
0222:            public void testKannadaVowelSignOO() throws ParsingException,
0223:                    IOException {
0224:
0225:                String input = "<a>&#x0CC6;&#x0CC2;&#x0CD5;</a>";
0226:                String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>&#xCCB;</a>\r\n";
0227:
0228:                Document doc = parser.build(input, null);
0229:                Serializer serializer = new Serializer(out, "US-ASCII");
0230:                serializer.setUnicodeNormalizationFormC(true);
0231:                serializer.write(doc);
0232:                serializer.flush();
0233:                String result = out.toString("US-ASCII");
0234:                assertEquals(output, result);
0235:
0236:            }
0237:
0238:            public void testCDATASectionEndDelimiter() throws IOException {
0239:
0240:                root.appendChild("]]>");
0241:                Serializer serializer = new Serializer(out, "UTF-8");
0242:                serializer.setMaxLength(20);
0243:                serializer.write(doc);
0244:                String result = out.toString("UTF-8");
0245:                assertTrue(result.indexOf("]]&gt;") > 0);
0246:
0247:            }
0248:
0249:            public void testXMLSpacePreserve() throws IOException {
0250:
0251:                root.addAttribute(new Attribute("xml:space",
0252:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0253:                String value = "This is a long sentence with plenty of opportunities for "
0254:                        + "breaking from beginning to end.";
0255:                root.appendChild(value);
0256:                Serializer serializer = new Serializer(out, "UTF-8");
0257:                serializer.setMaxLength(20);
0258:                serializer.write(doc);
0259:                String result = out.toString("UTF-8");
0260:                assertTrue(result.indexOf(value) > 0);
0261:
0262:            }
0263:
0264:            /**
0265:             * <p>
0266:             *   Check that the UTF-16LE encoding omits the byte-order mark.
0267:             * </p>
0268:             * 
0269:             * @throws IOException
0270:             */
0271:            public void testUTF16LEBOM() throws IOException {
0272:
0273:                if (version >= 1.3) {
0274:                    // UTF-16LE only supported in 1.3 and later
0275:                    Serializer serializer = new Serializer(out, "UTF-16LE");
0276:                    serializer.write(doc);
0277:                    serializer.flush();
0278:                    out.flush();
0279:                    out.close();
0280:                    byte[] data = out.toByteArray();
0281:                    assertEquals('<', (char) data[0]);
0282:                    assertEquals((byte) 0, data[1]);
0283:                }
0284:
0285:            }
0286:
0287:            /**
0288:             * <p>
0289:             *   Check that the UTF-16 encoding outputs a byte-order mark.
0290:             * </p>
0291:             * 
0292:             * @throws IOException
0293:             */
0294:            public void testUTF16BOM() throws IOException {
0295:
0296:                Serializer serializer = new Serializer(out, "UTF-16");
0297:                serializer.write(doc);
0298:                serializer.flush();
0299:                out.flush();
0300:                out.close();
0301:                byte[] data = out.toByteArray();
0302:                assertEquals((byte) 0xFE, data[0]);
0303:                assertEquals((byte) 0xFF, data[1]);
0304:                assertEquals((byte) 0, data[2]);
0305:                assertEquals('<', (char) data[3]);
0306:
0307:            }
0308:
0309:            /**
0310:             * <p>
0311:             *   Check that the UTF-16BE encoding omits the byte-order mark.
0312:             * </p>
0313:             * 
0314:             * @throws IOException
0315:             */
0316:            public void testUTF16BEBOM() throws IOException {
0317:
0318:                if (version >= 1.3) {
0319:                    // UTF-16LE only supported in 1.3 and later
0320:                    Serializer serializer = new Serializer(out, "UTF-16BE");
0321:                    serializer.write(doc);
0322:                    serializer.flush();
0323:                    out.flush();
0324:                    out.close();
0325:                    byte[] data = out.toByteArray();
0326:                    assertEquals((byte) 0, data[0]);
0327:                    assertEquals('<', (char) data[1]);
0328:                }
0329:
0330:            }
0331:
0332:            public void testXMLSpaceDefault() throws IOException {
0333:
0334:                root.addAttribute(new Attribute("xml:space",
0335:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0336:                Element child1 = new Element("preserve");
0337:                String value = "This is a long sentence with plenty of opportunities for "
0338:                        + "breaking from beginning to end.";
0339:                child1.appendChild(value);
0340:                Element child2 = new Element("default");
0341:                root.appendChild(child1);
0342:                root.appendChild(child2);
0343:                child2.addAttribute(new Attribute("xml:space",
0344:                        "http://www.w3.org/XML/1998/namespace", "default"));
0345:                String value2 = "This is another very long sentence with plenty"
0346:                        + " of opportunities for breaking from beginning to end.";
0347:                child2.appendChild(value2);
0348:
0349:                String value3 = "This is still another very long sentence with plenty of "
0350:                        + "opportunities for breaking from beginning to end.";
0351:                Element preserveAgain = new Element("test");
0352:                preserveAgain.appendChild(value3);
0353:                child2.appendChild(preserveAgain);
0354:                preserveAgain.addAttribute(new Attribute("xml:space",
0355:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0356:
0357:                Serializer serializer = new Serializer(out, "UTF-8");
0358:                serializer.setMaxLength(20);
0359:                serializer.write(doc);
0360:                String result = out.toString("UTF-8");
0361:                assertTrue(result.indexOf(value) > 0);
0362:                assertTrue(result.indexOf(value3) > 0);
0363:                assertEquals(-1, result.indexOf(value2));
0364:
0365:            }
0366:
0367:            public void testXMLSpacePreserveWithIndenting() throws IOException {
0368:
0369:                root.addAttribute(new Attribute("xml:space",
0370:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0371:                root.appendChild(new Element("sameline"));
0372:                ByteArrayOutputStream out = new ByteArrayOutputStream();
0373:                Serializer serializer = new Serializer(out, "UTF-8");
0374:                serializer.setIndent(4);
0375:                serializer.write(doc);
0376:                String result = out.toString("UTF-8");
0377:                assertEquals(
0378:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
0379:                                + "<root xml:space=\"preserve\"><sameline/></root>\r\n",
0380:                        result);
0381:
0382:            }
0383:
0384:            public void testXMLSpaceUnspecifiedValueWithIndenting()
0385:                    throws IOException {
0386:
0387:                root.addAttribute(new Attribute("xml:space",
0388:                        "http://www.w3.org/XML/1998/namespace", "undefined"));
0389:                root.appendChild(new Element("sameline"));
0390:                Serializer serializer = new Serializer(out, "UTF-8");
0391:                serializer.setIndent(4);
0392:                serializer.write(doc);
0393:                String result = out.toString("UTF-8");
0394:                assertEquals(
0395:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
0396:                                + "<root xml:space=\"undefined\">\r\n    <sameline/>\r\n</root>\r\n",
0397:                        result);
0398:
0399:            }
0400:
0401:            public void testXMLSpaceDefaultWithIndenting() throws IOException {
0402:
0403:                root.addAttribute(new Attribute("xml:space",
0404:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0405:                Element child = new Element("child");
0406:                child.addAttribute(new Attribute("xml:space",
0407:                        "http://www.w3.org/XML/1998/namespace", "default"));
0408:                root.appendChild(child);
0409:                Serializer serializer = new Serializer(out, "UTF-8");
0410:                serializer.setIndent(4);
0411:                serializer.write(doc);
0412:                String result = out.toString("UTF-8");
0413:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
0414:                        + "<root xml:space=\"preserve\">"
0415:                        + "<child xml:space=\"default\"/></root>\r\n", result);
0416:
0417:            }
0418:
0419:            public void testXMLSpaceDefaultWithIndentingAndGrandchildren()
0420:                    throws IOException {
0421:
0422:                root.addAttribute(new Attribute("xml:space",
0423:                        "http://www.w3.org/XML/1998/namespace", "preserve"));
0424:                Element child = new Element("child");
0425:                child.addAttribute(new Attribute("xml:space",
0426:                        "http://www.w3.org/XML/1998/namespace", "default"));
0427:                root.appendChild(child);
0428:                child.appendChild(new Element("differentLine"));
0429:                Serializer serializer = new Serializer(out, "UTF-8");
0430:                serializer.setIndent(2);
0431:                serializer.write(doc);
0432:                String result = out.toString("UTF-8");
0433:                assertEquals(
0434:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
0435:                                + "<root xml:space=\"preserve\">"
0436:                                + "<child xml:space=\"default\">\r\n    <differentLine/>\r\n"
0437:                                + "  </child></root>\r\n", result);
0438:
0439:            }
0440:
0441:            public void testDontSerializeXMLNamespace() throws IOException {
0442:
0443:                Element root = new Element("html",
0444:                        "http://www.w3.org/1999/xhtml");
0445:                root.addAttribute(new Attribute("xml:lang",
0446:                        "http://www.w3.org/XML/1998/namespace", "en"));
0447:                Document doc = new Document(root);
0448:                Serializer serializer = new Serializer(out, "UTF-8");
0449:                serializer.write(doc);
0450:                String result = out.toString("UTF-8");
0451:                assertEquals(-1, result.indexOf("xmlns:xml"));
0452:                assertTrue(result.indexOf("xml:lang=") > 1);
0453:
0454:            }
0455:
0456:            public void testDontSerializeNoNamespace() throws IOException {
0457:
0458:                Serializer serializer = new Serializer(out, "UTF-8");
0459:                serializer.write(doc);
0460:                String result = out.toString("UTF-8");
0461:                assertEquals(-1, result.indexOf("xmlns="));
0462:
0463:            }
0464:
0465:            public void testDefaultNamespace() throws IOException {
0466:
0467:                Element root = new Element("root", "http://www.example.com");
0468:                Document doc = new Document(root);
0469:                Serializer serializer = new Serializer(out, "UTF-8");
0470:                serializer.write(doc);
0471:                String result = out.toString("UTF-8");
0472:                assertTrue(result.indexOf("xmlns=") > 1);
0473:                assertTrue(result.indexOf("http://www.example.com") > 1);
0474:
0475:            }
0476:
0477:            public void testEmptyElement() throws IOException {
0478:
0479:                Serializer serializer = new Serializer(out, "UTF-8");
0480:                serializer.write(doc);
0481:                String result = out.toString("UTF-8");
0482:                assertEquals(
0483:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root/>\r\n",
0484:                        result);
0485:
0486:            }
0487:
0488:            public void testElementWithText() throws IOException {
0489:
0490:                String data = "   test   \n\n   \n  \n hello again";
0491:                root.appendChild(data);
0492:                Serializer serializer = new Serializer(out, "UTF-8");
0493:                serializer.write(doc);
0494:                String result = out.toString("UTF-8");
0495:
0496:                assertEquals(
0497:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
0498:                                + data + "</root>\r\n", result);
0499:
0500:            }
0501:
0502:            public void testStaticElementWithText() throws IOException {
0503:
0504:                String data = "   test   \n\n   \n  \n hello again";
0505:                root.appendChild(data);
0506:                Serializer serializer = new Serializer(out);
0507:                serializer.write(doc);
0508:                String result = out.toString("UTF-8");
0509:
0510:                assertEquals(
0511:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
0512:                                + data + "</root>\r\n", result);
0513:
0514:            }
0515:
0516:            public void testElementWithTextAndCarriageReturns()
0517:                    throws IOException {
0518:
0519:                String data = "   test   \r\n   \n  \r hello again";
0520:                root.appendChild(data);
0521:                Serializer serializer = new Serializer(out, "UTF-8");
0522:                serializer.write(doc);
0523:                String result = out.toString("UTF-8");
0524:
0525:                assertEquals(
0526:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
0527:                                + "   test   &#x0D;\n   \n  &#x0D; hello again"
0528:                                + "</root>\r\n", result);
0529:
0530:            }
0531:
0532:            private void serializeParseAndCompare(Document doc)
0533:                    throws IOException, ParsingException {
0534:
0535:                ByteArrayOutputStream out = new ByteArrayOutputStream();
0536:                Serializer serializer = new Serializer(out, "UTF-8");
0537:                serializer.write(doc);
0538:                String result = out.toString("UTF-8");
0539:
0540:                Document resultDoc = parser.build(result, null);
0541:                XOMTestCase.assertEquals(doc, resultDoc);
0542:
0543:                setOutputStreamSerializeParseAndCompare(doc);
0544:
0545:            }
0546:
0547:            private void setOutputStreamSerializeParseAndCompare(Document doc)
0548:                    throws IOException, ParsingException {
0549:
0550:                Serializer serializer = new Serializer(out);
0551:                serializer.write(doc);
0552:                ByteArrayOutputStream out2 = new ByteArrayOutputStream();
0553:                serializer.setOutputStream(out2);
0554:                serializer.write(doc);
0555:                String result = out2.toString("UTF-8");
0556:
0557:                Document resultDoc = parser.build(result, null);
0558:                XOMTestCase.assertEquals(doc, resultDoc);
0559:
0560:            }
0561:
0562:            public void testComment() throws IOException, ParsingException {
0563:
0564:                String data = "  <>&amp;&entity; test   \n  hello again";
0565:                root.appendChild(new Comment(data));
0566:                serializeParseAndCompare(doc);
0567:
0568:            }
0569:
0570:            public void testProcessingInstruction() throws IOException,
0571:                    ParsingException {
0572:
0573:                String data = "<>&amp;&entity; test   \n  hello again";
0574:                root.appendChild(new ProcessingInstruction("target", data));
0575:                serializeParseAndCompare(doc);
0576:
0577:            }
0578:
0579:            public void testBasicElementWithText() throws IOException,
0580:                    ParsingException {
0581:
0582:                String data = "   test   \n  hello again";
0583:                root.appendChild(data);
0584:                serializeParseAndCompare(doc);
0585:
0586:            }
0587:
0588:            public void testAttributes() throws IOException, ParsingException {
0589:
0590:                root.addAttribute(new Attribute("test", "sadlkhasdk"));
0591:                String data = "   test   \n  hello again";
0592:                root.appendChild(data);
0593:                serializeParseAndCompare(doc);
0594:
0595:                root.addAttribute(new Attribute("test2", "sadlkhasdk"));
0596:                serializeParseAndCompare(doc);
0597:
0598:                root.addAttribute(new Attribute("test3", " s adl  khasdk  "));
0599:                serializeParseAndCompare(doc);
0600:
0601:                root.addAttribute(new Attribute("xlink:type",
0602:                        "http://www.w3.org/2001/xlink", " s adl  khasdk  "));
0603:                serializeParseAndCompare(doc);
0604:
0605:            }
0606:
0607:            public void testChildElements() throws IOException,
0608:                    ParsingException {
0609:
0610:                serializeParseAndCompare(doc);
0611:
0612:                Element child1 = new Element("child");
0613:                Element child2 = new Element("child");
0614:                Element child3 = new Element("child");
0615:                serializeParseAndCompare(doc);
0616:                root.appendChild(child1);
0617:                serializeParseAndCompare(doc);
0618:                child1.appendChild(child2);
0619:                serializeParseAndCompare(doc);
0620:                root.appendChild(child3);
0621:                serializeParseAndCompare(doc);
0622:                child3.appendChild("some data");
0623:                serializeParseAndCompare(doc);
0624:                child2.appendChild("\nsome data with \n line breaks\n");
0625:                serializeParseAndCompare(doc);
0626:                root.insertChild("now let's have some mixed content", 0);
0627:                serializeParseAndCompare(doc);
0628:
0629:                root.setNamespaceURI("http://www.example.org/");
0630:                serializeParseAndCompare(doc);
0631:                child1.setNamespaceURI("http://www.example.org/");
0632:                serializeParseAndCompare(doc);
0633:                child2.setNamespaceURI("http://www.example.org/");
0634:                serializeParseAndCompare(doc);
0635:                child3.setNamespaceURI("http://www.example.org/");
0636:                serializeParseAndCompare(doc);
0637:                child1.setNamespacePrefix("example");
0638:                serializeParseAndCompare(doc);
0639:                child2.setNamespacePrefix("perverse");
0640:                serializeParseAndCompare(doc);
0641:
0642:            }
0643:
0644:            public void testPrologAndEpilog() throws IOException,
0645:                    ParsingException {
0646:
0647:                serializeParseAndCompare(doc);
0648:
0649:                doc.insertChild(new Comment("Hello"), 0);
0650:                serializeParseAndCompare(doc);
0651:                doc.insertChild(new DocType("root"), 0);
0652:                serializeParseAndCompare(doc);
0653:                doc.insertChild(new ProcessingInstruction("test", "some data"),
0654:                        0);
0655:                serializeParseAndCompare(doc);
0656:                doc.insertChild(new Comment("Goodbye"), 0);
0657:                serializeParseAndCompare(doc);
0658:                doc.insertChild(new ProcessingInstruction("goodbye",
0659:                        "some data"), 0);
0660:                serializeParseAndCompare(doc);
0661:                doc.appendChild(new Comment("Hello"));
0662:                serializeParseAndCompare(doc);
0663:                doc.appendChild(new ProcessingInstruction("test", "some data"));
0664:                serializeParseAndCompare(doc);
0665:
0666:            }
0667:
0668:            public void testChangeLineSeparator() throws IOException {
0669:
0670:                String breaks = "This\nstring\rcontains\r\nseveral\r\rweird line breaks.";
0671:                root.appendChild(breaks);
0672:                root.addAttribute(new Attribute("test", breaks));
0673:
0674:                Serializer serializer = new Serializer(out, "UTF-8");
0675:                serializer.setLineSeparator("\n");
0676:                serializer.write(doc);
0677:                String result = out.toString("UTF-8");
0678:                assertTrue(result.indexOf('\n') > 0);
0679:                assertTrue(result + "**\n" + result.indexOf('\r'), result
0680:                        .indexOf('\r') == -1);
0681:
0682:                out = new ByteArrayOutputStream();
0683:                serializer = new Serializer(out, "UTF-8");
0684:                serializer.setLineSeparator("\r");
0685:                serializer.write(doc);
0686:                result = out.toString("UTF-8");
0687:                assertTrue(result.indexOf('\r') > 0);
0688:                assertTrue(result.indexOf('\n') == -1);
0689:
0690:                out = new ByteArrayOutputStream();
0691:                serializer = new Serializer(out, "UTF-8");
0692:                serializer.setLineSeparator("\r\n");
0693:                serializer.write(doc);
0694:                result = out.toString("UTF-8");
0695:                assertTrue(result.indexOf("\r\n") > 0);
0696:                for (int i = 0; i < result.length(); i++) {
0697:                    int c = result.charAt(i);
0698:                    if (c == '\r')
0699:                        assertTrue(result.charAt(i + 1) == '\n');
0700:                }
0701:
0702:            }
0703:
0704:            public void testSettingOutputStreamDoesNotSetLineSeparator()
0705:                    throws IOException {
0706:
0707:                root.appendChild("This\rstring");
0708:                Serializer serializer = new Serializer(System.out, "UTF-8");
0709:
0710:                serializer.setOutputStream(out);
0711:
0712:                serializer.write(doc);
0713:                String result = out.toString("UTF-8");
0714:                assertTrue(result.indexOf("This&#x0D;string") > 0);
0715:
0716:            }
0717:
0718:            public void testSettingOutputStreamDoesNotUnnecessarilyEscapeLineBreaks()
0719:                    throws IOException {
0720:
0721:                root.appendChild("This\rstring");
0722:                Serializer serializer = new Serializer(System.out, "UTF-8");
0723:
0724:                serializer.setOutputStream(out);
0725:                serializer.setLineSeparator("\r\n");
0726:                serializer.write(doc);
0727:                String result = out.toString("UTF-8");
0728:                assertTrue(result.indexOf("This\r\nstring") > 0);
0729:
0730:            }
0731:
0732:            public void testDontChangeLineSeparator() throws IOException {
0733:
0734:                String breaks = "This\nstring\rcontains\r\rseveral\n\nweird line breaks.";
0735:                String breaksHalfEscaped = "This\nstring&#x0D;contains&#x0D;&#x0D;several"
0736:                        + "\n\nweird line breaks.";
0737:                String breaksEscaped = "This&#x0A;string&#x0D;contains&#x0D;&#x0D;several"
0738:                        + "&#x0A;&#x0A;weird line breaks.";
0739:                root.appendChild(breaks);
0740:
0741:                Serializer serializer = new Serializer(out, "UTF-8");
0742:                serializer.write(doc);
0743:                String result = out.toString("UTF-8");
0744:                assertTrue(result.indexOf(breaksHalfEscaped) > 0);
0745:
0746:                root = new Element("root");
0747:                doc = new Document(root);
0748:                root.addAttribute(new Attribute("test", breaks));
0749:
0750:                out = new ByteArrayOutputStream();
0751:                serializer = new Serializer(out, "UTF-8");
0752:                serializer.write(doc);
0753:                result = out.toString("UTF-8");
0754:                assertTrue(result.indexOf(breaksEscaped) > 0);
0755:
0756:            }
0757:
0758:            public void testPreserveBaseURI() throws IOException {
0759:
0760:                Serializer serializer = new Serializer(out, "UTF-8");
0761:                serializer.setPreserveBaseURI(true);
0762:                serializer.write(doc);
0763:                String result = out.toString("UTF-8");
0764:                assertTrue(result.indexOf("<root") > 1);
0765:                doc.setBaseURI("http://www.example.com/index.xml");
0766:                serializer.write(doc);
0767:                result = out.toString("UTF-8");
0768:                assertTrue(result.indexOf("<root ") > 1);
0769:                assertTrue(result.indexOf("xml:base=") > 1);
0770:                assertTrue(result.indexOf("http://www.example.com/index.xml") > 1);
0771:
0772:            }
0773:
0774:            public void testPreserveBaseURIWithChildren() throws IOException {
0775:
0776:                String base = "http://www.example.com/index.xml";
0777:                root.setBaseURI(base);
0778:                Element child = new Element("child");
0779:                child.setBaseURI(base);
0780:                root.appendChild(child);
0781:                Serializer serializer = new Serializer(out, "UTF-8");
0782:                serializer.setPreserveBaseURI(true);
0783:                serializer.write(doc);
0784:                String result = out.toString("UTF-8");
0785:                assertEquals(
0786:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
0787:                                + "<root xml:base=\"" + base
0788:                                + "\"><child/></root>\r\n", result);
0789:
0790:            }
0791:
0792:            public void testPreserveBaseURIDoesntOverrideXMLBase()
0793:                    throws IOException {
0794:
0795:                root.addAttribute(new Attribute("xml:base",
0796:                        "http://www.w3.org/XML/1998/namespace",
0797:                        "http://www.cafeconleche.org/"));
0798:                Serializer serializer = new Serializer(out, "UTF-8");
0799:                serializer.setPreserveBaseURI(true);
0800:                serializer.write(doc);
0801:                String result = out.toString("UTF-8");
0802:                assertTrue(result.indexOf("<root") > 1);
0803:                doc.setBaseURI("http://www.example.com/index.xml");
0804:                serializer.write(doc);
0805:                result = out.toString("UTF-8");
0806:                assertTrue(result.indexOf("<root ") > 1);
0807:                assertTrue(result.indexOf("xml:base=") > 1);
0808:                assertTrue(result.indexOf("http://www.cafeconleche.org/") > 1);
0809:                assertEquals(-1, result
0810:                        .indexOf("http://www.example.com/index.xml"));
0811:
0812:            }
0813:
0814:            public void testSetLineSeparator() {
0815:
0816:                Serializer serializer = new Serializer(System.out);
0817:
0818:                serializer.setLineSeparator("\r");
0819:                assertEquals("\r", serializer.getLineSeparator());
0820:                serializer.setLineSeparator("\n");
0821:                assertEquals("\n", serializer.getLineSeparator());
0822:                serializer.setLineSeparator("\r\n");
0823:                assertEquals("\r\n", serializer.getLineSeparator());
0824:
0825:                try {
0826:                    serializer.setLineSeparator("r");
0827:                    fail("Allowed illegal separator character");
0828:                } catch (IllegalArgumentException success) {
0829:                    assertNotNull(success.getMessage());
0830:                }
0831:
0832:                try {
0833:                    serializer.setLineSeparator("n");
0834:                    fail("Allowed illegal separator character");
0835:                } catch (IllegalArgumentException success) {
0836:                    assertNotNull(success.getMessage());
0837:                }
0838:
0839:                try {
0840:                    serializer.setLineSeparator(" ");
0841:                    fail("Allowed illegal separator character");
0842:                } catch (IllegalArgumentException success) {
0843:                    assertNotNull(success.getMessage());
0844:                }
0845:
0846:                try {
0847:                    serializer.setLineSeparator("rn");
0848:                    fail("Allowed illegal separator character");
0849:                } catch (IllegalArgumentException success) {
0850:                    assertNotNull(success.getMessage());
0851:                }
0852:
0853:                try {
0854:                    serializer.setLineSeparator("<");
0855:                    fail("Allowed illegal separator character");
0856:                } catch (IllegalArgumentException success) {
0857:                    assertNotNull(success.getMessage());
0858:                }
0859:
0860:                try {
0861:                    serializer.setLineSeparator("\u0085");
0862:                    fail("Allowed NEL separator character");
0863:                } catch (IllegalArgumentException success) {
0864:                    assertNotNull(success.getMessage());
0865:                }
0866:
0867:            }
0868:
0869:            public void testLowerLimitOfUnicodeInCharacterData()
0870:                    throws IOException {
0871:
0872:                root.appendChild("\uD800\uDC00");
0873:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0874:                serializer.write(doc);
0875:                String result = out.toString("ISO-8859-1");
0876:                assertTrue(result, result.indexOf("&#x10000;") > 12);
0877:
0878:            }
0879:
0880:            public void testUpperLimitOfUnicodeInCharacterData()
0881:                    throws IOException {
0882:
0883:                root.appendChild("\uDBFF\uDFFD");
0884:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0885:                serializer.write(doc);
0886:                String result = out.toString("ISO-8859-1");
0887:                assertTrue(result, result.indexOf("&#x10FFFD;") > 12);
0888:
0889:            }
0890:
0891:            public void testSerializePlane1CharacterInAttributeValue()
0892:                    throws IOException {
0893:
0894:                root.addAttribute(new Attribute("test", "\uD834\uDD1E"));
0895:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0896:                serializer.write(doc);
0897:                String result = out.toString("ISO-8859-1");
0898:                assertTrue(result, result.indexOf("&#x1D11E;") > 12);
0899:
0900:            }
0901:
0902:            public void testSerializePlane1CharacterInCharacterData()
0903:                    throws IOException {
0904:
0905:                root.appendChild("\uD834\uDD1E");
0906:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0907:                serializer.write(doc);
0908:                String result = out.toString("ISO-8859-1");
0909:                assertTrue(result, result.indexOf("&#x1D11E;") > 12);
0910:
0911:            }
0912:
0913:            public void testSurrogatePairCountsAsOneCharacterForColumnCount()
0914:                    throws IOException {
0915:
0916:                Element root = new Element("r");
0917:                root.appendChild("\uD834\uDD1E");
0918:                Document doc = new Document(root);
0919:                Serializer serializer = new ColumnSerializer(out);
0920:                serializer.write(doc);
0921:
0922:            }
0923:
0924:            private static class ColumnSerializer extends Serializer {
0925:
0926:                ColumnSerializer(OutputStream out) {
0927:                    super (out);
0928:                }
0929:
0930:                public void write(Document doc) throws IOException {
0931:
0932:                    for (int i = 0; i < doc.getChildCount(); i++) {
0933:                        writeChild(doc.getChild(i));
0934:                    }
0935:                    super .flush();
0936:                    assertEquals(8, super .getColumnNumber());
0937:
0938:                }
0939:
0940:            }
0941:
0942:            public void testEscapeAttributeValue() throws IOException {
0943:
0944:                root.addAttribute(new Attribute("test", "\u0110"));
0945:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0946:                serializer.write(doc);
0947:                String result = out.toString("ISO-8859-1");
0948:                assertTrue(result, result.indexOf("&#x110;") > 5);
0949:
0950:            }
0951:
0952:            public void testLineFeedInAttributeValueWithDefaultOptions()
0953:                    throws IOException, ParsingException {
0954:
0955:                root.addAttribute(new Attribute("test", "\n"));
0956:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0957:                serializer.write(doc);
0958:                out.close();
0959:                InputStream in = new ByteArrayInputStream(out.toByteArray());
0960:                Document reparsed = parser.build(in);
0961:                assertEquals(doc, reparsed);
0962:
0963:            }
0964:
0965:            public void testCarriageReturnInAttributeValueWithDefaultOptions()
0966:                    throws IOException, ParsingException {
0967:
0968:                root.addAttribute(new Attribute("test", "\r"));
0969:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0970:                serializer.write(doc);
0971:                out.close();
0972:                InputStream in = new ByteArrayInputStream(out.toByteArray());
0973:                Document reparsed = parser.build(in);
0974:                assertEquals(doc, reparsed);
0975:
0976:            }
0977:
0978:            public void testCarriageReturnInTextWithDefaultOptions()
0979:                    throws IOException, ParsingException {
0980:
0981:                root.appendChild("\r");
0982:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0983:                serializer.write(doc);
0984:                out.close();
0985:                InputStream in = new ByteArrayInputStream(out.toByteArray());
0986:                Document reparsed = parser.build(in);
0987:                assertEquals(doc, reparsed);
0988:
0989:            }
0990:
0991:            public void testTabInAttributeValueWithDefaultOptions()
0992:                    throws IOException, ParsingException {
0993:
0994:                root.addAttribute(new Attribute("test", "\t"));
0995:                Serializer serializer = new Serializer(out, "ISO-8859-1");
0996:                serializer.write(doc);
0997:                out.close();
0998:                InputStream in = new ByteArrayInputStream(out.toByteArray());
0999:                Document reparsed = parser.build(in);
1000:                assertEquals(doc, reparsed);
1001:
1002:            }
1003:
1004:            /**
1005:             * <p>
1006:             *   Test that tabs in attribute values are escaped even when
1007:             *   a line separator is set.
1008:             * </p>
1009:             */
1010:            public void testTabInAttributeValueWithLineSeparator()
1011:                    throws IOException, ParsingException {
1012:
1013:                root.addAttribute(new Attribute("test", "\t"));
1014:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1015:                serializer.setLineSeparator("\r");
1016:                serializer.write(doc);
1017:                out.close();
1018:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1019:                Document reparsed = parser.build(in);
1020:                assertEquals(doc, reparsed);
1021:
1022:            }
1023:
1024:            /**
1025:             * <p>
1026:             *   Test that tabs in attribute values are not escaped when 
1027:             *   indenting.
1028:             * </p>
1029:             */
1030:            public void testTabInAttributeValueWithIndenting()
1031:                    throws IOException, ParsingException {
1032:
1033:                root.addAttribute(new Attribute("test", "\t"));
1034:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1035:                serializer.setIndent(2);
1036:                serializer.write(doc);
1037:                out.close();
1038:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1039:                Document reparsed = parser.build(in);
1040:                String result = reparsed.getRootElement().getAttributeValue(
1041:                        "test");
1042:                assertEquals("Tab not normalized to space", " ", result);
1043:
1044:            }
1045:
1046:            public void testCRLFInAttributeValueWithLineSeparatorCR()
1047:                    throws IOException, ParsingException {
1048:
1049:                root.addAttribute(new Attribute("test", "\r\n"));
1050:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1051:                serializer.setLineSeparator("\r");
1052:                serializer.write(doc);
1053:                out.close();
1054:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1055:                Document reparsed = parser.build(in);
1056:                String result = reparsed.getRootElement().getAttributeValue(
1057:                        "test");
1058:                assertEquals("\r", result);
1059:
1060:            }
1061:
1062:            public void testCRLFInAttributeValueWithLineSeparatorLF()
1063:                    throws IOException, ParsingException {
1064:
1065:                root.addAttribute(new Attribute("test", "\r\n"));
1066:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1067:                serializer.setLineSeparator("\n");
1068:                serializer.write(doc);
1069:                out.close();
1070:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1071:                Document reparsed = parser.build(in);
1072:                String result = reparsed.getRootElement().getAttributeValue(
1073:                        "test");
1074:                assertEquals("\n", result);
1075:
1076:            }
1077:
1078:            public void testLFInAttributeValueWithLineSeparatorCRLF()
1079:                    throws IOException, ParsingException {
1080:
1081:                root.addAttribute(new Attribute("test", "\n"));
1082:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1083:                serializer.setLineSeparator("\r\n");
1084:                serializer.write(doc);
1085:                out.close();
1086:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1087:                Document reparsed = parser.build(in);
1088:                String result = reparsed.getRootElement().getAttributeValue(
1089:                        "test");
1090:                assertEquals("\r\n", result);
1091:
1092:            }
1093:
1094:            public void testNotEscapeLinefeedInTextContent() throws IOException {
1095:
1096:                root.appendChild("\r\n");
1097:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1098:                serializer.write(doc);
1099:                out.close();
1100:                String result = new String(out.toByteArray(), "ISO-8859-1");
1101:                assertEquals(
1102:                        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>&#x0D;\n</root>\r\n",
1103:                        result);
1104:
1105:            }
1106:
1107:            public void testCRLFInAttributeValue() throws IOException,
1108:                    ParsingException {
1109:
1110:                root.addAttribute(new Attribute("test", "a\r\na"));
1111:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1112:                serializer.write(doc);
1113:                out.close();
1114:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1115:                Document reparsed = parser.build(in);
1116:                String result = reparsed.getRootElement().getAttributeValue(
1117:                        "test");
1118:                assertEquals("CRLF not escaped", "a\r\na", result);
1119:
1120:            }
1121:
1122:            public void testPunctuationInAttributeValueNonUnicode()
1123:                    throws IOException, ParsingException {
1124:
1125:                root.addAttribute(new Attribute("test", "$()*+,="));
1126:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1127:                serializer.write(doc);
1128:                out.close();
1129:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1130:                Document reparsed = parser.build(in);
1131:                String result = reparsed.getRootElement().getAttributeValue(
1132:                        "test");
1133:                assertEquals("$()*+,=", result);
1134:
1135:            }
1136:
1137:            public void testCRLFInAttributeValueWithIndenting()
1138:                    throws IOException, ParsingException {
1139:
1140:                root.addAttribute(new Attribute("test", "\r\n"));
1141:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1142:                serializer.setIndent(2);
1143:                serializer.write(doc);
1144:                out.close();
1145:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1146:                Document reparsed = parser.build(in);
1147:                String result = reparsed.getRootElement().getAttributeValue(
1148:                        "test");
1149:                assertEquals("CRLF unnecessarily escaped", -1, result
1150:                        .indexOf('\r'));
1151:                // Need to figure out the serializer should indent this 
1152:                // and write a unit test for that too.
1153:
1154:            }
1155:
1156:            public void testCRLFInAttributeValueWithMaxLength()
1157:                    throws IOException, ParsingException {
1158:
1159:                root.addAttribute(new Attribute("test", "\r\n"));
1160:
1161:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1162:                serializer.setMaxLength(64);
1163:                serializer.write(doc);
1164:                out.close();
1165:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1166:                Document reparsed = parser.build(in);
1167:                String result = reparsed.getRootElement().getAttributeValue(
1168:                        "test");
1169:                assertEquals("CRLF unnecessarily escaped", " ", result);
1170:
1171:            }
1172:
1173:            public void testCRInTextValueWithLineSeparator()
1174:                    throws IOException, ParsingException {
1175:
1176:                root.appendChild("\r");
1177:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1178:                serializer.setLineSeparator("\n");
1179:                serializer.write(doc);
1180:                out.close();
1181:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1182:                Document reparsed = parser.build(in);
1183:                String result = reparsed.getValue();
1184:                assertEquals("\n", result);
1185:
1186:            }
1187:
1188:            public void testCRLFInTextValueWithLineSeparator()
1189:                    throws IOException, ParsingException {
1190:
1191:                root.appendChild("test \r\n test");
1192:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1193:                serializer.setLineSeparator("\n");
1194:                serializer.write(doc);
1195:                out.close();
1196:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1197:                Document reparsed = parser.build(in);
1198:                String result = reparsed.getValue();
1199:                assertEquals("test \n test", result);
1200:
1201:            }
1202:
1203:            public void testCRInTextWithIndenting() throws IOException,
1204:                    ParsingException {
1205:
1206:                root.appendChild("\r");
1207:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1208:                serializer.setIndent(2);
1209:                serializer.write(doc);
1210:                out.close();
1211:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1212:                Document reparsed = parser.build(in);
1213:                String result = reparsed.getValue();
1214:                assertEquals("Carriage return unnecessarily escaped", -1,
1215:                        result.indexOf('\r'));
1216:
1217:                // really need to think about what the serializer should output here
1218:                // and write a test case for that; this is not ideal output
1219:
1220:            }
1221:
1222:            public void testCRInTextWithMaxLength() throws IOException,
1223:                    ParsingException {
1224:
1225:                root.appendChild("\r");
1226:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1227:                serializer.setMaxLength(64);
1228:                serializer.write(doc);
1229:                out.close();
1230:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1231:                Document reparsed = parser.build(in);
1232:                String result = reparsed.getValue();
1233:                assertEquals("Carriage return unnecessarily escaped", "\n",
1234:                        result);
1235:
1236:            }
1237:
1238:            public void testTabInAttributeValueWithMaxLength()
1239:                    throws IOException, ParsingException {
1240:
1241:                root.addAttribute(new Attribute("test", "\t"));
1242:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1243:                serializer.setMaxLength(64);
1244:                serializer.write(doc);
1245:                out.close();
1246:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1247:                Document reparsed = parser.build(in);
1248:                String result = reparsed.getRootElement().getAttributeValue(
1249:                        "test");
1250:                assertEquals("Tab not normalized to space", " ", result);
1251:
1252:            }
1253:
1254:            /**
1255:             * <p>
1256:             *   Test that tabs in attribute values are escaped when 
1257:             *   max length is set to 0
1258:             * </p>
1259:             */
1260:            public void testTabInAttributeValueWithZeroMaxLength()
1261:                    throws IOException, ParsingException {
1262:
1263:                root.addAttribute(new Attribute("test", "\t"));
1264:                Serializer serializer = new Serializer(out, "ISO-8859-1");
1265:                serializer.setMaxLength(0);
1266:                serializer.write(doc);
1267:                out.close();
1268:                InputStream in = new ByteArrayInputStream(out.toByteArray());
1269:                Document reparsed = parser.build(in);
1270:                String result = reparsed.getRootElement().getAttributeValue(
1271:                        "test");
1272:                assertEquals("Tab not normalized to space", "\t", result);
1273:
1274:            }
1275:
1276:            public void testSetMaxLength() {
1277:                Serializer serializer = new Serializer(System.out);
1278:
1279:                serializer.setMaxLength(72);
1280:                assertEquals(72, serializer.getMaxLength());
1281:                serializer.setMaxLength(720);
1282:                assertEquals(720, serializer.getMaxLength());
1283:                serializer.setMaxLength(1);
1284:                assertEquals(1, serializer.getMaxLength());
1285:                serializer.setMaxLength(0);
1286:                assertEquals(0, serializer.getMaxLength());
1287:                serializer.setMaxLength(-1);
1288:                assertEquals(0, serializer.getMaxLength());
1289:
1290:            }
1291:
1292:            public void testSetIndent() {
1293:
1294:                Serializer serializer = new Serializer(System.out);
1295:                serializer.setIndent(72);
1296:                assertEquals(72, serializer.getIndent());
1297:                serializer.setIndent(720);
1298:                assertEquals(720, serializer.getIndent());
1299:                serializer.setIndent(1);
1300:                assertEquals(1, serializer.getIndent());
1301:                serializer.setIndent(0);
1302:                assertEquals(0, serializer.getIndent());
1303:                try {
1304:                    serializer.setIndent(-1);
1305:                    fail("Allowed negative indent");
1306:                } catch (IllegalArgumentException success) {
1307:                    assertNotNull(success.getMessage());
1308:                }
1309:
1310:            }
1311:
1312:            public void testReallyBigIndent() throws ValidityException,
1313:                    ParsingException, IOException {
1314:
1315:                ByteArrayOutputStream out = new ByteArrayOutputStream();
1316:                Serializer serializer = new Serializer(out);
1317:                serializer.setIndent(100);
1318:                StringBuffer spaces = new StringBuffer(400);
1319:                for (int i = 0; i < 400; i++)
1320:                    spaces.append(' ');
1321:                String _400 = spaces.toString();
1322:                String _300 = spaces.substring(0, 300);
1323:                String _200 = spaces.substring(0, 200);
1324:                String _100 = spaces.substring(0, 100);
1325:
1326:                String original = "<a><b><c><d><e>Hello</e></d></c></b></a>";
1327:                Document doc = parser.build(original, null);
1328:                serializer.write(doc);
1329:                String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<a>\r\n"
1330:                        + _100
1331:                        + "<b>\r\n"
1332:                        + _200
1333:                        + "<c>\r\n"
1334:                        + _300
1335:                        + "<d>\r\n"
1336:                        + _400
1337:                        + "<e>Hello</e>\r\n"
1338:                        + _300
1339:                        + "</d>\r\n"
1340:                        + _200
1341:                        + "</c>\r\n"
1342:                        + _100
1343:                        + "</b>\r\n</a>\r\n";
1344:                String result = new String(out.toByteArray(), "UTF-8");
1345:                assertEquals(expected, result);
1346:
1347:            }
1348:
1349:            public void testLineLength() throws IOException {
1350:
1351:                int length = 40;
1352:                String data = "This is a really long string that does not "
1353:                        + "contain any line breaks.  However, there is lots of "
1354:                        + "white space so there shouldn't be any trouble wrapping it"
1355:                        + " into 40 characters or less per line. ";
1356:                root.appendChild(data);
1357:                Serializer serializer = new Serializer(out, "UTF-8");
1358:                serializer.setMaxLength(length);
1359:                serializer.write(doc);
1360:                String result = out.toString("UTF-8");
1361:
1362:                BufferedReader reader = new BufferedReader(new StringReader(
1363:                        result));
1364:                for (String line = reader.readLine(); line != null; line = reader
1365:                        .readLine()) {
1366:                    assertTrue(line.length() + ": " + line,
1367:                            line.length() <= length);
1368:                }
1369:
1370:            }
1371:
1372:            public void testLineLengthWithSetOutputStream() throws IOException {
1373:
1374:                int length = 40;
1375:                String data = "This is a really long string that does not "
1376:                        + "contain any line breaks.  However, there is lots of "
1377:                        + "white space so there shouldn't be any trouble wrapping it"
1378:                        + " into 40 characters or less per line. ";
1379:                root.appendChild(data);
1380:                Serializer serializer = new Serializer(
1381:                        new ByteArrayOutputStream(), "UTF-8");
1382:                serializer.setMaxLength(length);
1383:                serializer.write(doc);
1384:                serializer.setOutputStream(out);
1385:                serializer.write(doc);
1386:                String result = out.toString("UTF-8");
1387:
1388:                BufferedReader reader = new BufferedReader(new StringReader(
1389:                        result));
1390:                for (String line = reader.readLine(); line != null; line = reader
1391:                        .readLine()) {
1392:                    assertTrue(line.length() + ": " + line,
1393:                            line.length() <= length);
1394:                }
1395:
1396:            }
1397:
1398:            public void testPrettyXML() throws IOException {
1399:
1400:                Element items = new Element("itemSet");
1401:                items.appendChild(new Element("item1"));
1402:                items.appendChild(new Element("item2"));
1403:                Document doc = new Document(items);
1404:                Serializer serializer = new Serializer(out);
1405:                serializer.setIndent(4);
1406:                serializer.write(doc);
1407:                serializer.flush();
1408:                out.close();
1409:                String result = new String(out.toByteArray(), "UTF-8");
1410:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1411:                        + "<itemSet>\r\n    <item1/>\r\n    <item2/>\r\n"
1412:                        + "</itemSet>\r\n", result);
1413:
1414:            }
1415:
1416:            public void testIndentAndBreakBeforeComment() throws IOException {
1417:
1418:                Element items = new Element("itemSet");
1419:                items.appendChild(new Comment("item1"));
1420:                Document doc = new Document(items);
1421:                Serializer serializer = new Serializer(out);
1422:                serializer.setIndent(4);
1423:                serializer.write(doc);
1424:                serializer.flush();
1425:                out.close();
1426:                String result = new String(out.toByteArray(), "UTF-8");
1427:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1428:                        + "<itemSet>\r\n    <!--item1-->\r\n"
1429:                        + "</itemSet>\r\n", result);
1430:
1431:            }
1432:
1433:            public void testOnlyXMLSpaceCountsAsBoundaryWhiteSpace()
1434:                    throws IOException {
1435:
1436:                String emSpace = "\u2003";
1437:                Element items = new Element("itemSet");
1438:                items.appendChild(emSpace);
1439:                items.appendChild(new Element("a"));
1440:
1441:                Document doc = new Document(items);
1442:                Serializer serializer = new Serializer(out);
1443:                serializer.setIndent(4);
1444:                serializer.write(doc);
1445:                serializer.flush();
1446:                out.close();
1447:                String result = new String(out.toByteArray(), "UTF-8");
1448:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1449:                        + "<itemSet>\u2003\r\n    <a/>\r\n</itemSet>\r\n",
1450:                        result);
1451:
1452:            }
1453:
1454:            public void testWhiteSpaceBetweenCommentsIsBoundaryWhiteSpace()
1455:                    throws IOException {
1456:
1457:                Element items = new Element("itemSet");
1458:                items.appendChild(new Comment("item1"));
1459:                items.appendChild("      \r\n              ");
1460:                items.appendChild(new Comment("item2"));
1461:                Document doc = new Document(items);
1462:                Serializer serializer = new Serializer(out);
1463:                serializer.setIndent(4);
1464:                serializer.write(doc);
1465:                serializer.flush();
1466:                out.close();
1467:                String result = new String(out.toByteArray(), "UTF-8");
1468:                assertEquals(
1469:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1470:                                + "<itemSet>\r\n    <!--item1-->\r\n    <!--item2-->\r\n"
1471:                                + "</itemSet>\r\n", result);
1472:
1473:            }
1474:
1475:            public void testWhiteSpaceBeforeCommentIsBoundaryWhiteSpace()
1476:                    throws IOException {
1477:
1478:                Element items = new Element("itemSet");
1479:                items.appendChild("      \r\n              ");
1480:                items.appendChild(new Comment("item1"));
1481:                items.appendChild(new Comment("item2"));
1482:                Document doc = new Document(items);
1483:                Serializer serializer = new Serializer(out);
1484:                serializer.setIndent(4);
1485:                serializer.write(doc);
1486:                serializer.flush();
1487:                out.close();
1488:                String result = new String(out.toByteArray(), "UTF-8");
1489:                assertEquals(
1490:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1491:                                + "<itemSet>\r\n    <!--item1-->\r\n    <!--item2-->\r\n"
1492:                                + "</itemSet>\r\n", result);
1493:
1494:            }
1495:
1496:            public void testWhiteSpaceAfterCommentsIsBoundaryWhiteSpace()
1497:                    throws IOException {
1498:
1499:                Element items = new Element("itemSet");
1500:                items.appendChild(new Comment("item1"));
1501:                items.appendChild(new Comment("item2"));
1502:                items.appendChild("      \r\n              ");
1503:                Document doc = new Document(items);
1504:                Serializer serializer = new Serializer(out);
1505:                serializer.setIndent(4);
1506:                serializer.write(doc);
1507:                serializer.flush();
1508:                out.close();
1509:                String result = new String(out.toByteArray(), "UTF-8");
1510:                assertEquals(
1511:                        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1512:                                + "<itemSet>\r\n    <!--item1-->\r\n    <!--item2-->\r\n"
1513:                                + "</itemSet>\r\n", result);
1514:
1515:            }
1516:
1517:            public void testIndentAndBreakBeforeProcessingInstruction()
1518:                    throws IOException {
1519:
1520:                Element items = new Element("itemSet");
1521:                items.appendChild(new ProcessingInstruction("target", "value"));
1522:                Document doc = new Document(items);
1523:                Serializer serializer = new Serializer(out);
1524:                serializer.setIndent(4);
1525:                serializer.write(doc);
1526:                serializer.flush();
1527:                out.close();
1528:                String result = new String(out.toByteArray(), "UTF-8");
1529:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1530:                        + "<itemSet>\r\n    <?target value?>\r\n"
1531:                        + "</itemSet>\r\n", result);
1532:
1533:            }
1534:
1535:            public void testDontBreakLineInElementWithSimpleContent()
1536:                    throws IOException {
1537:
1538:                Element items = new Element("itemSet");
1539:                Element item1 = new Element("item1");
1540:                items.appendChild(item1);
1541:                item1.appendChild("content");
1542:                Document doc = new Document(items);
1543:                Serializer serializer = new Serializer(out);
1544:                serializer.setIndent(4);
1545:                serializer.write(doc);
1546:                serializer.flush();
1547:                out.close();
1548:                String result = new String(out.toByteArray(), "UTF-8");
1549:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1550:                        + "<itemSet>\r\n    <item1>content</item1>\r\n"
1551:                        + "</itemSet>\r\n", result);
1552:
1553:            }
1554:
1555:            public void testPrettyXMLWithSetOutputStream() throws IOException {
1556:
1557:                Element items = new Element("itemSet");
1558:                items.appendChild(new Element("item1"));
1559:                items.appendChild(new Element("item2"));
1560:                Document doc = new Document(items);
1561:                Serializer serializer = new Serializer(
1562:                        new ByteArrayOutputStream());
1563:                serializer.setIndent(4);
1564:                serializer.setLineSeparator("\n");
1565:                serializer.write(doc);
1566:                serializer.setOutputStream(out);
1567:                serializer.write(doc);
1568:                serializer.flush();
1569:                out.close();
1570:                String result = new String(out.toByteArray(), "UTF-8");
1571:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1572:                        + "<itemSet>\n    <item1/>\n    <item2/>\n"
1573:                        + "</itemSet>\n", result);
1574:
1575:            }
1576:
1577:            public void testAmpersandAndLessThanInText() throws IOException {
1578:
1579:                root.appendChild("data<data&data");
1580:                Serializer serializer = new Serializer(out);
1581:                serializer.write(doc);
1582:                serializer.flush();
1583:                out.close();
1584:                String result = new String(out.toByteArray(), "UTF-8");
1585:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1586:                        + "<root>data&lt;data&amp;data" + "</root>\r\n", result);
1587:
1588:            }
1589:
1590:            public void testAmpersandAndAngleBracketsInAttributeValue()
1591:                    throws IOException {
1592:
1593:                root.addAttribute(new Attribute("b", "data<data>data&"));
1594:                Serializer serializer = new Serializer(out);
1595:                serializer.write(doc);
1596:                serializer.flush();
1597:                out.close();
1598:                String result = new String(out.toByteArray(), "UTF-8");
1599:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1600:                        + "<root b=\"data&lt;data&gt;data&amp;\"/>\r\n", result);
1601:
1602:            }
1603:
1604:            public void testSetNFC() {
1605:
1606:                Serializer serializer = new Serializer(System.out);
1607:                assertFalse(serializer.getUnicodeNormalizationFormC());
1608:                serializer.setUnicodeNormalizationFormC(true);
1609:                assertTrue(serializer.getUnicodeNormalizationFormC());
1610:
1611:            }
1612:
1613:            public void testNFCInElementContent() throws IOException {
1614:
1615:                Element root = new Element("a");
1616:                root.appendChild("c\u0327"); // c with combining cedilla
1617:                Document doc = new Document(root);
1618:                Serializer serializer = new Serializer(out);
1619:                serializer.setUnicodeNormalizationFormC(true);
1620:                serializer.write(doc);
1621:                serializer.flush();
1622:                out.close();
1623:                String result = new String(out.toByteArray(), "UTF-8");
1624:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1625:                        + "<a>\u00E7</a>\r\n", result);
1626:
1627:            }
1628:
1629:            public void testNFCInConsecutiveTextNodes() throws IOException {
1630:
1631:                Element root = new Element("a");
1632:                root.appendChild("c");
1633:                root.appendChild("\u0327"); // combining cedilla
1634:                Document doc = new Document(root);
1635:                Serializer serializer = new Serializer(out);
1636:                serializer.setUnicodeNormalizationFormC(true);
1637:                serializer.write(doc);
1638:                serializer.flush();
1639:                out.close();
1640:                String result = new String(out.toByteArray(), "UTF-8");
1641:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1642:                        + "<a>\u00E7</a>\r\n", result);
1643:
1644:            }
1645:
1646:            public void testNoNFCByDefault() throws IOException {
1647:
1648:                Element root = new Element("c\u0327");
1649:                root.appendChild("c\u0327"); // c with combining cedilla
1650:                Document doc = new Document(root);
1651:                Serializer serializer = new Serializer(out);
1652:                serializer.write(doc);
1653:                serializer.flush();
1654:                out.close();
1655:                String result = new String(out.toByteArray(), "UTF-8");
1656:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1657:                        + "<c\u0327>c\u0327</c\u0327>\r\n", result);
1658:
1659:            }
1660:
1661:            public void testUnicodeNFCTestSuite() throws ParsingException,
1662:                    IOException {
1663:
1664:                Builder builder = new Builder();
1665:                Document doc = builder.build("data/nfctests.xml");
1666:                Elements tests = doc.getRootElement().getChildElements("test");
1667:                int size = tests.size();
1668:                for (int i = 0; i < size; i++) {
1669:                    // System.out.println(i);
1670:                    Element test = tests.get(i);
1671:                    test.detach();
1672:                    Document testdoc = new Document(test);
1673:                    ByteArrayOutputStream out = new ByteArrayOutputStream();
1674:                    Serializer serializer = new Serializer(out);
1675:                    serializer.setUnicodeNormalizationFormC(true);
1676:                    serializer.write(testdoc);
1677:                    serializer.flush();
1678:                    String result = new String(out.toByteArray(), "UTF-8");
1679:                    Document resultDoc = builder.build(result, null);
1680:                    Element root = resultDoc.getRootElement();
1681:                    String c1 = root.getAttributeValue("c1");
1682:                    String c2 = root.getAttributeValue("c2");
1683:                    String c3 = root.getAttributeValue("c3");
1684:                    String c4 = root.getAttributeValue("c4");
1685:                    String c5 = root.getAttributeValue("c5");
1686:                    /* String v1 = root.getAttributeValue("v1");
1687:                    String v2 = root.getAttributeValue("v2");
1688:                    String v3 = root.getAttributeValue("v3");
1689:                    String v4 = root.getAttributeValue("v4");
1690:                    String v5 = root.getAttributeValue("v5"); */
1691:
1692:                    assertEquals(root.getValue(), c1, c2);
1693:                    assertEquals(c2, c3);
1694:                    // I'm not sure the v's are correct past the BMP
1695:                    //assertEquals(root.getValue(), c1, v1);
1696:                    // assertEquals(c1, v2);
1697:                    // assertEquals(c1, v3);
1698:
1699:                    assertEquals(c4, c5);
1700:                    // assertEquals(c4, v4);
1701:                    // assertEquals(c4, v5);
1702:
1703:                }
1704:
1705:            }
1706:
1707:            public void testNFCInAttribute() throws IOException {
1708:
1709:                root.addAttribute(new Attribute("c\u0327", "c\u0327")); // c with combining cedilla
1710:                Serializer serializer = new Serializer(out);
1711:                serializer.setUnicodeNormalizationFormC(true);
1712:                serializer.write(doc);
1713:                serializer.flush();
1714:                out.close();
1715:                String result = new String(out.toByteArray(), "UTF-8");
1716:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1717:                        + "<root \u00E7=\"\u00E7\"/>\r\n", result);
1718:
1719:            }
1720:
1721:            public void testNFCInElementName() throws IOException {
1722:
1723:                Element root = new Element("c\u0327"); // c with combining cedilla
1724:                Document doc = new Document(root);
1725:                Serializer serializer = new Serializer(out);
1726:                serializer.setUnicodeNormalizationFormC(true);
1727:                serializer.write(doc);
1728:                serializer.flush();
1729:                out.close();
1730:                String result = new String(out.toByteArray(), "UTF-8");
1731:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1732:                        + "<\u00E7/>\r\n", result);
1733:
1734:            }
1735:
1736:            public void testNFCInComment() throws IOException {
1737:
1738:                Element root = new Element("a");
1739:                Document doc = new Document(root);
1740:                doc.insertChild(new Comment("c\u0327hat"), 0); // c with combining cedilla
1741:                Serializer serializer = new Serializer(out);
1742:                serializer.setUnicodeNormalizationFormC(true);
1743:                serializer.write(doc);
1744:                serializer.flush();
1745:                out.close();
1746:                String result = new String(out.toByteArray(), "UTF-8");
1747:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1748:                        + "<!--\u00E7hat-->\r\n" + "<a/>\r\n", result);
1749:
1750:            }
1751:
1752:            public void testNFCInProcessingInstruction() throws IOException {
1753:
1754:                doc.appendChild(new ProcessingInstruction("c\u0327hat",
1755:                        "c\u0327hat"));
1756:                Serializer serializer = new Serializer(out);
1757:                serializer.setUnicodeNormalizationFormC(true);
1758:                serializer.write(doc);
1759:                serializer.flush();
1760:                out.close();
1761:                String result = new String(out.toByteArray(), "UTF-8");
1762:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1763:                        + "<root/>\r\n" + "<?\u00E7hat \u00E7hat?>\r\n", result);
1764:
1765:            }
1766:
1767:            public void testNFCInElementContentWithNonUnicodeEncoding()
1768:                    throws IOException {
1769:
1770:                root.appendChild("c\u0327"); // c with combining cedilla
1771:                Serializer serializer = new Serializer(out, "ISO-8859-5");
1772:                serializer.setUnicodeNormalizationFormC(true);
1773:                serializer.write(doc);
1774:                serializer.flush();
1775:                out.close();
1776:                String result = new String(out.toByteArray(), "ISO-8859-5");
1777:                assertEquals(
1778:                        "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
1779:                                + "<root>&#xE7;</root>\r\n", result);
1780:
1781:            }
1782:
1783:            public void testNFCFollowingEntityReference() throws IOException {
1784:
1785:                root.appendChild("<\u0338"); // < followed by not
1786:                Serializer serializer = new Serializer(out);
1787:                serializer.setUnicodeNormalizationFormC(true);
1788:                serializer.write(doc);
1789:                serializer.flush();
1790:                out.close();
1791:                String result = new String(out.toByteArray(), "UTF-8");
1792:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1793:                        + "<root>\u226E</root>\r\n", result);
1794:
1795:            }
1796:
1797:            public void testNFCWithSetOutputStream() throws IOException {
1798:
1799:                root.appendChild("c\u0327"); // c with combining cedilla
1800:                Serializer serializer = new Serializer(
1801:                        new ByteArrayOutputStream(), "ISO-8859-5");
1802:                serializer.setUnicodeNormalizationFormC(true);
1803:                serializer.write(doc);
1804:                serializer.setOutputStream(out);
1805:                serializer.write(doc);
1806:                serializer.flush();
1807:                out.close();
1808:                String result = new String(out.toByteArray(), "ISO-8859-5");
1809:                assertEquals(
1810:                        "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
1811:                                + "<root>&#xE7;</root>\r\n", result);
1812:
1813:            }
1814:
1815:            public void testNFCWithKoreanCharacter() throws IOException {
1816:
1817:                root.appendChild("\u1111\u1171\u11B6"); // see p. 88 of Unicode 4.0 book
1818:                Serializer serializer = new Serializer(
1819:                        new ByteArrayOutputStream());
1820:                serializer.setUnicodeNormalizationFormC(true);
1821:                serializer.write(doc);
1822:                serializer.setOutputStream(out);
1823:                serializer.write(doc);
1824:                serializer.flush();
1825:                out.close();
1826:                String result = new String(out.toByteArray(), "UTF-8");
1827:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1828:                        + "<root>\uD4DB</root>\r\n", result);
1829:
1830:            }
1831:
1832:            public void testNullOutputStream() {
1833:
1834:                try {
1835:                    new Serializer(null);
1836:                    fail("Allowed null output stream");
1837:                } catch (NullPointerException success) {
1838:                    assertNotNull(success.getMessage());
1839:                }
1840:
1841:            }
1842:
1843:            public void testNullOutputStreamWithEncoding()
1844:                    throws UnsupportedEncodingException {
1845:
1846:                try {
1847:                    new Serializer(null, "UTF-8");
1848:                    fail("Allowed null output stream");
1849:                } catch (NullPointerException success) {
1850:                    assertNotNull(success.getMessage());
1851:                }
1852:
1853:            }
1854:
1855:            public void testNullEncoding() throws UnsupportedEncodingException {
1856:
1857:                try {
1858:                    new Serializer(System.out, null);
1859:                    fail("Allowed null encoding");
1860:                } catch (NullPointerException success) {
1861:                    assertNotNull(success.getMessage());
1862:                }
1863:
1864:            }
1865:
1866:            // make sure null pointer exception doesn't cause any output
1867:            public void testNullDocument() throws IOException {
1868:
1869:                Serializer serializer = new Serializer(out, "UTF-16");
1870:                try {
1871:                    serializer.write(null);
1872:                    fail("Wrote null document");
1873:                } catch (NullPointerException success) {
1874:                    // success   
1875:                }
1876:                byte[] result = out.toByteArray();
1877:                assertEquals(0, result.length);
1878:
1879:            }
1880:
1881:            public void testGetEncoding() throws UnsupportedEncodingException {
1882:
1883:                Serializer serializer = new Serializer(System.out, "ISO-8859-1");
1884:                assertEquals("ISO-8859-1", serializer.getEncoding());
1885:
1886:            }
1887:
1888:            public void testGetPreserveBaseURI()
1889:                    throws UnsupportedEncodingException {
1890:
1891:                Serializer serializer = new Serializer(System.out, "ISO-8859-1");
1892:                assertFalse(serializer.getPreserveBaseURI());
1893:                serializer.setPreserveBaseURI(true);
1894:                assertTrue(serializer.getPreserveBaseURI());
1895:                serializer.setPreserveBaseURI(false);
1896:                assertFalse(serializer.getPreserveBaseURI());
1897:
1898:            }
1899:
1900:            public void testSerializeDocTypeWithSystemID() throws IOException {
1901:
1902:                Serializer serializer = new Serializer(out);
1903:                Document doc = new Document(new Element("a"));
1904:                doc.setDocType(new DocType("b", "example.dtd"));
1905:                serializer.write(doc);
1906:                String result = out.toString("UTF-8");
1907:                assertTrue(result.endsWith("<a/>\r\n"));
1908:                assertTrue(result
1909:                        .indexOf("<!DOCTYPE b SYSTEM \"example.dtd\">") > 0);
1910:
1911:            }
1912:
1913:            public void testSerializeDocTypeWithPublicAndSystemID()
1914:                    throws IOException {
1915:
1916:                Serializer serializer = new Serializer(out);
1917:                Document doc = new Document(new Element("a"));
1918:                doc
1919:                        .setDocType(new DocType("b",
1920:                                "-//W3C//DTD XHTML 1.0 Transitional//EN",
1921:                                "example.dtd"));
1922:                serializer.write(doc);
1923:                String result = out.toString("UTF-8");
1924:                assertTrue(result.endsWith("<a/>\r\n"));
1925:                assertTrue(result
1926:                        .indexOf("<!DOCTYPE b PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"example.dtd\">") > 0);
1927:
1928:            }
1929:
1930:            public void testSerializeXMLNamespaceDeclaration()
1931:                    throws ParsingException, IOException {
1932:
1933:                Serializer serializer = new Serializer(out);
1934:                Element root = new Element("root");
1935:                root.addNamespaceDeclaration("xml", Namespace.XML_NAMESPACE);
1936:                Document doc = new Document(root);
1937:                serializer.write(doc);
1938:                String result = out.toString("UTF-8");
1939:                assertTrue(result.endsWith("<root/>\r\n"));
1940:
1941:            }
1942:
1943:            public void testSerializeDocTypeWithInternalDTDSubset()
1944:                    throws ParsingException, IOException {
1945:
1946:                Serializer serializer = new Serializer(out);
1947:                String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> ]><test/>";
1948:                Builder builder = new Builder();
1949:                Document doc = builder.build(data, "http://www.example.com");
1950:                serializer.write(doc);
1951:                String result = out.toString("UTF-8");
1952:                assertTrue(result.endsWith("<test/>\r\n"));
1953:                assertTrue(result.indexOf("<!DOCTYPE root [\r\n") > 0);
1954:                assertTrue(result.indexOf("\r\n]>\r\n") > 0);
1955:                assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r\n") > 0);
1956:
1957:            }
1958:
1959:            public void testSerializeInternalDTDSubsetContainingUnavailableCharacter()
1960:                    throws ParsingException, IOException {
1961:
1962:                Serializer serializer = new Serializer(out, "US-ASCII");
1963:                String data = "<!DOCTYPE root [" + "<!ELEMENT root EMPTY> "
1964:                        + "<!ATTLIST root attr CDATA 'café creme'> "
1965:                        + "]><test/>";
1966:                Builder builder = new Builder();
1967:                Document doc = builder.build(data, "http://www.example.com");
1968:                try {
1969:                    serializer.write(doc);
1970:                    fail("How'd you serialize é in ASCII?");
1971:                } catch (UnavailableCharacterException success) {
1972:                    assertTrue(success.getMessage().indexOf("é (&#xE9;)") > 1);
1973:                }
1974:
1975:            }
1976:
1977:            public void testLineBreaksInInternalDTDSubset()
1978:                    throws ParsingException, IOException {
1979:
1980:                Serializer serializer = new Serializer(out);
1981:                serializer.setLineSeparator("\r");
1982:                String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> <!ELEMENT data EMPTY> ]><test/>";
1983:                Builder builder = new Builder();
1984:                Document doc = builder.build(data, "http://www.example.com");
1985:                serializer.write(doc);
1986:                String result = out.toString("UTF-8");
1987:                assertTrue(result.endsWith("<test/>\r"));
1988:                assertTrue(result.indexOf("<!DOCTYPE root [\r") > 0);
1989:                assertTrue(result.indexOf("\r]>\r") > 0);
1990:                assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r") > 0);
1991:                assertTrue(result.indexOf("<!ELEMENT data EMPTY>\r") > 0);
1992:                assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\n"));
1993:                assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\n"));
1994:                assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\r"));
1995:                assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\r"));
1996:                assertEquals(-1, result.indexOf('\n'));
1997:
1998:            }
1999:
2000:            public void testSerializeQuoteInAttributeValue() throws IOException {
2001:
2002:                Serializer serializer = new Serializer(out);
2003:                root.addAttribute(new Attribute("name", "\""));
2004:                serializer.write(doc);
2005:                String result = out.toString("UTF-8");
2006:                assertTrue(result.endsWith("<root name=\"&quot;\"/>\r\n"));
2007:
2008:            }
2009:
2010:            public void testSerializeUnavailableCharacterInMarkup()
2011:                    throws IOException {
2012:
2013:                Serializer serializer = new Serializer(out, "ISO-8859-1");
2014:                Element root = new Element("\u0419");
2015:                Document doc = new Document(root);
2016:                try {
2017:                    serializer.write(doc);
2018:                    fail("Wrote bad character: " + out.toString("ISO-8859-1"));
2019:                } catch (UnavailableCharacterException success) {
2020:                    assertNotNull(success.getMessage());
2021:                    assertEquals('\u0419', success.getCharacter());
2022:                    assertEquals("ISO-8859-1", success.getEncoding());
2023:                }
2024:
2025:            }
2026:
2027:            public void testTurnLineFeedInAttributeValueIntoSpaceWhenIndenting()
2028:                    throws IOException {
2029:
2030:                Element root = new Element("a");
2031:                root.appendChild("c");
2032:                root.addAttribute(new Attribute("name", "value1\nvalue2"));
2033:                Document doc = new Document(root);
2034:                Serializer serializer = new Serializer(out);
2035:                serializer.setMaxLength(245);
2036:                serializer.setIndent(4);
2037:                serializer.write(doc);
2038:                serializer.flush();
2039:                out.close();
2040:                String result = new String(out.toByteArray(), "UTF-8");
2041:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2042:                        + "<a name=\"value1 value2\">c</a>\r\n", result);
2043:
2044:            }
2045:
2046:            public void testConflictBetweenMaxLengthAndIndent()
2047:                    throws IOException {
2048:
2049:                Element root = new Element("a");
2050:                Element b = new Element("b");
2051:                Element c = new Element("c");
2052:                Element d = new Element("d");
2053:                b.appendChild(c);
2054:                root.appendChild(b);
2055:                c.appendChild(d);
2056:                d.appendChild("data");
2057:                Document doc = new Document(root);
2058:                Serializer serializer = new Serializer(out);
2059:                serializer.setMaxLength(16);
2060:                serializer.setIndent(4);
2061:                serializer.write(doc);
2062:                serializer.flush();
2063:                out.close();
2064:                String result = new String(out.toByteArray(), "UTF-8");
2065:                assertEquals(
2066:                        "<?xml version=\"1.0\"\r\nencoding=\"UTF-8\"?>\r\n"
2067:                                + "<a>\r\n    <b>\r\n        <c>\r\n        <d>data</d>\r\n        </c>\r\n    </b>\r\n</a>\r\n",
2068:                        result);
2069:
2070:            }
2071:
2072:            public void testWriteChild() throws IOException {
2073:
2074:                ExposingSerializer serializer = new ExposingSerializer(out,
2075:                        "UTF-8");
2076:                try {
2077:                    serializer.writeChild(doc);
2078:                    fail("writeChild wrote a document");
2079:                } catch (XMLException success) {
2080:                    assertNotNull(success.getMessage());
2081:                }
2082:                try {
2083:                    serializer.writeChild(new Attribute("name", "value"));
2084:                    fail("writeChild wrote an attribute");
2085:                } catch (XMLException success) {
2086:                    assertNotNull(success.getMessage());
2087:                }
2088:
2089:            }
2090:
2091:            // just so we can test protected methods
2092:            private static class ExposingSerializer extends Serializer {
2093:
2094:                ExposingSerializer(OutputStream out, String encoding)
2095:                        throws UnsupportedEncodingException {
2096:                    super (out, encoding);
2097:                }
2098:
2099:                public void writeChild(Node node) throws IOException {
2100:                    super .writeChild(node);
2101:                }
2102:
2103:                public void exposedWriteRaw(String text) throws IOException {
2104:                    writeRaw(text);
2105:                }
2106:
2107:                public void exposedWriteEscaped(String text) throws IOException {
2108:                    writeEscaped(text);
2109:                }
2110:
2111:                public void exposedWriteAttributeValue(String text)
2112:                        throws IOException {
2113:                    writeAttributeValue(text);
2114:                }
2115:
2116:                public int exposeGetColumnNumber() {
2117:                    return super .getColumnNumber();
2118:                }
2119:
2120:            }
2121:
2122:            private static class ElementSerializer extends Serializer {
2123:
2124:                ElementSerializer(OutputStream out, String encoding)
2125:                        throws UnsupportedEncodingException {
2126:                    super (out, encoding);
2127:                }
2128:
2129:                protected void write(Element element) throws IOException {
2130:                    super .write(element);
2131:                }
2132:
2133:            }
2134:
2135:            private static class TextSerializer extends Serializer {
2136:
2137:                TextSerializer(OutputStream out) {
2138:                    super (out);
2139:                }
2140:
2141:                protected void write(Text text) throws IOException {
2142:                    super .write(text);
2143:                }
2144:
2145:            }
2146:
2147:            public void testWriteParentlessTextNode() throws IOException {
2148:
2149:                Text t = new Text("Hello");
2150:                ByteArrayOutputStream out = new ByteArrayOutputStream();
2151:                TextSerializer serializer = new TextSerializer(out);
2152:                serializer.write(t);
2153:                serializer.flush();
2154:                byte[] result = out.toByteArray();
2155:                String s = new String(result, "UTF-8");
2156:                assertEquals(s, t.getValue());
2157:
2158:            }
2159:
2160:            public void testWriteParentlessTextNodeWhileIndenting()
2161:                    throws IOException {
2162:
2163:                Text t = new Text("Hello");
2164:                ByteArrayOutputStream out = new ByteArrayOutputStream();
2165:                TextSerializer serializer = new TextSerializer(out);
2166:                serializer.setIndent(2);
2167:                serializer.write(t);
2168:                serializer.flush();
2169:                byte[] result = out.toByteArray();
2170:                String s = new String(result, "UTF-8");
2171:                assertEquals(s, t.getValue());
2172:
2173:            }
2174:
2175:            public void testWriteWhiteSpaceOnlyTextNodeWhileIndenting()
2176:                    throws IOException {
2177:
2178:                Text t = new Text("  \r\n  ");
2179:                ByteArrayOutputStream out = new ByteArrayOutputStream();
2180:                TextSerializer serializer = new TextSerializer(out);
2181:                serializer.setIndent(2);
2182:                serializer.write(t);
2183:                serializer.flush();
2184:                byte[] result = out.toByteArray();
2185:                assertEquals(0, result.length);
2186:
2187:            }
2188:
2189:            public void testWriteRaw() throws IOException {
2190:
2191:                ExposingSerializer serializer = new ExposingSerializer(out,
2192:                        "UTF-8");
2193:                serializer.exposedWriteRaw("<>&\"'");
2194:                assertEquals(5, serializer.exposeGetColumnNumber());
2195:                serializer.flush();
2196:                String result = out.toString("UTF-8");
2197:                assertEquals("<>&\"'", result);
2198:
2199:            }
2200:
2201:            public void testWriteParentlessElementInANamespace()
2202:                    throws IOException {
2203:
2204:                ElementSerializer serializer = new ElementSerializer(out,
2205:                        "UTF-8");
2206:                Element element = new Element("a", "http://www.example.org");
2207:                serializer.write(element);
2208:                serializer.flush();
2209:                String result = out.toString("UTF-8");
2210:                assertEquals("<a xmlns=\"http://www.example.org\"/>", result);
2211:
2212:            }
2213:
2214:            public void testWriteEscaped() throws IOException {
2215:
2216:                ExposingSerializer serializer = new ExposingSerializer(out,
2217:                        "UTF-8");
2218:                serializer.exposedWriteEscaped("<>&\"'");
2219:                assertEquals(15, serializer.exposeGetColumnNumber());
2220:                serializer.flush();
2221:                byte[] data = out.toByteArray();
2222:                String result = new String(data, "UTF-8");
2223:                assertEquals("&lt;&gt;&amp;\"'", result);
2224:
2225:            }
2226:
2227:            public void testWriteAttributeValue() throws IOException {
2228:
2229:                ExposingSerializer serializer = new ExposingSerializer(out,
2230:                        "UTF-8");
2231:                serializer.exposedWriteAttributeValue("<>&\"'");
2232:                assertEquals(20, serializer.exposeGetColumnNumber());
2233:                serializer.flush();
2234:                String result = out.toString("UTF-8");
2235:                assertEquals("&lt;&gt;&amp;&quot;'", result);
2236:
2237:            }
2238:
2239:            // This test case reproduces a bug that
2240:            // showed up while working on SAX conformance testing.
2241:            public void testElementsThatOnlyContainsAnEmptyTextNodeShouldBeOutputWithAnEmptyElementTag()
2242:                    throws IOException {
2243:
2244:                Element child = new Element("b");
2245:                child.appendChild("");
2246:                root.appendChild(child);
2247:
2248:                Serializer serializer = new Serializer(out);
2249:                serializer.write(doc);
2250:
2251:                serializer.flush();
2252:                String result = out.toString("UTF-8");
2253:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2254:                        + "<root><b/></root>\r\n", result);
2255:
2256:            }
2257:
2258:            // This test case reproduces a bug that
2259:            // showed up while working on SAX conformance testing.
2260:            public void testElementsThatOnlyContainEmptyTextNodesShouldBeOutputWithAnEmptyElementTag()
2261:                    throws IOException {
2262:
2263:                Element root = new Element("a");
2264:                Element child = new Element("b");
2265:                child.appendChild("");
2266:                child.appendChild("");
2267:                child.appendChild("");
2268:                root.appendChild(child);
2269:
2270:                Document doc = new Document(root);
2271:
2272:                Serializer serializer = new Serializer(out);
2273:                serializer.write(doc);
2274:
2275:                serializer.flush();
2276:                String result = out.toString("UTF-8");
2277:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2278:                        + "<a><b/></a>\r\n", result);
2279:
2280:            }
2281:
2282:            // This test case reproduces a bug that
2283:            // showed up while working on SAX conformance testing.
2284:            public void testElementsThatOnlyContainASingleSpaceShouldNotBeSplitWhileIndenting()
2285:                    throws IOException {
2286:
2287:                Element root = new Element("a");
2288:                Element child = new Element("b");
2289:                child.appendChild(" ");
2290:                root.appendChild(child);
2291:
2292:                Document doc = new Document(root);
2293:
2294:                Serializer serializer = new Serializer(out);
2295:                serializer.setIndent(4);
2296:                serializer.write(doc);
2297:
2298:                serializer.flush();
2299:                String result = out.toString("UTF-8");
2300:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2301:                        + "<a>\r\n    <b> </b>\r\n</a>\r\n", result);
2302:
2303:            }
2304:
2305:            public void testElementsThatOnlyContainTextNodesWithBoundaryWhiteSpaceShouldNotBeSplitWhileIndenting()
2306:                    throws IOException {
2307:
2308:                Element root = new Element("a");
2309:                Element child = new Element("b");
2310:                child.appendChild(" ");
2311:                child.appendChild(" ");
2312:                root.appendChild(child);
2313:
2314:                Document doc = new Document(root);
2315:
2316:                Serializer serializer = new Serializer(out);
2317:                serializer.setIndent(4);
2318:                serializer.write(doc);
2319:
2320:                serializer.flush();
2321:                String result = out.toString("UTF-8");
2322:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2323:                        + "<a>\r\n    <b> </b>\r\n</a>\r\n", result);
2324:
2325:            }
2326:
2327:            public void testElementsThatOnlyContainASingleLinefeedShouldNotBeSplitWhileIndenting()
2328:                    throws IOException {
2329:
2330:                Element root = new Element("a");
2331:                Element child = new Element("b");
2332:                child.appendChild("\n");
2333:                root.appendChild(child);
2334:
2335:                Document doc = new Document(root);
2336:
2337:                Serializer serializer = new Serializer(out);
2338:                serializer.setIndent(4);
2339:                serializer.write(doc);
2340:
2341:                serializer.flush();
2342:                String result = out.toString("UTF-8");
2343:                assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2344:                        + "<a>\r\n    <b> </b>\r\n</a>\r\n", result);
2345:
2346:            }
2347:
2348:            public void testEndTagsOfElementsWithContentGoOnSeparateLine()
2349:                    throws ParsingException, IOException {
2350:
2351:                Serializer serializer = new Serializer(out, "ISO-8859-1");
2352:                serializer.setIndent(4);
2353:                serializer.setPreserveBaseURI(true);
2354:                serializer.flush();
2355:
2356:                File f = new File("data");
2357:                f = new File(f, "prettyxml.xml");
2358:                Builder builder = new Builder();
2359:                Document doc = builder.build(f);
2360:                serializer.write(doc);
2361:                String result = out.toString("UTF-8");
2362:                assertTrue(result.endsWith("\r\n</html>\r\n"));
2363:
2364:            }
2365:
2366:            public void testDontDoubleBreak() throws ParsingException,
2367:                    IOException {
2368:
2369:                Serializer serializer = new Serializer(out, "ISO-8859-1");
2370:                serializer.setIndent(4);
2371:                serializer.setMaxLength(64);
2372:
2373:                File f = new File("data");
2374:                f = new File(f, "prettytest.xml");
2375:                Builder builder = new Builder();
2376:                Document doc = builder.build(f);
2377:                serializer.write(doc);
2378:                String result = out.toString("UTF-8");
2379:                assertEquals(
2380:                        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n"
2381:                                + "<html a=\"AReallyLongNameWithNoOpportunitiesToBreakToPutUsPastTheMaxLineLengthAndForceABreak\">\r\n"
2382:                                + "    <head> </head>\r\n" + "</html>\r\n",
2383:                        result);
2384:
2385:            }
2386:
2387:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.