001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.web.servlet.setup; 018 019import javax.servlet.ServletContext; 020 021import org.springframework.context.ApplicationContext; 022import org.springframework.util.Assert; 023import org.springframework.web.context.WebApplicationContext; 024import org.springframework.web.context.support.WebApplicationContextUtils; 025 026/** 027 * A concrete implementation of {@link AbstractMockMvcBuilder} that provides 028 * the {@link WebApplicationContext} supplied to it as a constructor argument. 029 * 030 * <p>In addition, if the {@link ServletContext} in the supplied 031 * {@code WebApplicationContext} does not contain an entry for the 032 * {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} 033 * key, the root {@code WebApplicationContext} will be detected and stored 034 * in the {@code ServletContext} under the 035 * {@code ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key. 036 * 037 * @author Rossen Stoyanchev 038 * @author Rob Winch 039 * @author Sam Brannen 040 * @since 3.2 041 */ 042public class DefaultMockMvcBuilder extends AbstractMockMvcBuilder<DefaultMockMvcBuilder> { 043 044 private final WebApplicationContext webAppContext; 045 046 047 /** 048 * Protected constructor. Not intended for direct instantiation. 049 * @see MockMvcBuilders#webAppContextSetup(WebApplicationContext) 050 */ 051 protected DefaultMockMvcBuilder(WebApplicationContext webAppContext) { 052 Assert.notNull(webAppContext, "WebApplicationContext is required"); 053 Assert.notNull(webAppContext.getServletContext(), "WebApplicationContext must have a ServletContext"); 054 this.webAppContext = webAppContext; 055 } 056 057 @Override 058 protected WebApplicationContext initWebAppContext() { 059 060 ServletContext servletContext = this.webAppContext.getServletContext(); 061 ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); 062 063 if (rootWac == null) { 064 rootWac = this.webAppContext; 065 ApplicationContext parent = this.webAppContext.getParent(); 066 while (parent != null) { 067 if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { 068 rootWac = parent; 069 break; 070 } 071 parent = parent.getParent(); 072 } 073 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); 074 } 075 076 return this.webAppContext; 077 } 078 079}