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