KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > soap12 > TestDeser


1 package test.soap12;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.Constants;
5 import org.apache.axis.Message;
6 import org.apache.axis.MessageContext;
7 import org.apache.axis.encoding.TypeMapping;
8 import org.apache.axis.encoding.TypeMappingRegistry;
9 import org.apache.axis.message.RPCElement;
10 import org.apache.axis.message.RPCParam;
11 import org.apache.axis.message.SOAPEnvelope;
12 import org.apache.axis.server.AxisServer;
13 import org.apache.axis.soap.SOAPConstants;
14 import org.apache.axis.utils.JavaUtils;
15 import org.apache.axis.utils.Messages;
16
17 import javax.xml.namespace.QName JavaDoc;
18 import java.lang.reflect.Array JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Test deserialization of SOAP responses
28  */

29 public class TestDeser extends TestCase {
30     private AxisServer server = null;
31
32
33     public TestDeser(String JavaDoc name) {
34         super(name);
35
36         server = new AxisServer();
37
38         TypeMappingRegistry tmr = server.getTypeMappingRegistry();
39         TypeMapping tm = (TypeMapping) tmr.createTypeMapping();
40         tm.setSupportedEncodings(new String JavaDoc[] {Constants.URI_SOAP12_ENC});
41         tmr.register(Constants.URI_SOAP12_ENC, tm);
42         tm.register(java.lang.String JavaDoc[].class,
43                     new QName JavaDoc("http://soapinterop.org/xsd", "ArrayOfString"),
44                     new org.apache.axis.encoding.ser.ArraySerializerFactory(),
45                     new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
46     }
47
48     private final String JavaDoc SOAP_HEAD =
49         "<?xml version=\"1.0\"?>\n" +
50         "<soap:Envelope " +
51           "xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" " +
52           "xmlns:soapenc=\"http://www.w3.org/2003/05/soap-encoding\" " +
53           "xmlns:this=\"http://encoding.test\" " +
54           "xmlns:xsi=\"" + Constants.URI_DEFAULT_SCHEMA_XSI + "\" " +
55           "xmlns:xsd=\"" + Constants.URI_DEFAULT_SCHEMA_XSD + "\">\n";
56
57     private final String JavaDoc ITEM =
58            "<item xsi:type=\"xsd:string\">abc</item>\n";
59
60     private final String JavaDoc BODY_HEAD =
61           "<soap:Body>\n";
62
63     private final String JavaDoc METHOD_HEAD =
64             "<methodResult xmlns=\"http://tempuri.org/\">\n";
65
66     private final String JavaDoc METHOD_TAIL =
67             "</methodResult>\n";
68
69     private final String JavaDoc BODY_TAIL =
70           "</soap:Body>\n";
71
72     private final String JavaDoc SOAP_TAIL =
73         "</soap:Envelope>\n";
74
75
76     private final String JavaDoc HEAD = SOAP_HEAD + BODY_HEAD + METHOD_HEAD;
77     private final String JavaDoc TAIL = METHOD_TAIL + BODY_TAIL + SOAP_TAIL;
78
79
80     private static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
81        if ( (obj1 == null) || (obj2 == null) ) return (obj1 == obj2);
82        if (obj1.equals(obj2)) return true;
83        if (obj2.getClass().isArray() && obj1.getClass().isArray()) {
84            if (Array.getLength(obj1) != Array.getLength(obj2)) return false;
85            for (int i=0; i<Array.getLength(obj1); i++)
86                if (!equals(Array.get(obj1,i),Array.get(obj2,i))) return false;
87            return true;
88        }
89        if ((obj1 instanceof List JavaDoc) && (obj2 instanceof List JavaDoc)) {
90            List JavaDoc list1 = (List JavaDoc)obj1;
91            List JavaDoc list2 = (List JavaDoc)obj2;
92            if (list1.size() != list2.size()) return false;
93            for (int i=0; i < list1.size(); i++) {
94                if (!equals(list1.get(i), list2.get(i))) return false;
95            }
96            return true;
97        }
98        if ((obj1 instanceof Map JavaDoc) && (obj2 instanceof Map JavaDoc)) {
99            Map JavaDoc map1 = (Map JavaDoc)obj1;
100            Map JavaDoc map2 = (Map JavaDoc)obj2;
101            Set JavaDoc keys1 = map1.keySet();
102            Set JavaDoc keys2 = map2.keySet();
103            if (!(keys1.equals(keys2))) return false;
104            Iterator JavaDoc i = keys1.iterator();
105            while (i.hasNext()) {
106                Object JavaDoc key = i.next();
107                if (!map1.get(key).equals(map2.get(key)))
108                    return false;
109            }
110            return true;
111        }
112
113        return false;
114     }
115
116
117     public void deserialize(String JavaDoc soap, Object JavaDoc expected, boolean tryConvert) throws Exception JavaDoc {
118         Message message = new Message(soap);
119         MessageContext context = new MessageContext(server);
120         context.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
121         context.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);
122
123         message.setMessageContext(context);
124
125         SOAPEnvelope envelope = message.getSOAPEnvelope();
126         assertNotNull("SOAP envelope should not be null", envelope);
127
128         RPCElement body = (RPCElement)envelope.getFirstBody();
129         assertNotNull("SOAP body should not be null", body);
130
131         Vector JavaDoc arglist = body.getParams();
132         assertNotNull("arglist", arglist);
133         assertTrue("param.size()<=0 {Should be > 0}", arglist.size()>0);
134
135         RPCParam param = (RPCParam) arglist.get(0);
136         assertNotNull("SOAP param should not be null", param);
137
138         Object JavaDoc result = param.getObjectValue();
139         if (!equals(result, expected)) {
140            // Try to convert to the expected class
141
if (tryConvert) {
142                Object JavaDoc result2 = JavaUtils.convert(result, expected.getClass());
143                if (!equals(result2, expected)) {
144                    assertEquals("The result is not what is expected.", expected, result);
145                }
146            } else {
147                assertEquals("The result is not what is expected.", expected, result);
148            }
149         }
150     }
151
152     public void testDeser1() throws Exception JavaDoc {
153         deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + SOAP_TAIL, "abc", false);
154     }
155
156     public void testDeser2() throws Exception JavaDoc {
157         boolean expectedExceptionThrown = false;
158         try {
159             deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + BODY_TAIL + "<hello/>" + SOAP_TAIL, null, false);
160         } catch (org.apache.axis.AxisFault af) {
161             String JavaDoc expected = Messages.getMessage("noElemAfterBody12");
162             if(af.getFaultString().indexOf(expected)!=-1)
163                 expectedExceptionThrown = true;
164         }
165         assertTrue(expectedExceptionThrown);
166     }
167
168     public void testAfterBody() throws Exception JavaDoc {
169         boolean expectedExceptionThrown = false;
170         try {
171             deserialize(SOAP_HEAD + BODY_HEAD + METHOD_HEAD + ITEM + METHOD_TAIL + "<hello/>" + BODY_TAIL + SOAP_TAIL, null, false);
172         } catch (org.apache.axis.AxisFault af) {
173             // Should drop an ex about soap 1.2 doesn't permit any element after body
174
String JavaDoc expected = Messages.getMessage("onlyOneBodyFor12");
175             if(af.getFaultString().indexOf(expected)!=-1)
176                 expectedExceptionThrown = true;
177         }
178         assertTrue(expectedExceptionThrown);
179     }
180
181     public void testArray() throws Exception JavaDoc {
182         Vector JavaDoc v = new Vector JavaDoc();
183         v.addElement("abc");
184         v.addElement("def");
185         deserialize(HEAD +
186                     "<result xsi:type=\"soapenc:Array\" " +
187                             "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"2\"> " +
188                        "<item xsi:type=\"xsd:string\">abc</item>" +
189                        "<item xsi:type=\"xsd:string\">def</item>" +
190                     "</result>" +
191                     TAIL,
192                     v, true);
193     }
194
195     public void testSparseArray1() throws Exception JavaDoc {
196         ArrayList JavaDoc list = new ArrayList JavaDoc(4);
197         list.add(null);
198         list.add(null);
199         list.add("abc");
200         list.add("def");
201         boolean expectedExceptionThrown = false;
202         try {
203             deserialize(HEAD +
204                         "<result xsi:type=\"soapenc:Array\" " +
205                                 "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"4\" " +
206                                 " soapenc:offset=\"[2]\">" +
207                            "<item xsi:type=\"xsd:string\">abc</item>" +
208                            "<item xsi:type=\"xsd:string\">def</item>" +
209                         "</result>" +
210                         TAIL,
211                         list, true);
212         } catch (Exception JavaDoc af) {
213             String JavaDoc expected = Messages.getMessage("noSparseArray");
214             if(af.toString().indexOf(expected)!=-1)
215                 expectedExceptionThrown = true;
216         }
217         assertTrue(expectedExceptionThrown);
218     }
219
220     public void testSparseArray2() throws Exception JavaDoc {
221         ArrayList JavaDoc list = new ArrayList JavaDoc(4);
222         list.add("abc");
223         list.add(null);
224         list.add("def");
225         list.add(null);
226
227         boolean expectedExceptionThrown = false;
228         try {
229             deserialize(HEAD +
230                         "<result xsi:type=\"soapenc:Array\" " +
231                         "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"4\">" +
232                         "<item soapenc:position=\"[0]\" xsi:type=\"xsd:string\">abc</item>" +
233                         "<item soapenc:position=\"[2]\" xsi:type=\"xsd:string\">def</item>" +
234                         "</result>" +
235                         TAIL,
236                         list, true);
237         } catch (Exception JavaDoc af) {
238             String JavaDoc expected = Messages.getMessage("noSparseArray");
239             if(af.toString().indexOf(expected)!=-1)
240                 expectedExceptionThrown = true;
241         }
242         assertTrue(expectedExceptionThrown);
243     }
244
245     public void testNoSizeDefinedArray() throws Exception JavaDoc {
246         ArrayList JavaDoc a = new ArrayList JavaDoc();
247         a.add("abc");
248         a.add("def");
249
250         deserialize(HEAD +
251                     "<result xsi:type=\"soapenc:Array\" " +
252                     "soapenc:itemType=\"xsd:string\" soapenc:arraySize=\"*\">" +
253                         "<item xsi:type=\"xsd:string\">abc</item>" +
254                         "<item xsi:type=\"xsd:string\">def</item>" +
255                     "</result>" +
256                     TAIL,
257                     a, true);
258     }
259
260 }
261
Popular Tags