e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.remoting.jaxrpc.JaxRpcSupportTests   ©

 OK to copy?
001 /*
002  * Copyright 2002-2004 the original author or authors.
003  * 
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  * 
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  * 
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */ 
016 
017 package org.springframework.remoting.jaxrpc;
018 
019 import java.net.MalformedURLException;
020 import java.net.URL;
021 import java.rmi.Remote;
022 import java.rmi.RemoteException;
023 import java.util.HashMap;
024 import java.util.Iterator;
025 import java.util.Map;
026 import java.util.Arrays;
027 
028 import javax.xml.namespace.QName;
029 import javax.xml.rpc.Call;
030 import javax.xml.rpc.Service;
031 import javax.xml.rpc.ServiceException;
032 import javax.xml.rpc.ServiceFactory;
033 import javax.xml.rpc.Stub;
034 
035 import junit.framework.TestCase;
036 import org.easymock.MockControl;
037 import org.easymock.ArgumentsMatcher;
038 
039 import org.springframework.remoting.RemoteAccessException;
040 
041 /**
042  * @author Juergen Hoeller
043  * @since 18.12.2003
044  */
045 public class JaxRpcSupportTests extends TestCase {
046 
047     public void testLocalJaxRpcServiceFactoryBeanWithWsdlAndNamespace() throws Exception {
048         LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
049         factory.setServiceFactoryClass(MockServiceFactory.class);
050         factory.setWsdlDocumentUrl(new URL("http://myUrl1"));
051         factory.setServiceName("myService2");
052         factory.afterPropertiesSet();
053         assertTrue("Correct singleton value", factory.isSingleton());
054         assertEquals(MockServiceFactory.service2, factory.getObject());
055     }
056 
057     public void testLocalJaxRpcServiceFactoryBeanWithoutWsdlAndNamespace() throws Exception {
058         LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
059         factory.setServiceFactoryClass(MockServiceFactory.class);
060         factory.setNamespaceUri("myNamespace");
061         factory.setServiceName("myService1");
062         factory.afterPropertiesSet();
063         assertEquals(MockServiceFactory.service1, factory.getObject());
064     }
065 
066     public void testJaxRpcPortProxyFactoryBean() throws Exception {
067         JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
068         factory.setServiceFactoryClass(MockServiceFactory.class);
069         factory.setNamespaceUri("myNamespace");
070         factory.setServiceName("myService1");
071         factory.setPortName("myPort");
072         factory.setServiceInterface(IRemoteBean.class);
073         factory.afterPropertiesSet();
074         assertTrue("Correct singleton value", factory.isSingleton());
075         assertTrue(factory.getPortStub() instanceof Stub);
076 
077         assertTrue(factory.getObject() instanceof IRemoteBean);
078         IRemoteBean proxy = (IRemoteBean) factory.getObject();
079         proxy.setName("myName");
080         assertEquals("myName", RemoteBean.singleton.name);
081         MockServiceFactory.service1Control.verify();
082     }
083 
084     public void testJaxRpcPortProxyFactoryBeanWithProperties() throws Exception {
085         JaxRpcPortProxyFactoryBean factory = new JaxRpcPortProxyFactoryBean();
086         factory.setServiceFactoryClass(MockServiceFactory.class);
087         factory.setNamespaceUri("myNamespace");
088         factory.setServiceName("myService1");
089         factory.setPortName("myPort");
090         factory.setUsername("user");
091         factory.setPassword("pw");
092         factory.setEndpointAddress("ea");
093         factory.setMaintainSession(true);
094         factory.setServiceInterface(IRemoteBean.class);
095         factory.afterPropertiesSet();
096         assertTrue("Correct singleton value", factory.isSingleton());
097 
098         assertTrue(factory.getPortStub() instanceof Stub);
099         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             // expected
175         }
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             // expected
214         }
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 {
Rate280             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 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
280 0% of 0