KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestSerialize


1 /*
2  * Copyright 2001 (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.3 2003/11/02 18:31:28 per_nyfelt Exp $
8  *
9  * This test does not really apply to the Ozone implementation
10  * what good is it to serialize a Document proxy?
11  */

12 package test.dom4j;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16 import junit.textui.TestRunner;
17 import org.dom4j.Document;
18 import org.dom4j.Node;
19 import org.dom4j.XPath;
20 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
21 import org.ozoneDB.xml.dom4j.OzoneDocumentFactory;
22 import org.ozoneDB.xml.dom4j.o3impl.OzoneDocumentFactoryImpl;
23
24 import java.io.*;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /** Tests that a dom4j document is Serializable
29   *
30   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
31   * @version $Revision: 1.3 $
32   */

33 public class TestSerialize extends AbstractTestCase {
34
35     protected static final boolean VERBOSE = false;
36
37
38     public static void main( String JavaDoc[] args ) {
39         TestRunner.run( suite() );
40     }
41
42     public static Test suite() {
43         return new TestSuite( TestSerialize.class );
44     }
45
46     public TestSerialize(String JavaDoc name) {
47         super(name);
48     }
49
50     // Test case(s)
51
//-------------------------------------------------------------------------
52
public void testSerializePeriodicTable() throws Exception JavaDoc {
53         testSerialize( "xml/periodic_table.xml" );
54     }
55
56     public void testSerializeMuchAdo() throws Exception JavaDoc {
57         testSerialize( "xml/much_ado.xml" );
58     }
59
60     public void testSerializeTestSchema() throws Exception JavaDoc {
61         testSerialize( "xml/test/schema/personal.xsd" );
62     }
63
64     public void testSerializeXPath() throws Exception JavaDoc {
65         Map JavaDoc uris = new HashMap JavaDoc();
66         uris.put( "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/" );
67         uris.put( "m", "urn:xmethodsBabelFish" );
68
69         OzoneDocumentFactory factory = OzoneDocumentFactoryImpl.getInstance(db);
70         factory.setXPathNamespaceURIs( uris );
71
72         // Per: lets use the document helper here, using the SAXReader is too slow right now...
73
// now parse a document using my factory
74
//SAXReader reader = new O3SAXReader(db);
75
//reader.setNodeFactory( factory );
76
//Document doc = reader.read( "xml/soap.xml" );
77
Document doc = O3DocumentHelper.parse(new FileInputStream("xml/soap.xml"));
78
79         // now lets use the prefixes
80
Node element = doc.selectSingleNode( "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish" );
81         assertTrue( "Found valid element", element != null );
82
83         XPath xpath = factory.createXPath( "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish" );
84         element = xpath.selectSingleNode( doc );
85         assertTrue( "Found valid element", element != null );
86
87         // now serialize
88
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
89         ObjectOutputStream out = new ObjectOutputStream( bytesOut );
90         out.writeObject( xpath );
91         out.close();
92
93         byte[] data = bytesOut.toByteArray();
94
95         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
96         XPath xpath2 = (XPath) in.readObject();
97         in.close();
98
99         element = xpath2.selectSingleNode( doc );
100         assertTrue( "Found valid element", element != null );
101     }
102
103     // Implementation methods
104
//-------------------------------------------------------------------------
105
protected void testSerialize(String JavaDoc xmlFile) throws Exception JavaDoc {
106         // Per: lets use the document helper here, using the SAXReader is too slow right now...
107
//SAXReader reader = new O3SAXReader(db);
108
//Document document = reader.read( xmlFile );
109
Document document = O3DocumentHelper.parse(new FileInputStream(xmlFile) );
110         String JavaDoc text = document.asXML();
111
112         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
113         ObjectOutputStream out = new ObjectOutputStream( bytesOut );
114         out.writeObject( document );
115         out.close();
116
117         byte[] data = bytesOut.toByteArray();
118
119         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
120         Document doc2 = (Document) in.readObject();
121         in.close();
122
123         String JavaDoc text2 = doc2.asXML();
124
125         assertEquals( "Documents text are equal", text, text2 );
126
127         assertTrue( "Read back document after serialization", doc2 != null && doc2 instanceof Document );
128
129         assertDocumentsEqual( document, doc2 );
130
131         // now lets try add something to the document...
132

133         doc2.getRootElement().addElement( "new" );
134     }
135
136     protected void setUp() throws Exception JavaDoc {
137     }
138 }
139
140
141
142
143 /*
144  * Redistribution and use of this software and associated documentation
145  * ("Software"), with or without modification, are permitted provided
146  * that the following conditions are met:
147  *
148  * 1. Redistributions of source code must retain copyright
149  * statements and notices. Redistributions must also contain a
150  * copy of this document.
151  *
152  * 2. Redistributions in binary form must reproduce the
153  * above copyright notice, this list of conditions and the
154  * following disclaimer in the documentation and/or other
155  * materials provided with the distribution.
156  *
157  * 3. The name "DOM4J" must not be used to endorse or promote
158  * products derived from this Software without prior written
159  * permission of MetaStuff, Ltd. For written permission,
160  * please contact dom4j-info@metastuff.com.
161  *
162  * 4. Products derived from this Software may not be called "DOM4J"
163  * nor may "DOM4J" appear in their names without prior written
164  * permission of MetaStuff, Ltd. DOM4J is a registered
165  * trademark of MetaStuff, Ltd.
166  *
167  * 5. Due credit should be given to the DOM4J Project
168  * (http://dom4j.org/).
169  *
170  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
171  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
172  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
173  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
174  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
175  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
176  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
177  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
178  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
179  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
180  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
181  * OF THE POSSIBILITY OF SUCH DAMAGE.
182  *
183  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
184  *
185  * $Id: TestSerialize.java,v 1.3 2003/11/02 18:31:28 per_nyfelt Exp $
186  */

187
Popular Tags