@BeforeMethod
  public void setUp() throws Exception {
    // do cleanup of other tests that may have started writers.
    // this is necessary in a non-forked test suite environment
    // where partition ids could collide
    Utils.finishBackupWriters();

    m_tmpDir = new File(System.getProperty("java.io.tmpdir"), "PartitionStoreStageTest");
    if (!m_tmpDir.exists()) {
      assertTrue("Failed to make the directories.", m_tmpDir.mkdirs());
    }

    // Add a dummy partition
    String server = "test";
    String storage = m_tmpDir + "/storage";

    File storageDir = new File(storage);
    if (!storageDir.exists()) {
      assertTrue("Failed to make the storage directory.", storageDir.mkdirs());
      if (!storageDir.exists()) {
        s_logger.warn("Storage Directory " + storage + " does not exist.");
      }
    }

    String mapped = m_tmpDir + "/mapped";

    File mappedDir = new File(mapped);
    if (!mappedDir.exists()) {
      assertTrue("Failed to make the mapped directories.", mappedDir.mkdirs());
      if (!mappedDir.exists()) {
        s_logger.warn("Storage Directory " + mapped + " does not exist.");
      }
    }

    if (m_part == null) {
      IPartitionManager pm = ManagementContainer.getInstance().getPartitionManager();
      m_part = pm.initializePartition(TEST_PID, server, storage, mapped);
    }
    Utils.startBackupWriters();
  }
  @Test
  public void testBackupFailure() throws Exception {
    IPartitionManager pm = ManagementContainer.getInstance().getPartitionManager();

    m_part.setAccessibleRemotely(true);
    pm.updatePartition(m_part);
    m_part = pm.getPartition(TEST_PID);

    Customer cust =
        TestUtils.createActiveMailboxTestCustomer(Capabilities.ActiveMailboxSupport.ARCHIVE);
    try {
      MockMessageImporter mock = new MockMessageImporter(getClass().getSimpleName());
      PartitionStoreStage stg =
          new PartitionStoreStage(mock) {
            public void finish() {
              super.finish();
              ((StatComponent) m_importer.getStatComponent()).stop();
            }
          };
      MockStageController ctrl = new MockStageController("test");
      stg.setStageController(ctrl);

      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(0, mock.getNumSucceededMessages());
      // Partition dummyPart = new Partition(10, "sdcvsdv");

      File backupSubDir = new File(m_part.getStorageDirectory(), Partition.BACKUP_SUBDIR);

      Pair<Partition, List<IMessageContext>> buffer =
          new Pair<Partition, List<IMessageContext>>(
              m_part, generateMockMessageContext(10000, 10, cust));

      stg.process(buffer);

      // Nothing should have failed
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(10, ctrl.getOutputCount());

      assertTrue(backupSubDir.exists());

      // Stop backup writers, delete backup directory and then make sure everything works when we
      // start back up
      Utils.finishBackupWriters();

      FileUtils.deleteDirectory(backupSubDir);

      assertFalse(backupSubDir.exists());
      Utils.startBackupWriters();

      ctrl.resetOutputCount();
      buffer =
          new Pair<Partition, List<IMessageContext>>(
              m_part, generateMockMessageContext(100000, 10, cust));

      stg.process(buffer);

      // Nothing should have failed
      assertEquals(0, mock.getNumFailedMessages());
      assertEquals(10, ctrl.getOutputCount());

      assertTrue(backupSubDir.exists());
      stg.finish();
    } finally {
      TestUtils.quietDeleteCustomer(cust);
    }
  }
 public void setupPartition(int partitionId, int storageId) {
   IPartitionManager pMgr = ManagementContainer.getInstance().getPartitionManager();
   m_partitions.add(
       pMgr.initializePartition(partitionId, "localhost", "storage" + storageId, null));
 }