FileControlImpl implements FileControl { @Override public void renameFile(String srcFile, String newFile) { FileUtility.renameFile(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 testRenameFile01() 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 + "testRenameFile01_src.txt"; String newFile = directoryPath + "testRenameFile01_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListRenameFile.get(0)); assertEquals(newFile, argListRenameFile.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile02() 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 = "testRenameFile02_src.txt"; String newFile = "testRenameFile02_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(basePath + srcFile, argListRenameFile.get(0)); assertEquals(basePath + newFile, argListRenameFile.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testRenameFile03() 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 + "testRenameFile03_src.txt"; String newFile = directoryPath + "testRenameFile03_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListRenameFile.get(0)); assertEquals(newFile, argListRenameFile.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile04() 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 = "testRenameFile04_src.txt"; String newFile = "testRenameFile04_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListRenameFile.get(0)); assertEquals(newFile, argListRenameFile.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }
@Test public void testRenameFile05() 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 + "testRenameFile05_new.txt"; File file = new File(newFile); file.delete(); file.createNewFile(); String basePath = ""; ReflectionTestUtils.setField(fileControl, "basePath", basePath); 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.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { PowerMockito.verifyPrivate(fileControl).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); String argGetAbsolutePath = fileControlStringArgumentCaptor .getValue(); assertEquals(srcFile, argGetAbsolutePath); assertEquals(FileException.class, e.getClass()); assertEquals("File name is not set.", e.getMessage()); assertNull(e.getFileName()); } finally { file = new File(newFile); file.delete(); } }
@Test public void testRenameFile06() 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 + "testRenameFile06_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); PowerMockito.doNothing().when(FileUtility.class, "renameFile", Mockito .anyString(), Mockito.anyString()); 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.renameFile(srcFile, newFile); fail("FileExceptionが発生しませんでした。失敗です。"); } catch (FileException e) { PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(Mockito.never()); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); assertEquals(FileException.class, e.getClass()); assertEquals("File name is not set.", e.getMessage()); assertNull(e.getFileName()); } finally { file = new File(srcFile); file.delete(); } }
@Test public void testRenameFile07() 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 = "testRenameFile07_src.txt"; String newFile = directoryPath + "testRenameFile07_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(basePath + srcFile, argListRenameFile.get(0)); assertEquals(newFile, argListRenameFile.get(1)); } finally { file = new File(directoryPath + srcFile); file.delete(); file = new File(newFile); file.delete(); } }
@Test public void testRenameFile08() 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 + "testRenameFile08_src.txt"; String newFile = "testRenameFile08_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, "renameFile", 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.renameFile(srcFile, newFile); PowerMockito.verifyPrivate(fileControl, Mockito.times(2)).invoke( getAbsolutePathMethod).withArguments(Mockito.anyString()); List<String> argGetAbsolutePathList = fileControlStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argGetAbsolutePathList.get(0)); assertEquals(newFile, argGetAbsolutePathList.get(1)); PowerMockito.verifyStatic(); FileUtility.renameFile(Mockito.anyString(), Mockito.anyString()); List<String> argListRenameFile = fileUtilityStringArgumentCaptor .getAllValues(); assertEquals(srcFile, argListRenameFile.get(0)); assertEquals(basePath + newFile, argListRenameFile.get(1)); } finally { file = new File(srcFile); file.delete(); file = new File(directoryPath + newFile); file.delete(); } }