我有一个类管理一些文件的存档,我们做一些传入处理。在尝试测试归档过程时,我使用JUnit中的TemporaryFolder规则。无法删除由junit创建的文件夹TemporaryFolderRule
在我们正常的Linux开发站测试运行没有任何问题,在有些Windows笔记本电脑的一些团队成员具有的,但在我的Mac我最终得到如下:
whenFullExtractReceivedArchivesArePurged(housekeeping.ArchiveHousekeeperIntegrationTest):无法删除的/ var /文件夹/ PL/PlAbV + EpHhW2-nPFrFrwI ++++ TI/-Tmp-/junit5525189319546810170/secondaryArchive
下面是测试:
package housekeeping;
import static housekeeping.ArchiveHousekeeper.PRIMARY_ARCHIVE;
import static housekeeping.ArchiveHousekeeper.SECONDARY_ARCHIVE;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class ArchiveHousekeeperIntegrationTest {
private static final String LANDING_FOLDER = "landing";
private static final String PREVIOUS_FILE_NAME = "previousChild.txt";
private static final String CURRENT_FILE_NAME = "currentChild.txt";
private File primaryArchiveFolder;
private File secondaryArchiveFolder;
private File landingFolder;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void setUp() throws IOException {
temporaryFolder.getRoot();
primaryArchiveFolder = temporaryFolder.newFolder(PRIMARY_ARCHIVE);
secondaryArchiveFolder = temporaryFolder.newFolder(SECONDARY_ARCHIVE);
landingFolder = temporaryFolder.newFolder(LANDING_FOLDER);
createChildFile(primaryArchiveFolder, CURRENT_FILE_NAME);
createChildFile(secondaryArchiveFolder, PREVIOUS_FILE_NAME);
}
@Test
public void whenFullExtractReceivedArchivesArePurged() throws IOException {
final String extractFileName = "fullExtract0101.zip";
final File extractFile = new File(landingFolder, extractFileName);
extractFile.createNewFile();
final ArchiveHousekeeper housekeeper = new ArchiveHousekeeper(extractFile);
housekeeper.archiveFile();
assertThat(landingFolder.list().length, is(0));
assertThat(primaryArchiveFolder, containsFile(extractFileName));
assertThat(secondaryArchiveFolder, not(containsFile(PREVIOUS_FILE_NAME)));
assertThat(secondaryArchiveFolder, containsFile(CURRENT_FILE_NAME));
assertThat(primaryArchiveFolder, not(containsFile(CURRENT_FILE_NAME)));
}
@Factory
private static Matcher<File> containsFile(final String fileName) {
return new TypeSafeMatcher<File>() {
@Override
public void describeTo(final Description description) {
description.appendText("a directory containing " + fileName);
}
@Override
public boolean matchesSafely(final File item) {
return item != null && item.isDirectory() && Arrays.asList(item.list()).contains(fileName);
}
};
}
private File createChildFile(final File parent, final String child) throws IOException {
final File folder = new File(parent, child);
folder.createNewFile();
return folder;
}
}
我不知道如果我我是出现某种奇怪的权限问题或什么,这就是它的样子。当测试创建文件夹时,我只是觉得这很令人困惑,所以它似乎应该有权限。
值得注意的是,在其他平台上,测试下的代码似乎可以正常工作。