Skip to content

Commit

Permalink
Bump IfExists methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marschall committed Dec 29, 2023
1 parent ebb0799 commit b7ba680
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ public void delete(Path path) throws IOException {
memoryFileSystem.delete(abstractPath);
}

@Override
public boolean deleteIfExists(Path path) throws IOException {
AbstractPath abstractPath = castPath(path);
// FIXME
return false;
}

@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
this.copyOrMove(source, target, TwoPathOperation.COPY, options);
Expand Down Expand Up @@ -429,6 +436,18 @@ public void checkAccess(Path path, AccessMode... modes) throws IOException {
memoryFileSystem.checkAccess(abstractPath, modes);
}

// new with JDK 20
@Override
public boolean exists(Path path, LinkOption... options) {
return false;
}

// new with JDK 20
@Override
public <A extends BasicFileAttributes> A readAttributesIfExists(Path path, Class<A> type, LinkOption... options) throws IOException {
return null;
}

@Override
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
AbstractPath abstractPath = castPath(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.OverlappingFileLockException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -882,17 +883,54 @@ void readSymlinkLoop() throws IOException {
"loop");
}



@Test
void dontDeleteOpenFile() throws IOException {
FileSystem fileSystem = this.extension.getFileSystem();
Path path = fileSystem.getPath("test");
try (SeekableByteChannel channel = Files.newByteChannel(path, CREATE_NEW, WRITE)) {
assertThrows(FileSystemException.class , () -> Files.delete(path), "you shound't be able to delete a file wile it's open");
assertThrows(FileSystemException.class , () -> Files.deleteIfExists(path), "you shound't be able to delete a file wile it's open");
}
}

@Test
void delete() throws IOException {
FileSystem fileSystem = this.extension.getFileSystem();
Path directory = fileSystem.getPath("directory");
Path path = directory.resolve("test");
assertSame(directory, Files.createDirectory(directory));
assertSame(path, Files.createFile(path));

assertThrows(DirectoryNotEmptyException.class, () -> Files.delete(directory), "you shound't be able to delete a non-emtpy directory");

Files.delete(path);
assertThat(path, not(exists()));
assertThrows(NoSuchFileException.class, () -> Files.delete(path), "you shound't be able to delete a non-existing file");

Files.delete(directory);
assertThat(directory, not(exists()));
assertThrows(NoSuchFileException.class, () -> Files.delete(directory), "you shound't be able to delete a non-existing directory");
}

@Test
void deleteIfExists() throws IOException {
FileSystem fileSystem = this.extension.getFileSystem();
Path directory = fileSystem.getPath("directory");
Path path = directory.resolve("test");
assertSame(directory, Files.createDirectory(directory));
assertSame(path, Files.createFile(path));

assertThrows(DirectoryNotEmptyException.class, () -> Files.delete(directory), "you shound't be able to delete a non-emtpy directory");

assertTrue(Files.deleteIfExists(path));
assertThat(path, not(exists()));
assertFalse(Files.deleteIfExists(path));

assertTrue(Files.deleteIfExists(directory));
assertThat(directory, not(exists()));
assertFalse(Files.deleteIfExists(directory));
}

@Test
void inputStream() throws IOException {
FileSystem fileSystem = this.extension.getFileSystem();
Expand Down

0 comments on commit b7ba680

Please sign in to comment.