Commit b30d4303 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish contribution

Closes gh-8230
parent 48b0f157
...@@ -100,16 +100,6 @@ ...@@ -100,16 +100,6 @@
<artifactId>de.flapdoodle.embed.mongo</artifactId> <artifactId>de.flapdoodle.embed.mongo</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>javax.cache</groupId> <groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId> <artifactId>cache-api</artifactId>
...@@ -309,6 +299,16 @@ ...@@ -309,6 +299,16 @@
<artifactId>jboss-transaction-spi</artifactId> <artifactId>jboss-transaction-spi</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId> <artifactId>spring-jdbc</artifactId>
......
...@@ -39,6 +39,7 @@ import org.springframework.core.env.Environment; ...@@ -39,6 +39,7 @@ import org.springframework.core.env.Environment;
* @author Oliver Gierke * @author Oliver Gierke
* @author Phillip Webb * @author Phillip Webb
* @author Mark Paluch * @author Mark Paluch
* @author Stephane Nicoll
*/ */
@Configuration @Configuration
@ConditionalOnClass(MongoClient.class) @ConditionalOnClass(MongoClient.class)
...@@ -46,22 +47,16 @@ import org.springframework.core.env.Environment; ...@@ -46,22 +47,16 @@ import org.springframework.core.env.Environment;
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory") @ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration { public class MongoAutoConfiguration {
private final MongoProperties properties;
private final MongoClientOptions options; private final MongoClientOptions options;
private final Environment environment;
private final MongoClientFactory factory; private final MongoClientFactory factory;
private MongoClient mongo; private MongoClient mongo;
public MongoAutoConfiguration(MongoProperties properties, public MongoAutoConfiguration(MongoProperties properties,
ObjectProvider<MongoClientOptions> options, Environment environment) { ObjectProvider<MongoClientOptions> options, Environment environment) {
this.properties = properties;
this.options = options.getIfAvailable(); this.options = options.getIfAvailable();
this.environment = environment; this.factory = new MongoClientFactory(properties, environment);
this.factory = new MongoClientFactory(properties);
} }
@PreDestroy @PreDestroy
...@@ -74,7 +69,7 @@ public class MongoAutoConfiguration { ...@@ -74,7 +69,7 @@ public class MongoAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public MongoClient mongo() throws UnknownHostException { public MongoClient mongo() throws UnknownHostException {
this.mongo = this.factory.createMongoClient(this.options, this.environment); this.mongo = this.factory.createMongoClient(this.options);
return this.mongo; return this.mongo;
} }
......
...@@ -33,29 +33,37 @@ import org.springframework.core.env.Environment; ...@@ -33,29 +33,37 @@ import org.springframework.core.env.Environment;
/** /**
* A factory for a blocking {@link MongoClient} that applies {@link MongoProperties}. * A factory for a blocking {@link MongoClient} that applies {@link MongoProperties}.
* *
* @author Dave Syer
* @author Phillip Webb
* @author Josh Long
* @author Andy Wilkinson
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Nasko Vasilev
* @author Mark Paluch * @author Mark Paluch
* @since 2.0.0 * @since 2.0.0
*/ */
public class MongoClientFactory { public class MongoClientFactory {
private final MongoProperties properties; private final MongoProperties properties;
private final Environment environment;
public MongoClientFactory(MongoProperties properties) { public MongoClientFactory(MongoProperties properties, Environment environment) {
this.properties = properties; this.properties = properties;
this.environment = environment;
} }
/** /**
* Creates a {@link MongoClient} using the given {@code options} and * Creates a {@link MongoClient} using the given {@code options}. If the configured
* {@code environment}. If the configured port is zero, the value of the * port is zero, the value of the {@code local.mongo.port} property is used to
* {@code local.mongo.port} property retrieved from the {@code environment} is used to
* configure the client. * configure the client.
* @param options the options * @param options the options
* @param environment the environment
* @return the Mongo client * @return the Mongo client
* @throws UnknownHostException if the configured host is unknown * @throws UnknownHostException if the configured host is unknown
*/ */
public MongoClient createMongoClient(MongoClientOptions options, public MongoClient createMongoClient(MongoClientOptions options)
Environment environment) throws UnknownHostException { throws UnknownHostException {
if (hasCustomAddress() || hasCustomCredentials()) { if (hasCustomAddress() || hasCustomCredentials()) {
if (this.properties.getUri() != null) { if (this.properties.getUri() != null) {
throw new IllegalStateException("Invalid mongo configuration, " throw new IllegalStateException("Invalid mongo configuration, "
...@@ -75,7 +83,7 @@ public class MongoClientFactory { ...@@ -75,7 +83,7 @@ public class MongoClientFactory {
} }
String host = this.properties.getHost() == null ? "localhost" String host = this.properties.getHost() == null ? "localhost"
: this.properties.getHost(); : this.properties.getHost();
int port = determinePort(environment); int port = determinePort();
return new MongoClient( return new MongoClient(
Collections.singletonList(new ServerAddress(host, port)), Collections.singletonList(new ServerAddress(host, port)),
credentials, options); credentials, options);
...@@ -94,13 +102,13 @@ public class MongoClientFactory { ...@@ -94,13 +102,13 @@ public class MongoClientFactory {
&& this.properties.getPassword() != null; && this.properties.getPassword() != null;
} }
private int determinePort(Environment environment) { private int determinePort() {
if (this.properties.getPort() == null) { if (this.properties.getPort() == null) {
return MongoProperties.DEFAULT_PORT; return MongoProperties.DEFAULT_PORT;
} }
if (this.properties.getPort() == 0) { if (this.properties.getPort() == 0) {
if (environment != null) { if (this.environment != null) {
String localPort = environment.getProperty("local.mongo.port"); String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) { if (localPort != null) {
return Integer.valueOf(localPort); return Integer.valueOf(localPort);
} }
...@@ -118,4 +126,5 @@ public class MongoClientFactory { ...@@ -118,4 +126,5 @@ public class MongoClientFactory {
} }
return MongoClientOptions.builder(); return MongoClientOptions.builder();
} }
} }
...@@ -135,15 +135,6 @@ public class MongoProperties { ...@@ -135,15 +135,6 @@ public class MongoProperties {
this.fieldNamingStrategy = fieldNamingStrategy; this.fieldNamingStrategy = fieldNamingStrategy;
} }
public void clearPassword() {
if (this.password == null) {
return;
}
for (int i = 0; i < this.password.length; i++) {
this.password[i] = 0;
}
}
public String getUri() { public String getUri() {
return this.uri; return this.uri;
} }
......
...@@ -34,6 +34,7 @@ import org.springframework.core.env.Environment; ...@@ -34,6 +34,7 @@ import org.springframework.core.env.Environment;
* {@link EnableAutoConfiguration Auto-configuration} for Reactive Mongo. * {@link EnableAutoConfiguration Auto-configuration} for Reactive Mongo.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
@Configuration @Configuration
...@@ -41,22 +42,16 @@ import org.springframework.core.env.Environment; ...@@ -41,22 +42,16 @@ import org.springframework.core.env.Environment;
@EnableConfigurationProperties(MongoProperties.class) @EnableConfigurationProperties(MongoProperties.class)
public class ReactiveMongoAutoConfiguration { public class ReactiveMongoAutoConfiguration {
private final MongoProperties properties;
private final MongoClientSettings settings; private final MongoClientSettings settings;
private final Environment environment;
private final ReactiveMongoClientFactory factory; private final ReactiveMongoClientFactory factory;
private MongoClient mongo; private MongoClient mongo;
public ReactiveMongoAutoConfiguration(MongoProperties properties, public ReactiveMongoAutoConfiguration(MongoProperties properties,
ObjectProvider<MongoClientSettings> settings, Environment environment) { ObjectProvider<MongoClientSettings> settings, Environment environment) {
this.properties = properties;
this.settings = settings.getIfAvailable(); this.settings = settings.getIfAvailable();
this.environment = environment; this.factory = new ReactiveMongoClientFactory(properties, environment);
this.factory = new ReactiveMongoClientFactory(properties);
} }
@PreDestroy @PreDestroy
...@@ -69,7 +64,7 @@ public class ReactiveMongoAutoConfiguration { ...@@ -69,7 +64,7 @@ public class ReactiveMongoAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public MongoClient reactiveStreamsMongoClient() { public MongoClient reactiveStreamsMongoClient() {
this.mongo = this.factory.createMongoClient(this.settings, this.environment); this.mongo = this.factory.createMongoClient(this.settings);
return this.mongo; return this.mongo;
} }
......
...@@ -39,27 +39,29 @@ import org.springframework.core.env.Environment; ...@@ -39,27 +39,29 @@ import org.springframework.core.env.Environment;
* A factory for a reactive {@link MongoClient} that applies {@link MongoProperties}. * A factory for a reactive {@link MongoClient} that applies {@link MongoProperties}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
public class ReactiveMongoClientFactory { public class ReactiveMongoClientFactory {
private final MongoProperties properties; private final MongoProperties properties;
public ReactiveMongoClientFactory(MongoProperties properties) { private final Environment environment;
public ReactiveMongoClientFactory(MongoProperties properties,
Environment environment) {
this.properties = properties; this.properties = properties;
this.environment = environment;
} }
/** /**
* Creates a {@link MongoClient} using the given {@code options} and * Creates a {@link MongoClient} using the given {@code options}. If the configured
* {@code environment}. If the configured port is zero, the value of the * port is zero, the value of the {@code local.mongo.port} property is used to
* {@code local.mongo.port} property retrieved from the {@code environment} is used to
* configure the client. * configure the client.
* @param settings the settings * @param settings the settings
* @param environment the environment
* @return the Mongo client * @return the Mongo client
*/ */
public MongoClient createMongoClient(MongoClientSettings settings, public MongoClient createMongoClient(MongoClientSettings settings) {
Environment environment) {
if (hasCustomAddress() || hasCustomCredentials()) { if (hasCustomAddress() || hasCustomCredentials()) {
if (this.properties.getUri() != null) { if (this.properties.getUri() != null) {
throw new IllegalStateException("Invalid mongo configuration, " throw new IllegalStateException("Invalid mongo configuration, "
...@@ -79,7 +81,7 @@ public class ReactiveMongoClientFactory { ...@@ -79,7 +81,7 @@ public class ReactiveMongoClientFactory {
} }
String host = this.properties.getHost() == null ? "localhost" String host = this.properties.getHost() == null ? "localhost"
: this.properties.getHost(); : this.properties.getHost();
int port = determinePort(environment); int port = determinePort();
ClusterSettings clusterSettings = ClusterSettings.builder() ClusterSettings clusterSettings = ClusterSettings.builder()
.hosts(Collections.singletonList(new ServerAddress(host, port))) .hosts(Collections.singletonList(new ServerAddress(host, port)))
.build(); .build();
...@@ -134,13 +136,13 @@ public class ReactiveMongoClientFactory { ...@@ -134,13 +136,13 @@ public class ReactiveMongoClientFactory {
&& this.properties.getPassword() != null; && this.properties.getPassword() != null;
} }
private int determinePort(Environment environment) { private int determinePort() {
if (this.properties.getPort() == null) { if (this.properties.getPort() == null) {
return MongoProperties.DEFAULT_PORT; return MongoProperties.DEFAULT_PORT;
} }
if (this.properties.getPort() == 0) { if (this.properties.getPort() == 0) {
if (environment != null) { if (this.environment != null) {
String localPort = environment.getProperty("local.mongo.port"); String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) { if (localPort != null) {
return Integer.valueOf(localPort); return Integer.valueOf(localPort);
} }
......
...@@ -234,7 +234,8 @@ public class EmbeddedMongoAutoConfiguration { ...@@ -234,7 +234,8 @@ public class EmbeddedMongoAutoConfiguration {
* {@code embeddedMongoServer} bean. * {@code embeddedMongoServer} bean.
*/ */
@Configuration @Configuration
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class }) @ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class,
ReactiveMongoClientFactoryBean.class })
protected static class EmbeddedReactiveMongoDependencyConfiguration extends protected static class EmbeddedReactiveMongoDependencyConfiguration extends
ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor { ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
......
...@@ -112,6 +112,12 @@ ...@@ -112,6 +112,12 @@
"description": "Enable LDAP repositories.", "description": "Enable LDAP repositories.",
"defaultValue": true "defaultValue": true
}, },
{
"name": "spring.data.mongodb.reactive-repositories.enabled",
"type": "java.lang.Boolean",
"description": "Enable Mongo reactive repositories.",
"defaultValue": true
},
{ {
"name": "spring.data.mongodb.repositories.enabled", "name": "spring.data.mongodb.repositories.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
......
...@@ -41,6 +41,8 @@ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ ...@@ -41,6 +41,8 @@ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.ReactiveMongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.ReactiveMongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
...@@ -83,6 +85,7 @@ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoCo ...@@ -83,6 +85,7 @@ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoCo
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.ReactiveMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\ org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
......
...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.alt.mongo; ...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.alt.mongo;
import org.springframework.boot.autoconfigure.data.mongo.city.City; import org.springframework.boot.autoconfigure.data.mongo.city.City;
import org.springframework.data.repository.reactive.ReactiveCrudRepository; import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface ReactiveCityMongoDbRepository extends ReactiveCrudRepository<City, Long> { public interface ReactiveCityMongoDbRepository
extends ReactiveCrudRepository<City, Long> {
} }
...@@ -17,9 +17,7 @@ ...@@ -17,9 +17,7 @@
package org.springframework.boot.autoconfigure.data.mongo; package org.springframework.boot.autoconfigure.data.mongo;
import org.junit.After; import org.junit.After;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
...@@ -36,9 +34,6 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,9 +34,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class ReactiveMongoDataAutoConfigurationTests { public class ReactiveMongoDataAutoConfigurationTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context; private AnnotationConfigApplicationContext context;
@After @After
...@@ -54,8 +49,8 @@ public class ReactiveMongoDataAutoConfigurationTests { ...@@ -54,8 +49,8 @@ public class ReactiveMongoDataAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class, ReactiveMongoAutoConfiguration.class, MongoDataAutoConfiguration.class, ReactiveMongoAutoConfiguration.class,
ReactiveMongoDataAutoConfiguration.class); ReactiveMongoDataAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class).length) assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class))
.isEqualTo(1); .hasSize(1);
} }
} }
...@@ -52,7 +52,9 @@ public class ReactiveMongoRepositoriesAutoConfigurationTests { ...@@ -52,7 +52,9 @@ public class ReactiveMongoRepositoriesAutoConfigurationTests {
@After @After
public void close() { public void close() {
this.context.close(); if (this.context != null) {
this.context.close();
}
} }
@Test @Test
......
...@@ -35,7 +35,7 @@ import org.springframework.test.util.ReflectionTestUtils; ...@@ -35,7 +35,7 @@ import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link MongoClientFactory} via {@link MongoProperties}. * Tests for {@link MongoClientFactory}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
...@@ -51,7 +51,7 @@ public class MongoClientFactoryTests { ...@@ -51,7 +51,7 @@ public class MongoClientFactoryTests {
public void portCanBeCustomized() throws UnknownHostException { public void portCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setPort(12345); properties.setPort(12345);
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 12345); assertServerAddress(allAddresses.get(0), "localhost", 12345);
...@@ -61,7 +61,7 @@ public class MongoClientFactoryTests { ...@@ -61,7 +61,7 @@ public class MongoClientFactoryTests {
public void hostCanBeCustomized() throws UnknownHostException { public void hostCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com"); properties.setHost("mongo.example.com");
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
...@@ -72,7 +72,7 @@ public class MongoClientFactoryTests { ...@@ -72,7 +72,7 @@ public class MongoClientFactoryTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
"test"); "test");
} }
...@@ -83,7 +83,7 @@ public class MongoClientFactoryTests { ...@@ -83,7 +83,7 @@ public class MongoClientFactoryTests {
properties.setDatabase("foo"); properties.setDatabase("foo");
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
"foo"); "foo");
} }
...@@ -94,7 +94,7 @@ public class MongoClientFactoryTests { ...@@ -94,7 +94,7 @@ public class MongoClientFactoryTests {
properties.setAuthenticationDatabase("foo"); properties.setAuthenticationDatabase("foo");
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
"foo"); "foo");
} }
...@@ -104,7 +104,7 @@ public class MongoClientFactoryTests { ...@@ -104,7 +104,7 @@ public class MongoClientFactoryTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://user:secret@mongo1.example.com:12345," properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
+ "mongo2.example.com:23456/test"); + "mongo2.example.com:23456/test");
MongoClient client = new MongoClientFactory(properties).createMongoClient(null, null); MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2); assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
...@@ -123,7 +123,7 @@ public class MongoClientFactoryTests { ...@@ -123,7 +123,7 @@ public class MongoClientFactoryTests {
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " this.thrown.expectMessage("Invalid mongo configuration, "
+ "either uri or host/port/credentials must be specified"); + "either uri or host/port/credentials must be specified");
new MongoClientFactory(properties).createMongoClient(null, null); createMongoClient(properties);
} }
@Test @Test
...@@ -135,7 +135,12 @@ public class MongoClientFactoryTests { ...@@ -135,7 +135,12 @@ public class MongoClientFactoryTests {
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " this.thrown.expectMessage("Invalid mongo configuration, "
+ "either uri or host/port/credentials must be specified"); + "either uri or host/port/credentials must be specified");
new MongoClientFactory(properties).createMongoClient(null, null); createMongoClient(properties);
}
private MongoClient createMongoClient(MongoProperties properties)
throws UnknownHostException {
return new MongoClientFactory(properties, null).createMongoClient(null);
} }
private List<ServerAddress> extractServerAddresses(MongoClient client) { private List<ServerAddress> extractServerAddresses(MongoClient client) {
......
...@@ -78,7 +78,8 @@ public class MongoPropertiesTests { ...@@ -78,7 +78,8 @@ public class MongoPropertiesTests {
builder.requiredReplicaSetName("testReplicaSetName"); builder.requiredReplicaSetName("testReplicaSetName");
MongoClientOptions options = builder.build(); MongoClientOptions options = builder.build();
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
MongoClient client = new MongoClientFactory(properties).createMongoClient(options, null); MongoClient client = new MongoClientFactory(properties, null)
.createMongoClient(options);
MongoClientOptions wrapped = client.getMongoClientOptions(); MongoClientOptions wrapped = client.getMongoClientOptions();
assertThat(wrapped.isAlwaysUseMBeans()).isEqualTo(options.isAlwaysUseMBeans()); assertThat(wrapped.isAlwaysUseMBeans()).isEqualTo(options.isAlwaysUseMBeans());
assertThat(wrapped.getConnectionsPerHost()) assertThat(wrapped.getConnectionsPerHost())
......
...@@ -57,7 +57,8 @@ public class ReactiveMongoAutoConfigurationTests { ...@@ -57,7 +57,8 @@ public class ReactiveMongoAutoConfigurationTests {
@Test @Test
public void clientExists() { public void clientExists() {
this.context = new AnnotationConfigApplicationContext( this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class,
ReactiveMongoAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(MongoClient.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(MongoClient.class).length).isEqualTo(1);
} }
...@@ -67,10 +68,11 @@ public class ReactiveMongoAutoConfigurationTests { ...@@ -67,10 +68,11 @@ public class ReactiveMongoAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.mongodb.host:localhost"); "spring.data.mongodb.host:localhost");
this.context.register(OptionsConfig.class, this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class,
ReactiveMongoAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(MongoClient.class).getSettings().getSocketSettings() assertThat(this.context.getBean(MongoClient.class).getSettings()
.getReadTimeout(TimeUnit.SECONDS)).isEqualTo(300); .getSocketSettings().getReadTimeout(TimeUnit.SECONDS)).isEqualTo(300);
} }
@Test @Test
...@@ -79,10 +81,11 @@ public class ReactiveMongoAutoConfigurationTests { ...@@ -79,10 +81,11 @@ public class ReactiveMongoAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.mongodb.uri:mongodb://localhost/test"); "spring.data.mongodb.uri:mongodb://localhost/test");
this.context.register(OptionsConfig.class, this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class,
ReactiveMongoAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(MongoClient.class).getSettings().getReadPreference()) assertThat(this.context.getBean(MongoClient.class).getSettings()
.isEqualTo(ReadPreference.nearest()); .getReadPreference()).isEqualTo(ReadPreference.nearest());
} }
@Test @Test
...@@ -91,7 +94,8 @@ public class ReactiveMongoAutoConfigurationTests { ...@@ -91,7 +94,8 @@ public class ReactiveMongoAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.mongodb.uri:mongodb://localhost/test"); "spring.data.mongodb.uri:mongodb://localhost/test");
this.context.register(SslOptionsConfig.class, this.context.register(SslOptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, ReactiveMongoAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class,
ReactiveMongoAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
MongoClient mongo = this.context.getBean(MongoClient.class); MongoClient mongo = this.context.getBean(MongoClient.class);
MongoClientSettings settings = mongo.getSettings(); MongoClientSettings settings = mongo.getSettings();
......
...@@ -31,7 +31,7 @@ import org.junit.rules.ExpectedException; ...@@ -31,7 +31,7 @@ import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link ReactiveMongoClientFactory} via {@link MongoProperties}. * Tests for {@link ReactiveMongoClientFactory}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
...@@ -44,8 +44,7 @@ public class ReactiveMongoClientFactoryTests { ...@@ -44,8 +44,7 @@ public class ReactiveMongoClientFactoryTests {
public void portCanBeCustomized() throws UnknownHostException { public void portCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setPort(12345); properties.setPort(12345);
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 12345); assertServerAddress(allAddresses.get(0), "localhost", 12345);
...@@ -55,8 +54,7 @@ public class ReactiveMongoClientFactoryTests { ...@@ -55,8 +54,7 @@ public class ReactiveMongoClientFactoryTests {
public void hostCanBeCustomized() throws UnknownHostException { public void hostCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com"); properties.setHost("mongo.example.com");
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
...@@ -67,8 +65,7 @@ public class ReactiveMongoClientFactoryTests { ...@@ -67,8 +65,7 @@ public class ReactiveMongoClientFactoryTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null);
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret", assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
"test"); "test");
} }
...@@ -79,9 +76,9 @@ public class ReactiveMongoClientFactoryTests { ...@@ -79,9 +76,9 @@ public class ReactiveMongoClientFactoryTests {
properties.setDatabase("foo"); properties.setDatabase("foo");
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null); assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret", "foo"); "foo");
} }
@Test @Test
...@@ -90,9 +87,9 @@ public class ReactiveMongoClientFactoryTests { ...@@ -90,9 +87,9 @@ public class ReactiveMongoClientFactoryTests {
properties.setAuthenticationDatabase("foo"); properties.setAuthenticationDatabase("foo");
properties.setUsername("user"); properties.setUsername("user");
properties.setPassword("secret".toCharArray()); properties.setPassword("secret".toCharArray());
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null); assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret",
assertMongoCredential(extractMongoCredentials(client).get(0), "user", "secret", "foo"); "foo");
} }
@Test @Test
...@@ -100,8 +97,7 @@ public class ReactiveMongoClientFactoryTests { ...@@ -100,8 +97,7 @@ public class ReactiveMongoClientFactoryTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://user:secret@mongo1.example.com:12345," properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
+ "mongo2.example.com:23456/test"); + "mongo2.example.com:23456/test");
MongoClient client = new ReactiveMongoClientFactory(properties).createMongoClient(null, MongoClient client = createMongoClient(properties);
null);
List<ServerAddress> allAddresses = extractServerAddresses(client); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2); assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
...@@ -120,7 +116,7 @@ public class ReactiveMongoClientFactoryTests { ...@@ -120,7 +116,7 @@ public class ReactiveMongoClientFactoryTests {
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " this.thrown.expectMessage("Invalid mongo configuration, "
+ "either uri or host/port/credentials must be specified"); + "either uri or host/port/credentials must be specified");
new MongoClientFactory(properties).createMongoClient(null, null); createMongoClient(properties);
} }
@Test @Test
...@@ -132,7 +128,11 @@ public class ReactiveMongoClientFactoryTests { ...@@ -132,7 +128,11 @@ public class ReactiveMongoClientFactoryTests {
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Invalid mongo configuration, " this.thrown.expectMessage("Invalid mongo configuration, "
+ "either uri or host/port/credentials must be specified"); + "either uri or host/port/credentials must be specified");
new MongoClientFactory(properties).createMongoClient(null, null); createMongoClient(properties);
}
private MongoClient createMongoClient(MongoProperties properties) {
return new ReactiveMongoClientFactory(properties, null).createMongoClient(null);
} }
private List<ServerAddress> extractServerAddresses(MongoClient client) { private List<ServerAddress> extractServerAddresses(MongoClient client) {
......
...@@ -589,6 +589,7 @@ content into your application; rather pick only the properties that you need. ...@@ -589,6 +589,7 @@ content into your application; rather pick only the properties that you need.
spring.data.mongodb.host=localhost # Mongo server host. Cannot be set with uri. spring.data.mongodb.host=localhost # Mongo server host. Cannot be set with uri.
spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with uri. spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with uri.
spring.data.mongodb.port=27017 # Mongo server port. Cannot be set with uri. spring.data.mongodb.port=27017 # Mongo server port. Cannot be set with uri.
spring.data.mongodb.reactive-repositories.enabled=true # Enable Mongo reactive repositories.
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories. spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials. spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with uri. spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with uri.
......
...@@ -3299,7 +3299,8 @@ pooled connection factory by default. ...@@ -3299,7 +3299,8 @@ pooled connection factory by default.
http://www.mongodb.com/[MongoDB] is an open-source NoSQL document database that uses a http://www.mongodb.com/[MongoDB] is an open-source NoSQL document database that uses a
JSON-like schema instead of traditional table-based relational data. Spring Boot offers JSON-like schema instead of traditional table-based relational data. Spring Boot offers
several conveniences for working with MongoDB, including the several conveniences for working with MongoDB, including the
`spring-boot-starter-data-mongodb` '`Starter`'. `spring-boot-starter-data-mongodb` and `spring-boot-starter-data-mongodb-reactive`
'`Starters`'.
......
...@@ -23,6 +23,16 @@ ...@@ -23,6 +23,16 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.mongodb</groupId> <groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId> <artifactId>mongodb-driver</artifactId>
...@@ -39,15 +49,5 @@ ...@@ -39,15 +49,5 @@
<groupId>io.projectreactor</groupId> <groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId> <artifactId>reactor-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
</project> </project>
provides: spring-data-mongodb-reactive provides: spring-data-mongodb,mongodb-driver-async,mongodb-driver-reactivestreams
\ No newline at end of file \ No newline at end of file
provides: spring-data-mongodb provides: spring-data-mongodb,mongodb-driver
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment