Skip to content

Commit 3118ca9

Browse files
committed
Polish
1 parent 0cdb1b3 commit 3118ca9

File tree

1 file changed

+47
-64
lines changed

1 file changed

+47
-64
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@
1717
package org.springframework.boot.autoconfigure.h2;
1818

1919
import java.sql.Connection;
20-
import java.sql.SQLException;
2120

2221
import javax.sql.DataSource;
2322

24-
import org.junit.jupiter.api.AfterEach;
25-
import org.junit.jupiter.api.BeforeEach;
2623
import org.junit.jupiter.api.Test;
2724
import org.junit.jupiter.api.extension.ExtendWith;
2825

2926
import org.springframework.beans.BeansException;
3027
import org.springframework.beans.factory.BeanCreationException;
28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
3129
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
30+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3231
import org.springframework.boot.test.system.CapturedOutput;
3332
import org.springframework.boot.test.system.OutputCaptureExtension;
34-
import org.springframework.boot.test.util.TestPropertyValues;
3533
import org.springframework.boot.web.servlet.ServletRegistrationBean;
36-
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
37-
import org.springframework.mock.web.MockServletContext;
3834

3935
import static org.assertj.core.api.Assertions.assertThat;
40-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4136

4237
/**
4338
* Tests for {@link H2ConsoleAutoConfiguration}
@@ -48,90 +43,78 @@
4843
*/
4944
class H2ConsoleAutoConfigurationTests {
5045

51-
private AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
52-
53-
@BeforeEach
54-
void setupContext() {
55-
this.context.setServletContext(new MockServletContext());
56-
}
57-
58-
@AfterEach
59-
void close() {
60-
if (this.context != null) {
61-
this.context.close();
62-
}
63-
}
46+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
47+
.withConfiguration(AutoConfigurations.of(H2ConsoleAutoConfiguration.class));
6448

6549
@Test
6650
void consoleIsDisabledByDefault() {
67-
this.context.register(H2ConsoleAutoConfiguration.class);
68-
this.context.refresh();
69-
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).isEmpty();
51+
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ServletRegistrationBean.class));
7052
}
7153

7254
@Test
7355
void propertyCanEnableConsole() {
74-
this.context.register(H2ConsoleAutoConfiguration.class);
75-
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
76-
this.context.refresh();
77-
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
78-
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
79-
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
80-
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
81-
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
56+
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
57+
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
58+
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
59+
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
60+
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
61+
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
62+
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword");
63+
});
8264
}
8365

8466
@Test
8567
void customPathMustBeginWithASlash() {
86-
this.context.register(H2ConsoleAutoConfiguration.class);
87-
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:custom").applyTo(this.context);
88-
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
89-
.withMessageContaining("Failed to bind properties under 'spring.h2.console'");
68+
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=custom")
69+
.run((context) -> {
70+
assertThat(context).hasFailed();
71+
assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class)
72+
.hasMessageContaining("Failed to bind properties under 'spring.h2.console'");
73+
});
9074
}
9175

9276
@Test
9377
void customPathWithTrailingSlash() {
94-
this.context.register(H2ConsoleAutoConfiguration.class);
95-
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom/")
96-
.applyTo(this.context);
97-
this.context.refresh();
98-
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
99-
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class);
100-
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*");
78+
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom/")
79+
.run((context) -> {
80+
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
81+
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
82+
assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
83+
});
10184
}
10285

10386
@Test
10487
void customPath() {
105-
this.context.register(H2ConsoleAutoConfiguration.class);
106-
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom").applyTo(this.context);
107-
this.context.refresh();
108-
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
109-
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class);
110-
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*");
88+
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom")
89+
.run((context) -> {
90+
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
91+
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
92+
assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
93+
});
11194
}
11295

11396
@Test
11497
void customInitParameters() {
115-
this.context.register(H2ConsoleAutoConfiguration.class);
116-
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.settings.trace=true",
117-
"spring.h2.console.settings.webAllowOthers=true").applyTo(this.context);
118-
this.context.refresh();
119-
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
120-
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
121-
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
122-
assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
123-
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
98+
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.settings.trace=true",
99+
"spring.h2.console.settings.web-allow-others=true").run((context) -> {
100+
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
101+
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
102+
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
103+
assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
104+
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
105+
});
124106
}
125107

126108
@Test
127109
@ExtendWith(OutputCaptureExtension.class)
128-
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException, SQLException {
129-
this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class);
130-
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
131-
this.context.refresh();
132-
try (Connection connection = this.context.getBean(DataSource.class).getConnection()) {
133-
assertThat(output).contains("Database available at '" + connection.getMetaData().getURL() + "'");
134-
}
110+
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException {
111+
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
112+
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
113+
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
114+
assertThat(output)
115+
.contains("Database available at '" + connection.getMetaData().getURL() + "'");
116+
}
117+
});
135118
}
136119

137120
}

0 commit comments

Comments
 (0)