@Test
public void testDefaultBinaryManager() throws Exception {

    DefaultBinaryManager binaryManager = new DefaultBinaryManager();
    binaryManager.initialize(new BinaryManagerDescriptor());
    assertEquals(0, countFiles(binaryManager.getStorageDir()));
    Binary binary = binaryManager.getBinary(CONTENT_MD5);
    assertNull(binary);
    // store binary
    byte[] bytes = CONTENT.getBytes("UTF-8");
    binary = binaryManager.getBinary(new ByteArrayInputStream(bytes));
    assertNotNull(binary);
    assertEquals(1, countFiles(binaryManager.getStorageDir()));
    // get binary
    binary = binaryManager.getBinary(CONTENT_MD5);
    assertNotNull(binary);
    assertEquals(bytes.length, binary.getLength());
    assertEquals(CONTENT, IOUtils.toString(binary.getStream(), "UTF-8"));
    // other binary we'll GC
    binaryManager.getBinary(new ByteArrayInputStream(
            "abc".getBytes("UTF-8")));
    assertEquals(2, countFiles(binaryManager.getStorageDir()));
    // sleep before GC to pass its time threshold
    Thread.sleep(3 * 1000);
    // create another binary after time threshold, it won't be GCed
    binaryManager.getBinary(new ByteArrayInputStream(
            "defg".getBytes("UTF-8")));
    assertEquals(3, countFiles(binaryManager.getStorageDir()));
    // GC in non-delete mode
    BinaryGarbageCollector gc = binaryManager.getGarbageCollector();
    assertFalse(gc.isInProgress());
    gc.start();
    assertTrue(gc.isInProgress());
    gc.mark(CONTENT_MD5);
    assertTrue(gc.isInProgress());
    gc.stop(false);
    assertFalse(gc.isInProgress());
    BinaryManagerStatus status = gc.getStatus();
    assertEquals(2, status.numBinaries);
    assertEquals(bytes.length + 4, status.sizeBinaries);
    assertEquals(1, status.numBinariesGC);
    assertEquals(3, status.sizeBinariesGC);
    // still there
    assertEquals(3, countFiles(binaryManager.getStorageDir()));
    // real GC
    gc = binaryManager.getGarbageCollector();
    assertFalse(gc.isInProgress());
    gc.start();
    assertTrue(gc.isInProgress());
    gc.mark(CONTENT_MD5);
    assertTrue(gc.isInProgress());
    gc.stop(true);
    assertFalse(gc.isInProgress());
    status = gc.getStatus();
    assertEquals(2, status.numBinaries);
    assertEquals(bytes.length + 4, status.sizeBinaries);
    assertEquals(1, status.numBinariesGC);
    assertEquals(3, status.sizeBinariesGC);
    // one file gone
    assertEquals(2, countFiles(binaryManager.getStorageDir()));
    binaryManager.close();
}