diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/MockReactiveWebServerFactory.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/MockReactiveWebServerFactory.java new file mode 100644 index 0000000000..6ba06c67b6 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/MockReactiveWebServerFactory.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.web.reactive; + +import java.util.Map; + +import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; +import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; +import org.springframework.boot.web.server.WebServer; +import org.springframework.boot.web.server.WebServerException; +import org.springframework.http.server.reactive.HttpHandler; + +import static org.mockito.Mockito.spy; + +/** + * Mock {@link ReactiveWebServerFactory}. + * + * @author Brian Clozel + */ +class MockReactiveWebServerFactory extends AbstractReactiveWebServerFactory { + + private MockReactiveWebServer webServer; + + @Override + public WebServer getWebServer(HttpHandler httpHandler) { + this.webServer = spy(new MockReactiveWebServer(httpHandler, getPort())); + return this.webServer; + } + + MockReactiveWebServer getWebServer() { + return this.webServer; + } + + static class MockReactiveWebServer implements WebServer { + + private final int port; + + private HttpHandler httpHandler; + + private Map httpHandlerMap; + + MockReactiveWebServer(HttpHandler httpHandler, int port) { + this.httpHandler = httpHandler; + this.port = port; + } + + MockReactiveWebServer(Map httpHandlerMap, int port) { + this.httpHandlerMap = httpHandlerMap; + this.port = port; + } + + HttpHandler getHttpHandler() { + return this.httpHandler; + } + + Map getHttpHandlerMap() { + return this.httpHandlerMap; + } + + @Override + public void start() throws WebServerException { + + } + + @Override + public void stop() throws WebServerException { + + } + + @Override + public int getPort() { + return this.port; + } + + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactoryTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactoryTests.java index a046568ba8..01c325c469 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactoryTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactoryTests.java @@ -60,7 +60,7 @@ class ReactiveManagementContextFactoryTests { @Bean ReactiveWebServerFactory reactiveWebServerFactory() { - return mock(ReactiveWebServerFactory.class); + return new MockReactiveWebServerFactory(); } @Bean diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java index 9bc7db28e4..01bb1da678 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java @@ -31,6 +31,7 @@ import org.springframework.context.ApplicationContextException; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -90,7 +91,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli String webServerFactoryBeanName = getWebServerFactoryBeanName(); ReactiveWebServerFactory webServerFactory = getWebServerFactory(webServerFactoryBeanName); boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit(); - this.serverManager = ServerManager.get(webServerFactory, lazyInit); + this.serverManager = new ServerManager(webServerFactory, lazyInit); } initPropertySources(); } @@ -124,8 +125,11 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli private WebServer startReactiveWebServer() { ServerManager serverManager = this.serverManager; - ServerManager.start(serverManager, this::getHttpHandler); - return ServerManager.getWebServer(serverManager); + if (serverManager != null) { + serverManager.start(this::getHttpHandler); + return serverManager.server; + } + return null; } /** @@ -166,11 +170,13 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli private void stopAndReleaseReactiveWebServer() { ServerManager serverManager = this.serverManager; - try { - ServerManager.stop(serverManager); - } - finally { - this.serverManager = null; + if (serverManager != null) { + try { + serverManager.server.stop(); + } + finally { + this.serverManager = null; + } } } @@ -181,7 +187,8 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli */ @Override public WebServer getWebServer() { - return ServerManager.getWebServer(this.serverManager); + ServerManager serverManager = this.serverManager; + return (serverManager != null) ? serverManager.server : null; } @Override @@ -225,6 +232,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli private volatile HttpHandler handler; private ServerManager(ReactiveWebServerFactory factory, boolean lazyInit) { + Assert.notNull(factory, "ReactiveWebServerFactory must not be null"); this.handler = this::handleUninitialized; this.server = factory.getWebServer(this); this.lazyInit = lazyInit; @@ -243,31 +251,10 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli return this.handler; } - static ServerManager get(ReactiveWebServerFactory factory, boolean lazyInit) { - return new ServerManager(factory, lazyInit); - } - - static WebServer getWebServer(ServerManager manager) { - return (manager != null) ? manager.server : null; - } - - static void start(ServerManager manager, Supplier handlerSupplier) { - if (manager != null && manager.server != null) { - manager.handler = manager.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier)) - : handlerSupplier.get(); - manager.server.start(); - } - } - - static void stop(ServerManager manager) { - if (manager != null && manager.server != null) { - try { - manager.server.stop(); - } - catch (Exception ex) { - throw new IllegalStateException(ex); - } - } + private void start(Supplier handlerSupplier) { + this.handler = this.lazyInit ? new LazyHttpHandler(Mono.fromSupplier(handlerSupplier)) + : handlerSupplier.get(); + this.server.start(); } }