KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXMLWriter


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: TestXMLWriter.java,v 1.2 2003/11/02 18:31:28 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.dom4j.io.OutputFormat;
18 import org.dom4j.io.SAXReader;
19 import org.dom4j.io.XMLWriter;
20 import org.dom4j.tree.BaseElement;
21 import org.dom4j.tree.DefaultDocument;
22 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.StringWriter JavaDoc;
30
31 /** A simple test harness to check that the XML Writer works
32   *
33   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
34   * @version $Revision: 1.2 $
35   */

36 public class TestXMLWriter extends AbstractTestCase {
37
38     protected static final boolean VERBOSE = false;
39
40
41     public static void main( String JavaDoc[] args ) {
42         TestRunner.run( suite() );
43     }
44
45     public static Test suite() {
46         return new TestSuite( TestXMLWriter.class );
47     }
48
49     public TestXMLWriter(String JavaDoc name) {
50         super(name);
51     }
52
53     // Test case(s)
54
//-------------------------------------------------------------------------
55
public void testWriter() throws Exception JavaDoc {
56         Object JavaDoc object = document;
57         StringWriter JavaDoc out = new StringWriter JavaDoc();
58
59         XMLWriter writer = new XMLWriter( out );
60         writer.write( object );
61         writer.close();
62
63         String JavaDoc text = out.toString();
64
65         if ( VERBOSE ) {
66             log( "Text output is [" );
67             log( text );
68             log( "]. Done" );
69         }
70
71         assertTrue( "Output text is bigger than 10 characters", text.length() > 10 );
72     }
73
74     public void testEncodingFormats() throws Exception JavaDoc {
75         testEncoding( "UTF-8" );
76         testEncoding( "UTF-16" );
77         testEncoding( "ISO-8859-1" );
78     }
79
80     protected void testEncoding(String JavaDoc encoding) throws Exception JavaDoc {
81         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
82
83         OutputFormat format = OutputFormat.createPrettyPrint();
84         format.setEncoding( encoding );
85         XMLWriter writer = new XMLWriter( out, format );
86         writer.write( document );
87         writer.close();
88
89         log( "Wrote to encoding: " + encoding );
90     }
91
92     public void testWriterBug() throws Exception JavaDoc {
93         Element project = new BaseElement("project");
94         Document doc = new DefaultDocument(project);
95
96         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
97         XMLWriter writer = new XMLWriter(out, new OutputFormat("\t", true, "ISO-8859-1"));
98         writer.write(doc);
99
100         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( out.toByteArray() );
101         SAXReader reader = new SAXReader();
102         Document doc2 = reader.read( in );
103
104         assertTrue( "Generated document has a root element", doc2.getRootElement() != null );
105         assertEquals( "Generated document has corrent named root element", doc2.getRootElement().getName(), "project" );
106     }
107
108     public void testNamespaceBug() throws Exception JavaDoc {
109         Document doc = O3DocumentHelper.createDocument();
110
111         Element root = doc.addElement("root","ns1");
112         Element child1 = root.addElement("joe","ns2");
113         child1.addElement("zot","ns1");
114
115         StringWriter JavaDoc out = new StringWriter JavaDoc();
116         XMLWriter writer = new XMLWriter(
117             out,
118             OutputFormat.createPrettyPrint()
119         );
120         writer.write(doc);
121         String JavaDoc text = out.toString();
122
123         //System.out.println( "Generated:" + text );
124

125         Document doc2 = O3DocumentHelper.parseText( text );
126         root = doc2.getRootElement();
127         assertEquals( "root has correct namespace", "ns1", root.getNamespaceURI() );
128
129         Element joe = (Element) root.elementIterator().next();
130         assertEquals( "joe has correct namespace", "ns2", joe.getNamespaceURI() );
131
132         Element zot = (Element) joe.elementIterator().next();
133         assertEquals( "zot has correct namespace", "ns1", zot.getNamespaceURI() );
134     }
135
136     /** This test harness was supplied by Lari Hotari */
137     public void testContentHandler() throws Exception JavaDoc {
138         StringWriter JavaDoc out = new StringWriter JavaDoc();
139     OutputFormat format = OutputFormat.createPrettyPrint();
140     format.setEncoding("iso-8859-1");
141     XMLWriter writer = new XMLWriter(out, format);
142         generateXML(writer);
143     writer.close();
144     String JavaDoc text = out.toString();
145
146         if ( VERBOSE ) {
147             log( "Created XML" );
148             log( text );
149         }
150
151         // now lets parse the output and test it with XPath
152
Document doc = O3DocumentHelper.parseText( text );
153         String JavaDoc value = doc.valueOf( "/processes[@name='arvojoo']" );
154         assertEquals( "Document contains the correct text", "jeejee", value );
155     }
156
157     /** This test was provided by Manfred Lotz */
158     public void testWhitespaceBug() throws Exception JavaDoc {
159         Document doc = O3DocumentHelper.parseText(
160             "<notes> This is a multiline\n\rentry</notes>"
161         );
162
163         OutputFormat format = new OutputFormat();
164         format.setEncoding("UTF-8");
165         format.setIndentSize(4);
166         format.setNewlines(true);
167         format.setTrimText(true);
168         format.setExpandEmptyElements(true);
169
170         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
171         XMLWriter writer = new XMLWriter(buffer, format);
172         writer.write( doc );
173
174         String JavaDoc xml = buffer.toString();
175         log( xml );
176
177         Document doc2 = O3DocumentHelper.parseText( xml );
178         String JavaDoc text = doc2.valueOf( "/notes" );
179         String JavaDoc expected = "This is a multiline entry";
180
181         assertEquals( "valueOf() returns the correct text padding", expected, text );
182
183         assertEquals( "getText() returns the correct text padding", expected, doc2.getRootElement().getText() );
184     }
185
186     /** This test was provided by Manfred Lotz */
187     public void testWhitespaceBug2() throws Exception JavaDoc {
188         Document doc = O3DocumentHelper.createDocument();
189         Element root = doc.addElement( "root" );
190         Element meaning = root.addElement( "meaning" );
191         meaning.addText( "to li" );
192         meaning.addText( "ve" );
193
194         OutputFormat format = new OutputFormat();
195         format.setEncoding("UTF-8");
196         format.setIndentSize(4);
197         format.setNewlines(true);
198         format.setTrimText(true);
199         format.setExpandEmptyElements(true);
200
201         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
202         XMLWriter writer = new XMLWriter(buffer, format);
203         writer.write( doc );
204
205         String JavaDoc xml = buffer.toString();
206         log( xml );
207
208         Document doc2 = O3DocumentHelper.parseText( xml );
209         String JavaDoc text = doc2.valueOf( "/root/meaning" );
210         String JavaDoc expected = "to live";
211
212         assertEquals( "valueOf() returns the correct text padding", expected, text );
213
214         assertEquals( "getText() returns the correct text padding", expected, doc2.getRootElement().element("meaning").getText() );
215     }
216
217     /*
218      * This must be tested manually to see if the layout is correct.
219      */

220     public void testPrettyPrinting() throws Exception JavaDoc {
221         Document doc = nodeFactory.createDocument();
222         doc.addElement("summary").addAttribute("date", "6/7/8").addElement("orderline").addText("puffins").addElement("ranjit").addComment("Ranjit is a happy Puffin");
223         XMLWriter writer = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
224         writer.write(doc);
225
226         doc = nodeFactory.createDocument();
227         doc.addElement("summary").addAttribute("date", "6/7/8").addElement("orderline").addText("puffins").addElement("ranjit").addComment("Ranjit is a happy Puffin").addComment("another comment").addElement("anotherElement");
228         writer.write(doc);
229     }
230
231
232     protected void generateXML(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
233     handler.startDocument();
234     AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
235     attrs.clear();
236     attrs.addAttribute("","","name","CDATA", "arvojoo");
237     handler.startElement("","","processes",attrs);
238     String JavaDoc text="jeejee";
239     char textch[] = text.toCharArray();
240     handler.characters(textch,0,textch.length);
241     handler.endElement("","","processes" );
242     handler.endDocument();
243     }
244 }
245
246
247
248
249 /*
250  * Redistribution and use of this software and associated documentation
251  * ("Software"), with or without modification, are permitted provided
252  * that the following conditions are met:
253  *
254  * 1. Redistributions of source code must retain copyright
255  * statements and notices. Redistributions must also contain a
256  * copy of this document.
257  *
258  * 2. Redistributions in binary form must reproduce the
259  * above copyright notice, this list of conditions and the
260  * following disclaimer in the documentation and/or other
261  * materials provided with the distribution.
262  *
263  * 3. The name "DOM4J" must not be used to endorse or promote
264  * products derived from this Software without prior written
265  * permission of MetaStuff, Ltd. For written permission,
266  * please contact dom4j-info@metastuff.com.
267  *
268  * 4. Products derived from this Software may not be called "DOM4J"
269  * nor may "DOM4J" appear in their names without prior written
270  * permission of MetaStuff, Ltd. DOM4J is a registered
271  * trademark of MetaStuff, Ltd.
272  *
273  * 5. Due credit should be given to the DOM4J Project
274  * (http://dom4j.org/).
275  *
276  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
277  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
278  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
279  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
280  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
281  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
282  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
283  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
285  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
287  * OF THE POSSIBILITY OF SUCH DAMAGE.
288  *
289  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
290  *
291  * $Id: TestXMLWriter.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
292  */

293
Popular Tags