KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > httpinvoker > HttpInvokerTests


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.httpinvoker;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.util.zip.GZIPInputStream JavaDoc;
29 import java.util.zip.GZIPOutputStream JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import junit.framework.TestCase;
35 import org.aopalliance.intercept.MethodInvocation;
36
37 import org.springframework.beans.ITestBean;
38 import org.springframework.beans.TestBean;
39 import org.springframework.mock.web.MockHttpServletRequest;
40 import org.springframework.mock.web.MockHttpServletResponse;
41 import org.springframework.remoting.RemoteAccessException;
42 import org.springframework.remoting.support.DefaultRemoteInvocationExecutor;
43 import org.springframework.remoting.support.RemoteInvocation;
44 import org.springframework.remoting.support.RemoteInvocationFactory;
45 import org.springframework.remoting.support.RemoteInvocationResult;
46
47 /**
48  * @author Juergen Hoeller
49  * @since 09.08.2004
50  */

51 public class HttpInvokerTests extends TestCase {
52
53     public void testHttpInvokerProxyFactoryBeanAndServiceExporter() throws Throwable JavaDoc {
54         TestBean target = new TestBean("myname", 99);
55
56         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
57         exporter.setServiceInterface(ITestBean.class);
58         exporter.setService(target);
59         exporter.afterPropertiesSet();
60
61         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
62         pfb.setServiceInterface(ITestBean.class);
63         pfb.setServiceUrl("http://myurl");
64
65         pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
66             protected RemoteInvocationResult doExecuteRequest(
67                     HttpInvokerClientConfiguration config, ByteArrayOutputStream JavaDoc baos)
68                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
69                 assertEquals("http://myurl", config.getServiceUrl());
70                 MockHttpServletRequest request = new MockHttpServletRequest();
71                 MockHttpServletResponse response = new MockHttpServletResponse();
72                 request.setContent(baos.toByteArray());
73                 exporter.handleRequest(request, response);
74                 return readRemoteInvocationResult(
75                         new ByteArrayInputStream JavaDoc(response.getContentAsByteArray()), config.getCodebaseUrl());
76             }
77         });
78
79         pfb.afterPropertiesSet();
80         ITestBean proxy = (ITestBean) pfb.getObject();
81         assertEquals("myname", proxy.getName());
82         assertEquals(99, proxy.getAge());
83         proxy.setAge(50);
84         assertEquals(50, proxy.getAge());
85
86         try {
87             proxy.exceptional(new IllegalStateException JavaDoc());
88             fail("Should have thrown IllegalStateException");
89         }
90         catch (IllegalStateException JavaDoc ex) {
91             // expected
92
}
93         try {
94             proxy.exceptional(new IllegalAccessException JavaDoc());
95             fail("Should have thrown IllegalAccessException");
96         }
97         catch (IllegalAccessException JavaDoc ex) {
98             // expected
99
}
100     }
101
102     public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithIOException() throws Exception JavaDoc {
103         TestBean target = new TestBean("myname", 99);
104
105         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
106         exporter.setServiceInterface(ITestBean.class);
107         exporter.setService(target);
108         exporter.afterPropertiesSet();
109
110         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
111         pfb.setServiceInterface(ITestBean.class);
112         pfb.setServiceUrl("http://myurl");
113
114         pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
115             public RemoteInvocationResult executeRequest(
116                     HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException JavaDoc {
117                 throw new IOException JavaDoc("argh");
118             }
119         });
120
121         pfb.afterPropertiesSet();
122         ITestBean proxy = (ITestBean) pfb.getObject();
123         try {
124             proxy.setAge(50);
125             fail("Should have thrown RemoteAccessException");
126         }
127         catch (RemoteAccessException ex) {
128             // expected
129
assertTrue(ex.getCause() instanceof IOException JavaDoc);
130         }
131     }
132
133     public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithGzipCompression() throws Throwable JavaDoc {
134         TestBean target = new TestBean("myname", 99);
135
136         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
137             protected InputStream JavaDoc decorateInputStream(HttpServletRequest JavaDoc request, InputStream JavaDoc is) throws IOException JavaDoc {
138                 if ("gzip".equals(request.getHeader("Compression"))) {
139                     return new GZIPInputStream JavaDoc(is);
140                 }
141                 else {
142                     return is;
143                 }
144             }
145             protected OutputStream JavaDoc decorateOutputStream(
146                     HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, OutputStream JavaDoc os) throws IOException JavaDoc {
147                 if ("gzip".equals(request.getHeader("Compression"))) {
148                     return new GZIPOutputStream JavaDoc(os);
149                 }
150                 else {
151                     return os;
152                 }
153             }
154         };
155         exporter.setServiceInterface(ITestBean.class);
156         exporter.setService(target);
157         exporter.afterPropertiesSet();
158
159         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
160         pfb.setServiceInterface(ITestBean.class);
161         pfb.setServiceUrl("http://myurl");
162
163         pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
164             protected RemoteInvocationResult doExecuteRequest(
165                     HttpInvokerClientConfiguration config, ByteArrayOutputStream JavaDoc baos)
166                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
167                 assertEquals("http://myurl", config.getServiceUrl());
168                 MockHttpServletRequest request = new MockHttpServletRequest();
169                 request.addHeader("Compression", "gzip");
170                 MockHttpServletResponse response = new MockHttpServletResponse();
171                 request.setContent(baos.toByteArray());
172                 exporter.handleRequest(request, response);
173                 return readRemoteInvocationResult(
174                         new ByteArrayInputStream JavaDoc(response.getContentAsByteArray()), config.getCodebaseUrl());
175             }
176             protected OutputStream JavaDoc decorateOutputStream(OutputStream JavaDoc os) throws IOException JavaDoc {
177                 return new GZIPOutputStream JavaDoc(os);
178             }
179             protected InputStream JavaDoc decorateInputStream(InputStream JavaDoc is) throws IOException JavaDoc {
180                 return new GZIPInputStream JavaDoc(is);
181             }
182         });
183
184         pfb.afterPropertiesSet();
185         ITestBean proxy = (ITestBean) pfb.getObject();
186         assertEquals("myname", proxy.getName());
187         assertEquals(99, proxy.getAge());
188         proxy.setAge(50);
189         assertEquals(50, proxy.getAge());
190
191         try {
192             proxy.exceptional(new IllegalStateException JavaDoc());
193             fail("Should have thrown IllegalStateException");
194         }
195         catch (IllegalStateException JavaDoc ex) {
196             // expected
197
}
198         try {
199             proxy.exceptional(new IllegalAccessException JavaDoc());
200             fail("Should have thrown IllegalAccessException");
201         }
202         catch (IllegalAccessException JavaDoc ex) {
203             // expected
204
}
205     }
206
207     public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations() throws Throwable JavaDoc {
208         TestBean target = new TestBean("myname", 99);
209
210         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {
211             protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream JavaDoc ois)
212                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
213                 Object JavaDoc obj = ois.readObject();
214                 if (!(obj instanceof TestRemoteInvocationWrapper)) {
215                     throw new IOException JavaDoc("Deserialized object needs to be assignable to type [" +
216                             TestRemoteInvocationWrapper.class.getName() + "]: " + obj);
217                 }
218                 return ((TestRemoteInvocationWrapper) obj).remoteInvocation;
219             }
220             protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream JavaDoc oos)
221                     throws IOException JavaDoc {
222                 oos.writeObject(new TestRemoteInvocationResultWrapper(result));
223             }
224         };
225         exporter.setServiceInterface(ITestBean.class);
226         exporter.setService(target);
227         exporter.afterPropertiesSet();
228
229         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
230         pfb.setServiceInterface(ITestBean.class);
231         pfb.setServiceUrl("http://myurl");
232
233         pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
234             protected RemoteInvocationResult doExecuteRequest(
235                     HttpInvokerClientConfiguration config, ByteArrayOutputStream JavaDoc baos)
236                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
237                 assertEquals("http://myurl", config.getServiceUrl());
238                 MockHttpServletRequest request = new MockHttpServletRequest();
239                 MockHttpServletResponse response = new MockHttpServletResponse();
240                 request.setContent(baos.toByteArray());
241                 exporter.handleRequest(request, response);
242                 return readRemoteInvocationResult(
243                         new ByteArrayInputStream JavaDoc(response.getContentAsByteArray()), config.getCodebaseUrl());
244             }
245             protected void doWriteRemoteInvocation(RemoteInvocation invocation, ObjectOutputStream JavaDoc oos) throws IOException JavaDoc {
246                 oos.writeObject(new TestRemoteInvocationWrapper(invocation));
247             }
248             protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream JavaDoc ois)
249                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
250                 Object JavaDoc obj = ois.readObject();
251                 if (!(obj instanceof TestRemoteInvocationResultWrapper)) {
252                     throw new IOException JavaDoc("Deserialized object needs to be assignable to type ["
253                             + TestRemoteInvocationResultWrapper.class.getName() + "]: " + obj);
254                 }
255                 return ((TestRemoteInvocationResultWrapper) obj).remoteInvocationResult;
256             }
257         });
258
259         pfb.afterPropertiesSet();
260         ITestBean proxy = (ITestBean) pfb.getObject();
261         assertEquals("myname", proxy.getName());
262         assertEquals(99, proxy.getAge());
263         proxy.setAge(50);
264         assertEquals(50, proxy.getAge());
265
266         try {
267             proxy.exceptional(new IllegalStateException JavaDoc());
268             fail("Should have thrown IllegalStateException");
269         }
270         catch (IllegalStateException JavaDoc ex) {
271             // expected
272
}
273         try {
274             proxy.exceptional(new IllegalAccessException JavaDoc());
275             fail("Should have thrown IllegalAccessException");
276         }
277         catch (IllegalAccessException JavaDoc ex) {
278             // expected
279
}
280     }
281
282     public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception JavaDoc {
283         TestBean target = new TestBean("myname", 99);
284
285         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
286         exporter.setServiceInterface(ITestBean.class);
287         exporter.setService(target);
288         exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {
289             public Object JavaDoc invoke(RemoteInvocation invocation, Object JavaDoc targetObject)
290                     throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
291                 assertNotNull(invocation.getAttributes());
292                 assertEquals(1, invocation.getAttributes().size());
293                 assertEquals("myValue", invocation.getAttributes().get("myKey"));
294                 assertEquals("myValue", invocation.getAttribute("myKey"));
295                 return super.invoke(invocation, targetObject);
296             }
297         });
298         exporter.afterPropertiesSet();
299
300         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
301         pfb.setServiceInterface(ITestBean.class);
302         pfb.setServiceUrl("http://myurl");
303         pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {
304             public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
305                 RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
306                 invocation.addAttribute("myKey", "myValue");
307                 try {
308                     invocation.addAttribute("myKey", "myValue");
309                     fail("Should have thrown IllegalStateException");
310                 }
311                 catch (IllegalStateException JavaDoc ex) {
312                     // expected: already defined
313
}
314                 assertNotNull(invocation.getAttributes());
315                 assertEquals(1, invocation.getAttributes().size());
316                 assertEquals("myValue", invocation.getAttributes().get("myKey"));
317                 assertEquals("myValue", invocation.getAttribute("myKey"));
318                 return invocation;
319             }
320         });
321
322         pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
323             protected RemoteInvocationResult doExecuteRequest(
324                     HttpInvokerClientConfiguration config, ByteArrayOutputStream JavaDoc baos)
325                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
326                 assertEquals("http://myurl", config.getServiceUrl());
327                 MockHttpServletRequest request = new MockHttpServletRequest();
328                 MockHttpServletResponse response = new MockHttpServletResponse();
329                 request.setContent(baos.toByteArray());
330                 exporter.handleRequest(request, response);
331                 return readRemoteInvocationResult(
332                         new ByteArrayInputStream JavaDoc(response.getContentAsByteArray()), config.getCodebaseUrl());
333             }
334         });
335
336         pfb.afterPropertiesSet();
337         ITestBean proxy = (ITestBean) pfb.getObject();
338         assertEquals("myname", proxy.getName());
339         assertEquals(99, proxy.getAge());
340     }
341
342     public void testHttpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception JavaDoc {
343         TestBean target = new TestBean("myname", 99);
344
345         final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
346         exporter.setServiceInterface(ITestBean.class);
347         exporter.setService(target);
348         exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {
349             public Object JavaDoc invoke(RemoteInvocation invocation, Object JavaDoc targetObject)
350                     throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
351                 assertTrue(invocation instanceof TestRemoteInvocation);
352                 assertNull(invocation.getAttributes());
353                 assertNull(invocation.getAttribute("myKey"));
354                 return super.invoke(invocation, targetObject);
355             }
356         });
357         exporter.afterPropertiesSet();
358
359         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
360         pfb.setServiceInterface(ITestBean.class);
361         pfb.setServiceUrl("http://myurl");
362         pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {
363             public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
364                 RemoteInvocation invocation = new TestRemoteInvocation(methodInvocation);
365                 assertNull(invocation.getAttributes());
366                 assertNull(invocation.getAttribute("myKey"));
367                 return invocation;
368             }
369         });
370
371         pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
372             protected RemoteInvocationResult doExecuteRequest(
373                     HttpInvokerClientConfiguration config, ByteArrayOutputStream JavaDoc baos)
374                     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
375                 assertEquals("http://myurl", config.getServiceUrl());
376                 MockHttpServletRequest request = new MockHttpServletRequest();
377                 MockHttpServletResponse response = new MockHttpServletResponse();
378                 request.setContent(baos.toByteArray());
379                 exporter.handleRequest(request, response);
380                 return readRemoteInvocationResult(
381                         new ByteArrayInputStream JavaDoc(response.getContentAsByteArray()), config.getCodebaseUrl());
382             }
383         });
384
385         pfb.afterPropertiesSet();
386         ITestBean proxy = (ITestBean) pfb.getObject();
387         assertEquals("myname", proxy.getName());
388         assertEquals(99, proxy.getAge());
389     }
390
391     public void testHttpInvokerWithSpecialLocalMethods() throws Exception JavaDoc {
392         String JavaDoc serviceUrl = "http://myurl";
393         HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
394         pfb.setServiceInterface(ITestBean.class);
395         pfb.setServiceUrl(serviceUrl);
396
397         pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {
398             public RemoteInvocationResult executeRequest(
399                     HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException JavaDoc {
400                 throw new IOException JavaDoc("argh");
401             }
402         });
403
404         pfb.afterPropertiesSet();
405         ITestBean proxy = (ITestBean) pfb.getObject();
406
407         // shouldn't go through to remote service
408
assertTrue(proxy.toString().indexOf("HTTP invoker") != -1);
409         assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
410         assertEquals(proxy.hashCode(), proxy.hashCode());
411         assertTrue(proxy.equals(proxy));
412
413         // should go through
414
try {
415             proxy.setAge(50);
416             fail("Should have thrown RemoteAccessException");
417         }
418         catch (RemoteAccessException ex) {
419             // expected
420
assertTrue(ex.getCause() instanceof IOException JavaDoc);
421         }
422     }
423
424
425     private static class TestRemoteInvocation extends RemoteInvocation {
426
427         public TestRemoteInvocation(MethodInvocation methodInvocation) {
428             super(methodInvocation);
429         }
430     }
431
432
433     private static class TestRemoteInvocationWrapper implements Serializable JavaDoc {
434
435         private final RemoteInvocation remoteInvocation;
436
437         public TestRemoteInvocationWrapper(RemoteInvocation remoteInvocation) {
438             this.remoteInvocation = remoteInvocation;
439         }
440     }
441
442
443     private static class TestRemoteInvocationResultWrapper implements Serializable JavaDoc {
444
445         private final RemoteInvocationResult remoteInvocationResult;
446
447         public TestRemoteInvocationResultWrapper(RemoteInvocationResult remoteInvocationResult) {
448             this.remoteInvocationResult = remoteInvocationResult;
449         }
450     }
451
452 }
453
Popular Tags