|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.web.servlet;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Path; |
19 | 22 | import java.util.Collections;
|
20 | 23 | import java.util.Map;
|
21 | 24 | import java.util.function.Consumer;
|
|
25 | 28 | import javax.validation.constraints.NotEmpty;
|
26 | 29 |
|
27 | 30 | import org.junit.jupiter.api.Test;
|
| 31 | +import org.junit.jupiter.api.io.TempDir; |
28 | 32 | import reactor.core.publisher.Mono;
|
29 | 33 |
|
30 | 34 | import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
|
37 | 41 | import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
38 | 42 | import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
|
39 | 43 | import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
|
| 44 | +import org.springframework.boot.convert.ApplicationConversionService; |
| 45 | +import org.springframework.boot.env.ConfigTreePropertySource; |
40 | 46 | import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
41 | 47 | import org.springframework.boot.test.context.runner.ContextConsumer;
|
42 | 48 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
43 | 49 | import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
|
44 | 50 | import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
| 51 | +import org.springframework.context.ConfigurableApplicationContext; |
45 | 52 | import org.springframework.core.ParameterizedTypeReference;
|
| 53 | +import org.springframework.core.convert.support.ConfigurableConversionService; |
46 | 54 | import org.springframework.http.MediaType;
|
| 55 | +import org.springframework.util.FileCopyUtils; |
47 | 56 | import org.springframework.web.bind.annotation.GetMapping;
|
48 | 57 | import org.springframework.web.bind.annotation.PostMapping;
|
49 | 58 | import org.springframework.web.bind.annotation.RequestBody;
|
@@ -75,6 +84,9 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
|
75 | 84 | "management.endpoints.web.exposure.include=*", "server.error.include-exception=true",
|
76 | 85 | "server.error.include-message=always", "server.error.include-binding-errors=always");
|
77 | 86 |
|
| 87 | + @TempDir |
| 88 | + Path temp; |
| 89 | + |
78 | 90 | @Test // gh-17938
|
79 | 91 | void errorEndpointIsUsedWithEndpoint() {
|
80 | 92 | this.runner.run(withWebTestClient((client) -> {
|
@@ -135,6 +147,28 @@ void whenManagementServerBasePathIsConfiguredThenEndpointsAreBeneathThatPath() {
|
135 | 147 | }));
|
136 | 148 | }
|
137 | 149 |
|
| 150 | + @Test // gh-32941 |
| 151 | + void whenManagementServerPortLoadedFromConfigTree() { |
| 152 | + this.runner.withInitializer(this::addConfigTreePropertySource) |
| 153 | + .run((context) -> assertThat(context).hasNotFailed()); |
| 154 | + } |
| 155 | + |
| 156 | + private void addConfigTreePropertySource(ConfigurableApplicationContext applicationContext) { |
| 157 | + try { |
| 158 | + applicationContext.getEnvironment().setConversionService( |
| 159 | + (ConfigurableConversionService) ApplicationConversionService.getSharedInstance()); |
| 160 | + Path configtree = this.temp.resolve("configtree"); |
| 161 | + Path file = configtree.resolve("management/server/port"); |
| 162 | + file.toFile().getParentFile().mkdirs(); |
| 163 | + FileCopyUtils.copy("0".getBytes(StandardCharsets.UTF_8), file.toFile()); |
| 164 | + ConfigTreePropertySource source = new ConfigTreePropertySource("configtree", configtree); |
| 165 | + applicationContext.getEnvironment().getPropertySources().addFirst(source); |
| 166 | + } |
| 167 | + catch (IOException ex) { |
| 168 | + throw new IllegalStateException(ex); |
| 169 | + } |
| 170 | + } |
| 171 | + |
138 | 172 | private ContextConsumer<AssertableWebApplicationContext> withWebTestClient(Consumer<WebClient> webClient) {
|
139 | 173 | return (context) -> {
|
140 | 174 | String port = context.getEnvironment().getProperty("local.management.port");
|
|
0 commit comments