Skip to content

Commit 02e3387

Browse files
committed
DATA-1293 - Allowed id attribute in addition to client-uri attribute in MongoDbFactoryParser.
Additionally, the previous approach allowed any attribute to be set if write-concern attribute was set. Fixed that also.
1 parent 7b27368 commit 02e3387

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Oliver Gierke
4545
* @author Thomas Darimont
4646
* @author Christoph Strobl
47+
* @author Viktor Khoroshko
4748
*/
4849
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
4950

@@ -73,7 +74,10 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
7374
BeanDefinition mongoUri = getMongoUri(element);
7475

7576
if (mongoUri != null) {
76-
if (element.getAttributes().getLength() >= 2 && !element.hasAttribute("write-concern")) {
77+
if (element.hasAttribute("mongo-ref") || element.hasAttribute("dbname")
78+
|| element.hasAttribute("authentication-dbname")
79+
|| element.hasAttribute("port") || element.hasAttribute("host")
80+
|| element.hasAttribute("username") || element.hasAttribute("password")) {
7781
parserContext.getReaderContext().error("Configure either Mongo URI or details individually!",
7882
parserContext.extractSource(element));
7983
}

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

Lines changed: 45 additions & 0 deletions
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);
Lines changed: 10 additions & 0 deletions
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>
Lines changed: 10 additions & 0 deletions
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>
Lines changed: 10 additions & 0 deletions
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>
Lines changed: 10 additions & 0 deletions
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)