Skip to content

...will provide a platform neutral way for running mongodb in unittests.

Notifications You must be signed in to change notification settings

tw1nk/de.flapdoodle.embed.mongo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Organisation Flapdoodle OSS

Build Status

We are now a github organisation. You are invited to participate. :)

Embedded MongoDB

Embedded MongoDB will provide a platform neutral way for running mongodb in unittests.

Why?

  • dropping databases causing some pains (often you have to wait long time after each test)
  • its easy, much easier as installing right version by hand
  • you can change version per test

Dependencies

Build on top of

Other ways to use Embedded MongoDB

Comments about Embedded MongoDB in the Wild

Other MongoDB Stuff

Howto

Maven

IMPORTANT NOTE: maven groupId and artifactId change

  • groupId from de.flapdoodle.embedmongo to de.flapdoodle.embed
  • artifactId from de.flapdoodle.embedmongo to de.flapdoodle.embed.mongo

Stable (Maven Central Repository, Released: 31.01.2014 - wait 24hrs for maven central)

<dependency>
	<groupId>de.flapdoodle.embed</groupId>
	<artifactId>de.flapdoodle.embed.mongo</artifactId>
	<version>1.42</version>
</dependency>

Snapshots (Repository http:https://oss.sonatype.org/content/repositories/snapshots)

<dependency>
	<groupId>de.flapdoodle.embed</groupId>
	<artifactId>de.flapdoodle.embed.mongo</artifactId>
	<version>1.43-SNAPSHOT</version>
</dependency>

Gradle

Make sure you have mavenCentral() in your repositories or that your enterprise/local server prixies the maven central repository.

dependencies {
	testCompile group: "de.flapdoodle.embed", name: "de.flapdoodle.embed.mongo", "version: 1.42"
}

Build from source

When you fork or clone our branch you should always be able to build the library by running

mvn package

There is also a build.gradle file available which might sometimes be outdated but we try to keep it working. So the gradle command is

gradle build

Or if you want to use the gradle wrapper:

./gradlew build

Changelog

Changelog

Supported Versions

Versions: some older, a stable and a development version Support for Linux, Windows and MacOSX.

Usage

import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder;

...

int port = 12345;
IMongodConfig mongodConfig = new MongodConfigBuilder()
	.version(Version.Main.PRODUCTION)
	.net(new Net(port, Network.localhostIsIPv6()))
	.build();

MongodStarter runtime = MongodStarter.getDefaultInstance();

MongodExecutable mongodExecutable = null;
try {
	mongodExecutable = runtime.prepare(mongodConfig);
	MongodProcess mongod = mongodExecutable.start();

	MongoClient mongo = new MongoClient("localhost", port);
	DB db = mongo.getDB("test");
	DBCollection col = db.createCollection("testCol", new BasicDBObject());
	col.save(new BasicDBObject("testDoc", new Date()));

} finally {
	if (mongodExecutable != null)
		mongodExecutable.stop();
}

Usage - custom mongod filename

import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder;

...

int port = 12345;
IMongodConfig mongodConfig = new MongodConfigBuilder()
	.version(Version.Main.PRODUCTION)
	.net(new Net(port, Network.localhostIsIPv6()))
	.build();

Command command = Command.MongoD;

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(command)
	.artifactStore(new ArtifactStoreBuilder()
		.defaults(command)
		.download(new DownloadConfigBuilder()
			.defaultsForCommand(command))
			.executableNaming(new UserTempNaming()))
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

MongodExecutable mongodExecutable = null;
try {
	mongodExecutable = runtime.prepare(mongodConfig);
	MongodProcess mongod = mongodExecutable.start();

	MongoClient mongo = new MongoClient("localhost", port);
	DB db = mongo.getDB("test");
	DBCollection col = db.createCollection("testCol", new BasicDBObject());
	col.save(new BasicDBObject("testDoc", new Date()));

} finally {
	if (mongodExecutable != null)
		mongodExecutable.stop();
}

Unit Tests

public abstract class AbstractMongoDBTest extends TestCase {

	private MongodExecutable _mongodExe;
	private MongodProcess _mongod;

	private MongoClient _mongo;
	@Override
	protected void setUp() throws Exception {

		MongodStarter runtime = MongodStarter.getDefaultInstance();
		_mongodExe = runtime.prepare(new MongodConfigBuilder()
			.version(Version.Main.PRODUCTION)
			.net(new Net(12345, Network.localhostIsIPv6()))
			.build());
		_mongod = _mongodExe.start();

		super.setUp();

		_mongo = new MongoClient("localhost", 12345);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();

		_mongod.stop();
		_mongodExe.stop();
	}

	public Mongo getMongo() {
		return _mongo;
	}

}

... with some more help

...
MongodForTestsFactory factory = null;
try {
	factory = MongodForTestsFactory.with(Version.Main.PRODUCTION);

	MongoClient mongo = factory.newMongo();
	DB db = mongo.getDB("test-" + UUID.randomUUID());
	DBCollection col = db.createCollection("testCol", new BasicDBObject());
	col.save(new BasicDBObject("testDoc", new Date()));

} finally {
	if (factory != null)
		factory.shutdown();
}
...

Customize Download URL

...
Command command = Command.MongoD;

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(command)
	.artifactStore(new ArtifactStoreBuilder()
		.defaults(command)
		.download(new DownloadConfigBuilder()
			.defaultsForCommand(command)
			.downloadPath("http:https://my.custom.download.domain/")))
	.build();
...

Customize Artifact Storage

...
IDirectory artifactStorePath = new FixedPath(System.getProperty("user.home") + "/.embeddedMongodbCustomPath");
ITempNaming executableNaming = new UUIDTempNaming();

Command command = Command.MongoD;

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(command)
	.artifactStore(new ArtifactStoreBuilder()
		.defaults(command)
		.download(new DownloadConfigBuilder()
			.defaultsForCommand(command)
			.artifactStorePath(artifactStorePath))
		.executableNaming(executableNaming))
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
MongodExecutable mongodExe = runtime.prepare(mongodConfig);
...

Usage - custom mongod process output

... to console with line prefix

...
ProcessOutput processOutput = new ProcessOutput(Processors.namedConsole("[mongod>]"),
		Processors.namedConsole("[MONGOD>]"), Processors.namedConsole("[console>]"));

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(Command.MongoD)
	.processOutput(processOutput)
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...

... to file

...
IStreamProcessor mongodOutput = Processors.named("[mongod>]",
		new FileStreamProcessor(File.createTempFile("mongod", "log")));
IStreamProcessor mongodError = new FileStreamProcessor(File.createTempFile("mongod-error", "log"));
IStreamProcessor commandsOutput = Processors.namedConsole("[console>]");

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(Command.MongoD)
	.processOutput(new ProcessOutput(mongodOutput, mongodError, commandsOutput))
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...

...
public class FileStreamProcessor implements IStreamProcessor {

	private FileOutputStream outputStream;

	public FileStreamProcessor(File file) throws FileNotFoundException {
		outputStream = new FileOutputStream(file);
	}

	@Override
	public void process(String block) {
		try {
			outputStream.write(block.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onProcessed() {
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
...

... to java logging

...
Logger logger = Logger.getLogger(getClass().getName());

ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Level.INFO), Processors.logTo(logger,
		Level.SEVERE), Processors.named("[console>]", Processors.logTo(logger, Level.FINE)));

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaultsWithLogger(Command.MongoD,logger)
	.processOutput(processOutput)
	.artifactStore(new ArtifactStoreBuilder()
		.defaults(Command.MongoD)
		.download(new DownloadConfigBuilder()
			.defaultsForCommand(Command.MongoD)
			.progressListener(new LoggingProgressListener(logger, Level.FINE))))
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...

... to default java logging (the easy way)

...
Logger logger = Logger.getLogger(getClass().getName());

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaultsWithLogger(Command.MongoD, logger)
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...

... to null device

...
Logger logger = Logger.getLogger(getClass().getName());

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaultsWithLogger(Command.MongoD, logger)
	.processOutput(ProcessOutput.getDefaultInstanceSilent())
	.build();

MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...

Custom Version

...
int port = 12345;
IMongodConfig mongodConfig = new MongodConfigBuilder()
	.version(Versions.withFeatures(new GenericVersion("2.0.7-rc1"),Feature.SYNC_DELAY))
	.net(new Net(port, Network.localhostIsIPv6()))
	.build();

MongodStarter runtime = MongodStarter.getDefaultInstance();
MongodProcess mongod = null;

MongodExecutable mongodExecutable = null;
try {
	mongodExecutable = runtime.prepare(mongodConfig);
	mongod = mongodExecutable.start();

	...

} finally {
	if (mongod != null) {
		mongod.stop();
	}
	if (mongodExecutable != null)
		mongodExecutable.stop();
}
...

Main Versions

IVersion version = Version.V2_2_5;
// uses latest supported 2.2.x Version
version = Version.Main.V2_2;
// uses latest supported production version
version = Version.Main.PRODUCTION;
// uses latest supported development version
version = Version.Main.DEVELOPMENT;

Use Free Server Port

Warning: maybe not as stable, as expected.

... by hand

...
int port = Network.getFreeServerPort();
...

... automagic

...
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).build();

MongodStarter runtime = MongodStarter.getDefaultInstance();

MongodExecutable mongodExecutable = null;
MongodProcess mongod = null;
try {
	mongodExecutable = runtime.prepare(mongodConfig);
	mongod = mongodExecutable.start();

	MongoClient mongo = new MongoClient(new ServerAddress(mongodConfig.net().getServerAddress(), mongodConfig.net().getPort()));
	...

} finally {
	if (mongod != null) {
		mongod.stop();
	}
	if (mongodExecutable != null)
		mongodExecutable.stop();
}
...

... custom timeouts

...
IMongodConfig mongodConfig = new MongodConfigBuilder()
	.version(Version.Main.PRODUCTION)
	.timeout(new Timeout(30000))
	.build();
...

Command Line Post Processing

...
ICommandLinePostProcessor postProcessor= ...

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
	.defaults(Command.MongoD)
	.commandLinePostProcessor(postProcessor)
	.build();
...

Custom Command Line Options

We changed the syncDelay to 0 which turns off sync to disc. To turn on default value used defaultSyncDelay().

IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.cmdOptions(new MongoCmdOptionsBuilder()
	.syncDeplay(10)
	.build())
.build();
...

Snapshot database files from temp dir

We changed the syncDelay to 0 which turns off sync to disc. To get the files to create an snapshot you must turn on default value (use defaultSyncDelay()).

IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.processListener(new ProcessListenerBuilder()
	.copyDbFilesBeforeStopInto(destination)
	.build())
.cmdOptions(new MongoCmdOptionsBuilder()
	.defaultSyncDeplay()
	.build())
.build();
...

Start mongos with mongod instance

this is an very easy example to use mongos and mongod

int port = 12121;
int defaultConfigPort = 12345;
String defaultHost = "localhost";

MongodProcess mongod = startMongod(defaultConfigPort);

try {
	MongosProcess mongos = startMongos(port, defaultConfigPort, defaultHost);
	try {
		MongoClient mongoClient = new MongoClient(defaultHost, defaultConfigPort);
		System.out.println("DB Names: " + mongoClient.getDatabaseNames());
	} finally {
		mongos.stop();
	}
} finally {
	mongod.stop();
}

private MongosProcess startMongos(int port, int defaultConfigPort, String defaultHost) throws UnknownHostException,
		IOException {
	IMongosConfig mongosConfig = new MongosConfigBuilder()
		.version(Version.Main.PRODUCTION)
		.net(new Net(port, Network.localhostIsIPv6()))
		.configDB(defaultHost + ":" + defaultConfigPort)
		.build();

	MongosExecutable mongosExecutable = MongosStarter.getDefaultInstance().prepare(mongosConfig);
	MongosProcess mongos = mongosExecutable.start();
	return mongos;
}

private MongodProcess startMongod(int defaultConfigPort) throws UnknownHostException, IOException {
	IMongodConfig mongoConfigConfig = new MongodConfigBuilder()
		.version(Version.Main.PRODUCTION)
		.net(new Net(defaultConfigPort, Network.localhostIsIPv6()))
		.configServer(true)
		.build();

	MongodExecutable mongodExecutable = MongodStarter.getDefaultInstance().prepare(mongoConfigConfig);
	MongodProcess mongod = mongodExecutable.start();
	return mongod;
}

YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .NET Profiler.

About

...will provide a platform neutral way for running mongodb in unittests.

http:https://flapdoodle-oss.github.io/de.flapdoodle.embed.mongo

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Java 95.2%
  • Shell 4.0%
  • Groovy 0.8%