@Test
  public void testPurgeUnexpectedFiles() throws Exception {
    final DefaultDiskStorage storage = getStorageSupplier(1).get();

    final String resourceId = "file1";
    final byte[] CONTENT = "content".getBytes("UTF-8");

    File file = writeFileToStorage(storage, resourceId, CONTENT);

    // check file exists
    Assert.assertTrue(file.exists());
    Assert.assertArrayEquals(CONTENT, Files.toByteArray(file));

    final File unexpectedFile1 = new File(mDirectory, "unexpected-file-1");
    final File unexpectedFile2 = new File(mDirectory, "unexpected-file-2");

    Assert.assertTrue(unexpectedFile1.createNewFile());
    Assert.assertTrue(unexpectedFile2.createNewFile());

    final File unexpectedDir1 = new File(mDirectory, "unexpected-dir-1");
    Assert.assertTrue(unexpectedDir1.mkdirs());

    final File unexpectedDir2 = new File(mDirectory, "unexpected-dir-2");
    Assert.assertTrue(unexpectedDir2.mkdirs());
    final File unexpectedSubfile1 = new File(unexpectedDir2, "unexpected-sub-file-1");
    Assert.assertTrue(unexpectedSubfile1.createNewFile());

    Assert.assertEquals(5, mDirectory.listFiles().length); // 4 unexpected (files+dirs) + ver. dir
    Assert.assertEquals(1, unexpectedDir2.listFiles().length);
    Assert.assertEquals(0, unexpectedDir1.listFiles().length);

    File unexpectedFileInShard = new File(file.getParentFile(), "unexpected-in-shard");
    Assert.assertTrue(unexpectedFileInShard.createNewFile());

    storage.purgeUnexpectedResources();
    Assert.assertFalse(unexpectedFile1.exists());
    Assert.assertFalse(unexpectedFile2.exists());
    Assert.assertFalse(unexpectedSubfile1.exists());
    Assert.assertFalse(unexpectedDir1.exists());
    Assert.assertFalse(unexpectedDir2.exists());

    // check file still exists
    Assert.assertTrue(file.exists());
    // check unexpected sibling is gone
    Assert.assertFalse(unexpectedFileInShard.exists());
    // check the only thing in root is the version directory
    Assert.assertEquals(1, mDirectory.listFiles().length); // just the version directory
  }
Test that purgeUnexpectedResources deletes all files/directories outside the version directory
but leaves untouched the version directory and the content files.

@throws Exception
imagepipeline-base/src/test/java/com/facebook/cache/disk/DefaultDiskStorageTest.java