Implemented missing tests for NioFolder

This commit is contained in:
Markus Kreusch
2015-12-30 17:05:02 +01:00
parent 1bf0c76918
commit db4b8955f4
4 changed files with 138 additions and 21 deletions

View File

@@ -41,6 +41,11 @@ class NioFile extends NioNode implements File {
return new WritableView();
}
@Override
public boolean exists() throws UncheckedIOException {
return false;
}
private class ReadableView implements ReadableFile {
@Override

View File

@@ -10,7 +10,7 @@ import java.util.Optional;
import org.cryptomator.filesystem.Folder;
import org.cryptomator.filesystem.Node;
class NioNode implements Node {
abstract class NioNode implements Node {
protected final Optional<NioFolder> parent;
protected final Path path;
@@ -46,11 +46,6 @@ class NioNode implements Node {
return parent;
}
@Override
public boolean exists() throws UncheckedIOException {
return Files.exists(path);
}
@Override
public Instant lastModified() throws UncheckedIOException {
try {

View File

@@ -1,5 +1,6 @@
package org.cryptomator.filesystem.nio;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.cryptomator.common.test.matcher.ContainsMatcher.containsInAnyOrder;
import static org.cryptomator.common.test.matcher.OptionalMatcher.presentOptionalWithValueThat;
@@ -397,9 +398,9 @@ public class NioFolderTest {
folder("folderToMove"), //
folder("folderToMove/subfolder1"), //
folder("folderToMove/subfolder2"), //
file("folderToMove/subfolder1/file1"), //
file("folderToMove/file2"), //
file("folderToMove/file3"));
file("folderToMove/subfolder1/file1").withData("dataOfFile1"), //
file("folderToMove/file2").withData("dataOfFile2"), //
file("folderToMove/file3").withData("dataOfFile3"));
NioFileSystem fileSystem = NioFileSystem.rootedAt(filesystemPath);
Folder folderToMove = fileSystem.folder("folderToMove");
Folder folderToMoveTo = fileSystem.folder("folderToMoveTo");
@@ -410,19 +411,69 @@ public class NioFolderTest {
assertThat(filesystemPath.resolve("folderToMoveTo"), isDirectory());
assertThat(filesystemPath.resolve("folderToMoveTo/subfolder1"), isDirectory());
assertThat(filesystemPath.resolve("folderToMoveTo/subfolder2"), isDirectory());
assertThat(filesystemPath.resolve("folderToMoveTo/subfolder1/file1"), isFile());
assertThat(filesystemPath.resolve("folderToMoveTo/file2"), isFile());
assertThat(filesystemPath.resolve("folderToMoveTo/file3"), isFile());
assertThat(filesystemPath.resolve("folderToMoveTo/subfolder1/file1"), isFile().withContent("dataOfFile1"));
assertThat(filesystemPath.resolve("folderToMoveTo/file2"), isFile().withContent("dataOfFile2"));
assertThat(filesystemPath.resolve("folderToMoveTo/file3"), isFile().withContent("dataOfFile3"));
}
@Test
public void testMoveToOfFolderToExistingFolderReplacesTargetFolder() throws IOException {
// TODO Markus Kreusch implement test
Path filesystemPath = testFilesystem( //
folder("folderToMove"), //
file("folderToMove/file1").withData("dataOfFile1"), //
file("folderToMove/file2").withData("dataOfFile2"), //
folder("folderToMoveTo"), //
file("folderToMoveTo/file1").withData("wrongDataOfFile1"), //
file("folderToMoveTo/fileWhichShouldNotExistAfterMove"));
NioFileSystem fileSystem = NioFileSystem.rootedAt(filesystemPath);
Folder folderToMove = fileSystem.folder("folderToMove");
Folder folderToMoveTo = fileSystem.folder("folderToMoveTo");
folderToMove.moveTo(folderToMoveTo);
assertThat(filesystemPath.resolve("folderToMove"), doesNotExist());
assertThat(filesystemPath.resolve("folderToMoveTo"), isDirectory());
assertThat(filesystemPath.resolve("folderToMoveTo/file1"), isFile().withContent("dataOfFile1"));
assertThat(filesystemPath.resolve("folderToMoveTo/file2"), isFile().withContent("dataOfFile2"));
assertThat(filesystemPath.resolve("folderToMoveTo/fileWhichShouldNotExistAfterMove"), doesNotExist());
}
@Test
public void testMoveToOfFolderToExistingFileThrowsUncheckedIOExceptionWithAbsolutePathOfTarget() throws IOException {
// TODO Markus Kreusch implement test
Path filesystemPath = testFilesystem( //
folder("folderToMove"), //
file("folderToMoveTo"));
FileSystem fileSystem = NioFileSystem.rootedAt(filesystemPath);
Folder folderToMove = fileSystem.folder("folderToMove");
Folder folderToMoveTo = fileSystem.folder("folderToMoveTo");
thrown.expect(UncheckedIOException.class);
thrown.expectMessage(filesystemPath.resolve("folderToMoveTo").toString());
folderToMove.moveTo(folderToMoveTo);
}
@Test
public void testMoveToOfFolderToFolderOfAnotherFileSystemDoesNothingAndhrowsIllegalArgumentException() throws IOException {
Path filesystemPath = testFilesystem(folder("folderToMove"));
Folder folderToMove = NioFileSystem.rootedAt(filesystemPath).folder("folderToMove");
Folder folderToMoveTo = NioFileSystem.rootedAt(filesystemPath).folder("folderToMoveTo");
thrown.expect(IllegalArgumentException.class);
folderToMove.moveTo(folderToMoveTo);
assertThat(filesystemPath.resolve("folderToMove"), isDirectory());
assertThat(filesystemPath.resolve("folderToMoveTo"), doesNotExist());
}
@Test
public void testToString() {
Path filesystemPath = emptyFilesystem();
Path pathOfFolder = filesystemPath.resolve("aFolder").toAbsolutePath();
Folder folder = NioFileSystem.rootedAt(filesystemPath).folder("aFolder");
assertThat(folder.toString(), is(format("NioFolder(%s)", pathOfFolder)));
}
}

View File

@@ -2,11 +2,16 @@ package org.cryptomator.filesystem.nio;
import static org.hamcrest.CoreMatchers.is;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import org.hamcrest.Description;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
class PathMatcher {
@@ -19,13 +24,8 @@ class PathMatcher {
};
}
public static Matcher<Path> isFile() {
return new FeatureMatcher<Path, Boolean>(is(true), "a path for which Files.isRegularFile", "Files.isRegularFile") {
@Override
protected Boolean featureValueOf(Path actual) {
return Files.isRegularFile(actual);
}
};
public static IsFileMatcher isFile() {
return IsFileMatcher.INSTANCE;
}
public static Matcher<Path> doesNotExist() {
@@ -46,4 +46,70 @@ class PathMatcher {
};
}
public static class IsFileMatcher extends FeatureMatcher<Path, Boolean> {
public static final IsFileMatcher INSTANCE = new IsFileMatcher();
private IsFileMatcher() {
super(is(true), "a path for which Files.isRegularFile", "Files.isRegularFile");
}
@Override
protected Boolean featureValueOf(Path actual) {
return Files.isRegularFile(actual);
}
public Matcher<Path> withContent(String value) {
return new IsFileWithContentMatcher(value.getBytes());
}
public Matcher<Path> withContent(byte[] value) {
return new IsFileWithContentMatcher(value);
}
}
public static class IsFileWithContentMatcher extends TypeSafeDiagnosingMatcher<Path> {
private final byte[] expectedContent;
public IsFileWithContentMatcher(byte[] content) {
this.expectedContent = content;
}
@Override
public void describeTo(Description description) {
description //
.appendText("a file with content ") //
.appendText(Arrays.toString(expectedContent));
}
@Override
protected boolean matchesSafely(Path path, Description mismatchDescription) {
if (!IsFileMatcher.INSTANCE.matches(path)) {
IsFileMatcher.INSTANCE.describeMismatch(path, mismatchDescription);
return false;
}
byte[] actualContent = getFileContent(path);
if (!Arrays.equals(actualContent, expectedContent)) {
mismatchDescription //
.appendText("a file with content ") //
.appendText(Arrays.toString(actualContent));
return false;
}
return true;
}
private byte[] getFileContent(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}