I run test case and save files created during execution in 'execution' directory (files from each tests in separated subdirectory) At the beginning of suite I want to clean this folder
So in @BeforeSuite method I read all items from 'execution' directory:
public static List<File> getRecursivelyAllFilesFromDirectory(String directory) throws IOException {
List<File> files = new ArrayList<>();
files.addAll(readAllFilesFromDirectory(directory));
List<File> subDirs = readSubdirectories(directory);
subDirs.forEach(dir -> {
try {
files.addAll(readAllFilesFromDirectory(dir.getPath()));
files.add(dir);
} catch (IOException e) {
e.printStackTrace();
}
});
return files;
}
and then I want to delete everything
public static void cleanExecutionDirectory() throws IOException {
LOGGER.info("Cleaning execution directory...");
String executionDir = getExecutionDir();
List<File> files = FileUtils.getRecursivelyAllFilesFromDirectory(executionDir);
for (File file : files) {
file.delete();
}
LOGGER.info(String.format("%d items deleted", files.size()));
}
But then next in @BeforeScenario I try to create subfolder with same name for new execution
public static void setTestCaseDir(String testCaseName) throws IOException {
testCaseDir = testCaseName.split(StringUtils.SPACE)[0];
String executionDir = getExecutionDir();
Path testCaseDirPath = Paths.get(executionDir, testCaseDir);
boolean isSubDir = readSubdirectories(executionDir)
.stream()
.filter(subDir -> subDir.getPath().equals(testCaseDirPath))
.findFirst()
.isPresent();
if (!isSubDir) {
Files.createDirectory(testCaseDirPath);
}
testCaseDir = testCaseDirPath.toString();
}
I receive an eexception:
java.nio.file.AccessDeniedException: D:\...\src\test\resources\execution\TC01
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:504)
at java.nio.file.Files.createDirectory(Files.java:674)
at utils.ExecutionUtils.setTestCaseDir(ExecutionUtils.java:56)
at integration.steps.StepDefs.beforeScenario(StepDefs.java:29)
It happens although readSubdirectories() in setTestCaseDir method returns empty list. I see in file explorer that this subdirectories are visible but also with I can't access them. When this execution failed due to this exception this subdfolders are unlocked and disappears.
What process might locked this subfolders and how to solve this problem ?