KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > imp > TestPortletInterface


1 /**
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.portletcontainer.imp;
6
7 import java.util.Locale JavaDoc;
8
9 import javax.portlet.GenericPortlet;
10 import javax.portlet.Portlet;
11 import javax.portlet.PortletException;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import org.exoplatform.container.PortalContainer;
15 import org.exoplatform.services.portletcontainer.PortletContainerConstants;
16 import org.exoplatform.services.portletcontainer.PortletContainerException;
17 import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy;
18 import org.exoplatform.services.portletcontainer.impl.monitor.PortletMonitor;
19 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.pool.EmptyResponse;
20 import org.exoplatform.services.portletcontainer.monitor.PortletRuntimeData;
21 import org.exoplatform.services.portletcontainer.pci.*;
22 import org.exoplatform.test.mocks.servlet.MockHttpSession;
23 import org.exoplatform.test.mocks.servlet.MockServletRequest;
24 import org.exoplatform.test.mocks.servlet.MockServletResponse;
25
26
27 /**
28  * Created by the Exo Development team.
29  * Author : Mestrallet Benjamin
30  * benjmestrallet@users.sourceforge.net
31  * Date: 11 nov. 2003
32  * Time: 23:43:29
33  */

34 public class TestPortletInterface extends BaseTest {
35
36   PortletApplicationProxy proxy;
37
38     public void setUp() throws Exception JavaDoc {
39         super.setUp();
40     proxy = (PortletApplicationProxy) PortalContainer.getInstance().
41         getComponentInstance("hello");
42     }
43
44     public TestPortletInterface(String JavaDoc s) {
45         super(s);
46     }
47
48     /**
49      * Test (i)
50      * PLT.5.1
51      */

52     public void testPortletUnicity() throws Exception JavaDoc {
53         Portlet p1 = proxy.getPortlet(portletContext, "HelloWorld");
54         Portlet p2 = proxy.getPortlet(portletContext, "HelloWorld");
55         assertEquals(p1, p2);
56     }
57
58     /**
59      * Test (iii)
60      * PLT.5.2.1
61      */

62     public void testClassLoader() throws PortletException {
63         //assertEquals(cl2, proxy.getPortlet(portletContext, "HelloWorld").getClass().getClassLoader());
64
}
65
66     /**
67      * Test (iv)
68      * PLT.5.2.2
69      */

70     public void testInitialization() throws PortletException {
71         GenericPortlet p = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
72         assertNotNull(p.getPortletConfig());
73     }
74
75     /**
76      * test PortletConfig unicity per portlet def
77      * PLT.5.2.2
78      */

79     public void testPortletConfigUnicity() throws PortletException {
80         GenericPortlet p1 = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
81         GenericPortlet p2 = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
82         assertEquals(p1.getPortletConfig(), p2.getPortletConfig());
83     }
84
85     /**
86      * test (v) : test Portlet exception thrown in init method
87      * PLT.5.2.2.1
88      *
89      * test (vi) : test that the destroy method is not called (Should not see any output in conole)
90      * PLT.5.2.2.1
91      */

92     public void testPortletExceptionWhileInit() {
93         GenericPortlet p = null;
94         try {
95             p = (GenericPortlet) proxy.getPortlet(portletContext,
96                             "PortletWithPortletExceptionWhileInit");
97         } catch (Exception JavaDoc e) {
98             assertEquals(e.getMessage(), "exception while initializing portlet");
99             assertTrue(e instanceof PortletException);
100         }
101         assertNull(p);
102         assertFalse(portletMonitor.isInitialized("hello",
103                         "PortletWithPortletExceptionWhileInit"));
104     }
105
106     /**
107      * test (v) : test unavailable exception thrown in init method (Unavailable time 5s)
108      * PLT.5.2.2.1
109      *
110      * test (vi) : test that the destroy method is not called (Should not see any output in console)
111      * PLT.5.2.2.1
112      *
113      * test (vii) : test that the unavailable time period is respected
114      * PLT.5.2.2.1
115      */

116     public void testUnavailableExceptionWhileInit() throws InterruptedException JavaDoc {
117         GenericPortlet p = null;
118         try {
119             p = (GenericPortlet) proxy.getPortlet(portletContext,
120                             "PortletWithUnavailableExceptionWhileInit");
121         } catch (Exception JavaDoc e) {
122             assertEquals("Unavailable portlet", e.getMessage());
123             assertTrue(e instanceof PortletException);
124             PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.getPortletRuntimeDataMap().
125                             get("hello" + PortletMonitor.SEPARATOR +
126                             "PortletWithUnavailableExceptionWhileInit");
127             assertTrue(rD.getLastInitFailureAccessTime() > 0);
128             assertEquals(rD.getUnavailabilityPeriod(), 5000);
129         }
130         Thread.sleep(100);
131         try {
132             p = (GenericPortlet) proxy.getPortlet(portletContext,
133                             "PortletWithUnavailableExceptionWhileInit");
134         } catch (Exception JavaDoc e) {
135             assertEquals("Portlet initialization not possible", e.getMessage());
136             assertTrue(e instanceof PortletException);
137         }
138         Thread.sleep(6000);
139         try {
140             p = (GenericPortlet) proxy.getPortlet(portletContext,
141                             "PortletWithUnavailableExceptionWhileInit");
142         } catch (Exception JavaDoc e) {
143             assertEquals("Unavailable portlet", e.getMessage());
144             assertTrue(e instanceof PortletException);
145         }
146         assertNull(p);
147         assertFalse(portletMonitor.isInitialized("hello",
148                         "PortletWithUnavailableExceptionWhileInit"));
149     }
150
151     /**
152      * test (v) : test unavailable exception thrown in init method (Unavailable time 0s)
153      * PLT.5.2.2.1
154      *
155      * test (vi) : test that the destroy method is not called (Should not see any output in console)
156      * PLT.5.2.2.1
157      *
158      * test (vii) : test that an unavailable time period of 0 sec works too
159      * PLT.5.2.2.1
160      */

161     public void testUnavailableExceptionWhileInit2() throws InterruptedException JavaDoc {
162         GenericPortlet p = null;
163         try {
164             p = (GenericPortlet) proxy.getPortlet(portletContext,
165                             "PortletWithUnavailableExceptionWhileInit2");
166         } catch (Exception JavaDoc e) {
167             assertEquals("Unavailable portlet", e.getMessage());
168             assertTrue(e instanceof PortletException);
169         }
170         Thread.sleep(100);
171         try {
172             p = (GenericPortlet) proxy.getPortlet(portletContext,
173                             "PortletWithUnavailableExceptionWhileInit2");
174         } catch (Exception JavaDoc e) {
175             assertEquals("Unavailable portlet", e.getMessage());
176             assertTrue(e instanceof PortletException);
177         }
178         assertNull(p);
179         assertFalse(portletMonitor.isInitialized("hello",
180                         "PortletWithUnavailableExceptionWhileInit2"));
181     }
182
183     /**
184      * test (viii) : test that Runtime Exception is treated like PortletException
185      * PLT.5.2.2.1
186      */

187     public void testRuntimeExceptionWhileInit() {
188         GenericPortlet p = null;
189         try {
190             p = (GenericPortlet) proxy.getPortlet(portletContext,
191                             "PortletWithRuntimeExceptionWhileInit");
192         } catch (Exception JavaDoc e) {
193             assertEquals(e.getMessage(), "exception while initializing portlet");
194             assertTrue(e instanceof PortletException);
195         }
196         assertNull(p);
197         assertFalse(portletMonitor.isInitialized("hello",
198                         "PortletWithRuntimeExceptionWhileInit"));
199     }
200
201     /**
202      * test (xvii) : If a portlet throws an exception in the processAction method, all operations on
203      * the ActionResponse must be ignored and the render method must not be invoked within
204      * the current client request.
205      * PTL.5.2.4.4
206      */

207     public void testExceptionWhileProcessAction() throws PortletContainerException {
208     ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction");
209         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction");
210         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
211         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
212         portletContainer.processAction(request, response, actionInput);
213     RenderOutput rO = portletContainer.render(request, response, input);
214         assertEquals("Exception occured", rO.getTitle());
215         assertEquals("javax.portlet.PortletException: Exception in processAction", new String JavaDoc(rO.getContent()));
216     assertTrue(portletMonitor.isAvailable("hello",
217         "PortletWithExceptionWhileProcessAction"));
218     }
219
220     /**
221      * test (xviii) : If a permanent unavailability is indicated by the UnavailableException, the portlet
222      * container must remove the portlet from service immediately, call the portlets destroy
223      * method, and release the portlet object.
224      *
225      * test : A portlet that throws a permanent UnavailableException must be considered unavailable
226      * until the portlet application containing the portlet is restarted.
227      *
228      * PTL.5.2.4.4
229      */

230     public void testPortletWithPermanentUnavailableExceptionInProcessAction()
231                     throws Exception JavaDoc {
232         ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
233         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
234         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
235         portletContainer.processAction(request, response, actionInput);
236         assertTrue(portletMonitor.isBroken("hello",
237                         "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
238       PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.
239                         getPortletRuntimeDataMap().get("hello" + PortletMonitor.SEPARATOR +
240                         "PortletWithPermanentUnavailibiltyInProcessActionAndRender");
241         assertNull(rD);
242         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
243         portletContainer.render(request, response, input);
244         assertEquals("Exception occured", portletContainer.render(request, response, input).getTitle());
245         assertEquals("javax.portlet.UnavailableException: Permanent unavailable exception",
246         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
247     assertTrue(portletMonitor.isBroken("hello",
248         "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
249         ActionOutput o = portletContainer.processAction(request, response, actionInput);
250         assertEquals("output generated because of an exception",
251         o.getProperties().get(PortletContainerConstants.EXCEPTION));
252     assertTrue(portletMonitor.isBroken("hello",
253         "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
254         portletApplicationRegister.removePortletApplication(mockServletContext);
255         portletApplicationRegister.registerPortletApplication(mockServletContext, portletApp_, roles);
256         assertFalse(portletMonitor.isBroken("hello",
257                         "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
258         portletContainer.processAction(request, response, actionInput);
259         assertNull(rD);
260     }
261
262     /**
263      * test (xviii) : If a permanent unavailability is indicated by the UnavailableException, the portlet
264      * container must remove the portlet from service immediately, call the portlet's destroy
265      * method, and release the portlet object.
266      *
267      * test : A portlet that throws a permanent UnavailableException must be considered unavailable
268      * until the portlet application containing the portlet is restarted.
269      *
270      * PTL.5.2.4.4
271      */

272     public void testPortletWithPermanentUnavailableExceptionInRender() throws PortletContainerException {
273         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
274         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
275         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
276         RenderOutput o = portletContainer.render(request, response, input);
277         assertEquals("Exception occured", o.getTitle());
278         assertEquals("javax.portlet.UnavailableException: Permanent unavailable exception",
279         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
280         assertTrue(portletMonitor.isBroken("hello",
281         "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
282     }
283
284     /**
285      * test : When temporary unavailability is indicated by the UnavailableException,
286      * then the portlet container may choose not to route any requests to the
287      * portlet during the time period of the temporary unavailability.
288      *
289      * PTL.5.2.4.4
290      */

291     public void testNonPermanentUnavailableExceptionInProcessAction() throws PortletContainerException, InterruptedException JavaDoc {
292         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
293         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
294         ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithNonPermanentUnavailibiltyInProcessActionAndRender");
295         portletContainer.processAction(request, response, actionInput);
296         assertFalse(portletMonitor.isAvailable("hello",
297         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
298         System.currentTimeMillis()));
299         Thread.sleep(5000);
300         assertTrue(portletMonitor.isAvailable("hello",
301                         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender", System.currentTimeMillis()));
302         request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
303         portletContainer.processAction(request, response, actionInput);
304         assertFalse(portletMonitor.isAvailable("hello",
305         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
306         System.currentTimeMillis()));
307         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithNonPermanentUnavailibiltyInProcessActionAndRender");
308         RenderOutput o = portletContainer.render(request, response, input);
309         assertFalse(portletMonitor.isAvailable("hello",
310         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
311         System.currentTimeMillis()));
312         assertEquals("Exception occured", o.getTitle());
313         assertEquals("javax.portlet.UnavailableException: Non Permanent unavailable exception",
314         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
315     }
316
317     /**
318      * test : When temporary unavailability is indicated by the UnavailableException,
319      * then the portlet container may choose not to route any requests to the
320      * portlet during the time period of the temporary unavailability.
321      *
322      * PTL.5.2.4.4
323      */

324     public void testNonPermanentUnavailableExceptionInRender() throws PortletContainerException, InterruptedException JavaDoc {
325         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
326         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
327         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithNonPermanentUnavailibiltyInProcessActionAndRender");
328         RenderOutput o = portletContainer.render(request, response, input);
329         assertFalse(portletMonitor.isAvailable("hello",
330         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
331         System.currentTimeMillis()));
332         assertEquals("Exception occured", o.getTitle());
333         assertEquals("javax.portlet.UnavailableException: Non Permanent unavailable exception",
334         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
335         Thread.sleep(5000);
336         assertTrue(portletMonitor.isAvailable("hello",
337         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
338         System.currentTimeMillis()));
339         request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
340         o = portletContainer.render(request, response, input);
341         assertFalse(portletMonitor.isAvailable("hello",
342         "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
343         System.currentTimeMillis()));
344         assertEquals("Exception occured", o.getTitle());
345         assertEquals("javax.portlet.UnavailableException: Non Permanent unavailable exception",
346         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
347     }
348
349     /**
350      * test (xix) : A RuntimeException thrown during the request handling must be handled as a PortletException
351      *
352      * PTL.5.2.4.4
353      */

354     public void testRuntimeExceptionWhileProcessAction() throws PortletContainerException {
355         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
356         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
357         ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
358         portletContainer.processAction(request, response, actionInput);
359         assertTrue(portletMonitor.isBroken("hello",
360         "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
361         PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.getPortletRuntimeDataMap().
362         get("hello" + PortletMonitor.SEPARATOR +
363         "PortletWithRuntimeExceptionWhileProcessActionAndRender");
364         assertNull(rD);
365         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
366         portletContainer.render(request, response, input);
367         assertEquals("Exception occured", portletContainer.render(request, response, input).getTitle());
368         assertEquals("java.lang.RuntimeException: runtime exception in processAction",
369         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
370     }
371
372     /**
373      * test (xix) : A RuntimeException thrown during the request handling must be handled as a PortletException
374      *
375      * PTL.5.2.4.4
376      */

377     public void testRuntimeExceptionWhileRender() throws PortletContainerException {
378         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
379         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
380         ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
381         RenderOutput o = portletContainer.render(request, response, input);
382         assertEquals("Exception occured", o.getTitle());
383         assertEquals("java.lang.RuntimeException: runtime exception in render",
384         new String JavaDoc(portletContainer.render(request, response, input).getContent()));
385         assertTrue(portletMonitor.isBroken("hello",
386         "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
387     }
388
389     /**
390      * test (xix) : Once the destroy method is called on a portlet object, the portlet container must not
391      * route any requests to that portlet object.
392      *
393      * PTL.5.2.5
394      */

395     public void testNonRoutingProcessActionWhenPortletDestroyed() throws PortletContainerException {
396         proxy.destroy("HelloWorld");
397         assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
398         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
399         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
400         ((ExoWindowID)actionInput.getWindowID()).setPortletName("HelloWorld");
401         ActionOutput aO = portletContainer.processAction(request, response, actionInput);
402         assertEquals("output generated because of a destroyed portlet access",
403                         aO.getProperties().get(PortletContainerConstants.DESTROYED));
404         assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
405         ((ExoWindowID)input.getWindowID()).setPortletName("HelloWorld");
406         RenderOutput o = portletContainer.render(request, response, input);
407         assertEquals("Portlet destroyed", o.getTitle());
408         assertEquals("Portlet unvailable", new String JavaDoc(o.getContent()));
409     }
410
411     /**
412      * test (xix) : Once the destroy method is called on a portlet object, the portlet container must not
413      * route any requests to that portlet object.
414      *
415      * PTL.5.2.5
416      */

417     public void testNonRoutingRenderWhenPortletDestroyed() throws PortletContainerException {
418         proxy.destroy("HelloWorld");
419         assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
420         HttpServletRequest JavaDoc request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
421         HttpServletResponse JavaDoc response = new MockServletResponse(new EmptyResponse());
422         ((ExoWindowID)input.getWindowID()).setPortletName("HelloWorld");
423         RenderOutput o = portletContainer.render(request, response, input);
424         assertEquals("Portlet destroyed", o.getTitle());
425         assertEquals("Portlet unvailable", new String JavaDoc(o.getContent()));
426         request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
427         o = portletContainer.render(request, response, input);
428         assertEquals("Portlet destroyed", o.getTitle());
429         assertEquals("Portlet unvailable", new String JavaDoc(o.getContent()));
430     }
431
432     /**
433      * test (xxi) : If the portlet container needs to enable the portlet again, it must do so
434      * with a new portlet object, which is a new instance of the portlet's class.
435      *
436      * test (xxiii) : After the destroy method completes, the portlet container must release the
437      * portlet object so that it is eligible for garbage collection. (implicit)
438      *
439      * PTL.5.2.5
440      */

441     public void testPortletReUsedAfterDestroy() throws PortletException {
442         Portlet p1 = proxy.getPortlet(portletContext, "HelloWorld");
443         proxy.destroy("HelloWorld");
444         assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
445
446         proxy.registerPortletToMonitor("HelloWorld");
447         Portlet p2 = proxy.getPortlet(portletContext, "HelloWorld");
448         assertNotSame(p1, p2);
449     }
450
451     /**
452      * test (xxii) : If the portlet object throws a RuntimeException within the
453      * execution of the destroy method the portlet container must
454      * consider the portlet object successfully destroyed.
455      *
456      * PTL.5.2.5
457      */

458     public void testRuntimeExceptionWhileDestroy() throws PortletException {
459         proxy.getPortlet(portletContext, "PortletWithRuntimeExceptionWhileProcessActionAndRender");
460         proxy.destroy("PortletWithRuntimeExceptionWhileProcessActionAndRender");
461         assertTrue(portletMonitor.isDestroyed("hello",
462                         "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
463     }
464
465 }
466
Popular Tags