Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reconfigure Redis

- Abstract common Mongo and Redis config code into base class

- Replace a single RedisConnectionFactory with one connecting to a redis cloud
service bound to the current application

- Extract existing post-processor test into separate tests

Change-Id: Icca0b4a0d325d1ee9c8e987c0c06c2b0426655db
  • Loading branch information...
commit e867dff4f32a9e7fbfe801e72c08f61edb471e7d 1 parent 0f1e902
Jennifer Hickey authored
Showing with 896 additions and 458 deletions.
  1. +7 −0 auto-reconfiguration/auto-reconfiguration-core/pom.xml
  2. +75 −0 ...ion/auto-reconfiguration-core/src/main/java/org/cloudfoundry/reconfiguration/AbstractServiceConfigurer.java
  3. +13 −2 ...iguration-core/src/main/java/org/cloudfoundry/reconfiguration/CloudAutoStagingBeanFactoryPostProcessor.java
  4. +16 −60 ...auto-reconfiguration-core/src/main/java/org/cloudfoundry/reconfiguration/data/document/MongoConfigurer.java
  5. +43 −0 ...auto-reconfiguration-core/src/main/java/org/cloudfoundry/reconfiguration/data/keyvalue/RedisConfigurer.java
  6. +5 −5 .../auto-reconfiguration-core/src/main/java/org/cloudfoundry/reconfiguration/data/orm/HibernateConfigurer.java
  7. +5 −5 ...ration/auto-reconfiguration-core/src/main/java/org/cloudfoundry/reconfiguration/data/orm/JpaConfigurer.java
  8. +17 −13 ...configuration-core/src/main/java/org/cloudfoundry/reconfiguration/data/relational/DataSourceConfigurer.java
  9. +114 −0 ...ation-core/src/test/java/org/cloudfoundry/reconfiguration/CloudAutoStagingBeanFactoryPostProcessorTest.java
  10. +38 −0 ...n/auto-reconfiguration-core/src/test/java/org/cloudfoundry/reconfiguration/CloudEnvironmentMockingTest.java
  11. +0 −337 ...ore/src/test/java/org/cloudfoundry/reconfiguration/CloudFactoryAutoStagingBeanFactoryPostProcessorTest.java
  12. +26 −15 ...-reconfiguration-core/src/test/java/org/cloudfoundry/reconfiguration/data/document/MongoConfigurerTest.java
  13. +137 −0 ...-reconfiguration-core/src/test/java/org/cloudfoundry/reconfiguration/data/keyvalue/RedisConfigurerTest.java
  14. +116 −0 ...o-reconfiguration-core/src/test/java/org/cloudfoundry/reconfiguration/data/orm/HibernateConfigurerTest.java
  15. +83 −0 ...on/auto-reconfiguration-core/src/test/java/org/cloudfoundry/reconfiguration/data/orm/JpaConfigurerTest.java
  16. +117 −0 ...iguration-core/src/test/java/org/cloudfoundry/reconfiguration/data/relational/DataSourceConfigurerTest.java
  17. +19 −0 ...tion-core/src/test/resources/org/cloudfoundry/reconfiguration/data/keyvalue/test-multiple-redis-context.xml
  18. +13 −0 ...configuration-core/src/test/resources/org/cloudfoundry/reconfiguration/data/keyvalue/test-redis-context.xml
  19. 0  ...ation-core/src/test/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/hibernate.test.properties
  20. +1 −2  ...t/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/test-grails-embedded-props-good-context.xml
  21. +1 −2  ...st/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/test-grails-map-reference-good-context.xml
  22. +2 −3 .../org/cloudfoundry/reconfiguration/{ → data/orm}/test-grails-propertyFactory-reference-good-context.xml
  23. +1 −1  ...core/src/test/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/test-hibernate-good-context.xml
  24. +3 −3 ...ation-core/src/test/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/test-jpa-good-context.xml
  25. +3 −3 ...st/resources/org/cloudfoundry/reconfiguration/{ → data/orm}/test-jpa-typedStringValue-good-context.xml
  26. +2 −3 ...dry/reconfiguration/{ → data/orm}/test-propertyFactory-nested-reference-list-location-good-context.xml
  27. +2 −3 ...y/reconfiguration/{ → data/orm}/test-propertyFactory-nested-reference-single-location-good-context.xml
  28. +20 −0 ...iguration-core/src/test/resources/org/cloudfoundry/reconfiguration/data/relational/test-ds-good-context.xml
  29. +12 −1 vcap-java-test-app/src/main/java/org/cloudfoundry/test/ServiceController.java
  30. +5 −0 vcap-java-test-app/src/main/resources/META-INF/spring/root-context.xml
