Merge branch '3.0.x' into 3.1.x

Closes gh-37966
This commit is contained in:
Phillip Webb
2023-10-19 21:03:33 -07:00
31 changed files with 162 additions and 151 deletions

View File

@@ -35,9 +35,9 @@ import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link SpringApplicationShutdownHook}.
@@ -186,8 +186,7 @@ class SpringApplicationShutdownHookTests {
ConfigurableApplicationContext context = new GenericApplicationContext();
shutdownHook.registerApplicationContext(context);
context.refresh();
assertThatThrownBy(() -> shutdownHook.deregisterFailedApplicationContext(context))
.isInstanceOf(IllegalStateException.class);
assertThatIllegalStateException().isThrownBy(() -> shutdownHook.deregisterFailedApplicationContext(context));
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
}
@@ -197,7 +196,7 @@ class SpringApplicationShutdownHookTests {
GenericApplicationContext context = new GenericApplicationContext();
shutdownHook.registerApplicationContext(context);
context.registerBean(FailingBean.class);
assertThatThrownBy(context::refresh).isInstanceOf(BeanCreationException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(context::refresh);
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
shutdownHook.deregisterFailedApplicationContext(context);
assertThat(shutdownHook.isApplicationContextRegistered(context)).isFalse();

View File

@@ -27,7 +27,8 @@ import org.springframework.boot.context.metrics.buffering.StartupTimeline.Timeli
import org.springframework.core.metrics.StartupStep;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link BufferingApplicationStartup}.
@@ -86,8 +87,8 @@ class BufferingApplicationStartupTests {
void startRecordingShouldFailIfEventsWereRecorded() {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
applicationStartup.start("first").end();
assertThatThrownBy(applicationStartup::startRecording).isInstanceOf(IllegalStateException.class)
.hasMessage("Cannot restart recording once steps have been buffered.");
assertThatIllegalStateException().isThrownBy(applicationStartup::startRecording)
.withMessage("Cannot restart recording once steps have been buffered.");
}
@Test
@@ -95,8 +96,8 @@ class BufferingApplicationStartupTests {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
StartupStep step = applicationStartup.start("first");
step.end();
assertThatThrownBy(() -> step.tag("name", "value")).isInstanceOf(IllegalStateException.class)
.hasMessage("StartupStep has already ended.");
assertThatIllegalStateException().isThrownBy(() -> step.tag("name", "value"))
.withMessage("StartupStep has already ended.");
}
@Test
@@ -104,7 +105,8 @@ class BufferingApplicationStartupTests {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
StartupStep step = applicationStartup.start("first");
step.tag("name", "value");
assertThatThrownBy(() -> step.getTags().iterator().remove()).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> step.getTags().iterator().remove());
}
@Test // gh-25792

View File

@@ -101,6 +101,7 @@ import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.entry;
@@ -709,11 +710,10 @@ class ConfigurationPropertiesTests {
@Test
void loadWhenHasConfigurationPropertiesValidatorShouldApplyValidator() {
assertThatExceptionOfType(Exception.class).isThrownBy(() -> load(WithCustomValidatorConfiguration.class))
.satisfies((ex) -> {
assertThat(ex).hasCauseInstanceOf(BindException.class);
assertThat(ex.getCause()).hasCauseExactlyInstanceOf(BindValidationException.class);
});
assertThatException().isThrownBy(() -> load(WithCustomValidatorConfiguration.class)).satisfies((ex) -> {
assertThat(ex).hasCauseInstanceOf(BindException.class);
assertThat(ex.getCause()).hasCauseExactlyInstanceOf(BindValidationException.class);
});
}
@Test
@@ -726,7 +726,7 @@ class ConfigurationPropertiesTests {
@Test
void loadWhenConfigurationPropertiesIsAlsoValidatorShouldApplyValidator() {
assertThatExceptionOfType(Exception.class).isThrownBy(() -> load(ValidatorProperties.class)).satisfies((ex) -> {
assertThatException().isThrownBy(() -> load(ValidatorProperties.class)).satisfies((ex) -> {
assertThat(ex).hasCauseInstanceOf(BindException.class);
assertThat(ex.getCause()).hasCauseExactlyInstanceOf(BindValidationException.class);
});
@@ -734,8 +734,7 @@ class ConfigurationPropertiesTests {
@Test
void loadWhenConstructorBoundConfigurationPropertiesIsAlsoValidatorShouldApplyValidator() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> load(ValidatorConstructorBoundPropertiesConfiguration.class))
assertThatException().isThrownBy(() -> load(ValidatorConstructorBoundPropertiesConfiguration.class))
.satisfies((ex) -> {
assertThat(ex).hasCauseInstanceOf(BindException.class);
assertThat(ex.getCause()).hasCauseExactlyInstanceOf(BindValidationException.class);
@@ -909,8 +908,7 @@ class ConfigurationPropertiesTests {
Map<String, Object> source = new HashMap<>();
source.put("test.duration", "P12D");
sources.addLast(new MapPropertySource("test", source));
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class))
assertThatException().isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class))
.havingCause()
.isInstanceOf(BindException.class);
}
@@ -921,8 +919,7 @@ class ConfigurationPropertiesTests {
Map<String, Object> source = new HashMap<>();
source.put("test.period", "P12D");
sources.addLast(new MapPropertySource("test", source));
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class))
assertThatException().isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class))
.havingCause()
.isInstanceOf(BindException.class);
}
@@ -938,8 +935,7 @@ class ConfigurationPropertiesTests {
@Test
void loadWhenBindingToConstructorParametersShouldValidate() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> load(ConstructorParameterValidationConfiguration.class))
assertThatException().isThrownBy(() -> load(ConstructorParameterValidationConfiguration.class))
.satisfies((ex) -> {
assertThat(ex).hasCauseInstanceOf(BindException.class);
assertThat(ex.getCause()).hasCauseExactlyInstanceOf(BindValidationException.class);
@@ -3062,7 +3058,7 @@ class ConfigurationPropertiesTests {
@Bean
@ConfigurationPropertiesBinding
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
return new Converter<ArrayList<?>, CustomList<?>>() {
return new Converter<>() {
@Override
public CustomList<?> convert(ArrayList<?> source) {
@@ -3096,7 +3092,7 @@ class ConfigurationPropertiesTests {
@Bean
@ConfigurationPropertiesBinding
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
return new Converter<ArrayList<?>, CustomList<?>>() {
return new Converter<>() {
@Override
public CustomList<?> convert(ArrayList<?> source) {

View File

@@ -28,7 +28,7 @@ import org.springframework.boot.web.server.PortInUseException;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for {@link FailureAnalyzers}.
@@ -40,7 +40,7 @@ class FailureAnalyzersIntegrationTests {
@Test
void analysisIsPerformed(CapturedOutput output) {
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(() -> new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).run());
assertThat(output).contains("APPLICATION FAILED TO START");
}

View File

@@ -25,7 +25,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests for {@link ValidationExceptionFailureAnalyzer}
@@ -37,8 +37,7 @@ class JakartaApiValidationExceptionFailureAnalyzerTests {
@Test
void validatedPropertiesTest() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> new AnnotationConfigApplicationContext(TestConfiguration.class).close())
assertThatException().isThrownBy(() -> new AnnotationConfigApplicationContext(TestConfiguration.class).close())
.satisfies((ex) -> assertThat(new ValidationExceptionFailureAnalyzer().analyze(ex)).isNotNull());
}

View File

@@ -34,7 +34,7 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.datasource.init.ScriptStatementFailedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link DataSourceScriptDatabaseInitializer}.
@@ -82,7 +82,7 @@ class DataSourceScriptDatabaseInitializerTests
populator.setContinueOnError(false);
}
};
assertThatThrownBy(initializer::initializeDatabase).isInstanceOf(ScriptStatementFailedException.class);
assertThatExceptionOfType(ScriptStatementFailedException.class).isThrownBy(initializer::initializeDatabase);
}
@Override

View File

@@ -30,7 +30,6 @@ import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link PemPrivateKeyParser}.
@@ -76,12 +75,12 @@ class PemPrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedTraditionalPkcs1(String file) {
assertThatThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs1/" + file)))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.cause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs1/" + file)))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@ParameterizedTest
@@ -119,12 +118,12 @@ class PemPrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedEcPkcs8(String file) {
assertThatThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file)))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.cause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file)))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@ParameterizedTest
@@ -190,12 +189,12 @@ class PemPrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedEcSec1(String file) {
assertThatThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/sec1/" + file)))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.cause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/sec1/" + file)))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@Test

