SerializeTest xref

    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.ByteArrayInputStream;
    13  import java.io.ByteArrayOutputStream;
    14  import java.io.ObjectInputStream;
    15  import java.io.ObjectOutputStream;
    16  import java.util.HashMap;
    17  import java.util.Map;
    18
    19  import org.dom4j.io.SAXReader;
    20
    21  /***
    22   * Tests that a dom4j document is Serializable
    23   *
    24   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    25   * @version $Revision: 1.4 $
    26   */
    27  public class SerializeTest extends AbstractTestCase {
    28      public static void main(String[] args) {
    29          TestRunner.run(SerializeTest.class);
    30      }
    31
    32      // Test case(s)
    33      // -------------------------------------------------------------------------
    34      public void testSerializePeriodicTable() throws Exception {
    35          testSerialize("/xml/periodic_table.xml");
    36      }
    37
    38      public void testSerializeMuchAdo() throws Exception {
    39          testSerialize("/xml/much_ado.xml");
    40      }
    41
    42      public void testSerializeTestSchema() throws Exception {
    43          testSerialize("/xml/test/schema/personal.xsd");
    44      }
    45
    46      public void testSerializeXPath() throws Exception {
    47          Map uris = new HashMap();
    48          uris.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
    49          uris.put("m", "urn:xmethodsBabelFish");
    50
    51          DocumentFactory factory = new DocumentFactory();
    52          factory.setXPathNamespaceURIs(uris);
    53
    54          // now parse a document using my factory
    55          SAXReader reader = new SAXReader();
    56          reader.setDocumentFactory(factory);
    57
    58          Document doc = getDocument("/xml/soap.xml", reader);
    59
    60          // now lets use the prefixes
    61          String expr = "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish";
    62          Node element = doc.selectSingleNode(expr);
    63          assertTrue("Found valid element", element != null);
    64
    65          XPath xpath = factory
    66                  .createXPath("/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish");
    67          element = xpath.selectSingleNode(doc);
    68          assertTrue("Found valid element", element != null);
    69
    70          // now serialize
    71          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    72          ObjectOutputStream out = new ObjectOutputStream(bytesOut);
    73          out.writeObject(xpath);
    74          out.close();
    75
    76          byte[] data = bytesOut.toByteArray();
    77
    78          ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
    79                  data));
    80          XPath xpath2 = (XPath) in.readObject();
    81          in.close();
    82
    83          element = xpath2.selectSingleNode(doc);
    84          assertTrue("Found valid element", element != null);
    85      }
    86
    87      // Implementation methods
    88      // -------------------------------------------------------------------------
    89      protected void testSerialize(String xmlFile) throws Exception {
    90          Document document = getDocument(xmlFile);
    91          String text = document.asXML();
    92
    93          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    94          ObjectOutputStream out = new ObjectOutputStream(bytesOut);
    95          out.writeObject(document);
    96          out.close();
    97
    98          byte[] data = bytesOut.toByteArray();
    99
    100         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
    101                 data));
    102         Document doc2 = (Document) in.readObject();
    103         in.close();
    104
    105         String text2 = doc2.asXML();
    106
    107         assertEquals("Documents text are equal", text, text2);
    108
    109         assertTrue("Read back document after serialization", (doc2 != null)
    110                 && doc2 instanceof Document);
    111
    112         assertDocumentsEqual(document, (Document) doc2);
    113
    114         // now lets try add something to the document...
    115         doc2.getRootElement().addElement("new");
    116     }
    117 }
    118
    119 /*
    120  * Redistribution and use of this software and associated documentation
    121  * ("Software"), with or without modification, are permitted provided that the
    122  * following conditions are met:
    123  *
    124  * 1. Redistributions of source code must retain copyright statements and
    125  * notices. Redistributions must also contain a copy of this document.
    126  *
    127  * 2. Redistributions in binary form must reproduce the above copyright notice,
    128  * this list of conditions and the following disclaimer in the documentation
    129  * and/or other materials provided with the distribution.
    130  *
    131  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    132  * from this Software without prior written permission of MetaStuff, Ltd. For
    133  * written permission, please contact dom4j-info@metastuff.com.
    134  *
    135  * 4. Products derived from this Software may not be called "DOM4J" nor may
    136  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    137  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    138  *
    139  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    140  *
    141  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    142  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    143  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    144  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    145  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    146  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    147  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    148  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    149  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    150  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    151  * POSSIBILITY OF SUCH DAMAGE.
    152  *
    153  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    154  */