|
17 | 17 | package org.springframework.boot.autoconfigure.h2;
|
18 | 18 |
|
19 | 19 | import java.sql.Connection;
|
20 |
| -import java.sql.SQLException; |
21 | 20 |
|
22 | 21 | import javax.sql.DataSource;
|
23 | 22 |
|
24 |
| -import org.junit.jupiter.api.AfterEach; |
25 |
| -import org.junit.jupiter.api.BeforeEach; |
26 | 23 | import org.junit.jupiter.api.Test;
|
27 | 24 | import org.junit.jupiter.api.extension.ExtendWith;
|
28 | 25 |
|
29 | 26 | import org.springframework.beans.BeansException;
|
30 | 27 | import org.springframework.beans.factory.BeanCreationException;
|
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
31 | 29 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
| 30 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
32 | 31 | import org.springframework.boot.test.system.CapturedOutput;
|
33 | 32 | import org.springframework.boot.test.system.OutputCaptureExtension;
|
34 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
35 | 33 | import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
36 |
| -import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; |
37 |
| -import org.springframework.mock.web.MockServletContext; |
38 | 34 |
|
39 | 35 | import static org.assertj.core.api.Assertions.assertThat;
|
40 |
| -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
41 | 36 |
|
42 | 37 | /**
|
43 | 38 | * Tests for {@link H2ConsoleAutoConfiguration}
|
|
48 | 43 | */
|
49 | 44 | class H2ConsoleAutoConfigurationTests {
|
50 | 45 |
|
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)); |
64 | 48 |
|
65 | 49 | @Test
|
66 | 50 | 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)); |
70 | 52 | }
|
71 | 53 |
|
72 | 54 | @Test
|
73 | 55 | 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 | + }); |
82 | 64 | }
|
83 | 65 |
|
84 | 66 | @Test
|
85 | 67 | 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 | + }); |
90 | 74 | }
|
91 | 75 |
|
92 | 76 | @Test
|
93 | 77 | 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 | + }); |
101 | 84 | }
|
102 | 85 |
|
103 | 86 | @Test
|
104 | 87 | 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 | + }); |
111 | 94 | }
|
112 | 95 |
|
113 | 96 | @Test
|
114 | 97 | 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 | + }); |
124 | 106 | }
|
125 | 107 |
|
126 | 108 | @Test
|
127 | 109 | @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 | + }); |
135 | 118 | }
|
136 | 119 |
|
137 | 120 | }
|
0 commit comments