MapFile { public static void rename(FileSystem fs, String oldName, String newName) throws IOException { Path oldDir = new Path(oldName); Path newDir = new Path(newName); if (!fs.rename(oldDir, newDir)) { throw new IOException("Could not rename " + oldDir + " to " + newDir); } } protected  MapFile(); static void rename(FileSystem fs, String oldName, String newName); static void delete(FileSystem fs, String name); static long fix(FileSystem fs, Path dir,
                         Class<? extends Writable> keyClass,
                         Class<? extends Writable> valueClass, boolean dryrun,
                         Configuration conf); static void main(String[] args); static final String INDEX_FILE_NAME; static final String DATA_FILE_NAME; }
@Test public void testRename() { final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); MapFile.rename(fs, new Path(TEST_DIR, OLD_FILE_NAME).toString(), new Path(TEST_DIR, NEW_FILE_NAME).toString()); MapFile.delete(fs, new Path(TEST_DIR, NEW_FILE_NAME).toString()); } catch (IOException ex) { fail("testRename error " + ex); } finally { IOUtils.cleanupWithLogger(LOG, writer); } }
@Test public void testRenameWithException() { final String ERROR_MESSAGE = "Can't rename file"; final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); FileSystem spyFs = spy(fs); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); Path oldDir = new Path(TEST_DIR, OLD_FILE_NAME); Path newDir = new Path(TEST_DIR, NEW_FILE_NAME); when(spyFs.rename(oldDir, newDir)).thenThrow( new IOException(ERROR_MESSAGE)); MapFile.rename(spyFs, oldDir.toString(), newDir.toString()); fail("testRenameWithException no exception error !!!"); } catch (IOException ex) { assertEquals("testRenameWithException invalid IOExceptionMessage !!!", ex.getMessage(), ERROR_MESSAGE); } finally { IOUtils.cleanupWithLogger(LOG, writer); } }
@Test public void testRenameWithFalse() { final String ERROR_MESSAGE = "Could not rename"; final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); FileSystem spyFs = spy(fs); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); Path oldDir = new Path(TEST_DIR, OLD_FILE_NAME); Path newDir = new Path(TEST_DIR, NEW_FILE_NAME); when(spyFs.rename(oldDir, newDir)).thenReturn(false); MapFile.rename(spyFs, oldDir.toString(), newDir.toString()); fail("testRenameWithException no exception error !!!"); } catch (IOException ex) { assertTrue("testRenameWithFalse invalid IOExceptionMessage error !!!", ex .getMessage().startsWith(ERROR_MESSAGE)); } finally { IOUtils.cleanupWithLogger(LOG, writer); } }
@Test public void testRename() { final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); MapFile.rename(fs, new Path(TEST_DIR, OLD_FILE_NAME).toString(), new Path(TEST_DIR, NEW_FILE_NAME).toString()); MapFile.delete(fs, new Path(TEST_DIR, NEW_FILE_NAME).toString()); } catch (IOException ex) { fail("testRename error " + ex); } finally { IOUtils.cleanup(null, writer); } }
@Test public void testRenameWithException() { final String ERROR_MESSAGE = "Can't rename file"; final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); FileSystem spyFs = spy(fs); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); Path oldDir = new Path(TEST_DIR, OLD_FILE_NAME); Path newDir = new Path(TEST_DIR, NEW_FILE_NAME); when(spyFs.rename(oldDir, newDir)).thenThrow( new IOException(ERROR_MESSAGE)); MapFile.rename(spyFs, oldDir.toString(), newDir.toString()); fail("testRenameWithException no exception error !!!"); } catch (IOException ex) { assertEquals("testRenameWithException invalid IOExceptionMessage !!!", ex.getMessage(), ERROR_MESSAGE); } finally { IOUtils.cleanup(null, writer); } }
@Test public void testRenameWithFalse() { final String ERROR_MESSAGE = "Could not rename"; final String NEW_FILE_NAME = "test-new.mapfile"; final String OLD_FILE_NAME = "test-old.mapfile"; MapFile.Writer writer = null; try { FileSystem fs = FileSystem.getLocal(conf); FileSystem spyFs = spy(fs); writer = createWriter(OLD_FILE_NAME, IntWritable.class, IntWritable.class); writer.close(); Path oldDir = new Path(TEST_DIR, OLD_FILE_NAME); Path newDir = new Path(TEST_DIR, NEW_FILE_NAME); when(spyFs.rename(oldDir, newDir)).thenReturn(false); MapFile.rename(spyFs, oldDir.toString(), newDir.toString()); fail("testRenameWithException no exception error !!!"); } catch (IOException ex) { assertTrue("testRenameWithFalse invalid IOExceptionMessage error !!!", ex .getMessage().startsWith(ERROR_MESSAGE)); } finally { IOUtils.cleanup(null, writer); } }