diff --git a/main/filesystem-inmemory/src/main/java/org/cryptomator/filesystem/inmem/InMemoryReadableFile.java b/main/filesystem-inmemory/src/main/java/org/cryptomator/filesystem/inmem/InMemoryReadableFile.java index e7055f200..c36371764 100644 --- a/main/filesystem-inmemory/src/main/java/org/cryptomator/filesystem/inmem/InMemoryReadableFile.java +++ b/main/filesystem-inmemory/src/main/java/org/cryptomator/filesystem/inmem/InMemoryReadableFile.java @@ -37,7 +37,8 @@ class InMemoryReadableFile implements ReadableFile { @Override public void copyTo(WritableFile other) throws UncheckedIOException { ByteBuffer source = contentGetter.get().asReadOnlyBuffer(); - source.position(position); + source.position(0); + other.truncate(); this.position += other.write(source); } diff --git a/main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileTests.java b/main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileTests.java new file mode 100644 index 000000000..ac8afdfa1 --- /dev/null +++ b/main/filesystem-invariants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileTests.java @@ -0,0 +1,54 @@ +package org.cryptomator.filesystem.invariants; + +import static org.cryptomator.filesystem.invariants.matchers.NodeMatchers.hasContent; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assume.assumeThat; + +import java.nio.ByteBuffer; + +import org.cryptomator.filesystem.File; +import org.cryptomator.filesystem.FileSystem; +import org.cryptomator.filesystem.WritableFile; +import org.cryptomator.filesystem.invariants.FileSystemFactories.FileSystemFactory; +import org.cryptomator.filesystem.invariants.WaysToObtainAFile.WayToObtainAFile; +import org.cryptomator.filesystem.invariants.WaysToObtainAFolder.WayToObtainAFolder; +import org.junit.Rule; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +@RunWith(Theories.class) +public class FileTests { + + @DataPoints + public static final Iterable FILE_SYSTEM_FACTORIES = new FileSystemFactories(); + + @DataPoints + public static final Iterable WAYS_TO_OBTAIN_A_FOLDER = new WaysToObtainAFolder(); + + @DataPoints + public static final Iterable WAYS_TO_OBTAIN_A_FILE = new WaysToObtainAFile(); + + private static final String FILE_NAME = "fileName"; + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + @Theory + public void testWriteToNonExistingFileCreatesFileWithContent(FileSystemFactory fileSystemFactory, WayToObtainAFile wayToObtainANonExistingFile) { + assumeThat(wayToObtainANonExistingFile.returnedFilesExist(), is(false)); + FileSystem fileSystem = fileSystemFactory.create(); + File file = wayToObtainANonExistingFile.fileWithName(fileSystem, FILE_NAME); + byte[] dataToWrite = new byte[] {42, -43, 111, 104, -3, 83, -99, 30}; + + try (WritableFile writable = file.openWritable()) { + writable.write(ByteBuffer.wrap(dataToWrite)); + } + + assertThat(file, hasContent(dataToWrite)); + } + +}