@Test public void testRequestScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST); MockRenderRequest request = new MockRenderRequest(); PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute(NAME)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, request.getAttribute(NAME)); assertSame(bean, ac.getBean(NAME)); requestAttributes.requestCompleted(); assertTrue(bean.wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } }
@Test public void testSessionScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION); MockRenderRequest request = new MockRenderRequest(); PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getPortletSession().getAttribute(NAME)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, request.getPortletSession().getAttribute(NAME)); assertSame(bean, ac.getBean(NAME)); request.getPortletSession().invalidate(); assertTrue(bean.wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } }
@Test public void testGlobalSessionScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_GLOBAL_SESSION); MockRenderRequest request = new MockRenderRequest(); PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getPortletSession().getAttribute(NAME, PortletSession.APPLICATION_SCOPE)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, request.getPortletSession().getAttribute(NAME, PortletSession.APPLICATION_SCOPE)); assertSame(bean, ac.getBean(NAME)); request.getPortletSession().invalidate(); assertTrue(bean.wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } }
@Test public void testNativeRequest() { MockRenderRequest portletRequest = new MockRenderRequest(); MockRenderResponse portletResponse = new MockRenderResponse(); PortletWebRequest request = new PortletWebRequest(portletRequest, portletResponse); assertSame(portletRequest, request.getNativeRequest()); assertSame(portletRequest, request.getNativeRequest(PortletRequest.class)); assertSame(portletRequest, request.getNativeRequest(RenderRequest.class)); assertSame(portletRequest, request.getNativeRequest(MockRenderRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(portletResponse, request.getNativeResponse()); assertSame(portletResponse, request.getNativeResponse(PortletResponse.class)); assertSame(portletResponse, request.getNativeResponse(RenderResponse.class)); assertSame(portletResponse, request.getNativeResponse(MockRenderResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@Test public void testDecoratedNativeRequest() { MockRenderRequest portletRequest = new MockRenderRequest(); MockRenderResponse portletResponse = new MockRenderResponse(); PortletRequest decoratedRequest = new PortletRequestWrapper(portletRequest); PortletResponse decoratedResponse = new PortletResponseWrapper(portletResponse); PortletWebRequest request = new PortletWebRequest(decoratedRequest, decoratedResponse); assertSame(decoratedRequest, request.getNativeRequest()); assertSame(decoratedRequest, request.getNativeRequest(PortletRequest.class)); assertSame(portletRequest, request.getNativeRequest(RenderRequest.class)); assertSame(portletRequest, request.getNativeRequest(MockRenderRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(decoratedResponse, request.getNativeResponse()); assertSame(decoratedResponse, request.getNativeResponse(PortletResponse.class)); assertSame(portletResponse, request.getNativeResponse(RenderResponse.class)); assertSame(portletResponse, request.getNativeResponse(MockRenderResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@Test public void standardHandleMethod() throws Exception { DispatcherPortlet portlet = new DispatcherPortlet() { @Override protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class)); wac.refresh(); return wac; } }; portlet.init(new MockPortletConfig()); MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW); MockRenderResponse response = new MockRenderResponse(); portlet.render(request, response); assertEquals("test", response.getContentAsString()); }
@Test public void mavResolver() throws Exception { DispatcherPortlet portlet = new DispatcherPortlet() { @Override protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(ModelAndViewResolverController.class)); RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues() .add("customModelAndViewResolver", new MyModelAndViewResolver()); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; portlet.init(new MockPortletConfig()); MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW); MockRenderResponse response = new MockRenderResponse(); portlet.render(request, response); }
@Test public void portletModeMappingViewRenderRequestWithUnauthorizedUserRole() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setPortletMode(PortletMode.VIEW); request.addUserRole("role3"); request.setParameter("action", "not mapped"); request.setParameter("myParam", "not mapped"); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception) model.get("exception"); assertNotNull(exception); assertTrue(exception.getClass().equals(PortletSecurityException.class)); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-default-1", view.getBeanName()); }
@Test public void noDetectAllHandlerMappingsWithParameterRenderRequest() throws Exception { DispatcherPortlet complexDispatcherPortlet = new DispatcherPortlet(); complexDispatcherPortlet.setContextClass(ComplexPortletApplicationContext.class); complexDispatcherPortlet.setNamespace("test"); complexDispatcherPortlet.setDetectAllHandlerMappings(false); complexDispatcherPortlet.init(new MockPortletConfig(getPortletContext(), "complex")); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "test1"); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception) model.get("exception"); assertTrue(exception.getClass().equals(NoHandlerFoundException.class)); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-unavailable", view.getBeanName()); }
@Test public void portletHandlerAdapterRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "myPortlet"); complexDispatcherPortlet.doDispatch(request, response); assertEquals("myPortlet was here", response.getContentAsString()); 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()); }
public void testStandardHandleMethod() throws Exception { DispatcherPortlet portlet = new DispatcherPortlet() { @Override protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class)); wac.refresh(); return wac; } }; portlet.init(new MockPortletConfig()); MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW); MockRenderResponse response = new MockRenderResponse(); portlet.render(request, response); assertEquals("test", response.getContentAsString()); }
public void testMavResolver() throws Exception { DispatcherPortlet portlet = new DispatcherPortlet() { @Override protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(ModelAndViewResolverController.class)); RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues() .add("customModelAndViewResolver", new MyModelAndViewResolver()); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; portlet.init(new MockPortletConfig()); MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW); MockRenderResponse response = new MockRenderResponse(); portlet.render(request, response); }
public void testRenderRequestWithParams() throws Exception { TestController tc = new TestController(); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); String name = "test"; int age = 30; request.addParameter("name", name); request.addParameter("age", "" + age); request.setContextPath("test"); ModelAndView mav = tc.handleRenderRequest(request, response); assertEquals("test-view", mav.getViewName()); TestBean command = (TestBean)mav.getModel().get(tc.getCommandName()); assertEquals("Name should be bound", name, command.getName()); assertEquals("Age should be bound", age, command.getAge()); BindException errors = (BindException)mav.getModel().get(ERRORS_KEY); assertNotNull(errors); assertEquals("There should be no errors", 0, errors.getErrorCount()); }
public void testRenderRequestWithMismatch() throws Exception { TestController tc = new TestController(); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); String name = "test"; request.addParameter("name", name); request.addParameter("age", "zzz"); request.setContextPath("test"); ModelAndView mav = tc.handleRenderRequest(request, response); assertEquals("test-view", mav.getViewName()); TestBean command = (TestBean)mav.getModel().get(tc.getCommandName()); assertNotNull(command); assertEquals("Name should be bound", name, command.getName()); BindException errors = (BindException)mav.getModel().get(ERRORS_KEY); assertEquals("There should be 1 error", 1, errors.getErrorCount()); assertNotNull(errors.getFieldError("age")); assertEquals("typeMismatch", errors.getFieldError("age").getCode()); }
public void testWithValidatorAddingGlobalError() throws Exception { final String errorCode = "someCode"; final String defaultMessage = "validation error!"; TestController tc = new TestController(); tc.setValidator(new Validator() { @Override public boolean supports(Class c) { return TestBean.class.isAssignableFrom(c); } @Override public void validate(Object o, Errors e) { e.reject(errorCode, defaultMessage); } }); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); ModelAndView mav = tc.handleRenderRequest(request, response); BindException errors = (BindException)mav.getModel().get(ERRORS_KEY); assertEquals("There should be 1 error", 1, errors.getErrorCount()); ObjectError error = errors.getGlobalError(); assertEquals(error.getCode(), errorCode); assertEquals(error.getDefaultMessage(), defaultMessage); }
public void testUnauthorizedUser() throws Exception { UserRoleAuthorizationInterceptor interceptor = new UserRoleAuthorizationInterceptor(); String validRole = "allowed"; interceptor.setAuthorizedRoles(new String[] {validRole}); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); Object handler = new Object(); request.addUserRole("someOtherRole"); assertFalse(request.isUserInRole(validRole)); try { interceptor.preHandle(request, response, handler); fail("should have thrown PortletSecurityException"); } catch (PortletSecurityException ex) { // expected } }
public void testRequestWithNoUserRoles() throws Exception { UserRoleAuthorizationInterceptor interceptor = new UserRoleAuthorizationInterceptor(); String validRole = "allowed"; interceptor.setAuthorizedRoles(new String[] {validRole}); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); Object handler = new Object(); assertFalse(request.isUserInRole(validRole)); try { interceptor.preHandle(request, response, handler); fail("should have thrown PortletSecurityException"); } catch (PortletSecurityException ex) { // expected } }
public void testSimpleFormViewWithSessionAndBindOnNewForm() throws Exception { MockRenderRequest renderRequest = new MockRenderRequest(); MockRenderResponse renderResponse = new MockRenderResponse(); renderRequest.setParameter("action", "form-session-bind"); renderRequest.setParameter("age", "30"); TestBean testBean = new TestBean(); testBean.setAge(40); SimplePortletApplicationContext ac = (SimplePortletApplicationContext)simpleDispatcherPortlet.getPortletApplicationContext(); String formAttribute = ac.getFormSessionAttributeName(); PortletSession session = new MockPortletSession(); session.setAttribute(formAttribute, testBean); renderRequest.setSession(session); simpleDispatcherPortlet.doDispatch(renderRequest, renderResponse); assertEquals("35", renderResponse.getContentAsString()); }
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 testNoDetectAllHandlerMappingsWithParameterRenderRequest() throws Exception { DispatcherPortlet complexDispatcherPortlet = new DispatcherPortlet(); complexDispatcherPortlet.setContextClass(ComplexPortletApplicationContext.class); complexDispatcherPortlet.setNamespace("test"); complexDispatcherPortlet.setDetectAllHandlerMappings(false); complexDispatcherPortlet.init(new MockPortletConfig(getPortletContext(), "complex")); MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "test1"); complexDispatcherPortlet.doDispatch(request, response); Map model = (Map) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception) model.get("exception"); assertTrue(exception.getClass().equals(NoHandlerFoundException.class)); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-unavailable", view.getBeanName()); }
public void testPortletHandlerAdapterRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "myPortlet"); complexDispatcherPortlet.doDispatch(request, response); assertEquals("myPortlet was here", response.getContentAsString()); 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()); }
public void testValidRenderRequestWithExistingThreadLocalRequestContext() throws IOException, PortletException { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); 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", "form"); request.setParameter("age", "29"); simpleDispatcherPortlet.doDispatch(request, response); assertSame(servletLocaleContext, LocaleContextHolder.getLocaleContext()); assertSame(servletRequestAttrs, RequestContextHolder.getRequestAttributes()); } finally { RequestContextHolder.resetRequestAttributes(); LocaleContextHolder.resetLocaleContext(); } }
@Test public void renderRequest() throws Exception { MockRenderResponse response = new MockRenderResponse(); controller.handleRenderRequest(new MockRenderRequest(), response); assertEquals(RENDERED_RESPONSE_CONTENT, response.getContentAsString()); }
@Test public void portletModeParameterMappingInvalidHelpRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setPortletMode(PortletMode.HELP); request.setParameter("action", "help3"); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); assertTrue(model.get("exception").getClass().equals(NoHandlerFoundException.class)); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-unavailable", view.getBeanName()); }
@Test public void portletModeMappingValidViewRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setPortletMode(PortletMode.VIEW); request.addUserRole("role2"); request.setParameter("action", "not mapped"); request.setParameter("myParam", "not mapped"); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); assertEquals("view was here", model.get("result")); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("someViewName", view.getBeanName()); }
@Test public void parameterMappingValidRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setPortletMode(PortletMode.VIEW); request.setParameter("action", "not mapped"); request.setParameter("myParam", "test2"); complexDispatcherPortlet.doDispatch(request, response); assertEquals("test2-view", response.getProperty("result")); }
@Test public void unknownHandlerRenderRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "unknown"); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception)model.get("exception"); assertTrue(exception.getClass().equals(PortletException.class)); assertTrue(exception.getMessage().indexOf("No adapter for handler") != -1); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-default-1", view.getBeanName()); }
@Test public void renderRequestHandledEvent() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); complexDispatcherPortlet.doDispatch(request, response); ComplexPortletApplicationContext.TestApplicationListener listener = (ComplexPortletApplicationContext.TestApplicationListener) complexDispatcherPortlet.getPortletApplicationContext().getBean("testListener"); assertEquals(1, listener.counter); }
@Test public void correctLocaleInRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "requestLocaleChecker"); request.addPreferredLocale(Locale.CANADA); complexDispatcherPortlet.doDispatch(request, response); assertEquals("locale-ok", response.getContentAsString()); }
@Test public void incorrectLocaleInRequest() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "requestLocaleChecker"); request.addPreferredLocale(Locale.ENGLISH); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception) model.get("exception"); assertTrue(exception.getClass().equals(PortletException.class)); assertEquals("Incorrect Locale in RenderRequest", exception.getMessage()); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-default-1", view.getBeanName()); }
@Test public void correctLocaleInLocaleContextHolder() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "contextLocaleChecker"); request.addPreferredLocale(Locale.CANADA); complexDispatcherPortlet.doDispatch(request, response); assertEquals("locale-ok", response.getContentAsString()); }
@Test public void incorrectLocaleInLocalContextHolder() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setParameter("myParam", "contextLocaleChecker"); request.addPreferredLocale(Locale.ENGLISH); complexDispatcherPortlet.doDispatch(request, response); Map<?, ?> model = (Map<?, ?>) request.getAttribute(ViewRendererServlet.MODEL_ATTRIBUTE); Exception exception = (Exception) model.get("exception"); assertTrue(exception.getClass().equals(PortletException.class)); assertEquals("Incorrect Locale in LocaleContextHolder", exception.getMessage()); InternalResourceView view = (InternalResourceView) request.getAttribute(ViewRendererServlet.VIEW_ATTRIBUTE); assertEquals("failed-default-1", view.getBeanName()); }
@Test public void handlerInterceptorNoAbort() throws Exception { MockRenderRequest request = new MockRenderRequest(); MockRenderResponse response = new MockRenderResponse(); request.setPortletMode(PortletMode.VIEW); request.addUserRole("role1"); request.addParameter("abort", "false"); complexDispatcherPortlet.doDispatch(request, response); assertTrue(request.getAttribute("test1-remove-never") != null); assertTrue(request.getAttribute("test1-remove-post") == null); assertTrue(request.getAttribute("test1-remove-after") == null); assertTrue(request.getAttribute("test2-remove-never") != null); assertTrue(request.getAttribute("test2-remove-post") == null); assertTrue(request.getAttribute("test2-remove-after") == null); }