public void defaultHandlerMappings() throws Exception { StaticWebApplicationContext cxt = new StaticWebApplicationContext(); cxt.refresh(); List<HandlerMapping> actual = getIntrospector(cxt).getHandlerMappings(); assertEquals(2, actual.size()); assertEquals(BeanNameUrlHandlerMapping.class, actual.get(0).getClass()); assertEquals(RequestMappingHandlerMapping.class, actual.get(1).getClass()); }
@Test public void getMatchable() throws Exception { MutablePropertyValues pvs = new MutablePropertyValues( Collections.singletonMap("urlMap", Collections.singletonMap("/path", new Object()))); StaticWebApplicationContext cxt = new StaticWebApplicationContext(); cxt.registerSingleton("hm", SimpleUrlHandlerMapping.class, pvs); cxt.refresh(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); MatchableHandlerMapping hm = getIntrospector(cxt).getMatchableHandlerMapping(request); assertEquals(cxt.getBean("hm"), hm); assertNull("Attributes changes not ignored", request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE)); }
@Test public void modelAttributeAdviceInParentContext() throws Exception { StaticWebApplicationContext parent = new StaticWebApplicationContext(); parent.registerSingleton("maa", ModelAttributeAdvice.class); parent.refresh(); this.webAppContext.setParent(parent); this.webAppContext.refresh(); HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle"); this.handlerAdapter.afterPropertiesSet(); ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod); assertEquals("lAttr1", mav.getModel().get("attr1")); assertEquals("gAttr2", mav.getModel().get("attr2")); }
@Before @SuppressWarnings("resource") public void setup() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); Properties props = new Properties(); props.setProperty("myOrigin", "http://example.com"); wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class); wac.refresh(); this.handlerMapping.setRemoveSemicolonContent(false); wac.getAutowireCapableBeanFactory().initializeBean(this.handlerMapping, "hm"); this.request.setMethod("GET"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); }
@Test public void modelAttributeAdvice() throws Exception { this.webAppContext.registerSingleton("maa", ModelAttributeAdvice.class); this.webAppContext.refresh(); HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle"); this.handlerAdapter.afterPropertiesSet(); ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod); assertEquals("lAttr1", mav.getModel().get("attr1")); assertEquals("gAttr2", mav.getModel().get("attr2")); }
@Test @SuppressWarnings("resource") public void testWebApplicationObjectSupport() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); File tempDir = new File(""); wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir); wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class)); wac.refresh(); WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test"); assertEquals(wao.getServletContext(), wac.getServletContext()); assertEquals(wao.getTempDir(), tempDir); }
@Test public void modelAttributePackageNameAdvice() throws Exception { this.webAppContext.registerSingleton("mapa", ModelAttributePackageAdvice.class); this.webAppContext.registerSingleton("manupa", ModelAttributeNotUsedPackageAdvice.class); this.webAppContext.refresh(); HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle"); this.handlerAdapter.afterPropertiesSet(); ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod); assertEquals("lAttr1", mav.getModel().get("attr1")); assertEquals("gAttr2", mav.getModel().get("attr2")); assertEquals(null,mav.getModel().get("attr3")); }
@Test public void detectHandlerMappingsOrdered() throws Exception { StaticWebApplicationContext cxt = new StaticWebApplicationContext(); MutablePropertyValues pvs = new MutablePropertyValues(Collections.singletonMap("order", "3")); cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class, pvs); pvs = new MutablePropertyValues(Collections.singletonMap("order", "2")); cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class, pvs); pvs = new MutablePropertyValues(Collections.singletonMap("order", "1")); cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class, pvs); cxt.refresh(); List<?> expected = Arrays.asList(cxt.getBean("hmC"), cxt.getBean("hmB"), cxt.getBean("hmA")); List<HandlerMapping> actual = getIntrospector(cxt).getHandlerMappings(); assertEquals(expected, actual); }
@Test(expected = IllegalStateException.class) public void getMatchableWhereHandlerMappingDoesNotImplementMatchableInterface() throws Exception { StaticWebApplicationContext cxt = new StaticWebApplicationContext(); cxt.registerSingleton("hm1", TestHandlerMapping.class); cxt.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(); getIntrospector(cxt).getMatchableHandlerMapping(request); }
@Test public void controllerAdviceWithNestedException() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message")); assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); }
@Test public void detectHandlerMappings() throws Exception { StaticWebApplicationContext cxt = new StaticWebApplicationContext(); cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class); cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class); cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class); cxt.refresh(); List<?> expected = Arrays.asList(cxt.getBean("hmA"), cxt.getBean("hmB"), cxt.getBean("hmC")); List<HandlerMapping> actual = getIntrospector(cxt).getHandlerMappings(); assertEquals(expected, actual); }
@Before public void setUp() throws Exception { rb.setBasename(PROPS_FILE); rb.setCache(getCache()); rb.setDefaultParentView("testParent"); wac.setServletContext(new MockServletContext()); wac.refresh(); // This will be propagated to views, so we need it. rb.setApplicationContext(wac); }
@Before public void setUp() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); this.renderer = mock(Renderer.class); this.viewResolver = new TilesViewResolver(); this.viewResolver.setRenderer(this.renderer); this.viewResolver.setApplicationContext(wac); }
@Test public void staticWebApplicationContext() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); assertHasStandardServletEnvironment(ctx); registerEnvironmentBeanDefinition(ctx); ctx.setEnvironment(prodWebEnv); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); }
@Before public void createViewResolver() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); wac.refresh(); viewResolver = new ContentNegotiatingViewResolver(); viewResolver.setApplicationContext(wac); request = new MockHttpServletRequest("GET", "/test"); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); }
@Test @SuppressWarnings("resource") public void testServletContextParameterFactoryBean() { MockServletContext sc = new MockServletContext(); sc.addInitParameter("myParam", "myValue"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("initParamName", "myParam"); wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs); wac.refresh(); Object value = wac.getBean("importedParam"); assertEquals("myValue", value); }
@Test @SuppressWarnings("resource") public void testServletContextAttributeFactoryBean() { MockServletContext sc = new MockServletContext(); sc.setAttribute("myAttr", "myValue"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("attributeName", "myAttr"); wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs); wac.refresh(); Object value = wac.getBean("importedAttr"); assertEquals("myValue", value); }
@Test public void missingTemplate() throws Exception { MockServletContext servletContext = new MockServletContext(); this.wac.setServletContext(servletContext); this.wac.refresh(); this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/"); this.view.setUrl("missing.txt"); this.view.setEngine(mock(InvocableScriptEngine.class)); this.configurer.setRenderFunction("render"); this.view.setApplicationContext(this.wac); assertFalse(this.view.checkResource(Locale.ENGLISH)); }
@Test public void controllerAdviceWithinDispatcherServlet() throws Exception { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("controller", ExceptionThrowingController.class); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); DispatcherServlet servlet = new DispatcherServlet(ctx); servlet.init(new MockServletConfig()); servlet.service(this.servletRequest, this.servletResponse); assertEquals(400, this.servletResponse.getStatus()); assertEquals("error content", this.servletResponse.getContentAsString()); assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader")); }
@Test public void controllerAdvice() throws Exception { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); ServletRequestBindingException ex = new ServletRequestBindingException("message"); assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); assertEquals(400, this.servletResponse.getStatus()); assertEquals("error content", this.servletResponse.getContentAsString()); assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader")); }