diff --git a/src/main/java/org/springframework/data/couchbase/config/CouchbaseClusterParser.java b/src/main/java/org/springframework/data/couchbase/config/CouchbaseClusterParser.java index 9c07b0a8..1e78bf08 100644 --- a/src/main/java/org/springframework/data/couchbase/config/CouchbaseClusterParser.java +++ b/src/main/java/org/springframework/data/couchbase/config/CouchbaseClusterParser.java @@ -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; } diff --git a/src/test/java/org/springframework/data/couchbase/config/CouchbaseClusterParserTest.java b/src/test/java/org/springframework/data/couchbase/config/CouchbaseClusterParserTest.java index 8283b93c..1cfdcb16 100644 --- a/src/test/java/org/springframework/data/couchbase/config/CouchbaseClusterParserTest.java +++ b/src/test/java/org/springframework/data/couchbase/config/CouchbaseClusterParserTest.java @@ -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) holder.getValue();