1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.io.StringWriter;
13 import java.net.URL;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18
19 import org.dom4j.io.HTMLWriter;
20 import org.dom4j.io.OutputFormat;
21 import org.dom4j.io.SAXReader;
22
23 /*** Test harness for the HTMLWriter
24 *
25 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
26 * @version $Revision: 1.10 $
27 */
28 public class TestHTMLWriter extends AbstractTestCase {
29
30 protected static final boolean VERBOSE = false;
31
32
33 public static void main( String[] args ) {
34 TestRunner.run( suite() );
35 }
36
37 public static Test suite() {
38 return new TestSuite( TestHTMLWriter.class );
39 }
40
41 public TestHTMLWriter(String name) {
42 super(name);
43 }
44
45
46
47 public void testWriter() throws Exception {
48 Document document = DocumentHelper.parseText(
49 "<html> <body><![CDATA[First test]]></body> </html>"
50 );
51 StringWriter buffer = new StringWriter();
52 HTMLWriter writer = new HTMLWriter( buffer );
53 writer.write( document );
54
55 String output = buffer.toString();
56
57 String expects = "\n<html>\n <body>First test</body>\n</html>\n";
58
59 System.out.println("expects: " + expects);
60 System.out.println("output: " + output);
61
62 assertEquals( "Output is correct", expects, output );
63 }
64
65 public void testBug923882() throws Exception {
66 Document doc = DocumentFactory.getInstance().createDocument();
67 Element root = doc.addElement("root");
68 root.addText("this is ");
69 root.addText(" sim");
70 root.addText("ple text ");
71 root.addElement("child");
72 root.addText(" contai");
73 root.addText("ning spaces and");
74 root.addText(" multiple textnodes");
75 OutputFormat format = new OutputFormat();
76 format.setEncoding("UTF-8");
77 format.setIndentSize(4);
78 format.setNewlines(true);
79 format.setTrimText(true);
80 format.setExpandEmptyElements(true);
81 StringWriter buffer = new StringWriter();
82 HTMLWriter writer = new HTMLWriter(buffer, format);
83 writer.write( doc );
84 String xml = buffer.toString();
85 log( xml );
86 int start = xml.indexOf("<root"),
87 end = xml.indexOf("/root>")+6;
88 String eol = "\n";
89 String expected =
90 "<root>this is simple text" + eol +
91 " <child></child>containing spaces and multiple textnodes" + eol +
92 "</root>";
93 System.out.println("Expected:"); System.out.println(expected);
94 System.out.println("Obtained:"); System.out.println(xml.substring(start, end));
95 assertEquals(expected, xml.substring(start, end));
96 }
97
98 public void testBug923882asWriter() throws Exception {
99
100
101 StringWriter buffer = new StringWriter();
102 HTMLWriter writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
103 writer.characters("wor".toCharArray(), 0, 3);
104 writer.characters("d-being-cut".toCharArray(), 0, 11);
105
106 String expected = "word-being-cut";
107 assertEquals(expected, buffer.toString());
108
109 buffer = new StringWriter();
110 writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
111 writer.characters(" wor".toCharArray(), 0, 7);
112 writer.characters("d being ".toCharArray(), 0, 11);
113 writer.characters(" cut".toCharArray(), 0, 5);
114
115 expected = "word being cut";
116 assertEquals(expected, buffer.toString());
117 }
118
119 public void testBug923882asWriterWithEmptyCharArray() throws Exception {
120
121
122 StringWriter buffer = new StringWriter();
123 HTMLWriter writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
124 writer.characters("wor".toCharArray(), 0, 3);
125 writer.characters(new char[0], 0, 0);
126 writer.characters("d-being-cut".toCharArray(), 0, 11);
127
128 String expected = "word-being-cut";
129 assertEquals(expected, buffer.toString());
130 }
131
132 public void testBug619415() throws Exception {
133 URL url = getClass().getResource("/xml/test/dosLineFeeds.xml");
134 SAXReader reader = new SAXReader();
135 Document doc = reader.read(url);
136
137 StringWriter wr = new StringWriter();
138 HTMLWriter writer = new HTMLWriter(wr, new OutputFormat("", false));
139 writer.write(doc);
140
141 String result = wr.toString();
142 System.out.println(result);
143
144 assertTrue(result.indexOf("Mary had a little lamb.") > -1);
145 assertTrue(result.indexOf("Hello, this is a test.") > -1);
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196