1 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 ; 26 import java.util.Map ; 27 28 33 public class TestSerialize extends AbstractTestCase { 34 35 protected static final boolean VERBOSE = false; 36 37 38 public static void main( String [] args ) { 39 TestRunner.run( suite() ); 40 } 41 42 public static Test suite() { 43 return new TestSuite( TestSerialize.class ); 44 } 45 46 public TestSerialize(String name) { 47 super(name); 48 } 49 50 public void testSerializePeriodicTable() throws Exception { 53 testSerialize( "xml/periodic_table.xml" ); 54 } 55 56 public void testSerializeMuchAdo() throws Exception { 57 testSerialize( "xml/much_ado.xml" ); 58 } 59 60 public void testSerializeTestSchema() throws Exception { 61 testSerialize( "xml/test/schema/personal.xsd" ); 62 } 63 64 public void testSerializeXPath() throws Exception { 65 Map uris = new HashMap (); 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 Document doc = O3DocumentHelper.parse(new FileInputStream("xml/soap.xml")); 78 79 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 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 protected void testSerialize(String xmlFile) throws Exception { 106 Document document = O3DocumentHelper.parse(new FileInputStream(xmlFile) ); 110 String 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 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 133 doc2.getRootElement().addElement( "new" ); 134 } 135 136 protected void setUp() throws Exception { 137 } 138 } 139 140 141 142 143 187 | Popular Tags |