PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/exoportal-v1.0.2/services/portlet-container/impl/src/test/org/exoplatform/services/portletcontainer/imp/TestPortletInterface.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 465 lines | 300 code | 28 blank | 137 comment | 0 complexity | f52470d653ec03940c2c18e5203b25d7 MD5 | raw file
  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. import java.util.Locale;
  7. import javax.portlet.GenericPortlet;
  8. import javax.portlet.Portlet;
  9. import javax.portlet.PortletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.exoplatform.container.PortalContainer;
  13. import org.exoplatform.services.portletcontainer.PortletContainerConstants;
  14. import org.exoplatform.services.portletcontainer.PortletContainerException;
  15. import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy;
  16. import org.exoplatform.services.portletcontainer.impl.monitor.PortletMonitor;
  17. import org.exoplatform.services.portletcontainer.impl.portletAPIImp.pool.EmptyResponse;
  18. import org.exoplatform.services.portletcontainer.monitor.PortletRuntimeData;
  19. import org.exoplatform.services.portletcontainer.pci.*;
  20. import org.exoplatform.test.mocks.servlet.MockHttpSession;
  21. import org.exoplatform.test.mocks.servlet.MockServletRequest;
  22. import org.exoplatform.test.mocks.servlet.MockServletResponse;
  23. /**
  24. * Created by the Exo Development team.
  25. * Author : Mestrallet Benjamin
  26. * benjmestrallet@users.sourceforge.net
  27. * Date: 11 nov. 2003
  28. * Time: 23:43:29
  29. */
  30. public class TestPortletInterface extends BaseTest {
  31. PortletApplicationProxy proxy;
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. proxy = (PortletApplicationProxy) PortalContainer.getInstance().
  35. getComponentInstance("hello");
  36. }
  37. public TestPortletInterface(String s) {
  38. super(s);
  39. }
  40. /**
  41. * Test (i)
  42. * PLT.5.1
  43. */
  44. public void testPortletUnicity() throws Exception {
  45. Portlet p1 = proxy.getPortlet(portletContext, "HelloWorld");
  46. Portlet p2 = proxy.getPortlet(portletContext, "HelloWorld");
  47. assertEquals(p1, p2);
  48. }
  49. /**
  50. * Test (iii)
  51. * PLT.5.2.1
  52. */
  53. public void testClassLoader() throws PortletException {
  54. //assertEquals(cl2, proxy.getPortlet(portletContext, "HelloWorld").getClass().getClassLoader());
  55. }
  56. /**
  57. * Test (iv)
  58. * PLT.5.2.2
  59. */
  60. public void testInitialization() throws PortletException {
  61. GenericPortlet p = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
  62. assertNotNull(p.getPortletConfig());
  63. }
  64. /**
  65. * test PortletConfig unicity per portlet def
  66. * PLT.5.2.2
  67. */
  68. public void testPortletConfigUnicity() throws PortletException {
  69. GenericPortlet p1 = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
  70. GenericPortlet p2 = (GenericPortlet) proxy.getPortlet(portletContext, "HelloWorld");
  71. assertEquals(p1.getPortletConfig(), p2.getPortletConfig());
  72. }
  73. /**
  74. * test (v) : test Portlet exception thrown in init method
  75. * PLT.5.2.2.1
  76. *
  77. * test (vi) : test that the destroy method is not called (Should not see any output in conole)
  78. * PLT.5.2.2.1
  79. */
  80. public void testPortletExceptionWhileInit() {
  81. GenericPortlet p = null;
  82. try {
  83. p = (GenericPortlet) proxy.getPortlet(portletContext,
  84. "PortletWithPortletExceptionWhileInit");
  85. } catch (Exception e) {
  86. assertEquals(e.getMessage(), "exception while initializing portlet");
  87. assertTrue(e instanceof PortletException);
  88. }
  89. assertNull(p);
  90. assertFalse(portletMonitor.isInitialized("hello",
  91. "PortletWithPortletExceptionWhileInit"));
  92. }
  93. /**
  94. * test (v) : test unavailable exception thrown in init method (Unavailable time 5s)
  95. * PLT.5.2.2.1
  96. *
  97. * test (vi) : test that the destroy method is not called (Should not see any output in console)
  98. * PLT.5.2.2.1
  99. *
  100. * test (vii) : test that the unavailable time period is respected
  101. * PLT.5.2.2.1
  102. */
  103. public void testUnavailableExceptionWhileInit() throws InterruptedException {
  104. GenericPortlet p = null;
  105. try {
  106. p = (GenericPortlet) proxy.getPortlet(portletContext,
  107. "PortletWithUnavailableExceptionWhileInit");
  108. } catch (Exception e) {
  109. assertEquals("Unavailable portlet", e.getMessage());
  110. assertTrue(e instanceof PortletException);
  111. PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.getPortletRuntimeDataMap().
  112. get("hello" + PortletMonitor.SEPARATOR +
  113. "PortletWithUnavailableExceptionWhileInit");
  114. assertTrue(rD.getLastInitFailureAccessTime() > 0);
  115. assertEquals(rD.getUnavailabilityPeriod(), 5000);
  116. }
  117. Thread.sleep(100);
  118. try {
  119. p = (GenericPortlet) proxy.getPortlet(portletContext,
  120. "PortletWithUnavailableExceptionWhileInit");
  121. } catch (Exception e) {
  122. assertEquals("Portlet initialization not possible", e.getMessage());
  123. assertTrue(e instanceof PortletException);
  124. }
  125. Thread.sleep(6000);
  126. try {
  127. p = (GenericPortlet) proxy.getPortlet(portletContext,
  128. "PortletWithUnavailableExceptionWhileInit");
  129. } catch (Exception e) {
  130. assertEquals("Unavailable portlet", e.getMessage());
  131. assertTrue(e instanceof PortletException);
  132. }
  133. assertNull(p);
  134. assertFalse(portletMonitor.isInitialized("hello",
  135. "PortletWithUnavailableExceptionWhileInit"));
  136. }
  137. /**
  138. * test (v) : test unavailable exception thrown in init method (Unavailable time 0s)
  139. * PLT.5.2.2.1
  140. *
  141. * test (vi) : test that the destroy method is not called (Should not see any output in console)
  142. * PLT.5.2.2.1
  143. *
  144. * test (vii) : test that an unavailable time period of 0 sec works too
  145. * PLT.5.2.2.1
  146. */
  147. public void testUnavailableExceptionWhileInit2() throws InterruptedException {
  148. GenericPortlet p = null;
  149. try {
  150. p = (GenericPortlet) proxy.getPortlet(portletContext,
  151. "PortletWithUnavailableExceptionWhileInit2");
  152. } catch (Exception e) {
  153. assertEquals("Unavailable portlet", e.getMessage());
  154. assertTrue(e instanceof PortletException);
  155. }
  156. Thread.sleep(100);
  157. try {
  158. p = (GenericPortlet) proxy.getPortlet(portletContext,
  159. "PortletWithUnavailableExceptionWhileInit2");
  160. } catch (Exception e) {
  161. assertEquals("Unavailable portlet", e.getMessage());
  162. assertTrue(e instanceof PortletException);
  163. }
  164. assertNull(p);
  165. assertFalse(portletMonitor.isInitialized("hello",
  166. "PortletWithUnavailableExceptionWhileInit2"));
  167. }
  168. /**
  169. * test (viii) : test that Runtime Exception is treated like PortletException
  170. * PLT.5.2.2.1
  171. */
  172. public void testRuntimeExceptionWhileInit() {
  173. GenericPortlet p = null;
  174. try {
  175. p = (GenericPortlet) proxy.getPortlet(portletContext,
  176. "PortletWithRuntimeExceptionWhileInit");
  177. } catch (Exception e) {
  178. assertEquals(e.getMessage(), "exception while initializing portlet");
  179. assertTrue(e instanceof PortletException);
  180. }
  181. assertNull(p);
  182. assertFalse(portletMonitor.isInitialized("hello",
  183. "PortletWithRuntimeExceptionWhileInit"));
  184. }
  185. /**
  186. * test (xvii) : If a portlet throws an exception in the processAction method, all operations on
  187. * the ActionResponse must be ignored and the render method must not be invoked within
  188. * the current client request.
  189. * PTL.5.2.4.4
  190. */
  191. public void testExceptionWhileProcessAction() throws PortletContainerException {
  192. ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction");
  193. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithExceptionWhileProcessAction");
  194. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  195. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  196. portletContainer.processAction(request, response, actionInput);
  197. RenderOutput rO = portletContainer.render(request, response, input);
  198. assertEquals("Exception occured", rO.getTitle());
  199. assertEquals("javax.portlet.PortletException: Exception in processAction", new String(rO.getContent()));
  200. assertTrue(portletMonitor.isAvailable("hello",
  201. "PortletWithExceptionWhileProcessAction"));
  202. }
  203. /**
  204. * test (xviii) : If a permanent unavailability is indicated by the UnavailableException, the portlet
  205. * container must remove the portlet from service immediately, call the portlets destroy
  206. * method, and release the portlet object.
  207. *
  208. * test : A portlet that throws a permanent UnavailableException must be considered unavailable
  209. * until the portlet application containing the portlet is restarted.
  210. *
  211. * PTL.5.2.4.4
  212. */
  213. public void testPortletWithPermanentUnavailableExceptionInProcessAction()
  214. throws Exception {
  215. ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
  216. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  217. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  218. portletContainer.processAction(request, response, actionInput);
  219. assertTrue(portletMonitor.isBroken("hello",
  220. "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
  221. PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.
  222. getPortletRuntimeDataMap().get("hello" + PortletMonitor.SEPARATOR +
  223. "PortletWithPermanentUnavailibiltyInProcessActionAndRender");
  224. assertNull(rD);
  225. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
  226. portletContainer.render(request, response, input);
  227. assertEquals("Exception occured", portletContainer.render(request, response, input).getTitle());
  228. assertEquals("javax.portlet.UnavailableException: Permanent unavailable exception",
  229. new String(portletContainer.render(request, response, input).getContent()));
  230. assertTrue(portletMonitor.isBroken("hello",
  231. "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
  232. ActionOutput o = portletContainer.processAction(request, response, actionInput);
  233. assertEquals("output generated because of an exception",
  234. o.getProperties().get(PortletContainerConstants.EXCEPTION));
  235. assertTrue(portletMonitor.isBroken("hello",
  236. "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
  237. portletApplicationRegister.removePortletApplication(mockServletContext);
  238. portletApplicationRegister.registerPortletApplication(mockServletContext, portletApp_, roles);
  239. assertFalse(portletMonitor.isBroken("hello",
  240. "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
  241. portletContainer.processAction(request, response, actionInput);
  242. assertNull(rD);
  243. }
  244. /**
  245. * test (xviii) : If a permanent unavailability is indicated by the UnavailableException, the portlet
  246. * container must remove the portlet from service immediately, call the portlet's destroy
  247. * method, and release the portlet object.
  248. *
  249. * test : A portlet that throws a permanent UnavailableException must be considered unavailable
  250. * until the portlet application containing the portlet is restarted.
  251. *
  252. * PTL.5.2.4.4
  253. */
  254. public void testPortletWithPermanentUnavailableExceptionInRender() throws PortletContainerException {
  255. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  256. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  257. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithPermanentUnavailibiltyInProcessActionAndRender");
  258. RenderOutput o = portletContainer.render(request, response, input);
  259. assertEquals("Exception occured", o.getTitle());
  260. assertEquals("javax.portlet.UnavailableException: Permanent unavailable exception",
  261. new String(portletContainer.render(request, response, input).getContent()));
  262. assertTrue(portletMonitor.isBroken("hello",
  263. "PortletWithPermanentUnavailibiltyInProcessActionAndRender"));
  264. }
  265. /**
  266. * test : When temporary unavailability is indicated by the UnavailableException,
  267. * then the portlet container may choose not to route any requests to the
  268. * portlet during the time period of the temporary unavailability.
  269. *
  270. * PTL.5.2.4.4
  271. */
  272. public void testNonPermanentUnavailableExceptionInProcessAction() throws PortletContainerException, InterruptedException {
  273. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  274. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  275. ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithNonPermanentUnavailibiltyInProcessActionAndRender");
  276. portletContainer.processAction(request, response, actionInput);
  277. assertFalse(portletMonitor.isAvailable("hello",
  278. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
  279. System.currentTimeMillis()));
  280. Thread.sleep(5000);
  281. assertTrue(portletMonitor.isAvailable("hello",
  282. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender", System.currentTimeMillis()));
  283. request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  284. portletContainer.processAction(request, response, actionInput);
  285. assertFalse(portletMonitor.isAvailable("hello",
  286. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
  287. System.currentTimeMillis()));
  288. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithNonPermanentUnavailibiltyInProcessActionAndRender");
  289. RenderOutput o = portletContainer.render(request, response, input);
  290. assertFalse(portletMonitor.isAvailable("hello",
  291. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
  292. System.currentTimeMillis()));
  293. assertEquals("Exception occured", o.getTitle());
  294. assertEquals("javax.portlet.UnavailableException: Non Permanent unavailable exception",
  295. new String(portletContainer.render(request, response, input).getContent()));
  296. }
  297. /**
  298. * test : When temporary unavailability is indicated by the UnavailableException,
  299. * then the portlet container may choose not to route any requests to the
  300. * portlet during the time period of the temporary unavailability.
  301. *
  302. * PTL.5.2.4.4
  303. */
  304. public void testNonPermanentUnavailableExceptionInRender() throws PortletContainerException, InterruptedException {
  305. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  306. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  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. Thread.sleep(5000);
  316. assertTrue(portletMonitor.isAvailable("hello",
  317. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
  318. System.currentTimeMillis()));
  319. request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  320. o = portletContainer.render(request, response, input);
  321. assertFalse(portletMonitor.isAvailable("hello",
  322. "PortletWithNonPermanentUnavailibiltyInProcessActionAndRender",
  323. System.currentTimeMillis()));
  324. assertEquals("Exception occured", o.getTitle());
  325. assertEquals("javax.portlet.UnavailableException: Non Permanent unavailable exception",
  326. new String(portletContainer.render(request, response, input).getContent()));
  327. }
  328. /**
  329. * test (xix) : A RuntimeException thrown during the request handling must be handled as a PortletException
  330. *
  331. * PTL.5.2.4.4
  332. */
  333. public void testRuntimeExceptionWhileProcessAction() throws PortletContainerException {
  334. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  335. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  336. ((ExoWindowID)actionInput.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
  337. portletContainer.processAction(request, response, actionInput);
  338. assertTrue(portletMonitor.isBroken("hello",
  339. "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
  340. PortletRuntimeData rD = (PortletRuntimeData) portletMonitor.getPortletRuntimeDataMap().
  341. get("hello" + PortletMonitor.SEPARATOR +
  342. "PortletWithRuntimeExceptionWhileProcessActionAndRender");
  343. assertNull(rD);
  344. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
  345. portletContainer.render(request, response, input);
  346. assertEquals("Exception occured", portletContainer.render(request, response, input).getTitle());
  347. assertEquals("java.lang.RuntimeException: runtime exception in processAction",
  348. new String(portletContainer.render(request, response, input).getContent()));
  349. }
  350. /**
  351. * test (xix) : A RuntimeException thrown during the request handling must be handled as a PortletException
  352. *
  353. * PTL.5.2.4.4
  354. */
  355. public void testRuntimeExceptionWhileRender() throws PortletContainerException {
  356. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  357. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  358. ((ExoWindowID)input.getWindowID()).setPortletName("PortletWithRuntimeExceptionWhileProcessActionAndRender");
  359. RenderOutput o = portletContainer.render(request, response, input);
  360. assertEquals("Exception occured", o.getTitle());
  361. assertEquals("java.lang.RuntimeException: runtime exception in render",
  362. new String(portletContainer.render(request, response, input).getContent()));
  363. assertTrue(portletMonitor.isBroken("hello",
  364. "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
  365. }
  366. /**
  367. * test (xix) : Once the destroy method is called on a portlet object, the portlet container must not
  368. * route any requests to that portlet object.
  369. *
  370. * PTL.5.2.5
  371. */
  372. public void testNonRoutingProcessActionWhenPortletDestroyed() throws PortletContainerException {
  373. proxy.destroy("HelloWorld");
  374. assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
  375. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  376. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  377. ((ExoWindowID)actionInput.getWindowID()).setPortletName("HelloWorld");
  378. ActionOutput aO = portletContainer.processAction(request, response, actionInput);
  379. assertEquals("output generated because of a destroyed portlet access",
  380. aO.getProperties().get(PortletContainerConstants.DESTROYED));
  381. assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
  382. ((ExoWindowID)input.getWindowID()).setPortletName("HelloWorld");
  383. RenderOutput o = portletContainer.render(request, response, input);
  384. assertEquals("Portlet destroyed", o.getTitle());
  385. assertEquals("Portlet unvailable", new String(o.getContent()));
  386. }
  387. /**
  388. * test (xix) : Once the destroy method is called on a portlet object, the portlet container must not
  389. * route any requests to that portlet object.
  390. *
  391. * PTL.5.2.5
  392. */
  393. public void testNonRoutingRenderWhenPortletDestroyed() throws PortletContainerException {
  394. proxy.destroy("HelloWorld");
  395. assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
  396. HttpServletRequest request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  397. HttpServletResponse response = new MockServletResponse(new EmptyResponse());
  398. ((ExoWindowID)input.getWindowID()).setPortletName("HelloWorld");
  399. RenderOutput o = portletContainer.render(request, response, input);
  400. assertEquals("Portlet destroyed", o.getTitle());
  401. assertEquals("Portlet unvailable", new String(o.getContent()));
  402. request = new MockServletRequest(new MockHttpSession(), Locale.US, true);
  403. o = portletContainer.render(request, response, input);
  404. assertEquals("Portlet destroyed", o.getTitle());
  405. assertEquals("Portlet unvailable", new String(o.getContent()));
  406. }
  407. /**
  408. * test (xxi) : If the portlet container needs to enable the portlet again, it must do so
  409. * with a new portlet object, which is a new instance of the portlet's class.
  410. *
  411. * test (xxiii) : After the destroy method completes, the portlet container must release the
  412. * portlet object so that it is eligible for garbage collection. (implicit)
  413. *
  414. * PTL.5.2.5
  415. */
  416. public void testPortletReUsedAfterDestroy() throws PortletException {
  417. Portlet p1 = proxy.getPortlet(portletContext, "HelloWorld");
  418. proxy.destroy("HelloWorld");
  419. assertTrue(portletMonitor.isDestroyed("hello", "HelloWorld"));
  420. proxy.registerPortletToMonitor("HelloWorld");
  421. Portlet p2 = proxy.getPortlet(portletContext, "HelloWorld");
  422. assertNotSame(p1, p2);
  423. }
  424. /**
  425. * test (xxii) : If the portlet object throws a RuntimeException within the
  426. * execution of the destroy method the portlet container must
  427. * consider the portlet object successfully destroyed.
  428. *
  429. * PTL.5.2.5
  430. */
  431. public void testRuntimeExceptionWhileDestroy() throws PortletException {
  432. proxy.getPortlet(portletContext, "PortletWithRuntimeExceptionWhileProcessActionAndRender");
  433. proxy.destroy("PortletWithRuntimeExceptionWhileProcessActionAndRender");
  434. assertTrue(portletMonitor.isDestroyed("hello",
  435. "PortletWithRuntimeExceptionWhileProcessActionAndRender"));
  436. }
  437. }