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: TestSTAX.java,v 1.1 2004/07/14 19:32:24 maartenc Exp $
8    */
9   
10  package org.dom4j.io;
11  
12  import java.io.File;
13  import java.io.FileReader;
14  import java.io.StringWriter;
15  import java.net.URL;
16  import javax.xml.stream.XMLInputFactory;
17  
18  import junit.framework.Test;
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  import junit.textui.TestRunner;
22  
23  import org.dom4j.Document;
24  import org.dom4j.io.STAXEventReader;
25  
26  /*** Tests STAX->DOM4J functionality.
27    *
28    * @author <a href="mailto:maartenc@sourceforge.net">Maarten Coene</a>
29    * @author Christian Niles
30    */
31  public class TestSTAX extends TestCase {
32  
33      public static void main( String[] args ) {
34          TestRunner.run( suite() );
35      }
36      
37      public static Test suite() {
38          return new TestSuite( TestSTAX.class );
39      }
40      
41      public TestSTAX(String name) {
42          super(name);
43      }
44      
45      public void setUp() throws Exception {
46      	super.setUp();
47      	
48  //    	System.setProperty(	"javax.xml.stream.XMLInputFactory",
49  //    	  					"com.ctc.wstx.stax.WstxInputFactory");
50  //    	System.setProperty(	"javax.xml.stream.XMLOutputFactory",
51  //    	  					"com.ctc.wstx.stax.WstxOutputFactory");
52  //    	System.setProperty( "javax.xml.stream.XMLEventFactory",
53  //    	  					"com.ctc.wstx.stax.evt.WstxEventFactory");
54      }
55      
56      // Test case(s)
57      //-------------------------------------------------------------------------
58  
59      /***
60       * Tests that the encoding specified in the XML declaration is exposed in the
61       * Document read via StAX, and also that it gets output when writing.
62       */    
63      public void testEncoding() {
64  
65          /*
66           * only execute if a reference implementation is available
67           */
68          try {
69              XMLInputFactory.newInstance();
70          } catch (javax.xml.stream.FactoryConfigurationError e) {
71              // no implementation found, stop the test.
72              return;
73          }
74  
75          try {
76              URL location = TestSTAX.class.getResource("/xml/russArticle.xml");
77              STAXEventReader xmlReader = new STAXEventReader(); 
78              Document doc = xmlReader.readDocument(location.openStream(), location.toString() );
79  
80              assertEquals("russArticle.xml encoding wasn't correct", "koi8-r", doc.getXMLEncoding());
81              
82              StringWriter writer = new StringWriter();
83              STAXEventWriter xmlWriter = new STAXEventWriter(writer);
84              xmlWriter.writeDocument(doc);
85              
86              String output = writer.toString();
87              String xmlDecl = output.substring(0, output.indexOf("?>") + 2);
88              assertEquals("Unexpected xml declaration", 
89                      "<?xml version=\'1.0\' encoding=\'koi8-r\'?>", xmlDecl);
90              System.out.println(output);
91          } catch (Exception e) {
92              e.printStackTrace();
93              fail(e.getMessage());
94          }
95      }
96      
97  }
98  
99  
100 
101 
102 /*
103  * Redistribution and use of this software and associated documentation
104  * ("Software"), with or without modification, are permitted provided
105  * that the following conditions are met:
106  *
107  * 1. Redistributions of source code must retain copyright
108  *    statements and notices.  Redistributions must also contain a
109  *    copy of this document.
110  *
111  * 2. Redistributions in binary form must reproduce the
112  *    above copyright notice, this list of conditions and the
113  *    following disclaimer in the documentation and/or other
114  *    materials provided with the distribution.
115  *
116  * 3. The name "DOM4J" must not be used to endorse or promote
117  *    products derived from this Software without prior written
118  *    permission of MetaStuff, Ltd.  For written permission,
119  *    please contact dom4j-info@metastuff.com.
120  *
121  * 4. Products derived from this Software may not be called "DOM4J"
122  *    nor may "DOM4J" appear in their names without prior written
123  *    permission of MetaStuff, Ltd. DOM4J is a registered
124  *    trademark of MetaStuff, Ltd.
125  *
126  * 5. Due credit should be given to the DOM4J Project - 
127  *    http://www.dom4j.org
128  *
129  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
130  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
131  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
132  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
133  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
134  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
135  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
136  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
137  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
138  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
139  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
140  * OF THE POSSIBILITY OF SUCH DAMAGE.
141  *
142  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
143  *
144  * $Id: TestSTAX.java,v 1.1 2004/07/14 19:32:24 maartenc Exp $
145  */