FileUtility { public static void copyFile(String srcFile, String newFile) { checkAbsolutePath(srcFile); checkAbsolutePath(newFile); File srcFileObject = new File(srcFile); if (!srcFileObject.exists()) { throw new FileException(srcFile + " is not exist.", srcFile); } File newFileObject = new File(newFile); 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); } FileOutputStream fos = null; FileChannel outputFileChannel = null; FileInputStream ios = null; FileChannel inputFileChannel = null; FileLock inputFileLock = null; FileLock outputFileLock = null; try { fos = new FileOutputStream(newFileObject, true); outputFileChannel = fos.getChannel(); ios = new FileInputStream(srcFileObject); inputFileChannel = ios.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); inputFileLock = inputFileChannel.lock(0L, Long.MAX_VALUE, true); outputFileLock = outputFileChannel.lock(0L, Long.MAX_VALUE, false); while (inputFileChannel.position() < inputFileChannel.size()) { buffer.clear(); inputFileChannel.read(buffer); buffer.flip(); outputFileChannel.write(buffer); } } catch (FileNotFoundException e) { throw new FileException("File control operation was failed.", e); } catch (IOException e) { throw new FileException("File control operation was failed.", e); } finally { try { if (inputFileLock != null) { inputFileLock.release(); } if (outputFileLock != null) { outputFileLock.release(); } if (ios != null) { ios.close(); } if (fos != null) { fos.close(); } if (outputFileChannel != null) { outputFileChannel.close(); } if (inputFileChannel != null) { inputFileChannel.close(); } } catch (IOException e) { } } }  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);  }
@Ignore @Test public void _ignore_testCopyFile01() 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 + "testCopyFile01_src.txt"; String newFile = directoryPath + "testCopyFile01_new.txt"; ReflectionTestUtils.setField(FileUtility.class, "checkFileExist", false); File testSrcFile = new File(srcFile); testSrcFile.delete(); testSrcFile.createNewFile(); File testNewFile = new File(newFile); testNewFile.delete(); FileWriter testSrcFileFileWriter = null; PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile01_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); FileUtility.copyFile(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 getFile = new File(newFile); FileContent testFileContent = new FileContent(testSrcFile); FileContent fileContent = new FileContent(getFile); assertTrue(testFileContent.equals(fileContent)); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testCopyFile02() 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 + "testCopyFile02_src.txt"; String newFile = directoryPath + "testCopyFile02_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; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile02_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile02_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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 getFile = new File(newFile); FileContent testFileContent = new FileContent(testSrcFile); FileContent fileContent = new FileContent(getFile); assertTrue(testFileContent.equals(fileContent)); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } File file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testCopyFile03() 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 + "testCopyFile03_src.txt"; String newFile = directoryPath + "testCopyFile03_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("testCopyFile03_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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 resultFile = new File(newFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile03_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 testCopyFile04() 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 + "testCopyFile04_src.txt"; String newFile = directoryPath + "testCopyFile04_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("testCopyFile04_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile04_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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 resultFile = new File(newFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile04_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 (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 testCopyFile05() 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 + "testCopyFile05_src.txt"; String newFile = directoryPath + "testCopyFile05_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(); FileNotFoundException fileNotFoundException = new FileNotFoundException( "testCopyFile05例外"); PowerMockito.spy(FileWriter.class); PowerMockito.whenNew(FileOutputStream.class).withArguments(testNewFile, true).thenThrow(fileNotFoundException); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile05_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile05_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File control operation was failed.", e.getMessage()); assertSame(fileNotFoundException, e.getCause()); 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 file = new File(newFile); assertFalse(file.exists()); } 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 testCopyFile06() 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 + "testCopyFile06_src.txt"; String newFile = directoryPath + "testCopyFile06_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(); IOException exception = new IOException("testCopyFile06例外"); FileChannel fileChannel = PowerMockito.mock(FileChannel.class, Mockito.CALLS_REAL_METHODS); Mockito.doNothing().when(fileChannel).close(); Mockito.when(fileChannel.position()).thenThrow(exception); FileInputStream fis = Mockito.mock(FileInputStream.class, Mockito.RETURNS_DEFAULTS); Mockito.when(fis.getChannel()).thenReturn(fileChannel); PowerMockito.spy(FileInputStream.class); PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(fis); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile06_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile06_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { assertTrue(FileException.class.isAssignableFrom(e.getClass())); assertEquals("File control operation was failed.", e.getMessage()); assertSame(exception, e.getCause()); 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 resultFile = new File(newFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); 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 testCopyFile07() 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 + "testCopyFile07_src.txt"; String newFile = directoryPath + "testCopyFile07_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(); final IOException exception = new IOException("testCopyFile07例外"); FileOutputStream fos = Mockito.mock(FileOutputStream.class, Mockito.RETURNS_MOCKS); Mockito.doThrow(exception).when(fos).close(); PowerMockito.spy(FileOutputStream.class); PowerMockito.whenNew(FileOutputStream.class).withArguments(testNewFile, true).thenReturn(fos); FileWriter testSrcFileFileWriter = null; FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile07_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile07_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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); } catch (Exception e) { e.printStackTrace(); } 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 testCopyFile08() 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 + "testCopyFile08_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(); IOException exception = new IOException("TEST"); FileLock fileLock = PowerMockito.mock(FileLock.class, Mockito.CALLS_REAL_METHODS); Mockito.doThrow(exception).when(fileLock).release(); FileChannel fileChannel = PowerMockito.mock(FileChannel.class, Mockito.CALLS_REAL_METHODS); Mockito.doNothing().when(fileChannel).close(); Mockito.when(fileChannel.lock()).thenReturn(fileLock); FileInputStream fis = Mockito.mock(FileInputStream.class, Mockito.RETURNS_DEFAULTS); Mockito.when(fis.getChannel()).thenReturn(fileChannel); PowerMockito.spy(FileInputStream.class); PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(fis); FileWriter testNewFileFileWriter = null; BufferedReader postReader = null; try { testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile08_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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); } finally { if (testNewFileFileWriter != null) { testNewFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(newFile); file.delete(); } }
@Test public void testCopyFile09() 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 + "testCopyFile09_src.txt"; String newFile = "testCopyFile09_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("testCopyFile09_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile09_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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 resultFile = new File(directoryPath + newFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile09_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 (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } if (postReader != null) { postReader.close(); } File file = new File(srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testCopyFile10() 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 + "testCopyFile10_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; try { testSrcFileFileWriter = new FileWriter(testSrcFile); testSrcFileFileWriter.write("testCopyFile10_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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); } finally { if (testSrcFileFileWriter != null) { testSrcFileFileWriter.close(); } File file = new File(srcFile); file.delete(); } }
@Test public void testCopyFile11() 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 = "testCopyFile10_src.txt"; String newFile = directoryPath + "testCopyFile10_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("testCopyFile11_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile11_new.txtデータ"); testNewFileFileWriter.flush(); testNewFileFileWriter.close(); PowerMockito.mockStatic(FileUtility.class, Mockito.CALLS_REAL_METHODS); FileUtility.copyFile(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 resultFile = new File(newFile); assertTrue(resultFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(resultFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile11_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(directoryPath + srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Ignore @Test public void _ignore_testCopyFile12() 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 + "testCopyFile12_src.txt"; String newFile = directoryPath + "testCopyFile12_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("testCopyFile02_src.txtデータ"); testSrcFileFileWriter.flush(); testSrcFileFileWriter.close(); testNewFileFileWriter = new FileWriter(testNewFile); testNewFileFileWriter.write("testCopyFile02_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.copyFile(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); lock.release(); lock.release(); File getFile = new File(newFile); assertTrue(getFile.exists()); postReader = new BufferedReader(new InputStreamReader( new FileInputStream(getFile))); assertTrue(postReader.ready()); String expectationResultData = "testCopyFile02_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()); postReader.close(); } 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(); } }