我有一个类来管理我们对其进行一些传入处理的一些文件的存档。在尝试测试归档过程时,我在JUnit中使用了TemporaryFolder规则。
在我们正常的Linux开发工作站上,测试运行没有任何问题,在一些Windows成员的笔记本电脑上有一些团队成员,但在我的Mac上我最终得到以下内容:
whenFullExtractReceivedArchivesArePurged(housekeeping.ArchiveHousekeeperIntegrationTest):无法删除/ var / folders / 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;
}
}
我不确定我是否会看到某种奇怪的许可问题或者是什么,这就是它的样子。我发现在测试创建文件夹时感到困惑,所以看起来应该对它有权限。
值得注意的是,测试中的代码似乎确实有效,在其他平台上测试运行成功。
答案 0 :(得分:0)
事实证明,原始问题中没有足够的细节。 ArchiveHousekeeper正在使用来自Guava的Files.recursivelyDelete。
文件删除的特定实现有一些保护措施可以阻止删除符号链接。 TemporaryFolder在Mac上的工作方式创建符号链接,它不会删除辅助存档的内容,因此删除实际文件夹失败。导致抛出异常。