@Test public void testDelegationToMockPortletConfigIfSoConfigured() throws Exception { final String BEAN_NAME = "Sixpence None The Richer"; MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); PortletWrappingController controller = new PortletWrappingController(); controller.setPortletClass(MyPortlet.class); controller.setUseSharedPortletConfig(false); controller.setBeanName(BEAN_NAME); controller.afterPropertiesSet(); request.setParameter(PORTLET_NAME_ACTION_REQUEST_PARAMETER_NAME, "true"); controller.handleActionRequest(request, response); String result = response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME); assertEquals(BEAN_NAME, result); }
@Test public void delegationToMockPortletConfigIfSoConfigured() throws Exception { final String BEAN_NAME = "Sixpence None The Richer"; MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); PortletWrappingController controller = new PortletWrappingController(); controller.setPortletClass(MyPortlet.class); controller.setUseSharedPortletConfig(false); controller.setBeanName(BEAN_NAME); controller.afterPropertiesSet(); request.setParameter(PORTLET_NAME_ACTION_REQUEST_PARAMETER_NAME, "true"); controller.handleActionRequest(request, response); assertEquals(BEAN_NAME, response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME)); }
@Test public void testPrettifyActionRequestActionResponse() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); PortalPropertiesPrettierPortlet portlet = createMockBuilder( PortalPropertiesPrettierPortlet.class).addMockedMethod( "prettify", PortletRequest.class).createMock(); String expected = "expected"; expect(portlet.prettify(request)).andReturn(expected); replay(portlet); portlet.prettify(request, response); Assert.assertEquals(expected, request.getAttribute("portalPrettyProperties")); verify(portlet); }
public void testNonDefaultParameterMappedWhenHandlerMappingProvided() throws Exception { String param = "myParam"; String value = "someValue"; ParameterHandlerMapping handlerMapping = new ParameterHandlerMapping(); handlerMapping.setParameterName(param); ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); interceptor.setParameterName(param); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter(param, value); assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandleAction(request, response, handler); assertTrue(shouldProceed); assertNull(response.getRenderParameter(ParameterHandlerMapping.DEFAULT_PARAMETER_NAME)); assertNotNull(response.getRenderParameter(param)); assertEquals(value, response.getRenderParameter(param)); }
public void testSimpleFormViewWithSessionNoBindOnNewForm() throws Exception { MockActionRequest actionRequest = new MockActionRequest(); MockActionResponse actionResponse = new MockActionResponse(); actionRequest.setSession(new MockPortletSession()); actionRequest.setParameter("action", "form-session-nobind"); actionRequest.setParameter("age", "27"); simpleDispatcherPortlet.processAction(actionRequest, actionResponse); Map renderParameters = actionResponse.getRenderParameterMap(); MockRenderRequest renderRequest = new MockRenderRequest(); MockRenderResponse renderResponse = new MockRenderResponse(); renderRequest.setParameters(renderParameters); renderRequest.setParameter("action", "form-session-nobind"); renderRequest.setParameter("age", "30"); renderRequest.setSession(actionRequest.getPortletSession()); simpleDispatcherPortlet.doDispatch(renderRequest, renderResponse); assertEquals("finished42", renderResponse.getContentAsString()); }
public void testValidActionRequestWithExistingThreadLocalRequestContext() throws IOException, PortletException { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.addPreferredLocale(Locale.GERMAN); request.setParameter("action", "form"); request.setParameter("age", "29"); // see RequestContextListener.requestInitialized() try { LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(new PortletRequestAttributes(request)); LocaleContext servletLocaleContext = LocaleContextHolder.getLocaleContext(); RequestAttributes servletRequestAttrs = RequestContextHolder.getRequestAttributes(); simpleDispatcherPortlet.processAction(request, response); assertSame(servletLocaleContext, LocaleContextHolder.getLocaleContext()); assertSame(servletRequestAttrs, RequestContextHolder.getRequestAttributes()); } finally { RequestContextHolder.resetRequestAttributes(); LocaleContextHolder.resetLocaleContext(); } }
public void testInvalidActionRequestWithExistingThreadLocalRequestContext() throws IOException, PortletException { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.addPreferredLocale(Locale.GERMAN); // see RequestContextListener.requestInitialized() try { LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(new PortletRequestAttributes(request)); LocaleContext servletLocaleContext = LocaleContextHolder.getLocaleContext(); RequestAttributes servletRequestAttrs = RequestContextHolder.getRequestAttributes(); request.setParameter("action", "invalid"); simpleDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exceptionParam); // ensure that an exceptional condition occured assertSame(servletLocaleContext, LocaleContextHolder.getLocaleContext()); assertSame(servletRequestAttrs, RequestContextHolder.getRequestAttributes()); } finally { RequestContextHolder.resetRequestAttributes(); LocaleContextHolder.resetLocaleContext(); } }
@Test public void actionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("test", "test"); controller.handleActionRequest(request, response); assertEquals("myPortlet-action", response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME)); }
@Test public void portletName() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter(PORTLET_NAME_ACTION_REQUEST_PARAMETER_NAME, "test"); controller.handleActionRequest(request, response); assertEquals("wrappedPortlet", response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME)); }
@Test public void portletModeParameterMappingHelp1() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.HELP); request.setParameter("action", "help1"); complexDispatcherPortlet.processAction(request, response); String param = response.getRenderParameter("param"); assertEquals("help1 was here", param); }
@Test public void portletModeParameterMappingHelp2() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.HELP); request.setParameter("action", "help2"); complexDispatcherPortlet.processAction(request, response); String param = response.getRenderParameter("param"); assertEquals("help2 was here", param); }
@Test public void portletModeParameterMappingInvalidHelpActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.HELP); request.setParameter("action", "help3"); complexDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exceptionParam); assertTrue(exceptionParam.startsWith(NoHandlerFoundException.class.getName())); }
@Test public void portletModeMappingValidEditActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); request.addUserRole("role1"); request.setParameter("action", "not mapped"); request.setParameter("myParam", "not mapped"); complexDispatcherPortlet.processAction(request, response); assertEquals("edit was here", response.getRenderParameter("param")); }
@Test public void portletModeMappingEditActionRequestWithUnauthorizedUserRole() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); request.addUserRole("role3"); request.setParameter("action", "not mapped"); request.setParameter("myParam", "not mapped"); complexDispatcherPortlet.processAction(request, response); String exception = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exception); String name = PortletSecurityException.class.getName(); assertTrue(exception.startsWith(name)); }
@Test public void parameterMappingValidActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); request.setParameter("action", "not mapped"); request.setParameter("myParam", "test1"); complexDispatcherPortlet.processAction(request, response); assertEquals("test1-action", response.getRenderParameter("result")); }
@Test public void unknownHandlerActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("myParam", "unknown"); complexDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exceptionParam); assertTrue(exceptionParam.startsWith(PortletException.class.getName())); assertTrue(exceptionParam.indexOf("No adapter for handler") != -1); }
@Test public void noDetectAllHandlerMappingsWithPortletModeActionRequest() throws Exception { DispatcherPortlet complexDispatcherPortlet = new DispatcherPortlet(); complexDispatcherPortlet.setContextClass(ComplexPortletApplicationContext.class); complexDispatcherPortlet.setNamespace("test"); complexDispatcherPortlet.setDetectAllHandlerMappings(false); complexDispatcherPortlet.init(new MockPortletConfig(getPortletContext(), "complex")); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); complexDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exceptionParam); assertTrue(exceptionParam.startsWith(NoHandlerFoundException.class.getName())); }
@Test public void existingMultipartRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); ComplexPortletApplicationContext.MockMultipartResolver multipartResolver = (ComplexPortletApplicationContext.MockMultipartResolver) complexDispatcherPortlet.getPortletApplicationContext().getBean("portletMultipartResolver"); MultipartActionRequest multipartRequest = multipartResolver.resolveMultipart(request); complexDispatcherPortlet.processAction(multipartRequest, response); multipartResolver.cleanupMultipart(multipartRequest); assertNotNull(request.getAttribute("cleanedUp")); }
@Test public void multipartResolutionFailed() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.EDIT); request.addUserRole("role1"); request.setAttribute("fail", Boolean.TRUE); complexDispatcherPortlet.processAction(request, response); String exception = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertTrue(exception.startsWith(MaxUploadSizeExceededException.class.getName())); }
@Test public void actionRequestHandledEvent() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); complexDispatcherPortlet.processAction(request, response); ComplexPortletApplicationContext.TestApplicationListener listener = (ComplexPortletApplicationContext.TestApplicationListener) complexDispatcherPortlet.getPortletApplicationContext().getBean("testListener"); assertEquals(1, listener.counter); }
@Test public void publishEventsOff() throws Exception { complexDispatcherPortlet.setPublishEvents(false); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("action", "checker"); complexDispatcherPortlet.processAction(request, response); ComplexPortletApplicationContext.TestApplicationListener listener = (ComplexPortletApplicationContext.TestApplicationListener) complexDispatcherPortlet.getPortletApplicationContext().getBean("testListener"); assertEquals(0, listener.counter); }
@Test public void parameterMappingInterceptorWithCorrectParam() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.VIEW); request.addUserRole("role1"); request.addParameter("interceptingParam", "test1"); complexDispatcherPortlet.processAction(request, response); assertEquals("test1", response.getRenderParameter("interceptingParam")); }
@Test public void parameterMappingInterceptorWithIncorrectParam() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setPortletMode(PortletMode.VIEW); request.addUserRole("role1"); request.addParameter("incorrect", "test1"); complexDispatcherPortlet.processAction(request, response); assertNull(response.getRenderParameter("incorrect")); assertNull(response.getRenderParameter("interceptingParam")); }
@Test public void portletHandlerAdapterActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("myParam", "myPortlet"); complexDispatcherPortlet.processAction(request, response); assertEquals("myPortlet action called", response.getRenderParameter("result")); ComplexPortletApplicationContext.MyPortlet myPortlet = (ComplexPortletApplicationContext.MyPortlet) complexDispatcherPortlet.getPortletApplicationContext().getBean("myPortlet"); assertEquals("complex", myPortlet.getPortletConfig().getPortletName()); assertEquals(getPortletContext(), myPortlet.getPortletConfig().getPortletContext()); assertEquals(complexDispatcherPortlet.getPortletContext(), myPortlet.getPortletConfig().getPortletContext()); complexDispatcherPortlet.destroy(); assertNull(myPortlet.getPortletConfig()); }
@Test public void testPassAllParametersToRenderPhase() throws Exception { MockActionRequest request = new MockActionRequest(); request.setParameter("William", "Baskerville"); request.setParameter("Adso", "Melk"); MockActionResponse response = new MockActionResponse(); PortletUtils.passAllParametersToRenderPhase(request, response); assertEquals("The render parameters map is obviously not being populated with the request parameters.", request.getParameterMap().size(), response.getRenderParameterMap().size()); }
@Test public void testPassAllParametersToRenderPhaseDoesNotPropagateExceptionIfRedirectAlreadySentAtTimeOfCall() throws Exception { MockActionRequest request = new MockActionRequest(); request.setParameter("William", "Baskerville"); request.setParameter("Adso", "Melk"); MockActionResponse response = new MockActionResponse() { @Override public void setRenderParameter(String key, String[] values) { throw new IllegalStateException(); } }; PortletUtils.passAllParametersToRenderPhase(request, response); assertEquals("The render parameters map must not be being populated with the request parameters (Action.sendRedirect(..) aleady called).", 0, response.getRenderParameterMap().size()); }
@Test public void testActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("test", "test"); controller.handleActionRequest(request, response); String result = response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME); assertEquals("myPortlet-action", result); }
@Test public void testPortletName() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter(PORTLET_NAME_ACTION_REQUEST_PARAMETER_NAME, "test"); controller.handleActionRequest(request, response); String result = response.getRenderParameter(RESULT_RENDER_PARAMETER_NAME); assertEquals("wrappedPortlet", result); }
public void testActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); try { controller.handleActionRequest(request, response); fail("Should have thrown PortletException"); } catch(PortletException ex) { // expected } }
public void testActionRequestNotHandled() throws Exception { ParameterizableViewController controller = new ParameterizableViewController(); ActionRequest request = new MockActionRequest(); ActionResponse response = new MockActionResponse(); try { controller.handleActionRequest(request, response); fail("should have thrown PortletException"); } catch (PortletException ex) { // expected } }
public void testActionRequest() throws Exception { TestController tc = new TestController(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); tc.handleActionRequest(request, response); TestBean command = (TestBean)request.getPortletSession().getAttribute(tc.getRenderCommandSessionAttributeName()); assertTrue(command.isJedi()); }
public void testDefaultParameterMapped() throws Exception { ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); String param = ParameterHandlerMapping.DEFAULT_PARAMETER_NAME; String value = "someValue"; request.setParameter(param, value); assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandleAction(request, response, handler); assertTrue(shouldProceed); assertNotNull(response.getRenderParameter(param)); assertEquals(value, response.getRenderParameter(param)); }
public void testNonDefaultParameterNotMapped() throws Exception { ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); String param = "myParam"; String value = "someValue"; request.setParameter(param, value); assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandle(request, response, handler); assertTrue(shouldProceed); assertNull(response.getRenderParameter(param)); assertNull(response.getRenderParameter(ParameterHandlerMapping.DEFAULT_PARAMETER_NAME)); }
public void testNoParameterValueSetWithDefaultParameterName() throws Exception { ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); String param = ParameterHandlerMapping.DEFAULT_PARAMETER_NAME; assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandle(request, response, handler); assertTrue(shouldProceed); assertNull(response.getRenderParameter(param)); }
public void testNoParameterValueSetWithNonDefaultParameterName() throws Exception { ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); String param = "myParam"; assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandle(request, response, handler); assertTrue(shouldProceed); assertNull(response.getRenderParameter(param)); }
public void testNoParameterValueSetWithNonDefaultParameterNameWhenHandlerMappingProvided() throws Exception { String param = "myParam"; ParameterHandlerMapping handlerMapping = new ParameterHandlerMapping(); handlerMapping.setParameterName(param); ParameterMappingInterceptor interceptor = new ParameterMappingInterceptor(); interceptor.setParameterName(param); Object handler = new Object(); MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); assertNull(response.getRenderParameter(param)); boolean shouldProceed = interceptor.preHandle(request, response, handler); assertTrue(shouldProceed); assertNull(response.getRenderParameter(param)); }
public void testSimpleValidActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("action", "form"); request.setParameter("age", "29"); simpleDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNull(exceptionParam); SimplePortletApplicationContext ac = (SimplePortletApplicationContext)simpleDispatcherPortlet.getPortletApplicationContext(); String commandAttribute = ac.getRenderCommandSessionAttributeName(); TestBean testBean = (TestBean) request.getPortletSession().getAttribute(commandAttribute); assertEquals(39, testBean.getAge()); }
public void testSimpleInvalidActionRequest() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("action", "invalid"); simpleDispatcherPortlet.processAction(request, response); String exceptionParam = response.getRenderParameter(DispatcherPortlet.ACTION_EXCEPTION_RENDER_PARAMETER); assertNotNull(exceptionParam); assertTrue(exceptionParam.startsWith(NoHandlerFoundException.class.getName())); }
public void testSimpleInvalidActionRequestWithoutHandling() throws Exception { MockActionRequest request = new MockActionRequest(); MockActionResponse response = new MockActionResponse(); request.setParameter("action", "invalid"); simpleDispatcherPortlet.setForwardActionException(false); try { simpleDispatcherPortlet.processAction(request, response); fail("Should have thrown a " + NoHandlerFoundException.class); } catch (NoHandlerFoundException ex) { // expected } }
public void testSimpleFormSubmission() throws Exception { MockActionRequest actionRequest = new MockActionRequest(); MockActionResponse actionResponse = new MockActionResponse(); actionRequest.setParameter("action", "form"); actionRequest.setParameter("age", "29"); simpleDispatcherPortlet.processAction(actionRequest, actionResponse); MockRenderRequest renderRequest = new MockRenderRequest(); MockRenderResponse renderResponse = new MockRenderResponse(); renderRequest.setSession(actionRequest.getPortletSession()); renderRequest.setParameters(actionResponse.getRenderParameterMap()); renderRequest.setParameter("action", "form"); simpleDispatcherPortlet.doDispatch(renderRequest, renderResponse); assertEquals("finished44", renderResponse.getContentAsString()); }