1 16 17 package test.message; 18 19 import junit.framework.TestCase; 20 import org.apache.axis.message.MessageElement; 21 import org.apache.axis.message.SOAPBodyElement; 22 import org.apache.axis.utils.XMLUtils; 23 import org.w3c.dom.CDATASection ; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Text ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.soap.Name ; 31 import javax.xml.soap.SOAPBody ; 32 import javax.xml.soap.SOAPEnvelope ; 33 import javax.xml.soap.SOAPHeader ; 34 import javax.xml.soap.SOAPHeaderElement ; 35 import java.io.ByteArrayInputStream ; 36 import java.io.ByteArrayOutputStream ; 37 import java.io.ObjectInputStream ; 38 import java.io.ObjectOutputStream ; 39 import java.util.Iterator ; 40 41 47 public class TestJavaSerialization extends TestCase { 48 49 public TestJavaSerialization(String name) { 50 super(name); 51 } 52 53 public void testSOAPEnvelope() throws Exception { 54 SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope(); 56 SOAPHeader h = env.getHeader(); 57 SOAPBody b = env.getBody(); 58 Name heName = env.createName("localName", "prefix", "http://uri"); 59 SOAPHeaderElement he = h.addHeaderElement(heName); 60 he.setActor("actor"); 61 62 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 64 ObjectOutputStream os = new ObjectOutputStream (bos); 65 os.writeObject(env); 66 67 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 69 ObjectInputStream is = new ObjectInputStream (bis); 70 SOAPEnvelope env2 = (SOAPEnvelope )is.readObject(); 71 72 SOAPHeader h2 = env2.getHeader(); 74 SOAPHeaderElement he2 = (SOAPHeaderElement )h2. 75 examineHeaderElements("actor").next(); 76 Name heName2 = he2.getElementName(); 77 assertEquals("Local name did not survive java ser+deser", 78 heName.getLocalName(), heName2.getLocalName()); 79 assertEquals("Prefix did not survive java ser+deser", 80 heName.getPrefix(), heName2.getPrefix()); 81 assertEquals("URI did not survive java ser+deser", 82 heName.getURI(), heName2.getURI()); 83 } 84 85 public void testCDATASection() throws Exception { 86 SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope(); 88 SOAPBody body = env.getBody(); 89 SOAPBodyElement[] input = new SOAPBodyElement[3]; 90 91 input[0] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 92 "e1", "Hello")); 93 input[1] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 94 "e1", "World")); 95 96 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 97 Document doc = builder.newDocument(); 98 Element cdataElem = doc.createElementNS("urn:foo", "e3"); 99 CDATASection cdata = doc.createCDATASection("Text with\n\tImportant <b> whitespace </b> and tags! "); 100 cdataElem.appendChild(cdata); 101 102 input[2] = new SOAPBodyElement(cdataElem); 103 104 for(int i=0; i<input.length; i++) { 105 body.addChildElement(input[i]); 106 } 107 108 ByteArrayInputStream bais = new ByteArrayInputStream (env.toString().getBytes()); 109 SOAPEnvelope env2 = new org.apache.axis.message.SOAPEnvelope(bais); 110 111 Iterator iterator = env2.getBody().getChildElements(); 112 Element element = null; 113 for(int i=0;iterator.hasNext();i++) { 114 MessageElement e = (MessageElement) iterator.next(); 115 element = e.getAsDOM(); 116 } 117 String xml = element.getFirstChild().getNodeValue(); 118 assertEquals(xml, cdata.getData()); 119 } 120 121 public void testComments() throws Exception { 122 SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope(); 124 SOAPBody body = env.getBody(); 125 SOAPBodyElement[] input = new SOAPBodyElement[3]; 126 127 input[0] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 128 "e1", "Hello")); 129 input[1] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 130 "e1", "World")); 131 132 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 133 Document doc = builder.newDocument(); 134 Element commentsElem = doc.createElementNS("urn:foo", "e3"); 135 Text text = doc.createTextNode("This is a comment"); 136 commentsElem.appendChild(text); 137 138 input[2] = new SOAPBodyElement(commentsElem); 139 140 for(int i=0; i<input.length; i++) { 141 body.addChildElement(input[i]); 142 } 143 144 ByteArrayInputStream bais = new ByteArrayInputStream (env.toString().getBytes()); 145 SOAPEnvelope env2 = new org.apache.axis.message.SOAPEnvelope(bais); 146 147 Iterator iterator = env2.getBody().getChildElements(); 148 Element element = null; 149 for(int i=0;iterator.hasNext();i++) { 150 MessageElement e = (MessageElement) iterator.next(); 151 element = e.getAsDOM(); 152 } 153 String xml = element.getFirstChild().getNodeValue(); 154 assertEquals(xml, text.getData()); 155 } 156 } 157 | Popular Tags |