KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > jaxrpc > JaxRpcSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.remoting.jaxrpc;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.rmi.Remote JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Arrays JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.rpc.Call JavaDoc;
30 import javax.xml.rpc.Service JavaDoc;
31 import javax.xml.rpc.ServiceException JavaDoc;
32 import javax.xml.rpc.ServiceFactory JavaDoc;
33 import javax.xml.rpc.Stub JavaDoc;
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 /**
42  * @author Juergen Hoeller
43  * @since 18.12.2003
44  */

45 public class JaxRpcSupportTests extends TestCase {
46
47     public void testLocalJaxRpcServiceFactoryBeanWithWsdlAndNamespace() throws Exception JavaDoc {
48         LocalJaxRpcServiceFactoryBean factory = new LocalJaxRpcServiceFactoryBean();
49         factory.setServiceFactoryClass(MockServiceFactory.class);
50         factory.setWsdlDocumentUrl(new URL JavaDoc("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 JavaDoc {
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 JavaDoc {
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 JavaDoc);
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 JavaDoc {
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 JavaDoc);
99         Stub JavaDoc stub = (Stub JavaDoc) 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 JavaDoc) 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc);
161         Stub JavaDoc stub = (Stub JavaDoc) 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 JavaDoc ex) {
174             // expected
175
}
176         MockServiceFactory.service1Control.verify();
177     }
178
179     public void testJaxRpcPortProxyFactoryBeanWithPortInterface() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
220
221         protected static MockControl service1Control;
222         protected static Service JavaDoc service1;
223         protected static MockControl service2Control;
224         protected static Service JavaDoc service2;
225
226         public MockServiceFactory() throws Exception JavaDoc {
227             service1Control = MockControl.createControl(Service JavaDoc.class);
228             service1 = (Service JavaDoc) service1Control.getMock();
229             service2Control = MockControl.createControl(Service JavaDoc.class);
230             service2 = (Service JavaDoc) service2Control.getMock();
231             initMocks();
232             service1Control.replay();
233         }
234
235         protected void initMocks() throws Exception JavaDoc {
236             service1.getPort(new QName JavaDoc("myNamespace", "myPort"), IRemoteBean.class);
237             service1Control.setReturnValue(new RemoteBean());
238         }
239
240         public Service JavaDoc createService(URL JavaDoc url, QName JavaDoc qName) throws ServiceException JavaDoc {
241             try {
242                 if (!(new URL JavaDoc("http://myUrl1")).equals(url) || !"".equals(qName.getNamespaceURI()) ||
243                         !"myService2".equals(qName.getLocalPart())) {
244                     throw new ServiceException JavaDoc("not supported");
245                 }
246             }
247             catch (MalformedURLException JavaDoc ex) {
248             }
249             return service2;
250         }
251
252         public Service JavaDoc createService(QName JavaDoc qName) throws ServiceException JavaDoc {
253             if (!"myNamespace".equals(qName.getNamespaceURI()) || !"myService1".equals(qName.getLocalPart())) {
254                 throw new ServiceException JavaDoc("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 JavaDoc call1;
265
266         public CallMockServiceFactory() throws Exception JavaDoc {
267             super();
268         }
269
270         protected void initMocks() throws Exception JavaDoc {
271             call1Control = MockControl.createControl(Call JavaDoc.class);
272             call1 = (Call JavaDoc) call1Control.getMock();
273             service1.createCall(new QName JavaDoc("myNamespace", "myPort"), "setName");
274             service1Control.setReturnValue(call1);
275             initCall();
276             call1Control.replay();
277         }
278
279         protected void initCall() throws Exception JavaDoc {
280             call1.invoke(new Object JavaDoc[] {"myName"});
281             call1Control.setMatcher(new ArgumentsMatcher() {
282                 public boolean matches(Object JavaDoc[] objects, Object JavaDoc[] objects1) {
283                     return Arrays.equals((Object JavaDoc[]) objects[0], (Object JavaDoc[]) objects1[0]);
284                 }
285                 public String JavaDoc toString(Object JavaDoc[] 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 JavaDoc {
297         }
298
299         protected void initCall() throws Exception JavaDoc {
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 JavaDoc name);
316
317     }
318
319
320     public static interface IRemoteBean extends Remote JavaDoc {
321
322         public void setName(String JavaDoc name) throws RemoteException JavaDoc;
323
324     }
325
326
327     public static class RemoteBean implements IRemoteBean, Stub JavaDoc {
328
329         private static RemoteBean singleton;
330         private static String JavaDoc name;
331         private static Map JavaDoc properties;
332
333         public RemoteBean() {
334             singleton = this;
335             properties = new HashMap JavaDoc();
336         }
337
338         public void setName(String JavaDoc nam) throws RemoteException JavaDoc {
339             if ("exception".equals(nam)) {
340                 throw new RemoteException JavaDoc();
341             }
342             name = nam;
343         }
344
345         public void _setProperty(String JavaDoc key, Object JavaDoc o) {
346             properties.put(key, o);
347         }
348
349         public Object JavaDoc _getProperty(String JavaDoc key) {
350             return properties.get(key);
351         }
352
353         public Iterator JavaDoc _getPropertyNames() {
354             return properties.keySet().iterator();
355         }
356     }
357
358 }
359
Popular Tags