Java 创建无法由文件删除的文件。删除()

Java 创建无法由文件删除的文件。删除(),java,file,junit4,Java,File,Junit4,注意:我知道有几个问题与此类似,但是,我找不到任何解释如何解决我试图解决的情况的问题。我将用一个具体的例子来问这个问题,我需要一个解决方案 以代码为例: private final void writeToFile(final File parent, final String filename, final Charset charset, final String content) throws IOException { final File file = new File(par

注意:我知道有几个问题与此类似,但是,我找不到任何解释如何解决我试图解决的情况的问题。我将用一个具体的例子来问这个问题,我需要一个解决方案

以代码为例:

private final void writeToFile(final File parent, final String filename, final Charset charset, final String content) throws IOException {
    final File file = new File(parent, filename);

    if (file.exists()) {
        LOG.warn("File {} already exists, file will be replaced.", file.getCanonicalPath());
        if (!file.delete()) {
            logAndThrow(String.format("Cannot delete file '%s'.", file.getCanonicalPath()), null);
        }
    }

    try (final FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter writer = new OutputStreamWriter(fos, charset)) {
        writer.write(content);
    }
}
我试图编写一个单元测试,以在代码无法删除文件时引发IOException。我尝试过的单元测试如下:

@Test public void testFileNotDeletable() throws IOException {
  final File file = new File(folder.getRoot(), formattedFile.getMetaData().getFormattedCaptureFileName());
  file.createNewFile();
  try {
    file.setReadOnly();

    exception.expect(IOException.class);
    exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));

    writer.write(formattedFile);
  } finally {
    file.setWritable(true);
  }
}
我还尝试锁定该文件:

@Test public void testFileNotDeletable() throws IOException {
    final File file = new File(folder.getRoot(), formattedFile.getMetaData().getFormattedCaptureFileName());
    file.createNewFile();
    try (FileInputStream fis = new FileInputStream(file)) {
        final FileLock lock = fis.getChannel().tryLock(0L, Long.MAX_VALUE, true);
        try {
            exception.expect(IOException.class);
            exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));

            writer.write(formattedFile);
        } finally {
            lock.release();
        }
    }
}
无论我尝试什么,file.delete()都会成功删除该文件,并且测试失败,因为没有引发预期的IOException

非常感谢您的帮助


注意:为了澄清起见,添加了一些额外的代码,表明文件对象在环境中是完全独立的。传递给write方法的formattedFile不是File或File的子类,而是我们的内部类之一。JUnit测试中的文件使用根目录的临时文件夹,formattedFile有一个元数据项,它确定文件名。在JUnit测试中,我试图在实际代码尝试编写文件的位置创建一个不能删除的空文件。我需要file.delete()返回false,以便测试抛出的异常。因此,我无法模拟文件对象。

