Flesh out and polish Embedded MongoDB auto-configuration contribution

Embedded MongoDB is now auto-configured when it is on the classpath.
The Mongo instance will listen on the port specified by the
spring.data.mongodb.port property. If this property has a value of
zero and randomly allocated port will be used. In such an event, the
MongoClient created by MongoAutoConfiguration will be automatically
configured to use the port that was allocated.

By default, MongoDB 2.6.10 will be used. This can be configured using
the spring.embedded-mongodb.version property. Mongo's sync delay
feature is enabled by default. This can be configured using the
spring.embedded-mongobd.features property.

Closes gh-2002
This commit is contained in:
Andy Wilkinson
2014-11-26 11:40:31 +00:00
parent f2b2c085e9
commit 2c81907d58
19 changed files with 710 additions and 200 deletions

View File

@@ -16,14 +16,13 @@
package sample.data.mongo;
import java.util.regex.Pattern;
import org.junit.Rule;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.OutputCapture;
import org.springframework.core.NestedCheckedException;
import com.mongodb.MongoTimeoutException;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
@@ -31,44 +30,21 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link SampleMongoApplication}.
*
* @author Dave Syer
* @author Andy Wilkinson
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleMongoApplication.class)
@IntegrationTest
public class SampleMongoApplicationTests {
private static final Pattern TIMEOUT_MESSAGE_PATTERN = Pattern
.compile("Timed out after [0-9]+ ms while waiting for a server.*");
@Rule
public OutputCapture outputCapture = new OutputCapture();
@ClassRule
public static OutputCapture outputCapture = new OutputCapture();
@Test
public void testDefaultSettings() throws Exception {
try {
SampleMongoApplication.main(new String[0]);
}
catch (IllegalStateException ex) {
if (serverNotRunning(ex)) {
return;
}
}
String output = this.outputCapture.toString();
String output = SampleMongoApplicationTests.outputCapture.toString();
assertTrue("Wrong output: " + output,
output.contains("firstName='Alice', lastName='Smith'"));
}
private boolean serverNotRunning(IllegalStateException ex) {
@SuppressWarnings("serial")
NestedCheckedException nested = new NestedCheckedException("failed", ex) {
};
Throwable root = nested.getRootCause();
if (root instanceof MongoTimeoutException) {
if (root.getMessage().contains("Unable to connect to any server")) {
return true;
}
if (TIMEOUT_MESSAGE_PATTERN.matcher(root.getMessage()).matches()) {
return true;
}
}
return false;
}
}