@Test
public void testDeleteHierarchyIfEmpty() throws Exception {

  final File a = testFolder.newFile("test.tmp");
  a.deleteOnExit();
  final File tmpDir = a.getParentFile();
  assertFalse(deleteHierarchyIfEmpty(tmpDir, a));
  assertFalse(deleteHierarchyIfEmpty(a, a));
  // three nested sub dirs
  final File subDir1 = createDirWithRandomNameIn(tmpDir);
  final File subDir2 = createDirWithRandomNameIn(subDir1);
  final File subDir3 = createDirWithRandomNameIn(subDir2);
  final File subDir1File = createFileWithRandomNameIn(subDir1);
  final File subDir3File = createFileWithRandomNameIn(subDir3);
  //
  assertFalse("file in sub dir 3 prevents deletion", deleteHierarchyIfEmpty(subDir1, subDir3));
  assertTrue(subDir3.exists());
  assertTrue(subDir3File.exists());
  assertTrue(subDir3File.delete());
  assertTrue("sub dir 3 and sub dir 2 are empty", deleteHierarchyIfEmpty(subDir1, subDir3));
  assertFalse(subDir3.exists());
  assertFalse(subDir2.exists());
  assertTrue("sub dir 1 has not been deleted", subDir1.exists());
  assertTrue(subDir2.mkdirs());
  assertTrue(subDir3.mkdirs());
  assertTrue(subDir1File.delete());
  assertTrue("all sub dirs are empty", deleteHierarchyIfEmpty(tmpDir, subDir3));
  assertFalse(subDir3.exists());
  assertFalse(subDir2.exists());
  assertFalse(subDir1.exists());
  assertTrue(tmpDir.exists());
}