KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > SerializationTest


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom;
19
20 import org.w3c.dom.*;
21
22 import java.io.*;
23 import java.net.*;
24 import org.apache.batik.dom.util.*;
25 import org.apache.batik.util.*;
26
27 import org.apache.batik.test.*;
28
29 /**
30  * To test the Java serialization.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: SerializationTest.java,v 1.3 2004/08/18 07:16:40 vhardy Exp $
34  */

35 public class SerializationTest extends AbstractTest {
36     
37     protected String JavaDoc testFileName;
38     protected String JavaDoc rootTag;
39     protected String JavaDoc parserClassName = XMLResourceDescriptor.getXMLParserClassName();
40
41     public SerializationTest(String JavaDoc file,
42                              String JavaDoc root) {
43         testFileName = file;
44         rootTag = root;
45     }
46
47     public TestReport runImpl() throws Exception JavaDoc {
48         DocumentFactory df
49             = new SAXDocumentFactory(GenericDOMImplementation.getDOMImplementation(),
50                                      parserClassName);
51         
52         File f = (new File(testFileName));
53         URL url = f.toURL();
54         Document doc = df.createDocument(null,
55                                          rootTag,
56                                          url.toString(),
57                                          url.openStream());
58
59         File ser1 = File.createTempFile("doc1", "ser");
60         File ser2 = File.createTempFile("doc2", "ser");
61
62         try {
63             // Serialization 1
64
ObjectOutputStream oos;
65             oos = new ObjectOutputStream(new FileOutputStream(ser1));
66             oos.writeObject(doc);
67             oos.close();
68
69             // Deserialization 1
70
ObjectInputStream ois;
71             ois = new ObjectInputStream(new FileInputStream(ser1));
72             doc = (Document)ois.readObject();
73             ois.close();
74         
75             // Serialization 2
76
oos = new ObjectOutputStream(new FileOutputStream(ser2));
77             oos.writeObject(doc);
78             oos.close();
79         } catch (IOException e) {
80             DefaultTestReport report = new DefaultTestReport(this);
81             report.setErrorCode("io.error");
82             report.addDescriptionEntry("message",
83                                        e.getClass().getName() +
84                                        ": " + e.getMessage());
85             report.addDescriptionEntry("file.name", testFileName);
86             report.setPassed(false);
87             return report;
88         }
89         
90         // Binary diff
91
InputStream is1 = new FileInputStream(ser1);
92         InputStream is2 = new FileInputStream(ser2);
93
94         for (;;) {
95             int i1 = is1.read();
96             int i2 = is2.read();
97             if (i1 == -1 && i2 == -1) {
98                 return reportSuccess();
99             }
100             if (i1 != i2) {
101                 DefaultTestReport report = new DefaultTestReport(this);
102                 report.setErrorCode("difference.found");
103                 report.addDescriptionEntry("file.name", testFileName);
104                 report.setPassed(false);
105                 return report;
106             }
107         }
108     }
109 }
110
Popular Tags