|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet.setup; |
| 18 | + |
| 19 | +import org.springframework.mock.web.MockServletConfig; |
| 20 | +import org.springframework.test.web.servlet.*; |
| 21 | +import org.springframework.util.Assert; |
| 22 | +import org.springframework.web.context.WebApplicationContext; |
| 23 | + |
| 24 | +import javax.servlet.Filter; |
| 25 | +import javax.servlet.ServletContext; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** |
| 30 | + * An abstract implementation of {@link org.springframework.test.web.servlet.MockMvcBuilder} |
| 31 | + * with common methods for configuring filters, default request properties, global |
| 32 | + * expectations and global result actions. |
| 33 | + * <p> |
| 34 | + * Sub-classes can use different strategies to prepare a WebApplicationContext to |
| 35 | + * pass to the DispatcherServlet. |
| 36 | + * |
| 37 | + * @author Rossen Stoyanchev |
| 38 | + * @since 4.0 |
| 39 | + */ |
| 40 | +public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>> |
| 41 | + extends MockMvcBuilderSupport implements MockMvcBuilder { |
| 42 | + |
| 43 | + private List<Filter> filters = new ArrayList<Filter>(); |
| 44 | + |
| 45 | + private RequestBuilder defaultRequestBuilder; |
| 46 | + |
| 47 | + private final List<ResultMatcher> globalResultMatchers = new ArrayList<ResultMatcher>(); |
| 48 | + |
| 49 | + private final List<ResultHandler> globalResultHandlers = new ArrayList<ResultHandler>(); |
| 50 | + |
| 51 | + private Boolean dispatchOptions = Boolean.FALSE; |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Add filters mapped to any request (i.e. "/*"). For example: |
| 57 | + * |
| 58 | + * <pre class="code"> |
| 59 | + * mockMvcBuilder.addFilters(springSecurityFilterChain); |
| 60 | + * </pre> |
| 61 | + * |
| 62 | + * <p>is the equivalent of the following web.xml configuration: |
| 63 | + * |
| 64 | + * <pre class="code"> |
| 65 | + * <filter-mapping> |
| 66 | + * <filter-name>springSecurityFilterChain</filter-name> |
| 67 | + * <url-pattern>/*</url-pattern> |
| 68 | + * </filter-mapping> |
| 69 | + * </pre> |
| 70 | + * |
| 71 | + * <p>Filters will be invoked in the order in which they are provided. |
| 72 | + * |
| 73 | + * @param filters the filters to add |
| 74 | + */ |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public final <T extends B> T addFilters(Filter... filters) { |
| 77 | + Assert.notNull(filters, "filters cannot be null"); |
| 78 | + |
| 79 | + for(Filter f : filters) { |
| 80 | + Assert.notNull(f, "filters cannot contain null values"); |
| 81 | + this.filters.add(f); |
| 82 | + } |
| 83 | + return (T) this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Add a filter mapped to a specific set of patterns. For example: |
| 88 | + * |
| 89 | + * <pre class="code"> |
| 90 | + * mockMvcBuilder.addFilters(myResourceFilter, "/resources/*"); |
| 91 | + * </pre> |
| 92 | + * |
| 93 | + * <p>is the equivalent of: |
| 94 | + * |
| 95 | + * <pre class="code"> |
| 96 | + * <filter-mapping> |
| 97 | + * <filter-name>myResourceFilter</filter-name> |
| 98 | + * <url-pattern>/resources/*</url-pattern> |
| 99 | + * </filter-mapping> |
| 100 | + * </pre> |
| 101 | + * |
| 102 | + * <p>Filters will be invoked in the order in which they are provided. |
| 103 | + * |
| 104 | + * @param filter the filter to add |
| 105 | + * @param urlPatterns URL patterns to map to; if empty, "/*" is used by default |
| 106 | + */ |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + public final <T extends B> T addFilter(Filter filter, String... urlPatterns) { |
| 109 | + |
| 110 | + Assert.notNull(filter, "filter cannot be null"); |
| 111 | + Assert.notNull(urlPatterns, "urlPatterns cannot be null"); |
| 112 | + |
| 113 | + if(urlPatterns.length > 0) { |
| 114 | + filter = new PatternMappingFilterProxy(filter, urlPatterns); |
| 115 | + } |
| 116 | + |
| 117 | + this.filters.add(filter); |
| 118 | + return (T) this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Define default request properties that should be merged into all |
| 123 | + * performed requests. In effect this provides a mechanism for defining |
| 124 | + * common initialization for all requests such as the content type, request |
| 125 | + * parameters, session attributes, and any other request property. |
| 126 | + * |
| 127 | + * <p>Properties specified at the time of performing a request override the |
| 128 | + * default properties defined here. |
| 129 | + * |
| 130 | + * @param requestBuilder a RequestBuilder; see static factory methods in |
| 131 | + * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders} |
| 132 | + * . |
| 133 | + */ |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + public final <T extends B> T defaultRequest(RequestBuilder requestBuilder) { |
| 136 | + this.defaultRequestBuilder = requestBuilder; |
| 137 | + return (T) this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Define a global expectation that should <em>always</em> be applied to |
| 142 | + * every response. For example, status code 200 (OK), content type |
| 143 | + * {@code "application/json"}, etc. |
| 144 | + * |
| 145 | + * @param resultMatcher a ResultMatcher; see static factory methods in |
| 146 | + * {@link org.springframework.test.web.servlet.result.MockMvcResultMatchers} |
| 147 | + */ |
| 148 | + @SuppressWarnings("unchecked") |
| 149 | + public final <T extends B> T alwaysExpect(ResultMatcher resultMatcher) { |
| 150 | + this.globalResultMatchers.add(resultMatcher); |
| 151 | + return (T) this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Define a global action that should <em>always</em> be applied to every |
| 156 | + * response. For example, writing detailed information about the performed |
| 157 | + * request and resulting response to {@code System.out}. |
| 158 | + * |
| 159 | + * @param resultHandler a ResultHandler; see static factory methods in |
| 160 | + * {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers} |
| 161 | + */ |
| 162 | + @SuppressWarnings("unchecked") |
| 163 | + public final <T extends B> T alwaysDo(ResultHandler resultHandler) { |
| 164 | + this.globalResultHandlers.add(resultHandler); |
| 165 | + return (T) this; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Should the {@link org.springframework.web.servlet.DispatcherServlet} dispatch OPTIONS request to controllers. |
| 170 | + * @param dispatchOptions |
| 171 | + * @see org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest(boolean) |
| 172 | + */ |
| 173 | + @SuppressWarnings("unchecked") |
| 174 | + public final <T extends B> T dispatchOptions(boolean dispatchOptions) { |
| 175 | + this.dispatchOptions = dispatchOptions; |
| 176 | + return (T) this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Build a {@link org.springframework.test.web.servlet.MockMvc} instance. |
| 181 | + */ |
| 182 | + @Override |
| 183 | + public final MockMvc build() { |
| 184 | + |
| 185 | + WebApplicationContext wac = initWebAppContext(); |
| 186 | + |
| 187 | + ServletContext servletContext = wac.getServletContext(); |
| 188 | + MockServletConfig mockServletConfig = new MockServletConfig(servletContext); |
| 189 | + |
| 190 | + Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]); |
| 191 | + |
| 192 | + return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, |
| 193 | + this.globalResultMatchers, this.globalResultHandlers, this.dispatchOptions); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * A method to obtain the WebApplicationContext to be passed to the DispatcherServlet. |
| 198 | + * Invoked from {@link #build()} before the |
| 199 | + * {@link org.springframework.test.web.servlet.MockMvc} instance is created. |
| 200 | + */ |
| 201 | + protected abstract WebApplicationContext initWebAppContext(); |
| 202 | + |
| 203 | +} |
0 commit comments