FileUtility { public static void deleteFile(String srcFile) { checkAbsolutePath(srcFile); File srcFileObject = new File(srcFile); if (!srcFileObject.exists()) { throw new FileException(srcFile + " is not exist.", srcFile); } boolean result = srcFileObject.delete(); if (!result) { throw new FileException("File control operation was failed.", srcFile); } }  static void copyFile(String srcFile, String newFile); static void deleteFile(String srcFile); static void mergeFile(List<String> fileList, String newFile); static void renameFile(String srcFile, String newFile); static boolean isCheckFileExist(); static void setCheckFileExist(boolean checkFileExist);  }
@Test public void testDeleteFile01() throws Exception { String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = directoryPath + "testDeleteFile01_src.txt"; File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); FileWriter testSrcFileFileWriter = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile01_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); FileUtility.deleteFile(srcFile); File resultFile = new File(srcFile); assertFalse(resultFile.exists()); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } File file = new File(srcFile); file.delete(); } }
@Test public void testDeleteFile02() throws Exception { String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = directoryPath + "testDeleteFile01_src.txt"; File testSrcFile = new File(srcFile); testSrcFile.delete(); try { FileUtility.deleteFile(srcFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals(srcFile + " is not exist.", e.getMessage()); assertEquals(srcFile, e.getFileName()); } finally { File file = new File(srcFile); file.delete(); } }
@Test public void testDeleteFile03() throws Exception { String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = directoryPath + "testDeleteFile03_src.txt"; File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File f = PowerMockito.mock(File.class, Mockito.RETURNS_MOCKS); Mockito.when(f.delete()).thenReturn(false); Mockito.when(f.isAbsolute()).thenReturn(true); Mockito.when(f.exists()).thenReturn(true); PowerMockito.whenNew(File.class).withArguments(srcFile).thenReturn(f); FileWriter testSrcFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile03_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); FileUtility.deleteFile(srcFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File control operation was failed.", e.getMessage()); assertEquals(srcFile, e.getFileName()); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); } }
@Test public void testDeleteFile04() throws Exception { String srcFile = null; try { FileUtility.deleteFile(srcFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not set.", e.getMessage()); assertNull(e.getFileName()); } }
@Test public void testDeleteFile05() throws Exception { String classFileName = this.getClass().getSimpleName() + ".class"; URL url = this.getClass().getResource(classFileName); String directoryPath = url.getPath().substring(0, url.getPath().length() - classFileName.length()); String srcFile = "testDeleteFile05_src.txt"; File testSrcFile = new File(directoryPath + srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); FileWriter testSrcFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile05_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); FileUtility.deleteFile(srcFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not absolute.", e.getMessage()); assertEquals(srcFile, e.getFileName()); File resultFile = new File(directoryPath + srcFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile05_src.txtデータ"; String data = ""; for (int i = 0; i < expectationResultData.length(); i++) { assertTrue(i + "回目の判定で失敗しました。", postReader.ready()); data += (char) postReader.read(); } assertEquals(expectationResultData, data); assertFalse(postReader.ready()); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(directoryPath + srcFile); file.delete(); } }