KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXMLSpaceAttribute


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
18
19 import java.io.IOException JavaDoc;
20
21 /** A test harness to test the xml:space attribute for preserve.
22   * If it is preserve, then keep whitespace.
23   *
24   * @author <a HREF="mailto:ddlucas@lse.com">David Lucas</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestXMLSpaceAttribute extends AbstractTestCase {
28
29     public static void main( String JavaDoc[] args ) {
30         TestRunner.run( suite() );
31     }
32
33     public static Test suite() {
34         return new TestSuite( TestXMLSpaceAttribute.class );
35     }
36
37     public TestXMLSpaceAttribute(String JavaDoc name) {
38         super(name);
39     }
40
41     // Test case(s)
42
//-------------------------------------------------------------------------
43
public void testWithTextTrimOn() throws Exception JavaDoc {
44       try {
45         String JavaDoc xmlString = "<top >" +
46           "<row><col> This is a test!</col></row>"+
47           "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+
48           "<row><col> This is a test!</col></row>"+
49                          "</top>";
50         Document doc1 = O3DocumentHelper.parseText(xmlString);
51         Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col");
52     String JavaDoc expected=" New Text TrimOn! ";
53         c2.setText(expected);
54
55         String JavaDoc xml = rewriteToXmlString(doc1,true);
56
57         Document doc2 = O3DocumentHelper.parseText(xml);
58         Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col");
59         String JavaDoc actual=c4.getText();
60
61         assertEquals("compared element text expecting whitespace",expected,actual);
62
63         expected = expected.trim();
64         actual=c4.getTextTrim();
65         assertEquals("compared element getTextTrim",expected,actual);
66
67         expected="This is a test!";
68         Element c5 = (Element)doc2.selectSingleNode("/top/row[3]/col");
69         actual=c5.getText();
70         assertEquals("compared element text expecting trimmed whitespace",
71                      expected,actual);
72       }
73       catch (Exception JavaDoc ex) {
74         ex.printStackTrace();
75         this.assertTrue(ex.getMessage(),false);
76       }
77     }
78
79
80     //-------------------------------------------------------------------------
81
public void testWithTextTrimOff() throws Exception JavaDoc {
82       try {
83         String JavaDoc xmlString = "<top >" +
84           "<row><col> This is a test!</col></row>"+
85           "<row><col xml:space=\'preserve\' > This is a test!</col></row>"+
86           "<row><col> This is a test!</col></row>"+
87                          "</top>";
88         Document doc1 = O3DocumentHelper.parseText(xmlString);
89         Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col");
90         String JavaDoc expected=" New Text TrimOff! ";
91         c2.setText(expected);
92         String JavaDoc xml = rewriteToXmlString(doc1,false);
93
94         Document doc2 = O3DocumentHelper.parseText(xml);
95         Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col");
96         String JavaDoc actual=c4.getText();
97
98         assertEquals("compared element text expecting whitespace",expected,actual);
99       }
100       catch (Exception JavaDoc ex) {
101         ex.printStackTrace();
102         this.assertTrue(ex.getMessage(),false);
103       }
104     }
105
106     //-------------------------------------------------------------------------
107
public void testWithTextTrimOnFollow() throws Exception JavaDoc {
108       try {
109         String JavaDoc xmlString = "<top >" +
110           "<row><col> This is a test!</col></row>"+
111           "<row>"+
112                "<col xml:space=\'preserve\' >"+
113                  "<a><b> This is embedded!</b></a>"+
114                  "<a><b> This is space=preserve too!</b></a>"+
115                "</col>"+
116           "</row>"+
117           "<row><col> This is a test!</col></row>"+
118                          "</top>";
119         Document doc1 = O3DocumentHelper.parseText(xmlString);
120         Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
121         String JavaDoc expected=" New Text TrimOnFollow! ";
122         c2.setText(expected);
123         String JavaDoc xml = rewriteToXmlString(doc1,true);
124
125         Document doc2 = O3DocumentHelper.parseText(xml);
126
127         Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b");
128         String JavaDoc actual=c4.getText();
129
130         assertEquals("compared element text expecting whitespace",expected,actual);
131
132         Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[2]/b");
133
134         expected=" This is space=preserve too!";
135         actual=c8.getText();
136         assertEquals("compared element text follow trimmed whitespace",expected,actual);
137
138         expected = expected.trim();
139         actual=c8.getTextTrim();
140         assertEquals("compared element getTextTrim",expected,actual);
141         Element c12=(Element)doc2.selectSingleNode("/top/row[3]/col");
142
143         expected="This is a test!";
144         actual=c12.getText();
145         assertEquals("compared element text follow trimmed whitespace",expected,actual);
146       }
147       catch (Exception JavaDoc ex) {
148         ex.printStackTrace();
149         this.assertTrue(ex.getMessage(),false);
150       }
151     }
152     //-------------------------------------------------------------------------
153
public void testWithTextTrimOnNested() throws Exception JavaDoc {
154       try {
155         String JavaDoc xmlString = "<top >" +
156           "<row><col> This is a test!</col></row>"+
157           "<row>"+
158                "<col xml:space='preserve' >"+
159                  "<a>"+
160                    "<b> This is embedded! </b>"+
161                    "<b xml:space='default' > This should do global default! </b>"+
162                    "<b> This is embedded! </b>"+
163                  "</a>"+
164                "</col>"+
165           "</row>"+
166           "<row><col> This is a test!</col></row>"+
167                          "</top>";
168         Document doc1 = O3DocumentHelper.parseText(xmlString);
169         Element c2=(Element)doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
170         String JavaDoc expected=" New Text TrimOnNested! ";
171         c2.setText(expected);
172         String JavaDoc xml = rewriteToXmlString(doc1,true);
173
174         Document doc2 = O3DocumentHelper.parseText(xml);
175
176         Element c4=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[1]");
177         String JavaDoc actual=c4.getText();
178         assertEquals("compared element text expecting whitespace",expected,actual);
179
180         Element c8=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[2]");
181         expected="This should do global default!";
182         actual=c8.getText();
183         assertEquals("compared element text nested trimmed whitespace",expected,actual);
184
185         Element c12=(Element)doc2.selectSingleNode("/top/row[2]/col/a[1]/b[3]");
186         expected=" This is embedded! ";
187         actual=c12.getText();
188         assertEquals("compared element text nested preserved whitespace",expected,actual);
189       }
190       catch (Exception JavaDoc ex) {
191         ex.printStackTrace();
192         this.assertTrue(ex.getMessage(),false);
193       }
194     }
195     // Implementation methods
196
//-------------------------------------------------------------------------
197

198     private String JavaDoc rewriteToXmlString(Document doc,boolean trimOn) throws IOException JavaDoc {
199       org.dom4j.io.OutputFormat of = org.dom4j.io.OutputFormat.createCompactFormat();
200       of.setIndent(true);
201       of.setNewlines(true);
202       of.setExpandEmptyElements(false);
203       of.setSuppressDeclaration(false);
204       of.setOmitEncoding(false);
205       of.setEncoding("UTF-8");
206       of.setTrimText(trimOn);
207       java.io.ByteArrayOutputStream JavaDoc os = new java.io.ByteArrayOutputStream JavaDoc();
208       java.io.BufferedOutputStream JavaDoc bos= new java.io.BufferedOutputStream JavaDoc(os);
209       org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(of);
210
211       xmlWriter.setOutputStream(bos);
212       xmlWriter.write(doc);
213       xmlWriter.close();
214       String JavaDoc xml = os.toString();
215
216       //System.out.println("***** xml out *****\n"+xml);
217

218       return xml;
219     }
220
221     //-------------------------------------------------------------------------
222
public void testWithEscapeTextTrimOn() throws Exception JavaDoc {
223       try {
224         String JavaDoc xmlString = "<top >" +
225           "<row><col> This is a test!</col></row>"+
226           "<row><col xml:space=\'preserve\' > This is a test!\r\nWith a new line, special character like &amp; , and\t tab.</col></row>"+
227           "<row><col> This is a test!\r\nWith a new line, special character like &amp; , and\t tab.</col></row>"+
228                          "</top>";
229         Document doc1 = O3DocumentHelper.parseText(xmlString);
230         String JavaDoc xml = rewriteToXmlString(doc1,true);
231         Document doc2 = O3DocumentHelper.parseText(xml);
232
233         Element c2=(Element)doc2.selectSingleNode("/top/row[2]/col");
234         String JavaDoc expected=" This is a test!\nWith a new line, special character like & , and\t tab.";
235         String JavaDoc actual=c2.getText();
236         assertEquals("compared element text expecting whitespace",expected,actual);
237
238         Element c4=(Element)doc2.selectSingleNode("/top/row[3]/col");
239         expected="This is a test! With a new line, special character like & , and tab.";
240         actual=c4.getText();
241         assertEquals("compared element text expecting whitespace",expected,actual);
242       }
243       catch (Exception JavaDoc ex) {
244         ex.printStackTrace();
245         this.assertTrue(ex.getMessage(),false);
246       }
247     }
248
249
250 }
251
252
253
254
255 /*
256  * Redistribution and use of this software and associated documentation
257  * ("Software"), with or without modification, are permitted provided
258  * that the following conditions are met:
259  *
260  * 1. Redistributions of source code must retain copyright
261  * statements and notices. Redistributions must also contain a
262  * copy of this document.
263  *
264  * 2. Redistributions in binary form must reproduce the
265  * above copyright notice, this list of conditions and the
266  * following disclaimer in the documentation and/or other
267  * materials provided with the distribution.
268  *
269  * 3. The name "DOM4J" must not be used to endorse or promote
270  * products derived from this Software without prior written
271  * permission of MetaStuff, Ltd. For written permission,
272  * please contact dom4j-info@metastuff.com.
273  *
274  * 4. Products derived from this Software may not be called "DOM4J"
275  * nor may "DOM4J" appear in their names without prior written
276  * permission of MetaStuff, Ltd. DOM4J is a registered
277  * trademark of MetaStuff, Ltd.
278  *
279  * 5. Due credit should be given to the DOM4J Project
280  * (http://dom4j.org/).
281  *
282  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
283  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
284  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
285  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
286  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
287  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
288  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
289  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
291  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
293  * OF THE POSSIBILITY OF SUCH DAMAGE.
294  *
295  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
296  *
297  * $Id: TestXMLSpaceAttribute.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
298  */

299
Popular Tags