Polish
This commit is contained in:
parent
ac0f175c57
commit
9e2902130c
|
@ -17,27 +17,22 @@
|
|||
package org.springframework.boot.autoconfigure.h2;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
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.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
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.OutputCaptureExtension;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
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.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link H2ConsoleAutoConfiguration}
|
||||
|
@ -48,93 +43,80 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||
*/
|
||||
class H2ConsoleAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
|
||||
|
||||
@BeforeEach
|
||||
void setupContext() {
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(H2ConsoleAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void consoleIsDisabledByDefault() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).isEmpty();
|
||||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ServletRegistrationBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void propertyCanEnableConsole() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword");
|
||||
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
|
||||
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
|
||||
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
|
||||
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customPathMustBeginWithASlash() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:custom").applyTo(this.context);
|
||||
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh)
|
||||
.withMessageContaining("Failed to bind properties under 'spring.h2.console'");
|
||||
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=custom")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasFailed();
|
||||
assertThat(context.getStartupFailure()).isInstanceOf(BeanCreationException.class)
|
||||
.hasMessageContaining("Failed to bind properties under 'spring.h2.console'");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customPathWithTrailingSlash() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom/")
|
||||
.applyTo(this.context);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*");
|
||||
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom/")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
|
||||
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customPath() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.path:/custom").applyTo(this.context);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
ServletRegistrationBean<?> servletRegistrationBean = this.context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(servletRegistrationBean.getUrlMappings()).contains("/custom/*");
|
||||
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.path=/custom")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
|
||||
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/custom/*");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customInitParameters() {
|
||||
this.context.register(H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.settings.trace=true",
|
||||
"spring.h2.console.settings.webAllowOthers=true", "spring.h2.console.settings.webAdminPassword=abcd")
|
||||
.applyTo(this.context);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
|
||||
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("webAdminPassword", "abcd");
|
||||
this.contextRunner.withPropertyValues("spring.h2.console.enabled=true", "spring.h2.console.settings.trace=true",
|
||||
"spring.h2.console.settings.web-allow-others=true",
|
||||
"spring.h2.console.settings.web-admin-password=abcd").run((context) -> {
|
||||
assertThat(context).hasSingleBean(ServletRegistrationBean.class);
|
||||
ServletRegistrationBean<?> registrationBean = context.getBean(ServletRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
|
||||
assertThat(registrationBean.getInitParameters()).containsEntry("webAdminPassword", "abcd");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException, SQLException {
|
||||
this.context.register(DataSourceAutoConfiguration.class, H2ConsoleAutoConfiguration.class);
|
||||
TestPropertyValues.of("spring.h2.console.enabled:true").applyTo(this.context);
|
||||
this.context.refresh();
|
||||
try (Connection connection = this.context.getBean(DataSource.class).getConnection()) {
|
||||
assertThat(output).contains("Database available at '" + connection.getMetaData().getURL() + "'");
|
||||
}
|
||||
void dataSourceUrlIsLoggedWhenAvailable(CapturedOutput output) throws BeansException {
|
||||
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||
.withPropertyValues("spring.h2.console.enabled=true").run((context) -> {
|
||||
try (Connection connection = context.getBean(DataSource.class).getConnection()) {
|
||||
assertThat(output)
|
||||
.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");
|
||||
* 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 {
|
||||
|
||||
private H2ConsoleProperties properties;
|
||||
|
||||
@Test
|
||||
void pathMustNotBeEmpty() {
|
||||
this.properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath(""))
|
||||
H2ConsoleProperties properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath(""))
|
||||
.withMessageContaining("Path must have length greater than 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathMustHaveLengthGreaterThanOne() {
|
||||
this.properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath("/"))
|
||||
H2ConsoleProperties properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath("/"))
|
||||
.withMessageContaining("Path must have length greater than 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void customPathMustBeginWithASlash() {
|
||||
this.properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.properties.setPath("custom"))
|
||||
H2ConsoleProperties properties = new H2ConsoleProperties();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> properties.setPath("custom"))
|
||||
.withMessageContaining("Path must start with '/'");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue