KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > tree > DefaultDocumentTest


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j.tree;
9
10 import junit.textui.TestRunner;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13
14 import org.dom4j.AbstractTestCase;
15 import org.dom4j.Document;
16 import org.dom4j.DocumentFactory;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.Element;
19 import org.dom4j.IllegalAddException;
20 import org.dom4j.io.OutputFormat;
21 import org.dom4j.io.XMLWriter;
22
23 /**
24  * Some tests on DefaultDocument.
25  *
26  * @author <a HREF="mailto:maartenc@users.sourceforge.net">Maarten Coene </a>
27  */

28 public class DefaultDocumentTest extends AbstractTestCase {
29     public static void main(String JavaDoc[] args) {
30         TestRunner.run(DefaultDocumentTest.class);
31     }
32
33     // Test case(s)
34
// -------------------------------------------------------------------------
35
public void testDoubleRootElement() {
36         Document document = DocumentFactory.getInstance().createDocument();
37         document.addElement("root");
38
39         Element root = DocumentFactory.getInstance().createElement(
40                 "anotherRoot");
41
42         try {
43             document.add(root);
44             fail();
45         } catch (IllegalAddException e) {
46             String JavaDoc msg = e.getMessage();
47             assertTrue(msg.indexOf(root.toString()) != -1);
48         }
49     }
50
51     public void testBug799656() throws Exception JavaDoc {
52         Document document = DocumentFactory.getInstance().createDocument();
53         Element el = document.addElement("root");
54         el.setText("text with an \u00FC in it"); // u00FC is umlaut
55

56         System.out.println(document.asXML());
57
58         DocumentHelper.parseText(document.asXML());
59     }
60
61     public void testAsXMLWithEncoding() throws Exception JavaDoc {
62         DefaultDocument document = new DefaultDocument();
63         document.addElement("root");
64         document.setXMLEncoding("ISO-8859-1");
65
66         Document doc = DocumentHelper.parseText("<?xml version='1.0' "
67                 + "encoding='ISO-8859-1'?><root/>");
68
69         String JavaDoc xml1 = document.asXML();
70         String JavaDoc xml2 = doc.asXML();
71
72         assertTrue(xml1.indexOf("ISO-8859-1") != -1);
73         assertTrue(xml2.indexOf("ISO-8859-1") != -1);
74     }
75
76     public void testBug1156909() throws Exception JavaDoc {
77         Document doc = DocumentHelper.parseText("<?xml version='1.0' "
78                 + "encoding='ISO-8859-1'?><root/>");
79
80         assertEquals("XMLEncoding not correct", "ISO-8859-1", doc
81                 .getXMLEncoding());
82     }
83
84     public void testAsXMLWithEncodingAndContent() throws Exception JavaDoc {
85         DefaultDocument document = new DefaultDocument();
86         document.setXMLEncoding("UTF-16");
87         Element root = document.addElement("root");
88         root.setText("text with an \u00FC in it"); // u00FC is umlaut
89

90         String JavaDoc xml = document.asXML();
91         assertTrue(xml.indexOf("UTF-16") != -1);
92         assertTrue(xml.indexOf('\u00FC') != -1);
93     }
94
95     public void testEncoding() throws Exception JavaDoc {
96         Document document = DocumentFactory.getInstance().createDocument(
97                 "koi8-r");
98         Element el = document.addElement("root");
99         el.setText("text with an \u00FC in it"); // u00FC is umlaut
100

101         System.out.println(document.asXML());
102
103         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
104         OutputFormat of = OutputFormat.createPrettyPrint();
105         of.setEncoding("koi8-r");
106
107         XMLWriter writer = new XMLWriter(out, of);
108         writer.write(document);
109
110         String JavaDoc result = out.toString("koi8-r");
111         System.out.println(result);
112
113         Document doc2 = DocumentHelper.parseText(result);
114         // System.out.println(doc2.asXML());
115

116     }
117 }
118
119 /*
120  * Redistribution and use of this software and associated documentation
121  * ("Software"), with or without modification, are permitted provided that the
122  * following conditions are met:
123  *
124  * 1. Redistributions of source code must retain copyright statements and
125  * notices. Redistributions must also contain a copy of this document.
126  *
127  * 2. Redistributions in binary form must reproduce the above copyright notice,
128  * this list of conditions and the following disclaimer in the documentation
129  * and/or other materials provided with the distribution.
130  *
131  * 3. The name "DOM4J" must not be used to endorse or promote products derived
132  * from this Software without prior written permission of MetaStuff, Ltd. For
133  * written permission, please contact dom4j-info@metastuff.com.
134  *
135  * 4. Products derived from this Software may not be called "DOM4J" nor may
136  * "DOM4J" appear in their names without prior written permission of MetaStuff,
137  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
138  *
139  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
140  *
141  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
142  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
143  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
144  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
145  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
146  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
147  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
148  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
149  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
150  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
151  * POSSIBILITY OF SUCH DAMAGE.
152  *
153  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
154  */

155
Popular Tags