KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > encoding > TestSer


1 package test.encoding;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.Constants;
5 import org.apache.axis.MessageContext;
6 import org.apache.axis.components.logger.LogFactory;
7 import org.apache.axis.encoding.DeserializationContext;
8 import org.apache.axis.encoding.SerializationContext;
9 import org.apache.axis.encoding.SerializationContext;
10 import org.apache.axis.encoding.TypeMapping;
11 import org.apache.axis.encoding.TypeMappingRegistry;
12 import org.apache.axis.message.RPCElement;
13 import org.apache.axis.message.RPCParam;
14 import org.apache.axis.message.SOAPEnvelope;
15 import org.apache.axis.server.AxisServer;
16 import org.apache.axis.utils.XMLUtils;
17 import org.apache.commons.logging.Log;
18 import org.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.NodeList JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23 import org.xml.sax.helpers.AttributesImpl JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 /** Little serialization test with a struct.
31  */

32 public class TestSer extends TestCase {
33     Log log =
34             LogFactory.getLog(TestSer.class.getName());
35
36     public static final String JavaDoc myNS = "urn:myNS";
37
38     public TestSer(String JavaDoc name) {
39         super(name);
40     }
41
42     public void testDataNoHrefs () throws Exception JavaDoc {
43         doTestData(false);
44     }
45
46     public void testDataWithHrefs () throws Exception JavaDoc {
47         doTestData(true);
48     }
49
50     public void doTestData (boolean multiref) throws Exception JavaDoc {
51         MessageContext msgContext = new MessageContext(new AxisServer());
52         SOAPEnvelope msg = new SOAPEnvelope();
53         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
54
55         Data data = new Data();
56         data.stringMember = "String member";
57         data.floatMember = new Float JavaDoc("4.54");
58
59         RPCParam arg2 = new RPCParam("", "struct", data);
60         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[]{ arg1, arg2 });
61         msg.addBodyElement(body);
62
63         Writer JavaDoc stringWriter = new StringWriter JavaDoc();
64         SerializationContext context = new SerializationContext(stringWriter, msgContext);
65         context.setDoMultiRefs(multiref);
66
67         // Create a TypeMapping and register the specialized Type Mapping
68
TypeMappingRegistry reg = context.getTypeMappingRegistry();
69         TypeMapping tm = (TypeMapping) reg.createTypeMapping();
70         tm.setSupportedEncodings(new String JavaDoc[] {Constants.URI_DEFAULT_SOAP_ENC});
71         reg.register(Constants.URI_DEFAULT_SOAP_ENC, tm);
72
73         QName JavaDoc dataQName = new QName JavaDoc("typeNS", "Data");
74         tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());
75
76         msg.output(context);
77
78         String JavaDoc msgString = stringWriter.toString();
79
80         log.debug("---");
81         log.debug(msgString);
82         log.debug("---");
83
84         StringReader JavaDoc reader = new StringReader JavaDoc(msgString);
85
86         DeserializationContext dser = new DeserializationContext(
87             new InputSource JavaDoc(reader), msgContext, org.apache.axis.Message.REQUEST);
88         dser.parse();
89
90         SOAPEnvelope env = dser.getEnvelope();
91         RPCElement rpcElem = (RPCElement)env.getFirstBody();
92         RPCParam struct = rpcElem.getParam("struct");
93         assertNotNull("No <struct> param", struct);
94
95         Data val = (Data)struct.getObjectValue();
96         assertNotNull("No value for struct param", val);
97
98         assertEquals("Data and Val string members are not equal", data.stringMember, val.stringMember);
99         assertEquals("Data and Val float members are not equal",data.floatMember.floatValue(),
100                      val.floatMember.floatValue(), 0.00001F);
101     }
102     
103     public void testAttributeQNames() throws Exception JavaDoc {
104         String JavaDoc NS1 = "urn:foo";
105         MessageContext context = new MessageContext(new AxisServer());
106         StringWriter JavaDoc writer = new StringWriter JavaDoc();
107         SerializationContext ser = new SerializationContext(writer);
108         ser.registerPrefixForURI("", NS1);
109         ser.startElement(new QName JavaDoc(NS1, "e1"), null);
110         String JavaDoc foo = ser.attributeQName2String(new QName JavaDoc(NS1, "attr"));
111         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
112         attrs.addAttribute("a", "a", "a", "CDATA", foo);
113         ser.startElement(new QName JavaDoc(NS1, "e2"), attrs);
114         ser.endElement();
115         foo = ser.attributeQName2String(new QName JavaDoc(NS1, "attr"));
116         attrs = new AttributesImpl JavaDoc();
117         attrs.addAttribute("a", "a", "a", "CDATA", foo);
118         ser.startElement(new QName JavaDoc(NS1, "e3"), null);
119         ser.endElement();
120         ser.endElement();
121         System.out.println(writer.getBuffer().toString());
122     }
123
124     /**
125      * Test RPC element serialization when we have no MessageContext
126      */

