I am using Apache Commons Virtual File System (VFS).
I am building some unit tests for my code and I need to modify the write permission for a folder, to make it not writeable. I tried the below code:
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Test
public void test_notWriteable() throws IOException {
// given
File dir = tempFolder.newFolder("testDirectory");
dir.setWritable(false, false);
when(fileApi.isHdfs()).thenReturn(false);
when(fileApi.getBaseDirectory()).thenReturn("file://" + dir.getAbsolutePath());
// when
String isBaseFolderAccessible = fileApi.isLocalBaseFolderAccessible();
// then
assertEquals("dashboard.basedir.not_writeable", isBaseFolderAccessible);
}
The method:
private String isLocalBaseFolderAccessible() {
try {
final FileSystemManager fileSystemManager = VFS.getManager();
final FileObject folder = fileSystemManager.resolveFile(fileApi.getBaseDirectory());
if (!folder.exists()) {
return BASEDIR_NOT_FOUND;
} else if (!folder.isReadable()) {
return BASEDIR_NOT_READABLE;
} else if (!folder.isWriteable()) {
return BASEDIR_NOT_WRITEABLE;
}
} catch (Exception exception) {
final String exceptionMessage = exception.getMessage();
logger.error("exception while trying to check base directory access: {}", exceptionMessage);
return SETTINGS_CHECK_BASEDIR_NOT_FOUND_I18N;
}
return null;
}
Unfortunately, the method returns null, so the folder is writeable.
The problem comes from the fact that isWriteable()
is called for AbstractFileObject
and it always returns true:
from AbstractFileObject
:
protected boolean doIsWriteable() throws Exception
{ return true; }
I also tried using ACLs for changing the permissions, but that also did not work. Does anyone know what I can do to change the folder permissions on Windows, with Java?