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 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 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 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 OperationDesc oper = desc.getOperationByName("concatenate"); 87 assertNotNull(oper); 88 89 firstParamName = oper.getParameter(0).getQName(); 90 secondParamName = oper.getParameter(1).getQName(); 91 } 92 93 99 private final Object rpc(String method, String bodyStr, 100 boolean setService) 101 throws AxisFault, SAXException 102 { 103 104 MessageContext msgContext = new MessageContext(engine); 106 107 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 String msgStr = header + bodyElemHead + bodyStr + 120 bodyElemFoot + footer; 121 msgContext.setRequestMessage(new Message(msgStr)); 122 msgContext.setTypeMappingRegistry(engine.getTypeMappingRegistry()); 123 124 engine.invoke(msgContext); 126 127 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 RPCElement body = (RPCElement)envelope.getFirstBody(); 135 assertNotNull("SOAP body was null", body); 136 137 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 RPCParam param = (RPCParam) arglist.get(0); 144 return param.getObjectValue(); 145 } 146 147 150 public void testSerReverseString() throws Exception { 151 String arg = "<arg0 xsi:type=\"xsd:string\">abc</arg0>"; 152 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 assertEquals("Did not reverse the string as expected", "cba", rpc("reverseString", arg, false)); 160 } 161 162 165 public void testSerReverseData() throws Exception { 166 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 177 public void testReverseDataWithUntypedParam() throws Exception { 178 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 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 203 public void testArgAsDOM() throws Exception { 204 String arg = "<arg0 xmlns:foo=\"urn:foo\">"; 206 arg += "<field1>5</field1><field2>abc</field2><field3>3</field3>"; 207 arg += "</arg0>"; 208 209 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 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 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 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 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 272 public void testOverloadedMethodDispatch() throws Exception 273 { 274 276 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 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 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 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 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 } 319 | Popular Tags |