@Override protected WebApplicationContext createRootApplicationContext() { StaticWebApplicationContext rootContext = new StaticWebApplicationContext(); rootContext.registerSingleton(BEAN_NAME, MyBean.class); return rootContext; } }
@Override protected WebApplicationContext createServletApplicationContext() { StaticWebApplicationContext servletContext = new StaticWebApplicationContext(); servletContext.registerSingleton("bean", MyBean.class); return servletContext; }
@Before public void setup() { StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class); context.registerSingleton("tilesConfigurer", TilesConfigurer.class); context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class); context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class); this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context); }
@Before public void setup() { StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class); context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class); this.registry = new ViewResolverRegistry(context); }
@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); }
@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 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 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)); }
@Before public void setUp() { this.context = new StaticWebApplicationContext(); this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader())); this.context.registerSingleton("controller", TestController.class); this.context.registerSingleton("userController", UserController.class); this.config = new TestWebMvcConfigurationSupport(); this.config.setApplicationContext(this.context); this.config.setServletContext(this.context.getServletContext()); }
@Test public void getCorsConfigWithBeanNameHandler() throws Exception { String key = "foo"; String beanName = "handler1"; StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton(beanName, MyHandler.class); this.mapping.setApplicationContext(context); this.mapping.registerMapping(key, beanName, this.method1); HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)); CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod); assertNotNull(config); assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0)); }
@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(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 responseBodyAdvice() throws Exception { List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); this.handlerAdapter.setMessageConverters(converters); this.webAppContext.registerSingleton("rba", ResponseCodeSuppressingAdvice.class); this.webAppContext.refresh(); this.request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE); this.request.setParameter("c", "callback"); HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleBadRequest"); this.handlerAdapter.afterPropertiesSet(); this.handlerAdapter.handle(this.request, this.response, handlerMethod); assertEquals(200, this.response.getStatus()); assertEquals("{\"status\":400,\"message\":\"body\"}", this.response.getContentAsString()); }
@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 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")); }
protected MockPageContext createAndPopulatePageContext() throws JspException { MockPageContext pageContext = createPageContext(); MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest(); ((StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request)) .registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); extendRequest(request); extendPageContext(pageContext); RequestContext requestContext = new JspAwareRequestContext(pageContext); pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, requestContext); return pageContext; }
@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")); }
@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 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")); }