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