View
7 auto-reconfiguration/auto-reconfiguration-core/pom.xml
@@ -15,6 +15,7 @@
<properties>
<spring-data-mongo.version>1.0.0.M4</spring-data-mongo.version>
<spring.version>2.5.6</spring.version>
+ <spring-data-redis.version>1.0.0.M4</spring-data-redis.version>
</properties>
<dependencies>
<dependency>
@@ -151,6 +152,12 @@
<version>2.5</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-redis</artifactId>
+ <version>${spring-data-redis.version}</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
View
75 ...ration-core/src/main/java/org/cloudfoundry/reconfiguration/AbstractServiceConfigurer.java
@@ -0,0 +1,75 @@
+package org.cloudfoundry.reconfiguration;
+
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.cloudfoundry.runtime.env.CloudServiceException;
+import org.cloudfoundry.runtime.service.AbstractServiceCreator;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+
+abstract public class AbstractServiceConfigurer<SC extends AbstractServiceCreator<?,?>> implements Configurer {
+
+ private final Logger logger = Logger.getLogger(getClass().getName());
+
+ protected List<Map<String,Object>> cloudServices;
+
+ protected SC serviceCreator;
+
+ public AbstractServiceConfigurer(List<Map<String,Object>> cloudServices, SC serviceCreator) {
+ this.cloudServices = cloudServices;
+ this.serviceCreator = serviceCreator;
+ }
+
+ public boolean configure(DefaultListableBeanFactory beanFactory) {
+ Class<?> beanClass = loadClass(getBeanClass());
+ if(beanClass == null) {
+ logger.log(Level.INFO, "Class " + getBeanClass() + " not found. Skipping autostaging.");
+ return false;
+ }
+ String[] beanNames = beanFactory.getBeanNamesForType(beanClass);
+ if (beanNames.length == 0) {
+ logger.log(Level.INFO, "No beans of type " + getBeanClass() + " found in application context");
+ return false;
+ } else if (beanNames.length > 1) {
+ logger.log(Level.INFO, "More than 1 (" + beanNames.length
+ + ") beans of type " + getBeanClass() + "found in application context. Skipping autostaging.");
+ return false;
+ }
+ for (Map<String, Object> service : cloudServices) {
+ String label = (String) service.get("label");
+ if (label == null) {
+ continue;
+ }
+ if (label.startsWith(getServiceLabel())) {
+ try {
+ beanFactory.registerSingleton(getServiceBeanName(),
+ serviceCreator.createSingletonService().service);
+ beanFactory.removeBeanDefinition(beanNames[0]);
+ beanFactory.registerAlias(getServiceBeanName(), beanNames[0]);
+ return true;
+ } catch (CloudServiceException ex) {
+ logger.log(Level.INFO, "Error creating cloud service. Skipping autostaging", ex);
+ return false;
+ }
+ }
+ }
+ logger.log(Level.INFO, "No services found. Skipping autostaging");
+ return false;
+ }
+
+ abstract public String getServiceLabel();
+
+ abstract public String getBeanClass();
+
+ abstract public String getServiceBeanName();
+
+ protected Class<?> loadClass(String name) {
+ try {
+ return Class.forName(name);
+ } catch (Throwable ex) {
+ return null;
+ }
+ }
+}
View
15 .../main/java/org/cloudfoundry/reconfiguration/CloudAutoStagingBeanFactoryPostProcessor.java
@@ -4,6 +4,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -11,8 +12,13 @@
import javax.sql.DataSource;
import org.cloudfoundry.reconfiguration.data.document.MongoConfigurer;
+import org.cloudfoundry.reconfiguration.data.keyvalue.RedisConfigurer;
import org.cloudfoundry.reconfiguration.data.relational.DataSourceConfigurer;
import org.cloudfoundry.runtime.env.CloudEnvironment;
+import org.cloudfoundry.runtime.service.document.MongoServiceCreator;
+import org.cloudfoundry.runtime.service.keyvalue.RedisServiceCreator;
+import org.cloudfoundry.runtime.service.relational.MysqlServiceCreator;
+import org.cloudfoundry.runtime.service.relational.PostgresqlServiceCreator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -64,8 +70,13 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
} else {
cloudEnvironment = new CloudEnvironment();
}
- new DataSourceConfigurer(cloudEnvironment).configure( defaultListableBeanFactory);
- new MongoConfigurer(cloudEnvironment).configure(defaultListableBeanFactory);
+ final List<Map<String, Object>> cloudServices = cloudEnvironment.getServices();
+ new DataSourceConfigurer(cloudServices, new PostgresqlServiceCreator(cloudEnvironment),
+ new MysqlServiceCreator(cloudEnvironment)).configure(defaultListableBeanFactory);
+ new MongoConfigurer(cloudServices, new MongoServiceCreator(cloudEnvironment))
+ .configure(defaultListableBeanFactory);
+ new RedisConfigurer(cloudServices, new RedisServiceCreator(cloudEnvironment))
+ .configure(defaultListableBeanFactory);
}
/**
View
76 ...on-core/src/main/java/org/cloudfoundry/reconfiguration/data/document/MongoConfigurer.java
@@ -1,87 +1,43 @@
package org.cloudfoundry.reconfiguration.data.document;
+import java.util.List;
import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.cloudfoundry.reconfiguration.Configurer;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
-import org.cloudfoundry.runtime.env.CloudServiceException;
+import org.cloudfoundry.reconfiguration.AbstractServiceConfigurer;
import org.cloudfoundry.runtime.service.document.MongoServiceCreator;
-import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.data.mongodb.MongoDbFactory;
/**
- * Implementation of {@link Configurer} that replaces a single
+ * Implementation of {@link AbstractServiceConfigurer} that replaces a single
* {@link MongoDbFactory} with one connecting to a mongo cloud service bound to
* the current application.
*
* @author Jennifer Hickey
*
*/
-public class MongoConfigurer implements Configurer {
-
- private final Logger logger = Logger.getLogger(MongoConfigurer.class.getName());
+public class MongoConfigurer extends AbstractServiceConfigurer<MongoServiceCreator> {
static final String CF_MONGO_DB_FACTORY_NAME = "__cloudFoundryMongoDbFactory";
private static final String MONGO_DB_FACTORY_CLASS_NAME = "org.springframework.data.mongodb.MongoDbFactory";
- private CloudEnvironment cloudEnvironment;
-
- private MongoServiceCreator serviceCreator;
-
- public MongoConfigurer(CloudEnvironment cloudEnvironment) {
- this.cloudEnvironment = cloudEnvironment;
- this.serviceCreator = new MongoServiceCreator(cloudEnvironment);
+ public MongoConfigurer(List<Map<String, Object>> cloudServices, MongoServiceCreator serviceCreator) {
+ super(cloudServices, serviceCreator);
}
@Override
- public boolean configure(DefaultListableBeanFactory beanFactory) {
- Class<?> mongoDbFactoryClass = loadClass(MONGO_DB_FACTORY_CLASS_NAME);
- if (mongoDbFactoryClass == null) {
- logger.log(Level.INFO, "No MongoDbFactory class found.");
- return false;
- }
- String[] mongoDbFactoryBeanNames = beanFactory.getBeanNamesForType(mongoDbFactoryClass);
- if (mongoDbFactoryBeanNames.length == 0) {
- logger.log(Level.INFO, "No MongoDbFactory found in application context");
- return false;
- } else if (mongoDbFactoryBeanNames.length > 1) {
- logger.log(Level.INFO, "More than 1 (" + mongoDbFactoryBeanNames.length
- + ") MongoDbFactory beans found in application context. Skipping autostaging.");
- return false;
- }
- for (Map<String, Object> service : cloudEnvironment.getServices()) {
- String label = (String) service.get("label");
- if (label == null) {
- continue;
- }
- if (label.startsWith("mongodb")) {
- try {
- beanFactory.registerSingleton(CF_MONGO_DB_FACTORY_NAME,
- serviceCreator.createSingletonService().service);
- beanFactory.removeBeanDefinition(mongoDbFactoryBeanNames[0]);
- beanFactory.registerAlias(CF_MONGO_DB_FACTORY_NAME, mongoDbFactoryBeanNames[0]);
- return true;
- } catch (CloudServiceException ex) {
- logger.log(Level.INFO, "Multiple mongo services found. Skipping autostaging", ex);
- return false;
- }
- }
- }
- logger.log(Level.INFO, "No mongo service found. Skipping autostaging");
- return false;
+ public String getServiceLabel() {
+ return "mongodb";
}
- private Class<?> loadClass(String name) {
- try {
- return Class.forName(name);
- } catch (Throwable ex) {
- return null;
- }
+ @Override
+ public String getBeanClass() {
+ return MONGO_DB_FACTORY_CLASS_NAME;
}
- public void setServiceCreator(MongoServiceCreator serviceCreator) {
- this.serviceCreator = serviceCreator;
+ @Override
+ public String getServiceBeanName() {
+ return CF_MONGO_DB_FACTORY_NAME;
}
+
}
View
43 ...on-core/src/main/java/org/cloudfoundry/reconfiguration/data/keyvalue/RedisConfigurer.java
@@ -0,0 +1,43 @@
+package org.cloudfoundry.reconfiguration.data.keyvalue;
+
+import java.util.List;
+import java.util.Map;
+
+import org.cloudfoundry.reconfiguration.AbstractServiceConfigurer;
+import org.cloudfoundry.runtime.service.keyvalue.RedisServiceCreator;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+
+/**
+ * Implementation of {@link AbstractServiceConfigurer} that replaces a single
+ * {@link RedisConnectionFactory} with one connecting to a redis cloud service
+ * bound to the current application.
+ *
+ * @author Jennifer Hickey
+ *
+ */
+public class RedisConfigurer extends AbstractServiceConfigurer<RedisServiceCreator> {
+
+ private static final String CF_REDIS_CONN_FACTORY_NAME = "__cloudFoundryRedisConnectionFactory";
+
+ private static final String REDIS_CONN_FACTORY_CLASS_NAME = "org.springframework.data.redis.connection.RedisConnectionFactory";
+
+ public RedisConfigurer(List<Map<String, Object>> cloudServices, RedisServiceCreator serviceCreator) {
+ super(cloudServices, serviceCreator);
+ }
+
+ @Override
+ public String getServiceLabel() {
+ return "redis";
+ }
+
+ @Override
+ public String getBeanClass() {
+ return REDIS_CONN_FACTORY_CLASS_NAME;
+ }
+
+ @Override
+ public String getServiceBeanName() {
+ return CF_REDIS_CONN_FACTORY_NAME;
+ }
+
+}
View
10 ...ion-core/src/main/java/org/cloudfoundry/reconfiguration/data/orm/HibernateConfigurer.java
@@ -3,11 +3,11 @@
*/
package org.cloudfoundry.reconfiguration.data.orm;
+import java.util.List;
import java.util.Map;
import org.cloudfoundry.reconfiguration.Configurer;
import org.cloudfoundry.reconfiguration.PropertyReplacer;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
/**
@@ -28,15 +28,15 @@
private PropertyReplacer propertyReplacer = new PropertyReplacer();
- private CloudEnvironment cloudEnvironment;
+ private List<Map<String,Object>> cloudServices;
- public HibernateConfigurer(CloudEnvironment cloudEnvironment) {
- this.cloudEnvironment = cloudEnvironment;
+ public HibernateConfigurer(List<Map<String,Object>> cloudServices) {
+ this.cloudServices = cloudServices;
}
public boolean configure(DefaultListableBeanFactory beanFactory) {
boolean configured = false;
- for (Map<String, Object> service : cloudEnvironment.getServices()) {
+ for (Map<String, Object> service : cloudServices) {
String label = (String) service.get("label");
if (label == null) {
continue;
View
10 ...iguration-core/src/main/java/org/cloudfoundry/reconfiguration/data/orm/JpaConfigurer.java
@@ -3,11 +3,11 @@
*/
package org.cloudfoundry.reconfiguration.data.orm;
+import java.util.List;
import java.util.Map;
import org.cloudfoundry.reconfiguration.Configurer;
import org.cloudfoundry.reconfiguration.PropertyReplacer;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
/**
@@ -28,15 +28,15 @@
private PropertyReplacer propertyReplacer = new PropertyReplacer();
- private CloudEnvironment cloudEnvironment;
+ private List<Map<String,Object>> cloudServices;
- public JpaConfigurer(CloudEnvironment cloudEnvironment) {
- this.cloudEnvironment = cloudEnvironment;
+ public JpaConfigurer(List<Map<String,Object>> cloudServices) {
+ this.cloudServices = cloudServices;
}
public boolean configure(DefaultListableBeanFactory beanFactory) {
boolean configured = false;
- for (Map<String, Object> service : cloudEnvironment.getServices()) {
+ for (Map<String, Object> service : cloudServices) {
String label = (String) service.get("label");
if (label == null) {
View
30 .../src/main/java/org/cloudfoundry/reconfiguration/data/relational/DataSourceConfigurer.java
@@ -14,7 +14,6 @@
import org.cloudfoundry.reconfiguration.Configurer;
import org.cloudfoundry.reconfiguration.data.orm.HibernateConfigurer;
import org.cloudfoundry.reconfiguration.data.orm.JpaConfigurer;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.cloudfoundry.runtime.env.CloudServiceException;
import org.cloudfoundry.runtime.service.relational.MysqlServiceCreator;
import org.cloudfoundry.runtime.service.relational.PostgresqlServiceCreator;
@@ -26,8 +25,8 @@
/**
* Implementation of {@link Configurer} that replaces a single
- * {@link DataSource} with one connecting to a postgresql or mysql cloud service bound
- * to the current application.
+ * {@link DataSource} with one connecting to a postgresql or mysql cloud service
+ * bound to the current application.
*
* @author Ramnivas Laddad
* @author Xin Li
@@ -44,12 +43,19 @@
private Configurer jpaConfigurer;
- private CloudEnvironment cloudEnvironment;
+ private List<Map<String, Object>> cloudServices;
- public DataSourceConfigurer(CloudEnvironment cloudEnvironment) {
- this.cloudEnvironment = cloudEnvironment;
- this.hibernateConfigurer = new HibernateConfigurer(cloudEnvironment);
- this.jpaConfigurer = new JpaConfigurer(cloudEnvironment);
+ private PostgresqlServiceCreator postgresqlServiceCreator;
+
+ private MysqlServiceCreator mysqlServiceCreator;
+
+ public DataSourceConfigurer(List<Map<String, Object>> cloudServices,
+ PostgresqlServiceCreator postgresqlServiceCreator, MysqlServiceCreator mysqlServiceCreator) {
+ this.cloudServices = cloudServices;
+ this.postgresqlServiceCreator = postgresqlServiceCreator;
+ this.mysqlServiceCreator = mysqlServiceCreator;
+ this.hibernateConfigurer = new HibernateConfigurer(cloudServices);
+ this.jpaConfigurer = new JpaConfigurer(cloudServices);
}
public boolean configure(DefaultListableBeanFactory defaultListableBeanFactory) {
@@ -64,7 +70,7 @@ public boolean configure(DefaultListableBeanFactory defaultListableBeanFactory)
}
ArrayList<DataSource> dataSourceList = new ArrayList<DataSource>();
- for (Map<String, Object> service : cloudEnvironment.getServices()) {
+ for (Map<String, Object> service : cloudServices) {
String label = (String) service.get("label");
if (label == null) {
continue;
@@ -72,16 +78,14 @@ public boolean configure(DefaultListableBeanFactory defaultListableBeanFactory)
if (label.startsWith("postgresql")) {
try {
- PostgresqlServiceCreator postgresqlCreationHelper = new PostgresqlServiceCreator(cloudEnvironment);
- dataSourceList.add(postgresqlCreationHelper.createSingletonService().service);
+ dataSourceList.add(postgresqlServiceCreator.createSingletonService().service);
} catch (CloudServiceException ex) {
logger.log(Level.INFO, "Multiple database services found. Skipping autostaging", ex);
return false;
}
} else if (label.startsWith("mysql")) {
try {
- MysqlServiceCreator mysqlCreationHelper = new MysqlServiceCreator(cloudEnvironment);
- dataSourceList.add(mysqlCreationHelper.createSingletonService().service);
+ dataSourceList.add(mysqlServiceCreator.createSingletonService().service);
} catch (CloudServiceException ex) {
logger.log(Level.INFO, "Multiple database services found. Skipping autostaging");
return false;
View
114 ...t/java/org/cloudfoundry/reconfiguration/CloudAutoStagingBeanFactoryPostProcessorTest.java
@@ -0,0 +1,114 @@
+package org.cloudfoundry.reconfiguration;
+
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+
+import org.cloudfoundry.runtime.service.AbstractCloudServiceFactory;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.context.ApplicationContext;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+/**
+ * Unit test of the {@link CloudAutoStagingBeanFactoryPostProcessor}
+ *
+ * @author Jennifer Hickey
+ *
+ */
+public class CloudAutoStagingBeanFactoryPostProcessorTest {
+
+ @Mock
+ private ConfigurableListableBeanFactory beanFactory;
+ @Mock
+ private ApplicationContext applicationContext;
+ @Mock
+ private Resource cloudServicesFile;
+
+ private CloudAutoStagingBeanFactoryPostProcessor testBFPP;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ testBFPP = new CloudAutoStagingBeanFactoryPostProcessor();
+ }
+
+ @Test
+ public void autoStagingOffCloudServiceFound() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-1";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(
+ new Resource[] { new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1") });
+ String[] beans = new String[] { "intBean" };
+ when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
+ when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(beans);
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertTrue(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffCloudServiceFoundMultipleFiles() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-*";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(
+ new Resource[] { new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1"),
+ new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-2") });
+ String[] beans = new String[] { "stringBean" };
+ when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
+ when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(new String[0]);
+ when(beanFactory.getBeanNamesForType(String.class)).thenReturn(beans);
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertTrue(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffCloudServiceNotFound() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-1";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(
+ new Resource[] { new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1") });
+ when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
+ when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(new String[0]);
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffCloudServiceClassNotFound() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-invalid-cloud-services";
+ when(applicationContext.getResources(cloudServicesFileLocation))
+ .thenReturn(
+ new Resource[] { new ClassPathResource(
+ "/org/cloudfoundry/reconfiguration/test-invalid-cloud-services") });
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffCloudServiceFilesNotFound() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[0]);
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffErrorLookingForCloudServicesFile() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenThrow(new IOException());
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+
+ @Test
+ public void autoStagingOffErrorReadingCloudServicesFile() throws IOException {
+ String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
+ when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(
+ new Resource[] { cloudServicesFile });
+ when(cloudServicesFile.getInputStream()).thenThrow(new IOException());
+ testBFPP.setApplicationContext(applicationContext);
+ Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation, beanFactory));
+ }
+}
View
38 ...tion-core/src/test/java/org/cloudfoundry/reconfiguration/CloudEnvironmentMockingTest.java
@@ -0,0 +1,38 @@
+package org.cloudfoundry.reconfiguration;
+
+import org.cloudfoundry.runtime.env.CloudEnvironment;
+import org.junit.Before;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Base class for auto-reconfig tests that instantiate an application context
+ * and need to mock the {@link CloudEnvironment} bean
+ *
+ * @author Jennifer Hickey
+ *
+ */
+abstract public class CloudEnvironmentMockingTest {
+
+ @Mock
+ protected CloudEnvironment mockEnvironment;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ protected ClassPathXmlApplicationContext getTestApplicationContext(String fileName) {
+ return new ClassPathXmlApplicationContext(new String[] {
+ getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + fileName,
+ "META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml" }) {
+ @Override
+ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
+ getBeanFactory().registerSingleton("test-cloudEnvironment", mockEnvironment);
+ super.prepareBeanFactory(beanFactory);
+ }
+ };
+ }
+}
View
337 ...org/cloudfoundry/reconfiguration/CloudFactoryAutoStagingBeanFactoryPostProcessorTest.java
@@ -1,337 +0,0 @@
-package org.cloudfoundry.reconfiguration;
-
-import static org.mockito.Mockito.when;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.sql.DataSource;
-
-import org.cloudfoundry.runtime.env.AbstractDataSourceServiceInfo;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
-import org.cloudfoundry.runtime.env.MysqlServiceInfo;
-import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
-import org.cloudfoundry.runtime.service.AbstractCloudServiceFactory;
-import org.hibernate.SessionFactory;
-import org.hibernate.impl.SessionFactoryImpl;
-import org.hibernate.impl.SessionImpl;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.Resource;
-import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
-
-/**
- * Unit-tests for {@link CloudAutoStagingBeanFactoryPostProcessor}.
- * <p>
- * This test uses a mock application context to introduce environment with services
- * to avoid the need of actual working application and services. The assertions
- * are made to check if the actual beans got replaced/not replaced with the mock beans.
- *
- * @author Ramnivas Laddad
- * @author Jennifer Hickey
- *
- */
-public class CloudFactoryAutoStagingBeanFactoryPostProcessorTest {
- @Mock private MysqlServiceInfo mockMysqlServiceInfo;
- @Mock private PostgresqlServiceInfo mockPostgresqlServiceInfo;
- @Mock private CloudEnvironment mockEnvironment;
- @Mock private ConfigurableListableBeanFactory beanFactory;
- @Mock private ApplicationContext applicationContext;
- @Mock private Resource cloudServicesFile;
-
- private CloudAutoStagingBeanFactoryPostProcessor testBFPP;
-
- @Before
- public void setup() {
- MockitoAnnotations.initMocks(this);
- testBFPP = new CloudAutoStagingBeanFactoryPostProcessor();
- }
-
- @Test
- public void cloudDataSourceReplacesUserDataSourceIfMySqlServiceDetected() {
- String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
- List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
- serviceInfos.add(mockMysqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "mysql-5.1");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs", DataSource.class);
- Assert.assertEquals(serviceJdbcUrl, replacedDataSource.getUrl());
- }
-
- @Test
- public void cloudDataSourceReplacesUserDataSourceIfPostgreSqlServiceDetected() {
- String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/mydb-1";
- List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
- serviceInfos.add(mockPostgresqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "postgresql-9.0");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs", DataSource.class);
- Assert.assertEquals(serviceJdbcUrl, replacedDataSource.getUrl());
- }
-
- @Test
- public void cloudDataSourceLeavesOriginalInPlaceIfMultipleServicesDetected() {
- String serviceJdbcUrl1 = "jdbc:mysql://10.20.20.40:1234/mysql-1";
- String serviceJdbcUrl2 = "jdbc:postgresql://10.20.20.40:5432/pg-2";
- List<AbstractDataSourceServiceInfo> serviceInfos = new ArrayList<AbstractDataSourceServiceInfo>();
- serviceInfos.add(mockMysqlServiceInfo);
- serviceInfos.add(mockPostgresqlServiceInfo);
- Map<String, Object> service1 = new HashMap<String, Object>();
- service1.put("label", "mysql-5.1");
- Map<String, Object> service2 = new HashMap<String, Object>();
- service2.put("label", "postgresql-9.0");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service1);
- serviceList.add(service2);
- when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl1);
- when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl2);
- when(mockEnvironment.getServiceInfos(AbstractDataSourceServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs", DataSource.class);
- Assert.assertEquals("jdbc:hsql:localdb", replacedDataSource.getUrl());
- }
-
- @Test
- public void cloudDataSourceLeavesOriginalInPlaceIfNoServicesDetected() {
- List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "mysql-5.1");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs", DataSource.class);
- Assert.assertEquals("jdbc:hsql:localdb", replacedDataSource.getUrl());
- }
-
- @Test
- public void hibernateSessionFactoryDialectUpdated() {
- assertApplicationContextProcessingForMysql("test-hibernate-good-context.xml");
- }
-
- @Test
- public void entityManagerFactoryMysqlDialectUpdated() {
- String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
- List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
- serviceInfos.add(mockMysqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "mysql-5.1");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- EntityManagerFactory entityManagerFactory = (EntityManagerFactory) context.getBean("entityManagerFactory", EntityManagerFactory.class);
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- SessionImpl entityManagerDelegate = (SessionImpl) entityManager.getDelegate();
- SessionFactoryImpl underlyingSessionFactory = (SessionFactoryImpl) entityManagerDelegate.getSessionFactory();
- Assert.assertEquals("org.hibernate.dialect.MySQLDialect", underlyingSessionFactory.getDialect().toString());
- }
-
- @Test
- public void entityManagerFactoryPostgresqlDialectUpdated() {
- String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/pg-1";
- List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
- serviceInfos.add(mockPostgresqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "postgresql-9.0");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
- EntityManagerFactory entityManagerFactory = (EntityManagerFactory) context.getBean("entityManagerFactory", EntityManagerFactory.class);
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- SessionImpl entityManagerDelegate = (SessionImpl) entityManager.getDelegate();
- SessionFactoryImpl underlyingSessionFactory = (SessionFactoryImpl) entityManagerDelegate.getSessionFactory();
- Assert.assertEquals("org.hibernate.dialect.PostgreSQLDialect", underlyingSessionFactory.getDialect().toString());
- }
-
- @Test
- public void grailsLikeEmbeddedPropertyApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-grails-embedded-props-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-grails-embedded-props-good-context.xml");
- }
-
- @Test
- public void grailsLikeReferencedMapApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-grails-map-reference-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-grails-map-reference-good-context.xml");
- }
-
- @Test
- public void grailsLikeReferencedPropertyFactoryApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-grails-propertyFactory-reference-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-grails-propertyFactory-reference-good-context.xml");
- }
-
- @Test
- public void typeStringValuePropertyApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-jpa-typedStringValue-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-jpa-typedStringValue-good-context.xml");
- }
-
- @Test
- public void referencedNestedPropertyFactorySingleLocationApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-propertyFactory-nested-reference-single-location-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-propertyFactory-nested-reference-single-location-good-context.xml");
- }
-
- @Test
- public void referencedNestedPropertyFactoryListLocationApplicationContextProcessed() {
- assertApplicationContextProcessingForMysql("test-propertyFactory-nested-reference-list-location-good-context.xml");
- assertApplicationContextProcessingForPostgresql("test-propertyFactory-nested-reference-list-location-good-context.xml");
- }
-
- @Test
- public void autoStagingOffCloudServiceFound() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-1";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[] {
- new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1")} );
- String[] beans = new String[] {"intBean"};
- when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
- when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(beans);
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertTrue(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
- @Test
- public void autoStagingOffCloudServiceFoundMultipleFiles() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-*";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[] {
- new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1"), new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-2")} );
- String[] beans = new String[] {"stringBean"};
- when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
- when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(new String[0]);
- when(beanFactory.getBeanNamesForType(String.class)).thenReturn(beans);
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertTrue(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- @Test
- public void autoStagingOffCloudServiceNotFound() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-cloud-services-1";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[] {
- new ClassPathResource("/org/cloudfoundry/reconfiguration/test-cloud-services-1")} );
- when(beanFactory.getBeanNamesForType(AbstractCloudServiceFactory.class)).thenReturn(new String[0]);
- when(beanFactory.getBeanNamesForType(Integer.class)).thenReturn(new String[0]);
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- @Test
- public void autoStagingOffCloudServiceClassNotFound() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-invalid-cloud-services";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[] {
- new ClassPathResource("/org/cloudfoundry/reconfiguration/test-invalid-cloud-services")} );
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- @Test
- public void autoStagingOffCloudServiceFilesNotFound() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[0]);
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- @Test
- public void autoStagingOffErrorLookingForCloudServicesFile() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenThrow(new IOException());
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- @Test
- public void autoStagingOffErrorReadingCloudServicesFile() throws IOException {
- String cloudServicesFileLocation = "classpath:/org/cloudfoundry/reconfiguration/test-nonexistent-cloud-services";
- when(applicationContext.getResources(cloudServicesFileLocation)).thenReturn(new Resource[] {cloudServicesFile});
- when(cloudServicesFile.getInputStream()).thenThrow(new IOException());
- testBFPP.setApplicationContext(applicationContext);
- Assert.assertFalse(testBFPP.autoStagingOff(cloudServicesFileLocation,beanFactory));
- }
-
- private void assertApplicationContextProcessingForMysql(String appContextFile) {
- String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
- List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
- serviceInfos.add(mockMysqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "mysql-5.1");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext(appContextFile);
- SessionFactoryImpl sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory", SessionFactory.class);
-
- Assert.assertEquals("org.hibernate.dialect.MySQLDialect", sessionFactory.getDialect().toString());
-
- }
-
- private void assertApplicationContextProcessingForPostgresql(String appContextFile) {
- String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/pg-1";
- List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
- serviceInfos.add(mockPostgresqlServiceInfo);
- Map<String, Object> service = new HashMap<String, Object>();
- service.put("label", "postgresql-9.0");
- List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
- serviceList.add(service);
- when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
- when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
-
- ApplicationContext context = getTestApplicationContext(appContextFile);
- SessionFactoryImpl sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory", SessionFactory.class);
-
- Assert.assertEquals("org.hibernate.dialect.PostgreSQLDialect", sessionFactory.getDialect().toString());
- }
-
- private ClassPathXmlApplicationContext getTestApplicationContext(String fileName) {
- return new ClassPathXmlApplicationContext(new String[]{"org/cloudfoundry/reconfiguration/" + fileName,
- "META-INF/cloud/cloudfoundry-auto-reconfiguration-context.xml"}) {
- @Override
- protected void prepareBeanFactory(
- ConfigurableListableBeanFactory beanFactory) {
- getBeanFactory().registerSingleton("test-cloudEnvironment", mockEnvironment);
- super.prepareBeanFactory(beanFactory);
- }
- };
- }
-}
View
41 ...ore/src/test/java/org/cloudfoundry/reconfiguration/data/document/MongoConfigurerTest.java
@@ -1,12 +1,12 @@
package org.cloudfoundry.reconfiguration.data.document;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.never;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.net.UnknownHostException;
import java.util.ArrayList;
@@ -14,7 +14,6 @@
import java.util.List;
import java.util.Map;
-import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.cloudfoundry.runtime.env.CloudServiceException;
import org.cloudfoundry.runtime.service.AbstractServiceCreator.ServiceNameTuple;
import org.cloudfoundry.runtime.service.document.MongoServiceCreator;
@@ -44,21 +43,14 @@
public class MongoConfigurerTest {
@Mock
- private CloudEnvironment mockEnvironment;
-
- @Mock
private DefaultListableBeanFactory beanFactory;
@Mock
private MongoServiceCreator serviceCreator;
- private MongoConfigurer mongoConfigurer;
-
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
- this.mongoConfigurer = new MongoConfigurer(mockEnvironment);
- this.mongoConfigurer.setServiceCreator(serviceCreator);
}
@Test
@@ -72,7 +64,7 @@ public void replacesUserDbFactory() throws UnknownHostException, MongoException
service.put("label", "mongodb-1.8");
List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
serviceList.add(service);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
+ MongoConfigurer mongoConfigurer = new MongoConfigurer(serviceList, serviceCreator);
when(beanFactory.getBeanNamesForType(MongoDbFactory.class)).thenReturn(new String[] { mongoBeanName });
when(serviceCreator.createSingletonService()).thenReturn(expectedService);
assertTrue(mongoConfigurer.configure(beanFactory));
@@ -83,6 +75,7 @@ public void replacesUserDbFactory() throws UnknownHostException, MongoException
@Test
public void leavesOriginalInPlaceIfMultipleBeansDetected() {
+ MongoConfigurer mongoConfigurer = new MongoConfigurer(new ArrayList<Map<String, Object>>(), serviceCreator);
when(beanFactory.getBeanNamesForType(MongoDbFactory.class)).thenReturn(new String[] { "bean1", "bean2" });
assertFalse(mongoConfigurer.configure(beanFactory));
verify(beanFactory, never()).registerSingleton(eq(MongoConfigurer.CF_MONGO_DB_FACTORY_NAME),
@@ -98,7 +91,7 @@ public void leavesOriginalInPlaceIfMultipleServicesDetected() {
service.put("label", "mongodb-1.8");
List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
serviceList.add(service);
- when(mockEnvironment.getServices()).thenReturn(serviceList);
+ MongoConfigurer mongoConfigurer = new MongoConfigurer(serviceList, serviceCreator);
when(beanFactory.getBeanNamesForType(MongoDbFactory.class)).thenReturn(new String[] { mongoBeanName });
when(serviceCreator.createSingletonService()).thenThrow(new CloudServiceException("Multiple services"));
assertFalse(mongoConfigurer.configure(beanFactory));
@@ -113,7 +106,7 @@ public void leavesOriginalInPlaceIfNoServicesDetected() {
String mongoBeanName = "testMongoDb";
when(beanFactory.getBeanNamesForType(MongoDbFactory.class)).thenReturn(new String[] { mongoBeanName });
List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
- when(mockEnvironment.getServices()).thenReturn(serviceList);
+ MongoConfigurer mongoConfigurer = new MongoConfigurer(serviceList, serviceCreator);
assertFalse(mongoConfigurer.configure(beanFactory));
verify(beanFactory, never()).registerSingleton(eq(MongoConfigurer.CF_MONGO_DB_FACTORY_NAME),
isA(MongoDbFactory.class));
@@ -123,6 +116,7 @@ public void leavesOriginalInPlaceIfNoServicesDetected() {
@Test
public void doesNothingIfNoMongoBeansDetected() {
+ MongoConfigurer mongoConfigurer = new MongoConfigurer(new ArrayList<Map<String, Object>>(), serviceCreator);
when(beanFactory.getBeanNamesForType(MongoDbFactory.class)).thenReturn(new String[0]);
assertFalse(mongoConfigurer.configure(beanFactory));
verify(beanFactory, never()).registerSingleton(eq(MongoConfigurer.CF_MONGO_DB_FACTORY_NAME),
@@ -131,4 +125,21 @@ public void doesNothingIfNoMongoBeansDetected() {
verify(beanFactory, never()).registerAlias(eq(MongoConfigurer.CF_MONGO_DB_FACTORY_NAME), isA(String.class));
}
+ @Test
+ public void doesNothingIfMongoDbFactoryClassNotFound() {
+ MongoConfigurer configurer = new StubMongoConfigurer(new ArrayList<Map<String, Object>>(), serviceCreator);
+ assertFalse(configurer.configure(beanFactory));
+ }
+
+ private class StubMongoConfigurer extends MongoConfigurer {
+ public StubMongoConfigurer(List<Map<String, Object>> cloudServices, MongoServiceCreator serviceCreator) {
+ super(cloudServices, serviceCreator);
+ }
+
+ @Override
+ protected Class<?> loadClass(String name) {
+ return null;
+ }
+ }
+
}
View
137 ...ore/src/test/java/org/cloudfoundry/reconfiguration/data/keyvalue/RedisConfigurerTest.java
@@ -0,0 +1,137 @@
+package org.cloudfoundry.reconfiguration.data.keyvalue;
+
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.cloudfoundry.reconfiguration.CloudEnvironmentMockingTest;
+import org.cloudfoundry.runtime.env.RedisServiceInfo;
+import org.cloudfoundry.runtime.service.keyvalue.RedisServiceCreator;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.context.ApplicationContext;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
+
+/**
+ * Test of {@link RedisConfigurer}
+ * <p>
+ * Integration tests bootstrap application contexts containing various
+ * {@link RedisConnectionFactory} beans and check if the actual beans got
+ * replaced/not replaced with the mock beans.
+ * <p>
+ * Unit test verifies auto-reconfig behavior when the {@link RedisConnectionFactory}
+ * class is not found on the classpath
+ *
+ *
+ * @author Jennifer Hickey
+ *
+ */
+public class RedisConfigurerTest extends CloudEnvironmentMockingTest {
+ @Mock
+ private RedisServiceInfo mockRedisServiceInfo;
+ @Mock
+ private RedisServiceInfo mockRedisServiceInfo2;
+
+ @Mock
+ private DefaultListableBeanFactory beanFactory;
+
+ @Mock
+ private RedisServiceCreator serviceCreator;
+
+ @Test
+ public void cloudRedisConnFactoryReplacesUserConnFactory() {
+ List<RedisServiceInfo> serviceInfos = new ArrayList<RedisServiceInfo>();
+ serviceInfos.add(mockRedisServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "redis-2.2");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockRedisServiceInfo.getHost()).thenReturn("cloudhost");
+ when(mockRedisServiceInfo.getPassword()).thenReturn("mypass");
+ when(mockRedisServiceInfo.getPort()).thenReturn(1234);
+ when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-redis-context.xml");
+ JedisConnectionFactory replacedConnFactory = (JedisConnectionFactory) context.getBean("redisConnection",
+ RedisConnectionFactory.class);
+ Assert.assertEquals("cloudhost", replacedConnFactory.getHostName());
+ Assert.assertEquals("mypass", replacedConnFactory.getPassword());
+ Assert.assertEquals(1234, replacedConnFactory.getPort());
+ }
+
+ @Test
+ public void cloudRedisConnFactoryLeavesOriginalInPlaceIfMultipleBeansDetected() {
+ List<RedisServiceInfo> serviceInfos = new ArrayList<RedisServiceInfo>();
+ serviceInfos.add(mockRedisServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "redis-2.2");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-multiple-redis-context.xml");
+ JedisConnectionFactory replacedConnFactory = (JedisConnectionFactory) context.getBean("redisConnection",
+ RedisConnectionFactory.class);
+ Assert.assertEquals("localhost", replacedConnFactory.getHostName());
+ Assert.assertEquals("localpass", replacedConnFactory.getPassword());
+ Assert.assertEquals(6789, replacedConnFactory.getPort());
+ }
+
+ @Test
+ public void cloudRedisConnFactoryLeavesOriginalInPlaceIfMultipleServicesDetected() {
+ List<RedisServiceInfo> serviceInfos = new ArrayList<RedisServiceInfo>();
+ serviceInfos.add(mockRedisServiceInfo);
+ serviceInfos.add(mockRedisServiceInfo2);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "redis-2.2");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+ ApplicationContext context = getTestApplicationContext("test-redis-context.xml");
+ JedisConnectionFactory replacedConnFactory = (JedisConnectionFactory) context.getBean("redisConnection",
+ RedisConnectionFactory.class);
+ Assert.assertEquals("localhost", replacedConnFactory.getHostName());
+ Assert.assertEquals("localpass", replacedConnFactory.getPassword());
+ Assert.assertEquals(6789, replacedConnFactory.getPort());
+ }
+
+ @Test
+ public void cloudRedisConnFactoryLeavesOriginalInPlaceIfNoServicesDetected() {
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+ ApplicationContext context = getTestApplicationContext("test-redis-context.xml");
+ JedisConnectionFactory replacedConnFactory = (JedisConnectionFactory) context.getBean("redisConnection",
+ RedisConnectionFactory.class);
+ Assert.assertEquals("localhost", replacedConnFactory.getHostName());
+ Assert.assertEquals("localpass", replacedConnFactory.getPassword());
+ Assert.assertEquals(6789, replacedConnFactory.getPort());
+ }
+
+ @Test
+ public void doesNothingIfRedisConnFactoryClassNotFound() {
+ RedisConfigurer configurer = new StubRedisConfigurer(new ArrayList<Map<String,Object>>(), serviceCreator);
+ assertFalse(configurer.configure(beanFactory));
+ }
+
+ private class StubRedisConfigurer extends RedisConfigurer {
+ public StubRedisConfigurer(List<Map<String, Object>> cloudServices, RedisServiceCreator serviceCreator) {
+ super(cloudServices, serviceCreator);
+ }
+
+ @Override
+ protected Class<?> loadClass(String name) {
+ return null;
+ }
+ }
+
+}
View
116 ...core/src/test/java/org/cloudfoundry/reconfiguration/data/orm/HibernateConfigurerTest.java
@@ -0,0 +1,116 @@
+package org.cloudfoundry.reconfiguration.data.orm;
+
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.cloudfoundry.reconfiguration.CloudEnvironmentMockingTest;
+import org.cloudfoundry.runtime.env.MysqlServiceInfo;
+import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
+import org.hibernate.SessionFactory;
+import org.hibernate.impl.SessionFactoryImpl;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * Integration test of the {@link HibernateConfigurer}
+ * <p>
+ * Bootstraps application contexts containing various DataSource and
+ * Hibernate beans and checks if the actual beans got replaced/not replaced with
+ * the mock beans.
+ *
+ * @author Ramnivas Laddad
+ * @author Jennifer Hickey
+ *
+ */
+public class HibernateConfigurerTest extends CloudEnvironmentMockingTest {
+
+ @Mock
+ private MysqlServiceInfo mockMysqlServiceInfo;
+ @Mock
+ private PostgresqlServiceInfo mockPostgresqlServiceInfo;
+
+ @Test
+ public void hibernateSessionFactoryDialectUpdated() {
+ assertApplicationContextProcessingForMysql("test-hibernate-good-context.xml");
+ }
+
+ @Test
+ public void grailsLikeEmbeddedPropertyApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-grails-embedded-props-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-grails-embedded-props-good-context.xml");
+ }
+
+ @Test
+ public void grailsLikeReferencedMapApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-grails-map-reference-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-grails-map-reference-good-context.xml");
+ }
+
+ @Test
+ public void grailsLikeReferencedPropertyFactoryApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-grails-propertyFactory-reference-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-grails-propertyFactory-reference-good-context.xml");
+ }
+
+ @Test
+ public void referencedNestedPropertyFactorySingleLocationApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-propertyFactory-nested-reference-single-location-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-propertyFactory-nested-reference-single-location-good-context.xml");
+ }
+
+ @Test
+ public void referencedNestedPropertyFactoryListLocationApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-propertyFactory-nested-reference-list-location-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-propertyFactory-nested-reference-list-location-good-context.xml");
+ }
+
+ @Test
+ public void typeStringValuePropertyApplicationContextProcessed() {
+ assertApplicationContextProcessingForMysql("test-jpa-typedStringValue-good-context.xml");
+ assertApplicationContextProcessingForPostgresql("test-jpa-typedStringValue-good-context.xml");
+ }
+
+ private void assertApplicationContextProcessingForMysql(String appContextFile) {
+ String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
+ List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
+ serviceInfos.add(mockMysqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "mysql-5.1");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext(appContextFile);
+ SessionFactoryImpl sessionFactory = (SessionFactoryImpl) context
+ .getBean("sessionFactory", SessionFactory.class);
+
+ Assert.assertEquals("org.hibernate.dialect.MySQLDialect", sessionFactory.getDialect().toString());
+ }
+
+ private void assertApplicationContextProcessingForPostgresql(String appContextFile) {
+ String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/pg-1";
+ List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
+ serviceInfos.add(mockPostgresqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "postgresql-9.0");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext(appContextFile);
+ SessionFactoryImpl sessionFactory = (SessionFactoryImpl) context
+ .getBean("sessionFactory", SessionFactory.class);
+
+ Assert.assertEquals("org.hibernate.dialect.PostgreSQLDialect", sessionFactory.getDialect().toString());
+ }
+}
View
83 ...ation-core/src/test/java/org/cloudfoundry/reconfiguration/data/orm/JpaConfigurerTest.java
@@ -0,0 +1,83 @@
+package org.cloudfoundry.reconfiguration.data.orm;
+
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.cloudfoundry.reconfiguration.CloudEnvironmentMockingTest;
+import org.cloudfoundry.runtime.env.MysqlServiceInfo;
+import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
+import org.hibernate.impl.SessionFactoryImpl;
+import org.hibernate.impl.SessionImpl;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * Integration test of the {@link JpaConfigurer}
+ * <p>
+ * Bootstraps application contexts containing various DataSource, JPA, and
+ * Hibernate beans and checks if the actual beans got replaced/not replaced with
+ * the mock beans.
+ *
+ * @author Ramnivas Laddad
+ * @author Jennifer Hickey
+ *
+ */
+public class JpaConfigurerTest extends CloudEnvironmentMockingTest {
+
+ @Mock private MysqlServiceInfo mockMysqlServiceInfo;
+ @Mock private PostgresqlServiceInfo mockPostgresqlServiceInfo;
+
+ @Test
+ public void entityManagerFactoryMysqlDialectUpdated() {
+ String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
+ List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
+ serviceInfos.add(mockMysqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "mysql-5.1");
+ List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
+ serviceList.add(service);
+ when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
+ EntityManagerFactory entityManagerFactory = (EntityManagerFactory) context.getBean("entityManagerFactory", EntityManagerFactory.class);
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ SessionImpl entityManagerDelegate = (SessionImpl) entityManager.getDelegate();
+ SessionFactoryImpl underlyingSessionFactory = (SessionFactoryImpl) entityManagerDelegate.getSessionFactory();
+ Assert.assertEquals("org.hibernate.dialect.MySQLDialect", underlyingSessionFactory.getDialect().toString());
+ }
+
+ @Test
+ public void entityManagerFactoryPostgresqlDialectUpdated() {
+ String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/pg-1";
+ List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
+ serviceInfos.add(mockPostgresqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "postgresql-9.0");
+ List<Map<String,Object>> serviceList = new ArrayList<Map<String,Object>>();
+ serviceList.add(service);
+ when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-jpa-good-context.xml");
+ EntityManagerFactory entityManagerFactory = (EntityManagerFactory) context.getBean("entityManagerFactory", EntityManagerFactory.class);
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ SessionImpl entityManagerDelegate = (SessionImpl) entityManager.getDelegate();
+ SessionFactoryImpl underlyingSessionFactory = (SessionFactoryImpl) entityManagerDelegate.getSessionFactory();
+ Assert.assertEquals("org.hibernate.dialect.PostgreSQLDialect", underlyingSessionFactory.getDialect().toString());
+ }
+
+
+
+}
View
117 .../test/java/org/cloudfoundry/reconfiguration/data/relational/DataSourceConfigurerTest.java
@@ -0,0 +1,117 @@
+package org.cloudfoundry.reconfiguration.data.relational;
+
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.cloudfoundry.reconfiguration.CloudEnvironmentMockingTest;
+import org.cloudfoundry.runtime.env.AbstractDataSourceServiceInfo;
+import org.cloudfoundry.runtime.env.MysqlServiceInfo;
+import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.springframework.context.ApplicationContext;
+import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
+
+/**
+ * Integration test of the {@link DataSourceConfigurer}
+ * <p>
+ * Bootstraps application contexts containing various DataSource beans and
+ * checks if the actual beans got replaced/not replaced with the mock beans.
+ *
+ * @author Ramnivas Laddad
+ * @author Jennifer Hickey
+ *
+ */
+public class DataSourceConfigurerTest extends CloudEnvironmentMockingTest {
+
+ @Mock
+ private MysqlServiceInfo mockMysqlServiceInfo;
+ @Mock
+ private PostgresqlServiceInfo mockPostgresqlServiceInfo;
+
+ @Test
+ public void cloudDataSourceReplacesUserDataSourceIfMySqlServiceDetected() {
+ String serviceJdbcUrl = "jdbc:mysql://10.20.20.40:1234/mysql-1";
+ List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
+ serviceInfos.add(mockMysqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "mysql-5.1");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-ds-good-context.xml");
+ AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs",
+ DataSource.class);
+ Assert.assertEquals(serviceJdbcUrl, replacedDataSource.getUrl());
+ }
+
+ @Test
+ public void cloudDataSourceReplacesUserDataSourceIfPostgreSqlServiceDetected() {
+ String serviceJdbcUrl = "jdbc:postgresql://10.20.20.40:5432/mydb-1";
+ List<PostgresqlServiceInfo> serviceInfos = new ArrayList<PostgresqlServiceInfo>();
+ serviceInfos.add(mockPostgresqlServiceInfo);
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "postgresql-9.0");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl);
+ when(mockEnvironment.getServiceInfos(PostgresqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-ds-good-context.xml");
+ AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs",
+ DataSource.class);
+ Assert.assertEquals(serviceJdbcUrl, replacedDataSource.getUrl());
+ }
+
+ @Test
+ public void cloudDataSourceLeavesOriginalInPlaceIfMultipleServicesDetected() {
+ String serviceJdbcUrl1 = "jdbc:mysql://10.20.20.40:1234/mysql-1";
+ String serviceJdbcUrl2 = "jdbc:postgresql://10.20.20.40:5432/pg-2";
+ List<AbstractDataSourceServiceInfo> serviceInfos = new ArrayList<AbstractDataSourceServiceInfo>();
+ serviceInfos.add(mockMysqlServiceInfo);
+ serviceInfos.add(mockPostgresqlServiceInfo);
+ Map<String, Object> service1 = new HashMap<String, Object>();
+ service1.put("label", "mysql-5.1");
+ Map<String, Object> service2 = new HashMap<String, Object>();
+ service2.put("label", "postgresql-9.0");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service1);
+ serviceList.add(service2);
+ when(mockMysqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl1);
+ when(mockPostgresqlServiceInfo.getUrl()).thenReturn(serviceJdbcUrl2);
+ when(mockEnvironment.getServiceInfos(AbstractDataSourceServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-ds-good-context.xml");
+ AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs",
+ DataSource.class);
+ Assert.assertEquals("jdbc:hsql:localdb", replacedDataSource.getUrl());
+ }
+
+ @Test
+ public void cloudDataSourceLeavesOriginalInPlaceIfNoServicesDetected() {
+ List<MysqlServiceInfo> serviceInfos = new ArrayList<MysqlServiceInfo>();
+ Map<String, Object> service = new HashMap<String, Object>();
+ service.put("label", "mysql-5.1");
+ List<Map<String, Object>> serviceList = new ArrayList<Map<String, Object>>();
+ serviceList.add(service);
+ when(mockEnvironment.getServiceInfos(MysqlServiceInfo.class)).thenReturn(serviceInfos);
+ when(mockEnvironment.getServices()).thenReturn(serviceList);
+
+ ApplicationContext context = getTestApplicationContext("test-ds-good-context.xml");
+ AbstractDriverBasedDataSource replacedDataSource = (AbstractDriverBasedDataSource) context.getBean("myDs",
+ DataSource.class);
+ Assert.assertEquals("jdbc:hsql:localdb", replacedDataSource.getUrl());
+ }
+}
View
19 .../resources/org/cloudfoundry/reconfiguration/data/keyvalue/test-multiple-redis-context.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+ <bean name="redisConnection" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
+ <property name="hostName" value="localhost"/>
+ <property name="port" value="6789"/>
+ <property name="password" value="localpass"/>
+ </bean>
+
+ <bean name="redisConnection2" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
+ <property name="hostName" value="localhost"/>
+ <property name="port" value="6789"/>
+ <property name="password" value="localpass"/>
+ </bean>
+
+</beans>
View
13 .../src/test/resources/org/cloudfoundry/reconfiguration/data/keyvalue/test-redis-context.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+ <bean name="redisConnection" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
+ <property name="hostName" value="localhost"/>
+ <property name="port" value="6789"/>
+ <property name="password" value="localpass"/>
+ </bean>
+
+</beans>
View
0  ...reconfiguration/hibernate.test.properties → ...ration/data/orm/hibernate.test.properties
File renamed without changes
View
3  ...st-grails-embedded-props-good-context.xml → ...st-grails-embedded-props-good-context.xml
@@ -10,7 +10,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
@@ -29,4 +29,3 @@
</bean>
</beans>
-
View
3  ...est-grails-map-reference-good-context.xml → ...est-grails-map-reference-good-context.xml
@@ -10,7 +10,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
@@ -30,4 +30,3 @@
</util:map>
</beans>
-
View
5 ...ropertyFactory-reference-good-context.xml → ...ropertyFactory-reference-good-context.xml
@@ -10,7 +10,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
@@ -20,7 +20,6 @@
<property name="hibernateProperties" ref="hibernateProperties"/>
</bean>
- <util:properties id="hibernateProperties" location="org/cloudfoundry/reconfiguration/hibernate.test.properties"/>
+ <util:properties id="hibernateProperties" location="org/cloudfoundry/reconfiguration/data/orm/hibernate.test.properties"/>
</beans>
-
View
2  ...iguration/test-hibernate-good-context.xml → .../data/orm/test-hibernate-good-context.xml
@@ -7,7 +7,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
View
6 ...reconfiguration/test-jpa-good-context.xml → ...ration/data/orm/test-jpa-good-context.xml
@@ -7,13 +7,13 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
- lazy-init="true">
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
+ lazy-init="true">
<property name="dataSource" ref="myDs"/>
</bean>
View
6 ...est-jpa-typedStringValue-good-context.xml → ...est-jpa-typedStringValue-good-context.xml
@@ -9,13 +9,13 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
- lazy-init="true">
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
+ lazy-init="true">
<property name="dataSource" ref="myDs"/>
<property name="jpaProperties">
<value>
View
5 ...-reference-list-location-good-context.xml → ...-reference-list-location-good-context.xml
@@ -10,7 +10,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
@@ -25,7 +25,7 @@
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
- <value>org/cloudfoundry/reconfiguration/hibernate.test.properties</value>
+ <value>org/cloudfoundry/reconfiguration/data/orm/hibernate.test.properties</value>
</list>
</property>
</bean>
@@ -33,4 +33,3 @@
</bean>
</beans>
-
View
5 ...eference-single-location-good-context.xml → ...eference-single-location-good-context.xml
@@ -10,7 +10,7 @@
<bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:hsql:localdb" />
</bean>
-
+
<bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="myDs"/>
</bean>
@@ -24,11 +24,10 @@
<property name="properties">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
- <value>org/cloudfoundry/reconfiguration/hibernate.test.properties</value>
+ <value>org/cloudfoundry/reconfiguration/data/orm/hibernate.test.properties</value>
</property>
</bean>
</property>
</bean>
</beans>
-
View
20 .../test/resources/org/cloudfoundry/reconfiguration/data/relational/test-ds-good-context.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+ <bean name="myDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+ <property name="url" value="jdbc:hsql:localdb" />
+ </bean>
+
+ <bean name="txAwareDXProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
+ <property name="targetDataSource" ref="myDs"/>
+ </bean>
+
+ <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
+ lazy-init="true">
+ <property name="dataSource" ref="myDs"/>
+ </bean>
+
+</beans>
View
13 vcap-java-test-app/src/main/java/org/cloudfoundry/test/ServiceController.java
@@ -21,6 +21,7 @@
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@@ -89,11 +90,21 @@ public String hello(HttpServletResponse response) throws IOException {
HttpStatus.OK);
}
- @RequestMapping(value = "/redis", method = RequestMethod.GET)
+ @RequestMapping(value = "/redis/class", method = RequestMethod.GET)
public ResponseEntity<String> getRedisClass() {
if (serviceHolder.getRedisConnectionFactory() == null) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(serviceHolder.getRedisConnectionFactory().getClass().getName(), HttpStatus.OK);
}
+
+ @RequestMapping(value = "/redis/host", method = RequestMethod.GET)
+ public ResponseEntity<String> getRedisHostAddress() {
+ if (serviceHolder.getRedisConnectionFactory() == null) {
+ return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
+ }
+ //Jedis is the only client we currently support
+ JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory)serviceHolder.getRedisConnectionFactory();
+ return new ResponseEntity<String>(jedisConnectionFactory.getHostName() + ':' + jedisConnectionFactory.getPort(), HttpStatus.OK);
+ }
}
View
5 vcap-java-test-app/src/main/resources/META-INF/spring/root-context.xml
@@ -34,6 +34,11 @@ http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mongo:db-factory id="mongoDbFactory" dbname="pwdtest" host="127.0.0.1" port="1234" username="test_user" password="efgh"/>
</beans>
+ <beans profile="redis-auto-staging">
+ <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
+ p:hostName="localhost" p:port="1345"/>
+ </beans>
+
<beans profile="auto-staging-off-using-cloud-service">
<bean class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" id="test_mysql_database">
<property name="driverClassName" value="${database.driverClassName}"/>
Please sign in to comment.
Something went wrong with that request. Please try again.