diff --git a/src/main/java/io/mkr/archivefs/fs/ArchivePath.java b/src/main/java/io/mkr/archivefs/fs/ArchivePath.java index fc6c56a..c26a7cb 100644 --- a/src/main/java/io/mkr/archivefs/fs/ArchivePath.java +++ b/src/main/java/io/mkr/archivefs/fs/ArchivePath.java @@ -9,11 +9,11 @@ public class ArchivePath implements Path { - private ArchiveFileSystem fileSystem; - private String localPath; - private PathSegments pathSegments; + private final ArchiveFileSystem fileSystem; + private final String localPath; + private final PathSegments pathSegments; - public ArchivePath(ArchiveFileSystem fileSystem, String localPath) { + public ArchivePath(final ArchiveFileSystem fileSystem, final String localPath) { this.fileSystem = fileSystem; this.localPath = localPath; this.pathSegments = new PathSegments(localPath); @@ -36,7 +36,7 @@ public Path getRoot() { @Override public Path getFileName() { - throw new UnsupportedOperationException(); + return fileSystem.getPath(localPath.substring(localPath.lastIndexOf('/') + 1)); } @Override @@ -50,32 +50,32 @@ public int getNameCount() { } @Override - public Path getName(int index) { + public Path getName(final int index) { return null; //Todo } @Override - public Path subpath(int beginIndex, int endIndex) { + public Path subpath(final int beginIndex, final int endIndex) { return null; //Todo } @Override - public boolean startsWith(Path other) { + public boolean startsWith(final Path other) { return false; //Todo } @Override - public boolean startsWith(String other) { + public boolean startsWith(final String other) { return false; //Todo } @Override - public boolean endsWith(Path other) { + public boolean endsWith(final Path other) { return false; //Todo } @Override - public boolean endsWith(String other) { + public boolean endsWith(final String other) { return false; //Todo } @@ -85,27 +85,27 @@ public Path normalize() { } @Override - public Path resolve(Path other) { + public Path resolve(final Path other) { return null; //Todo } @Override - public Path resolve(String other) { + public Path resolve(final String other) { return null; //Todo } @Override - public Path resolveSibling(Path other) { + public Path resolveSibling(final Path other) { return null; //Todo } @Override - public Path resolveSibling(String other) { + public Path resolveSibling(final String other) { return null; //Todo } @Override - public Path relativize(Path other) { + public Path relativize(final Path other) { return null; //Todo } @@ -128,7 +128,7 @@ public Path toAbsolutePath() { } @Override - public Path toRealPath(LinkOption... options) throws IOException { + public Path toRealPath(final LinkOption... options) throws IOException { return this; } @@ -138,12 +138,12 @@ public File toFile() { } @Override - public WatchKey register(WatchService watcher, WatchEvent.Kind[] events, WatchEvent.Modifier... modifiers) throws IOException { + public WatchKey register(final WatchService watcher, final WatchEvent.Kind[] events, final WatchEvent.Modifier... modifiers) throws IOException { throw new UnsupportedOperationException(); } @Override - public WatchKey register(WatchService watcher, WatchEvent.Kind... events) throws IOException { + public WatchKey register(final WatchService watcher, final WatchEvent.Kind... events) throws IOException { throw new UnsupportedOperationException(); } @@ -153,7 +153,7 @@ public Iterator iterator() { } @Override - public int compareTo(Path other) { + public int compareTo(final Path other) { return 0; //Todo } @@ -166,7 +166,7 @@ public String toString() { return localPath; } - public DirectoryStream newDirectoryStream(DirectoryStream.Filter filter) { + public DirectoryStream newDirectoryStream(final DirectoryStream.Filter filter) { return fileSystem.newDirectoryStream(this, filter); } diff --git a/src/test/java/io/mkr/archivefs/fs/ArchivePathTest.java b/src/test/java/io/mkr/archivefs/fs/ArchivePathTest.java new file mode 100644 index 0000000..48d4fae --- /dev/null +++ b/src/test/java/io/mkr/archivefs/fs/ArchivePathTest.java @@ -0,0 +1,48 @@ +package io.mkr.archivefs.fs; + +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ArchivePathTest { + + /** + * Provider for the archive file system implementation. + */ + private static final ArchiveFilesystemProvider afsp = new ArchiveFilesystemProvider(); + + @Test + void getFileName(@TempDir Path tempDir) throws IOException { + final Path testFile = tempDir.resolve("test.zip"); + try (final ZipArchiveOutputStream out = new ZipArchiveOutputStream(Files.newOutputStream(testFile))) { + final String content = "Test"; + final ZipArchiveEntry entry = new ZipArchiveEntry("test.txt"); + entry.setSize(content.length()); + out.putArchiveEntry(entry); + out.write(content.getBytes(StandardCharsets.UTF_8)); + out.closeArchiveEntry(); + } + + try (final FileSystem afs = afsp.newFileSystem(testFile, new HashMap<>())) { + final Path testFileInArchive = afs.getPath("/test.txt"); + final Path fileName = testFileInArchive.getFileName(); + assertAll( + () -> assertTrue(fileName instanceof ArchivePath), + () -> assertEquals("test.txt", fileName.toString()) + ); + } + } + +} \ No newline at end of file