NativeIO { public static void renameTo(File src, File dst) throws IOException { if (!nativeLoaded) { if (!src.renameTo(dst)) { throw new IOException("renameTo(src=" + src + ", dst=" + dst + ") failed."); } } else { renameTo0(src.getAbsolutePath(), dst.getAbsolutePath()); } }  static boolean isAvailable(); static String getOwner(FileDescriptor fd); static FileInputStream getShareDeleteFileInputStream(File f); static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset); static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions); static void renameTo(File src, File dst); static void link(File src, File dst); static void copyFileUnbuffered(File src, File dst);  }
@Test (timeout = 30000) public void testCreateFile() throws Exception { if (!Path.WINDOWS) { return; } LOG.info("Open a file on Windows with SHARE_DELETE shared mode"); try { File testfile = new File(TEST_DIR, "testCreateFile"); assertTrue("Create test subject", testfile.exists() || testfile.createNewFile()); FileDescriptor fd = NativeIO.Windows.createFile( testfile.getCanonicalPath(), NativeIO.Windows.GENERIC_READ, NativeIO.Windows.FILE_SHARE_READ | NativeIO.Windows.FILE_SHARE_WRITE | NativeIO.Windows.FILE_SHARE_DELETE, NativeIO.Windows.OPEN_EXISTING); FileInputStream fin = new FileInputStream(fd); try { fin.read(); File newfile = new File(TEST_DIR, "testRenamedFile"); boolean renamed = testfile.renameTo(newfile); assertTrue("Rename failed.", renamed); fin.read(); } catch (Exception e) { fail("Got unexpected exception: " + e.getMessage()); } finally { fin.close(); } } catch (Exception e) { fail("Got unexpected exception: " + e.getMessage()); } }
@Test (timeout = 30000) public void testRenameTo() throws Exception { final File TEST_DIR = new File(new File( System.getProperty("test.build.data","build/test/data")), "renameTest"); assumeTrue(TEST_DIR.mkdirs()); File nonExistentFile = new File(TEST_DIR, "nonexistent"); File targetFile = new File(TEST_DIR, "target"); try { NativeIO.renameTo(nonExistentFile, targetFile); Assert.fail(); } catch (NativeIOException e) { if (Path.WINDOWS) { Assert.assertEquals( String.format("The system cannot find the file specified.%n"), e.getMessage()); } else { Assert.assertEquals(Errno.ENOENT, e.getErrno()); } } File sourceFile = new File(TEST_DIR, "source"); Assert.assertTrue(sourceFile.createNewFile()); NativeIO.renameTo(sourceFile, sourceFile); NativeIO.renameTo(sourceFile, targetFile); sourceFile = new File(TEST_DIR, "source"); Assert.assertTrue(sourceFile.createNewFile()); File badTarget = new File(targetFile, "subdir"); try { NativeIO.renameTo(sourceFile, badTarget); Assert.fail(); } catch (NativeIOException e) { if (Path.WINDOWS) { Assert.assertEquals( String.format("The parameter is incorrect.%n"), e.getMessage()); } else { Assert.assertEquals(Errno.ENOTDIR, e.getErrno()); } } FileUtils.deleteQuietly(TEST_DIR); }