Fix DataSourceMetadata location

This commit complements 3dc932db8 and fix the package location of
DataSourceMetadata.

Fixes gh-1013
This commit is contained in:
Stephane Nicoll
2014-08-29 14:35:20 +02:00
parent 3dc932db88
commit 2694941b93
15 changed files with 18 additions and 20 deletions

View File

@@ -1,105 +0,0 @@
/*
* Copyright 2012-2014 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.actuate.metrics.jdbc;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
/**
*
* @author Stephane Nicoll
*/
public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSourceMetadata> {
/**
* Return a data source metadata instance with a min size of 0 and max size of 2.
*/
protected abstract D getDataSourceMetadata();
@Test
public void getMaxPoolSize() {
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getMaxPoolSize());
}
@Test
public void getMinPoolSize() {
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getMinPoolSize());
}
@Test
public void getPoolSizeNoConnection() {
// Make sure the pool is initialized
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
return null;
}
});
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getPoolSize());
assertEquals(Float.valueOf(0), getDataSourceMetadata().getPoolUsage());
}
@Test
public void getPoolSizeOneConnection() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize());
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage());
return null;
}
});
}
@Test
public void getPoolSizeTwoConnections() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getPoolSize());
assertEquals(Float.valueOf(1F), getDataSourceMetadata().getPoolUsage());
return null;
}
});
return null;
}
});
}
protected DataSourceBuilder initializeBuilder() {
return DataSourceBuilder.create()
.driverClassName("org.hsqldb.jdbc.JDBCDriver")
.url("jdbc:hsqldb:mem:test")
.username("sa");
}
}

View File

@@ -1,82 +0,0 @@
/*
* Copyright 2012-2014 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.actuate.metrics.jdbc;
import static org.junit.Assert.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author Stephane Nicoll
*/
public class CommonsDbcpDataSourceMetadataTests extends AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> {
private CommonsDbcpDataSourceMetadata dataSourceMetadata;
@Before
public void setup() {
this.dataSourceMetadata = createDataSourceMetadata(0, 2);
}
@Override
protected CommonsDbcpDataSourceMetadata getDataSourceMetadata() {
return this.dataSourceMetadata;
}
@Test
public void getPoolUsageWithNoCurrent() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) {
@Override
public Integer getPoolSize() {
return null;
}
};
assertNull(dsm.getPoolUsage());
}
@Test
public void getPoolUsageWithNoMax() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(createDataSource()) {
@Override
public Integer getMaxPoolSize() {
return null;
}
};
assertNull(dsm.getPoolUsage());
}
@Test
public void getPoolUsageWithUnlimitedPool() {
DataSourceMetadata unlimitedDataSource = createDataSourceMetadata(0, -1);
assertEquals(Float.valueOf(-1F), unlimitedDataSource.getPoolUsage());
}
private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize, int maxSize) {
BasicDataSource dataSource = createDataSource();
dataSource.setMinIdle(minSize);
dataSource.setMaxActive(maxSize);
return new CommonsDbcpDataSourceMetadata(dataSource);
}
private BasicDataSource createDataSource() {
return (BasicDataSource) initializeBuilder().type(BasicDataSource.class).build();
}
}

View File

@@ -1,84 +0,0 @@
/*
* Copyright 2012-2014 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.actuate.metrics.jdbc;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import java.util.Arrays;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
*
* @author Stephane Nicoll
*/
public class CompositeDataSourceMetadataProviderTests {
@Mock
private DataSourceMetadataProvider firstProvider;
@Mock
private DataSourceMetadata first;
@Mock
private DataSource firstDataSource;
@Mock
private DataSourceMetadataProvider secondProvider;
@Mock
private DataSourceMetadata second;
@Mock
private DataSource secondDataSource;
@Mock
private DataSource unknownDataSource;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
given(firstProvider.getDataSourceMetadata(firstDataSource)).willReturn(first);
given(firstProvider.getDataSourceMetadata(secondDataSource)).willReturn(second);
}
@Test
public void createWithProviders() {
CompositeDataSourceMetadataProvider provider =
new CompositeDataSourceMetadataProvider(Arrays.asList(firstProvider, secondProvider));
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
assertSame(second, provider.getDataSourceMetadata(secondDataSource));
assertNull(provider.getDataSourceMetadata(unknownDataSource));
}
@Test
public void addProvider() {
CompositeDataSourceMetadataProvider provider =
new CompositeDataSourceMetadataProvider();
assertNull(provider.getDataSourceMetadata(firstDataSource));
provider.addDataSourceMetadataProvider(firstProvider);
assertSame(first, provider.getDataSourceMetadata(firstDataSource));
}
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2012-2014 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.actuate.metrics.jdbc;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Before;
/**
*
* @author Stephane Nicoll
*/
public class HikariDataSourceMetadataTests extends AbstractDataSourceMetadataTests<HikariDataSourceMetadata> {
private HikariDataSourceMetadata dataSourceMetadata;
@Before
public void setup() {
this.dataSourceMetadata = createDataSourceMetadata(0, 2);
}
@Override
protected HikariDataSourceMetadata getDataSourceMetadata() {
return this.dataSourceMetadata;
}
private HikariDataSourceMetadata createDataSourceMetadata(int minSize, int maxSize) {
HikariDataSource dataSource = (HikariDataSource) initializeBuilder().type(HikariDataSource.class).build();
dataSource.setMinimumIdle(minSize);
dataSource.setMaximumPoolSize(maxSize);
return new HikariDataSourceMetadata(dataSource);
}
}

View File

@@ -1,51 +0,0 @@
/*
* Copyright 2012-2014 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.actuate.metrics.jdbc;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.junit.Before;
/**
*
* @author Stephane Nicoll
*/
public class TomcatDataSourceMetadataTests extends AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> {
private TomcatDataSourceMetadata dataSourceMetadata;
@Before
public void setup() {
this.dataSourceMetadata = createDataSourceMetadata(0, 2);
}
@Override
protected TomcatDataSourceMetadata getDataSourceMetadata() {
return this.dataSourceMetadata;
}
private TomcatDataSourceMetadata createDataSourceMetadata(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 new TomcatDataSourceMetadata(dataSource);
}
}