Apply eclipse formatting rules to 3dc932db

pull/1487/head
Phillip Webb 10 years ago
parent 53c4859a6a
commit adbded33ff

@ -30,8 +30,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} that provides * {@link EnableAutoConfiguration Auto-configuration} that provides metrics on dataSource
* metrics on dataSource usage. * usage.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0

@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -33,8 +34,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
/** /**
* A {@link PublicMetrics} implementation that provides data source usage * A {@link PublicMetrics} implementation that provides data source usage statistics.
* statistics.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
@ -49,21 +49,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
@Autowired @Autowired
private Collection<DataSourceMetadataProvider> dataSourceMetadataProviders; private Collection<DataSourceMetadataProvider> dataSourceMetadataProviders;
private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix private final Map<String, DataSourceMetadata> dataSourceMetadataByPrefix = new HashMap<String, DataSourceMetadata>();
= new HashMap<String, DataSourceMetadata>();
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
Map<String, DataSource> dataSources = this.applicationContext.getBeansOfType(DataSource.class); Map<String, DataSource> dataSources = this.applicationContext
.getBeansOfType(DataSource.class);
DataSource primaryDataSource = getPrimaryDataSource(); DataSource primaryDataSource = getPrimaryDataSource();
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(this.dataSourceMetadataProviders); this.dataSourceMetadataProviders);
for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) { for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
String prefix = createPrefix(entry.getKey(), entry.getValue(), entry.getValue().equals(primaryDataSource)); String prefix = createPrefix(entry.getKey(), entry.getValue(), entry
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry.getValue()); .getValue().equals(primaryDataSource));
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(entry
.getValue());
if (dataSourceMetadata != null) { if (dataSourceMetadata != null) {
dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata); this.dataSourceMetadataByPrefix.put(prefix, dataSourceMetadata);
} }
} }
} }
@ -71,7 +73,8 @@ public class DataSourcePublicMetrics implements PublicMetrics {
@Override @Override
public Collection<Metric<?>> metrics() { public Collection<Metric<?>> metrics() {
Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>(); Collection<Metric<?>> result = new LinkedHashSet<Metric<?>>();
for (Map.Entry<String, DataSourceMetadata> entry : dataSourceMetadataByPrefix.entrySet()) { for (Map.Entry<String, DataSourceMetadata> entry : this.dataSourceMetadataByPrefix
.entrySet()) {
String prefix = entry.getKey(); String prefix = entry.getKey();
// Make sure the prefix ends with a dot // Make sure the prefix ends with a dot
if (!prefix.endsWith(".")) { if (!prefix.endsWith(".")) {
@ -91,19 +94,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
} }
/** /**
* Create the prefix to use for the metrics to associate with the given {@link DataSource}. * Create the prefix to use for the metrics to associate with the given
* {@link DataSource}.
* @param dataSourceName the name of the data source bean * @param dataSourceName the name of the data source bean
* @param dataSource the data source to configure * @param dataSource the data source to configure
* @param primary if this data source is the primary data source * @param primary if this data source is the primary data source
* @return a prefix for the given data source * @return a prefix for the given data source
*/ */
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) { protected String createPrefix(String dataSourceName, DataSource dataSource,
boolean primary) {
StringBuilder sb = new StringBuilder("datasource."); StringBuilder sb = new StringBuilder("datasource.");
if (primary) { if (primary) {
sb.append("primary"); sb.append("primary");
} }
else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of the name else if (endWithDataSource(dataSourceName)) { // Strip the data source part out of
sb.append(dataSourceName.substring(0, dataSourceName.length() - DATASOURCE_SUFFIX.length())); // the name
sb.append(dataSourceName.substring(0, dataSourceName.length()
- DATASOURCE_SUFFIX.length()));
} }
else { else {
sb.append(dataSourceName); sb.append(dataSourceName);
@ -131,7 +138,7 @@ public class DataSourcePublicMetrics implements PublicMetrics {
*/ */
private DataSource getPrimaryDataSource() { private DataSource getPrimaryDataSource() {
try { try {
return applicationContext.getBean(DataSource.class); return this.applicationContext.getBean(DataSource.class);
} }
catch (NoSuchBeanDefinitionException e) { catch (NoSuchBeanDefinitionException e) {
return null; return null;

@ -16,8 +16,6 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import static org.junit.Assert.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.Collection;
@ -26,11 +24,9 @@ import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics; import org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics;
import org.springframework.boot.actuate.endpoint.PublicMetrics; import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
@ -44,8 +40,12 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import com.zaxxer.hikari.HikariDataSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class MetricDataSourceAutoConfigurationTests { public class MetricDataSourceAutoConfigurationTests {
@ -78,24 +78,25 @@ public class MetricDataSourceAutoConfigurationTests {
load(MultipleDataSourcesConfig.class); load(MultipleDataSourcesConfig.class);
PublicMetrics bean = this.context.getBean(PublicMetrics.class); PublicMetrics bean = this.context.getBean(PublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics(); Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage",
"datasource.tomcat.active", "datasource.tomcat.usage",
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
// Hikari won't work unless a first connection has been retrieved // Hikari won't work unless a first connection has been retrieved
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean("hikariDS", DataSource.class)); JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS",
DataSource.class));
jdbcTemplate.execute(new ConnectionCallback<Void>() { jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException { public Void doInConnection(Connection connection) throws SQLException,
DataAccessException {
return null; return null;
} }
}); });
Collection<Metric<?>> anotherMetrics = bean.metrics(); Collection<Metric<?>> anotherMetrics = bean.metrics();
assertMetrics(anotherMetrics, assertMetrics(anotherMetrics, "datasource.tomcat.active",
"datasource.tomcat.active", "datasource.tomcat.usage", "datasource.tomcat.usage", "datasource.hikariDS.active",
"datasource.hikariDS.active", "datasource.hikariDS.usage", "datasource.hikariDS.usage", "datasource.commonsDbcp.active",
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); "datasource.commonsDbcp.usage");
} }
@Test @Test
@ -103,19 +104,18 @@ public class MetricDataSourceAutoConfigurationTests {
load(MultipleDataSourcesWithPrimaryConfig.class); load(MultipleDataSourcesWithPrimaryConfig.class);
PublicMetrics bean = this.context.getBean(PublicMetrics.class); PublicMetrics bean = this.context.getBean(PublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics(); Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
"datasource.primary.active", "datasource.primary.usage",
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
} }
@Test @Test
public void customPrefix() { public void customPrefix() {
load(MultipleDataSourcesWithPrimaryConfig.class, CustomDataSourcePublicMetrics.class); load(MultipleDataSourcesWithPrimaryConfig.class,
CustomDataSourcePublicMetrics.class);
PublicMetrics bean = this.context.getBean(PublicMetrics.class); PublicMetrics bean = this.context.getBean(PublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics(); Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, assertMetrics(metrics, "ds.first.active", "ds.first.usage", "ds.second.active",
"ds.first.active", "ds.first.usage", "ds.second.usage");
"ds.second.active", "ds.second.usage");
} }
@ -138,13 +138,13 @@ public class MetricDataSourceAutoConfigurationTests {
this.context.refresh(); this.context.refresh();
} }
@Configuration @Configuration
static class MultipleDataSourcesConfig { static class MultipleDataSourcesConfig {
@Bean @Bean
public DataSource tomcatDataSource() { public DataSource tomcatDataSource() {
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build(); return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
.build();
} }
@Bean @Bean
@ -164,7 +164,8 @@ public class MetricDataSourceAutoConfigurationTests {
@Bean @Bean
@Primary @Primary
public DataSource myDataSource() { public DataSource myDataSource() {
return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class).build(); return initializeBuilder().type(org.apache.tomcat.jdbc.pool.DataSource.class)
.build();
} }
@Bean @Bean
@ -180,7 +181,8 @@ public class MetricDataSourceAutoConfigurationTests {
public DataSourcePublicMetrics myDataSourcePublicMetrics() { public DataSourcePublicMetrics myDataSourcePublicMetrics() {
return new DataSourcePublicMetrics() { return new DataSourcePublicMetrics() {
@Override @Override
protected String createPrefix(String dataSourceName, DataSource dataSource, boolean primary) { protected String createPrefix(String dataSourceName,
DataSource dataSource, boolean primary) {
return (primary ? "ds.first." : "ds.second"); return (primary ? "ds.first." : "ds.second");
} }
}; };
@ -188,9 +190,7 @@ public class MetricDataSourceAutoConfigurationTests {
} }
private static DataSourceBuilder initializeBuilder() { private static DataSourceBuilder initializeBuilder() {
return DataSourceBuilder.create() return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
.driverClassName("org.hsqldb.jdbc.JDBCDriver") .url("jdbc:hsqldb:mem:test").username("sa");
.url("jdbc:hsqldb:mem:test")
.username("sa");
} }
} }

@ -24,7 +24,8 @@ import javax.sql.DataSource;
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public abstract class AbstractDataSourceMetadata<D extends DataSource> implements DataSourceMetadata { public abstract class AbstractDataSourceMetadata<D extends DataSource> implements
DataSourceMetadata {
private final D dataSource; private final D dataSource;
@ -55,7 +56,7 @@ public abstract class AbstractDataSourceMetadata<D extends DataSource> implement
} }
protected final D getDataSource() { protected final D getDataSource() {
return dataSource; return this.dataSource;
} }
} }

@ -19,13 +19,13 @@ package org.springframework.boot.autoconfigure.jdbc;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
/** /**
* A {@link DataSourceMetadata} implementation for the commons dbcp * A {@link DataSourceMetadata} implementation for the commons dbcp data source.
* data source.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public class CommonsDbcpDataSourceMetadata extends AbstractDataSourceMetadata<BasicDataSource> { public class CommonsDbcpDataSourceMetadata extends
AbstractDataSourceMetadata<BasicDataSource> {
public CommonsDbcpDataSourceMetadata(BasicDataSource dataSource) { public CommonsDbcpDataSourceMetadata(BasicDataSource dataSource) {
super(dataSource); super(dataSource);
@ -50,4 +50,5 @@ public class CommonsDbcpDataSourceMetadata extends AbstractDataSourceMetadata<Ba
public String getValidationQuery() { public String getValidationQuery() {
return getDataSource().getValidationQuery(); return getDataSource().getValidationQuery();
} }
} }

@ -35,7 +35,8 @@ public class CompositeDataSourceMetadataProvider implements DataSourceMetadataPr
/** /**
* Create an instance with an initial collection of delegates to use. * Create an instance with an initial collection of delegates to use.
*/ */
public CompositeDataSourceMetadataProvider(Collection<DataSourceMetadataProvider> providers) { public CompositeDataSourceMetadataProvider(
Collection<DataSourceMetadataProvider> providers) {
this.providers = providers; this.providers = providers;
} }
@ -48,8 +49,9 @@ public class CompositeDataSourceMetadataProvider implements DataSourceMetadataPr
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
for (DataSourceMetadataProvider provider : providers) { for (DataSourceMetadataProvider provider : this.providers) {
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(dataSource); DataSourceMetadata dataSourceMetadata = provider
.getDataSourceMetadata(dataSource);
if (dataSourceMetadata != null) { if (dataSourceMetadata != null) {
return dataSourceMetadata; return dataSourceMetadata;
} }

@ -19,9 +19,8 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* Provide various metadata regarding a {@link DataSource} that * Provide various metadata regarding a {@link DataSource} that are shared by most data
* are shared by most data source types but not accessible in a * source types but not accessible in a standard manner.
* standard manner.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
@ -29,44 +28,39 @@ import javax.sql.DataSource;
public interface DataSourceMetadata { public interface DataSourceMetadata {
/** /**
* Return the usage of the pool as a double value between * Return the usage of the pool as a double value between 0 and 1.
* 0 and 1.
* <ul> * <ul>
* <li>1 means that the maximum number of connections * <li>1 means that the maximum number of connections have been allocated</li>
* have been allocated</li>
* <li>0 means that no connection is currently active</li> * <li>0 means that no connection is currently active</li>
* <li>-1 means there is not limit to the number of connections * <li>-1 means there is not limit to the number of connections that can be allocated</li>
* that can be allocated</li>
* </ul> * </ul>
* This may also return {@code null} if the data source does * This may also return {@code null} if the data source does not provide the necessary
* not provide the necessary information to compute the poll usage. * information to compute the poll usage.
*/ */
Float getPoolUsage(); Float getPoolUsage();
/** /**
* Return the current number of active connections that * Return the current number of active connections that have been allocated from the
* have been allocated from the data source or {@code null} * data source or {@code null} if that information is not available.
* if that information is not available.
*/ */
Integer getPoolSize(); Integer getPoolSize();
/** /**
* Return the maximum number of active connections that can be * Return the maximum number of active connections that can be allocated at the same
* allocated at the same time or {@code -1} if there is no * time or {@code -1} if there is no limit. Can also return {@code null} if that
* limit. Can also return {@code null} if that information is * information is not available.
* not available.
*/ */
Integer getMaxPoolSize(); Integer getMaxPoolSize();
/** /**
* Return the minimum number of idle connections in the pool * Return the minimum number of idle connections in the pool or {@code null} if that
* or {@code null} if that information is not available. * information is not available.
*/ */
Integer getMinPoolSize(); Integer getMinPoolSize();
/** /**
* Return the query to use to validate that a connection is * Return the query to use to validate that a connection is valid or {@code null} if
* valid or {@code null} if that information is not available. * that information is not available.
*/ */
String getValidationQuery(); String getValidationQuery();

@ -27,9 +27,8 @@ import javax.sql.DataSource;
public interface DataSourceMetadataProvider { public interface DataSourceMetadataProvider {
/** /**
* Return the {@link DataSourceMetadata} instance able to manage the * Return the {@link DataSourceMetadata} instance able to manage the specified
* specified {@link DataSource} or {@code null} if the given data * {@link DataSource} or {@code null} if the given data source could not be handled.
* source could not be handled.
*/ */
DataSourceMetadata getDataSourceMetadata(DataSource dataSource); DataSourceMetadata getDataSourceMetadata(DataSource dataSource);

@ -18,16 +18,16 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.zaxxer.hikari.HikariDataSource;
/** /**
* Register the {@link DataSourceMetadataProvider} instances for the supported * Register the {@link DataSourceMetadataProvider} instances for the supported data
* data sources. * sources.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
@ -45,12 +45,14 @@ public class DataSourceMetadataProvidersConfiguration {
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) { if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
return new TomcatDataSourceMetadata((org.apache.tomcat.jdbc.pool.DataSource) dataSource); return new TomcatDataSourceMetadata(
(org.apache.tomcat.jdbc.pool.DataSource) dataSource);
} }
return null; return null;
} }
}; };
} }
} }
@Configuration @Configuration
@ -69,6 +71,7 @@ public class DataSourceMetadataProvidersConfiguration {
} }
}; };
} }
} }
@Configuration @Configuration
@ -81,12 +84,14 @@ public class DataSourceMetadataProvidersConfiguration {
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
if (dataSource instanceof BasicDataSource) { if (dataSource instanceof BasicDataSource) {
return new CommonsDbcpDataSourceMetadata((BasicDataSource) dataSource); return new CommonsDbcpDataSourceMetadata(
(BasicDataSource) dataSource);
} }
return null; return null;
} }
}; };
} }
} }
} }

@ -16,21 +16,20 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;
/** /**
* A {@link DataSourceMetadata} implementation for the hikari * A {@link DataSourceMetadata} implementation for the hikari data source.
* data source.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariDataSource> { public class HikariDataSourceMetadata extends
AbstractDataSourceMetadata<HikariDataSource> {
private final HikariPoolProvider hikariPoolProvider; private final HikariPoolProvider hikariPoolProvider;
@ -41,13 +40,14 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
@Override @Override
public Integer getPoolSize() { public Integer getPoolSize() {
HikariPool hikariPool = hikariPoolProvider.getHikariPool(); HikariPool hikariPool = this.hikariPoolProvider.getHikariPool();
if (hikariPool != null) { if (hikariPool != null) {
return hikariPool.getActiveConnections(); return hikariPool.getActiveConnections();
} }
return null; return null;
} }
@Override
public Integer getMaxPoolSize() { public Integer getMaxPoolSize() {
return getDataSource().getMaximumPoolSize(); return getDataSource().getMaximumPoolSize();
} }
@ -63,9 +63,9 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
} }
/** /**
* Provide the {@link HikariPool} instance managed internally by * Provide the {@link HikariPool} instance managed internally by the
* the {@link HikariDataSource} as there is no other way to retrieve * {@link HikariDataSource} as there is no other way to retrieve that information
* that information except JMX access. * except JMX access.
*/ */
private static class HikariPoolProvider { private static class HikariPoolProvider {
private final HikariDataSource dataSource; private final HikariDataSource dataSource;
@ -78,7 +78,7 @@ public class HikariDataSourceMetadata extends AbstractDataSourceMetadata<HikariD
} }
public HikariPool getHikariPool() { public HikariPool getHikariPool() {
if (!poolAvailable) { if (!this.poolAvailable) {
return null; return null;
} }

@ -21,8 +21,7 @@ import org.apache.tomcat.jdbc.pool.DataSource;
/** /**
* *
* A {@link DataSourceMetadata} implementation for the tomcat * A {@link DataSourceMetadata} implementation for the tomcat data source.
* data source.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@ -52,4 +51,5 @@ public class TomcatDataSourceMetadata extends AbstractDataSourceMetadata<DataSou
public String getValidationQuery() { public String getValidationQuery() {
return getDataSource().getValidationQuery(); return getDataSource().getValidationQuery();
} }
} }

@ -16,19 +16,17 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import static org.junit.Assert.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import org.junit.Test; import org.junit.Test;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.Assert.assertEquals;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSourceMetadata> { public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSourceMetadata> {
@ -51,10 +49,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
@Test @Test
public void getPoolSizeNoConnection() { public void getPoolSizeNoConnection() {
// Make sure the pool is initialized // Make sure the pool is initialized
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource()); JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
.getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() { jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException { public Void doInConnection(Connection connection) throws SQLException,
DataAccessException {
return null; return null;
} }
}); });
@ -64,10 +64,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
@Test @Test
public void getPoolSizeOneConnection() { public void getPoolSizeOneConnection() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource()); JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
.getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() { jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException { public Void doInConnection(Connection connection) throws SQLException,
DataAccessException {
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize()); assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize());
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage()); assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage());
return null; return null;
@ -77,15 +79,20 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
@Test @Test
public void getPoolSizeTwoConnections() { public void getPoolSizeTwoConnections() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource()); final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata()
.getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() { jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException { public Void doInConnection(Connection connection) throws SQLException,
DataAccessException {
jdbcTemplate.execute(new ConnectionCallback<Void>() { jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException { public Void doInConnection(Connection connection)
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getPoolSize()); throws SQLException, DataAccessException {
assertEquals(Float.valueOf(1F), getDataSourceMetadata().getPoolUsage()); assertEquals(Integer.valueOf(2), getDataSourceMetadata()
.getPoolSize());
assertEquals(Float.valueOf(1F), getDataSourceMetadata()
.getPoolUsage());
return null; return null;
} }
}); });
@ -98,10 +105,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
public abstract void getValidationQuery(); public abstract void getValidationQuery();
protected DataSourceBuilder initializeBuilder() { protected DataSourceBuilder initializeBuilder() {
return DataSourceBuilder.create() return DataSourceBuilder.create().driverClassName("org.hsqldb.jdbc.JDBCDriver")
.driverClassName("org.hsqldb.jdbc.JDBCDriver") .url("jdbc:hsqldb:mem:test").username("sa");
.url("jdbc:hsqldb:mem:test")
.username("sa");
} }
} }

@ -16,17 +16,18 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import static org.junit.Assert.*;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> { public class CommonsDbcpDataSourceMetadataTests extends
AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> {
private CommonsDbcpDataSourceMetadata dataSourceMetadata; private CommonsDbcpDataSourceMetadata dataSourceMetadata;
@ -42,7 +43,8 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
@Test @Test
public void getPoolUsageWithNoCurrent() { public void getPoolUsageWithNoCurrent() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) { CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
createDataSource()) {
@Override @Override
public Integer getPoolSize() { public Integer getPoolSize() {
return null; return null;
@ -53,7 +55,8 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
@Test @Test
public void getPoolUsageWithNoMax() { public void getPoolUsageWithNoMax() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) { CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
createDataSource()) {
@Override @Override
public Integer getMaxPoolSize() { public Integer getMaxPoolSize() {
return null; return null;
@ -72,10 +75,12 @@ public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetada
public void getValidationQuery() { public void getValidationQuery() {
BasicDataSource dataSource = createDataSource(); BasicDataSource dataSource = createDataSource();
dataSource.setValidationQuery("SELECT FROM FOO"); dataSource.setValidationQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", new CommonsDbcpDataSourceMetadata(dataSource).getValidationQuery()); assertEquals("SELECT FROM FOO",
new CommonsDbcpDataSourceMetadata(dataSource).getValidationQuery());
} }
private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize, int maxSize) { private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize,
int maxSize) {
BasicDataSource dataSource = createDataSource(); BasicDataSource dataSource = createDataSource();
dataSource.setMinIdle(minSize); dataSource.setMinIdle(minSize);
dataSource.setMaxActive(maxSize); dataSource.setMaxActive(maxSize);

@ -16,9 +16,6 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import java.util.Arrays; import java.util.Arrays;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -28,8 +25,11 @@ import org.junit.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.BDDMockito.given;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CompositeDataSourceMetadataProviderTests { public class CompositeDataSourceMetadataProviderTests {
@ -55,30 +55,30 @@ public class CompositeDataSourceMetadataProviderTests {
@Mock @Mock
private DataSource unknownDataSource; private DataSource unknownDataSource;
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
given(firstProvider.getDataSourceMetadata(firstDataSource)).willReturn(first); given(this.firstProvider.getDataSourceMetadata(this.firstDataSource)).willReturn(
given(firstProvider.getDataSourceMetadata(secondDataSource)).willReturn(second); this.first);
given(this.firstProvider.getDataSourceMetadata(this.secondDataSource))
.willReturn(this.second);
} }
@Test @Test
public void createWithProviders() { public void createWithProviders() {
CompositeDataSourceMetadataProvider provider = CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
new CompositeDataSourceMetadataProvider(Arrays.asList(firstProvider, secondProvider)); Arrays.asList(this.firstProvider, this.secondProvider));
assertSame(first, provider.getDataSourceMetadata(firstDataSource)); assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource));
assertSame(second, provider.getDataSourceMetadata(secondDataSource)); assertSame(this.second, provider.getDataSourceMetadata(this.secondDataSource));
assertNull(provider.getDataSourceMetadata(unknownDataSource)); assertNull(provider.getDataSourceMetadata(this.unknownDataSource));
} }
@Test @Test
public void addProvider() { public void addProvider() {
CompositeDataSourceMetadataProvider provider = CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider();
new CompositeDataSourceMetadataProvider(); assertNull(provider.getDataSourceMetadata(this.firstDataSource));
assertNull(provider.getDataSourceMetadata(firstDataSource)); provider.addDataSourceMetadataProvider(this.firstProvider);
provider.addDataSourceMetadataProvider(firstProvider); assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource));
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
} }
} }

@ -16,15 +16,17 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Before; import org.junit.Before;
import com.zaxxer.hikari.HikariDataSource;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class HikariDataSourceMetadataTests extends AbstractDataSourceMetadataTests<HikariDataSourceMetadata> { public class HikariDataSourceMetadataTests extends
AbstractDataSourceMetadataTests<HikariDataSourceMetadata> {
private HikariDataSourceMetadata dataSourceMetadata; private HikariDataSourceMetadata dataSourceMetadata;
@ -42,11 +44,13 @@ public class HikariDataSourceMetadataTests extends AbstractDataSourceMetadataTes
public void getValidationQuery() { public void getValidationQuery() {
HikariDataSource dataSource = createDataSource(0, 4); HikariDataSource dataSource = createDataSource(0, 4);
dataSource.setConnectionTestQuery("SELECT FROM FOO"); dataSource.setConnectionTestQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", new HikariDataSourceMetadata(dataSource).getValidationQuery()); assertEquals("SELECT FROM FOO",
new HikariDataSourceMetadata(dataSource).getValidationQuery());
} }
private HikariDataSource createDataSource(int minSize, int maxSize) { private HikariDataSource createDataSource(int minSize, int maxSize) {
HikariDataSource dataSource = (HikariDataSource) initializeBuilder().type(HikariDataSource.class).build(); HikariDataSource dataSource = (HikariDataSource) initializeBuilder().type(
HikariDataSource.class).build();
dataSource.setMinimumIdle(minSize); dataSource.setMinimumIdle(minSize);
dataSource.setMaximumPoolSize(maxSize); dataSource.setMaximumPoolSize(maxSize);
return dataSource; return dataSource;

@ -22,10 +22,10 @@ import org.junit.Before;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class TomcatDataSourceMetadataTests extends AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> { public class TomcatDataSourceMetadataTests extends
AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> {
private TomcatDataSourceMetadata dataSourceMetadata; private TomcatDataSourceMetadata dataSourceMetadata;
@ -43,11 +43,13 @@ public class TomcatDataSourceMetadataTests extends AbstractDataSourceMetadataTes
public void getValidationQuery() { public void getValidationQuery() {
DataSource dataSource = createDataSource(0, 4); DataSource dataSource = createDataSource(0, 4);
dataSource.setValidationQuery("SELECT FROM FOO"); dataSource.setValidationQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", new TomcatDataSourceMetadata(dataSource).getValidationQuery()); assertEquals("SELECT FROM FOO",
new TomcatDataSourceMetadata(dataSource).getValidationQuery());
} }
private DataSource createDataSource(int minSize, int maxSize) { private DataSource createDataSource(int minSize, int maxSize) {
DataSource dataSource = (DataSource) initializeBuilder().type(DataSource.class).build(); DataSource dataSource = (DataSource) initializeBuilder().type(DataSource.class)
.build();
dataSource.setMinIdle(minSize); dataSource.setMinIdle(minSize);
dataSource.setMaxActive(maxSize); dataSource.setMaxActive(maxSize);

Loading…
Cancel
Save