View File

@@ -32,7 +32,7 @@ import org.springframework.context.support.StaticMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for {@link MessageSourceMessageInterpolator}.
@@ -85,8 +85,8 @@ class MessageSourceMessageInterpolatorIntegrationTests {
@Test
void recursion() {
assertThatThrownBy(() -> validate("recursion"))
.hasStackTraceContaining("Circular reference '{recursion -> middle -> recursion}'");
assertThatException().isThrownBy(() -> validate("recursion"))
.withStackTraceContaining("Circular reference '{recursion -> middle -> recursion}'");
}
@Test

View File

@@ -77,8 +77,8 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Base for testing classes that extends {@link AbstractReactiveWebServerFactory}.
@@ -211,7 +211,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
}
protected void assertThatSslWithInvalidAliasCallFails(ThrowingCallable call) {
assertThatThrownBy(call).hasStackTraceContaining("Keystore does not contain alias 'test-alias-404'");
assertThatException().isThrownBy(call)
.withStackTraceContaining("Keystore does not contain alias 'test-alias-404'");
}
protected ReactorClientHttpConnector buildTrustAllSslConnector() {

View File

@@ -24,7 +24,7 @@ import java.security.KeyStoreException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link SslConfigurationValidator}.
@@ -66,17 +66,17 @@ class SslConfigurationValidatorTests {
@Test
void validateKeyAliasWhenAliasNotFoundShouldThrowException() {
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Keystore does not contain alias '" + INVALID_ALIAS + "'");
assertThatIllegalStateException()
.isThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.withMessage("Keystore does not contain alias '" + INVALID_ALIAS + "'");
}
@Test
void validateKeyAliasWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException {
KeyStore uninitializedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Could not determine if keystore contains alias 'alias'");
assertThatIllegalStateException()
.isThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.withMessage("Could not determine if keystore contains alias 'alias'");
}
}

