FileUtility { public static void renameFile(String srcFile, String newFile) { checkAbsolutePath(srcFile); checkAbsolutePath(newFile); File srcFileObject = new File(srcFile); File newFileObject = new File(newFile); if (!srcFileObject.exists()) { throw new FileException(srcFile + " is not exist.", srcFile); } if (newFileObject.exists() && checkFileExist) { boolean result = newFileObject.delete(); if (!result) { throw new FileException("File control operation was failed.", newFile); } } else if (newFileObject.exists() && !checkFileExist) { throw new FileException(newFile + " is exist.", newFile); } boolean result = true; result = srcFileObject.renameTo(newFileObject); if (!result) { throw new FileException("File control operation was failed."); } }  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 testRenameFile01() 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 + "testRenameFile01_src.txt"; String newFile = directoryPath + "testRenameFile01_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, false); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); FileWriter testSrcFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile01_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertFalse(removeFile.exists()); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile01_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(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile02() 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 + "testRenameFile02_src.txt"; String newFile = directoryPath + "testRenameFile02_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, true); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile02_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile02_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertFalse(removeFile.exists()); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile02_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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile03() 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 + "testRenameFile03_src.txt"; String newFile = directoryPath + "testRenameFile03_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, false); File testSrcFile = new File(srcFile); testSrcFile.delete(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile03_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals(srcFile + " is not exist.", e.getMessage()); assertEquals(srcFile, e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile03_new.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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile04() 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 + "testRenameFile04_src.txt"; String newFile = directoryPath + "testRenameFile04_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, false); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile04_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile04_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals(newFile + " is exist.", e.getMessage()); assertEquals(newFile, e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile04_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()); postReader.close(); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); expectationResultData = "testRenameFile04_new.txtデータ"; 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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Ignore @Test public void _ignore_testRenameFile05() 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 + "testRenameFile05_src.txt"; String newFile = directoryPath + "testRenameFile05_new.txt"; ReflectionTestUtils.setField(FileUtility.class, "checkFileExist", true); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; FileInputStream fis = null; FileLock lock = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile05_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); fis = new FileInputStream(testSrcFile); lock = fis.getChannel().lock(0L, Long.MAX_VALUE, true); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile05_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File control operation was failed.", e.getMessage()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); lock.release(); File removeFile = new File(srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile05_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()); postReader.close(); File renameFile = new File(newFile); assertFalse(renameFile.exists()); } finally { if (lock != null) { lock.release(); } if (fis != null) { fis.close(); } if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile06() 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 = null; String newFile = directoryPath + "testRenameFile06_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, true); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile06_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not set.", e.getMessage()); assertNull(e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile06_new.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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(newFile); file.delete(); } }
@Test public void testRenameFile07() 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 + "testRenameFile07_src.txt"; String newFile = "testRenameFile07_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, true); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(directoryPath + newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile07_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile07_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not absolute.", e.getMessage()); assertEquals(newFile, e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile07_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()); postReader.close(); File renameFile = new File(directoryPath + newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); expectationResultData = "testRenameFile07_new.txtデータ"; 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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testRenameFile08() 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 + "testRenameFile08_src.txt"; String newFile = null; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, true); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); FileWriter testSrcFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile08_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not set.", e.getMessage()); assertNull(e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile08_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()); postReader.close(); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); } }
@Test public void testRenameFile09() 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 = "testRenameFile09_src.txt"; String newFile = directoryPath + "testRenameFile09_new.txt"; Field field = FileUtility.class.getDeclaredField("checkFileExist"); field.setAccessible(true); field.set(FileUtility.class, true); File testSrcFile = new File(directoryPath + srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile09_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile09_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File path is not absolute.", e.getMessage()); assertEquals(srcFile, e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); File removeFile = new File(directoryPath + srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile09_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()); postReader.close(); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); expectationResultData = "testRenameFile09_new.txtデータ"; 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 (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(directoryPath + srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@SuppressWarnings("resource") @Ignore @Test public void _ignore_testRenameFile10() 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 + "testRenameFile10_src.txt"; String newFile = directoryPath + "testRenameFile10_new.txt"; ReflectionTestUtils.setField(FileUtility.class, "checkFileExist", true); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); testNewFile.createNewFile(); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; FileInputStream fis = null; FileLock lock = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testRenameFile10_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testRenameFile10_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); fis = new FileInputStream(testNewFile); lock = fis.getChannel().lock(0L, Long.MAX_VALUE, true); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File control operation was failed.", e.getMessage()); assertEquals(newFile, e.getFileName()); PowerMockito.verifyPrivate(FileUtility.class, Mockito.times(2)).invoke("checkAbsolutePath", Mockito.anyString()); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", srcFile); PowerMockito.verifyPrivate(FileUtility.class).invoke("checkAbsolutePath", newFile); File removeFile = new File(srcFile); assertTrue(removeFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(removeFile))); assertTrue(postReader.ready()); String expectationResultData = "testRenameFile10_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()); File renameFile = new File(newFile); assertTrue(renameFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(renameFile))); assertTrue(postReader.ready()); expectationResultData = "testRenameFile10_new.txtデータ"; 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 (lock != null) { lock.release(); } if (fis != null) { fis.close(); } if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }