Debian Codegraph @

test/RPCDispatch/TestRPC.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.handlers.soap.SOAPService;
10 import org.apache.axis.message.RPCElement;
11 import org.apache.axis.message.RPCParam;
12 import org.apache.axis.message.SOAPEnvelope;
13 import org.apache.axis.providers.java.RPCProvider;
14 import org.apache.axis.server.AxisServer;
15 import org.xml.sax.SAXException;
16
17 import javax.xml.namespace.QName;
18 import java.util.Vector;
19
20 /**
21 * Test org.apache.axis.handlers.RPCDispatcher
22 *
23 * @author Sam Ruby <[email protected]>
24 */
25 public class TestRPC extends TestCase {
26
27 private final String header =
28 "<?xml version=\"1.0\"?>\n" +
29 "<soap:Envelope " +
30 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
31 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" " +
32 "xmlns:xsi=\"" + Constants.URI_DEFAULT_SCHEMA_XSI + "\" " +
33 "xmlns:xsd=\"" + Constants.URI_DEFAULT_SCHEMA_XSD + "\">\n" +
34 "<soap:Body>\n";
35
36 private final String footer =
37 "</soap:Body>\n" +
38 "</soap:Envelope>\n";
39
40 private SimpleProvider provider = new SimpleProvider();
41 private AxisServer engine = new AxisServer(provider);
42
43 private String SOAPAction = "urn:reverse";
44
45 public TestRPC(String name) {
46 super(name);
47 engine.init();
48 }
49
50 /**
51 * Invoke a given RPC method, and return the result
52 * @return Deserialized result
53 */
54 private final Object rpc(String method, Object[] parms)
55 throws AxisFault, SAXException
56 {
57
58 // Create the message context
59 MessageContext msgContext = new MessageContext(engine);
60
61 // Set the dispatch either by SOAPAction or methodNS
62 String methodNS = null;
63 msgContext.setTargetService(SOAPAction);
64
65 // Construct the soap request
66 SOAPEnvelope envelope = new SOAPEnvelope();
67 msgContext.setRequestMessage(new Message(envelope));
68 RPCElement body = new RPCElement(methodNS, method, parms);
69 envelope.addBodyElement(body);
70
71 // Invoke the Axis engine
72 engine.invoke(msgContext);
73
74 // Extract the response Envelope
75 Message message = msgContext.getResponseMessage();
76 envelope = (SOAPEnvelope)message.getSOAPEnvelope();
77 assertNotNull("SOAP envelope was null", envelope);
78
79 // Extract the body from the envelope
80 body = (RPCElement)envelope.getFirstBody();
81 assertNotNull("SOAP body was null", body);
82
83 // Extract the list of parameters from the body
84 Vector arglist = body.getParams();
85 assertNotNull("SOAP argument list was null", arglist);
86 if (arglist.size()==0) return null;
87
88 // Return the first parameter
89 RPCParam param = (RPCParam) arglist.get(0);
90 return param.getObjectValue();
91 }
92
93 /**
94 * Test a simple method that reverses a string
95 */
96 public void testReverseString() throws Exception {
97 // Register the reverseString service
98 SOAPService reverse = new SOAPService(new RPCProvider());
99 reverse.setOption("className", "test.RPCDispatch.Service");
100 reverse.setOption("allowedMethods", "reverseString");
101 provider.deployService(new QName(null,SOAPAction), reverse);
102
103 // invoke the service and verify the result
104 assertEquals("Did not reverse the string correctly.", "cba",
105 rpc("reverseString", new Object[] {"abc"}));
106 }
107
108 /**
109 * Test a simple method that reverses a string (but fails)
110 */
111 public void testReverseStringThatShouldFail() throws Exception {
112 try {
113 // Register the reverseString service
114 SOAPService reverse = new SOAPService(new RPCProvider());
115 reverse.setOption("className", "test.RPCDispatch.Service");
116 reverse.setOption("allowedMethods", "reverseString2");
117 provider.deployService(new QName(null,SOAPAction), reverse);
118
119 // invoke the service and verify the result
120 rpc("reverseString", new Object[] {"abc"});
121 throw new junit.framework.AssertionFailedError("Should not reach here");
122 } catch (AxisFault af){
123 //This test should cause an exception.
124 return;
125 }
126 }
127
128 /**
129 * Test a simple method that reverses a string (with a comma delimiter
130 */
131 public void testReverseStringWithCommaDelimiter() throws Exception {
132 // Register the reverseString service
133 SOAPService reverse = new SOAPService(new RPCProvider());
134 reverse.setOption("className", "test.RPCDispatch.Service");
135 reverse.setOption("allowedMethods", "reverseString2,reverseString");
136 provider.deployService(new QName(null,SOAPAction), reverse);
137
138 // invoke the service and verify the result
139 assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object[] {"abc"}));
140 }
141
142 /**
143 * Test a simple method that reverses a string (with a space delimiter
144 */
145 public void testReverseStringWithSpaceDelimiter() throws Exception {
146 // Register the reverseString service
147 SOAPService reverse = new SOAPService(new RPCProvider());
148 reverse.setOption("className", "test.RPCDispatch.Service");
149 reverse.setOption("allowedMethods", "reverseString2 reverseString");
150 provider.deployService(new QName(null,SOAPAction), reverse);
151
152 // invoke the service and verify the result
153 assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object[] {"abc"}));
154 }
155
156 /**
157 * Test a simple method that reverses a string (with a '*'
158 */
159 public void testReverseStringWithStar() throws Exception {
160 // Register the reverseString service
161 SOAPService reverse = new SOAPService(new RPCProvider());
162 reverse.setOption("className", "test.RPCDispatch.Service");
163 reverse.setOption("allowedMethods", "*");
164 provider.deployService(new QName(null,SOAPAction), reverse);
165
166 // invoke the service and verify the result
167 assertEquals("Did not reverse the string correctly.", "cba",
168 rpc("reverseString", new Object[] {"abc"}));
169 }
170
171 /**
172 * Test a method that reverses a data structure
173 */
174 public void testReverseData() throws Exception {
175 // Register the reverseData service
176 SOAPService reverse = new SOAPService(new RPCProvider());
177 reverse.setOption("className", "test.RPCDispatch.Service");
178 reverse.setOption("allowedMethods", "reverseData");
179 provider.deployService(new QName(null, SOAPAction), reverse);
180
181 // invoke the service and verify the result
182 Data input = new Data(5, "abc", 3);
183 Data expected = new Data(3, "cba", 5);
184 assertEquals("Did not reverse the data as expected.", expected, rpc("reverseData", new Object[] {input}));
185 }
186
187 /**
188 * Test a simple method that returns a field from the message context
189 */
190 public void testMessageContextImplicit() throws Exception {
191 // Register the targetService service
192 SOAPService tgtSvc = new SOAPService(new RPCProvider());
193 tgtSvc.setOption("className", "test.RPCDispatch.Service");
194 tgtSvc.setOption("allowedMethods", "targetServiceImplicit");
195 provider.deployService(new QName(null, SOAPAction), tgtSvc);
196
197 // invoke the service and verify the result
198 assertEquals("SOAP Action did not equal the targetService.",
199 SOAPAction, rpc("targetServiceImplicit", new Object[] {}));
200 }
201
202 /**
203 * Test a simple method that accepts and returns a null
204 */
205 public void testNull() throws Exception {
206 // Register the echoInt service
207 SOAPService echoInt = new SOAPService(new RPCProvider());
208 echoInt.setOption("className", "test.RPCDispatch.Service");
209 echoInt.setOption("allowedMethods", "echoInt");
210 provider.deployService(new QName(null, SOAPAction), echoInt);
211
212 // invoke the service and verify the result
213 assertNull("The result was not null as expected.", rpc("echoInt", new Object[] {null}));
214 }
215
216 /**
217 * Test faults
218 */
219 public void testSimpleFault() throws Exception {
220 // Register the reverseData service
221 SOAPService simpleFault = new SOAPService(new RPCProvider());
222 simpleFault.setOption("className", "test.RPCDispatch.Service");
223 simpleFault.setOption("allowedMethods", "simpleFault");
224 provider.deployService(new QName(null, SOAPAction), simpleFault);
225
226 try {
227 rpc("simpleFault", new Object[] {"foobar"});
228 } catch (AxisFault result) {
229 assertEquals("faultString was not set correctly.",
230 "test.RPCDispatch.Service$TestFault: foobar",
231 result.getFaultString());
232 return;
233 }
234
235 fail("Did not get an expected fault!");
236 }
237
238 public static void main(String args[])
239 {
240 try {
241 TestRPC tester = new TestRPC("RPC test");
242 tester.testReverseString();
243 tester.testReverseData();
244 tester.testSimpleFault();
245 tester.testReverseStringThatShouldFail();
246 tester.testReverseStringWithCommaDelimiter();
247 tester.testReverseStringWithSpaceDelimiter();
248 tester.testReverseStringWithStar();
249 } catch (Exception e) {
250 e.printStackTrace();
251 }
252 }
253 }