为了防止文件被删除,您必须拒绝windows中的安全权限。从UI中,我们需要执行以下操作

  • 右键单击电脑中的文件或文档=>选择属性
  • 在安全中,选项卡编辑以更改权限=>选择添加并输入所有人
  • 按OK并选择要将完全控制权限更改为拒绝的组
  • 按Yes确认。
  • 据我所知,使用Java更改文件权限的唯一方法是:

    file.setExecutable(true|false);
    file.setReadable(true|false);
    file.setWritable(true|false);
    

    上述操作使/backups/passwd文件不可变(或不可删除)。这意味着文件不能以任何方式修改:不能删除或重命名。您甚至无法创建指向它的链接,也无法将任何数据写入该文件。 这大概是我唯一能想到的


    希望这有帮助

    我完全同意图灵85关于使用mockito的说法

    假设您有一个原始类,其方法与您要测试的方法类似:

    public class FileDel {
    
        public void logOnIOException(File file) throws IOException {
            if (file.exists()) {
                LOG.warn("File {} already exists, file will be replaced.", file.getCanonicalPath());
                if (!file.delete()) {
                    logAndThrow(String.format("Cannot delete file '%s'.", file.getCanonicalPath()), null);
                }
            }
        }
    
        public void logAndThrow(String msg, String s) {
            //Do nothing
        }
    
        private static class LOG {
            public static void warn(String msg, String path) {
            }
        }
    }
    
    然后可以通过以下方式触发内部异常:

    @RunWith(MockitoJUnitRunner.class)
    public class FileDelTest {
        @Test(expected = IOException.class)
        public void testFileNotDeletable() throws IOException {
            File file = mock(File.class);
            when(file.exists()).thenReturn(true);
            when(file.delete()).thenAnswer(new Answer<Boolean>() {
                @Override
                public Boolean answer(InvocationOnMock iom) throws Throwable {
                    throw new IOException();
                }
            });
    
            FileDel f = new FileDel();
    
            try {
                f.methodToTest(file);
            } finally {
            }
        }
    }
    
    @RunWith(MockitoJUnitRunner.class)
    公共类FileDelTest{
    @测试(预期=IOException.class)
    public void testFileNotDeletable()引发IOException{
    File=mock(File.class);
    当(file.exists())。然后返回(true);
    当(file.delete())。然后回答(new Answer()){
    @凌驾
    公共布尔应答(InvocationMock iom)抛出可丢弃的{
    抛出新IOException();
    }
    });
    FileDel f=新FileDel();
    试一试{
    f、 方法测试(文件);
    }最后{
    }
    }
    }
    
    如何打开此文件的
    InputStream
    ,而不要关闭它。除非文件的描述符不会被关闭,否则文件不会被删除。

    对于您的问题,有两种解决方案,我推荐第一种

    • 解决方案1 这里不是测试java文件I/O操作/类,而是测试代码响应文件操作的功能行为。因此,理想情况下,在JUnit中,您应该模拟
      文件
      对象及其各自的调用,并且只关注测试代码

    • 解决方案2 如果您仍然希望测试与java文件IO的完全集成,请在尝试删除之前以写模式打开文件,它将负责您的测试用例

    注:代码在CENTOS、WINDOWS、UBUNTU、MAC OS-X中测试

    科目类别:

        public class FileSolution {
            public void fileHandler(File file) throws IOException, Exception {
                if (file.exists()) {
                    LOG.warn("File {} already exists, file will be replaced.", 
                            file.getCanonicalPath());
                    if (!file.delete()) {
                        logAndThrow(String.format("Cannot delete file '%s'.", 
                                file.getCanonicalPath()),
                                new IOException(String.format("Cannot delete file '%s'.", 
                                        file.getCanonicalPath())));
                    }
                }
            }
        }
    
    import static org.mockito.BDDMockito.*;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ExpectedException;
    
    public class FileSolutionTest {
    
        @Rule
        public final ExpectedException exception = ExpectedException.none();
    
        /**
         * Solution 1
         * 
         * @throws Exception
         */
        @Test
        public void testFileNotDeletableWithMock() throws Exception {
            final File file = mock(File.class);
            file.createNewFile();
            // mock file & IO operations
            given(file.exists()).willReturn(true);
            given(file.delete()).willReturn(false);
            given(file.getCanonicalPath()).willReturn("test.txt");
    
            exception.expect(IOException.class);
            exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
    
            new FileSolution().fileHandler(file);
        }
    
        /**
         * Solution 2
         * 
         * @throws Exception
         */
        @Test
        public void testFileNotDeletable() throws Exception {
            File file = null;
            FileWriter fileWriter = null;
            try{
                file = new File("test.txt");
                file.createNewFile();
                file.deleteOnExit();
                exception.expect(IOException.class);
                exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
                // open file with another process for writing
                fileWriter = new FileWriter(file, true);
                new FileSolution().fileHandler(file);
            } finally{
                if(fileWriter != null){
                    fileWriter.flush();
                    fileWriter.close();
                }
            }
        }
    }
    
    受试者未完成测试:

        public class FileSolution {
            public void fileHandler(File file) throws IOException, Exception {
                if (file.exists()) {
                    LOG.warn("File {} already exists, file will be replaced.", 
                            file.getCanonicalPath());
                    if (!file.delete()) {
                        logAndThrow(String.format("Cannot delete file '%s'.", 
                                file.getCanonicalPath()),
                                new IOException(String.format("Cannot delete file '%s'.", 
                                        file.getCanonicalPath())));
                    }
                }
            }
        }
    
    import static org.mockito.BDDMockito.*;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.rules.ExpectedException;
    
    public class FileSolutionTest {
    
        @Rule
        public final ExpectedException exception = ExpectedException.none();
    
        /**
         * Solution 1
         * 
         * @throws Exception
         */
        @Test
        public void testFileNotDeletableWithMock() throws Exception {
            final File file = mock(File.class);
            file.createNewFile();
            // mock file & IO operations
            given(file.exists()).willReturn(true);
            given(file.delete()).willReturn(false);
            given(file.getCanonicalPath()).willReturn("test.txt");
    
            exception.expect(IOException.class);
            exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
    
            new FileSolution().fileHandler(file);
        }
    
        /**
         * Solution 2
         * 
         * @throws Exception
         */
        @Test
        public void testFileNotDeletable() throws Exception {
            File file = null;
            FileWriter fileWriter = null;
            try{
                file = new File("test.txt");
                file.createNewFile();
                file.deleteOnExit();
                exception.expect(IOException.class);
                exception.expectMessage(String.format("Cannot delete file '%s'.", file.getCanonicalPath()));
                // open file with another process for writing
                fileWriter = new FileWriter(file, true);
                new FileSolution().fileHandler(file);
            } finally{
                if(fileWriter != null){
                    fileWriter.flush();
                    fileWriter.close();
                }
            }
        }
    }
    

    在Linux中,您可以使用chattr命令设置一个“不可变”的文件,即使根用户也不能删除该文件。还有人说“设置文件权限”是正确的,但没有给出具体细节

    干杯
    D

    模拟调用
    file.delete()
    。您使用旧的文件api而不是围绕
    文件
    路径
    路径
    旋转的现代NIO有何原因?使用不允许删除的文件权限。我确实查看了文件界面,然而,我不想使用抛出IOException的机制,我想使用一个已经封装了逻辑的机制,如果成功,只返回true/false。Files.deleteIfExists()不能满足我的需要。有些操作系统允许删除文件,即使该文件是在另一个进程中打开的。Linux就是其中之一。在测试中打开并锁定文件不会阻止Linux删除它。是的,你是对的,因此删除了操作系统特定的注释。Linux允许删除打开的文件。我不能模拟文件对象,它与单元测试完全分离。我需要标记实际文件,以便无法删除它,但file.delete()总是成功(看起来如此)。嗨,Marcus。这件事越来越有趣了。你想在不同的操作系统上为你的应用程序创建一个真实的测试,并将现有文件真正锁定吗?或者更确切地说,你想模仿这种行为(文件删除引发IOException),但你目前在这个方向上没有成功?什么是PC中的右键点击。我是一个使用Linux的开发人员。将文件permissons设置为000不会产生任何解决方案。files.delete()仍然删除该文件。