merge branch 'master' into docs-site
This commit is contained in:
@@ -45,7 +45,7 @@ The connector will check for:
|
||||
|
||||
* a `CLEARDB_DATABASE_URL` config var with value using the scheme `mysql`
|
||||
|
||||
==== Redis (Redis To Go, Redis Cloud, RedisGreen, openredis)
|
||||
==== Redis (Redis To Go, Redis Cloud, RedisGreen, openredis, Heroku Redis)
|
||||
|
||||
The connector will check for:
|
||||
|
||||
@@ -53,6 +53,7 @@ The connector will check for:
|
||||
* a `REDISCLOUD_URL` config var with value using the scheme `redis`
|
||||
* an `OPENREDIS_URL` config var with value using the scheme `redis`
|
||||
* a `REDISGREEN_URL` config var with value using the scheme `redis`
|
||||
* a `REDIS_URL` config var with value using the scheme `redis`
|
||||
|
||||
==== MongoDB (MongoLab, MongoHQ, MongoSoup)
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
version=1.2.1.BUILD-SNAPSHOT
|
||||
version=1.2.2.BUILD-SNAPSHOT
|
||||
group=org.springframework.cloud
|
||||
|
||||
@@ -5,7 +5,7 @@ currently knows about:
|
||||
|
||||
- PostgreSQL (Heroku)
|
||||
- MySQL (ClearDB)
|
||||
- Redis (RedisToGo, Redis Cloud, RedisGreen, openredis)
|
||||
- Redis (RedisToGo, Redis Cloud, RedisGreen, openredis, Heroku Redis)
|
||||
- MongoDB (MongoLab, MongoHQ, MongoSoup)
|
||||
- RabbitMQ (CloudAMQP)
|
||||
|
||||
|
||||
@@ -18,22 +18,21 @@ import org.springframework.cloud.util.EnvironmentAccessor;
|
||||
|
||||
/**
|
||||
* Implementation of CloudConnector for Heroku
|
||||
*
|
||||
* <p/>
|
||||
* Currently support Postgres (default provided), Mysql (Cleardb), MongoDb (MongoLab, MongoHQ, MongoSoup),
|
||||
* Redis (RedisToGo, RedisCloud, OpenRedis, RedisGreen), and AMQP (CloudAmqp).
|
||||
*
|
||||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData> {
|
||||
|
||||
private EnvironmentAccessor environment = new EnvironmentAccessor();
|
||||
private ApplicationInstanceInfoCreator applicationInstanceInfoCreator
|
||||
= new ApplicationInstanceInfoCreator(environment);
|
||||
= new ApplicationInstanceInfoCreator(environment);
|
||||
|
||||
private List<String> serviceEnvPrefixes;
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public HerokuConnector() {
|
||||
super((Class) HerokuServiceInfoCreator.class);
|
||||
}
|
||||
@@ -60,15 +59,15 @@ public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData>
|
||||
|
||||
@Override
|
||||
protected void registerServiceInfoCreator(ServiceInfoCreator<? extends ServiceInfo, UriBasedServiceData> serviceInfoCreator) {
|
||||
super.registerServiceInfoCreator(serviceInfoCreator);
|
||||
HerokuServiceInfoCreator<?> herokuServiceInfoCreator = (HerokuServiceInfoCreator<?>)serviceInfoCreator;
|
||||
String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes();
|
||||
super.registerServiceInfoCreator(serviceInfoCreator);
|
||||
HerokuServiceInfoCreator<?> herokuServiceInfoCreator = (HerokuServiceInfoCreator<?>) serviceInfoCreator;
|
||||
String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes();
|
||||
|
||||
// need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this
|
||||
if (serviceEnvPrefixes == null) {
|
||||
serviceEnvPrefixes = new ArrayList<String>();
|
||||
}
|
||||
serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes));
|
||||
// need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this
|
||||
if (serviceEnvPrefixes == null) {
|
||||
serviceEnvPrefixes = new ArrayList<String>();
|
||||
}
|
||||
serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,26 +75,27 @@ public class HerokuConnector extends AbstractCloudConnector<UriBasedServiceData>
|
||||
* <p>
|
||||
* Returns map whose key is the env key and value is the associated url
|
||||
* </p>
|
||||
*
|
||||
* @return information about services bound to the app
|
||||
*/
|
||||
protected List<UriBasedServiceData> getServicesData() {
|
||||
List<UriBasedServiceData> serviceData = new ArrayList<UriBasedServiceData>();
|
||||
|
||||
Map<String,String> env = environment.getEnv();
|
||||
Map<String, String> env = environment.getEnv();
|
||||
|
||||
for (Map.Entry<String, String> envEntry : env.entrySet()) {
|
||||
for (String envPrefix : serviceEnvPrefixes) {
|
||||
if (envEntry.getKey().startsWith(envPrefix)) {
|
||||
serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue()));
|
||||
}
|
||||
}
|
||||
for (String envPrefix : serviceEnvPrefixes) {
|
||||
if (envEntry.getKey().startsWith(envPrefix)) {
|
||||
serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return serviceData;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FallbackServiceInfoCreator<BaseServiceInfo,UriBasedServiceData> getFallbackServiceInfoCreator() {
|
||||
protected FallbackServiceInfoCreator<BaseServiceInfo, UriBasedServiceData> getFallbackServiceInfoCreator() {
|
||||
return new FallbackBaseServiceInfoCreator();
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,6 @@ public class PostgresqlServiceInfoCreator extends RelationalServiceInfoCreator<P
|
||||
|
||||
@Override
|
||||
public String[] getEnvPrefixes() {
|
||||
return new String[]{"HEROKU_POSTGRESQL_"};
|
||||
return new String[]{"DATABASE_URL", "HEROKU_POSTGRESQL_"};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,6 @@ public class RedisServiceInfoCreator extends HerokuServiceInfoCreator<RedisServi
|
||||
|
||||
@Override
|
||||
public String[] getEnvPrefixes() {
|
||||
return new String[]{ "REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL" };
|
||||
return new String[]{ "REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL", "REDIS_URL" };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,23 @@ public class HerokuConnectorPostgresqlServiceTest extends AbstractHerokuConnecto
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postgresqlServiceCreation() {
|
||||
public void postgresqlServiceCreationPrimary() {
|
||||
assertPostgresServiceCreated("DATABASE_URL", "DATABASE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postgresqlServiceCreationSecondary() {
|
||||
assertPostgresServiceCreated("HEROKU_POSTGRESQL_YELLOW_URL", "HEROKU_POSTGRESQL_YELLOW");
|
||||
}
|
||||
|
||||
private void assertPostgresServiceCreated(String envVarName, String serviceInstanceName) {
|
||||
Map<String, String> env = new HashMap<String, String>();
|
||||
String postgresUrl = getRelationalServiceUrl("db");
|
||||
env.put("HEROKU_POSTGRESQL_YELLOW_URL", postgresUrl);
|
||||
env.put(envVarName, postgresUrl);
|
||||
when(mockEnvironment.getEnv()).thenReturn(env);
|
||||
|
||||
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
|
||||
ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "HEROKU_POSTGRESQL_YELLOW");
|
||||
ServiceInfo serviceInfo = getServiceInfo(serviceInfos, serviceInstanceName);
|
||||
assertNotNull(serviceInfo);
|
||||
assertTrue(serviceInfo instanceof PostgresqlServiceInfo);
|
||||
assertReleationServiceInfo((PostgresqlServiceInfo) serviceInfo, "db");
|
||||
|
||||
@@ -20,7 +20,7 @@ public class HerokuConnectorRedisServiceTest extends AbstractHerokuConnectorTest
|
||||
|
||||
@Test
|
||||
public void redisServiceCreation() {
|
||||
for (String redisEnv : new String[]{"REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL"}) {
|
||||
for (String redisEnv : new String[]{"REDISTOGO_URL", "REDISCLOUD_URL", "OPENREDIS_URL", "REDISGREEN_URL", "REDIS_URL"}) {
|
||||
Map<String, String> env = new HashMap<String, String>();
|
||||
String redisUrl = getRedisServiceUrl();
|
||||
env.put(redisEnv, redisUrl);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ public abstract class DataSourceCreator<SI extends RelationalServiceInfo> extend
|
||||
this.validationQuery = validationQuery;
|
||||
|
||||
if (pooledDataSourceCreators.size() == 0) {
|
||||
putPooledDataSourceCreator(new BasicDbcpPooledDataSourceCreator<SI>());
|
||||
putPooledDataSourceCreator(new TomcatDbcpPooledDataSourceCreator<SI>());
|
||||
putPooledDataSourceCreator(new TomcatJdbcPooledDataSourceCreator<SI>());
|
||||
putPooledDataSourceCreator(new HikariCpPooledDataSourceCreator<SI>());
|
||||
putPooledDataSourceCreator(new TomcatDbcpPooledDataSourceCreator<SI>());
|
||||
putPooledDataSourceCreator(new BasicDbcpPooledDataSourceCreator<SI>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.springframework.cloud;
|
||||
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
public class ReflectionUtils {
|
||||
@SuppressWarnings("EmptyCatchBlock")
|
||||
public static Object getValue(Object target, String... fieldNames) {
|
||||
for (String fieldName : fieldNames) {
|
||||
try {
|
||||
return ReflectionTestUtils.invokeGetterMethod(target, fieldName);
|
||||
} catch (IllegalArgumentException e1) {
|
||||
try {
|
||||
return ReflectionTestUtils.getField(target, fieldName);
|
||||
} catch (IllegalArgumentException e2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,13 @@
|
||||
package org.springframework.cloud.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.springframework.cloud.ReflectionUtils.getValue;
|
||||
|
||||
public class CommonPoolCloudConfigTestHelper {
|
||||
|
||||
public static void assertCommonsPoolProperties(Object pool, int maxActive, int minIdle, long maxWait) {
|
||||
assertEquals(maxActive, getValue(pool, "maxActive", "maxTotal"));
|
||||
assertEquals(minIdle, getValue(pool, "minIdle"));
|
||||
assertEquals(maxWait, getValue(pool, "maxWait", "maxWaitMillis"));
|
||||
assertEquals(maxWait, (long) Long.valueOf(getValue(pool, "maxWait", "maxWaitMillis").toString()));
|
||||
}
|
||||
|
||||
protected static Object getValue(Object object, String... fieldNames) {
|
||||
for (String fieldName : fieldNames) {
|
||||
try {
|
||||
return ReflectionTestUtils.getField(object, fieldName);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.cloud.ReflectionUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -20,6 +20,10 @@ public class DataSourceCloudConfigTestHelper extends CommonPoolCloudConfigTestHe
|
||||
}
|
||||
|
||||
public static void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
|
||||
assertEquals(connectionProp, ReflectionTestUtils.getField(dataSource, "connectionProperties"));
|
||||
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,18 +1,26 @@
|
||||
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;
|
||||
import org.springframework.cloud.service.relational.DataSourceConfig.ConnectionConfig;
|
||||
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
|
||||
*
|
||||
@@ -20,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);
|
||||
}
|
||||
@@ -46,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,17 +119,36 @@ 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);
|
||||
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, connectionConfig, basicDbcpConnectionPool());
|
||||
return connectionFactory().dataSource("my-service", serviceConfig);
|
||||
}
|
||||
|
||||
@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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.cloud.config.DataSourceCloudConfigTestHelper;
|
||||
import org.springframework.cloud.service.ServiceInfo;
|
||||
import org.springframework.cloud.service.relational.BasicDbcpPooledDataSourceCreator;
|
||||
import org.springframework.cloud.service.relational.HikariCpPooledDataSourceCreator;
|
||||
import org.springframework.cloud.service.relational.TomcatJdbcPooledDataSourceCreator;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -16,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;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -23,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);
|
||||
|
||||
@@ -56,29 +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());
|
||||
assertThat(ds, instanceOf(Class.forName(BasicDbcpPooledDataSourceCreator.DBCP2_BASIC_DATASOURCE)));
|
||||
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",
|
||||
|
||||
@@ -4,11 +4,14 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.ReflectionUtils;
|
||||
import org.springframework.cloud.config.DataSourceCloudConfigTestHelper;
|
||||
import org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig;
|
||||
import org.springframework.cloud.service.common.RelationalServiceInfo;
|
||||
@@ -30,7 +33,9 @@ public abstract class AbstractDataSourceCreatorTest<C extends DataSourceCreator<
|
||||
public void cloudDataSourceCreationNoConfig() throws Exception {
|
||||
SI relationalServiceInfo = createServiceInfo();
|
||||
|
||||
DataSource dataSource = getCreator().create(relationalServiceInfo, null);
|
||||
List<String> pooledDataSource = Collections.singletonList(BasicDbcpPooledDataSourceCreator.class.getSimpleName());
|
||||
DataSourceConfig config = new DataSourceConfig(pooledDataSource);
|
||||
DataSource dataSource = getCreator().create(relationalServiceInfo, config);
|
||||
|
||||
assertDataSourceProperties(relationalServiceInfo, dataSource);
|
||||
}
|
||||
@@ -39,7 +44,10 @@ public abstract class AbstractDataSourceCreatorTest<C extends DataSourceCreator<
|
||||
public void cloudDataSourceCreationWithConfig() throws Exception {
|
||||
SI relationalServiceInfo = createServiceInfo();
|
||||
|
||||
DataSourceConfig config = new DataSourceConfig(new PoolConfig("5", 100), new ConnectionConfig("foo=bar"));
|
||||
PoolConfig poolConfig = new PoolConfig("5", 100);
|
||||
ConnectionConfig connectionConfig = new ConnectionConfig("foo=bar");
|
||||
List<String> pooledDataSource = Collections.singletonList(BasicDbcpPooledDataSourceCreator.class.getSimpleName());
|
||||
DataSourceConfig config = new DataSourceConfig(poolConfig, connectionConfig, pooledDataSource);
|
||||
DataSource dataSource = getCreator().create(relationalServiceInfo, config);
|
||||
|
||||
assertDataSourceProperties(relationalServiceInfo, dataSource);
|
||||
@@ -57,11 +65,16 @@ public abstract class AbstractDataSourceCreatorTest<C extends DataSourceCreator<
|
||||
private void assertDataSourceProperties(RelationalServiceInfo relationalServiceInfo, DataSource dataSource) {
|
||||
assertNotNull(dataSource);
|
||||
|
||||
assertEquals(getDriverName(), ReflectionTestUtils.getField(dataSource, "driverClassName"));
|
||||
assertEquals(relationalServiceInfo.getJdbcUrl(), ReflectionTestUtils.getField(dataSource, "url"));
|
||||
assertTrue((Boolean) ReflectionTestUtils.invokeGetterMethod(dataSource, "testOnBorrow"));
|
||||
assertNotNull(ReflectionTestUtils.invokeGetterMethod(dataSource, "validationQuery"));
|
||||
assertTrue(((String) ReflectionTestUtils.invokeGetterMethod(dataSource, "validationQuery")).startsWith(getValidationQueryStart()));
|
||||
assertEquals(getDriverName(), ReflectionUtils.getValue(dataSource, "driverClassName"));
|
||||
assertEquals(relationalServiceInfo.getJdbcUrl(), ReflectionUtils.getValue(dataSource, "url"));
|
||||
|
||||
Object testOnBorrow = ReflectionUtils.getValue(dataSource, "testOnBorrow");
|
||||
assertNotNull(testOnBorrow);
|
||||
assertTrue(Boolean.valueOf(testOnBorrow.toString()));
|
||||
|
||||
Object validationQuery = ReflectionUtils.getValue(dataSource, "validationQuery");
|
||||
assertNotNull(validationQuery);
|
||||
assertTrue(validationQuery.toString().startsWith(getValidationQueryStart()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class PooledDataSourceCreatorsTest {
|
||||
@Test
|
||||
public void pooledDataSourceCreationDefault() throws Exception {
|
||||
DataSource ds = createMysqlDataSource(null);
|
||||
assertBasicDbcpDataSource(ds);
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -50,17 +50,6 @@ public class PooledDataSourceCreatorsTest {
|
||||
assertBasicDbcpDataSource(ds);
|
||||
}
|
||||
|
||||
private void assertBasicDbcpDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertTrue(hasClass(DBCP2_BASIC_DATASOURCE) || hasClass(DBCP_BASIC_DATASOURCE));
|
||||
|
||||
if (hasClass(DBCP2_BASIC_DATASOURCE)) {
|
||||
assertThat(ds, instanceOf(Class.forName(DBCP2_BASIC_DATASOURCE)));
|
||||
}
|
||||
if (hasClass(DBCP_BASIC_DATASOURCE) && !hasClass(DBCP2_BASIC_DATASOURCE)) {
|
||||
assertThat(ds, instanceOf(Class.forName(DBCP_BASIC_DATASOURCE)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatDbcp() throws Exception {
|
||||
DataSource ds = createMysqlDataSourceWithPooledName("TomcatDbcp");
|
||||
@@ -70,33 +59,22 @@ public class PooledDataSourceCreatorsTest {
|
||||
assertTomcatDbcpDataSource(ds);
|
||||
}
|
||||
|
||||
private void assertTomcatDbcpDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertTrue(hasClass(TOMCAT_7_DBCP) || hasClass(TOMCAT_8_DBCP));
|
||||
|
||||
if (hasClass(TOMCAT_7_DBCP)) {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_7_DBCP)));
|
||||
}
|
||||
if (hasClass(TOMCAT_8_DBCP)) {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_8_DBCP)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationTomcatJdbc() throws Exception {
|
||||
DataSource ds = createMysqlDataSourceWithPooledName("TomcatJdbc");
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_JDBC_DATASOURCE)));
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
|
||||
ds = createMysqlDataSourceWithPooledName(TomcatJdbcPooledDataSourceCreator.class.getSimpleName());
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_JDBC_DATASOURCE)));
|
||||
assertTomcatJdbcDataSource(ds);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pooledDataSourceCreationHikariCP() throws Exception {
|
||||
DataSource ds = createMysqlDataSourceWithPooledName("HikariCp");
|
||||
assertThat(ds, instanceOf(Class.forName(HIKARI_DATASOURCE)));
|
||||
assertHikariDataSource(ds);
|
||||
|
||||
ds = createMysqlDataSourceWithPooledName(HikariCpPooledDataSourceCreator.class.getSimpleName());
|
||||
assertThat(ds, instanceOf(Class.forName(HIKARI_DATASOURCE)));
|
||||
assertHikariDataSource(ds);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,4 +91,34 @@ public class PooledDataSourceCreatorsTest {
|
||||
private DataSource createMysqlDataSource(ServiceConnectorConfig config) {
|
||||
return mysqlDataSourceCreator.create(mockMysqlServiceInfo, config);
|
||||
}
|
||||
|
||||
private void assertBasicDbcpDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertTrue(hasClass(DBCP2_BASIC_DATASOURCE) || hasClass(DBCP_BASIC_DATASOURCE));
|
||||
|
||||
if (hasClass(DBCP2_BASIC_DATASOURCE)) {
|
||||
assertThat(ds, instanceOf(Class.forName(DBCP2_BASIC_DATASOURCE)));
|
||||
}
|
||||
if (hasClass(DBCP_BASIC_DATASOURCE) && !hasClass(DBCP2_BASIC_DATASOURCE)) {
|
||||
assertThat(ds, instanceOf(Class.forName(DBCP_BASIC_DATASOURCE)));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTomcatDbcpDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertTrue(hasClass(TOMCAT_7_DBCP) || hasClass(TOMCAT_8_DBCP));
|
||||
|
||||
if (hasClass(TOMCAT_7_DBCP)) {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_7_DBCP)));
|
||||
}
|
||||
if (hasClass(TOMCAT_8_DBCP)) {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_8_DBCP)));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTomcatJdbcDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertThat(ds, instanceOf(Class.forName(TOMCAT_JDBC_DATASOURCE)));
|
||||
}
|
||||
|
||||
private void assertHikariDataSource(DataSource ds) throws ClassNotFoundException {
|
||||
assertThat(ds, instanceOf(Class.forName(HIKARI_DATASOURCE)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,13 +5,32 @@
|
||||
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>
|
||||
<value>BasicDbcp</value>
|
||||
</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">
|
||||
|
||||
Reference in New Issue
Block a user