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: TestDefaultDocument.java,v 1.5 2004/07/11 10:49:38 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.ByteArrayOutputStream;
14  import java.io.StringReader;
15  import junit.framework.Test;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  import org.dom4j.AbstractTestCase;
19  import org.dom4j.Document;
20  import org.dom4j.DocumentFactory;
21  import org.dom4j.DocumentHelper;
22  import org.dom4j.Element;
23  import org.dom4j.IllegalAddException;
24  import org.dom4j.io.OutputFormat;
25  import org.dom4j.io.SAXReader;
26  import org.dom4j.io.XMLWriter;
27  import org.xml.sax.InputSource;
28  
29  /*** A test harness to test the addAttribute() methods on attributes
30    *
31    * @author <a href="mailto:maartenc@users.sourceforge.net">Maarten Coene</a>
32    */
33  public class TestDefaultDocument extends AbstractTestCase {
34  
35      public static void main( String[] args ) {
36          TestRunner.run( suite() );
37      }
38      
39      public static Test suite() {
40          return new TestSuite( TestDefaultDocument.class );
41      }
42      
43      public TestDefaultDocument(String name) {
44          super(name);
45      }
46  
47      // Test case(s)
48      //-------------------------------------------------------------------------
49      public void testDoubleRootElement() {
50          Document document = DocumentFactory.getInstance().createDocument();
51          document.addElement("root");
52          
53          Element root = DocumentFactory.getInstance().createElement("anotherRoot");
54          try {
55              document.add(root);
56              fail();
57          } catch (IllegalAddException e) {
58              String msg = e.getMessage();
59              assertTrue(msg.indexOf(root.toString()) != -1);
60          }
61      }
62      
63      public void testBug799656() throws Exception {
64          Document document = DocumentFactory.getInstance().createDocument();
65          Element el = document.addElement("root");
66          el.setText("text with an \u00FC in it");  // u00FC is umlaut
67          
68          System.out.println(document.asXML());
69          
70          DocumentHelper.parseText(document.asXML());
71      }
72      
73      public void testEncoding() throws Exception {
74          Document document = DocumentFactory.getInstance().createDocument();
75          Element el = document.addElement("root");
76          el.setText("text with an \u00FC in it");  // u00FC is umlaut
77          
78          ByteArrayOutputStream out = new ByteArrayOutputStream();
79          OutputFormat of = OutputFormat.createPrettyPrint();
80          of.setEncoding("koi8-r");
81          XMLWriter writer = new XMLWriter(out, of);
82          writer.write(document);
83          
84          String result = out.toString();
85          
86          System.out.println(result);
87          
88          DocumentHelper.parseText(result);
89      }
90      
91  }
92  
93  
94  
95  
96  /*
97   * Redistribution and use of this software and associated documentation
98   * ("Software"), with or without modification, are permitted provided
99   * that the following conditions are met:
100  *
101  * 1. Redistributions of source code must retain copyright
102  *    statements and notices.  Redistributions must also contain a
103  *    copy of this document.
104  *
105  * 2. Redistributions in binary form must reproduce the
106  *    above copyright notice, this list of conditions and the
107  *    following disclaimer in the documentation and/or other
108  *    materials provided with the distribution.
109  *
110  * 3. The name "DOM4J" must not be used to endorse or promote
111  *    products derived from this Software without prior written
112  *    permission of MetaStuff, Ltd.  For written permission,
113  *    please contact dom4j-info@metastuff.com.
114  *
115  * 4. Products derived from this Software may not be called "DOM4J"
116  *    nor may "DOM4J" appear in their names without prior written
117  *    permission of MetaStuff, Ltd. DOM4J is a registered
118  *    trademark of MetaStuff, Ltd.
119  *
120  * 5. Due credit should be given to the DOM4J Project - 
121  *    http://www.dom4j.org
122  *
123  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
124  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
125  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
126  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
127  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
128  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
129  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
130  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
131  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
132  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
133  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
134  * OF THE POSSIBILITY OF SUCH DAMAGE.
135  *
136  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
137  *
138  * $Id: TestDefaultDocument.java,v 1.5 2004/07/11 10:49:38 maartenc Exp $
139  */