KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > rmi > RmiSupportTests


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.rmi;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.rmi.ConnectException JavaDoc;
21 import java.rmi.ConnectIOException JavaDoc;
22 import java.rmi.MarshalException JavaDoc;
23 import java.rmi.NoSuchObjectException JavaDoc;
24 import java.rmi.Remote JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.rmi.StubNotFoundException JavaDoc;
27 import java.rmi.UnknownHostException JavaDoc;
28 import java.rmi.UnmarshalException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import junit.framework.TestCase;
32 import org.aopalliance.intercept.MethodInvocation;
33
34 import org.springframework.aop.framework.ReflectiveMethodInvocation;
35 import org.springframework.remoting.RemoteAccessException;
36 import org.springframework.remoting.RemoteConnectFailureException;
37 import org.springframework.remoting.support.RemoteInvocation;
38
39 /**
40  * @author Juergen Hoeller
41  * @since 16.05.2003
42  */

43 public class RmiSupportTests extends TestCase {
44
45     public void testRmiProxyFactoryBean() throws Exception JavaDoc {
46         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
47         factory.setServiceInterface(IRemoteBean.class);
48         factory.setServiceUrl("rmi://localhost:1090/test");
49         factory.afterPropertiesSet();
50         assertTrue("Correct singleton value", factory.isSingleton());
51         assertTrue(factory.getObject() instanceof IRemoteBean);
52         IRemoteBean proxy = (IRemoteBean) factory.getObject();
53         proxy.setName("myName");
54         assertEquals(RemoteBean.name, "myName");
55         assertEquals(1, factory.counter);
56     }
57
58     public void testRmiProxyFactoryBeanWithRemoteException() throws Exception JavaDoc {
59         doTestRmiProxyFactoryBeanWithException(RemoteException JavaDoc.class);
60     }
61
62     public void testRmiProxyFactoryBeanWithConnectException() throws Exception JavaDoc {
63         doTestRmiProxyFactoryBeanWithException(ConnectException JavaDoc.class);
64     }
65
66     public void testRmiProxyFactoryBeanWithConnectIOException() throws Exception JavaDoc {
67         doTestRmiProxyFactoryBeanWithException(ConnectIOException JavaDoc.class);
68     }
69
70     public void testRmiProxyFactoryBeanWithUnknownHostException() throws Exception JavaDoc {
71         doTestRmiProxyFactoryBeanWithException(UnknownHostException JavaDoc.class);
72     }
73
74     public void testRmiProxyFactoryBeanWithNoSuchObjectException() throws Exception JavaDoc {
75         doTestRmiProxyFactoryBeanWithException(NoSuchObjectException JavaDoc.class);
76     }
77
78     public void testRmiProxyFactoryBeanWithStubNotFoundException() throws Exception JavaDoc {
79         doTestRmiProxyFactoryBeanWithException(StubNotFoundException JavaDoc.class);
80     }
81
82     public void testRmiProxyFactoryBeanWithMarshalException() throws Exception JavaDoc {
83         doTestRmiProxyFactoryBeanWithException(MarshalException JavaDoc.class);
84     }
85
86     public void testRmiProxyFactoryBeanWithUnmarshalException() throws Exception JavaDoc {
87         doTestRmiProxyFactoryBeanWithException(UnmarshalException JavaDoc.class);
88     }
89
90     private void doTestRmiProxyFactoryBeanWithException(Class JavaDoc exceptionClass) throws Exception JavaDoc {
91         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
92         factory.setServiceInterface(IRemoteBean.class);
93         factory.setServiceUrl("rmi://localhost:1090/test");
94         factory.afterPropertiesSet();
95         assertTrue(factory.getObject() instanceof IRemoteBean);
96         IRemoteBean proxy = (IRemoteBean) factory.getObject();
97         try {
98             proxy.setName(exceptionClass.getName());
99             fail("Should have thrown " + exceptionClass.getName());
100         }
101         catch (Exception JavaDoc ex) {
102             if (exceptionClass.isInstance(ex)) {
103                 // expected
104
}
105             else {
106                 throw ex;
107             }
108         }
109         assertEquals(1, factory.counter);
110     }
111
112     public void testRmiProxyFactoryBeanWithConnectExceptionAndRefresh() throws Exception JavaDoc {
113         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectException JavaDoc.class);
114     }
115
116     public void testRmiProxyFactoryBeanWithConnectIOExceptionAndRefresh() throws Exception JavaDoc {
117         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectIOException JavaDoc.class);
118     }
119
120     public void testRmiProxyFactoryBeanWithUnknownHostExceptionAndRefresh() throws Exception JavaDoc {
121         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(UnknownHostException JavaDoc.class);
122     }
123
124     public void testRmiProxyFactoryBeanWithNoSuchObjectExceptionAndRefresh() throws Exception JavaDoc {
125         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(NoSuchObjectException JavaDoc.class);
126     }
127
128     public void testRmiProxyFactoryBeanWithStubNotFoundExceptionAndRefresh() throws Exception JavaDoc {
129         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(StubNotFoundException JavaDoc.class);
130     }
131
132     public void testRmiProxyFactoryBeanWithMarshalExceptionAndRefresh() throws Exception JavaDoc {
133         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(MarshalException JavaDoc.class);
134     }
135
136     public void testRmiProxyFactoryBeanWithUnmarshalExceptionAndRefresh() throws Exception JavaDoc {
137         doTestRmiProxyFactoryBeanWithExceptionAndRefresh(UnmarshalException JavaDoc.class);
138     }
139
140     private void doTestRmiProxyFactoryBeanWithExceptionAndRefresh(Class JavaDoc exceptionClass) throws Exception JavaDoc {
141         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
142         factory.setServiceInterface(IRemoteBean.class);
143         factory.setServiceUrl("rmi://localhost:1090/test");
144         factory.setRefreshStubOnConnectFailure(true);
145         factory.afterPropertiesSet();
146         assertTrue(factory.getObject() instanceof IRemoteBean);
147         IRemoteBean proxy = (IRemoteBean) factory.getObject();
148         try {
149             proxy.setName(exceptionClass.getName());
150             fail("Should have thrown " + exceptionClass.getName());
151         }
152         catch (Exception JavaDoc ex) {
153             if (exceptionClass.isInstance(ex)) {
154                 // expected
155
}
156             else {
157                 throw ex;
158             }
159         }
160         assertEquals(2, factory.counter);
161     }
162
163     public void testRmiProxyFactoryBeanWithBusinessInterface() throws Exception JavaDoc {
164         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
165         factory.setServiceInterface(IBusinessBean.class);
166         factory.setServiceUrl("rmi://localhost:1090/test");
167         factory.afterPropertiesSet();
168         assertTrue(factory.getObject() instanceof IBusinessBean);
169         IBusinessBean proxy = (IBusinessBean) factory.getObject();
170         assertFalse(proxy instanceof IRemoteBean);
171         proxy.setName("myName");
172         assertEquals(RemoteBean.name, "myName");
173         assertEquals(1, factory.counter);
174     }
175
176     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteException() throws Exception JavaDoc {
177         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
178                 RemoteException JavaDoc.class, RemoteAccessException.class);
179     }
180
181     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectException() throws Exception JavaDoc {
182         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
183                 ConnectException JavaDoc.class, RemoteConnectFailureException.class);
184     }
185
186     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOException() throws Exception JavaDoc {
187         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
188                 ConnectIOException JavaDoc.class, RemoteConnectFailureException.class);
189     }
190
191     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostException() throws Exception JavaDoc {
192         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
193                 UnknownHostException JavaDoc.class, RemoteConnectFailureException.class);
194     }
195
196     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionException() throws Exception JavaDoc {
197         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
198                 NoSuchObjectException JavaDoc.class, RemoteConnectFailureException.class);
199     }
200
201     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundException() throws Exception JavaDoc {
202         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
203                 StubNotFoundException JavaDoc.class, RemoteConnectFailureException.class);
204     }
205
206     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndMarshalException() throws Exception JavaDoc {
207         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
208                 MarshalException JavaDoc.class, RemoteConnectFailureException.class);
209     }
210
211     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnmarshalException() throws Exception JavaDoc {
212         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
213                 UnmarshalException JavaDoc.class, RemoteConnectFailureException.class);
214     }
215
216     private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
217             Class JavaDoc rmiExceptionClass, Class JavaDoc springExceptionClass) throws Exception JavaDoc {
218         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
219         factory.setServiceInterface(IBusinessBean.class);
220         factory.setServiceUrl("rmi://localhost:1090/test");
221         factory.afterPropertiesSet();
222         assertTrue(factory.getObject() instanceof IBusinessBean);
223         IBusinessBean proxy = (IBusinessBean) factory.getObject();
224         assertFalse(proxy instanceof IRemoteBean);
225         try {
226             proxy.setName(rmiExceptionClass.getName());
227             fail("Should have thrown " + rmiExceptionClass.getName());
228         }
229         catch (Exception JavaDoc ex) {
230             if (springExceptionClass.isInstance(ex)) {
231                 // expected
232
}
233             else {
234                 throw ex;
235             }
236         }
237         assertEquals(1, factory.counter);
238     }
239
240     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteExceptionAndRefresh() throws Exception JavaDoc {
241         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
242                 RemoteException JavaDoc.class, RemoteAccessException.class);
243     }
244
245     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectExceptionAndRefresh() throws Exception JavaDoc {
246         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
247                 ConnectException JavaDoc.class, RemoteConnectFailureException.class);
248     }
249
250     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOExceptionAndRefresh() throws Exception JavaDoc {
251         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
252                 ConnectIOException JavaDoc.class, RemoteConnectFailureException.class);
253     }
254
255     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostExceptionAndRefresh() throws Exception JavaDoc {
256         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
257                 UnknownHostException JavaDoc.class, RemoteConnectFailureException.class);
258     }
259
260     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionAndRefresh() throws Exception JavaDoc {
261         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
262                 NoSuchObjectException JavaDoc.class, RemoteConnectFailureException.class);
263     }
264
265     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundExceptionAndRefresh() throws Exception JavaDoc {
266         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
267                 StubNotFoundException JavaDoc.class, RemoteConnectFailureException.class);
268     }
269
270     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndMarshalExceptionAndRefresh() throws Exception JavaDoc {
271         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
272                 MarshalException JavaDoc.class, RemoteConnectFailureException.class);
273     }
274
275     public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnmarshalExceptionAndRefresh() throws Exception JavaDoc {
276         doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
277                 UnmarshalException JavaDoc.class, RemoteConnectFailureException.class);
278     }
279
280     private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
281             Class JavaDoc rmiExceptionClass, Class JavaDoc springExceptionClass) throws Exception JavaDoc {
282         
283         CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
284         factory.setServiceInterface(IBusinessBean.class);
285         factory.setServiceUrl("rmi://localhost:1090/test");
286         factory.setRefreshStubOnConnectFailure(true);
287         factory.afterPropertiesSet();
288         assertTrue(factory.getObject() instanceof IBusinessBean);
289         IBusinessBean proxy = (IBusinessBean) factory.getObject();
290         assertFalse(proxy instanceof IRemoteBean);
291         try {
292             proxy.setName(rmiExceptionClass.getName());
293             fail("Should have thrown " + rmiExceptionClass.getName());
294         }
295         catch (Exception JavaDoc ex) {
296             if (springExceptionClass.isInstance(ex)) {
297                 // expected
298
}
299             else {
300                 throw ex;
301             }
302         }
303         if (RemoteConnectFailureException.class.isAssignableFrom(springExceptionClass)) {
304             assertEquals(2, factory.counter);
305         }
306         else {
307             assertEquals(1, factory.counter);
308         }
309     }
310
311     public void testRmiClientInterceptorRequiresUrl() throws Exception JavaDoc{
312         RmiClientInterceptor client = new RmiClientInterceptor();
313         client.setServiceInterface(IRemoteBean.class);
314
315         try {
316             client.afterPropertiesSet();
317             fail("url isn't set, expected IllegalArgumentException");
318         }
319         catch(IllegalArgumentException JavaDoc e){
320             // expected
321
}
322     }
323
324     public void testRemoteInvocation() throws NoSuchMethodException JavaDoc {
325         // let's see if the remote invocation object works
326

327         RemoteBean rb = new RemoteBean();
328
329         MethodInvocation mi = new ReflectiveMethodInvocation(
330                 rb, rb, rb.getClass().getDeclaredMethod("setName", new Class JavaDoc[] {String JavaDoc.class}),
331             new Object JavaDoc[] { "bla" }, RemoteBean.class, new ArrayList JavaDoc());
332
333         RemoteInvocation inv = new RemoteInvocation(mi);
334
335         assertEquals("setName", inv.getMethodName());
336         assertEquals("bla", inv.getArguments()[0]);
337         assertEquals(String JavaDoc.class, inv.getParameterTypes()[0]);
338
339         // this is a bit BS, but we need to test it
340
inv = new RemoteInvocation();
341         inv.setArguments(new Object JavaDoc[] { "bla" });
342         assertEquals("bla", inv.getArguments()[0]);
343         inv.setMethodName("setName");
344         assertEquals("setName", inv.getMethodName());
345         inv.setParameterTypes(new Class JavaDoc[] {String JavaDoc.class});
346         assertEquals(String JavaDoc.class, inv.getParameterTypes()[0]);
347
348         inv = new RemoteInvocation("setName", new Class JavaDoc[] {String JavaDoc.class}, new Object JavaDoc[] {"bla"});
349         assertEquals("bla", inv.getArguments()[0]);
350         assertEquals("setName", inv.getMethodName());
351         assertEquals(String JavaDoc.class, inv.getParameterTypes()[0]);
352     }
353
354     public void testRmiInvokerWithSpecialLocalMethods() throws Exception JavaDoc {
355         String JavaDoc serviceUrl = "rmi://localhost:1090/test";
356         RmiProxyFactoryBean factory = new RmiProxyFactoryBean() {
357             protected Remote JavaDoc lookupStub() throws Exception JavaDoc {
358                 return new RmiInvocationHandler() {
359                     public Object JavaDoc invoke(RemoteInvocation invocation) throws RemoteException JavaDoc {
360                         throw new RemoteException JavaDoc();
361                     }
362                 };
363             }
364         };
365         factory.setServiceInterface(IBusinessBean.class);
366         factory.setServiceUrl(serviceUrl);
367         factory.afterPropertiesSet();
368         IBusinessBean proxy = (IBusinessBean) factory.getObject();
369
370         // shouldn't go through to remote service
371
assertTrue(proxy.toString().indexOf("RMI invoker") != -1);
372         assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
373         assertEquals(proxy.hashCode(), proxy.hashCode());
374         assertTrue(proxy.equals(proxy));
375
376         // should go through
377
try {
378             proxy.setName("test");
379             fail("Should have thrown RemoteAccessException");
380         }
381         catch (RemoteAccessException ex) {
382             // expected
383
}
384     }
385
386
387     private static class CountingRmiProxyFactoryBean extends RmiProxyFactoryBean {
388
389         private int counter = 0;
390
391         protected Remote JavaDoc lookupStub() {
392             counter++;
393             return new RemoteBean();
394         }
395     }
396
397
398     public static interface IBusinessBean {
399
400         public void setName(String JavaDoc name);
401
402     }
403
404
405     public static interface IRemoteBean extends Remote JavaDoc {
406
407         public void setName(String JavaDoc name) throws RemoteException JavaDoc;
408
409     }
410
411
412     public static class RemoteBean implements IRemoteBean {
413
414         private static String JavaDoc name;
415
416         public void setName(String JavaDoc nam) throws RemoteException JavaDoc {
417             if (nam != null && nam.endsWith("Exception")) {
418                 RemoteException JavaDoc rex = null;
419                 try {
420                     Class JavaDoc exClass = Class.forName(nam);
421                     Constructor JavaDoc ctor = exClass.getConstructor(new Class JavaDoc[] {String JavaDoc.class});
422                     rex = (RemoteException JavaDoc) ctor.newInstance(new Object JavaDoc[] {"myMessage"});
423                 }
424                 catch (Exception JavaDoc ex) {
425                     throw new RemoteException JavaDoc("Illegal exception class name: " + nam, ex);
426                 }
427                 throw rex;
428             }
429             name = nam;
430         }
431     }
432
433 }
434
Popular Tags