Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement archivepath getfilename #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/main/java/io/mkr/archivefs/fs/ArchivePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,7 +36,7 @@ public Path getRoot() {

@Override
public Path getFileName() {
throw new UnsupportedOperationException();
return fileSystem.getPath(localPath.substring(localPath.lastIndexOf('/') + 1));
}

@Override
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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;
}

Expand All @@ -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();
}

Expand All @@ -153,7 +153,7 @@ public Iterator<Path> iterator() {
}

@Override
public int compareTo(Path other) {
public int compareTo(final Path other) {
return 0; //Todo
}

Expand All @@ -166,7 +166,7 @@ public String toString() {
return localPath;
}

public DirectoryStream<Path> newDirectoryStream(DirectoryStream.Filter<? super Path> filter) {
public DirectoryStream<Path> newDirectoryStream(final DirectoryStream.Filter<? super Path> filter) {
return fileSystem.newDirectoryStream(this, filter);
}

Expand Down
48 changes: 48 additions & 0 deletions src/test/java/io/mkr/archivefs/fs/ArchivePathTest.java
Original file line number Diff line number Diff line change
@@ -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())
);
}
}

}