1 16 17 package org.springframework.remoting.jaxrpc; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.rmi.Remote ; 22 import java.rmi.RemoteException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Arrays ; 27 28 import javax.xml.namespace.QName ; 29 import javax.xml.rpc.Call ; 30 import javax.xml.rpc.Service ; 31 import javax.xml.rpc.ServiceException ; 32 import javax.xml.rpc.ServiceFactory ; 33 import javax.xml.rpc.Stub ; 34 35 import junit.framework.TestCase; 36 import org.easymock.MockControl; 37 import org.easymock.ArgumentsMatcher; 38 39 import org.springframework.remoting.RemoteAccessException; 40 41 45 public class JaxRpcSupportTests extends TestCase { 46 47 public void testLocalJaxRpcServiceFactoryBeanWithWsdlAndNamespace() throws Exception { 48 LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean(); 49 factory.setServiceFactoryClass(MockServiceFactory.class); 50 factory.setWsdlDocumentUrl(new URL ("http://myUrl1")); 51 factory.setServiceName("myService2"); 52 factory.afterPropertiesSet(); 53 assertTrue("Correct singleton value", factory.isSingleton()); 54 assertEquals(MockServiceFactory.service2, factory.getObject()); 55 } 56 57 public void testLocalJaxRpcServiceFactoryBeanWithoutWsdlAndNamespace() throws Exception { 58 LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean(); 59 factory.setServiceFactoryClass(MockServiceFactory.class); 60 factory.setNamespaceUri("myNamespace"); 61 factory.setServiceName("myService1"); 62 factory.afterPropertiesSet(); 63 assertEquals(MockServiceFactory.service1, factory.getObject()); 64 } 65 66 public void testJaxRpcPortProxyFactoryBean() throws Exception { 67 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 68 factory.setServiceFactoryClass(MockServiceFactory.class); 69 factory.setNamespaceUri("myNamespace"); 70 factory.setServiceName("myService1"); 71 factory.setPortName("myPort"); 72 factory.setServiceInterface(IRemoteBean.class); 73 factory.afterPropertiesSet(); 74 assertTrue("Correct singleton value", factory.isSingleton()); 75 assertTrue(factory.getPortStub() instanceof Stub ); 76 77 assertTrue(factory.getObject() instanceof IRemoteBean); 78 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 79 proxy.setName("myName"); 80 assertEquals("myName", RemoteBean.singleton.name); 81 MockServiceFactory.service1Control.verify(); 82 } 83 84 public void testJaxRpcPortProxyFactoryBeanWithProperties() throws Exception { 85 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 86 factory.setServiceFactoryClass(MockServiceFactory.class); 87 factory.setNamespaceUri("myNamespace"); 88 factory.setServiceName("myService1"); 89 factory.setPortName("myPort"); 90 factory.setUsername("user"); 91 factory.setPassword("pw"); 92 factory.setEndpointAddress("ea"); 93 factory.setMaintainSession(true); 94 factory.setServiceInterface(IRemoteBean.class); 95 factory.afterPropertiesSet(); 96 assertTrue("Correct singleton value", factory.isSingleton()); 97 98 assertTrue(factory.getPortStub() instanceof Stub ); 99 Stub stub = (Stub ) factory.getPortStub(); 100 assertEquals("user", stub._getProperty(Stub.USERNAME_PROPERTY)); 101 assertEquals("pw", stub._getProperty(Stub.PASSWORD_PROPERTY)); 102 assertEquals("ea", stub._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY)); 103 assertTrue(((Boolean ) stub._getProperty(Stub.SESSION_MAINTAIN_PROPERTY)).booleanValue()); 104 105 assertTrue(factory.getObject() instanceof IRemoteBean); 106 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 107 proxy.setName("myName"); 108 assertEquals("myName", RemoteBean.singleton.name); 109 MockServiceFactory.service1Control.verify(); 110 } 111 112 public void testJaxRpcPortProxyFactoryBeanWithDynamicCalls() throws Exception { 113 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 114 factory.setServiceFactoryClass(CallMockServiceFactory.class); 115 factory.setNamespaceUri("myNamespace"); 116 factory.setServiceName("myService1"); 117 factory.setPortName("myPort"); 118 factory.setServiceInterface(IBusinessBean.class); 119 factory.afterPropertiesSet(); 120 assertTrue("Correct singleton value", factory.isSingleton()); 121 122 assertTrue(factory.getObject() instanceof IBusinessBean); 123 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 124 proxy.setName("myName"); 125 MockServiceFactory.service1Control.verify(); 126 CallMockServiceFactory.call1Control.verify(); 127 } 128 129 public void testJaxRpcPortProxyFactoryBeanWithDynamicCallsAndProperties() throws Exception { 130 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 131 factory.setServiceFactoryClass(CallWithPropertiesMockServiceFactory.class); 132 factory.setNamespaceUri("myNamespace"); 133 factory.setServiceName("myService1"); 134 factory.setPortName("myPort"); 135 factory.setUsername("user"); 136 factory.setPassword("pw"); 137 factory.setEndpointAddress("ea"); 138 factory.setMaintainSession(true); 139 factory.setServiceInterface(IBusinessBean.class); 140 factory.afterPropertiesSet(); 141 assertTrue("Correct singleton value", factory.isSingleton()); 142 assertNull(factory.getPortStub()); 143 144 assertTrue(factory.getObject() instanceof IBusinessBean); 145 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 146 proxy.setName("myName"); 147 MockServiceFactory.service1Control.verify(); 148 CallMockServiceFactory.call1Control.verify(); 149 } 150 151 public void testJaxRpcPortProxyFactoryBeanWithRemoteException() throws Exception { 152 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 153 factory.setServiceFactoryClass(MockServiceFactory.class); 154 factory.setNamespaceUri("myNamespace"); 155 factory.setServiceName("myService1"); 156 factory.setPortName("myPort"); 157 factory.setServiceInterface(IRemoteBean.class); 158 factory.afterPropertiesSet(); 159 160 assertTrue(factory.getPortStub() instanceof Stub ); 161 Stub stub = (Stub ) factory.getPortStub(); 162 assertNull(stub._getProperty(Stub.USERNAME_PROPERTY)); 163 assertNull(stub._getProperty(Stub.PASSWORD_PROPERTY)); 164 assertNull(stub._getProperty(Stub.ENDPOINT_ADDRESS_PROPERTY)); 165 assertNull(stub._getProperty(Stub.SESSION_MAINTAIN_PROPERTY)); 166 167 assertTrue(factory.getObject() instanceof IRemoteBean); 168 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 169 try { 170 proxy.setName("exception"); 171 fail("Should have thrown RemoteException"); 172 } 173 catch (RemoteException ex) { 174 } 176 MockServiceFactory.service1Control.verify(); 177 } 178 179 public void testJaxRpcPortProxyFactoryBeanWithPortInterface() throws Exception { 180 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 181 factory.setServiceFactoryClass(MockServiceFactory.class); 182 factory.setNamespaceUri("myNamespace"); 183 factory.setServiceName("myService1"); 184 factory.setPortName("myPort"); 185 factory.setPortInterface(IRemoteBean.class); 186 factory.setServiceInterface(IBusinessBean.class); 187 factory.afterPropertiesSet(); 188 assertTrue(factory.getObject() instanceof IBusinessBean); 189 assertFalse(factory.getObject() instanceof IRemoteBean); 190 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 191 proxy.setName("myName"); 192 assertEquals("myName", RemoteBean.singleton.name); 193 MockServiceFactory.service1Control.verify(); 194 } 195 196 public void testJaxRpcPortProxyFactoryBeanWithPortInterfaceAndRemoteException() throws Exception { 197 JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean(); 198 factory.setServiceFactoryClass(MockServiceFactory.class); 199 factory.setNamespaceUri("myNamespace"); 200 factory.setServiceName("myService1"); 201 factory.setPortName("myPort"); 202 factory.setPortInterface(IRemoteBean.class); 203 factory.setServiceInterface(IBusinessBean.class); 204 factory.afterPropertiesSet(); 205 assertTrue(factory.getObject() instanceof IBusinessBean); 206 assertFalse(factory.getObject() instanceof IRemoteBean); 207 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 208 try { 209 proxy.setName("exception"); 210 fail("Should have thrown RemoteAccessException"); 211 } 212 catch (RemoteAccessException ex) { 213 } 215 MockServiceFactory.service1Control.verify(); 216 } 217 218 219 public static class MockServiceFactory extends ServiceFactory { 220 221 protected static MockControl service1Control; 222 protected static Service service1; 223 protected static MockControl service2Control; 224 protected static Service service2; 225 226 public MockServiceFactory() throws Exception { 227 service1Control = MockControl.createControl(Service .class); 228 service1 = (Service ) service1Control.getMock(); 229 service2Control = MockControl.createControl(Service .class); 230 service2 = (Service ) service2Control.getMock(); 231 initMocks(); 232 service1Control.replay(); 233 } 234 235 protected void initMocks() throws Exception { 236 service1.getPort(new QName ("myNamespace", "myPort"), IRemoteBean.class); 237 service1Control.setReturnValue(new RemoteBean()); 238 } 239 240 public Service createService(URL url, QName qName) throws ServiceException { 241 try { 242 if (!(new URL ("http://myUrl1")).equals(url) || !"".equals(qName.getNamespaceURI()) || 243 !"myService2".equals(qName.getLocalPart())) { 244 throw new ServiceException ("not supported"); 245 } 246 } 247 catch (MalformedURLException ex) { 248 } 249 return service2; 250 } 251 252 public Service createService(QName qName) throws ServiceException { 253 if (!"myNamespace".equals(qName.getNamespaceURI()) || !"myService1".equals(qName.getLocalPart())) { 254 throw new ServiceException ("not supported"); 255 } 256 return service1; 257 } 258 } 259 260 261 public static class CallMockServiceFactory extends MockServiceFactory { 262 263 protected static MockControl call1Control; 264 protected static Call call1; 265 266 public CallMockServiceFactory() throws Exception { 267 super(); 268 } 269 270 protected void initMocks() throws Exception { 271 call1Control = MockControl.createControl(Call .class); 272 call1 = (Call ) call1Control.getMock(); 273 service1.createCall(new QName ("myNamespace", "myPort"), "setName"); 274 service1Control.setReturnValue(call1); 275 initCall(); 276 call1Control.replay(); 277 } 278 279 protected void initCall() throws Exception { 280 call1.invoke(new Object [] {"myName"}); 281 call1Control.setMatcher(new ArgumentsMatcher() { 282 public boolean matches(Object [] objects, Object [] objects1) { 283 return Arrays.equals((Object []) objects[0], (Object []) objects1[0]); 284 } 285 public String toString(Object [] objects) { 286 return null; 287 } 288 }); 289 call1Control.setReturnValue(null); 290 } 291 } 292 293 294 public static class CallWithPropertiesMockServiceFactory extends CallMockServiceFactory { 295 296 public CallWithPropertiesMockServiceFactory() throws Exception { 297 } 298 299 protected void initCall() throws Exception { 300 call1.setProperty(Call.USERNAME_PROPERTY, "user"); 301 call1Control.setVoidCallable(); 302 call1.setProperty(Call.PASSWORD_PROPERTY, "pw"); 303 call1Control.setVoidCallable(); 304 call1.setTargetEndpointAddress("ea"); 305 call1Control.setVoidCallable(); 306 call1.setProperty(Call.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE); 307 call1Control.setVoidCallable(); 308 super.initCall(); 309 } 310 } 311 312 313 public static interface IBusinessBean { 314 315 public void setName(String name); 316 317 } 318 319 320 public static interface IRemoteBean extends Remote { 321 322 public void setName(String name) throws RemoteException ; 323 324 } 325 326 327 public static class RemoteBean implements IRemoteBean, Stub { 328 329 private static RemoteBean singleton; 330 private static String name; 331 private static Map properties; 332 333 public RemoteBean() { 334 singleton = this; 335 properties = new HashMap (); 336 } 337 338 public void setName(String nam) throws RemoteException { 339 if ("exception".equals(nam)) { 340 throw new RemoteException (); 341 } 342 name = nam; 343 } 344 345 public void _setProperty(String key, Object o) { 346 properties.put(key, o); 347 } 348 349 public Object _getProperty(String key) { 350 return properties.get(key); 351 } 352 353 public Iterator _getPropertyNames() { 354 return properties.keySet().iterator(); 355 } 356 } 357 358 } 359 | Popular Tags |