fallback to couchbaseEnv default env reference if none explicitely set

This commit is contained in:
Simon Baslé
2015-06-22 12:11:46 +02:00
parent 14e08e9717
commit 38d440a2e8
2 changed files with 30 additions and 4 deletions

View File

@@ -37,7 +37,8 @@ import org.springframework.util.xml.DomUtils;
* Such a definition can be tuned by either referencing a {@link CouchbaseEnvironment} via
* the {@value #CLUSTER_ENVIRONMENT_REF} attribute or define a custom environment inline via
* the <{@value #CLUSTER_ENVIRONMENT_TAG}> tag (not recommended, environments should be
* shared as possible).
* shared as possible). If no environment reference or inline description is provided, the
* default environment reference {@value BeanNames#COUCHBASE_ENV} is used.
*
* To bootstrap the connection, one can provide IPs or hostnames of nodes to connect to
* via 1 or more <{@value #CLUSTER_NODE_TAG}> tags.
@@ -117,6 +118,10 @@ public class CouchbaseClusterParser extends AbstractSingleBeanDefinitionParser {
}
}
/**
* @return true if a custom environment was parsed and injected (either reference or inline), false if
* the default environment reference was used.
*/
protected boolean parseEnvironment(BeanDefinitionBuilder clusterBuilder, Element clusterElement) {
//any inline environment description would take precedence over a reference
Element envElement = DomUtils.getChildElementByTagName(clusterElement, CLUSTER_ENVIRONMENT_TAG);
@@ -131,6 +136,9 @@ public class CouchbaseClusterParser extends AbstractSingleBeanDefinitionParser {
injectEnvReference(clusterBuilder, envRef);
return true;
}
//if no custom value provided, consider it a reference to the default bean for Couchbase Environment
injectEnvReference(clusterBuilder, BeanNames.COUCHBASE_ENV);
return false;
}

View File

@@ -54,8 +54,8 @@ public class CouchbaseClusterParserTest {
}
@Test
public void testClusterWithNodes() {
BeanDefinition def = factory.getBeanDefinition("clusterWithNodes");
public void testClusterWithoutSpecificEnv() {
BeanDefinition def = factory.getBeanDefinition("clusterDefault");
assertThat(def, is(notNullValue()));
assertThat(def.getConstructorArgumentValues().getArgumentCount(), is(equalTo(1)));
@@ -63,7 +63,25 @@ public class CouchbaseClusterParserTest {
assertThat(def.getFactoryMethodName(), is(equalTo("create")));
ConstructorArgumentValues.ValueHolder holder = def.getConstructorArgumentValues()
.getArgumentValue(0, List.class);
.getArgumentValue(0, CouchbaseEnvironment.class);
assertThat(holder.getValue(), instanceOf(RuntimeBeanReference.class));
RuntimeBeanReference envRef = (RuntimeBeanReference) holder.getValue();
assertThat(envRef.getBeanName(), is(equalTo("couchbaseEnv")));
}
@Test
public void testClusterWithNodes() {
BeanDefinition def = factory.getBeanDefinition("clusterWithNodes");
assertThat(def, is(notNullValue()));
assertThat(def.getConstructorArgumentValues().getArgumentCount(), is(equalTo(2)));
assertThat(def.getPropertyValues().size(), is(equalTo(0)));
assertThat(def.getFactoryMethodName(), is(equalTo("create")));
ConstructorArgumentValues.ValueHolder holder = def.getConstructorArgumentValues()
.getArgumentValue(1, List.class);
assertThat(holder.getValue(), is(instanceOf(List.class)));
List nodes = (List<String>) holder.getValue();