Add the ability to pass a map of connection properties to the DataSource service connection creator for more control over the created DataSource.
This commit is contained in:
@@ -10,6 +10,7 @@ import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Parser for the {@code <cloud:data-source>} namespace element
|
||||
@@ -23,6 +24,7 @@ public class CloudDataSourceFactoryParser extends AbstractPoolingCloudServiceFac
|
||||
private static final String ELEMENT_CONNECTION = "connection";
|
||||
private static final String ELEMENT_POOL = "pool";
|
||||
private static final String ELEMENT_DATASOURCE_NAMES = "pool-data-sources";
|
||||
private static final String ELEMENT_CONNECTION_PROPERTIES = "connection-properties";
|
||||
|
||||
public CloudDataSourceFactoryParser() {
|
||||
super(CloudDataSourceFactory.class);
|
||||
@@ -54,9 +56,16 @@ public class CloudDataSourceFactoryParser extends AbstractPoolingCloudServiceFac
|
||||
parseListElement(dataSourceNamesElement, dataSourceConfigBeanBuilder.getRawBeanDefinition());
|
||||
}
|
||||
|
||||
Map<?, ?> properties = null;
|
||||
Element propertiesElement = DomUtils.getChildElementByTagName(element, ELEMENT_CONNECTION_PROPERTIES);
|
||||
if (propertiesElement != null) {
|
||||
properties = parserContext.getDelegate().parseMapElement(propertiesElement, builder.getRawBeanDefinition());
|
||||
}
|
||||
|
||||
dataSourceConfigBeanBuilder.addConstructorArgValue(cloudPoolConfiguration);
|
||||
dataSourceConfigBeanBuilder.addConstructorArgValue(cloudConnectionConfiguration);
|
||||
dataSourceConfigBeanBuilder.addConstructorArgValue(dataSourceNames);
|
||||
dataSourceConfigBeanBuilder.addConstructorArgValue(properties);
|
||||
|
||||
builder.addConstructorArgValue(dataSourceConfigBeanBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.springframework.cloud.service.relational;
|
||||
|
||||
import org.springframework.cloud.service.MapServiceConnectorConfig;
|
||||
import org.springframework.cloud.service.PooledServiceConnectorConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -11,21 +13,37 @@ import java.util.List;
|
||||
*/
|
||||
public class DataSourceConfig extends PooledServiceConnectorConfig {
|
||||
private final ConnectionConfig connectionConfig;
|
||||
private final MapServiceConnectorConfig connectionProperties;
|
||||
private final List<String> pooledDataSourceNames;
|
||||
|
||||
public DataSourceConfig(PoolConfig poolConfig, ConnectionConfig connectionConfig) {
|
||||
this(poolConfig, connectionConfig, null);
|
||||
this(poolConfig, connectionConfig, null, null);
|
||||
}
|
||||
|
||||
public DataSourceConfig(List<String> pooledDataSourceNames) {
|
||||
this(null, null, pooledDataSourceNames);
|
||||
this(null, null, pooledDataSourceNames, null);
|
||||
}
|
||||
|
||||
public DataSourceConfig(Map<String, Object> properties) {
|
||||
this(null, null, null, properties);
|
||||
}
|
||||
|
||||
public DataSourceConfig(PoolConfig poolConfig, ConnectionConfig connectionConfig,
|
||||
Map<String, Object> properties) {
|
||||
this(poolConfig, connectionConfig, null, properties);
|
||||
}
|
||||
|
||||
public DataSourceConfig(PoolConfig poolConfig, ConnectionConfig connectionConfig,
|
||||
List<String> pooledDataSourceNames) {
|
||||
this(poolConfig, connectionConfig, pooledDataSourceNames, null);
|
||||
}
|
||||
|
||||
public DataSourceConfig(PoolConfig poolConfig, ConnectionConfig connectionConfig,
|
||||
List<String> pooledDataSourceNames, Map<String, Object> properties) {
|
||||
super(poolConfig);
|
||||
this.connectionConfig = connectionConfig;
|
||||
this.pooledDataSourceNames = pooledDataSourceNames;
|
||||
this.connectionProperties = new MapServiceConnectorConfig(properties);
|
||||
}
|
||||
|
||||
public ConnectionConfig getConnectionConfiguration() {
|
||||
@@ -36,6 +54,10 @@ public class DataSourceConfig extends PooledServiceConnectorConfig {
|
||||
return pooledDataSourceNames;
|
||||
}
|
||||
|
||||
public MapServiceConnectorConfig getConnectionProperties() {
|
||||
return connectionProperties;
|
||||
}
|
||||
|
||||
public static class ConnectionConfig {
|
||||
private String prop;
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.cloud.service.MapServiceConnectionConfigurer;
|
||||
import org.springframework.cloud.service.MapServiceConnectorConfig;
|
||||
import org.springframework.cloud.service.PooledServiceConnectorConfigurer;
|
||||
|
||||
/**
|
||||
@@ -14,17 +16,30 @@ import org.springframework.cloud.service.PooledServiceConnectorConfigurer;
|
||||
*
|
||||
*/
|
||||
public class DataSourceConfigurer extends PooledServiceConnectorConfigurer<DataSource, DataSourceConfig> {
|
||||
private MapServiceConnectionConfigurer<DataSource, MapServiceConnectorConfig> mapServiceConnectionConfigurer =
|
||||
new MapServiceConnectionConfigurer<DataSource, MapServiceConnectorConfig>();
|
||||
|
||||
@Override
|
||||
public DataSource configure(DataSource dataSource, DataSourceConfig config) {
|
||||
if (config != null) {
|
||||
BeanWrapper target = new BeanWrapperImpl(dataSource);
|
||||
|
||||
if (config.getConnectionConfiguration() != null) {
|
||||
BeanWrapper connectionSource = new BeanWrapperImpl(config.getConnectionConfiguration());
|
||||
setCorrespondingProperties(target, connectionSource);
|
||||
}
|
||||
configureConnection(dataSource, config);
|
||||
configureConnectionProperties(dataSource, config);
|
||||
return super.configure(dataSource, config);
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private void configureConnection(DataSource dataSource, DataSourceConfig config) {
|
||||
if (config.getConnectionConfiguration() != null) {
|
||||
BeanWrapper target = new BeanWrapperImpl(dataSource);
|
||||
BeanWrapper connectionSource = new BeanWrapperImpl(config.getConnectionConfiguration());
|
||||
setCorrespondingProperties(target, connectionSource);
|
||||
}
|
||||
}
|
||||
|
||||
private void configureConnectionProperties(DataSource dataSource, DataSourceConfig config) {
|
||||
if (config.getConnectionProperties() != null) {
|
||||
mapServiceConnectionConfigurer.configure(dataSource, config.getConnectionProperties());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ import org.springframework.cloud.service.common.MysqlServiceInfo;
|
||||
*
|
||||
*/
|
||||
public class MysqlDataSourceCreator extends DataSourceCreator<MysqlServiceInfo> {
|
||||
private static final String[] DRIVERS = new String[]{"org.mariadb.jdbc.Driver", "com.mysql.jdbc.Driver"};
|
||||
public static final String[] DRIVERS = new String[]{"org.mariadb.jdbc.Driver", "com.mysql.jdbc.Driver"};
|
||||
/**
|
||||
* Validation query obtained from the MySQL reference manual:
|
||||
* http://dev.mysql.com/doc/refman/5.1/en/connector-j-usagenotes-j2ee.html
|
||||
*/
|
||||
private static final String VALIDATION_QUERY = "/* ping */ SELECT 1";
|
||||
public static final String VALIDATION_QUERY = "/* ping */ SELECT 1";
|
||||
|
||||
public MysqlDataSourceCreator() {
|
||||
super("spring-cloud.mysql.driver", DRIVERS, VALIDATION_QUERY);
|
||||
|
||||
@@ -4,8 +4,8 @@ import org.springframework.cloud.service.common.OracleServiceInfo;
|
||||
|
||||
public class OracleDataSourceCreator extends DataSourceCreator<OracleServiceInfo> {
|
||||
|
||||
private static final String[] DRIVERS = new String[]{"oracle.jdbc.OracleDriver"};
|
||||
private static final String VALIDATION_QUERY = "SELECT 'Y' from dual";
|
||||
public static final String[] DRIVERS = new String[]{"oracle.jdbc.OracleDriver"};
|
||||
public static final String VALIDATION_QUERY = "SELECT 'Y' from dual";
|
||||
|
||||
public OracleDataSourceCreator() {
|
||||
super("spring-cloud.oracle.driver", DRIVERS, VALIDATION_QUERY);
|
||||
|
||||
@@ -10,8 +10,8 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
|
||||
*/
|
||||
public class PostgresqlDataSourceCreator extends DataSourceCreator<PostgresqlServiceInfo> {
|
||||
|
||||
private static final String[] DRIVERS = new String[]{"org.postgresql.Driver"};
|
||||
private static final String VALIDATION_QUERY = "SELECT 1";
|
||||
public static final String[] DRIVERS = new String[]{"org.postgresql.Driver"};
|
||||
public static final String VALIDATION_QUERY = "SELECT 1";
|
||||
|
||||
public PostgresqlDataSourceCreator() {
|
||||
super("spring-cloud.postgresql.driver", DRIVERS, VALIDATION_QUERY);
|
||||
|
||||
@@ -60,6 +60,7 @@
|
||||
<xsd:element name="connection" type="jdbcConnectionType" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element name="pool" type="poolType" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element ref="pool-data-sources" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element ref="connection-properties" minOccurs="0" maxOccurs="1"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string" use="optional">
|
||||
<xsd:annotation>
|
||||
|
||||
@@ -22,4 +22,8 @@ public class DataSourceCloudConfigTestHelper extends CommonPoolCloudConfigTestHe
|
||||
public static void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
|
||||
assertEquals(connectionProp, ReflectionUtils.getValue(dataSource, "connectionProperties"));
|
||||
}
|
||||
|
||||
public static void assertConnectionProperty(DataSource dataSource, String key, Object value) {
|
||||
assertEquals(value, ReflectionUtils.getValue(dataSource, key));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.cloud.config.java;
|
||||
|
||||
import org.springframework.cloud.service.common.MysqlServiceInfo;
|
||||
import org.springframework.cloud.service.relational.MysqlDataSourceCreator;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -13,6 +14,15 @@ public class DataSourceJavaConfigMysqlTest extends DataSourceJavaConfigTest {
|
||||
return createMysqlService(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverClassName() {
|
||||
return MysqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getValidationQuery() {
|
||||
return MysqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.cloud.config.java;
|
||||
|
||||
import org.springframework.cloud.service.common.PostgresqlServiceInfo;
|
||||
import org.springframework.cloud.service.relational.PostgresqlDataSourceCreator;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -13,6 +14,15 @@ public class DataSourceJavaConfigPostgesqlTest extends DataSourceJavaConfigTest
|
||||
return createPostgresqlService(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverClassName() {
|
||||
return PostgresqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getValidationQuery() {
|
||||
return PostgresqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package org.springframework.cloud.config.java;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.cloud.config.DataSourceCloudConfigTestHelper;
|
||||
import org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig;
|
||||
import org.springframework.cloud.service.relational.BasicDbcpPooledDataSourceCreator;
|
||||
import org.springframework.cloud.service.relational.DataSourceConfig;
|
||||
@@ -16,6 +17,10 @@ import org.springframework.cloud.service.relational.DataSourceConfig.ConnectionC
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import static org.springframework.cloud.config.DataSourceCloudConfigTestHelper.assertConnectionProperties;
|
||||
import static org.springframework.cloud.config.DataSourceCloudConfigTestHelper.assertConnectionProperty;
|
||||
import static org.springframework.cloud.config.DataSourceCloudConfigTestHelper.assertPoolProperties;
|
||||
|
||||
/**
|
||||
* Common base class for testing datasource-related Java config
|
||||
*
|
||||
@@ -23,7 +28,9 @@ import org.springframework.context.annotation.Bean;
|
||||
*
|
||||
*/
|
||||
public abstract class DataSourceJavaConfigTest extends AbstractServiceJavaConfigTest<DataSource> {
|
||||
|
||||
protected abstract String getDriverClassName();
|
||||
protected abstract String getValidationQuery();
|
||||
|
||||
public DataSourceJavaConfigTest() {
|
||||
super(DatasourceConfigWithId.class, DatasourceConfigWithoutId.class);
|
||||
}
|
||||
@@ -49,28 +56,50 @@ public abstract class DataSourceJavaConfigTest extends AbstractServiceJavaConfig
|
||||
testContext.getBean(getConnectorType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithNoConfig() {
|
||||
ApplicationContext testContext = getTestApplicationContext(DatasourceConfigWithServiceConfig.class,
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("dataSourceWithNoConfig", getConnectorType());
|
||||
|
||||
assertConnectionProperties(ds, null);
|
||||
assertConnectionProperty(ds, "driverClassName", getDriverClassName());
|
||||
assertConnectionProperty(ds, "validationQuery", getValidationQuery());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithMaxPool() {
|
||||
ApplicationContext testContext = getTestApplicationContext(DatasourceConfigWithServiceConfig.class,
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("dbPool20Wait200", getConnectorType());
|
||||
DataSourceCloudConfigTestHelper.assertPoolProperties(ds, 20, 0, 200);
|
||||
|
||||
|
||||
DataSource ds = testContext.getBean("dataSourceWithPoolAndConnectionConfig", getConnectorType());
|
||||
assertPoolProperties(ds, 20, 0, 200);
|
||||
|
||||
Properties connectionProp = new Properties();
|
||||
connectionProp.put("sessionVariables", "sql_mode='ANSI'");
|
||||
connectionProp.put("characterEncoding", "UTF-8");
|
||||
DataSourceCloudConfigTestHelper.assertConnectionProperties(ds, connectionProp);
|
||||
assertConnectionProperties(ds, connectionProp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithMinMaxPool() {
|
||||
ApplicationContext testContext = getTestApplicationContext(DatasourceConfigWithServiceConfig.class,
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("dbPool5_20Wait3000", getConnectorType());
|
||||
DataSourceCloudConfigTestHelper.assertPoolProperties(ds, 30, 5, 3000);
|
||||
DataSource ds = testContext.getBean("dataSourceWithPoolConfig", getConnectorType());
|
||||
assertPoolProperties(ds, 30, 5, 3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithConnectionProperties() {
|
||||
ApplicationContext testContext = getTestApplicationContext(DatasourceConfigWithServiceConfig.class,
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("dataSourceWithConnectionPropertiesConfig", getConnectorType());
|
||||
assertConnectionProperty(ds, "driverClassName", "test.driver");
|
||||
assertConnectionProperty(ds, "validationQuery", "test validation query");
|
||||
assertConnectionProperty(ds, "testOnBorrow", false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +119,12 @@ class DatasourceConfigWithoutId extends AbstractCloudConfig {
|
||||
|
||||
class DatasourceConfigWithServiceConfig extends AbstractCloudConfig {
|
||||
@Bean
|
||||
public DataSource dbPool20Wait200() { // use this name so that we have a case with default name
|
||||
public DataSource dataSourceWithNoConfig() {
|
||||
return connectionFactory().dataSource("my-service");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSourceWithPoolAndConnectionConfig() {
|
||||
PoolConfig poolConfig = new PoolConfig(20, 200);
|
||||
ConnectionConfig connectionConfig = new ConnectionConfig("sessionVariables=sql_mode='ANSI';characterEncoding=UTF-8");
|
||||
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, connectionConfig, basicDbcpConnectionPool());
|
||||
@@ -98,12 +132,22 @@ class DatasourceConfigWithServiceConfig extends AbstractCloudConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dbPool5_20Wait3000() { // use this name so that we have a case with default name
|
||||
public DataSource dataSourceWithPoolConfig() {
|
||||
PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
|
||||
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, null, basicDbcpConnectionPool());
|
||||
return connectionFactory().dataSource("my-service", serviceConfig);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSourceWithConnectionPropertiesConfig() {
|
||||
Map<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put("driverClassName", "test.driver");
|
||||
properties.put("validationQuery", "test validation query");
|
||||
properties.put("testOnBorrow", false);
|
||||
DataSourceConfig serviceConfig = new DataSourceConfig(null, null, basicDbcpConnectionPool(), properties);
|
||||
return connectionFactory().dataSource("my-service", serviceConfig);
|
||||
}
|
||||
|
||||
private List<String> basicDbcpConnectionPool() {
|
||||
return Collections.singletonList(BasicDbcpPooledDataSourceCreator.class.getSimpleName());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.cloud.config.xml;
|
||||
|
||||
import org.springframework.cloud.service.ServiceInfo;
|
||||
import org.springframework.cloud.service.relational.MysqlDataSourceCreator;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -8,10 +9,17 @@ import org.springframework.cloud.service.ServiceInfo;
|
||||
*
|
||||
*/
|
||||
public class DataSourceXmlConfigMysqlTest extends DataSourceXmlConfigTest {
|
||||
|
||||
protected ServiceInfo createService(String id) {
|
||||
return createMysqlService(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverClassName() {
|
||||
return MysqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getValidationQuery() {
|
||||
return MysqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.springframework.cloud.config.xml;
|
||||
|
||||
import org.springframework.cloud.service.ServiceInfo;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public class DataSourceXmlConfigPostgesqlTest extends DataSourceXmlConfigTest {
|
||||
|
||||
@Override
|
||||
protected ServiceInfo createService(String id) {
|
||||
return createPostgresqlService(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.springframework.cloud.config.xml;
|
||||
|
||||
import org.springframework.cloud.service.ServiceInfo;
|
||||
import org.springframework.cloud.service.relational.PostgresqlDataSourceCreator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public class DataSourceXmlConfigPostgresqlTest extends DataSourceXmlConfigTest {
|
||||
@Override
|
||||
protected ServiceInfo createService(String id) {
|
||||
return createPostgresqlService(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDriverClassName() {
|
||||
return PostgresqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getValidationQuery() {
|
||||
return PostgresqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.cloud.config.DataSourceCloudConfigTestHelper.assertConnectionProperties;
|
||||
import static org.springframework.cloud.config.DataSourceCloudConfigTestHelper.assertConnectionProperty;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -22,6 +24,8 @@ import static org.junit.Assert.assertThat;
|
||||
*
|
||||
*/
|
||||
public abstract class DataSourceXmlConfigTest extends AbstractServiceXmlConfigTest<DataSource> {
|
||||
protected abstract String getDriverClassName();
|
||||
protected abstract String getValidationQuery();
|
||||
|
||||
protected abstract ServiceInfo createService(String id);
|
||||
|
||||
@@ -55,28 +59,50 @@ public abstract class DataSourceXmlConfigTest extends AbstractServiceXmlConfigTe
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithMaxPool() throws Exception {
|
||||
public void cloudDataSourceWithNoConfig() throws Exception {
|
||||
ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("db-pool20-wait200", getConnectorType());
|
||||
DataSource ds = testContext.getBean("no-config", getConnectorType());
|
||||
assertConnectionProperties(ds, null);
|
||||
assertConnectionProperty(ds, "driverClassName", getDriverClassName());
|
||||
assertConnectionProperty(ds, "validationQuery", getValidationQuery());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithMaxPool() throws Exception {
|
||||
ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("pool-and-connection-config", getConnectorType());
|
||||
DataSourceCloudConfigTestHelper.assertPoolProperties(ds, 20, 0, 200);
|
||||
|
||||
|
||||
Properties connectionProp = new Properties();
|
||||
connectionProp.put("sessionVariables", "sql_mode='ANSI'");
|
||||
connectionProp.put("characterEncoding", "UTF-8");
|
||||
DataSourceCloudConfigTestHelper.assertConnectionProperties(ds, connectionProp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithMinMaxPool() {
|
||||
ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("db-pool5-30-wait3000", getConnectorType());
|
||||
DataSource ds = testContext.getBean("pool-config", getConnectorType());
|
||||
DataSourceCloudConfigTestHelper.assertPoolProperties(ds, 30, 5, 3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithConnectionProperties() {
|
||||
ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
|
||||
createService("my-service"));
|
||||
|
||||
DataSource ds = testContext.getBean("properties-config", getConnectorType());
|
||||
assertConnectionProperty(ds, "driverClassName", "test.driver");
|
||||
assertConnectionProperty(ds, "validationQuery", "test validation query");
|
||||
assertConnectionProperty(ds, "testOnBorrow", false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloudDataSourceWithTomcatJdbcDataSource() throws Exception {
|
||||
ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
|
||||
|
||||
@@ -8,13 +8,15 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.cloud.service.common.DB2ServiceInfo;;
|
||||
|
||||
public class DB2ServiceCreatorTest extends AbstractDataSourceCreatorTest<DB2DataSourceCreator, DB2ServiceInfo> {
|
||||
public static final String TEST_DB2_DRIVER = "com.db2.example.Driver";
|
||||
|
||||
@Mock private DB2ServiceInfo mockDB2ServiceInfo;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
// set a dummy JDBC driver since we can't yet include a real DB2 driver in the project due to licensing restrictions
|
||||
System.setProperty("spring-cloud.db2.driver", "com.example.Driver");
|
||||
System.setProperty("spring-cloud.db2.driver", TEST_DB2_DRIVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,7 +28,7 @@ public class DB2ServiceCreatorTest extends AbstractDataSourceCreatorTest<DB2Data
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return "com.example.Driver";
|
||||
return TEST_DB2_DRIVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -34,6 +34,6 @@ public abstract class MysqlServiceCreatorTest extends AbstractDataSourceCreatorT
|
||||
|
||||
@Override
|
||||
public String getValidationQueryStart() {
|
||||
return "/* ping */ SELECT 1";
|
||||
return MysqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@ package org.springframework.cloud.service.relational;
|
||||
*
|
||||
*/
|
||||
public class MysqlServiceCreatorWithDefaultDriverTest extends MysqlServiceCreatorTest {
|
||||
private static final String MYSQL_DRIVER_CLASS_NAME = "org.mariadb.jdbc.Driver";
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return MYSQL_DRIVER_CLASS_NAME;
|
||||
return MysqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ import org.junit.Before;
|
||||
*
|
||||
*/
|
||||
public class MysqlServiceCreatorWithMysqlDriverTest extends MysqlServiceCreatorTest {
|
||||
private static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
|
||||
private static final String TEST_MYSQL_DRIVER = "com.mysql.example.Driver";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
super.setup();
|
||||
System.setProperty("spring-cloud.mysql.driver", MYSQL_DRIVER_CLASS_NAME);
|
||||
System.setProperty("spring-cloud.mysql.driver", TEST_MYSQL_DRIVER);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -24,6 +24,6 @@ public class MysqlServiceCreatorWithMysqlDriverTest extends MysqlServiceCreatorT
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return MYSQL_DRIVER_CLASS_NAME;
|
||||
return TEST_MYSQL_DRIVER;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,16 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.cloud.service.common.OracleServiceInfo;
|
||||
|
||||
public class OracleServiceCreatorTest extends AbstractDataSourceCreatorTest<OracleDataSourceCreator, OracleServiceInfo> {
|
||||
@Mock private OracleServiceInfo mockOracleServiceInfo;
|
||||
public static final String TEST_ORACLE_DRIVER = "com.oracle.example.Driver";
|
||||
|
||||
@Mock
|
||||
private OracleServiceInfo mockOracleServiceInfo;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
// set a dummy JDBC driver since we can't include a real Oracle driver in the project due to licensing restrictions
|
||||
System.setProperty("spring-cloud.oracle.driver", "com.example.Driver");
|
||||
System.setProperty("spring-cloud.oracle.driver", TEST_ORACLE_DRIVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,7 +29,7 @@ public class OracleServiceCreatorTest extends AbstractDataSourceCreatorTest<Orac
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return "com.example.Driver";
|
||||
return TEST_ORACLE_DRIVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,6 +39,6 @@ public class OracleServiceCreatorTest extends AbstractDataSourceCreatorTest<Orac
|
||||
|
||||
@Override
|
||||
public String getValidationQueryStart() {
|
||||
return "SELECT 'Y' from dual";
|
||||
return OracleDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
|
||||
*
|
||||
*/
|
||||
public class PostgresqlServiceCreatorTest extends AbstractDataSourceCreatorTest<PostgresqlDataSourceCreator, PostgresqlServiceInfo> {
|
||||
@Mock private PostgresqlServiceInfo mockPostgresqlServiceInfo;
|
||||
@Mock
|
||||
private PostgresqlServiceInfo mockPostgresqlServiceInfo;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@@ -29,7 +30,7 @@ public class PostgresqlServiceCreatorTest extends AbstractDataSourceCreatorTest<
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return "org.postgresql.Driver";
|
||||
return PostgresqlDataSourceCreator.DRIVERS[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,6 +40,6 @@ public class PostgresqlServiceCreatorTest extends AbstractDataSourceCreatorTest<
|
||||
|
||||
@Override
|
||||
public String getValidationQueryStart() {
|
||||
return "SELECT 1";
|
||||
return PostgresqlDataSourceCreator.VALIDATION_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,16 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.cloud.service.common.SqlServerServiceInfo;
|
||||
|
||||
public class SqlServerServiceCreatorTest extends AbstractDataSourceCreatorTest<SqlServerDataSourceCreator, SqlServerServiceInfo> {
|
||||
@Mock private SqlServerServiceInfo mockSqlServerServiceInfo;
|
||||
public static final String TEST_SQLSERVER_DRIVER = "com.sqlserver.example.Driver";
|
||||
|
||||
@Mock
|
||||
private SqlServerServiceInfo mockSqlServerServiceInfo;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
// set a dummy JDBC driver since we can't include a real SQL-Server driver in the project due to licensing restrictions
|
||||
System.setProperty("spring-cloud.sqlserver.driver", "com.example.Driver");
|
||||
System.setProperty("spring-cloud.sqlserver.driver", TEST_SQLSERVER_DRIVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,7 +29,7 @@ public class SqlServerServiceCreatorTest extends AbstractDataSourceCreatorTest<S
|
||||
|
||||
@Override
|
||||
public String getDriverName() {
|
||||
return "com.example.Driver";
|
||||
return TEST_SQLSERVER_DRIVER;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/cloud http://www.springframework.org/schema/cloud/spring-cloud.xsd">
|
||||
|
||||
<cloud:data-source id="db-pool20-wait200" service-name="my-service">
|
||||
<cloud:data-source id="no-config" service-name="my-service"/>
|
||||
|
||||
<cloud:data-source id="pool-and-connection-config" service-name="my-service">
|
||||
<cloud:connection properties="sessionVariables=sql_mode='ANSI';characterEncoding=UTF-8"/>
|
||||
<cloud:pool pool-size="20" max-wait-time="200"/>
|
||||
<cloud:pool-data-sources>
|
||||
@@ -13,13 +15,24 @@
|
||||
</cloud:pool-data-sources>
|
||||
</cloud:data-source>
|
||||
|
||||
<cloud:data-source id="db-pool5-30-wait3000" service-name="my-service">
|
||||
<cloud:data-source id="pool-config" service-name="my-service">
|
||||
<cloud:pool pool-size="5-30" max-wait-time="3000"/>
|
||||
<cloud:pool-data-sources>
|
||||
<value>BasicDbcp</value>
|
||||
</cloud:pool-data-sources>
|
||||
</cloud:data-source>
|
||||
|
||||
<cloud:data-source id="properties-config" service-name="my-service">
|
||||
<cloud:pool-data-sources>
|
||||
<value>BasicDbcp</value>
|
||||
</cloud:pool-data-sources>
|
||||
<cloud:connection-properties>
|
||||
<entry key="driverClassName" value="test.driver"/>
|
||||
<entry key="validationQuery" value="test validation query"/>
|
||||
<entry key="testOnBorrow" value="false"/>
|
||||
</cloud:connection-properties>
|
||||
</cloud:data-source>
|
||||
|
||||
<cloud:data-source id="db-pool-tomcat-jdbc" service-name="my-service">
|
||||
<cloud:pool-data-sources>
|
||||
<value>TomcatJdbc</value>
|
||||
|
||||
Reference in New Issue
Block a user