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