(function() {

  if (typeof getToolTest === 'undefined') {
    load('jstests/configs/plain_28.config.js');
  }

  // Tests using mongorestore to restore data to multiple dbs.

  jsTest.log('Testing restoration to multiple dbs');

  var toolTest = getToolTest('multiple_dbs');
  var commonToolArgs = getCommonToolArguments();

  // where we'll put the dump
  var dumpTarget = 'multiple_dbs_dump';
  resetDbpath(dumpTarget);

  // the dbs we will be using
  var dbOne = toolTest.db.getSiblingDB('dbOne');
  var dbTwo = toolTest.db.getSiblingDB('dbTwo');

  // we'll use two collections in each db, with one of
  // the collection names common across the dbs
  var oneOnlyCollName = 'dbOneColl';
  var twoOnlyCollName = 'dbTwoColl';
  var sharedCollName = 'bothColl';

  // insert a bunch of data
  var data = {};
  data[oneOnlyCollName] = [];
  data[twoOnlyCollName] = [];
  data[sharedCollName+'one'] = [];
  data[sharedCollName+'two'] = [];
  for (var i = 0; i < 50; i++) {
    data[oneOnlyCollName].push({_id: i+'_'+oneOnlyCollName});
    data[twoOnlyCollName].push({_id: i+'_'+twoOnlyCollName});
    data[sharedCollName+'one'].push({_id: i+'_dbOne_'+sharedCollName});
    data[sharedCollName+'two'].push({_id: i+'_dbTwo_'+sharedCollName});
  }
  dbOne[oneOnlyCollName].insertMany(data[oneOnlyCollName]);
  dbTwo[twoOnlyCollName].insertMany(data[twoOnlyCollName]);
  dbOne[sharedCollName].insertMany(data[sharedCollName+'one']);
  dbTwo[sharedCollName].insertMany(data[sharedCollName+'two']);

  // sanity check the insertion worked
  assert.eq(50, dbOne[oneOnlyCollName].count());
  assert.eq(50, dbTwo[twoOnlyCollName].count());
  assert.eq(50, dbOne[sharedCollName].count());
  assert.eq(50, dbTwo[sharedCollName].count());

  // dump the data
  var ret = toolTest.runTool.apply(toolTest, ['dump']
    .concat(getDumpTarget(dumpTarget))
    .concat(commonToolArgs));
  assert.eq(0, ret);

  // drop the databases
  dbOne.dropDatabase();
  dbTwo.dropDatabase();

  // restore the data
  ret = toolTest.runTool.apply(toolTest, ['restore']
    .concat(getRestoreTarget(dumpTarget))
    .concat(commonToolArgs));
  assert.eq(0, ret);

  // make sure the data was restored properly
  assert.eq(50, dbOne[oneOnlyCollName].count());
  assert.eq(50, dbTwo[twoOnlyCollName].count());
  assert.eq(50, dbOne[sharedCollName].count());
  assert.eq(50, dbTwo[sharedCollName].count());
  for (i = 0; i < 50; i++) {
    assert.eq(1, dbOne[oneOnlyCollName].count({_id: i+'_'+oneOnlyCollName}));
    assert.eq(1, dbTwo[twoOnlyCollName].count({_id: i+'_'+twoOnlyCollName}));
    assert.eq(1, dbOne[sharedCollName].count({_id: i+'_dbOne_'+sharedCollName}));
    assert.eq(1, dbTwo[sharedCollName].count({_id: i+'_dbTwo_'+sharedCollName}));
  }

  // success
  toolTest.stop();

}());
