Refactor actuator package locations
Restructure actuator packages to improve structure. The following changes have been made: - Separate actuator and actuator auto-configuration into different modules. - Move endpoint code into `spring-boot-actuator`. - Move `Endpoint` implementations from a single package into technology specific packages. - Move `HealthIndicator` implementations from a single package into technology specific packages. - As much as possible attempt to mirror the `spring-boot` package structure and class naming in `spring-boot-actuator` and `spring-boot-actuator-autoconfigure`. - Move `DataSourceBuilder` and DataSource meta-data support from `spring-boot-actuator` to `spring-boot`. Fixes gh-10261
This commit is contained in:
@@ -38,8 +38,8 @@ import org.mockito.InOrder;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.jdbc.SchemaManagement;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.context.HidePackagesClassLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourceBuilder}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataSourceBuilderTests {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
@After
|
||||
public void shutdownDataSource() throws IOException {
|
||||
if (this.dataSource instanceof Closeable) {
|
||||
((Closeable) this.dataSource).close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToHikari() {
|
||||
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource).isInstanceOf(HikariDataSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToTomcatIfHikariIsNotAvailable() {
|
||||
this.dataSource = DataSourceBuilder
|
||||
.create(new HidePackagesClassLoader("com.zaxxer.hikari"))
|
||||
.url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource)
|
||||
.isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultToCommonsDbcp2AsLastResort() {
|
||||
this.dataSource = DataSourceBuilder
|
||||
.create(new HidePackagesClassLoader("com.zaxxer.hikari",
|
||||
"org.apache.tomcat.jdbc.pool"))
|
||||
.url("jdbc:h2:test").build();
|
||||
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.jdbc.core.ConnectionCallback;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link DataSourcePoolMetadata} tests.
|
||||
*
|
||||
* @param <D> the data source pool metadata type
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public abstract class AbstractDataSourcePoolMetadataTests<D extends AbstractDataSourcePoolMetadata<?>> {
|
||||
|
||||
/**
|
||||
* Return a data source metadata instance with a min size of 0 and max size of 2.
|
||||
* @return the data source metadata
|
||||
*/
|
||||
protected abstract D getDataSourceMetadata();
|
||||
|
||||
@Test
|
||||
public void getMaxPoolSize() {
|
||||
assertThat(getDataSourceMetadata().getMax()).isEqualTo(Integer.valueOf(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMinPoolSize() {
|
||||
assertThat(getDataSourceMetadata().getMin()).isEqualTo(Integer.valueOf(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolSizeNoConnection() {
|
||||
// Make sure the pool is initialized
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(
|
||||
getDataSourceMetadata().getDataSource());
|
||||
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> null);
|
||||
assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(0));
|
||||
assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolSizeOneConnection() {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(
|
||||
getDataSourceMetadata().getDataSource());
|
||||
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> {
|
||||
assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(1));
|
||||
assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0.5F));
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolSizeTwoConnections() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(
|
||||
getDataSourceMetadata().getDataSource());
|
||||
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> {
|
||||
jdbcTemplate.execute((ConnectionCallback<Void>) connection1 -> {
|
||||
assertThat(getDataSourceMetadata().getActive()).isEqualTo(2);
|
||||
assertThat(getDataSourceMetadata().getUsage()).isEqualTo(1.0f);
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public abstract void getValidationQuery();
|
||||
|
||||
protected DataSourceBuilder initializeBuilder() {
|
||||
return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
||||
.url("jdbc:hsqldb:mem:test").username("sa");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link CommonsDbcp2DataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CommonsDbcp2DataSourcePoolMetadataTests
|
||||
extends AbstractDataSourcePoolMetadataTests<CommonsDbcp2DataSourcePoolMetadata> {
|
||||
|
||||
private CommonsDbcp2DataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.dataSourceMetadata = createDataSourceMetadata(0, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommonsDbcp2DataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithNoCurrent() {
|
||||
CommonsDbcp2DataSourcePoolMetadata dsm = new CommonsDbcp2DataSourcePoolMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getActive() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
assertThat(dsm.getUsage()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithNoMax() {
|
||||
CommonsDbcp2DataSourcePoolMetadata dsm = new CommonsDbcp2DataSourcePoolMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getMax() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
assertThat(dsm.getUsage()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithUnlimitedPool() {
|
||||
DataSourcePoolMetadata unlimitedDataSource = createDataSourceMetadata(0, -1);
|
||||
assertThat(unlimitedDataSource.getUsage()).isEqualTo(Float.valueOf(-1F));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getValidationQuery() {
|
||||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setValidationQuery("SELECT FROM FOO");
|
||||
assertThat(
|
||||
new CommonsDbcp2DataSourcePoolMetadata(dataSource).getValidationQuery())
|
||||
.isEqualTo("SELECT FROM FOO");
|
||||
}
|
||||
|
||||
private CommonsDbcp2DataSourcePoolMetadata createDataSourceMetadata(int minSize,
|
||||
int maxSize) {
|
||||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setMinIdle(minSize);
|
||||
dataSource.setMaxTotal(maxSize);
|
||||
return new CommonsDbcp2DataSourcePoolMetadata(dataSource);
|
||||
}
|
||||
|
||||
private BasicDataSource createDataSource() {
|
||||
return (BasicDataSource) initializeBuilder().type(BasicDataSource.class).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourcePoolMetadataProviders}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataSourcePoolMetadataProvidersTests {
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadataProvider firstProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadata first;
|
||||
|
||||
@Mock
|
||||
private DataSource firstDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadataProvider secondProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourcePoolMetadata second;
|
||||
|
||||
@Mock
|
||||
private DataSource secondDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSource unknownDataSource;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.firstDataSource))
|
||||
.willReturn(this.first);
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.secondDataSource))
|
||||
.willReturn(this.second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWithProviders() {
|
||||
DataSourcePoolMetadataProviders provider = new DataSourcePoolMetadataProviders(
|
||||
Arrays.asList(this.firstProvider, this.secondProvider));
|
||||
assertThat(provider.getDataSourcePoolMetadata(this.firstDataSource))
|
||||
.isSameAs(this.first);
|
||||
assertThat(provider.getDataSourcePoolMetadata(this.secondDataSource))
|
||||
.isSameAs(this.second);
|
||||
assertThat(provider.getDataSourcePoolMetadata(this.unknownDataSource)).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link HikariDataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HikariDataSourcePoolMetadataTests
|
||||
extends AbstractDataSourcePoolMetadataTests<HikariDataSourcePoolMetadata> {
|
||||
|
||||
private HikariDataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.dataSourceMetadata = new HikariDataSourcePoolMetadata(
|
||||
createDataSource(0, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HikariDataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getValidationQuery() {
|
||||
HikariDataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setConnectionTestQuery("SELECT FROM FOO");
|
||||
assertThat(new HikariDataSourcePoolMetadata(dataSource).getValidationQuery())
|
||||
.isEqualTo("SELECT FROM FOO");
|
||||
}
|
||||
|
||||
private HikariDataSource createDataSource(int minSize, int maxSize) {
|
||||
HikariDataSource dataSource = (HikariDataSource) initializeBuilder()
|
||||
.type(HikariDataSource.class).build();
|
||||
dataSource.setMinimumIdle(minSize);
|
||||
dataSource.setMaximumPoolSize(maxSize);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc.metadata;
|
||||
|
||||
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TomcatDataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class TomcatDataSourcePoolMetadataTests
|
||||
extends AbstractDataSourcePoolMetadataTests<TomcatDataSourcePoolMetadata> {
|
||||
|
||||
private TomcatDataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.dataSourceMetadata = new TomcatDataSourcePoolMetadata(
|
||||
createDataSource(0, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TomcatDataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getValidationQuery() {
|
||||
DataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setValidationQuery("SELECT FROM FOO");
|
||||
assertThat(new TomcatDataSourcePoolMetadata(dataSource).getValidationQuery())
|
||||
.isEqualTo("SELECT FROM FOO");
|
||||
}
|
||||
|
||||
private DataSource createDataSource(int minSize, int maxSize) {
|
||||
DataSource dataSource = (DataSource) initializeBuilder().type(DataSource.class)
|
||||
.build();
|
||||
dataSource.setMinIdle(minSize);
|
||||
dataSource.setMaxActive(maxSize);
|
||||
|
||||
// Avoid warnings
|
||||
dataSource.setInitialSize(minSize);
|
||||
dataSource.setMaxIdle(maxSize);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,7 +36,7 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -32,8 +32,8 @@ import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.liquibase.CommonsLoggingLiquibaseLogger;
|
||||
import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web.servlet.error;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.MapBindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultErrorAttributes}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
public class DefaultErrorAttributesTests {
|
||||
|
||||
private DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes();
|
||||
|
||||
private MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
private WebRequest webRequest = new ServletWebRequest(this.request);
|
||||
|
||||
@Test
|
||||
public void includeTimeStamp() throws Exception {
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("timestamp")).isInstanceOf(Date.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void specificStatusCode() throws Exception {
|
||||
this.request.setAttribute("javax.servlet.error.status_code", 404);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("error"))
|
||||
.isEqualTo(HttpStatus.NOT_FOUND.getReasonPhrase());
|
||||
assertThat(attributes.get("status")).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingStatusCode() throws Exception {
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("error")).isEqualTo("None");
|
||||
assertThat(attributes.get("status")).isEqualTo(999);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mvcError() throws Exception {
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
ModelAndView modelAndView = this.errorAttributes.resolveException(this.request,
|
||||
null, null, ex);
|
||||
this.request.setAttribute("javax.servlet.error.exception",
|
||||
new RuntimeException("Ignored"));
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
|
||||
assertThat(modelAndView).isNull();
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletError() throws Exception {
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletMessage() throws Exception {
|
||||
this.request.setAttribute("javax.servlet.error.message", "Test");
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullMessage() throws Exception {
|
||||
this.request.setAttribute("javax.servlet.error.exception",
|
||||
new RuntimeException());
|
||||
this.request.setAttribute("javax.servlet.error.message", "Test");
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unwrapServletException() throws Exception {
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
ServletException wrapped = new ServletException(new ServletException(ex));
|
||||
this.request.setAttribute("javax.servlet.error.exception", wrapped);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(wrapped);
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getError() throws Exception {
|
||||
Error error = new OutOfMemoryError("Test error");
|
||||
this.request.setAttribute("javax.servlet.error.exception", error);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(error);
|
||||
assertThat(attributes.get("exception")).isNull();
|
||||
assertThat(attributes.get("message")).isEqualTo("Test error");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extractBindingResultErrors() throws Exception {
|
||||
BindingResult bindingResult = new MapBindingResult(
|
||||
Collections.singletonMap("a", "b"), "objectName");
|
||||
bindingResult.addError(new ObjectError("c", "d"));
|
||||
Exception ex = new BindException(bindingResult);
|
||||
testBindingResult(bindingResult, ex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extractMethodArgumentNotValidExceptionBindingResultErrors()
|
||||
throws Exception {
|
||||
BindingResult bindingResult = new MapBindingResult(
|
||||
Collections.singletonMap("a", "b"), "objectName");
|
||||
bindingResult.addError(new ObjectError("c", "d"));
|
||||
Exception ex = new MethodArgumentNotValidException(null, bindingResult);
|
||||
testBindingResult(bindingResult, ex);
|
||||
}
|
||||
|
||||
private void testBindingResult(BindingResult bindingResult, Exception ex) {
|
||||
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("message"))
|
||||
.isEqualTo("Validation failed for object='objectName'. Error count: 1");
|
||||
assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withExceptionAttribute() throws Exception {
|
||||
DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(true);
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||
Map<String, Object> attributes = errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("exception"))
|
||||
.isEqualTo(RuntimeException.class.getName());
|
||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trace() throws Exception {
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, true);
|
||||
assertThat(attributes.get("trace").toString()).startsWith("java.lang");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noTrace() throws Exception {
|
||||
RuntimeException ex = new RuntimeException("Test");
|
||||
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("trace")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void path() throws Exception {
|
||||
this.request.setAttribute("javax.servlet.error.request_uri", "path");
|
||||
Map<String, Object> attributes = this.errorAttributes
|
||||
.getErrorAttributes(this.webRequest, false);
|
||||
assertThat(attributes.get("path")).isEqualTo("path");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user