Skip to content

Commit 7bfbc44

Browse files
DATAMONGO-1158 - Add config option for credentials to mongo-client.
We now reject mongo-options configuration when using mongo-java-driver generation 3 and above.
1 parent acab48d commit 7bfbc44

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
* @author Mike Saavedra
3333
* @author Thomas Darimont
3434
* @author Christoph Strobl
35+
* @deprecated since 1.7. Please use {@link MongoClientOptionsFactoryBean} instead.
3536
*/
36-
@SuppressWarnings("deprecation")
37+
@Deprecated
3738
public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, InitializingBean {
3839

3940
private static final MongoOptions DEFAULT_MONGO_OPTIONS = new MongoOptions();
@@ -216,6 +217,12 @@ public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
216217
*/
217218
public void afterPropertiesSet() {
218219

220+
if (MongoClientVersion.isMongo3Driver()) {
221+
throw new IllegalArgumentException(
222+
String
223+
.format("Usage of 'mongo-options' is no longer supported for mongo-java-driver version 3 and above. Please use 'mongo-client-options' and refer to chapter 'MongoDB 3.0 Support' for details."));
224+
}
225+
219226
MongoOptions options = new MongoOptions();
220227

221228
options.setConnectionsPerHost(connectionsPerHost);
@@ -233,12 +240,9 @@ public void afterPropertiesSet() {
233240
options.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault());
234241
}
235242

236-
if (!MongoClientVersion.isMongo3Driver()) {
237-
238-
ReflectiveMongoOptionsInvoker.setAutoConnectRetry(options, autoConnectRetry);
239-
ReflectiveMongoOptionsInvoker.setMaxAutoConnectRetryTime(options, maxAutoConnectRetryTime);
240-
ReflectiveMongoOptionsInvoker.setSlaveOk(options, slaveOk);
241-
}
243+
ReflectiveMongoOptionsInvoker.setAutoConnectRetry(options, autoConnectRetry);
244+
ReflectiveMongoOptionsInvoker.setMaxAutoConnectRetryTime(options, maxAutoConnectRetryTime);
245+
ReflectiveMongoOptionsInvoker.setSlaveOk(options, slaveOk);
242246

243247
this.options = options;
244248
}
@@ -266,4 +270,5 @@ public Class<?> getObjectType() {
266270
public boolean isSingleton() {
267271
return true;
268272
}
273+
269274
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.springframework.data.mongodb.MongoClientVersion.*;
2222

2323
import org.junit.Before;
24+
import org.junit.BeforeClass;
2425
import org.junit.Test;
2526
import org.springframework.beans.factory.config.BeanDefinition;
2627
import org.springframework.beans.factory.config.ConstructorArgumentValues;
@@ -47,13 +48,18 @@
4748
* Integration tests for {@link MongoDbFactoryParser}.
4849
*
4950
* @author Oliver Gierke
50-
* @auhtor Christoph Strobl
51+
* @author Christoph Strobl
5152
*/
5253
public class MongoDbFactoryParserIntegrationTests {
5354

5455
DefaultListableBeanFactory factory;
5556
BeanDefinitionReader reader;
5657

58+
@BeforeClass
59+
public static void validateMongoDriver() {
60+
assumeFalse(isMongo3Driver());
61+
}
62+
5763
@Before
5864
public void setUp() {
5965
factory = new DefaultListableBeanFactory();
@@ -136,8 +142,6 @@ public void createsDbFactoryBean() {
136142
@Test
137143
public void parsesMaxAutoConnectRetryTimeCorrectly() {
138144

139-
assumeFalse(isMongo3Driver());
140-
141145
reader.loadBeanDefinitions(new ClassPathResource("namespace/db-factory-bean.xml"));
142146
Mongo mongo = factory.getBean(Mongo.class);
143147
assertThat(ReflectiveMongoOptionsInvokerTestUtil.getMaxAutoConnectRetryTime(mongo.getMongoOptions()), is(27L));

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package org.springframework.data.mongodb.config;
1717

1818
import static org.junit.Assert.*;
19+
import static org.junit.Assume.*;
1920
import static org.springframework.data.mongodb.MongoClientVersion.*;
2021
import static org.springframework.test.util.ReflectionTestUtils.*;
2122

2223
import javax.net.ssl.SSLSocketFactory;
2324

25+
import org.junit.BeforeClass;
2426
import org.junit.Test;
2527
import org.junit.runner.RunWith;
2628
import org.springframework.beans.factory.annotation.Autowired;
@@ -54,6 +56,11 @@ public class MongoNamespaceTests {
5456

5557
@Autowired ApplicationContext ctx;
5658

59+
@BeforeClass
60+
public static void validateMongoDriver() {
61+
assumeFalse(isMongo3Driver());
62+
}
63+
5764
@Test
5865
public void testMongoSingleton() throws Exception {
5966

@@ -251,12 +258,8 @@ public void testMongoSingletonWithPropertyPlaceHolders() throws Exception {
251258
assertEquals(0, mongoOpts.getWriteConcern().getWtimeout());
252259
assertEquals(true, mongoOpts.getWriteConcern().fsync());
253260

254-
if (!isMongo3Driver()) {
255-
assertEquals(true, mongoOpts.fsync);
256-
assertEquals(true, ReflectiveMongoOptionsInvokerTestUtil.getAutoConnectRetry(mongoOpts));
257-
assertEquals(true, ReflectiveMongoOptionsInvokerTestUtil.getSlaveOk(mongoOpts));
258-
} else {
259-
assertEquals(false, mongoOpts.fsync);
260-
}
261+
assertEquals(true, mongoOpts.fsync);
262+
assertEquals(true, ReflectiveMongoOptionsInvokerTestUtil.getAutoConnectRetry(mongoOpts));
263+
assertEquals(true, ReflectiveMongoOptionsInvokerTestUtil.getSlaveOk(mongoOpts));
261264
}
262265
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBeanUnitTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import javax.net.ssl.SSLSocketFactory;
2525

26+
import org.junit.BeforeClass;
2627
import org.junit.Test;
2728

2829
import com.mongodb.MongoOptions;
@@ -36,14 +37,17 @@
3637
*/
3738
public class MongoOptionsFactoryBeanUnitTests {
3839

40+
@BeforeClass
41+
public static void validateMongoDriver() {
42+
assumeFalse(isMongo3Driver());
43+
}
44+
3945
/**
4046
* @see DATADOC-280
4147
*/
4248
@Test
4349
public void setsMaxConnectRetryTime() {
4450

45-
assumeFalse(isMongo3Driver());
46-
4751
MongoOptionsFactoryBean bean = new MongoOptionsFactoryBean();
4852
bean.setMaxAutoConnectRetryTime(27);
4953
bean.afterPropertiesSet();

0 commit comments

Comments
 (0)