@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() { 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); }
@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/"); }
public DefaultRuntimeSpringConfiguration(ApplicationContext parent) { super(); this.context = new StaticWebApplicationContext(); context.setParent(parent); // if(parent != null){ // trySettingClassLoaderOnContextIfFoundInParent(parent); // } }
@Override protected WebApplicationContext createRootApplicationContext() { StaticWebApplicationContext rootContext = new StaticWebApplicationContext(); rootContext.registerSingleton(BEAN_NAME, MyBean.class); return rootContext; } }
@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); }
StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.addMessage("code1", locale, "messageX"); wac.refresh(); InternalResourceViewResolver vr = new InternalResourceViewResolver(); vr.setViewClass(JstlView.class);
@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); }
@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); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy() { StaticApplicationContext ear = new StaticApplicationContext(); StaticWebApplicationContext root = new StaticWebApplicationContext(); root.setParent(ear); root.setServletContext(this.servletContext); StaticWebApplicationContext dispatcher = new StaticWebApplicationContext(); dispatcher.setParent(root); dispatcher.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(dispatcher); WebApplicationContext wac = builder.initWebAppContext(); assertSame(dispatcher, wac); assertSame(root, wac.getParent()); assertSame(ear, wac.getParent().getParent()); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
@Before public void setup() { this.context = new StaticWebApplicationContext(); this.handlerMapping = new TestHandlerMapping(); this.request = new MockHttpServletRequest(); }
@Before public void setUp() throws Exception { ServletContext sc = new MockServletContext(); wac = new StaticWebApplicationContext(); wac.setServletContext(sc); // final Template expectedTemplate = new Template(); fc = new FreeMarkerConfigurer(); fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir")); fc.setServletContext(sc); fc.afterPropertiesSet(); wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc); wac.refresh(); request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); response = new MockHttpServletResponse(); }
@Test public void freeMarkerViewResolver() throws Exception { MockServletContext sc = new MockServletContext(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); wac.getBeanFactory().registerSingleton("configurer", configurer); wac.refresh(); FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix"); vr.setApplicationContext(wac); View view = vr.resolveViewName("test", Locale.CANADA); assertEquals("Correct view class", FreeMarkerView.class, view.getClass()); assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl()); view = vr.resolveViewName("non-existing", Locale.CANADA); assertNull(view); view = vr.resolveViewName("redirect:myUrl", Locale.getDefault()); assertEquals("Correct view class", RedirectView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl()); view = vr.resolveViewName("forward:myUrl", Locale.getDefault()); assertEquals("Correct view class", InternalResourceView.class, view.getClass()); assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl()); }
@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")); }
servletConfig.addInitParameter("pConfig1", "pConfig1Value"); StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.setServletConfig(servletConfig); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
@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); }
@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")); }
private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); given(request.getMethod()).willReturn("GET"); given(context.getNamedDispatcher("action")).willReturn(dispatcher); if (include) { given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath"); } else { given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null); } StaticWebApplicationContext sac = new StaticWebApplicationContext(); sac.setServletContext(context); sfc.setApplicationContext(sac); assertNull(sfc.handleRequest(request, response)); if (include) { verify(dispatcher).include(request, response); } else { verify(dispatcher).forward(request, response); } }
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 updateTargetUrlWithContextLoader() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); MockServletContext servletContext = new MockServletContext(); ContextLoader contextLoader = new ContextLoader(wac); contextLoader.initWebApplicationContext(servletContext); try { RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class); wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor); RedirectView rv = new RedirectView(); rv.setUrl("/path"); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); rv.render(new ModelMap(), request, response); verify(mockProcessor).processUrl(request, "/path"); } finally { contextLoader.closeWebApplicationContext(servletContext); } }