FastFileUtility extends FileUtility { public static void copyFile(String srcFile, String newFile) { if (srcFile == null) { throw new FileException("srcFile is null."); } if (newFile == null) { throw new FileException("newFile is null."); } File srcFileObject = new File(srcFile); if (!srcFileObject.exists()) { throw new FileException(srcFile + " is not exist."); } File outputFileObject = new File(newFile); copyFile(srcFileObject, outputFileObject); }  static void copyFile(String srcFile, String newFile); static void closeQuietly(Channel channel); static void closeQuietly(FileInputStream stream); static void closeQuietly(FileOutputStream stream);  }
@Test public void testCopyFile01() throws Exception { URL srcUrl = this.getClass().getResource("/testdata/test01.txt"); String srcFile = srcUrl.getPath(); String newFile = srcFile.replaceAll("01", "01A"); File newFileCheck = new File(newFile); if (newFileCheck.exists()) { newFileCheck.delete(); } FastFileUtility.copyFile(srcFile, newFile); try (FileInputStream fis1 = new FileInputStream(srcFile); BufferedInputStream expected = new BufferedInputStream(fis1); FileInputStream fis2 = new FileInputStream(newFile); BufferedInputStream actual = new BufferedInputStream(fis2);) { int ch = expected.read(); while (-1 != ch) { final int ch2 = actual.read(); if (ch != ch2) { fail(); } ch = expected.read(); } final int ch2 = actual.read(); assertEquals(-1, ch2); } }
@Test public void testCopyFile02() throws Exception { URL srcUrl = this.getClass().getResource("/testdata/test01.txt"); String srcFile = srcUrl.getPath().replaceAll("01", "02"); String newFile = srcFile.replaceAll("02", "02A"); try { FastFileUtility.copyFile(srcFile, newFile); fail(); } catch (FileException e) { assertTrue(e.getMessage().endsWith("test02.txt is not exist.")); } }
@Test public void testCopyFile03() throws Exception { File srcFile = new File("dummy\u0000.txt"); File newFile = new File("newFile"); try { FastFileUtility.copyFile(srcFile, newFile); fail(); } catch (FileException e) { assertEquals("File control operation was failed.", e.getMessage()); } }
@Test public void testCopyFile04() throws Exception { URL srcUrl = this.getClass().getResource("/testdata/test01.txt"); String srcFile = null; String newFile = srcUrl.getPath().replaceAll("01", "04A"); try { FastFileUtility.copyFile(srcFile, newFile); fail(); } catch (FileException e) { } }
@Test public void testCopyFile05() throws Exception { URL srcUrl = this.getClass().getResource("/testdata/test01.txt"); String srcFile = srcUrl.getPath(); String newFile = null; try { FastFileUtility.copyFile(srcFile, newFile); fail(); } catch (FileException e) { } }
@Test public void testCopyFile06() throws Exception { create3GBFile("/testdata", "test06.txt"); URL srcUrl = this.getClass().getResource("/testdata/test06.txt"); String srcFile = srcUrl.getPath(); String newFile = srcFile.replaceAll("test06\\.txt", "test06A\\.txt"); File newFileCheck = new File(newFile); if (newFileCheck.exists()) { newFileCheck.delete(); } FastFileUtility.copyFile(srcFile, newFile); try (FileInputStream fis1 = new FileInputStream(srcFile); BufferedInputStream expected = new BufferedInputStream(fis1); FileInputStream fis2 = new FileInputStream(newFile); BufferedInputStream actual = new BufferedInputStream(fis2);) { int ch = expected.read(); while (-1 != ch) { final int ch2 = actual.read(); if (ch != ch2) { fail(); } ch = expected.read(); } final int ch2 = actual.read(); assertEquals(-1, ch2); } }