1
2
3
4
5
6
7
8 package org.dom4j.io;
9
10 import junit.textui.TestRunner;
11
12 import java.io.StringReader;
13 import java.io.StringWriter;
14
15 import org.dom4j.AbstractTestCase;
16 import org.dom4j.Document;
17
18 /***
19 * A simple test harness to check that the XML Writer works
20 *
21 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
22 * @version $Revision: 1.3 $
23 */
24 public class WriteUnmergedTextTest extends AbstractTestCase {
25 protected static final boolean VERBOSE = true;
26
27 private String inputText = "<?xml version = \"1.0\"?>"
28 + "<TestEscapedEntities><TEXT>Test using < "
29 + "& ></TEXT></TestEscapedEntities>";
30
31 public static void main(String[] args) {
32 TestRunner.run(WriteUnmergedTextTest.class);
33 }
34
35
36
37 public String readwriteText(OutputFormat outFormat,
38 boolean mergeAdjacentText) throws Exception {
39 StringWriter out = new StringWriter();
40 StringReader in = new StringReader(inputText);
41 SAXReader reader = new SAXReader();
42
43
44 reader.setMergeAdjacentText(mergeAdjacentText);
45
46 Document document = reader.read(in);
47
48 XMLWriter writer = (outFormat == null) ? new XMLWriter(out)
49 : new XMLWriter(out, outFormat);
50 writer.write(document);
51 writer.close();
52
53 String outText = out.toString();
54
55 return outText;
56 }
57
58 public void testWithoutFormatNonMerged() throws Exception {
59 String outText = readwriteText(null, false);
60
61 if (VERBOSE) {
62 log("Text output is [");
63 log(outText);
64 log("]. Done");
65 }
66
67
68 assertTrue("Output text contains \"&\"", outText
69 .lastIndexOf("&") >= 0);
70 assertTrue("Output text contains \"<\"",
71 outText.lastIndexOf("<") >= 0);
72 }
73
74 public void testWithCompactFormatNonMerged() throws Exception {
75 String outText = readwriteText(OutputFormat.createCompactFormat(),
76 false);
77
78 if (VERBOSE) {
79 log("Text output is [");
80 log(outText);
81 log("]. Done");
82 }
83
84
85 assertTrue("Output text contains \"&\"", outText
86 .lastIndexOf("&") >= 0);
87 assertTrue("Output text contains \"<\"",
88 outText.lastIndexOf("<") >= 0);
89 }
90
91 public void testWithPrettyPrintFormatNonMerged() throws Exception {
92 String outText = readwriteText(OutputFormat.createPrettyPrint(), false);
93
94 if (VERBOSE) {
95 log("Text output is [");
96 log(outText);
97 log("]. Done");
98 }
99
100
101 assertTrue("Output text contains \"&\"", outText
102 .lastIndexOf("&") >= 0);
103 assertTrue("Output text contains \"<\"",
104 outText.lastIndexOf("<") >= 0);
105 }
106
107 public void testWithoutFormatMerged() throws Exception {
108 String outText = readwriteText(null, true);
109
110 if (VERBOSE) {
111 log("Text output is [");
112 log(outText);
113 log("]. Done");
114 }
115
116
117 assertTrue("Output text contains \"&\"", outText
118 .lastIndexOf("&") >= 0);
119 assertTrue("Output text contains \"<\"",
120 outText.lastIndexOf("<") >= 0);
121 }
122
123 public void testWithCompactFormatMerged() throws Exception {
124 String out = readwriteText(OutputFormat.createCompactFormat(), true);
125
126 if (VERBOSE) {
127 log("Text output is [");
128 log(out);
129 log("]. Done");
130 }
131
132
133 assertTrue("Output text contains \"&\"", out
134 .lastIndexOf("&") >= 0);
135 assertTrue("Output text contains \"<\"",
136 out.lastIndexOf("<") >= 0);
137 }
138
139 public void testWithPrettyPrintFormatMerged() throws Exception {
140 String outText = readwriteText(OutputFormat.createPrettyPrint(), true);
141
142 if (VERBOSE) {
143 log("Text output is [");
144 log(outText);
145 log("]. Done");
146 }
147
148
149 assertTrue("Output text contains \"&\"", outText
150 .lastIndexOf("&") >= 0);
151 assertTrue("Output text contains \"<\"",
152 outText.lastIndexOf("<") >= 0);
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