-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
TempDirFactory implementations based on non-default file systems like jimfs and memoryfilesystem cannot work with @TempDir annotated elements of type File.
See also the Path::toFile Javadoc:
Throws:
UnsupportedOperationException- if thisPathis not associated with the default provider
Right now, a test like the example in the user guide, but with tempDir of type File:
@Test
void test(@TempDir(factory = JimfsTempDirFactory.class) File tempDir) {
// perform test
}
static class JimfsTempDirFactory implements TempDirFactory {
private final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
@Override
public Path createTempDirectory(AnnotatedElementContext elementContext, ExtensionContext extensionContext) throws IOException {
return Files.createTempDirectory(fileSystem.getPath("/"), "junit-");
}
@Override
public void close() throws IOException {
fileSystem.close();
}
}fails with:
org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [java.io.File arg0] in method [void io.github.scordio.jimfs.junit.jupiter.Tests.test(java.io.File)]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.UnsupportedOperationException
at com.google.common.jimfs.JimfsPath.toFile(JimfsPath.java:386)
... 2 more
I would like to improve the user experience, clearly stating that File annotated elements are not supported.
As of today, the factory doesn't have easy access to the type of the annotated element.
In my extension for Jimfs, I've implemented a workaround:
Deliverables
I see two possible directions:
- JUnit owns the error handling and asks the factory if
Fileannotated elements are supported- This could be done with a new default method in
TempDirFactory, e.g.,boolean supportsFile().
- This could be done with a new default method in
- The error handling happens in the factory
AnnotatedElementContextshould expose the element type somehow so that the factory can build up the error handling around it without having to cast the result ofgetAnnotatedElement(). For example, a newClass<?> getAnnotatedElementType()method could be added toAnnotatedElementContext.
My preference goes to option 1 to reduce duplication in each TempDirFactory implementation.
I'm happy to contribute once a solution is agreed.
Relates to:
- scordio/jimfs-junit-jupiter@99d946e
- JUnit5 test fails with: memory file system does not support #toFile() marschall/memoryfilesystem-junit-provider#2
CC @marschall