1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    *
7    * $Id: TestXMLSpaceAttribute.java,v 1.3 2004/06/25 08:03:47 maartenc Exp $
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      // Test case(s)
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     // Implementation methods
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       //System.out.println("***** xml out *****\n"+xml);
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 &amp; , and\t tab.</col></row>"+
223           "<row><col>   This is a test!\r\nWith a new line, special character like &amp; , 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  * Redistribution and use of this software and associated documentation
253  * ("Software"), with or without modification, are permitted provided
254  * that the following conditions are met:
255  *
256  * 1. Redistributions of source code must retain copyright
257  *    statements and notices.  Redistributions must also contain a
258  *    copy of this document.
259  *
260  * 2. Redistributions in binary form must reproduce the
261  *    above copyright notice, this list of conditions and the
262  *    following disclaimer in the documentation and/or other
263  *    materials provided with the distribution.
264  *
265  * 3. The name "DOM4J" must not be used to endorse or promote
266  *    products derived from this Software without prior written
267  *    permission of MetaStuff, Ltd.  For written permission,
268  *    please contact dom4j-info@metastuff.com.
269  *
270  * 4. Products derived from this Software may not be called "DOM4J"
271  *    nor may "DOM4J" appear in their names without prior written
272  *    permission of MetaStuff, Ltd. DOM4J is a registered
273  *    trademark of MetaStuff, Ltd.
274  *
275  * 5. Due credit should be given to the DOM4J Project - 
276  *    http://www.dom4j.org
277  *
278  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
279  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
280  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
281  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
282  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
283  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
284  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
285  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
287  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
289  * OF THE POSSIBILITY OF SUCH DAMAGE.
290  *
291  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
292  *
293  * $Id: TestXMLSpaceAttribute.java,v 1.3 2004/06/25 08:03:47 maartenc Exp $
294  */