/** * Return the underlying ServletContext. Only intended for cooperating classes in this package. */ @Nullable protected final ServletContext getServletContext() { return this.webApplicationContext.getServletContext(); }
/** * Protected constructor. Not intended for direct instantiation. * @see MockMvcBuilders#webAppContextSetup(WebApplicationContext) */ protected DefaultMockMvcBuilder(WebApplicationContext webAppContext) { Assert.notNull(webAppContext, "WebApplicationContext is required"); Assert.notNull(webAppContext.getServletContext(), "WebApplicationContext must have a ServletContext"); this.webAppContext = webAppContext; }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { if (context instanceof WebApplicationContext) { WebApplicationContext wac = (WebApplicationContext) context; ServletContext sc = wac.getServletContext(); if (sc != null) { sc.setAttribute("javax.websocket.server.ServerContainer", new MockServerContainer()); } } }
/** * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the * given ApplicationContext is a {@link WebApplicationContext}. */ @Override protected void initApplicationContext(ApplicationContext context) { super.initApplicationContext(context); if (this.servletContext == null && context instanceof WebApplicationContext) { this.servletContext = ((WebApplicationContext) context).getServletContext(); if (this.servletContext != null) { initServletContext(this.servletContext); } } }
@Override protected WebApplicationContext initWebAppContext() { ServletContext servletContext = this.webAppContext.getServletContext(); Assert.state(servletContext != null, "No ServletContext"); ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (rootWac == null) { rootWac = this.webAppContext; ApplicationContext parent = this.webAppContext.getParent(); while (parent != null) { if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { rootWac = parent; break; } parent = parent.getParent(); } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); } return this.webAppContext; }
@Test public void testApplicationScope() { WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION); assertNull(ac.getServletContext().getAttribute(NAME)); DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class); assertSame(bean, ac.getServletContext().getAttribute(NAME)); assertSame(bean, ac.getBean(NAME)); new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext())); assertTrue(bean.wasDestroyed()); }
@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void testContextLoaderListenerWithLocalContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString( new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithGlobalContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString( new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithMixedContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName()); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestWebContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void renderWithoutStaticAttributes() throws Exception { WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getServletContext()).willReturn(new MockServletContext()); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); TestView tv = new TestView(wac); // Check superclass handles duplicate init tv.setApplicationContext(wac); tv.setApplicationContext(wac); Map<String, Object> model = new HashMap<>(); model.put("foo", "bar"); model.put("something", new Object()); tv.render(model, request, response); checkContainsAll(model, tv.model); assertTrue(tv.initialized); }
@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void noFreeMarkerConfig() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<>()); given(wac.getServletContext()).willReturn(new MockServletContext()); fv.setUrl("anythingButNull"); exception.expect(ApplicationContextException.class); exception.expectMessage(containsString("FreeMarkerConfig")); fv.setApplicationContext(wac); }
@Test // SPR-12553 public void applicationContextAttribute() { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController()); builder.addPlaceholderValue("sys.login.ajax", "/foo"); WebApplicationContext wac = builder.initWebAppContext(); assertEquals(wac, WebApplicationContextUtils.getRequiredWebApplicationContext(wac.getServletContext())); }
@Before public void setUp() { given(wac.getServletContext()).willReturn(mockServletContext); given(testContext.getApplicationContext()).willReturn(wac); MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext); MockHttpServletResponse response = new MockHttpServletResponse(); ServletWebRequest servletWebRequest = new ServletWebRequest(request, response); request.setAttribute(SET_UP_OUTSIDE_OF_STEL, "true"); RequestContextHolder.setRequestAttributes(servletWebRequest); assertSetUpOutsideOfStelAttributeExists(); }