KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > message > TestJavaSerialization


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

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 JavaDoc;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Text JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.soap.Name JavaDoc;
31 import javax.xml.soap.SOAPBody JavaDoc;
32 import javax.xml.soap.SOAPEnvelope JavaDoc;
33 import javax.xml.soap.SOAPHeader JavaDoc;
34 import javax.xml.soap.SOAPHeaderElement JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.io.ObjectOutputStream JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * Test certain classes in the message package for java
43  * serializability.
44  *
45  * @author Glyn Normington (glyn@apache.org)
46  */

47 public class TestJavaSerialization extends TestCase {
48
49     public TestJavaSerialization(String JavaDoc name) {
50         super(name);
51     }
52
53     public void testSOAPEnvelope() throws Exception JavaDoc {
54         // Create an example SOAP envelope
55
SOAPEnvelope JavaDoc env = new org.apache.axis.message.SOAPEnvelope();
56         SOAPHeader JavaDoc h = env.getHeader();
57         SOAPBody JavaDoc b = env.getBody();
58         Name JavaDoc heName = env.createName("localName", "prefix", "http://uri");
59         SOAPHeaderElement JavaDoc he = h.addHeaderElement(heName);
60         he.setActor("actor");
61
62         // Serialize the SOAP envelope
63
ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
64         ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(bos);
65         os.writeObject(env);
66
67         // Deserializet the SOAP envelope
68
ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
69         ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(bis);
70         SOAPEnvelope JavaDoc env2 = (SOAPEnvelope JavaDoc)is.readObject();
71
72         // Check that the SOAP envelope survived the round trip
73
SOAPHeader JavaDoc h2 = env2.getHeader();
74         SOAPHeaderElement JavaDoc he2 = (SOAPHeaderElement JavaDoc)h2.
75             examineHeaderElements("actor").next();
76         Name JavaDoc 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 JavaDoc {
86         // Create a SOAP envelope
87
SOAPEnvelope JavaDoc env = new org.apache.axis.message.SOAPEnvelope();
88         SOAPBody JavaDoc 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 JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
97         Document JavaDoc doc = builder.newDocument();
98         Element cdataElem = doc.createElementNS("urn:foo", "e3");
99         CDATASection JavaDoc 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 JavaDoc bais = new ByteArrayInputStream JavaDoc(env.toString().getBytes());
109         SOAPEnvelope JavaDoc env2 = new org.apache.axis.message.SOAPEnvelope(bais);
110         
111         Iterator JavaDoc 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 JavaDoc xml = element.getFirstChild().getNodeValue();
118         assertEquals(xml, cdata.getData());
119     }
120     
121     public void testComments() throws Exception JavaDoc {
122         // Create a SOAP envelope
123
SOAPEnvelope JavaDoc env = new org.apache.axis.message.SOAPEnvelope();
124         SOAPBody JavaDoc 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 JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
133         Document JavaDoc doc = builder.newDocument();
134         Element commentsElem = doc.createElementNS("urn:foo", "e3");
135         Text JavaDoc 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 JavaDoc bais = new ByteArrayInputStream JavaDoc(env.toString().getBytes());
145         SOAPEnvelope JavaDoc env2 = new org.apache.axis.message.SOAPEnvelope(bais);
146         
147         Iterator JavaDoc 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 JavaDoc xml = element.getFirstChild().getNodeValue();
154         assertEquals(xml, text.getData());
155     }
156 }
157
Popular Tags