@AfterClass public static void stopMongo() { if (mongoDb != null) { mongoDb.stop(); } }
@Override public void after() { if (client != null) { client.close(); client = null; } if (mongoProc != null) { mongoProc.stop(); mongoProc = null; } if (mongoExec != null) { mongoExec.stop(); mongoExec = null; } }
@BeforeClass public static void startMongoDB() throws UnknownHostException, IOException { MongodStarter starter = MongodStarter.getDefaultInstance(); IMongodConfig mongodConfig = new MongodConfigBuilder() .version(Version.Main.PRODUCTION) .net(new Net(PORT, Network.localhostIsIPv6())) .build(); mongodExecutable = null; try { mongodExecutable = starter.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); } catch (Exception e) { if (mongodExecutable != null) mongodExecutable.stop(); } }
// ### Usage public void testStandard() throws UnknownHostException, IOException { int port = 12345; MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6()); MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodExecutable mongodExecutable = null; try { mongodExecutable = runtime.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); Mongo mongo = new Mongo("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(); } }
// ### ... automagic public void testFreeServerPortAuto() throws UnknownHostException, IOException { MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0); MongodStarter runtime = MongodStarter.getDefaultInstance(); MongodExecutable mongodExecutable = null; try { mongodExecutable = runtime.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); Mongo mongo = new Mongo( new ServerAddress( mongodConfig.net().getServerAddress(), mongodConfig.net().getPort())); 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(); } }
// ### Customize Artifact Storage public void testCustomizeArtifactStorage() throws IOException { MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, 12345, Network.localhostIsIPv6()); /// - 8<- - - - IDirectory artifactStorePath = new FixedPath(System.getProperty("user.home") + "/.embeddedMongodbCustomPath"); ITempNaming executableNaming = new UUIDTempNaming(); RuntimeConfig runtimeConfig = new RuntimeConfig(); runtimeConfig.getDownloadConfig().setArtifactStorePathNaming(artifactStorePath); runtimeConfig.setExecutableNaming(executableNaming); MongodStarter runtime = MongodStarter.getInstance(runtimeConfig); MongodExecutable mongodExe = runtime.prepare(mongodConfig); /// - >8- - - - MongodProcess mongod = mongodExe.start(); mongod.stop(); mongodExe.stop(); }
@AfterClass public static void stopDB() { if (mongodExecutable != null) { mongodExecutable.stop(); } }
@AfterClass public static void shutdownDB() throws InterruptedException { mongodProcess.stop(); mongodExecutable.stop(); }