Provide a condition for detecting war deployments

Closes gh-19421
This commit is contained in:
Madhura Bhave
2020-04-09 08:50:45 -07:00
parent 9aae072872
commit 1342e4970a
19 changed files with 315 additions and 6 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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 com.autoconfig;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ConditionalOnWarDeployment
@Configuration
public class ExampleAutoConfiguration {
@Bean
public TestEndpoint testEndpoint() {
return new TestEndpoint();
}
@Endpoint(id = "war")
static class TestEndpoint {
@ReadOperation
String hello() {
return "{\"hello\":\"world\"}";
}
}
}

View File

@@ -146,6 +146,23 @@ class ApplicationBuilder {
srcMainWebapp.mkdirs();
FileCopyUtils.copy("webapp resource", new FileWriter(new File(srcMainWebapp, "webapp-resource.txt")));
}
copyAutoConfigurationFiles(appFolder);
return;
}
private void copyAutoConfigurationFiles(File appFolder) throws IOException {
File autoConfigPackage = new File(appFolder, "src/main/java/com/autoconfig");
autoConfigPackage.mkdirs();
FileCopyUtils.copy(new File("src/test/java/com/autoconfig/ExampleAutoConfiguration.java"),
new File(autoConfigPackage, "ExampleAutoConfiguration.java"));
File srcMainResources = new File(appFolder, "src/main/resources");
srcMainResources.mkdirs();
File metaInf = new File(srcMainResources, "META-INF");
metaInf.mkdirs();
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
new File(metaInf, "spring.factories"));
FileCopyUtils.copy(new File("src/test/resources/application.yml"),
new File(srcMainResources, "application.yml"));
}
private void packageApplication(File appFolder, File settingsXml) throws MavenInvocationException {

View File

@@ -79,4 +79,10 @@ class EmbeddedServletContainerJarPackagingIntegrationTests {
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
@TestTemplate
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
}

View File

@@ -102,6 +102,12 @@ class EmbeddedServletContainerWarPackagingIntegrationTests {
.noneMatch((resourcePath) -> resourcePath.startsWith("/org/springframework/boot/loader"));
}
@TestTemplate
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
private List<String> readLines(String input) {
if (input == null) {
return Collections.emptyList();