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 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 54 private final Object rpc(String method, Object [] parms) 55 throws AxisFault, SAXException 56 { 57 58 MessageContext msgContext = new MessageContext(engine); 60 61 String methodNS = null; 63 msgContext.setTargetService(SOAPAction); 64 65 SOAPEnvelope envelope = new SOAPEnvelope(); 67 msgContext.setRequestMessage(new Message(envelope)); 68 RPCElement body = new RPCElement(methodNS, method, parms); 69 envelope.addBodyElement(body); 70 71 engine.invoke(msgContext); 73 74 Message message = msgContext.getResponseMessage(); 76 envelope = (SOAPEnvelope)message.getSOAPEnvelope(); 77 assertNotNull("SOAP envelope was null", envelope); 78 79 body = (RPCElement)envelope.getFirstBody(); 81 assertNotNull("SOAP body was null", body); 82 83 Vector arglist = body.getParams(); 85 assertNotNull("SOAP argument list was null", arglist); 86 if (arglist.size()==0) return null; 87 88 RPCParam param = (RPCParam) arglist.get(0); 90 return param.getObjectValue(); 91 } 92 93 96 public void testReverseString() throws Exception { 97 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 assertEquals("Did not reverse the string correctly.", "cba", 105 rpc("reverseString", new Object [] {"abc"})); 106 } 107 108 111 public void testReverseStringThatShouldFail() throws Exception { 112 try { 113 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 rpc("reverseString", new Object [] {"abc"}); 121 throw new junit.framework.AssertionFailedError("Should not reach here"); 122 } catch (AxisFault af){ 123 return; 125 } 126 } 127 128 131 public void testReverseStringWithCommaDelimiter() throws Exception { 132 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 assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object [] {"abc"})); 140 } 141 142 145 public void testReverseStringWithSpaceDelimiter() throws Exception { 146 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 assertEquals("Did not reverse the string correctly.", "cba", rpc("reverseString", new Object [] {"abc"})); 154 } 155 156 159 public void testReverseStringWithStar() throws Exception { 160 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 assertEquals("Did not reverse the string correctly.", "cba", 168 rpc("reverseString", new Object [] {"abc"})); 169 } 170 171 174 public void testReverseData() throws Exception { 175 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 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 190 public void testMessageContextImplicit() throws Exception { 191 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 assertEquals("SOAP Action did not equal the targetService.", 199 SOAPAction, rpc("targetServiceImplicit", new Object [] {})); 200 } 201 202 205 public void testNull() throws Exception { 206 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 assertNull("The result was not null as expected.", rpc("echoInt", new Object [] {null})); 214 } 215 216 219 public void testSimpleFault() throws Exception { 220 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 } 254 | Popular Tags |