1 5 package org.exoplatform.services.portletcontainer.imp; 6 7 import java.util.Locale ; 8 9 import javax.portlet.GenericPortlet; 10 import javax.portlet.Portlet; 11 import javax.portlet.PortletException; 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 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 34 public class TestPortletInterface extends BaseTest { 35 36 PortletApplicationProxy proxy; 37 38 public void setUp() throws Exception { 39 super.setUp(); 40 proxy = (PortletApplicationProxy) PortalContainer.getInstance(). 41 getComponentInstance("hello"); 42 } 43 44 public TestPortletInterface(String s) { 45 super(s); 46 } 47 48 52 public void testPortletUnicity() throws Exception { 53 Portlet p1 = proxy.getPortlet(portletContext, "HelloWorld"); 54 Portlet p2 = proxy.getPortlet(portletContext, "HelloWorld"); 55 assertEquals(p1, p2); 56 } 57 58 62 public void testClassLoader() throws PortletException { 63 } 65 66 70 public void testInitialization() throws PortletException { 71 GenericPortlet p = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld"); 72 assertNotNull(p.getPortletConfig()); 73 } 74 75 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 92 public void testPortletExceptionWhileInit() { 93 GenericPortlet p = null; 94 try { 95 p = (GenericPortlet) proxy.getPortlet(portletContext, 96 "PortletWithPortletExceptionWhileInit"); 97 } catch (Exception 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 116 public void testUnavailableExceptionWhileInit() throws InterruptedException { 117 GenericPortlet p = null; 118 try { 119 p = (GenericPortlet) proxy.getPortlet(portletContext, 120 "PortletWithUnavailableExceptionWhileInit"); 121 } catch (Exception 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 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 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 161 public void testUnavailableExceptionWhileInit2() throws InterruptedException { 162 GenericPortlet p = null; 163 try { 164 p = (GenericPortlet) proxy.getPortlet(portletContext, 165 "PortletWithUnavailableExceptionWhileInit2"); 166 } catch (Exception 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 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 187 public void testRuntimeExceptionWhileInit() { 188 GenericPortlet p = null; 189 try { 190 p = (GenericPortlet) proxy.getPortlet(portletContext, 191 "PortletWithRuntimeExceptionWhileInit"); 192 } catch (Exception 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 207 public void testExceptionWhileProcessAction() throws PortletContainerException { 208 ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction"); 209 ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction"); 210 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 211 HttpServletResponse 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 (rO.getContent())); 216 assertTrue(portletMonitor.isAvailable("hello", 217 "PortletWithExceptionWhileProcessAction")); 218 } 219 220 230 public void testPortletWithPermanentUnavailableExceptionInProcessAction() 231 throws Exception { 232 ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender"); 233 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 234 HttpServletResponse 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 (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 272 public void testPortletWithPermanentUnavailableExceptionInRender() throws PortletContainerException { 273 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 274 HttpServletResponse 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 (portletContainer.render(request, response, input).getContent())); 280 assertTrue(portletMonitor.isBroken("hello", 281 "PortletWithPermanentUnavailibiltyInProcessActionAndRender")); 282 } 283 284 291 public void testNonPermanentUnavailableExceptionInProcessAction() throws PortletContainerException, InterruptedException { 292 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 293 HttpServletResponse 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 (portletContainer.render(request, response, input).getContent())); 315 } 316 317 324 public void testNonPermanentUnavailableExceptionInRender() throws PortletContainerException, InterruptedException { 325 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 326 HttpServletResponse 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 (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 (portletContainer.render(request, response, input).getContent())); 347 } 348 349 354 public void testRuntimeExceptionWhileProcessAction() throws PortletContainerException { 355 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 356 HttpServletResponse 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 (portletContainer.render(request, response, input).getContent())); 370 } 371 372 377 public void testRuntimeExceptionWhileRender() throws PortletContainerException { 378 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 379 HttpServletResponse 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 (portletContainer.render(request, response, input).getContent())); 385 assertTrue(portletMonitor.isBroken("hello", 386 "PortletWithRuntimeExceptionWhileProcessActionAndRender")); 387 } 388 389 395 public void testNonRoutingProcessActionWhenPortletDestroyed() throws PortletContainerException { 396 proxy.destroy("HelloWorld"); 397 assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld")); 398 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 399 HttpServletResponse 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 (o.getContent())); 409 } 410 411 417 public void testNonRoutingRenderWhenPortletDestroyed() throws PortletContainerException { 418 proxy.destroy("HelloWorld"); 419 assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld")); 420 HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true); 421 HttpServletResponse 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 (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 (o.getContent())); 430 } 431 432 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 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 |