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