Skip to content

Commit

Permalink
Implemented missing tests for NioFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
markuskreusch committed Dec 30, 2015
1 parent 1bf0c76 commit db4b895
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public WritableFile openWritable() throws UncheckedIOException {
return new WritableView();
}

@Override
public boolean exists() throws UncheckedIOException {
return false;
}

private class ReadableView implements ReadableFile {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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;
Expand Down Expand Up @@ -46,11 +46,6 @@ public Optional<? extends Folder> parent() throws UncheckedIOException {
return parent;
}

@Override
public boolean exists() throws UncheckedIOException {
return Files.exists(path);
}

@Override
public Instant lastModified() throws UncheckedIOException {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -397,9 +398,9 @@ public void testMoveToOfFolderWithChildrenMovesFolderAndChildren() throws IOExce
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");
Expand All @@ -410,19 +411,69 @@ public void testMoveToOfFolderWithChildrenMovesFolderAndChildren() throws IOExce
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)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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 {

Expand All @@ -19,13 +24,8 @@ protected Boolean featureValueOf(Path actual) {
};
}

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() {
Expand All @@ -46,4 +46,70 @@ protected Boolean featureValueOf(Path actual) {
};
}

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);
}
}

}

}

0 comments on commit db4b895

Please sign in to comment.