1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import java.io.*;
16
17 /*** A test harness to test the xml:space attribute for preserve.
18 * If it is preserve, then keep whitespace.
19 *
20 * @author <a href="mailto:ddlucas@lse.com">David Lucas</a>
21 * @version $Revision: 1.3 $
22 */
23 public class TestXMLSpaceAttribute extends AbstractTestCase {
24
25 public static void main( String[] args ) {
26 TestRunner.run( suite() );
27 }
28
29 public static Test suite() {
30 return new TestSuite( TestXMLSpaceAttribute.class );
31 }
32
33 public TestXMLSpaceAttribute(String name) {
34 super(name);
35 }
36
37
38
39 public void testWithTextTrimOn() throws Exception {
40 try {
41 String xmlString = "<top >" +
42 "<row><col> This is a test!</col></row>"+
43 "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+
44 "<row><col> This is a test!</col></row>"+
45 "</top>";
46 Document doc1 = DocumentHelper.parseText(xmlString);
47 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col");
48 String expected=" New Text TrimOn! ";
49 c2.setText(expected);
50
51 String xml = rewriteToXmlString(doc1,true);
52
53 Document doc2 = DocumentHelper.parseText(xml);
54 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col");
55 String actual=c4.getText();
56
57 assertEquals("compared element text expecting whitespace",expected,actual);
58
59 expected = expected.trim();
60 actual=c4.getTextTrim();
61 assertEquals("compared element getTextTrim",expected,actual);
62
63 expected="This is a test!";
64 Element c5 = (Element)doc2.selectSingleNode("/top/row[3]/col");
65 actual=c5.getText();
66 assertEquals("compared element text expecting trimmed whitespace",
67 expected,actual);
68 }
69 catch (Exception ex) {
70 ex.printStackTrace();
71 this.assertTrue(ex.getMessage(),false);
72 }
73 }
74
75
76
77 public void testWithTextTrimOff() throws Exception {
78 try {
79 String xmlString = "<top >" +
80 "<row><col> This is a test!</col></row>"+
81 "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+
82 "<row><col> This is a test!</col></row>"+
83 "</top>";
84 Document doc1 = DocumentHelper.parseText(xmlString);
85 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col");
86 String expected=" New Text TrimOff! ";
87 c2.setText(expected);
88 String xml = rewriteToXmlString(doc1,false);
89
90 Document doc2 = DocumentHelper.parseText(xml);
91 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col");
92 String actual=c4.getText();
93
94 assertEquals("compared element text expecting whitespace",expected,actual);
95 }
96 catch (Exception ex) {
97 ex.printStackTrace();
98 this.assertTrue(ex.getMessage(),false);
99 }
100 }
101
102
103 public void testWithTextTrimOnFollow() throws Exception {
104 try {
105 String xmlString = "<top >" +
106 "<row><col> This is a test!</col></row>"+
107 "<row>"+
108 "<col xml:space=\'preserve\' >"+
109 "<a><b> This is embedded!</b></a>"+
110 "<a><b> This is space=preserve too!</b></a>"+
111 "</col>"+
112 "</row>"+
113 "<row><col> This is a test!</col></row>"+
114 "</top>";
115 Document doc1 = DocumentHelper.parseText(xmlString);
116 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
117 String expected=" New Text TrimOnFollow! ";
118 c2.setText(expected);
119 String xml = rewriteToXmlString(doc1,true);
120
121 Document doc2 = DocumentHelper.parseText(xml);
122
123 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b");
124 String actual=c4.getText();
125
126 assertEquals("compared element text expecting whitespace",expected,actual);
127
128 Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[2]/b");
129
130 expected=" This is space=preserve too!";
131 actual=c8.getText();
132 assertEquals("compared element text follow trimmed whitespace",expected,actual);
133
134 expected = expected.trim();
135 actual=c8.getTextTrim();
136 assertEquals("compared element getTextTrim",expected,actual);
137 Element c12=(Element)doc2.selectSingleNode("/top/row[3]/col");
138
139 expected="This is a test!";
140 actual=c12.getText();
141 assertEquals("compared element text follow trimmed whitespace",expected,actual);
142 }
143 catch (Exception ex) {
144 ex.printStackTrace();
145 this.assertTrue(ex.getMessage(),false);
146 }
147 }
148
149 public void testWithTextTrimOnNested() throws Exception {
150 try {
151 String xmlString = "<top >" +
152 "<row><col> This is a test!</col></row>"+
153 "<row>"+
154 "<col xml:space='preserve' >"+
155 "<a>"+
156 "<b> This is embedded! </b>"+
157 "<b xml:space='default' > This should do global default! </b>"+
158 "<b> This is embedded! </b>"+
159 "</a>"+
160 "</col>"+
161 "</row>"+
162 "<row><col> This is a test!</col></row>"+
163 "</top>";
164 Document doc1 = DocumentHelper.parseText(xmlString);
165 Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
166 String expected=" New Text TrimOnNested! ";
167 c2.setText(expected);
168 String xml = rewriteToXmlString(doc1,true);
169
170 Document doc2 = DocumentHelper.parseText(xml);
171
172 Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[1]");
173 String actual=c4.getText();
174 assertEquals("compared element text expecting whitespace",expected,actual);
175
176 Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[2]");
177 expected="This should do global default!";
178 actual=c8.getText();
179 assertEquals("compared element text nested trimmed whitespace",expected,actual);
180
181 Element c12=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[3]");
182 expected=" This is embedded! ";
183 actual=c12.getText();
184 assertEquals("compared element text nested preserved whitespace",expected,actual);
185 }
186 catch (Exception ex) {
187 ex.printStackTrace();
188 this.assertTrue(ex.getMessage(),false);
189 }
190 }
191
192
193
194 private String rewriteToXmlString(Document doc,boolean trimOn) throws IOException {
195 org.dom4j.io.OutputFormat of = org.dom4j.io.OutputFormat.createCompactFormat();
196 of.setIndent(true);
197 of.setNewlines(true);
198 of.setExpandEmptyElements(false);
199 of.setSuppressDeclaration(false);
200 of.setOmitEncoding(false);
201 of.setEncoding("UTF-8");
202 of.setTrimText(trimOn);
203 java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();
204 java.io.BufferedOutputStream bos= new java.io.BufferedOutputStream(os);
205 org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(of);
206
207 xmlWriter.setOutputStream(bos);
208 xmlWriter.write(doc);
209 xmlWriter.close();
210 String xml = os.toString();
211
212
213
214 return xml;
215 }
216
217
218 public void testWithEscapeTextTrimOn() throws Exception {
219 try {
220 String xmlString = "<top >" +
221 "<row><col> This is a test!</col></row>"+
222 "<row><col xml:space=\'preserve\' > This is a test!\r\nWith a new line, special character like & , and\t tab.</col></row>"+
223 "<row><col> This is a test!\r\nWith a new line, special character like & , and\t tab.</col></row>"+
224 "</top>";
225 Document doc1 = DocumentHelper.parseText(xmlString);
226 String xml = rewriteToXmlString(doc1,true);
227 Document doc2 = DocumentHelper.parseText(xml);
228
229 Element c2=(Element)doc2.selectSingleNode("/top/row[2]/col");
230 String expected=" This is a test!\nWith a new line, special character like & , and\t tab.";
231 String actual=c2.getText();
232 assertEquals("compared element text expecting whitespace",expected,actual);
233
234 Element c4=(Element)doc2.selectSingleNode("/top/row[3]/col");
235 expected="This is a test! With a new line, special character like & , and tab.";
236 actual=c4.getText();
237 assertEquals("compared element text expecting whitespace",expected,actual);
238 }
239 catch (Exception ex) {
240 ex.printStackTrace();
241 this.assertTrue(ex.getMessage(),false);
242 }
243 }
244
245
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294