From 1d35c7c55a8294fe05d259f196318cfb9dd98c32 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 25 Jan 2017 11:37:32 -0500 Subject: [PATCH] Polish MockMvc setup classes --- .../servlet/setup/AbstractMockMvcBuilder.java | 26 +++++++------- .../setup/ConfigurableMockMvcBuilder.java | 5 +-- .../web/servlet/setup/MockMvcConfigurer.java | 36 ++++++++++++------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java index 125ccbe436..a20674b007 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,14 +33,16 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; +import org.springframework.test.web.servlet.MockMvcBuilder; /** - * An abstract implementation of {@link org.springframework.test.web.servlet.MockMvcBuilder} - * with common methods for configuring filters, default request properties, global - * expectations and global result actions. - *

- * Sub-classes can use different strategies to prepare a WebApplicationContext to - * pass to the DispatcherServlet. + * Abstract implementation of {@link MockMvcBuilder} with common methods for + * configuring filters, default request properties, global expectations and + * global result actions. + * + *

Sub-classes can use different strategies to prepare the Spring + * {@code WebApplicationContext} that will be passed to the + * {@code DispatcherServlet}. * * @author Rossen Stoyanchev * @author Stephane Nicoll @@ -64,7 +66,6 @@ public abstract class AbstractMockMvcBuilder public final T addFilters(Filter... filters) { Assert.notNull(filters, "filters cannot be null"); - for (Filter f : filters) { Assert.notNull(f, "filters cannot contain null values"); this.filters.add(f); @@ -73,14 +74,11 @@ public abstract class AbstractMockMvcBuilder } public final T addFilter(Filter filter, String... urlPatterns) { - Assert.notNull(filter, "filter cannot be null"); Assert.notNull(urlPatterns, "urlPatterns cannot be null"); - if (urlPatterns.length > 0) { filter = new PatternMappingFilterProxy(filter, urlPatterns); } - this.filters.add(filter); return self(); } @@ -153,9 +151,9 @@ public abstract class AbstractMockMvcBuilder } /** - * A method to obtain the WebApplicationContext to be passed to the DispatcherServlet. - * Invoked from {@link #build()} before the - * {@link org.springframework.test.web.servlet.MockMvc} instance is created. + * A method to obtain the {@code WebApplicationContext} to be passed to the + * {@code DispatcherServlet}. Invoked from {@link #build()} before the + * {@link MockMvc} instance is created. */ protected abstract WebApplicationContext initWebAppContext(); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/ConfigurableMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/ConfigurableMockMvcBuilder.java index 179307de87..b97d63969b 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/ConfigurableMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/ConfigurableMockMvcBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,8 @@ import org.springframework.test.web.servlet.ResultMatcher; * @author Rossen Stoyanchev * @since 4.1 */ -public interface ConfigurableMockMvcBuilder> extends MockMvcBuilder { +public interface ConfigurableMockMvcBuilder> + extends MockMvcBuilder { /** * Add filters mapped to any request (i.e. "/*"). For example: diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcConfigurer.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcConfigurer.java index 4ec79765c9..c0152adbfb 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcConfigurer.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,17 +19,21 @@ package org.springframework.test.web.servlet.setup; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.web.context.WebApplicationContext; - /** - * Allows a sub-class to encapsulate logic for pre-configuring a - * {@code ConfigurableMockMvcBuilder} for some specific purpose. A 3rd party - * library may use this to provide shortcuts for setting up MockMvc. + * Contract for customizing a {@code ConfigurableMockMvcBuilder} in some + * specific way, e.g. a 3rd party library that wants to provide shortcuts for + * setting up a MockMvc. * - *

Can be plugged in via {@link ConfigurableMockMvcBuilder#apply} with - * instances of this type likely created via static methods, e.g.: + *

An implementation of this interface can be plugged in via + * {@link ConfigurableMockMvcBuilder#apply} with instances of this type likely + * created via static methods, e.g.: * *

- * 	MockMvcBuilders.webAppContextSetup(context).apply(mySetup("foo","bar")).build();
+ * import static org.example.ExampleSetup.mySetup;
+ *
+ * // ...
+ *
+ * MockMvcBuilders.webAppContextSetup(context).apply(mySetup("foo","bar")).build();
  * 
* * @author Rossen Stoyanchev @@ -39,16 +43,22 @@ import org.springframework.web.context.WebApplicationContext; public interface MockMvcConfigurer { /** - * Invoked immediately after a {@code MockMvcConfigurer} is added via + * Invoked immediately when this {@code MockMvcConfigurer} is added via * {@link ConfigurableMockMvcBuilder#apply}. + * @param builder the builder for the MockMvc */ void afterConfigurerAdded(ConfigurableMockMvcBuilder builder); /** - * Invoked just before the MockMvc instance is created. Implementations may - * return a RequestPostProcessor to be applied to every request performed - * through the created {@code MockMvc} instance. + * Invoked when the MockMvc instance is about to be created with the MockMvc + * builder and the Spring WebApplicationContext that will be passed to the + * {@code DispatcherServlet}. + * @param builder the builder for the MockMvc + * @param context the Spring configuration + * @return a post processor to be applied to every request performed + * through the {@code MockMvc} instance. */ - RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder builder, WebApplicationContext context); + RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder builder, + WebApplicationContext context); }