1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.io.StringWriter;
13
14 import org.dom4j.io.HTMLWriter;
15 import org.dom4j.io.OutputFormat;
16
17 /***
18 * Test harness for the HTMLWriter
19 *
20 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
21 * @version $Revision: 1.4 $
22 */
23 public class HTMLWriterTest extends AbstractTestCase {
24 public static void main(String[] args) {
25 TestRunner.run(HTMLWriterTest.class);
26 }
27
28
29
30 public void testWriter() throws Exception {
31 String xml = "<html> <body><![CDATA[First test]]></body> </html>";
32 Document document = DocumentHelper.parseText(xml);
33 StringWriter buffer = new StringWriter();
34 HTMLWriter writer = new HTMLWriter(buffer);
35 writer.write(document);
36
37 String output = buffer.toString();
38
39 String expects = "\n<html>\n <body>First test</body>\n</html>\n";
40
41 System.out.println("expects: " + expects);
42 System.out.println("output: " + output);
43
44 assertEquals("Output is correct", expects, output);
45 }
46
47 public void testBug923882() throws Exception {
48 Document doc = DocumentFactory.getInstance().createDocument();
49 Element root = doc.addElement("root");
50 root.addText("this is ");
51 root.addText(" sim");
52 root.addText("ple text ");
53 root.addElement("child");
54 root.addText(" contai");
55 root.addText("ning spaces and");
56 root.addText(" multiple textnodes");
57
58 OutputFormat format = new OutputFormat();
59 format.setEncoding("UTF-8");
60 format.setIndentSize(4);
61 format.setNewlines(true);
62 format.setTrimText(true);
63 format.setExpandEmptyElements(true);
64
65 StringWriter buffer = new StringWriter();
66 HTMLWriter writer = new HTMLWriter(buffer, format);
67 writer.write(doc);
68
69 String xml = buffer.toString();
70 log(xml);
71
72 int start = xml.indexOf("<root");
73 int end = xml.indexOf("/root>") + 6;
74 String eol = "\n";
75 String expected = "<root>this is simple text" + eol
76 + " <child></child>containing spaces and multiple textnodes"
77 + eol + "</root>";
78 System.out.println("Expected:");
79 System.out.println(expected);
80 System.out.println("Obtained:");
81 System.out.println(xml.substring(start, end));
82 assertEquals(expected, xml.substring(start, end));
83 }
84
85 public void testBug923882asWriter() throws Exception {
86
87
88 StringWriter buffer = new StringWriter();
89 HTMLWriter writer = new HTMLWriter(buffer, OutputFormat
90 .createPrettyPrint());
91 writer.characters("wor".toCharArray(), 0, 3);
92 writer.characters("d-being-cut".toCharArray(), 0, 11);
93
94 String expected = "word-being-cut";
95 assertEquals(expected, buffer.toString());
96
97 buffer = new StringWriter();
98 writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
99 writer.characters(" wor".toCharArray(), 0, 7);
100 writer.characters("d being ".toCharArray(), 0, 11);
101 writer.characters(" cut".toCharArray(), 0, 5);
102
103 expected = "word being cut";
104 assertEquals(expected, buffer.toString());
105 }
106
107 public void testBug923882asWriterWithEmptyCharArray() throws Exception {
108
109
110 StringWriter buffer = new StringWriter();
111 HTMLWriter writer = new HTMLWriter(buffer, OutputFormat
112 .createPrettyPrint());
113 writer.characters("wor".toCharArray(), 0, 3);
114 writer.characters(new char[0], 0, 0);
115 writer.characters("d-being-cut".toCharArray(), 0, 11);
116
117 String expected = "word-being-cut";
118 assertEquals(expected, buffer.toString());
119 }
120
121 public void testBug619415() throws Exception {
122 Document doc = getDocument("/xml/test/dosLineFeeds.xml");
123
124 StringWriter wr = new StringWriter();
125 HTMLWriter writer = new HTMLWriter(wr, new OutputFormat("", false));
126 writer.write(doc);
127
128 String result = wr.toString();
129 System.out.println(result);
130
131 assertTrue(result.indexOf("Mary had a little lamb.") > -1);
132 assertTrue(result.indexOf("Hello, this is a test.") > -1);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171