FileControlImpl implements FileControl { @Override public void copyFile(String srcFile, String newFile) { FileUtility .copyFile(getAbsolutePath(srcFile), getAbsolutePath(newFile)); }  @Override void copyFile(String srcFile, String newFile); @Override void deleteFile(String srcFile); @Override void mergeFile(List<String> fileList, String newFile); @Override void renameFile(String srcFile, String newFile); String getBasePath(); void setBasePath(String basePath); void setCheckFileExist(boolean checkFileExist);  }
@Test public void testCopyFile01() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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"; File file = new File(srcFile); file.delete(); file.createNewFile(); file = new File(newFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> fileControlStringArguments = fileControlStringArgumentCaptor .getAllValues(); assertEquals(2, fileControlStringArguments.size()); assertEquals(srcFile, fileControlStringArguments.get(0)); assertEquals(newFile, fileControlStringArguments.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> fileUtilityStringArguments = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, fileUtilityStringArguments.get(0)); assertEquals(newFile, fileUtilityStringArguments.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testCopyFile02() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 = "testCopyFile02_src.txt"; String newFile = "testCopyFile02_new.txt"; File file = new File(directoryPath + srcFile); file.delete(); file.createNewFile(); file = new File(directoryPath + newFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> fileControlStringArguments = fileControlStringArgumentCaptor .getAllValues(); assertEquals(2, fileControlStringArguments.size()); assertEquals(srcFile, fileControlStringArguments.get(0)); assertEquals(newFile, fileControlStringArguments.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> fileUtilityStringArguments = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(basePath + srcFile, fileUtilityStringArguments.get(0)); assertEquals(basePath + newFile, fileUtilityStringArguments.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testCopyFile03() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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"; File file = new File(srcFile); file.delete(); file.createNewFile(); file = new File(newFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> fileControlStringArguments = fileControlStringArgumentCaptor .getAllValues(); assertEquals(2, fileControlStringArguments.size()); assertEquals(srcFile, fileControlStringArguments.get(0)); assertEquals(newFile, fileControlStringArguments.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> fileUtilityStringArguments = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, fileUtilityStringArguments.get(0)); assertEquals(newFile, fileUtilityStringArguments.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testCopyFile04() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 = "testCopyFile04_src.txt"; String newFile = "testCopyFile04_new.txt"; File file = new File(directoryPath + srcFile); file.delete(); file.createNewFile(); file = new File(directoryPath + newFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> fileControlStringArguments = fileControlStringArgumentCaptor .getAllValues(); assertEquals(2, fileControlStringArguments.size()); assertEquals(srcFile, fileControlStringArguments.get(0)); assertEquals(newFile, fileControlStringArguments.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> fileUtilityStringArguments = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, fileUtilityStringArguments.get(0)); assertEquals(newFile, fileUtilityStringArguments.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testCopyFile05() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 + "testCopyFile05_new.txt"; File file = new File(newFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { PowerMockito.verifyPrivate(fileControl, Mockito.times(1)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String fileControlStringArgument = fileControlStringArgumentCaptor .getValue(); assertNull(fileControlStringArgument); assertEquals(FileException.class, e.getClass()); assertEquals("File name is not set.", e.getMessage()); assertNull(e.getFileName()); PowerMockito.verifyStatic(Mockito.never()); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); } finally { file = new File(newFile); file.delete(); } }
@Test public void testCopyFile06() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 = null; File file = new File(srcFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String fileControlStringArgument = fileControlStringArgumentCaptor .getValue(); assertNull(fileControlStringArgument); assertEquals(FileException.class, e.getClass()); assertEquals("File name is not set.", e.getMessage()); assertNull(e.getFileName()); PowerMockito.verifyStatic(Mockito.never()); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); } finally { file = new File(srcFile); file.delete(); } }
@Test public void testCopyFile07() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 = "testCopyFile07_src.txt"; String newFile = directoryPath + "testCopyFile07_new.txt"; File file = new File(directoryPath + srcFile); file.delete(); file.createNewFile(); file = new File(newFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argListGetAbsolutePath = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListGetAbsolutePath.get(0)); assertEquals(newFile, argListGetAbsolutePath.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> argListCopyFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(basePath + srcFile, argListCopyFile.get(0)); assertEquals(newFile, argListCopyFile.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testCopyFile08() throws Exception { FileControlImpl fileControl = PowerMockito.spy(new FileControlImpl()); 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 + "testCopyFile08_src.txt"; String newFile = "testCopyFile08_new.txt"; File file = new File(srcFile); file.delete(); file.createNewFile(); file = new File(directoryPath + newFile); file.delete(); file.createNewFile(); String basePath = directoryPath; ReflectionTestUtils.setField(fileControl, "basePath", basePath); PowerMockito.mockStatic(FileUtility.class); ArgumentCaptor<String> fileUtilityStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doNothing().when(FileUtility.class, "copyFile", fileUtilityStringArgumentCaptor.capture(), fileUtilityStringArgumentCaptor.capture()); Method getAbsolutePathMethod = PowerMockito.method( FileControlImpl.class, "getAbsolutePath", String.class); ArgumentCaptor<String> fileControlStringArgumentCaptor = ArgumentCaptor .forClass(String.class); PowerMockito.doCallRealMethod().when(fileControl, getAbsolutePathMethod) .withArguments(fileControlStringArgumentCaptor.capture()); try { fileControl.copyFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argListGetAbsolutePath = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListGetAbsolutePath.get(0)); assertEquals(newFile, argListGetAbsolutePath.get(1)); PowerMockito.verifyStatic(); FileUtility.copyFile(Mockito.anyString(), Mockito.anyString()); List<String> argListCopyFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListCopyFile.get(0)); assertEquals(basePath + newFile, argListCopyFile.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }