pull/21936/head
Stephane Nicoll 4 years ago
parent ac0f175c57
commit 9e2902130c

@ -17,27 +17,22 @@
package org.springframework.boot.autoconfigure.h2; package org.springframework.boot.autoconfigure.h2;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.mock.web.MockServletContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/** /**
* Tests for {@link H2ConsoleAutoConfiguration} * Tests for {@link H2ConsoleAutoConfiguration}
@ -48,93 +43,80 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/ */
class H2ConsoleAutoConfigurationTests { class H2ConsoleAutoConfigurationTests {
private AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext(); private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(H2ConsoleAutoConfiguration.class));
@BeforeEach
void setupContext() {
this.context.setServletContext(new MockServletContext());
}
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
void consoleIsDisabledByDefault() { void consoleIsDisabledByDefault() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ServletRegistrationBean.class));
this.context.refresh();
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).isEmpty();
} }
@Test @Test
void propertyCanEnableConsole() { void propertyCanEnableConsole() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context); assertThat(context).hasSingleBean(ServletRegistrationBean.class);
this.context.refresh(); ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*"); assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace"); assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers"); assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword"); assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword");
});
} }
@Test @Test
void customPathMustBeginWithASlash() { void customPathMustBeginWithASlash() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=custom")
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:custom").applyTo(this.context); .run((context) -> {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh) assertThat(context).hasFailed();
.withMessageContaining("Failed to bind properties under 'spring.h2.console'"); assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class)
.hasMessageContaining("Failed to bind properties under 'spring.h2.console'");
});
} }
@Test @Test
void customPathWithTrailingSlash() { void customPathWithTrailingSlash() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom/")
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom/") .run((context) -> {
.applyTo(this.context); assertThat(context).hasSingleBean(ServletRegistrationBean.class);
this.context.refresh(); ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class); });
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*");
} }
@Test @Test
void customPath() { void customPath() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom")
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom").applyTo(this.context); .run((context) -> {
this.context.refresh(); assertThat(context).hasSingleBean(ServletRegistrationBean.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class); assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*"); });
} }
@Test @Test
void customInitParameters() { void customInitParameters() {
this.context.register(H2ConsoleAutoConfiguration.class); this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.settings.trace=true",
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.settings.trace=true", "spring.h2.console.settings.web-allow-others=true",
"spring.h2.console.settings.webAllowOthers=true", "spring.h2.console.settings.webAdminPassword=abcd") "spring.h2.console.settings.web-admin-password=abcd").run((context) -> {
.applyTo(this.context); assertThat(context).hasSingleBean(ServletRegistrationBean.class);
this.context.refresh(); ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*"); assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
assertThat(registrationBean.getInitParameters()).containsEntry("trace", ""); assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", ""); assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
assertThat(registrationBean.getInitParameters()).containsEntry("webAdminPassword", "abcd"); assertThat(registrationBean.getInitParameters()).containsEntry("webAdminPassword", "abcd");
});
} }
@Test @Test
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException, SQLException { void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException {
this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class); this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context); .withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
this.context.refresh(); try (Connection connection = context.getBean(DataSource.class).getConnection()) {
try (Connection connection = this.context.getBean(DataSource.class).getConnection()) { assertThat(output)
assertThat(output).contains("Database available at '" + connection.getMetaData().getURL() + "'"); .contains("Database available at '" + connection.getMetaData().getURL() + "'");
} }
});
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,26 +27,24 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/ */
class H2ConsolePropertiesTests { class H2ConsolePropertiesTests {
private H2ConsoleProperties properties;
@Test @Test
void pathMustNotBeEmpty() { void pathMustNotBeEmpty() {
this.properties = new H2ConsoleProperties(); H2ConsoleProperties properties = new H2ConsoleProperties();
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath("")) assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath(""))
.withMessageContaining("Path must have length greater than 1"); .withMessageContaining("Path must have length greater than 1");
} }
@Test @Test
void pathMustHaveLengthGreaterThanOne() { void pathMustHaveLengthGreaterThanOne() {
this.properties = new H2ConsoleProperties(); H2ConsoleProperties properties = new H2ConsoleProperties();
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath("/")) assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath("/"))
.withMessageContaining("Path must have length greater than 1"); .withMessageContaining("Path must have length greater than 1");
} }
@Test @Test
void customPathMustBeginWithASlash() { void customPathMustBeginWithASlash() {
this.properties = new H2ConsoleProperties(); H2ConsoleProperties properties = new H2ConsoleProperties();
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath("custom")) assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath("custom"))
.withMessageContaining("Path must start with '/'"); .withMessageContaining("Path must start with '/'");
} }

Loading…
Cancel
Save