LocalFileSystem extends FileSystem { public LocalFileSystem() { this.workingDir = new Path(System.getProperty("user.dir")).makeQualified(this); String tmp = "unknownHost"; try { tmp = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { LOG.error("Could not resolve local host", e); } this.hostName = tmp; } LocalFileSystem(); @Override BlockLocation[] getFileBlockLocations(final FileStatus file, final long start, final long len); @Override FileStatus getFileStatus(Path f); @Override URI getUri(); @Override Path getWorkingDirectory(); @Override Path getHomeDirectory(); @Override void initialize(final URI name); @Override FSDataInputStream open(final Path f, final int bufferSize); @Override FSDataInputStream open(final Path f); @Override FileStatus[] listStatus(final Path f); @Override boolean delete(final Path f, final boolean recursive); boolean mkdirs(final Path f); @Override FSDataOutputStream create(final Path f, final boolean overwrite, final int bufferSize,
			final short replication, final long blockSize); @Override FSDataOutputStream create(final Path f, final boolean overwrite); @Override boolean rename(final Path src, final Path dst); @Override boolean isDistributedFS();  }
@Test public void testLocalFilesystem() { File tempdir = new File(CommonTestUtils.getTempDir() + File.separator + CommonTestUtils.getRandomDirectoryName() + File.separator); while (tempdir.exists()) { tempdir = new File(CommonTestUtils.getTempDir() + File.separator + CommonTestUtils.getRandomDirectoryName() + File.separator); } final File testfile1 = new File(tempdir.getAbsolutePath() + File.separator + CommonTestUtils.getRandomFilename()); final File testfile2 = new File(tempdir.getAbsolutePath() + File.separator + CommonTestUtils.getRandomFilename()); final Path pathtotestfile1 = new Path(testfile1.toURI().getPath()); final Path pathtotestfile2 = new Path(testfile2.toURI().getPath()); try { final LocalFileSystem lfs = new LocalFileSystem(); final Path pathtotmpdir = new Path(tempdir.toURI().getPath()); assertFalse(lfs.exists(pathtotmpdir)); tempdir.mkdirs(); assertTrue(lfs.exists(pathtotmpdir)); final FileStatus localstatus1 = lfs.getFileStatus(pathtotmpdir); assertTrue(localstatus1.isDir()); final FileStatus[] statusforfiles = lfs.listStatus(pathtotmpdir); assertTrue(statusforfiles.length == 0); lfs.delete(pathtotmpdir, true); assertFalse(lfs.exists(pathtotmpdir)); assertFalse(tempdir.exists()); lfs.mkdirs(pathtotmpdir); assertTrue(tempdir.exists()); final FSDataOutputStream lfsoutput1 = lfs.create(pathtotestfile1, false); testfile2.createNewFile(); assertTrue(testfile1.exists()); assertTrue(lfs.exists(pathtotestfile2)); final byte[] testbytes = { 1, 2, 3, 4, 5 }; lfsoutput1.write(testbytes); lfsoutput1.close(); assertEquals(testfile1.length(), 5l); final FileInputStream fisfile1 = new FileInputStream(testfile1); byte[] testbytestest = new byte[5]; fisfile1.read(testbytestest); fisfile1.close(); assertTrue(Arrays.equals(testbytes, testbytestest)); assertEquals(lfs.getFileStatus(pathtotestfile1).getLen(), testfile1.length()); assertEquals(lfs.listStatus(pathtotestfile1)[0].getLen(), testfile1.length()); final FileOutputStream fosfile2 = new FileOutputStream(testfile2); fosfile2.write(testbytes); fosfile2.close(); testbytestest = new byte[5]; final FSDataInputStream lfsinput2 = lfs.open(pathtotestfile2); assertEquals(lfsinput2.read(testbytestest), 5); lfsinput2.close(); assertTrue(Arrays.equals(testbytes, testbytestest)); assertEquals(lfs.listStatus(pathtotmpdir).length, 2); assertEquals(lfs.getFileBlockLocations(lfs.getFileStatus(pathtotestfile1), 0, 0).length, 1); assertTrue(lfs.delete(pathtotestfile1, false)); assertTrue(lfs.delete(pathtotmpdir, true)); assertTrue(!tempdir.exists()); } catch (IOException e) { fail(e.getMessage()); } finally { testfile1.delete(); testfile2.delete(); tempdir.delete(); } }