View File

@@ -27,6 +27,7 @@ import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -34,7 +35,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -204,14 +205,16 @@ abstract class AbstractFilterRegistrationBeanTests {
@Test
void failsWithDoubleRegistration() {
assertThatThrownBy(() -> {
AbstractFilterRegistrationBean<?> bean = createFilterRegistrationBean();
bean.setName("double-registration");
given(this.servletContext.addFilter(anyString(), any(Filter.class))).willReturn(null);
bean.onStartup(this.servletContext);
}).isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to register 'filter double-registration' on the servlet context. Possibly already registered?");
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
.withMessage("Failed to register 'filter double-registration' on the "
+ "servlet context. Possibly already registered?");
}
private void doubleRegistration() throws ServletException {
AbstractFilterRegistrationBean<?> bean = createFilterRegistrationBean();
bean.setName("double-registration");
given(this.servletContext.addFilter(anyString(), any(Filter.class))).willReturn(null);
bean.onStartup(this.servletContext);
}
@Test

View File

@@ -24,6 +24,7 @@ import java.util.Map;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -33,7 +34,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.web.servlet.mock.MockServlet;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@@ -68,14 +69,16 @@ class ServletRegistrationBeanTests {
@Test
void failsWithDoubleRegistration() {
assertThatThrownBy(() -> {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
bean.setName("double-registration");
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(null);
bean.onStartup(this.servletContext);
}).isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to register 'servlet double-registration' on the servlet context. Possibly already registered?");
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
.withMessage("Failed to register 'servlet double-registration' on "
+ "the servlet context. Possibly already registered?");
}
private void doubleRegistration() throws ServletException {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
bean.setName("double-registration");
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(null);
bean.onStartup(this.servletContext);
}
@Test

View File

@@ -155,11 +155,11 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -481,7 +481,8 @@ public abstract class AbstractServletWebServerFactoryTests {
}
protected void assertThatSslWithInvalidAliasCallFails(ThrowingCallable call) {
assertThatThrownBy(call).hasStackTraceContaining("Keystore does not contain alias 'test-alias-404'");
assertThatException().isThrownBy(call)
.withStackTraceContaining("Keystore does not contain alias 'test-alias-404'");
}
@Test