@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 testSecureParameters() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, Boolean.TRUE.toString()); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "true"); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "TRUE"); Assert.assertTrue(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "yes"); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, null); Assert.assertFalse(href.isRequestedSecure()); href.addParameter(PortletHref.PARAM_SECURE, "false"); Assert.assertFalse(href.isRequestedSecure()); }
@Test public void testAddParameters() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter(PortletHref.PARAM_MODE, "help"); href.addParameter(PortletHref.PARAM_STATE, "maximized"); href.addParameter(PortletHref.PARAM_SECURE, "true"); href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION); href.addParameter("SINGLE_PARAM", "VAL1"); href.addParameter("INT_PARAM", 31337); Assert.assertTrue(href.isRequestedSecure()); Assert.assertEquals(new PortletMode("help"), href.getRequestedMode()); Assert.assertEquals(new WindowState("maximized"), href.getRequestedState()); Assert.assertTrue(href.isAction()); final Map<String, String[]> actualParams = href.getParameterMap(); Assert.assertEquals(2, actualParams.size()); Assert.assertArrayEquals(new String[]{"VAL1"}, actualParams.get("SINGLE_PARAM")); Assert.assertArrayEquals(new String[]{"31337"}, actualParams.get("INT_PARAM")); }
@Test public void testRemoveParameter() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter("SINGLE_PARAM", "VAL1"); final Map<String, String[]> actualParams = href.getParameterMap(); Assert.assertEquals(1, actualParams.size()); Assert.assertArrayEquals(new String[]{"VAL1"}, actualParams.get("SINGLE_PARAM")); href.removeParameter("SINGLE_PARAM"); final Map<String, String[]> actualParams2 = href.getParameterMap(); Assert.assertEquals(0, actualParams2.size()); Assert.assertNull(actualParams2.get("SINGLE_PARAM")); }
@Test public void testBaseUrl() { final PortletHref href = new PortletHref(new MockPortletRequest(), new MockRenderResponse()); href.addParameter(PortletHref.PARAM_MODE, "help"); href.addParameter(PortletHref.PARAM_STATE, "maximized"); href.addParameter(PortletHref.PARAM_SECURE, "true"); href.addParameter("SINGLE_PARAM", "VAL1"); final String baseRenderUrl = href.getBaseUrl(); Assert.assertEquals("http://localhost/mockportlet?urlType=render", baseRenderUrl); href.addParameter(PortletHref.PARAM_TYPE, PortletHref.TYPE_ACTION); final String baseActionUrl = href.getBaseUrl(); Assert.assertEquals("http://localhost/mockportlet?urlType=action", baseActionUrl); }
@Test public void testCreateEmptyHref() { final MockPageContext pageContext = new MockPageContext(); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, new MockPortletRequest()); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); final PortletRequestHelper helper = new PortletRequestHelper(pageContext); final PortletHref ref = (PortletHref) helper.getHref(); final Map<String, String[]> params = ref.getParameterMap(); Assert.assertEquals(0, params.size()); Assert.assertNull(ref.getAnchor()); Assert.assertNull(ref.getRequestedMode()); Assert.assertNull(ref.getRequestedState()); Assert.assertFalse(ref.isRequestedSecure()); }
@Test public void testCreateSecureHref() { final MockPageContext pageContext = new MockPageContext(); final MockPortletRequest request = new MockPortletRequest(); request.setSecure(true); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_REQUEST, request); pageContext.setAttribute(PortletRequestHelper.JAVAX_PORTLET_RESPONSE, new MockRenderResponse()); final PortletRequestHelper helper = new PortletRequestHelper(pageContext); final PortletHref ref = (PortletHref) helper.getHref(); final Map<String, String[]> params = ref.getParameterMap(); Assert.assertEquals(0, params.size()); Assert.assertNull(ref.getAnchor()); Assert.assertNull(ref.getRequestedMode()); Assert.assertNull(ref.getRequestedState()); Assert.assertTrue(ref.isRequestedSecure()); }
@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); }