Skip to content

Commit

Permalink
Init support mongo embed - add junit rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspouzac committed Apr 2, 2014
1 parent d868788 commit ebee44a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions restx-jongo-specs-tests/pom.xml
Expand Up @@ -18,6 +18,7 @@

<properties>
<restx.version>0.32-SNAPSHOT</restx.version>
<de.flapdoodle.embed.version>1.42</de.flapdoodle.embed.version>

This comment has been minimized.

Copy link
@xhanin

xhanin Apr 2, 2014

just for the record, pom.xml is generated from md.restx.json using restx build generate pom, so the source is the md.restx.json file. But I'll take care of updating it.

<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
</properties>
Expand All @@ -38,5 +39,10 @@
<artifactId>restx-jongo</artifactId>
<version>${restx.version}</version>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>${de.flapdoodle.embed.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,63 @@
package restx.jongo.specs.tests;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import restx.mongo.MongoModule;

import com.mongodb.MongoClient;

import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;

public class MongoEmbedRule implements TestRule {

@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {

@Override
public void evaluate() throws Throwable {
InetAddress addr = Network.getLocalHost();
int port = Network.getFreeServerPort(addr);

System.setProperty(MongoModule.MONGO_URI, new StringBuilder(
"mongodb://").append(addr.getHostName()).append(":")
.append(port).toString());

MongodStarter runtime = MongodStarter.getDefaultInstance();
MongodExecutable _mongodExe = runtime
.prepare(new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(port, hostIsIPv6(addr))).build());
MongodProcess _mongod = _mongodExe.start();
new MongoClient(addr.getHostName(), port);

base.evaluate();

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

private boolean hostIsIPv6(InetAddress addr)
throws UnknownHostException {
byte[] ipAddr = addr.getAddress();
if (ipAddr.length > 4) {
return true;
}
return false;
}

};
}

}
1 change: 1 addition & 0 deletions restx-jongo/src/main/java/restx/mongo/MongoModule.java
Expand Up @@ -26,6 +26,7 @@
public class MongoModule {
public static final String MONGO_CLIENT_NAME = "mongoClient";
public static final String MONGO_DB_NAME = "mongo.db";
public static final String MONGO_URI = "mongo.uri";

@Provides @Named(MONGO_CLIENT_NAME)
public MongoClient mongoClient(MongoSettings settings) {
Expand Down
2 changes: 1 addition & 1 deletion restx-jongo/src/main/java/restx/mongo/MongoSettings.java
Expand Up @@ -12,7 +12,7 @@ public interface MongoSettings {
@SettingsKey(key = MongoModule.MONGO_DB_NAME, doc = "the name of the mongo database to use")
String dbName();

@SettingsKey(key = "mongo.uri", defaultValue = "mongodb://localhost:27017",
@SettingsKey(key = MongoModule.MONGO_URI, defaultValue = "mongodb://localhost:27017",
doc = "the mongo URI to use to connect to mongodb. " +
"See http://api.mongodb.org/java/current/com/mongodb/MongoClientURI.html")
String uri();
Expand Down

0 comments on commit ebee44a

Please sign in to comment.