From c022f7c19d1dd47d47c36a9ec4dcc448f1fddad9 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 26 Jul 2018 15:42:30 -0400 Subject: [PATCH] Polish --- .../server/AbstractMockServerSpec.java | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java index 4191a353af..4725dd06a6 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java @@ -20,7 +20,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.springframework.util.Assert; +import org.springframework.lang.Nullable; +import org.springframework.util.CollectionUtils; import org.springframework.web.server.WebFilter; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.session.DefaultWebSessionManager; @@ -36,22 +37,33 @@ import org.springframework.web.server.session.WebSessionManager; abstract class AbstractMockServerSpec> implements WebTestClient.MockServerSpec { - private final List filters = new ArrayList<>(4); + @Nullable + private List filters; - private WebSessionManager sessionManager = new DefaultWebSessionManager(); + @Nullable + private WebSessionManager sessionManager; - private final List configurers = new ArrayList<>(4); + @Nullable + private List configurers; + + + AbstractMockServerSpec() { + // Default instance to be re-used across requests, unless one is configured explicitly + this.sessionManager = new DefaultWebSessionManager(); + } @Override - public T webFilter(WebFilter... filter) { - this.filters.addAll(Arrays.asList(filter)); + public T webFilter(WebFilter... filters) { + if (filters.length > 0) { + this.filters = (this.filters != null ? this.filters : new ArrayList<>(4)); + this.filters.addAll(Arrays.asList(filters)); + } return self(); } @Override public T webSessionManager(WebSessionManager sessionManager) { - Assert.notNull(sessionManager, "WebSessionManager must not be null."); this.sessionManager = sessionManager; return self(); } @@ -59,6 +71,7 @@ abstract class AbstractMockServerSpec> @Override public T apply(MockServerConfigurer configurer) { configurer.afterConfigureAdded(this); + this.configurers = (this.configurers != null ? this.configurers : new ArrayList<>(4)); this.configurers.add(configurer); return self(); } @@ -71,9 +84,15 @@ abstract class AbstractMockServerSpec> @Override public WebTestClient.Builder configureClient() { WebHttpHandlerBuilder builder = initHttpHandlerBuilder(); - builder.filters(theFilters -> theFilters.addAll(0, this.filters)); - builder.sessionManager(this.sessionManager); - this.configurers.forEach(configurer -> configurer.beforeServerCreated(builder)); + if (!CollectionUtils.isEmpty(this.filters)) { + builder.filters(theFilters -> theFilters.addAll(0, this.filters)); + } + if (this.sessionManager != null) { + builder.sessionManager(this.sessionManager); + } + if (!CollectionUtils.isEmpty(this.configurers)) { + this.configurers.forEach(configurer -> configurer.beforeServerCreated(builder)); + } return new DefaultWebTestClientBuilder(builder); }