This commit is contained in:
Phillip Webb
2019-07-14 20:32:49 +01:00
parent 01933f9b06
commit 0cb6a7f47d
5 changed files with 42 additions and 48 deletions

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.boot.actuate.autoconfigure.web.server;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -30,6 +29,7 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -51,16 +51,14 @@ class ManagementContextAutoConfigurationTests {
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
EndpointAutoConfiguration.class));
contextRunner.withPropertyValues("server.port=0", "management.server.port=0")
.run((context) -> assertThat(tomcatStartedOccurencesIn(output)).isEqualTo(2));
.run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2)));
}
private int tomcatStartedOccurencesIn(CharSequence output) {
int matches = 0;
Matcher matcher = Pattern.compile("Tomcat started on port").matcher(output);
while (matcher.find()) {
matches++;
}
return matches;
private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
return (charSequence) -> {
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);
assertThat(count).isEqualTo(expectedCount);
};
}
}