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: TestRoundTrip.java,v 1.17 2004/08/03 20:08:49 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.io.BufferedInputStream;
13  import java.io.File;
14  import java.io.FileInputStream;
15  import java.io.InputStream;
16  import java.io.StringReader;
17  import java.io.StringWriter;
18  import java.net.URL;
19  
20  import javax.xml.transform.Transformer;
21  import javax.xml.transform.TransformerFactory;
22  import javax.xml.transform.stream.StreamResult;
23  import javax.xml.transform.stream.StreamSource;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  import junit.textui.TestRunner;
28  
29  import org.dom4j.io.DOMReader;
30  import org.dom4j.io.DOMWriter;
31  import org.dom4j.io.DocumentResult;
32  import org.dom4j.io.DocumentSource;
33  import org.dom4j.io.SAXContentHandler;
34  import org.dom4j.io.SAXReader;
35  import org.dom4j.io.SAXWriter;
36  import org.dom4j.io.XMLWriter;
37  
38  /*** A test harness to test the the round trips of Documents.
39    *
40    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
41    * @version $Revision: 1.17 $
42    */
43  public class TestRoundTrip extends AbstractTestCase {
44      
45      protected String[] testDocuments = {
46          "/xml/test/encode.xml",
47          "/xml/fibo.xml",
48          "/xml/test/schema/personal-prefix.xsd",
49          "/xml/test/soap2.xml",
50          "/xml/test/test_schema.xml",
51      };
52      
53      public static void main( String[] args ) {
54          TestRunner.run( suite() );
55      }
56      
57      public static Test suite() {
58          return new TestSuite( TestRoundTrip.class );
59      }
60      
61      public TestRoundTrip(String name) {
62          super(name);
63      }
64  
65      // Test case(s)
66      //-------------------------------------------------------------------------                    
67      public void testTextRoundTrip() throws Exception {
68          for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
69              Document doc = parseDocument( testDocuments[i] );
70              roundTripText( doc );
71          }
72      }
73      
74      public void testSAXRoundTrip() throws Exception {
75          for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
76              Document doc = parseDocument( testDocuments[i] );
77              roundTripSAX( doc );
78          }
79      }
80      
81      public void testDOMRoundTrip() throws Exception {
82          for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
83              Document doc = parseDocument( testDocuments[i] );
84              roundTripDOM( doc );
85          }
86      }
87      
88      public void testJAXPRoundTrip() throws Exception {
89          for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
90              Document doc = parseDocument( testDocuments[i] );
91              roundTripJAXP( doc );
92          }
93      }
94      
95      public void testFullRoundTrip() throws Exception {        
96          for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
97              Document doc = parseDocument( testDocuments[i] );
98              roundTripFull( doc );
99          }
100     }
101 
102     public void testRoundTrip() throws Exception {
103     	SAXReader reader = new SAXReader();
104     	Document document = reader.read(getClass().getResource("/xml/xmlspec.xml"));
105   
106         //Document doc1 = roundTripText( document );
107         Document doc1 = roundTripSAX( document );
108         Document doc2 = roundTripDOM( doc1);
109         Document doc3 = roundTripSAX( doc2 );
110         //Document doc4 = roundTripText( doc3 );
111         //Document doc5 = roundTripDOM( doc4 );
112         Document doc5 = roundTripDOM( doc3 );
113         
114         assertDocumentsEqual( document, doc5 );
115     }
116     
117     // Implementation methods
118     //-------------------------------------------------------------------------                    
119     protected void setUp() throws Exception {
120     }
121 
122     protected Document parseDocument(String file) throws Exception {
123         SAXReader reader = new SAXReader();
124         URL  url = getClass().getResource(file);
125         return reader.read(url);
126     }
127     
128     protected Document roundTripDOM(Document document) throws Exception {
129         // now lets make a DOM object
130         DOMWriter domWriter = new DOMWriter();
131         org.w3c.dom.Document domDocument = domWriter.write(document);
132         
133         // now lets read it back as a DOM4J object
134         DOMReader domReader = new DOMReader();        
135         Document newDocument = domReader.read( domDocument );
136         
137         // lets ensure names are same
138         newDocument.setName( document.getName() );
139         
140         assertDocumentsEqual( document, newDocument );
141         
142         return newDocument;
143     }
144     
145     protected Document roundTripJAXP(Document document) throws Exception {
146         // output the document to a text buffer via JAXP
147         TransformerFactory factory = TransformerFactory.newInstance();
148         Transformer transformer = factory.newTransformer();
149 
150         StringWriter buffer = new StringWriter();        
151         StreamResult streamResult = new StreamResult(buffer);
152         DocumentSource documentSource = new DocumentSource(document);
153 
154         transformer.transform(documentSource, streamResult);
155 
156         // now lets parse it back again via JAXP
157         DocumentResult documentResult = new DocumentResult();
158         StreamSource streamSource = new StreamSource( new StringReader( buffer.toString() ) );
159 
160         transformer.transform(streamSource, documentResult);
161 
162         Document newDocument = documentResult.getDocument();
163             
164         // lets ensure names are same
165         newDocument.setName( document.getName() );
166         
167         assertDocumentsEqual( document, newDocument );
168         
169         return newDocument;
170     }
171     
172     protected Document roundTripSAX(Document document) throws Exception {
173         
174         // now lets write it back as SAX events to
175         // a SAX ContentHandler which should build up a new document
176         SAXContentHandler contentHandler = new SAXContentHandler();
177         SAXWriter saxWriter = new SAXWriter( contentHandler, contentHandler, contentHandler );
178         
179         saxWriter.write( document );
180         Document newDocument = contentHandler.getDocument();
181         
182         // lets ensure names are same
183         newDocument.setName( document.getName() );
184         
185         assertDocumentsEqual( document, newDocument );
186         
187         return newDocument;
188     }
189         
190     protected Document roundTripText(Document document) throws Exception {
191         StringWriter out = new StringWriter();
192         XMLWriter xmlWriter = new XMLWriter(out);
193         
194         xmlWriter.write( document );
195         
196         // now lets read it back
197         String xml = out.toString();
198         
199         StringReader in = new StringReader( xml );
200         SAXReader reader = new SAXReader();
201         Document newDocument = reader.read(in);
202         
203         // lets ensure names are same
204         newDocument.setName( document.getName() );
205         
206         assertDocumentsEqual( document, newDocument );
207         
208         return newDocument;
209     }
210     
211     protected Document roundTripFull(Document document) throws Exception {
212         Document doc2 = roundTripDOM( document );
213         Document doc3 = roundTripSAX( doc2 );
214         Document doc4 = roundTripText( doc3 );
215 
216         assertDocumentsEqual( document, doc4 );
217         
218         return doc4;
219     }
220 }
221 
222 
223 
224 
225 /*
226  * Redistribution and use of this software and associated documentation
227  * ("Software"), with or without modification, are permitted provided
228  * that the following conditions are met:
229  *
230  * 1. Redistributions of source code must retain copyright
231  *    statements and notices.  Redistributions must also contain a
232  *    copy of this document.
233  *
234  * 2. Redistributions in binary form must reproduce the
235  *    above copyright notice, this list of conditions and the
236  *    following disclaimer in the documentation and/or other
237  *    materials provided with the distribution.
238  *
239  * 3. The name "DOM4J" must not be used to endorse or promote
240  *    products derived from this Software without prior written
241  *    permission of MetaStuff, Ltd.  For written permission,
242  *    please contact dom4j-info@metastuff.com.
243  *
244  * 4. Products derived from this Software may not be called "DOM4J"
245  *    nor may "DOM4J" appear in their names without prior written
246  *    permission of MetaStuff, Ltd. DOM4J is a registered
247  *    trademark of MetaStuff, Ltd.
248  *
249  * 5. Due credit should be given to the DOM4J Project - 
250  *    http://www.dom4j.org
251  *
252  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
253  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
254  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
255  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
256  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
257  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
258  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
259  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
261  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
263  * OF THE POSSIBILITY OF SUCH DAMAGE.
264  *
265  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
266  *
267  * $Id: TestRoundTrip.java,v 1.17 2004/08/03 20:08:49 maartenc Exp $
268  */