1 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 35 public class SerializationTest extends AbstractTest { 36 37 protected String testFileName; 38 protected String rootTag; 39 protected String parserClassName = XMLResourceDescriptor.getXMLParserClassName(); 40 41 public SerializationTest(String file, 42 String root) { 43 testFileName = file; 44 rootTag = root; 45 } 46 47 public TestReport runImpl() throws Exception { 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 ObjectOutputStream oos; 65 oos = new ObjectOutputStream(new FileOutputStream(ser1)); 66 oos.writeObject(doc); 67 oos.close(); 68 69 ObjectInputStream ois; 71 ois = new ObjectInputStream(new FileInputStream(ser1)); 72 doc = (Document)ois.readObject(); 73 ois.close(); 74 75 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 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 |