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