127     public void testRPCElement()
128     {
129         try {
130             SOAPEnvelope env = new SOAPEnvelope();
131             RPCElement method = new RPCElement("ns",
132                                                "method",
133                                                new Object JavaDoc [] { "argument" });
134             env.addBodyElement(method);
135             env.toString();
136         } catch (Exception JavaDoc e) {
137             fail(e.getMessage());
138         }
139
140         // If there was no exception, we succeeded in serializing it.
141
}
142
143     public void testEmptyXMLNS() throws Exception JavaDoc
144     {
145         MessageContext msgContext = new MessageContext(new AxisServer());
146         String JavaDoc req =
147                 "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
148                 + "<xsd1:B>"
149                 + "<xsd1:C>foo bar</xsd1:C>"
150                 + "</xsd1:B>"
151                 + "</xsd1:A>";
152         
153         StringWriter JavaDoc stringWriter=new StringWriter JavaDoc();
154         StringReader JavaDoc reqReader = new StringReader JavaDoc(req);
155         InputSource JavaDoc reqSource = new InputSource JavaDoc(reqReader);
156         
157         Document JavaDoc document = XMLUtils.newDocument(reqSource);
158         
159         String JavaDoc msgString = null;
160         
161         SOAPEnvelope msg = new SOAPEnvelope();
162         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
163         arg1.setXSITypeGeneration(Boolean.FALSE);
164         
165         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[] { arg1 });
166         msg.addBodyElement(body);
167         body.setEncodingStyle(Constants.URI_LITERAL_ENC);
168         
169         SerializationContext context = new SerializationContext(stringWriter, msgContext);
170         msg.output(context);
171         
172         msgString = stringWriter.toString();
173         assertTrue(msgString.indexOf("xmlns=\"\"")==-1);
174     }
175     
176     /**
177      * Confirm that default namespaces when writing doc/lit messages don't
178      * trample namespace mappings.
179      *
180      * @throws Exception
181      */

182     public void testDefaultNamespace() throws Exception JavaDoc
183     {
184         MessageContext msgContext = new MessageContext(new AxisServer());
185         String JavaDoc req =
186                 "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
187                 + "<B>" // Note that B and C are in no namespace!
188
+ "<C>foo bar</C>"
189                 + "</B>"
190                 + "</xsd1:A>";
191         
192         StringWriter JavaDoc stringWriter=new StringWriter JavaDoc();
193         StringReader JavaDoc reqReader = new StringReader JavaDoc(req);
194         InputSource JavaDoc reqSource = new InputSource JavaDoc(reqReader);
195         
196         Document JavaDoc document = XMLUtils.newDocument(reqSource);
197         
198         String JavaDoc msgString = null;
199         
200         SOAPEnvelope msg = new SOAPEnvelope();
201         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
202         arg1.setXSITypeGeneration(Boolean.FALSE);
203         
204         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[] { arg1 });
205         msg.addBodyElement(body);
206         body.setEncodingStyle(Constants.URI_LITERAL_ENC);
207         
208         SerializationContext context = new SerializationContext(stringWriter, msgContext);
209         msg.output(context);
210         
211         msgString = stringWriter.toString();
212         
213         // Now reparse into DOM so we can check namespaces.
214
StringReader JavaDoc resReader = new StringReader JavaDoc(msgString);
215         InputSource JavaDoc resSource = new InputSource JavaDoc(resReader);
216         Document JavaDoc doc = XMLUtils.newDocument(resSource);
217         
218         // Go make sure B and C are in fact in no namespace
219
Element el = findChildElementByLocalName(doc.getDocumentElement(),
220                                                  "B");
221         assertNotNull("Couldn't find <B> element!", el);
222         assertNull("Element <B> has a namespace!", el.getNamespaceURI());
223         el = findChildElementByLocalName(el, "C");
224         assertNotNull("Couldn't find <C> element!", el);
225         assertNull("Element <C> has a namespace!", el.getNamespaceURI());
226     }
227     
228     private Element findChildElementByLocalName(Element src, String JavaDoc localName) {
229         NodeList JavaDoc nl = src.getChildNodes();
230         for (int i = 0; i < nl.getLength(); i++) {
231             Node JavaDoc node = nl.item(i);
232             if (node instanceof Element) {
233                 Element e = (Element)node;
234                 if (e.getLocalName().equals(localName)) {
235                     return e;
236                 }
237                 // Depth-first search
238
e = findChildElementByLocalName(e, localName);
239                 if (e != null) {
240                     return e;
241                 }
242             }
243         }
244         return null;
245     }
246 }
247
Popular Tags