HadoopArchiveLogs implements Tool { @VisibleForTesting boolean prepareWorkingDir(FileSystem fs, Path workingDir) throws IOException { if (fs.exists(workingDir)) { if (force) { LOG.info("Existing Working Dir detected: -" + FORCE_OPTION + " specified -> recreating Working Dir"); fs.delete(workingDir, true); } else { LOG.info("Existing Working Dir detected: -" + FORCE_OPTION + " not specified -> exiting"); return false; } } fs.mkdirs(workingDir); fs.setPermission(workingDir, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL, true)); return true; } HadoopArchiveLogs(Configuration conf); static void main(String[] args); @Override int run(String[] args); @Override void setConf(Configuration conf); @Override Configuration getConf();  }
@Test(timeout = 5000) public void testPrepareWorkingDir() throws Exception { Configuration conf = new Configuration(); HadoopArchiveLogs hal = new HadoopArchiveLogs(conf); FileSystem fs = FileSystem.getLocal(conf); Path workingDir = new Path("target", "testPrepareWorkingDir"); fs.delete(workingDir, true); Assert.assertFalse(fs.exists(workingDir)); hal.force = false; boolean dirPrepared = hal.prepareWorkingDir(fs, workingDir); Assert.assertTrue(dirPrepared); Assert.assertTrue(fs.exists(workingDir)); Assert.assertEquals( new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL, true), fs.getFileStatus(workingDir).getPermission()); Path dummyFile = new Path(workingDir, "dummy.txt"); fs.createNewFile(dummyFile); Assert.assertTrue(fs.exists(dummyFile)); dirPrepared = hal.prepareWorkingDir(fs, workingDir); Assert.assertFalse(dirPrepared); Assert.assertTrue(fs.exists(workingDir)); Assert.assertTrue(fs.exists(dummyFile)); Assert.assertEquals( new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL, true), fs.getFileStatus(workingDir).getPermission()); hal.force = true; dirPrepared = hal.prepareWorkingDir(fs, workingDir); Assert.assertTrue(dirPrepared); Assert.assertTrue(fs.exists(workingDir)); Assert.assertEquals( new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL, true), fs.getFileStatus(workingDir).getPermission()); Assert.assertFalse(fs.exists(dummyFile)); }