Skip to content

Commit 4f94f37

Browse files
vkhoroshkochristophstrobl
authored andcommitted
DATAMONGO-1293 - Allowed id attribute in addition to client-uri attribute in MongoDbFactoryParser.
We now allow write-concern and id to be configured along with the uri or client-uri attribute of <mongo:db-factory. Original Pull Request: spring-projects#328 CLA: 140120150929074128 (Viktor Khoroshko)
1 parent 528de58 commit 4f94f37

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoDbFactoryParser.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import static org.springframework.data.config.ParsingUtils.*;
1919
import static org.springframework.data.mongodb.config.MongoParsingUtils.*;
2020

21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
2125
import org.springframework.beans.factory.BeanDefinitionStoreException;
2226
import org.springframework.beans.factory.config.BeanDefinition;
2327
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
@@ -44,8 +48,18 @@
4448
* @author Oliver Gierke
4549
* @author Thomas Darimont
4650
* @author Christoph Strobl
51+
* @author Viktor Khoroshko
4752
*/
4853
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
54+
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES;
55+
56+
static {
57+
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>();
58+
mongoUriAllowedAdditionalAttributes.add("id");
59+
mongoUriAllowedAdditionalAttributes.add("write-concern");
60+
61+
MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES = Collections.unmodifiableSet(mongoUriAllowedAdditionalAttributes);
62+
}
4963

5064
/*
5165
* (non-Javadoc)
@@ -73,7 +87,14 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
7387
BeanDefinition mongoUri = getMongoUri(element);
7488

7589
if (mongoUri != null) {
76-
if (element.getAttributes().getLength() >= 2 && !element.hasAttribute("write-concern")) {
90+
int allowedAttributesCount = 1;
91+
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) {
92+
if (element.hasAttribute(attribute)) {
93+
allowedAttributesCount++;
94+
}
95+
}
96+
97+
if (element.getAttributes().getLength() > allowedAttributesCount) {
7798
parserContext.getReaderContext().error("Configure either Mongo URI or details individually!",
7899
parserContext.extractSource(element));
79100
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoDbFactoryParserIntegrationTests.java

+45
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Oliver Gierke
5252
* @author Christoph Strobl
53+
* @author Viktor Khoroshko
5354
*/
5455
public class MongoDbFactoryParserIntegrationTests {
5556

@@ -198,6 +199,50 @@ public void rejectsClientUriPlusDetailedConfiguration() {
198199
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-and-details.xml"));
199200
}
200201

202+
/**
203+
* @see DATAMONGO-1293
204+
*/
205+
@Test
206+
public void setsUpClientUriWithId() {
207+
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-and-id.xml"));
208+
BeanDefinition definition = factory.getBeanDefinition("testMongo");
209+
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
210+
211+
assertThat(constructorArguments.getArgumentCount(), is(1));
212+
ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class);
213+
assertThat(argument, is(notNullValue()));
214+
}
215+
216+
/**
217+
* @see DATAMONGO-1293
218+
*/
219+
@Test
220+
public void setsUpUriWithId() {
221+
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-and-id.xml"));
222+
BeanDefinition definition = factory.getBeanDefinition("testMongo");
223+
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
224+
225+
assertThat(constructorArguments.getArgumentCount(), is(1));
226+
ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class);
227+
assertThat(argument, is(notNullValue()));
228+
}
229+
230+
/**
231+
* @see DATAMONGO-1293
232+
*/
233+
@Test(expected = BeanDefinitionParsingException.class)
234+
public void rejectsClientUriPlusDetailedConfigurationAndWriteConcern() {
235+
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-write-concern-and-details.xml"));
236+
}
237+
238+
/**
239+
* @see DATAMONGO-1293
240+
*/
241+
@Test(expected = BeanDefinitionParsingException.class)
242+
public void rejectsUriPlusDetailedConfigurationAndWriteConcern() {
243+
reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri-write-concern-and-details.xml"));
244+
}
245+
201246
private static void assertWriteConcern(ClassPathXmlApplicationContext ctx, WriteConcern expectedWriteConcern) {
202247

203248
SimpleMongoDbFactory dbFactory = ctx.getBean("first", SimpleMongoDbFactory.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:db-factory id="testMongo" client-uri="mongodb://username:password@localhost/database" />
9+
10+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:db-factory client-uri="mongodb://username:password@localhost/database" write-concern="NORMAL" username="username" />
9+
10+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:db-factory id="testMongo" uri="mongodb://username:password@localhost/database" />
9+
10+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:db-factory uri="mongodb://username:password@localhost/database" write-concern="NORMAL" username="username" />
9+
10+
</beans>

0 commit comments

Comments
 (0)