Commit 40a7445f authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.1.x'

parents 8ee237a9 64599261
...@@ -35,13 +35,16 @@ import com.mongodb.ServerAddress; ...@@ -35,13 +35,16 @@ import com.mongodb.ServerAddress;
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Josh Long * @author Josh Long
* @author Andy Wilkinson
*/ */
@ConfigurationProperties(prefix = "spring.data.mongodb") @ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties { public class MongoProperties {
private static final int DEFAULT_PORT = 27017;
private String host; private String host;
private int port = 27017; private Integer port = null;
private String uri = "mongodb://localhost/test"; private String uri = "mongodb://localhost/test";
...@@ -112,11 +115,11 @@ public class MongoProperties { ...@@ -112,11 +115,11 @@ public class MongoProperties {
this.uri = uri; this.uri = uri;
} }
public int getPort() { public Integer getPort() {
return this.port; return this.port;
} }
public void setPort(int port) { public void setPort(Integer port) {
this.port = port; this.port = port;
} }
...@@ -138,17 +141,19 @@ public class MongoProperties { ...@@ -138,17 +141,19 @@ public class MongoProperties {
public MongoClient createMongoClient(MongoClientOptions options) public MongoClient createMongoClient(MongoClientOptions options)
throws UnknownHostException { throws UnknownHostException {
try { try {
if (this.host != null) { if (customAddress() || customCredentials()) {
if (options == null) { if (options == null) {
options = MongoClientOptions.builder().build(); options = MongoClientOptions.builder().build();
} }
List<MongoCredential> credentials = null; List<MongoCredential> credentials = null;
if (this.password != null && this.username != null) { if (customCredentials()) {
credentials = Arrays.asList(MongoCredential.createMongoCRCredential( credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
this.username, getMongoClientDatabase(), this.password)); this.username, getMongoClientDatabase(), this.password));
} }
return new MongoClient(Arrays.asList(new ServerAddress(this.host, String host = this.host == null ? "localhost" : this.host;
this.port)), credentials, options); int port = this.port == null ? DEFAULT_PORT : this.port;
return new MongoClient(Arrays.asList(new ServerAddress(host, port)),
credentials, options);
} }
// The options and credentials are in the URI // The options and credentials are in the URI
return new MongoClient(new MongoClientURI(this.uri, builder(options))); return new MongoClient(new MongoClientURI(this.uri, builder(options)));
...@@ -158,6 +163,14 @@ public class MongoProperties { ...@@ -158,6 +163,14 @@ public class MongoProperties {
} }
} }
private boolean customAddress() {
return this.host != null || this.port != null;
}
private boolean customCredentials() {
return this.username != null && this.password != null;
}
private Builder builder(MongoClientOptions options) { private Builder builder(MongoClientOptions options) {
Builder builder = MongoClientOptions.builder(); Builder builder = MongoClientOptions.builder();
if (options != null) { if (options != null) {
......
...@@ -16,19 +16,29 @@ ...@@ -16,19 +16,29 @@
package org.springframework.boot.autoconfigure.mongo; package org.springframework.boot.autoconfigure.mongo;
import java.net.UnknownHostException;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link MongoProperties}. * Tests for {@link MongoProperties}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
public class MongoPropertiesTests { public class MongoPropertiesTests {
...@@ -43,6 +53,66 @@ public class MongoPropertiesTests { ...@@ -43,6 +53,66 @@ public class MongoPropertiesTests {
assertThat(properties.getPassword(), equalTo("word".toCharArray())); assertThat(properties.getPassword(), equalTo("word".toCharArray()));
} }
@Test
public void portCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setPort(12345);
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertThat(allAddresses, hasSize(1));
assertServerAddress(allAddresses.get(0), "localhost", 12345);
}
@Test
public void hostCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com");
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertThat(allAddresses, hasSize(1));
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
}
@Test
public void credentialsCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = properties.createMongoClient(null);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret");
}
@Test
public void uriCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties
.setUri("mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test");
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertEquals(2, allAddresses.size());
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = client.getCredentialsList();
assertEquals(1, credentialsList.size());
assertMongoCredential(credentialsList.get(0), "user", "secret");
}
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
int expectedPort) {
assertThat(serverAddress.getHost(), equalTo(expectedHost));
assertThat(serverAddress.getPort(), equalTo(expectedPort));
}
private void assertMongoCredential(MongoCredential credentials,
String expectedUsername, String expectedPassword) {
assertThat(credentials.getUserName(), equalTo(expectedUsername));
assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray()));
}
@Configuration @Configuration
@EnableConfigurationProperties(MongoProperties.class) @EnableConfigurationProperties(MongoProperties.class)
static class Conf { static class Conf {
......
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