public EmbeddedMongo() throws IOException {

    // the following logging setup makes the logging a lot less
    // verbose
    // TODO: figure out how to control it further
    Logger logger = Logger.getLogger(getClass().getName());
    IRuntimeConfig runtimeConfig =
        new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger).build();
    MongodStarter starter = MongodStarter.getInstance(runtimeConfig);

    // use instead of logging code above
    // MongodStarter starter = MongodStarter.getDefaultInstance();

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

    _mongodExecutable = starter.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()));
    */
  }
  // ### Usage - custom mongod process output
  // #### ... to console with line prefix
  public void testCustomOutputToConsolePrefix() {

    RuntimeConfig runtimeConfig = new RuntimeConfig();
    runtimeConfig.setProcessOutput(
        new ProcessOutput(
            Processors.namedConsole("[mongod>]"),
            Processors.namedConsole("[MONGOD>]"),
            Processors.namedConsole("[console>]")));
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
  }
  // #### ... to file
  public void testCustomOutputToFile() throws FileNotFoundException, IOException {
    RuntimeConfig runtimeConfig = new RuntimeConfig();
    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>]");

    runtimeConfig.setProcessOutput(new ProcessOutput(mongodOutput, mongodError, commandsOutput));
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
  }
  // #### ... to java logging
  public void testCustomOutputToLogging() throws FileNotFoundException, IOException {
    Logger logger = Logger.getLogger(getClass().getName());

    RuntimeConfig runtimeConfig = new RuntimeConfig();
    runtimeConfig.setProcessOutput(
        new ProcessOutput(
            Processors.logTo(logger, Level.INFO),
            Processors.logTo(logger, Level.SEVERE),
            Processors.named("[console>]", Processors.logTo(logger, Level.FINE))));
    runtimeConfig
        .getDownloadConfig()
        .setProgressListener(new LoggingProgressListener(logger, Level.FINE));
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
  }
Esempio n. 5
0
  @Override
  public void before() throws Throwable {

    int port = Network.getFreeServerPort();
    String portProp = System.getProperty(MONGO_PORT_PROP);
    if (portProp != null && !portProp.isEmpty()) {
      port = Integer.valueOf(portProp);
    }

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

    Command command = Command.MongoD;
    IRuntimeConfig runtimeConfig =
        new RuntimeConfigBuilder()
            .defaultsWithLogger(command, LOGGER)
            .artifactStore(
                new ArtifactStoreBuilder()
                    .defaults(command)
                    .download(
                        new DownloadConfigBuilder()
                            .defaultsForCommand(command)
                            .proxyFactory(new SystemProxy())))
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    mongoExec = runtime.prepare(conf);

    mongoProc = mongoExec.start();

    client =
        new MongoClient(new ServerAddress(conf.net().getServerAddress(), conf.net().getPort()));

    // set the property for our config...
    System.setProperty("dbhost", conf.net().getServerAddress().getHostAddress());
    System.setProperty("dbport", Integer.toString(conf.net().getPort()));
  }
  // ### 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();
  }
  // ### Usage - custom mongod filename
  public void testCustomMongodFilename() throws UnknownHostException, IOException {

    int port = 12345;
    MongodConfig mongodConfig =
        new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6());

    RuntimeConfig runtimeConfig = new RuntimeConfig();
    runtimeConfig.setExecutableNaming(new UserTempNaming());
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    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();
    }
  }
 // #### ... to default java logging (the easy way)
 public void testDefaultOutputToLogging() throws FileNotFoundException, IOException {
   Logger logger = Logger.getLogger(getClass().getName());
   RuntimeConfig runtimeConfig = RuntimeConfig.getInstance(logger);
   MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
 }