Debian Codegraph @

test/RPCDispatch/TestSerializedRPC.java

raw
1 package test.RPCDispatch;
2
3 import junit.framework.TestCase;
4 import org.apache.axis.AxisFault;
5 import org.apache.axis.Constants;
6 import org.apache.axis.Message;
7 import org.apache.axis.MessageContext;
8 import org.apache.axis.configuration.SimpleProvider;
9 import org.apache.axis.description.OperationDesc;
10 import org.apache.axis.description.JavaServiceDesc;
11 import org.apache.axis.encoding.TypeMapping;
12 import org.apache.axis.encoding.TypeMappingRegistry;
13 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
14 import org.apache.axis.encoding.ser.BeanSerializerFactory;
15 import org.apache.axis.handlers.soap.SOAPService;
16 import org.apache.axis.message.RPCElement;
17 import org.apache.axis.message.RPCParam;
18 import org.apache.axis.message.SOAPEnvelope;
19 import org.apache.axis.providers.java.RPCProvider;
20 import org.apache.axis.server.AxisServer;
21 import org.xml.sax.SAXException;
22
23 import javax.xml.namespace.QName;
24 import java.util.Vector;
25 import org.custommonkey.xmlunit.XMLTestCase;
26 import org.apache.axis.AxisEngine;
27
28 /**
29 * Test org.apache.axis.handlers.RPCDispatcher
30 *
31 * @author Sam Ruby <[email protected]>
32 */
33 public class TestSerializedRPC extends XMLTestCase {
34
35 private final String header =
36 "<?xml version=\"1.0\"?>\n" +
37 "<soap:Envelope " +
38 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
39 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
40 "xmlns:xsi=\"" + Constants.URI_DEFAULT_SCHEMA_XSI + "\" " +
41 "xmlns:xsd=\"" + Constants.URI_DEFAULT_SCHEMA_XSD + "\">\n" +
42 "<soap:Body>\n";
43
44 private final String footer =
45 "</soap:Body>\n" +
46 "</soap:Envelope>\n";
47
48 private SimpleProvider provider = new SimpleProvider();
49 private AxisServer engine = new AxisServer(provider);
50
51 QName firstParamName = null;
52 QName secondParamName = null;
53
54 private String SOAPAction = "urn:reverse";
55
56 public TestSerializedRPC(String name) throws Exception {
57 super(name);
58 engine.init();
59
60 // And deploy the type mapping
61 Class javaType = Data.class;
62 QName xmlType = new QName("urn:foo", "Data");
63 BeanSerializerFactory sf = new BeanSerializerFactory(javaType, xmlType);
64 BeanDeserializerFactory df = new BeanDeserializerFactory(javaType, xmlType);
65
66 TypeMappingRegistry tmr = engine.getTypeMappingRegistry();
67 TypeMapping tm =
68 tmr.getOrMakeTypeMapping(Constants.URI_DEFAULT_SOAP_ENC);
69 tm.register(javaType, xmlType, sf, df);
70
71 // Register the reverseString service
72 SOAPService reverse = new SOAPService(new RPCProvider());
73 reverse.setOption("className", "test.RPCDispatch.Service");
74 reverse.setOption("allowedMethods", "*");
75
76 JavaServiceDesc desc = new JavaServiceDesc();
77 desc.loadServiceDescByIntrospection(Service.class, tm);
78 reverse.setServiceDescription(desc);
79
80 provider.deployService(SOAPAction, reverse);
81
82 // Now we've got the service description loaded up. We're going to
83 // be testing parameter dispatch by name, so if debug info isn't
84 // compiled into the Service class, the names are going to be "in0",
85 // etc. Make sure they match.
86 OperationDesc oper = desc.getOperationByName("concatenate");
87 assertNotNull(oper);
88
89 firstParamName = oper.getParameter(0).getQName();
90 secondParamName = oper.getParameter(1).getQName();
91 }
92
93 /**
94 * Invoke a given RPC method, and return the result
95 * @param method action to be performed
96 * @param bodyStr XML body of the request
97 * @return Deserialized result
98 */
99 private final Object rpc(String method, String bodyStr,
100 boolean setService)
101 throws AxisFault, SAXException
102 {
103
104 // Create the message context
105 MessageContext msgContext = new MessageContext(engine);
106
107 // Set the dispatch either by SOAPAction or methodNS
108 String methodNS = "urn:dont.match.me";
109 if (setService) {
110 msgContext.setTargetService(SOAPAction);
111 } else {
112 methodNS = SOAPAction;
113 }
114
115 String bodyElemHead = "<m:" + method + " xmlns:m=\"" +
116 methodNS + "\">";
117 String bodyElemFoot = "</m:" + method + ">";
118 // Construct the soap request
119 String msgStr = header + bodyElemHead + bodyStr +
120 bodyElemFoot + footer;
121 msgContext.setRequestMessage(new Message(msgStr));
122 msgContext.setTypeMappingRegistry(engine.getTypeMappingRegistry());
123
124 // Invoke the Axis engine
125 engine.invoke(msgContext);
126
127 // Extract the response Envelope
128 Message message = msgContext.getResponseMessage();
129 assertNotNull("Response message was null!", message);
130 SOAPEnvelope envelope = message.getSOAPEnvelope();
131 assertNotNull("SOAP envelope was null", envelope);
132
133 // Extract the body from the envelope
134 RPCElement body = (RPCElement)envelope.getFirstBody();
135 assertNotNull("SOAP body was null", body);
136
137 // Extract the list of parameters from the body
138 Vector arglist = body.getParams();
139 assertNotNull("SOAP argument list was null", arglist);
140 assertTrue("param.size()<=0 {Should be > 0}", arglist.size()>0);
141
142 // Return the first parameter
143 RPCParam param = (RPCParam) arglist.get(0);
144 return param.getObjectValue();
145 }
146
147 /**
148 * Test a simple method that reverses a string
149 */
150 public void testSerReverseString() throws Exception {
151 String arg = "<arg0 xsi:type=\"xsd:string\">abc</arg0>";
152 // invoke the service and verify the result
153 assertEquals("Did not reverse the string as expected", "cba", rpc("reverseString", arg, true));
154 }
155
156 public void testSerReverseBodyDispatch() throws Exception {
157 String arg = "<arg0 xsi:type=\"xsd:string\">abc</arg0>";
158 // invoke the service and verify the result
159 assertEquals("Did not reverse the string as expected", "cba", rpc("reverseString", arg, false));
160 }
161
162 /**
163 * Test a method that reverses a data structure
164 */
165 public void testSerReverseData() throws Exception {
166 // invoke the service and verify the result
167 String arg = "<arg0 xmlns:foo=\"urn:foo\" xsi:type=\"foo:Data\">";
168 arg += "<field1>5</field1><field2>abc</field2><field3>3</field3>";
169 arg += "</arg0>";
170 Data expected = new Data(3, "cba", 5);
171 assertEquals("Did not reverse data as expected", expected, rpc("reverseData", arg, true));
172 }
173
174 /**
175 * Test a method that reverses a data structure
176 */
177 public void testReverseDataWithUntypedParam() throws Exception {
178 // invoke the service and verify the result
179 String arg = "<arg0 xmlns:foo=\"urn:foo\">";
180 arg += "<field1>5</field1><field2>abc</field2><field3>3</field3>";
181 arg += "</arg0>";
182 Data expected = new Data(3, "cba", 5);
183 assertEquals("Did not reverse data as expected", expected, rpc("reverseData", arg, true));
184 }
185
186 /**
187 * Test out-of-order parameters, using the names to match
188 */
189 public void testOutOfOrderParams() throws Exception {
190 String body = "<" + secondParamName.getLocalPart() + " xmlns=\""+ secondParamName.getNamespaceURI() + "\">world!</" +
191 secondParamName.getLocalPart() + ">" +
192 "<" + firstParamName.getLocalPart() + " xmlns=\""+ firstParamName.getNamespaceURI() + "\">Hello, </" +
193 firstParamName.getLocalPart() + ">";
194 String expected = "Hello, world!";
195 assertEquals("Concatenated value was wrong",
196 expected,
197 rpc("concatenate", body, true));
198 }
199
200 /**
201 * Test DOM round tripping
202 */
203 public void testArgAsDOM() throws Exception {
204 // invoke the service and verify the result
205 String arg = "<arg0 xmlns:foo=\"urn:foo\">";
206 arg += "<field1>5</field1><field2>abc</field2><field3>3</field3>";
207 arg += "</arg0>";
208
209 // invoke the service and verify the result
210 engine.setOption(AxisEngine.PROP_SEND_XSI, Boolean.FALSE);
211 assertXMLEqual("Did not echo arg correctly.", arg, rpc("argAsDOM", arg, true).toString());
212 }
213
214 public void testWrappedTypes() throws Exception
215 {
216 // boolean
217 String arg = "<arg0>true</arg0>";
218 Object expected = Boolean.TRUE;
219 assertEquals("method test failed with a boolean",
220 expected,
221 rpc("testBoolean", arg, true));
222
223 // boolean
224 arg = "<arg0>1</arg0>";
225 expected = Boolean.TRUE;
226 assertEquals("method test failed with a boolean",
227 expected,
228 rpc("testBoolean", arg, true));
229
230 // float
231 arg = "<arg0>NaN</arg0>";
232 expected = new Float(Float.NaN);
233 assertEquals("method test failed with a float",
234 expected,
235 rpc("testFloat", arg, true));
236
237 arg = "<arg0>INF</arg0>";
238 expected = new Float(Float.POSITIVE_INFINITY);
239 assertEquals("method test failed with a float",
240 expected,
241 rpc("testFloat", arg, true));
242
243 arg = "<arg0>-INF</arg0>";
244 expected = new Float(Float.NEGATIVE_INFINITY);
245 assertEquals("method test failed with a float",
246 expected,
247 rpc("testFloat", arg, true));
248
249 // double
250 arg = "<arg0>NaN</arg0>";
251 expected = new Double(Double.NaN);
252 assertEquals("method test failed with a double",
253 expected,
254 rpc("testDouble", arg, true));
255
256 arg = "<arg0>INF</arg0>";
257 expected = new Double(Double.POSITIVE_INFINITY);
258 assertEquals("method test failed with a double",
259 expected,
260 rpc("testDouble", arg, true));
261
262 arg = "<arg0>-INF</arg0>";
263 expected = new Double(Double.NEGATIVE_INFINITY);
264 assertEquals("method test failed with a double",
265 expected,
266 rpc("testDouble", arg, true));
267 }
268
269 /**
270 * Test overloaded method dispatch without the benefit of xsi:types
271 */
272 public void testOverloadedMethodDispatch() throws Exception
273 {
274 // invoke the service for each overloaded method, and verify the results
275
276 // boolean
277 String arg = "<arg0>true</arg0>";
278 Object expected = Boolean.TRUE;
279 assertEquals("Overloaded method test failed with a boolean",
280 expected,
281 rpc("overloaded", arg, true));
282
283 // boolean, string
284 arg = "<arg0>true</arg0><arg1>hello world</arg1>";
285 expected = Boolean.TRUE + "hello world";
286 assertEquals("Overloaded method test failed with boolean, string",
287 expected,
288 rpc("overloaded", arg, true));
289
290 // string, boolean
291 arg = "<arg0>hello world</arg0><arg1>true</arg1>";
292 expected = "hello world" + Boolean.TRUE;
293 assertEquals("Overloaded method test failed with string, boolean",
294 expected,
295 rpc("overloaded", arg, true));
296
297 // int
298 arg = "<arg0>5</arg0>";
299 expected = new Integer(5);
300 assertEquals("Overloaded method test failed with an int",
301 expected,
302 rpc("overloaded", arg, true));
303
304 // int, string
305 arg = "<arg0>5</arg0><arg1>hello world</arg1>";
306 expected = 5 + "hello world";
307 assertEquals("Overloaded method test failed with int, string",
308 expected,
309 rpc("overloaded", arg, true));
310 }
311
312 // public void testEncodedArrayConversion() throws Exception {
313 // String arg = "<arg0>a simple string</arg0>";
314 // AxisFault fault = (AxisFault)rpc("arrayMethod", arg, true);
315 // assertTrue("Erroneous conversion occurred!",
316 // !fault.getFaultString().equals("You shouldn't have called me!"));
317 // }
318 }