forked from restx/restx
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init support mongo embed - add junit rule
- Loading branch information
1 parent
d868788
commit ebee44a
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
restx-jongo-specs-tests/src/main/java/restx/jongo/specs/tests/MongoEmbedRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
just for the record, pom.xml is generated from
md.restx.json
usingrestx build generate pom
, so the source is themd.restx.json
file. But I'll take care of updating it.